1 | /*
|
---|
2 | * Copyright (C) 1999-2002 Harri Porten ([email protected])
|
---|
3 | * Copyright (C) 2001 Peter Kelly ([email protected])
|
---|
4 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
|
---|
5 | * Copyright (C) 2007 Cameron Zwarich ([email protected])
|
---|
6 | * Copyright (C) 2007 Maks Orlovich
|
---|
7 | *
|
---|
8 | * This library is free software; you can redistribute it and/or
|
---|
9 | * modify it under the terms of the GNU Library General Public
|
---|
10 | * License as published by the Free Software Foundation; either
|
---|
11 | * version 2 of the License, or (at your option) any later version.
|
---|
12 | *
|
---|
13 | * This library is distributed in the hope that it will be useful,
|
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
16 | * Library General Public License for more details.
|
---|
17 | *
|
---|
18 | * You should have received a copy of the GNU Library General Public License
|
---|
19 | * along with this library; see the file COPYING.LIB. If not, write to
|
---|
20 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
---|
21 | * Boston, MA 02110-1301, USA.
|
---|
22 | *
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "config.h"
|
---|
26 | #include "JSFunction.h"
|
---|
27 |
|
---|
28 | #include "CodeBlock.h"
|
---|
29 | #include "CommonIdentifiers.h"
|
---|
30 | #include "CallFrame.h"
|
---|
31 | #include "FunctionPrototype.h"
|
---|
32 | #include "JSGlobalObject.h"
|
---|
33 | #include "Interpreter.h"
|
---|
34 | #include "ObjectPrototype.h"
|
---|
35 | #include "Parser.h"
|
---|
36 | #include "PropertyNameArray.h"
|
---|
37 | #include "ScopeChainMark.h"
|
---|
38 |
|
---|
39 | using namespace WTF;
|
---|
40 | using namespace Unicode;
|
---|
41 |
|
---|
42 | namespace JSC {
|
---|
43 |
|
---|
44 | ASSERT_CLASS_FITS_IN_CELL(JSFunction);
|
---|
45 |
|
---|
46 | const ClassInfo JSFunction::info = { "Function", &InternalFunction::info, 0, 0 };
|
---|
47 |
|
---|
48 | bool JSFunction::isHostFunctionNonInline() const
|
---|
49 | {
|
---|
50 | return isHostFunction();
|
---|
51 | }
|
---|
52 |
|
---|
53 | JSFunction::JSFunction(PassRefPtr<Structure> structure)
|
---|
54 | : Base(structure)
|
---|
55 | , m_executable(adoptRef(new VPtrHackExecutable()))
|
---|
56 | {
|
---|
57 | }
|
---|
58 |
|
---|
59 | JSFunction::JSFunction(ExecState* exec, PassRefPtr<Structure> structure, int length, const Identifier& name, NativeFunction func)
|
---|
60 | : Base(&exec->globalData(), structure, name)
|
---|
61 | #if ENABLE(JIT)
|
---|
62 | , m_executable(adoptRef(new NativeExecutable(exec)))
|
---|
63 | #endif
|
---|
64 | {
|
---|
65 | #if ENABLE(JIT)
|
---|
66 | setNativeFunction(func);
|
---|
67 | putDirect(exec->propertyNames().length, jsNumber(exec, length), DontDelete | ReadOnly | DontEnum);
|
---|
68 | #else
|
---|
69 | UNUSED_PARAM(length);
|
---|
70 | UNUSED_PARAM(func);
|
---|
71 | ASSERT_NOT_REACHED();
|
---|
72 | #endif
|
---|
73 | }
|
---|
74 |
|
---|
75 | JSFunction::JSFunction(ExecState* exec, PassRefPtr<FunctionExecutable> executable, ScopeChainNode* scopeChainNode)
|
---|
76 | : Base(&exec->globalData(), exec->lexicalGlobalObject()->functionStructure(), executable->name())
|
---|
77 | , m_executable(executable)
|
---|
78 | {
|
---|
79 | setScopeChain(scopeChainNode);
|
---|
80 | }
|
---|
81 |
|
---|
82 | JSFunction::~JSFunction()
|
---|
83 | {
|
---|
84 | // JIT code for other functions may have had calls linked directly to the code for this function; these links
|
---|
85 | // are based on a check for the this pointer value for this JSFunction - which will no longer be valid once
|
---|
86 | // this memory is freed and may be reused (potentially for another, different JSFunction).
|
---|
87 | if (!isHostFunction()) {
|
---|
88 | #if ENABLE(JIT_OPTIMIZE_CALL)
|
---|
89 | ASSERT(m_executable);
|
---|
90 | if (jsExecutable()->isGenerated())
|
---|
91 | jsExecutable()->generatedBytecode().unlinkCallers();
|
---|
92 | #endif
|
---|
93 | scopeChain().~ScopeChain(); // FIXME: Don't we need to do this in the interpreter too?
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | void JSFunction::markChildren(MarkStack& markStack)
|
---|
98 | {
|
---|
99 | Base::markChildren(markStack);
|
---|
100 | if (!isHostFunction()) {
|
---|
101 | jsExecutable()->markAggregate(markStack);
|
---|
102 | scopeChain().markAggregate(markStack);
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | CallType JSFunction::getCallData(CallData& callData)
|
---|
107 | {
|
---|
108 | if (isHostFunction()) {
|
---|
109 | callData.native.function = nativeFunction();
|
---|
110 | return CallTypeHost;
|
---|
111 | }
|
---|
112 | callData.js.functionExecutable = jsExecutable();
|
---|
113 | callData.js.scopeChain = scopeChain().node();
|
---|
114 | return CallTypeJS;
|
---|
115 | }
|
---|
116 |
|
---|
117 | JSValue JSFunction::call(ExecState* exec, JSValue thisValue, const ArgList& args)
|
---|
118 | {
|
---|
119 | ASSERT(!isHostFunction());
|
---|
120 | return exec->interpreter()->execute(jsExecutable(), exec, this, thisValue.toThisObject(exec), args, scopeChain().node(), exec->exceptionSlot());
|
---|
121 | }
|
---|
122 |
|
---|
123 | JSValue JSFunction::argumentsGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
|
---|
124 | {
|
---|
125 | JSFunction* thisObj = asFunction(slot.slotBase());
|
---|
126 | ASSERT(!thisObj->isHostFunction());
|
---|
127 | return exec->interpreter()->retrieveArguments(exec, thisObj);
|
---|
128 | }
|
---|
129 |
|
---|
130 | JSValue JSFunction::callerGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
|
---|
131 | {
|
---|
132 | JSFunction* thisObj = asFunction(slot.slotBase());
|
---|
133 | ASSERT(!thisObj->isHostFunction());
|
---|
134 | return exec->interpreter()->retrieveCaller(exec, thisObj);
|
---|
135 | }
|
---|
136 |
|
---|
137 | JSValue JSFunction::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot)
|
---|
138 | {
|
---|
139 | JSFunction* thisObj = asFunction(slot.slotBase());
|
---|
140 | ASSERT(!thisObj->isHostFunction());
|
---|
141 | return jsNumber(exec, thisObj->jsExecutable()->parameterCount());
|
---|
142 | }
|
---|
143 |
|
---|
144 | bool JSFunction::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot)
|
---|
145 | {
|
---|
146 | if (isHostFunction())
|
---|
147 | return Base::getOwnPropertySlot(exec, propertyName, slot);
|
---|
148 |
|
---|
149 | if (propertyName == exec->propertyNames().prototype) {
|
---|
150 | JSValue* location = getDirectLocation(propertyName);
|
---|
151 |
|
---|
152 | if (!location) {
|
---|
153 | JSObject* prototype = new (exec) JSObject(scopeChain().globalObject()->emptyObjectStructure());
|
---|
154 | prototype->putDirect(exec->propertyNames().constructor, this, DontEnum);
|
---|
155 | putDirect(exec->propertyNames().prototype, prototype, DontDelete);
|
---|
156 | location = getDirectLocation(propertyName);
|
---|
157 | }
|
---|
158 |
|
---|
159 | slot.setValueSlot(this, location, offsetForLocation(location));
|
---|
160 | }
|
---|
161 |
|
---|
162 | if (propertyName == exec->propertyNames().arguments) {
|
---|
163 | slot.setCustom(this, argumentsGetter);
|
---|
164 | return true;
|
---|
165 | }
|
---|
166 |
|
---|
167 | if (propertyName == exec->propertyNames().length) {
|
---|
168 | slot.setCustom(this, lengthGetter);
|
---|
169 | return true;
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (propertyName == exec->propertyNames().caller) {
|
---|
173 | slot.setCustom(this, callerGetter);
|
---|
174 | return true;
|
---|
175 | }
|
---|
176 |
|
---|
177 | return Base::getOwnPropertySlot(exec, propertyName, slot);
|
---|
178 | }
|
---|
179 |
|
---|
180 | void JSFunction::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot)
|
---|
181 | {
|
---|
182 | if (isHostFunction()) {
|
---|
183 | Base::put(exec, propertyName, value, slot);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
|
---|
187 | return;
|
---|
188 | Base::put(exec, propertyName, value, slot);
|
---|
189 | }
|
---|
190 |
|
---|
191 | bool JSFunction::deleteProperty(ExecState* exec, const Identifier& propertyName)
|
---|
192 | {
|
---|
193 | if (isHostFunction())
|
---|
194 | return Base::deleteProperty(exec, propertyName);
|
---|
195 | if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length)
|
---|
196 | return false;
|
---|
197 | return Base::deleteProperty(exec, propertyName);
|
---|
198 | }
|
---|
199 |
|
---|
200 | // ECMA 13.2.2 [[Construct]]
|
---|
201 | ConstructType JSFunction::getConstructData(ConstructData& constructData)
|
---|
202 | {
|
---|
203 | if (isHostFunction())
|
---|
204 | return ConstructTypeNone;
|
---|
205 | constructData.js.functionExecutable = jsExecutable();
|
---|
206 | constructData.js.scopeChain = scopeChain().node();
|
---|
207 | return ConstructTypeJS;
|
---|
208 | }
|
---|
209 |
|
---|
210 | JSObject* JSFunction::construct(ExecState* exec, const ArgList& args)
|
---|
211 | {
|
---|
212 | ASSERT(!isHostFunction());
|
---|
213 | Structure* structure;
|
---|
214 | JSValue prototype = get(exec, exec->propertyNames().prototype);
|
---|
215 | if (prototype.isObject())
|
---|
216 | structure = asObject(prototype)->inheritorID();
|
---|
217 | else
|
---|
218 | structure = exec->lexicalGlobalObject()->emptyObjectStructure();
|
---|
219 | JSObject* thisObj = new (exec) JSObject(structure);
|
---|
220 |
|
---|
221 | JSValue result = exec->interpreter()->execute(jsExecutable(), exec, this, thisObj, args, scopeChain().node(), exec->exceptionSlot());
|
---|
222 | if (exec->hadException() || !result.isObject())
|
---|
223 | return thisObj;
|
---|
224 | return asObject(result);
|
---|
225 | }
|
---|
226 |
|
---|
227 | } // namespace JSC
|
---|