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 "JSObjectWithGlobalObject.h"
28
29namespace JSC {
30
31    class ExecutableBase;
32    class FunctionExecutable;
33    class FunctionPrototype;
34    class JSActivation;
35    class JSGlobalObject;
36    class NativeExecutable;
37    class VPtrHackExecutable;
38
39    EncodedJSValue JSC_HOST_CALL callHostFunctionAsConstructor(ExecState*);
40
41    class JSFunction : public JSObjectWithGlobalObject {
42        friend class JIT;
43        friend class JSGlobalData;
44
45        typedef JSObjectWithGlobalObject Base;
46
47    public:
48        JSFunction(ExecState*, JSGlobalObject*, Structure*, int length, const Identifier&, NativeFunction);
49        JSFunction(ExecState*, JSGlobalObject*, Structure*, int length, const Identifier&, NativeExecutable*);
50        JSFunction(ExecState*, FunctionExecutable*, ScopeChainNode*);
51        virtual ~JSFunction();
52
53        const UString& name(ExecState*);
54        const UString displayName(ExecState*);
55        const UString calculatedDisplayName(ExecState*);
56
57        ScopeChainNode* scope()
58        {
59            ASSERT(!isHostFunctionNonInline());
60            return m_scopeChain.get();
61        }
62        void setScope(JSGlobalData& globalData, ScopeChainNode* scopeChain)
63        {
64            ASSERT(!isHostFunctionNonInline());
65            m_scopeChain.set(globalData, this, scopeChain);
66        }
67
68        ExecutableBase* executable() const { return m_executable.get(); }
69
70        // To call either of these methods include Executable.h
71        inline bool isHostFunction() const;
72        FunctionExecutable* jsExecutable() const;
73
74        static JS_EXPORTDATA const ClassInfo s_info;
75
76        static Structure* createStructure(JSGlobalData& globalData, JSValue prototype)
77        {
78            return Structure::create(globalData, prototype, TypeInfo(ObjectType, StructureFlags), AnonymousSlotCount, &s_info);
79        }
80
81        NativeFunction nativeFunction();
82
83        virtual ConstructType getConstructData(ConstructData&);
84        virtual CallType getCallData(CallData&);
85
86    protected:
87        const static unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | OverridesMarkChildren | OverridesGetPropertyNames | JSObject::StructureFlags;
88
89    private:
90        explicit JSFunction(VPtrStealingHackType);
91
92        bool isHostFunctionNonInline() const;
93
94        virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
95        virtual bool getOwnPropertyDescriptor(ExecState*, const Identifier&, PropertyDescriptor&);
96        virtual void getOwnPropertyNames(ExecState*, PropertyNameArray&, EnumerationMode mode = ExcludeDontEnumProperties);
97        virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
98        virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
99
100        virtual void markChildren(MarkStack&);
101
102        static JSValue argumentsGetter(ExecState*, JSValue, const Identifier&);
103        static JSValue callerGetter(ExecState*, JSValue, const Identifier&);
104        static JSValue lengthGetter(ExecState*, JSValue, const Identifier&);
105
106        WriteBarrier<ExecutableBase> m_executable;
107        WriteBarrier<ScopeChainNode> m_scopeChain;
108    };
109
110    JSFunction* asFunction(JSValue);
111
112    inline JSFunction* asFunction(JSValue value)
113    {
114        ASSERT(asObject(value)->inherits(&JSFunction::s_info));
115        return static_cast<JSFunction*>(asObject(value));
116    }
117
118} // namespace JSC
119
120#endif // JSFunction_h
121