source: webkit/trunk/JavaScriptCore/runtime/JSFunction.h@ 47641

Last change on this file since 47641 was 47641, checked in by [email protected], 16 years ago

Restructure Executable types so that host functions do not hold a FunctionExecutable.
https://bugs.webkit.org/show_bug.cgi?id=28621

Reviewed by Oliver Hunt.

All JSFunction objects have a pointer to an Executable*. This is currently always a
FunctionExecutable, however this has a couple of drawbacks. Host functions do not
store a range of information that the FunctionExecutable provides (source, name,
CodeBlock & information presently held on the FunctionBodyNode).

[ * nearly all... see below! ]

Instead, make JSFunctions hold a pointer to an ExecutableBase, move fields specific
to JS sourced executable types (source, node) into a new subclass (ScriptExecutable),
and create a new NativeExecutable type. We now provide a new method in JSFunction
to access & downcast to FunctionExecutable, but in doing so we can make an early
check (with an ASSERT) to ensure that the Executable read from a function will only
be treated as a FunctionExecutable (and thus the JS sepcific fields will only be
accessed) if the JSFunction is not a host function.

There is one JSFunction that currently does not have an Executable, which is the
object created to allow us to read out the vtable pointer. By making this change
we can also add a new Executable type fror this object (VPtrHackExecutable).
Since this means that really all JSFunctions have an Executable we no longer have
to null-check m_executable before us it - particularly in isHostFunction().

This patch removes CacheableEvalExecutable, since all subclasses of ExecutableBase
can now be ref-counted - since both JSFunction holds (and ref-counts) an ExecutableBase
that might be a FunctionExecutable or a NativeExecutable. This does now mean that all
ProgramExecutables and EvalExecutables (unnecessarily) provide an interface to be
ref-counted, however this seems less-bad than host functions unnecessarily providing
interface to access non-host specific information.

The class hierarcy has changed from this:

  • ExecutableBase
    • ProgramExecutable
    • EvalExecutable
      • CacheableEvalExecutable (also RefCounted by multiple-inheritance)
    • FunctionExecutable (also RefCounted by multiple-inheritance, 'special' FunctionExecutable also used for host functions)

To this:

  • RefCounted
    • ExecutableBase
      • NativeExecutable
      • VPtrHackExecutable
      • ScriptExecutable
        • ProgramExecutable
        • EvalExecutable
        • FunctionExecutable

This patch speeds up sunspidey by a couple of ms (presumably due to the changes to isHostFunction()).

  • bytecode/CodeBlock.cpp:

(JSC::CodeBlock::CodeBlock):

  • bytecode/CodeBlock.h:

(JSC::CodeBlock::ownerExecutable):
(JSC::GlobalCodeBlock::GlobalCodeBlock):

  • bytecode/EvalCodeCache.h:

(JSC::EvalCodeCache::get):

  • debugger/Debugger.cpp:

(JSC::Debugger::recompileAllJSFunctions):

  • interpreter/CachedCall.h:

(JSC::CachedCall::CachedCall):

  • interpreter/Interpreter.cpp:

(JSC::Interpreter::callEval):
(JSC::Interpreter::privateExecute):

  • jit/JITStubs.cpp:

(JSC::DEFINE_STUB_FUNCTION):

  • profiler/Profiler.cpp:

(JSC::createCallIdentifierFromFunctionImp):

  • runtime/Arguments.h:

(JSC::Arguments::getArgumentsData):
(JSC::Arguments::Arguments):

  • runtime/Executable.cpp:

(JSC::NativeExecutable::~NativeExecutable):
(JSC::VPtrHackExecutable::~VPtrHackExecutable):

  • runtime/Executable.h:

(JSC::ExecutableBase::ExecutableBase):
(JSC::ExecutableBase::~ExecutableBase):
(JSC::ExecutableBase::isHostFunction):
(JSC::NativeExecutable::NativeExecutable):
(JSC::VPtrHackExecutable::VPtrHackExecutable):
(JSC::ScriptExecutable::ScriptExecutable):
(JSC::ScriptExecutable::source):
(JSC::ScriptExecutable::sourceID):
(JSC::ScriptExecutable::sourceURL):
(JSC::ScriptExecutable::lineNo):
(JSC::ScriptExecutable::lastLine):
(JSC::ScriptExecutable::usesEval):
(JSC::ScriptExecutable::usesArguments):
(JSC::ScriptExecutable::needsActivation):
(JSC::EvalExecutable::EvalExecutable):
(JSC::EvalExecutable::create):
(JSC::ProgramExecutable::ProgramExecutable):
(JSC::FunctionExecutable::FunctionExecutable):

  • runtime/FunctionPrototype.cpp:

(JSC::functionProtoFuncToString):

  • runtime/JSFunction.cpp:

(JSC::JSFunction::JSFunction):
(JSC::JSFunction::~JSFunction):
(JSC::JSFunction::markChildren):
(JSC::JSFunction::getCallData):
(JSC::JSFunction::call):
(JSC::JSFunction::lengthGetter):
(JSC::JSFunction::getConstructData):
(JSC::JSFunction::construct):

  • runtime/JSFunction.h:

(JSC::JSFunction::executable):
(JSC::JSFunction::jsExecutable):
(JSC::JSFunction::isHostFunction):

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/*
2 * Copyright (C) 1999-2000 Harri Porten ([email protected])
3 * Copyright (C) 2003, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Cameron Zwarich ([email protected])
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 "Executable.h"
28#include "InternalFunction.h"
29
30namespace JSC {
31
32 class FunctionPrototype;
33 class JSActivation;
34 class JSGlobalObject;
35 class ExecutableBase;
36 class FunctionExecutable;
37
38 class JSFunction : public InternalFunction {
39 friend class JIT;
40 friend struct VPtrSet;
41
42 typedef InternalFunction Base;
43
44 public:
45 JSFunction(ExecState*, PassRefPtr<Structure>, int length, const Identifier&, NativeFunction);
46 JSFunction(ExecState*, PassRefPtr<FunctionExecutable>, ScopeChainNode*);
47 virtual ~JSFunction();
48
49 JSObject* construct(ExecState*, const ArgList&);
50 JSValue call(ExecState*, JSValue thisValue, const ArgList&);
51
52 void setScope(const ScopeChain& scopeChain) { setScopeChain(scopeChain); }
53 ScopeChain& scope() { return scopeChain(); }
54
55 ExecutableBase* executable() const { return m_executable.get(); }
56
57 // To call either of these methods include Executable.h
58 inline bool isHostFunction() const;
59 FunctionExecutable* jsExecutable() const;
60
61 static JS_EXPORTDATA const ClassInfo info;
62
63 static PassRefPtr<Structure> createStructure(JSValue prototype)
64 {
65 return Structure::create(prototype, TypeInfo(ObjectType, ImplementsHasInstance));
66 }
67
68 NativeFunction nativeFunction()
69 {
70 return *reinterpret_cast<NativeFunction*>(m_data);
71 }
72
73 virtual ConstructType getConstructData(ConstructData&);
74 virtual CallType getCallData(CallData&);
75
76 private:
77 JSFunction(PassRefPtr<Structure>);
78
79 bool isHostFunctionNonInline() const;
80
81 virtual bool getOwnPropertySlot(ExecState*, const Identifier&, PropertySlot&);
82 virtual void put(ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
83 virtual bool deleteProperty(ExecState*, const Identifier& propertyName);
84
85 virtual void markChildren(MarkStack&);
86
87 virtual const ClassInfo* classInfo() const { return &info; }
88
89 static JSValue argumentsGetter(ExecState*, const Identifier&, const PropertySlot&);
90 static JSValue callerGetter(ExecState*, const Identifier&, const PropertySlot&);
91 static JSValue lengthGetter(ExecState*, const Identifier&, const PropertySlot&);
92
93 RefPtr<ExecutableBase> m_executable;
94 ScopeChain& scopeChain()
95 {
96 ASSERT(!isHostFunctionNonInline());
97 return *reinterpret_cast<ScopeChain*>(m_data);
98 }
99 void clearScopeChain()
100 {
101 ASSERT(!isHostFunctionNonInline());
102 new (m_data) ScopeChain(NoScopeChain());
103 }
104 void setScopeChain(ScopeChainNode* sc)
105 {
106 ASSERT(!isHostFunctionNonInline());
107 new (m_data) ScopeChain(sc);
108 }
109 void setScopeChain(const ScopeChain& sc)
110 {
111 ASSERT(!isHostFunctionNonInline());
112 *reinterpret_cast<ScopeChain*>(m_data) = sc;
113 }
114 void setNativeFunction(NativeFunction func)
115 {
116 *reinterpret_cast<NativeFunction*>(m_data) = func;
117 }
118 unsigned char m_data[sizeof(void*)];
119 };
120
121 JSFunction* asFunction(JSValue);
122
123 inline JSFunction* asFunction(JSValue value)
124 {
125 ASSERT(asObject(value)->inherits(&JSFunction::info));
126 return static_cast<JSFunction*>(asObject(value));
127 }
128
129} // namespace JSC
130
131#endif // JSFunction_h
Note: See TracBrowser for help on using the repository browser.