]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSFunction.h
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / runtime / JSFunction.h
1 /*
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
6 *
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
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 *
22 */
23
24 #ifndef JSFunction_h
25 #define JSFunction_h
26
27 #include "InternalFunction.h"
28 #include "JSObject.h"
29
30 namespace JSC {
31
32 class ExecutableBase;
33 class FunctionExecutable;
34 class FunctionPrototype;
35 class JSActivation;
36 class JSGlobalObject;
37 class LLIntOffsetsExtractor;
38 class NativeExecutable;
39 class SourceCode;
40 namespace DFG {
41 class SpeculativeJIT;
42 class JITCompiler;
43 }
44
45 JS_EXPORT_PRIVATE EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState*);
46
47 JS_EXPORT_PRIVATE UString getCalculatedDisplayName(CallFrame*, JSObject*);
48
49 class JSFunction : public JSNonFinalObject {
50 friend class JIT;
51 friend class DFG::SpeculativeJIT;
52 friend class DFG::JITCompiler;
53 friend class JSGlobalData;
54
55 public:
56 typedef JSNonFinalObject Base;
57
58 JS_EXPORT_PRIVATE static JSFunction* create(ExecState*, JSGlobalObject*, int length, const Identifier& name, NativeFunction nativeFunction, Intrinsic = NoIntrinsic, NativeFunction nativeConstructor = callHostFunctionAsConstructor);
59
60 static JSFunction* create(ExecState* exec, FunctionExecutable* executable, ScopeChainNode* scopeChain)
61 {
62 JSFunction* function = new (NotNull, allocateCell<JSFunction>(*exec->heap())) JSFunction(exec, executable, scopeChain);
63 ASSERT(function->structure()->globalObject());
64 function->finishCreation(exec, executable, scopeChain);
65 return function;
66 }
67
68 JS_EXPORT_PRIVATE const UString& name(ExecState*);
69 JS_EXPORT_PRIVATE const UString displayName(ExecState*);
70 const UString calculatedDisplayName(ExecState*);
71
72 ScopeChainNode* scope()
73 {
74 ASSERT(!isHostFunctionNonInline());
75 return m_scopeChain.get();
76 }
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()
83 {
84 return m_scopeChain.get();
85 }
86 void setScope(JSGlobalData& globalData, ScopeChainNode* scopeChain)
87 {
88 ASSERT(!isHostFunctionNonInline());
89 m_scopeChain.set(globalData, this, scopeChain);
90 }
91
92 ExecutableBase* executable() const { return m_executable.get(); }
93
94 // To call either of these methods include Executable.h
95 inline bool isHostFunction() const;
96 FunctionExecutable* jsExecutable() const;
97
98 JS_EXPORT_PRIVATE const SourceCode* sourceCode() const;
99
100 static JS_EXPORTDATA const ClassInfo s_info;
101
102 static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
103 {
104 ASSERT(globalObject);
105 return Structure::create(globalData, globalObject, prototype, TypeInfo(JSFunctionType, StructureFlags), &s_info);
106 }
107
108 NativeFunction nativeFunction();
109 NativeFunction nativeConstructor();
110
111 static ConstructType getConstructData(JSCell*, ConstructData&);
112 static CallType getCallData(JSCell*, CallData&);
113
114 static inline size_t offsetOfScopeChain()
115 {
116 return OBJECT_OFFSETOF(JSFunction, m_scopeChain);
117 }
118
119 static inline size_t offsetOfExecutable()
120 {
121 return OBJECT_OFFSETOF(JSFunction, m_executable);
122 }
123
124 protected:
125 const static unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesVisitChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
126
127 JS_EXPORT_PRIVATE JSFunction(ExecState*, JSGlobalObject*, Structure*);
128 JSFunction(ExecState*, FunctionExecutable*, ScopeChainNode*);
129
130 void finishCreation(ExecState*, NativeExecutable*, int length, const Identifier& name);
131 void finishCreation(ExecState*, FunctionExecutable*, ScopeChainNode*);
132
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);
137
138 static void put(JSCell*, ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
139
140 static bool deleteProperty(JSCell*, ExecState*, const Identifier& propertyName);
141
142 static void visitChildren(JSCell*, SlotVisitor&);
143
144 private:
145 friend class LLIntOffsetsExtractor;
146
147 JS_EXPORT_PRIVATE bool isHostFunctionNonInline() const;
148
149 static JSValue argumentsGetter(ExecState*, JSValue, const Identifier&);
150 static JSValue callerGetter(ExecState*, JSValue, const Identifier&);
151 static JSValue lengthGetter(ExecState*, JSValue, const Identifier&);
152
153 WriteBarrier<ExecutableBase> m_executable;
154 WriteBarrier<ScopeChainNode> m_scopeChain;
155 };
156
157 inline bool JSValue::isFunction() const
158 {
159 return isCell() && (asCell()->inherits(&JSFunction::s_info) || asCell()->inherits(&InternalFunction::s_info));
160 }
161
162 } // namespace JSC
163
164 #endif // JSFunction_h