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(ExecState
*, JSGlobalObject
*, int length
, const String
& name
, NativeFunction
, Intrinsic
= NoIntrinsic
, NativeFunction nativeConstructor
= callHostFunctionAsConstructor
);
63 static JSFunction
* create(ExecState
* exec
, FunctionExecutable
* executable
, JSScope
* scope
)
66 JSFunction
* function
= new (NotNull
, allocateCell
<JSFunction
>(vm
.heap
)) JSFunction(vm
, executable
, scope
);
67 ASSERT(function
->structure()->globalObject());
68 function
->finishCreation(vm
);
72 static void destroy(JSCell
*);
74 JS_EXPORT_PRIVATE String
name(ExecState
*);
75 JS_EXPORT_PRIVATE String
displayName(ExecState
*);
76 const String
calculatedDisplayName(ExecState
*);
80 ASSERT(!isHostFunctionNonInline());
83 // This method may be called for host functins, in which case it
84 // will return an arbitrary value. This should only be used for
85 // optimized paths in which the return value does not matter for
86 // host functions, and checking whether the function is a host
87 // function is deemed too expensive.
88 JSScope
* scopeUnchecked()
92 void setScope(VM
& vm
, JSScope
* scope
)
94 ASSERT(!isHostFunctionNonInline());
95 m_scope
.set(vm
, this, scope
);
98 ExecutableBase
* executable() const { return m_executable
.get(); }
100 // To call either of these methods include Executable.h
101 inline bool isHostFunction() const;
102 FunctionExecutable
* jsExecutable() const;
104 JS_EXPORT_PRIVATE
const SourceCode
* sourceCode() const;
106 static JS_EXPORTDATA
const ClassInfo s_info
;
108 static Structure
* createStructure(VM
& vm
, JSGlobalObject
* globalObject
, JSValue prototype
)
110 ASSERT(globalObject
);
111 return Structure::create(vm
, globalObject
, prototype
, TypeInfo(JSFunctionType
, StructureFlags
), &s_info
);
114 NativeFunction
nativeFunction();
115 NativeFunction
nativeConstructor();
117 static ConstructType
getConstructData(JSCell
*, ConstructData
&);
118 static CallType
getCallData(JSCell
*, CallData
&);
120 static inline ptrdiff_t offsetOfScopeChain()
122 return OBJECT_OFFSETOF(JSFunction
, m_scope
);
125 static inline ptrdiff_t offsetOfExecutable()
127 return OBJECT_OFFSETOF(JSFunction
, m_executable
);
130 static inline ptrdiff_t offsetOfAllocationProfile()
132 return OBJECT_OFFSETOF(JSFunction
, m_allocationProfile
);
135 ObjectAllocationProfile
* allocationProfile(ExecState
* exec
, unsigned inlineCapacity
)
137 if (UNLIKELY(m_allocationProfile
.isNull()))
138 return createAllocationProfile(exec
, inlineCapacity
);
139 return &m_allocationProfile
;
142 ObjectAllocationProfile
* tryGetAllocationProfile()
144 if (m_allocationProfile
.isNull())
146 if (m_allocationProfileWatchpoint
.hasBeenInvalidated())
148 return &m_allocationProfile
;
151 void addAllocationProfileWatchpoint(Watchpoint
* watchpoint
)
153 ASSERT(tryGetAllocationProfile());
154 m_allocationProfileWatchpoint
.add(watchpoint
);
158 const static unsigned StructureFlags
= OverridesGetOwnPropertySlot
| ImplementsHasInstance
| OverridesVisitChildren
| OverridesGetPropertyNames
| JSObject::StructureFlags
;
160 JS_EXPORT_PRIVATE
JSFunction(ExecState
*, JSGlobalObject
*, Structure
*);
161 JSFunction(VM
&, FunctionExecutable
*, JSScope
*);
163 void finishCreation(ExecState
*, NativeExecutable
*, int length
, const String
& name
);
164 using Base::finishCreation
;
166 ObjectAllocationProfile
* createAllocationProfile(ExecState
*, size_t inlineCapacity
);
168 static bool getOwnPropertySlot(JSCell
*, ExecState
*, PropertyName
, PropertySlot
&);
169 static bool getOwnPropertyDescriptor(JSObject
*, ExecState
*, PropertyName
, PropertyDescriptor
&);
170 static void getOwnNonIndexPropertyNames(JSObject
*, ExecState
*, PropertyNameArray
&, EnumerationMode
= ExcludeDontEnumProperties
);
171 static bool defineOwnProperty(JSObject
*, ExecState
*, PropertyName
, PropertyDescriptor
&, bool shouldThrow
);
173 static void put(JSCell
*, ExecState
*, PropertyName
, JSValue
, PutPropertySlot
&);
175 static bool deleteProperty(JSCell
*, ExecState
*, PropertyName
);
177 static void visitChildren(JSCell
*, SlotVisitor
&);
180 friend class LLIntOffsetsExtractor
;
182 JS_EXPORT_PRIVATE
bool isHostFunctionNonInline() const;
184 static JSValue
argumentsGetter(ExecState
*, JSValue
, PropertyName
);
185 static JSValue
callerGetter(ExecState
*, JSValue
, PropertyName
);
186 static JSValue
lengthGetter(ExecState
*, JSValue
, PropertyName
);
187 static JSValue
nameGetter(ExecState
*, JSValue
, PropertyName
);
189 WriteBarrier
<ExecutableBase
> m_executable
;
190 WriteBarrier
<JSScope
> m_scope
;
191 ObjectAllocationProfile m_allocationProfile
;
192 InlineWatchpointSet m_allocationProfileWatchpoint
;
197 #endif // JSFunction_h