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"
32 class FunctionExecutable
;
33 class FunctionPrototype
;
36 class NativeExecutable
;
38 class JSFunction
: public InternalFunction
{
40 friend class JSGlobalData
;
42 typedef InternalFunction Base
;
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();
50 JSObject
* construct(ExecState
*, const ArgList
&);
51 JSValue
call(ExecState
*, JSValue thisValue
, const ArgList
&);
53 void setScope(const ScopeChain
& scopeChain
) { setScopeChain(scopeChain
); }
54 ScopeChain
& scope() { return scopeChain(); }
56 ExecutableBase
* executable() const { return m_executable
.get(); }
58 // To call either of these methods include Executable.h
59 inline bool isHostFunction() const;
60 FunctionExecutable
* jsExecutable() const;
62 static JS_EXPORTDATA
const ClassInfo info
;
64 static PassRefPtr
<Structure
> createStructure(JSValue prototype
)
66 return Structure::create(prototype
, TypeInfo(ObjectType
, StructureFlags
), AnonymousSlotCount
);
69 NativeFunction
nativeFunction()
71 return *WTF::bitwise_cast
<NativeFunction
*>(m_data
);
74 virtual ConstructType
getConstructData(ConstructData
&);
75 virtual CallType
getCallData(CallData
&);
78 const static unsigned StructureFlags
= OverridesGetOwnPropertySlot
| ImplementsHasInstance
| OverridesMarkChildren
| OverridesGetPropertyNames
| InternalFunction::StructureFlags
;
81 JSFunction(NonNullPassRefPtr
<Structure
>);
83 bool isHostFunctionNonInline() const;
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
);
91 virtual void markChildren(MarkStack
&);
93 virtual const ClassInfo
* classInfo() const { return &info
; }
95 static JSValue
argumentsGetter(ExecState
*, JSValue
, const Identifier
&);
96 static JSValue
callerGetter(ExecState
*, JSValue
, const Identifier
&);
97 static JSValue
lengthGetter(ExecState
*, JSValue
, const Identifier
&);
99 RefPtr
<ExecutableBase
> m_executable
;
100 ScopeChain
& scopeChain()
102 ASSERT(!isHostFunctionNonInline());
103 return *WTF::bitwise_cast
<ScopeChain
*>(m_data
);
105 void clearScopeChain()
107 ASSERT(!isHostFunctionNonInline());
108 new (m_data
) ScopeChain(NoScopeChain());
110 void setScopeChain(ScopeChainNode
* sc
)
112 ASSERT(!isHostFunctionNonInline());
113 new (m_data
) ScopeChain(sc
);
115 void setScopeChain(const ScopeChain
& sc
)
117 ASSERT(!isHostFunctionNonInline());
118 *WTF::bitwise_cast
<ScopeChain
*>(m_data
) = sc
;
120 void setNativeFunction(NativeFunction func
)
122 *WTF::bitwise_cast
<NativeFunction
*>(m_data
) = func
;
124 unsigned char m_data
[sizeof(void*)];
127 JSFunction
* asFunction(JSValue
);
129 inline JSFunction
* asFunction(JSValue value
)
131 ASSERT(asObject(value
)->inherits(&JSFunction::info
));
132 return static_cast<JSFunction
*>(asObject(value
));
137 #endif // JSFunction_h