2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2006, 2007, 2008, 2009 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 "InternalFunction.h"
28 #include "JSDestructibleObject.h"
30 #include "ObjectAllocationProfile.h"
31 #include "Watchpoint.h"
36 class FunctionExecutable
;
37 class FunctionPrototype
;
40 class LLIntOffsetsExtractor
;
41 class NativeExecutable
;
48 JS_EXPORT_PRIVATE EncodedJSValue JSC_HOST_CALL
callHostFunctionAsConstructor(ExecState
*);
50 JS_EXPORT_PRIVATE String
getCalculatedDisplayName(CallFrame
*, JSObject
*);
52 class JSFunction
: public JSDestructibleObject
{
54 friend class DFG::SpeculativeJIT
;
55 friend class DFG::JITCompiler
;
59 typedef JSDestructibleObject Base
;
61 JS_EXPORT_PRIVATE
static JSFunction
* create(VM
&, JSGlobalObject
*, int length
, const String
& name
, NativeFunction
, Intrinsic
= NoIntrinsic
, NativeFunction nativeConstructor
= callHostFunctionAsConstructor
);
63 static JSFunction
* create(VM
& vm
, FunctionExecutable
* executable
, JSScope
* scope
)
65 JSFunction
* function
= new (NotNull
, allocateCell
<JSFunction
>(vm
.heap
)) JSFunction(vm
, executable
, scope
);
66 ASSERT(function
->structure()->globalObject());
67 function
->finishCreation(vm
);
71 static JSFunction
* createBuiltinFunction(VM
&, FunctionExecutable
*, JSGlobalObject
*);
73 static void destroy(JSCell
*);
75 JS_EXPORT_PRIVATE String
name(ExecState
*);
76 JS_EXPORT_PRIVATE String
displayName(ExecState
*);
77 const String
calculatedDisplayName(ExecState
*);
81 ASSERT(!isHostFunctionNonInline());
84 // This method may be called for host functins, in which case it
85 // will return an arbitrary value. This should only be used for
86 // optimized paths in which the return value does not matter for
87 // host functions, and checking whether the function is a host
88 // function is deemed too expensive.
89 JSScope
* scopeUnchecked()
93 void setScope(VM
& vm
, JSScope
* scope
)
95 ASSERT(!isHostFunctionNonInline());
96 m_scope
.set(vm
, this, scope
);
98 void addNameScopeIfNeeded(VM
&);
100 ExecutableBase
* executable() const { return m_executable
.get(); }
102 // To call either of these methods include Executable.h
103 bool isHostFunction() const;
104 FunctionExecutable
* jsExecutable() const;
106 JS_EXPORT_PRIVATE
const SourceCode
* sourceCode() const;
110 static Structure
* createStructure(VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
112 ASSERT(globalObject
);
113 return Structure::create(vm
, globalObject
, prototype
, TypeInfo(JSFunctionType
, StructureFlags
), info());
116 NativeFunction
nativeFunction();
117 NativeFunction
nativeConstructor();
119 static ConstructType
getConstructData(JSCell
*, ConstructData
&);
120 static CallType
getCallData(JSCell
*, CallData
&);
122 static inline ptrdiff_t offsetOfScopeChain()
124 return OBJECT_OFFSETOF(JSFunction
, m_scope
);
127 static inline ptrdiff_t offsetOfExecutable()
129 return OBJECT_OFFSETOF(JSFunction
, m_executable
);
132 static inline ptrdiff_t offsetOfAllocationProfile()
134 return OBJECT_OFFSETOF(JSFunction
, m_allocationProfile
);
137 ObjectAllocationProfile
* allocationProfile(ExecState
* exec
, unsigned inlineCapacity
)
139 if (UNLIKELY(m_allocationProfile
.isNull()))
140 return createAllocationProfile(exec
, inlineCapacity
);
141 return &m_allocationProfile
;
144 Structure
* allocationStructure() { return m_allocationProfile
.structure(); }
146 InlineWatchpointSet
& allocationProfileWatchpointSet()
148 return m_allocationProfileWatchpoint
;
151 bool isHostOrBuiltinFunction() const;
152 bool isBuiltinFunction() const;
153 JS_EXPORT_PRIVATE
bool isHostFunctionNonInline() const;
156 const static unsigned StructureFlags
= OverridesGetOwnPropertySlot
| ImplementsHasInstance
| OverridesVisitChildren
| OverridesGetPropertyNames
| JSObject::StructureFlags
;
158 JS_EXPORT_PRIVATE
JSFunction(VM
&, JSGlobalObject
*, Structure
*);
159 JSFunction(VM
&, FunctionExecutable
*, JSScope
*);
161 void finishCreation(VM
&, NativeExecutable
*, int length
, const String
& name
);
162 using Base::finishCreation
;
164 ObjectAllocationProfile
* createAllocationProfile(ExecState
*, size_t inlineCapacity
);
166 static bool getOwnPropertySlot(JSObject
*, ExecState
*, PropertyName
, PropertySlot
&);
167 static void getOwnNonIndexPropertyNames(JSObject
*, ExecState
*, PropertyNameArray
&, EnumerationMode
= ExcludeDontEnumProperties
);
168 static bool defineOwnProperty(JSObject
*, ExecState
*, PropertyName
, const PropertyDescriptor
&, bool shouldThrow
);
170 static void put(JSCell
*, ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
172 static bool deleteProperty(JSCell
*, ExecState
*, PropertyName
);
174 static void visitChildren(JSCell
*, SlotVisitor
&);
177 friend class LLIntOffsetsExtractor
;
179 static EncodedJSValue
argumentsGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
180 static EncodedJSValue
callerGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
181 static EncodedJSValue
lengthGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
182 static EncodedJSValue
nameGetter(ExecState
*, JSObject
*, EncodedJSValue
, PropertyName
);
184 WriteBarrier
<ExecutableBase
> m_executable
;
185 WriteBarrier
<JSScope
> m_scope
;
186 ObjectAllocationProfile m_allocationProfile
;
187 InlineWatchpointSet m_allocationProfileWatchpoint
;
192 #endif // JSFunction_h