blob: 4d3f131ce9694785e533517936927beccaba2a92 [file] [log] [blame]
kocienda66a6d362001-08-24 14:24:45 +00001/*
kocienda66a6d362001-08-24 14:24:45 +00002 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
fpizlo@apple.comc5108b02015-02-23 18:13:41 +00003 * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2015 Apple Inc. All rights reserved.
ggaren6c0384f2007-10-29 05:32:23 +00004 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
5 * Copyright (C) 2007 Maks Orlovich
kocienda66a6d362001-08-24 14:24:45 +00006 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
mjscdff33b2006-01-23 21:41:36 +000019 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
ggaren07d4ce62005-07-14 18:27:04 +000020 * Boston, MA 02110-1301, USA.
mjs6f821c82002-03-22 00:31:57 +000021 *
kocienda66a6d362001-08-24 14:24:45 +000022 */
23
darin@apple.com5c0863d2008-06-16 04:17:44 +000024#ifndef JSFunction_h
25#define JSFunction_h
kocienda66a6d362001-08-24 14:24:45 +000026
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +000027#include "FunctionRareData.h"
barraclough@apple.com484a9d32012-03-22 18:54:50 +000028#include "InternalFunction.h"
msaboff@apple.com42142a12014-09-11 21:52:33 +000029#include "JSCallee.h"
ggaren@apple.comb11e7872012-08-30 22:50:00 +000030#include "JSScope.h"
fpizlo@apple.comf5db15e2012-11-14 07:22:57 +000031#include "Watchpoint.h"
kocienda66a6d362001-08-24 14:24:45 +000032
cwzwarich@webkit.org3f782f62008-09-08 01:28:33 +000033namespace JSC {
kocienda66a6d362001-08-24 14:24:45 +000034
mark.lam@apple.com188640e2014-09-04 19:10:36 +000035class ExecutableBase;
36class FunctionExecutable;
37class FunctionPrototype;
oliver@apple.coma7dfb4d2014-09-11 18:18:14 +000038class JSLexicalEnvironment;
mark.lam@apple.com188640e2014-09-04 19:10:36 +000039class JSGlobalObject;
40class LLIntOffsetsExtractor;
41class NativeExecutable;
42class SourceCode;
commit-queue@webkit.orgfa196632015-08-28 21:07:22 +000043class WebAssemblyExecutable;
mark.lam@apple.com188640e2014-09-04 19:10:36 +000044namespace DFG {
45class SpeculativeJIT;
46class JITCompiler;
47}
48
49JS_EXPORT_PRIVATE EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState*);
50
51JS_EXPORT_PRIVATE String getCalculatedDisplayName(CallFrame*, JSObject*);
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +000052
msaboff@apple.com42142a12014-09-11 21:52:33 +000053class JSFunction : public JSCallee {
mark.lam@apple.com188640e2014-09-04 19:10:36 +000054 friend class JIT;
55 friend class DFG::SpeculativeJIT;
56 friend class DFG::JITCompiler;
57 friend class VM;
58
59public:
msaboff@apple.com42142a12014-09-11 21:52:33 +000060 typedef JSCallee Base;
akling@apple.com4b9e0002015-04-13 19:12:48 +000061 const static unsigned StructureFlags = Base::StructureFlags | OverridesGetOwnPropertySlot | OverridesGetPropertyNames;
mark.lam@apple.com188640e2014-09-04 19:10:36 +000062
commit-queue@webkit.org14aa0542015-04-17 00:44:58 +000063 static size_t allocationSize(size_t inlineCapacity)
64 {
65 ASSERT_UNUSED(inlineCapacity, !inlineCapacity);
66 return sizeof(JSFunction);
67 }
68
mark.lam@apple.com188640e2014-09-04 19:10:36 +000069 JS_EXPORT_PRIVATE static JSFunction* create(VM&, JSGlobalObject*, int length, const String& name, NativeFunction, Intrinsic = NoIntrinsic, NativeFunction nativeConstructor = callHostFunctionAsConstructor);
fpizlo@apple.com3a2fa4c2015-04-13 22:13:12 +000070
71 static JSFunction* createWithInvalidatedReallocationWatchpoint(VM&, FunctionExecutable*, JSScope*);
mark.lam@apple.com188640e2014-09-04 19:10:36 +000072
fpizlo@apple.com3a2fa4c2015-04-13 22:13:12 +000073 static JSFunction* create(VM&, FunctionExecutable*, JSScope*);
commit-queue@webkit.orgfa196632015-08-28 21:07:22 +000074#if ENABLE(WEBASSEMBLY)
75 static JSFunction* create(VM&, WebAssemblyExecutable*, JSScope*);
76#endif
bdash53ebcaf2007-05-30 12:19:36 +000077
youenn.fablet@crf.canon.frfafcd042015-09-24 09:18:15 +000078 JS_EXPORT_PRIVATE static JSFunction* createBuiltinFunction(VM&, FunctionExecutable*, JSGlobalObject*);
utatane.tea@gmail.comde247cf2015-06-16 02:11:41 +000079 static JSFunction* createBuiltinFunction(VM&, FunctionExecutable*, JSGlobalObject*, const String& name);
barraclough@apple.comfd8c28a2010-05-25 03:04:43 +000080
mark.lam@apple.com188640e2014-09-04 19:10:36 +000081 JS_EXPORT_PRIVATE String name(ExecState*);
82 JS_EXPORT_PRIVATE String displayName(ExecState*);
83 const String calculatedDisplayName(ExecState*);
commit-queue@webkit.org6c25c522011-08-09 20:46:17 +000084
mark.lam@apple.com188640e2014-09-04 19:10:36 +000085 ExecutableBase* executable() const { return m_executable.get(); }
barraclough@apple.com4be541c2010-05-20 02:38:01 +000086
keith_miller@apple.com59bba5d2015-10-16 22:18:42 +000087 // To call any of these methods include JSFunctionInlines.h
mark.lam@apple.com188640e2014-09-04 19:10:36 +000088 bool isHostFunction() const;
89 FunctionExecutable* jsExecutable() const;
keith_miller@apple.com59bba5d2015-10-16 22:18:42 +000090 Intrinsic intrinsic() const;
mjsa84805f2006-03-15 10:21:48 +000091
mark.lam@apple.com188640e2014-09-04 19:10:36 +000092 JS_EXPORT_PRIVATE const SourceCode* sourceCode() const;
barraclough@apple.com12710b62009-08-21 21:54:20 +000093
mark.lam@apple.com188640e2014-09-04 19:10:36 +000094 DECLARE_EXPORT_INFO;
weinig@apple.com0b4376b2008-11-28 02:24:20 +000095
mark.lam@apple.com188640e2014-09-04 19:10:36 +000096 static Structure* createStructure(VM& vm, JSGlobalObject* globalObject, JSValue prototype)
97 {
98 ASSERT(globalObject);
99 return Structure::create(vm, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), info());
100 }
yurys@chromium.org0114f052011-11-14 09:27:25 +0000101
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000102 NativeFunction nativeFunction();
103 NativeFunction nativeConstructor();
mjs308be5a2006-08-14 03:06:14 +0000104
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000105 static ConstructType getConstructData(JSCell*, ConstructData&);
106 static CallType getCallData(JSCell*, CallData&);
darin@apple.com74e6ed62008-10-23 00:11:11 +0000107
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000108 static inline ptrdiff_t offsetOfExecutable()
109 {
110 return OBJECT_OFFSETOF(JSFunction, m_executable);
111 }
mjs308be5a2006-08-14 03:06:14 +0000112
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000113 static inline ptrdiff_t offsetOfRareData()
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000114 {
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000115 return OBJECT_OFFSETOF(JSFunction, m_rareData);
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000116 }
oliver@apple.come843bc02011-08-05 20:03:19 +0000117
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000118 FunctionRareData* rareData(ExecState* exec, unsigned inlineCapacity)
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000119 {
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000120 if (UNLIKELY(!m_rareData))
commit-queue@webkit.orga89ef272015-04-22 17:39:44 +0000121 return allocateAndInitializeRareData(exec, inlineCapacity);
122 if (UNLIKELY(!m_rareData->isInitialized()))
123 return initializeRareData(exec, inlineCapacity);
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000124 return m_rareData.get();
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000125 }
oliver@apple.come843bc02011-08-05 20:03:19 +0000126
commit-queue@webkit.org311b1e12015-04-23 21:56:23 +0000127 FunctionRareData* rareData()
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000128 {
commit-queue@webkit.org311b1e12015-04-23 21:56:23 +0000129 FunctionRareData* rareData = m_rareData.get();
commit-queue@webkit.orga60da232015-04-21 18:50:16 +0000130
commit-queue@webkit.org311b1e12015-04-23 21:56:23 +0000131 // The JS thread may be concurrently creating the rare data
132 // If we see it, we want to ensure it has been properly created
133 WTF::loadLoadFence();
134
135 return rareData;
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000136 }
barraclough@apple.comcef11dc2012-05-10 18:40:29 +0000137
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000138 bool isHostOrBuiltinFunction() const;
139 bool isBuiltinFunction() const;
140 JS_EXPORT_PRIVATE bool isHostFunctionNonInline() const;
joepeck@webkit.org31c46502015-03-27 01:42:37 +0000141 bool isClassConstructorFunction() const;
barraclough@apple.comcef11dc2012-05-10 18:40:29 +0000142
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000143protected:
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000144 JS_EXPORT_PRIVATE JSFunction(VM&, JSGlobalObject*, Structure*);
145 JSFunction(VM&, FunctionExecutable*, JSScope*);
commit-queue@webkit.orga4201b02015-08-17 22:24:20 +0000146 JSFunction(VM&, FunctionExecutable*, JSScope*, Structure*);
oliver@apple.come9eda782009-10-17 01:06:40 +0000147
commit-queue@webkit.orgfa196632015-08-28 21:07:22 +0000148#if ENABLE(WEBASSEMBLY)
149 JSFunction(VM&, WebAssemblyExecutable*, JSScope*);
150#endif
151
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000152 void finishCreation(VM&, NativeExecutable*, int length, const String& name);
153 using Base::finishCreation;
commit-queue@webkit.org53aecd22011-08-19 00:58:34 +0000154
commit-queue@webkit.orga89ef272015-04-22 17:39:44 +0000155 FunctionRareData* allocateAndInitializeRareData(ExecState*, size_t inlineCapacity);
156 FunctionRareData* initializeRareData(ExecState*, size_t inlineCapacity);
barraclough@apple.comcef11dc2012-05-10 18:40:29 +0000157
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000158 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
utatane.tea@gmail.com69af1962015-04-02 18:53:32 +0000159 static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode = EnumerationMode());
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000160 static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
barraclough@apple.com22215662011-09-22 21:22:17 +0000161
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000162 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
mhahnenberg@apple.com61a11712011-10-22 00:48:40 +0000163
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000164 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
mhahnenberg@apple.comc751c3e2011-10-24 00:21:52 +0000165
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000166 static void visitChildren(JSCell*, SlotVisitor&);
barraclough@apple.com22215662011-09-22 21:22:17 +0000167
utatane.tea@gmail.com4218f532015-08-28 21:35:39 +0000168
169 static NativeExecutable* lookUpOrCreateNativeExecutable(VM&, NativeFunction, Intrinsic, NativeFunction nativeConstructor);
170
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000171private:
fpizlo@apple.com3a2fa4c2015-04-13 22:13:12 +0000172 static JSFunction* createImpl(VM& vm, FunctionExecutable* executable, JSScope* scope)
173 {
174 JSFunction* function = new (NotNull, allocateCell<JSFunction>(vm.heap)) JSFunction(vm, executable, scope);
175 ASSERT(function->structure()->globalObject());
176 function->finishCreation(vm);
177 return function;
178 }
179
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000180 friend class LLIntOffsetsExtractor;
weinig@apple.com0e2d66e2008-07-06 05:26:58 +0000181
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000182 static EncodedJSValue argumentsGetter(ExecState*, JSObject*, EncodedJSValue, PropertyName);
183 static EncodedJSValue callerGetter(ExecState*, JSObject*, EncodedJSValue, PropertyName);
184 static EncodedJSValue lengthGetter(ExecState*, JSObject*, EncodedJSValue, PropertyName);
185 static EncodedJSValue nameGetter(ExecState*, JSObject*, EncodedJSValue, PropertyName);
186
187 WriteBarrier<ExecutableBase> m_executable;
commit-queue@webkit.orgcc894b02015-04-16 19:15:09 +0000188 WriteBarrier<FunctionRareData> m_rareData;
mark.lam@apple.com188640e2014-09-04 19:10:36 +0000189};
kocienda66a6d362001-08-24 14:24:45 +0000190
ddkilzer@apple.comf9f6bbd2009-01-02 20:59:17 +0000191} // namespace JSC
mjs70d74212005-08-07 06:17:49 +0000192
weinig@apple.com7dab4652008-07-05 23:19:36 +0000193#endif // JSFunction_h