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"
33 class FunctionExecutable
;
34 class FunctionPrototype
;
37 class LLIntOffsetsExtractor
;
38 class NativeExecutable
;
45 JS_EXPORT_PRIVATE EncodedJSValue JSC_HOST_CALL
callHostFunctionAsConstructor(ExecState
*);
47 JS_EXPORT_PRIVATE UString
getCalculatedDisplayName(CallFrame
*, JSObject
*);
49 class JSFunction
: public JSNonFinalObject
{
51 friend class DFG::SpeculativeJIT
;
52 friend class DFG::JITCompiler
;
53 friend class JSGlobalData
;
56 typedef JSNonFinalObject Base
;
58 JS_EXPORT_PRIVATE
static JSFunction
* create(ExecState
*, JSGlobalObject
*, int length
, const Identifier
& name
, NativeFunction nativeFunction
, Intrinsic
= NoIntrinsic
, NativeFunction nativeConstructor
= callHostFunctionAsConstructor
);
60 static JSFunction
* create(ExecState
* exec
, FunctionExecutable
* executable
, ScopeChainNode
* scopeChain
)
62 JSFunction
* function
= new (NotNull
, allocateCell
<JSFunction
>(*exec
->heap())) JSFunction(exec
, executable
, scopeChain
);
63 ASSERT(function
->structure()->globalObject());
64 function
->finishCreation(exec
, executable
, scopeChain
);
68 JS_EXPORT_PRIVATE
const UString
& name(ExecState
*);
69 JS_EXPORT_PRIVATE
const UString
displayName(ExecState
*);
70 const UString
calculatedDisplayName(ExecState
*);
72 ScopeChainNode
* scope()
74 ASSERT(!isHostFunctionNonInline());
75 return m_scopeChain
.get();
77 // This method may be called for host functins, in which case it
78 // will return an arbitrary value. This should only be used for
79 // optimized paths in which the return value does not matter for
80 // host functions, and checking whether the function is a host
81 // function is deemed too expensive.
82 ScopeChainNode
* scopeUnchecked()
84 return m_scopeChain
.get();
86 void setScope(JSGlobalData
& globalData
, ScopeChainNode
* scopeChain
)
88 ASSERT(!isHostFunctionNonInline());
89 m_scopeChain
.set(globalData
, this, scopeChain
);
92 ExecutableBase
* executable() const { return m_executable
.get(); }
94 // To call either of these methods include Executable.h
95 inline bool isHostFunction() const;
96 FunctionExecutable
* jsExecutable() const;
98 JS_EXPORT_PRIVATE
const SourceCode
* sourceCode() const;
100 static JS_EXPORTDATA
const ClassInfo s_info
;
102 static Structure
* createStructure(JSGlobalData
& globalData
, JSGlobalObject
* globalObject
, JSValue prototype
)
104 ASSERT(globalObject
);
105 return Structure::create(globalData
, globalObject
, prototype
, TypeInfo(JSFunctionType
, StructureFlags
), &s_info
);
108 NativeFunction
nativeFunction();
109 NativeFunction
nativeConstructor();
111 static ConstructType
getConstructData(JSCell
*, ConstructData
&);
112 static CallType
getCallData(JSCell
*, CallData
&);
114 static inline size_t offsetOfScopeChain()
116 return OBJECT_OFFSETOF(JSFunction
, m_scopeChain
);
119 static inline size_t offsetOfExecutable()
121 return OBJECT_OFFSETOF(JSFunction
, m_executable
);
125 const static unsigned StructureFlags
= OverridesGetOwnPropertySlot
| ImplementsHasInstance
| OverridesVisitChildren
| OverridesGetPropertyNames
| JSObject::StructureFlags
;
127 JS_EXPORT_PRIVATE
JSFunction(ExecState
*, JSGlobalObject
*, Structure
*);
128 JSFunction(ExecState
*, FunctionExecutable
*, ScopeChainNode
*);
130 void finishCreation(ExecState
*, NativeExecutable
*, int length
, const Identifier
& name
);
131 void finishCreation(ExecState
*, FunctionExecutable
*, ScopeChainNode
*);
133 static bool getOwnPropertySlot(JSCell
*, ExecState
*, const Identifier
&, PropertySlot
&);
134 static bool getOwnPropertyDescriptor(JSObject
*, ExecState
*, const Identifier
&, PropertyDescriptor
&);
135 static void getOwnPropertyNames(JSObject
*, ExecState
*, PropertyNameArray
&, EnumerationMode
= ExcludeDontEnumProperties
);
136 static bool defineOwnProperty(JSObject
*, ExecState
*, const Identifier
& propertyName
, PropertyDescriptor
&, bool shouldThrow
);
138 static void put(JSCell
*, ExecState
*, const Identifier
& propertyName
, JSValue
, PutPropertySlot
&);
140 static bool deleteProperty(JSCell
*, ExecState
*, const Identifier
& propertyName
);
142 static void visitChildren(JSCell
*, SlotVisitor
&);
145 friend class LLIntOffsetsExtractor
;
147 JS_EXPORT_PRIVATE
bool isHostFunctionNonInline() const;
149 static JSValue
argumentsGetter(ExecState
*, JSValue
, const Identifier
&);
150 static JSValue
callerGetter(ExecState
*, JSValue
, const Identifier
&);
151 static JSValue
lengthGetter(ExecState
*, JSValue
, const Identifier
&);
153 WriteBarrier
<ExecutableBase
> m_executable
;
154 WriteBarrier
<ScopeChainNode
> m_scopeChain
;
157 inline bool JSValue::isFunction() const
159 return isCell() && (asCell()->inherits(&JSFunction::s_info
) || asCell()->inherits(&InternalFunction::s_info
));
164 #endif // JSFunction_h