| 1 | /* |
| 2 | * Copyright (C) 1999-2002 Harri Porten (porten@kde.org) |
| 3 | * Copyright (C) 2001 Peter Kelly (pmk@post.com) |
| 4 | * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. |
| 5 | * Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca) |
| 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 = { .className: "Function" , .parentClass: &InternalFunction::info, .staticPropHashTable: 0, .classPropHashTableGetterFunction: 0 }; |
| 47 | |
| 48 | bool JSFunction::isHostFunctionNonInline() const |
| 49 | { |
| 50 | return isHostFunction(); |
| 51 | } |
| 52 | |
| 53 | JSFunction::JSFunction(NonNullPassRefPtr<Structure> structure) |
| 54 | : Base(structure) |
| 55 | , m_executable(adoptRef(p: new VPtrHackExecutable())) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | JSFunction::JSFunction(ExecState* exec, NonNullPassRefPtr<Structure> structure, int length, const Identifier& name, NativeFunction func) |
| 60 | : Base(&exec->globalData(), structure, name) |
| 61 | #if ENABLE(JIT) |
| 62 | , m_executable(adoptRef(p: new NativeExecutable(exec))) |
| 63 | #endif |
| 64 | { |
| 65 | #if ENABLE(JIT) |
| 66 | setNativeFunction(func); |
| 67 | putDirect(propertyName: exec->propertyNames().length, value: jsNumber(exec, i: length), attributes: 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, NonNullPassRefPtr<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 | ASSERT(vptr() == JSGlobalData::jsFunctionVPtr); |
| 85 | |
| 86 | // JIT code for other functions may have had calls linked directly to the code for this function; these links |
| 87 | // are based on a check for the this pointer value for this JSFunction - which will no longer be valid once |
| 88 | // this memory is freed and may be reused (potentially for another, different JSFunction). |
| 89 | if (!isHostFunction()) { |
| 90 | #if ENABLE(JIT_OPTIMIZE_CALL) |
| 91 | ASSERT(m_executable); |
| 92 | if (jsExecutable()->isGenerated()) |
| 93 | jsExecutable()->generatedBytecode().unlinkCallers(); |
| 94 | #endif |
| 95 | scopeChain().~ScopeChain(); // FIXME: Don't we need to do this in the interpreter too? |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void JSFunction::markChildren(MarkStack& markStack) |
| 100 | { |
| 101 | Base::markChildren(markStack); |
| 102 | if (!isHostFunction()) { |
| 103 | jsExecutable()->markAggregate(markStack); |
| 104 | scopeChain().markAggregate(markStack); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | CallType JSFunction::getCallData(CallData& callData) |
| 109 | { |
| 110 | if (isHostFunction()) { |
| 111 | callData.native.function = nativeFunction(); |
| 112 | return CallTypeHost; |
| 113 | } |
| 114 | callData.js.functionExecutable = jsExecutable(); |
| 115 | callData.js.scopeChain = scopeChain().node(); |
| 116 | return CallTypeJS; |
| 117 | } |
| 118 | |
| 119 | JSValue JSFunction::call(ExecState* exec, JSValue thisValue, const ArgList& args) |
| 120 | { |
| 121 | ASSERT(!isHostFunction()); |
| 122 | return exec->interpreter()->execute(jsExecutable(), exec, this, thisObj: thisValue.toThisObject(exec), args, scopeChain().node(), exception: exec->exceptionSlot()); |
| 123 | } |
| 124 | |
| 125 | JSValue JSFunction::argumentsGetter(ExecState* exec, const Identifier&, const PropertySlot& slot) |
| 126 | { |
| 127 | JSFunction* thisObj = asFunction(value: slot.slotBase()); |
| 128 | ASSERT(!thisObj->isHostFunction()); |
| 129 | return exec->interpreter()->retrieveArguments(exec, thisObj); |
| 130 | } |
| 131 | |
| 132 | JSValue JSFunction::callerGetter(ExecState* exec, const Identifier&, const PropertySlot& slot) |
| 133 | { |
| 134 | JSFunction* thisObj = asFunction(value: slot.slotBase()); |
| 135 | ASSERT(!thisObj->isHostFunction()); |
| 136 | return exec->interpreter()->retrieveCaller(exec, thisObj); |
| 137 | } |
| 138 | |
| 139 | JSValue JSFunction::lengthGetter(ExecState* exec, const Identifier&, const PropertySlot& slot) |
| 140 | { |
| 141 | JSFunction* thisObj = asFunction(value: slot.slotBase()); |
| 142 | ASSERT(!thisObj->isHostFunction()); |
| 143 | return jsNumber(exec, i: thisObj->jsExecutable()->parameterCount()); |
| 144 | } |
| 145 | |
| 146 | bool JSFunction::getOwnPropertySlot(ExecState* exec, const Identifier& propertyName, PropertySlot& slot) |
| 147 | { |
| 148 | if (isHostFunction()) |
| 149 | return Base::getOwnPropertySlot(exec, propertyName, slot); |
| 150 | |
| 151 | if (propertyName == exec->propertyNames().prototype) { |
| 152 | JSValue* location = getDirectLocation(propertyName); |
| 153 | |
| 154 | if (!location) { |
| 155 | JSObject* prototype = new (exec) JSObject(scopeChain().globalObject()->emptyObjectStructure()); |
| 156 | prototype->putDirect(propertyName: exec->propertyNames().constructor, value: this, attributes: DontEnum); |
| 157 | putDirect(propertyName: exec->propertyNames().prototype, value: prototype, attributes: DontDelete); |
| 158 | location = getDirectLocation(propertyName); |
| 159 | } |
| 160 | |
| 161 | slot.setValueSlot(slotBase: this, valueSlot: location, offset: offsetForLocation(location)); |
| 162 | } |
| 163 | |
| 164 | if (propertyName == exec->propertyNames().arguments) { |
| 165 | slot.setCustom(slotBase: this, getValue: argumentsGetter); |
| 166 | return true; |
| 167 | } |
| 168 | |
| 169 | if (propertyName == exec->propertyNames().length) { |
| 170 | slot.setCustom(slotBase: this, getValue: lengthGetter); |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | if (propertyName == exec->propertyNames().caller) { |
| 175 | slot.setCustom(slotBase: this, getValue: callerGetter); |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | return Base::getOwnPropertySlot(exec, propertyName, slot); |
| 180 | } |
| 181 | |
| 182 | bool JSFunction::getOwnPropertyDescriptor(ExecState* exec, const Identifier& propertyName, PropertyDescriptor& descriptor) |
| 183 | { |
| 184 | if (isHostFunction()) |
| 185 | return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor); |
| 186 | |
| 187 | if (propertyName == exec->propertyNames().prototype) { |
| 188 | PropertySlot slot; |
| 189 | getOwnPropertySlot(exec, propertyName, slot); |
| 190 | return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor); |
| 191 | } |
| 192 | |
| 193 | if (propertyName == exec->propertyNames().arguments) { |
| 194 | descriptor.setDescriptor(value: exec->interpreter()->retrieveArguments(exec, this), attributes: ReadOnly | DontEnum | DontDelete); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | if (propertyName == exec->propertyNames().length) { |
| 199 | descriptor.setDescriptor(value: jsNumber(exec, i: jsExecutable()->parameterCount()), attributes: ReadOnly | DontEnum | DontDelete); |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | if (propertyName == exec->propertyNames().caller) { |
| 204 | descriptor.setDescriptor(value: exec->interpreter()->retrieveCaller(exec, this), attributes: ReadOnly | DontEnum | DontDelete); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | return Base::getOwnPropertyDescriptor(exec, propertyName, descriptor); |
| 209 | } |
| 210 | |
| 211 | void JSFunction::getOwnPropertyNames(ExecState* exec, PropertyNameArray& propertyNames, EnumerationMode mode) |
| 212 | { |
| 213 | if (!isHostFunction() && (mode == IncludeDontEnumProperties)) { |
| 214 | propertyNames.add(identifier: exec->propertyNames().arguments); |
| 215 | propertyNames.add(identifier: exec->propertyNames().callee); |
| 216 | propertyNames.add(identifier: exec->propertyNames().caller); |
| 217 | propertyNames.add(identifier: exec->propertyNames().length); |
| 218 | } |
| 219 | Base::getOwnPropertyNames(exec, propertyNames, mode); |
| 220 | } |
| 221 | |
| 222 | void JSFunction::put(ExecState* exec, const Identifier& propertyName, JSValue value, PutPropertySlot& slot) |
| 223 | { |
| 224 | if (isHostFunction()) { |
| 225 | Base::put(exec, propertyName, value, slot); |
| 226 | return; |
| 227 | } |
| 228 | if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length) |
| 229 | return; |
| 230 | Base::put(exec, propertyName, value, slot); |
| 231 | } |
| 232 | |
| 233 | bool JSFunction::deleteProperty(ExecState* exec, const Identifier& propertyName) |
| 234 | { |
| 235 | if (isHostFunction()) |
| 236 | return Base::deleteProperty(exec, propertyName); |
| 237 | if (propertyName == exec->propertyNames().arguments || propertyName == exec->propertyNames().length) |
| 238 | return false; |
| 239 | return Base::deleteProperty(exec, propertyName); |
| 240 | } |
| 241 | |
| 242 | // ECMA 13.2.2 [[Construct]] |
| 243 | ConstructType JSFunction::getConstructData(ConstructData& constructData) |
| 244 | { |
| 245 | if (isHostFunction()) |
| 246 | return ConstructTypeNone; |
| 247 | constructData.js.functionExecutable = jsExecutable(); |
| 248 | constructData.js.scopeChain = scopeChain().node(); |
| 249 | return ConstructTypeJS; |
| 250 | } |
| 251 | |
| 252 | JSObject* JSFunction::construct(ExecState* exec, const ArgList& args) |
| 253 | { |
| 254 | ASSERT(!isHostFunction()); |
| 255 | Structure* structure; |
| 256 | JSValue prototype = get(exec, propertyName: exec->propertyNames().prototype); |
| 257 | if (prototype.isObject()) |
| 258 | structure = asObject(value: prototype)->inheritorID(); |
| 259 | else |
| 260 | structure = exec->lexicalGlobalObject()->emptyObjectStructure(); |
| 261 | JSObject* thisObj = new (exec) JSObject(structure); |
| 262 | |
| 263 | JSValue result = exec->interpreter()->execute(jsExecutable(), exec, this, thisObj, args, scopeChain().node(), exception: exec->exceptionSlot()); |
| 264 | if (exec->hadException() || !result.isObject()) |
| 265 | return thisObj; |
| 266 | return asObject(value: result); |
| 267 | } |
| 268 | |
| 269 | } // namespace JSC |
| 270 | |