]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/JSFunction.h
JavaScriptCore-621.1.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
29 namespace JSC {
30
31 class ExecutableBase;
32 class FunctionExecutable;
33 class FunctionPrototype;
34 class JSActivation;
35 class JSGlobalObject;
36 class NativeExecutable;
37
38 class JSFunction : public InternalFunction {
39 friend class JIT;
40 friend class JSGlobalData;
41
42 typedef InternalFunction Base;
43
44 public:
45 JSFunction(ExecState*, NonNullPassRefPtr<Structure>, int length, const Identifier&, NativeFunction);
46 JSFunction(ExecState*, NonNullPassRefPtr<Structure>, int length, const Identifier&, NativeExecutable*, NativeFunction);
47 JSFunction(ExecState*, NonNullPassRefPtr<FunctionExecutable>, ScopeChainNode*);
48 virtual ~JSFunction();
49
50 JSObject* construct(ExecState*, const ArgList&);
51 JSValue call(ExecState*, JSValue thisValue, const ArgList&);
52
53 void setScope(const ScopeChain& scopeChain) { setScopeChain(scopeChain); }
54 ScopeChain& scope() { return scopeChain(); }
55
56 ExecutableBase* executable() const { return m_executable.get(); }
57
58 // To call either of these methods include Executable.h
59 inline bool isHostFunction() const;
60 FunctionExecutable* jsExecutable() const;
61
62 static JS_EXPORTDATA const ClassInfo info;
63
64 static PassRefPtr<Structure> createStructure(JSValue prototype)
65 {
66 return Structure::create(prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount);
67 }
68
69 NativeFunction nativeFunction()
70 {
71 return *WTF::bitwise_cast<NativeFunction*>(m_data);
72 }
73
74 virtual ConstructType getConstructData(ConstructData&);
75 virtual CallType getCallData(CallData&);
76
77 protected:
78 const static unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesMarkChildren | OverridesGetPropertyNames | InternalFunction::StructureFlags;
79
80 private:
81 JSFunction(NonNullPassRefPtr<Structure>);
82
83 bool isHostFunctionNonInline() const;
84
85 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
86 virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
87 virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
88 virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
89 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
90
91 virtual void markChildren(MarkStack&);
92
93 virtual const ClassInfo* classInfo() const { return &info; }
94
95 static JSValue argumentsGetter(ExecState*, JSValue, const Identifier&);
96 static JSValue callerGetter(ExecState*, JSValue, const Identifier&);
97 static JSValue lengthGetter(ExecState*, JSValue, const Identifier&);
98
99 RefPtr<ExecutableBase> m_executable;
100 ScopeChain& scopeChain()
101 {
102 ASSERT(!isHostFunctionNonInline());
103 return *WTF::bitwise_cast<ScopeChain*>(m_data);
104 }
105 void clearScopeChain()
106 {
107 ASSERT(!isHostFunctionNonInline());
108 new (m_data) ScopeChain(NoScopeChain());
109 }
110 void setScopeChain(ScopeChainNode* sc)
111 {
112 ASSERT(!isHostFunctionNonInline());
113 new (m_data) ScopeChain(sc);
114 }
115 void setScopeChain(const ScopeChain& sc)
116 {
117 ASSERT(!isHostFunctionNonInline());
118 *WTF::bitwise_cast<ScopeChain*>(m_data) = sc;
119 }
120 void setNativeFunction(NativeFunction func)
121 {
122 *WTF::bitwise_cast<NativeFunction*>(m_data) = func;
123 }
124 unsigned char m_data[sizeof(void*)];
125 };
126
127 JSFunction* asFunction(JSValue);
128
129 inline JSFunction* asFunction(JSValue value)
130 {
131 ASSERT(asObject(value)->inherits(&JSFunction::info));
132 return static_cast<JSFunction*>(asObject(value));
133 }
134
135 } // namespace JSC
136
137 #endif // JSFunction_h