2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2006, 2007, 2008, 2009, 2015 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
5 * Copyright (C) 2007 Maks Orlovich
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.
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.
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
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
27 #include "FunctionRareData.h"
28 #include "InternalFunction.h"
31 #include "Watchpoint.h"
36 class FunctionExecutable
;
37 class FunctionPrototype
;
38 class JSLexicalEnvironment
;
40 class LLIntOffsetsExtractor
;
41 class NativeExecutable
;
48 typedef std::function
<EncodedJSValue (ExecState
*)> NativeStdFunction
;
50 JS_EXPORT_PRIVATE EncodedJSValue JSC_HOST_CALL
callHostFunctionAsConstructor(ExecState
*);
52 JS_EXPORT_PRIVATE String
getCalculatedDisplayName(CallFrame
*, JSObject
*);
54 class JSFunction
: public JSCallee
{
56 friend class DFG::SpeculativeJIT
;
57 friend class DFG::JITCompiler
;
61 typedef JSCallee Base
;
62 const static unsigned StructureFlags
= Base::StructureFlags
| OverridesGetOwnPropertySlot
| OverridesGetPropertyNames
;
64 static size_t allocationSize(size_t inlineCapacity
)
66 ASSERT_UNUSED(inlineCapacity
, !inlineCapacity
);
67 return sizeof(JSFunction
);
70 JS_EXPORT_PRIVATE
static JSFunction
* create(VM
&, JSGlobalObject
*, int length
, const String
& name
, NativeFunction
, Intrinsic
= NoIntrinsic
, NativeFunction nativeConstructor
= callHostFunctionAsConstructor
);
72 static JSFunction
* createWithInvalidatedReallocationWatchpoint(VM
&, FunctionExecutable
*, JSScope
*);
73 JS_EXPORT_PRIVATE
static JSFunction
* create(VM
&, JSGlobalObject
*, int length
, const String
& name
, NativeStdFunction
&&, Intrinsic
= NoIntrinsic
, NativeFunction nativeConstructor
= callHostFunctionAsConstructor
);
75 static JSFunction
* create(VM
&, FunctionExecutable
*, JSScope
*);
77 static JSFunction
* createBuiltinFunction(VM
&, FunctionExecutable
*, JSGlobalObject
*);
78 static JSFunction
* createBuiltinFunction(VM
&, FunctionExecutable
*, JSGlobalObject
*, const String
& name
);
80 JS_EXPORT_PRIVATE String
name(ExecState
*);
81 JS_EXPORT_PRIVATE String
displayName(ExecState
*);
82 const String
calculatedDisplayName(ExecState
*);
84 ExecutableBase
* executable() const { return m_executable
.get(); }
86 // To call either of these methods include Executable.h
87 bool isHostFunction() const;
88 FunctionExecutable
* jsExecutable() const;
90 JS_EXPORT_PRIVATE
const SourceCode
* sourceCode() const;
94 static Structure
* createStructure(VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
97 return Structure::create(vm
, globalObject
, prototype
, TypeInfo(JSFunctionType
, StructureFlags
), info());
100 NativeFunction
nativeFunction();
101 NativeFunction
nativeConstructor();
103 static ConstructType
getConstructData(JSCell
*, ConstructData
&);
104 static CallType
getCallData(JSCell
*, CallData
&);
106 static inline ptrdiff_t offsetOfExecutable()
108 return OBJECT_OFFSETOF(JSFunction
, m_executable
);
111 static inline ptrdiff_t offsetOfRareData()
113 return OBJECT_OFFSETOF(JSFunction
, m_rareData
);
116 FunctionRareData
* rareData(ExecState
* exec
, unsigned inlineCapacity
)
118 if (UNLIKELY(!m_rareData
))
119 return allocateAndInitializeRareData(exec
, inlineCapacity
);
120 if (UNLIKELY(!m_rareData
->isInitialized()))
121 return initializeRareData(exec
, inlineCapacity
);
122 return m_rareData
.get();
125 FunctionRareData
* rareData()
127 FunctionRareData
* rareData
= m_rareData
.get();
129 // The JS thread may be concurrently creating the rare data
130 // If we see it, we want to ensure it has been properly created
131 WTF::loadLoadFence();
136 bool isHostOrBuiltinFunction() const;
137 bool isBuiltinFunction() const;
138 JS_EXPORT_PRIVATE
bool isHostFunctionNonInline() const;
139 bool isClassConstructorFunction() const;
142 JS_EXPORT_PRIVATE
JSFunction(VM
&, JSGlobalObject
*, Structure
*);
143 JSFunction(VM
&, FunctionExecutable
*, JSScope
*);
145 void finishCreation(VM
&, NativeExecutable
*, int length
, const String
& name
);
146 using Base::finishCreation
;
148 FunctionRareData
* allocateAndInitializeRareData(ExecState
*, size_t inlineCapacity
);
149 FunctionRareData
* initializeRareData(ExecState
*, size_t inlineCapacity
);
151 static bool getOwnPropertySlot(JSObject
*, ExecState
*, PropertyName
, PropertySlot
&);
152 static void getOwnNonIndexPropertyNames(JSObject
*, ExecState
*, PropertyNameArray
&, EnumerationMode
= EnumerationMode());
153 static bool defineOwnProperty(JSObject
*, ExecState
*, PropertyName
, const PropertyDescriptor
&, bool shouldThrow
);
155 static void put(JSCell
*, ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
157 static bool deleteProperty(JSCell
*, ExecState
*, PropertyName
);
159 static void visitChildren(JSCell
*, SlotVisitor
&);
162 static JSFunction
* createImpl(VM
& vm
, FunctionExecutable
* executable
, JSScope
* scope
)
164 JSFunction
* function
= new (NotNull
, allocateCell
<JSFunction
>(vm
.heap
)) JSFunction(vm
, executable
, scope
);
165 ASSERT(function
->structure()->globalObject());
166 function
->finishCreation(vm
);
170 friend class LLIntOffsetsExtractor
;
172 static EncodedJSValue
argumentsGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
173 static EncodedJSValue
callerGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
174 static EncodedJSValue
lengthGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
175 static EncodedJSValue
nameGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
177 WriteBarrier
<ExecutableBase
> m_executable
;
178 WriteBarrier
<FunctionRareData
> m_rareData
;
183 #endif // JSFunction_h