Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * vim: set ts=8 sts=4 et sw=4 tw=99: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | /* |
michael@0 | 8 | * JS function support. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "jsfuninlines.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "mozilla/ArrayUtils.h" |
michael@0 | 14 | #include "mozilla/PodOperations.h" |
michael@0 | 15 | |
michael@0 | 16 | #include <string.h> |
michael@0 | 17 | |
michael@0 | 18 | #include "jsapi.h" |
michael@0 | 19 | #include "jsarray.h" |
michael@0 | 20 | #include "jsatom.h" |
michael@0 | 21 | #include "jscntxt.h" |
michael@0 | 22 | #include "jsobj.h" |
michael@0 | 23 | #include "jsproxy.h" |
michael@0 | 24 | #include "jsscript.h" |
michael@0 | 25 | #include "jsstr.h" |
michael@0 | 26 | #include "jstypes.h" |
michael@0 | 27 | #include "jswrapper.h" |
michael@0 | 28 | |
michael@0 | 29 | #include "builtin/Eval.h" |
michael@0 | 30 | #include "builtin/Object.h" |
michael@0 | 31 | #include "frontend/BytecodeCompiler.h" |
michael@0 | 32 | #include "frontend/TokenStream.h" |
michael@0 | 33 | #include "gc/Marking.h" |
michael@0 | 34 | #ifdef JS_ION |
michael@0 | 35 | #include "jit/Ion.h" |
michael@0 | 36 | #include "jit/JitFrameIterator.h" |
michael@0 | 37 | #endif |
michael@0 | 38 | #include "vm/Interpreter.h" |
michael@0 | 39 | #include "vm/Shape.h" |
michael@0 | 40 | #include "vm/StringBuffer.h" |
michael@0 | 41 | #include "vm/WrapperObject.h" |
michael@0 | 42 | #include "vm/Xdr.h" |
michael@0 | 43 | |
michael@0 | 44 | #include "jsscriptinlines.h" |
michael@0 | 45 | |
michael@0 | 46 | #include "vm/Interpreter-inl.h" |
michael@0 | 47 | #include "vm/Stack-inl.h" |
michael@0 | 48 | |
michael@0 | 49 | using namespace js; |
michael@0 | 50 | using namespace js::gc; |
michael@0 | 51 | using namespace js::types; |
michael@0 | 52 | using namespace js::frontend; |
michael@0 | 53 | |
michael@0 | 54 | using mozilla::ArrayLength; |
michael@0 | 55 | using mozilla::PodCopy; |
michael@0 | 56 | |
michael@0 | 57 | static bool |
michael@0 | 58 | fun_getProperty(JSContext *cx, HandleObject obj_, HandleId id, MutableHandleValue vp) |
michael@0 | 59 | { |
michael@0 | 60 | RootedObject obj(cx, obj_); |
michael@0 | 61 | while (!obj->is<JSFunction>()) { |
michael@0 | 62 | if (!JSObject::getProto(cx, obj, &obj)) |
michael@0 | 63 | return false; |
michael@0 | 64 | if (!obj) |
michael@0 | 65 | return true; |
michael@0 | 66 | } |
michael@0 | 67 | RootedFunction fun(cx, &obj->as<JSFunction>()); |
michael@0 | 68 | |
michael@0 | 69 | /* Set to early to null in case of error */ |
michael@0 | 70 | vp.setNull(); |
michael@0 | 71 | |
michael@0 | 72 | /* Find fun's top-most activation record. */ |
michael@0 | 73 | NonBuiltinScriptFrameIter iter(cx); |
michael@0 | 74 | for (; !iter.done(); ++iter) { |
michael@0 | 75 | if (!iter.isFunctionFrame() || iter.isEvalFrame()) |
michael@0 | 76 | continue; |
michael@0 | 77 | if (iter.callee() == fun) |
michael@0 | 78 | break; |
michael@0 | 79 | } |
michael@0 | 80 | if (iter.done()) |
michael@0 | 81 | return true; |
michael@0 | 82 | |
michael@0 | 83 | if (JSID_IS_ATOM(id, cx->names().arguments)) { |
michael@0 | 84 | if (fun->hasRest()) { |
michael@0 | 85 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, |
michael@0 | 86 | JSMSG_FUNCTION_ARGUMENTS_AND_REST); |
michael@0 | 87 | return false; |
michael@0 | 88 | } |
michael@0 | 89 | /* Warn if strict about f.arguments or equivalent unqualified uses. */ |
michael@0 | 90 | if (!JS_ReportErrorFlagsAndNumber(cx, JSREPORT_WARNING | JSREPORT_STRICT, js_GetErrorMessage, |
michael@0 | 91 | nullptr, JSMSG_DEPRECATED_USAGE, js_arguments_str)) { |
michael@0 | 92 | return false; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | ArgumentsObject *argsobj = ArgumentsObject::createUnexpected(cx, iter); |
michael@0 | 96 | if (!argsobj) |
michael@0 | 97 | return false; |
michael@0 | 98 | |
michael@0 | 99 | #ifdef JS_ION |
michael@0 | 100 | // Disabling compiling of this script in IonMonkey. |
michael@0 | 101 | // IonMonkey does not guarantee |f.arguments| can be |
michael@0 | 102 | // fully recovered, so we try to mitigate observing this behavior by |
michael@0 | 103 | // detecting its use early. |
michael@0 | 104 | JSScript *script = iter.script(); |
michael@0 | 105 | jit::ForbidCompilation(cx, script); |
michael@0 | 106 | #endif |
michael@0 | 107 | |
michael@0 | 108 | vp.setObject(*argsobj); |
michael@0 | 109 | return true; |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | if (JSID_IS_ATOM(id, cx->names().caller)) { |
michael@0 | 113 | ++iter; |
michael@0 | 114 | if (iter.done() || !iter.isFunctionFrame()) { |
michael@0 | 115 | JS_ASSERT(vp.isNull()); |
michael@0 | 116 | return true; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | /* Callsite clones should never escape to script. */ |
michael@0 | 120 | JSObject &maybeClone = iter.calleev().toObject(); |
michael@0 | 121 | if (maybeClone.is<JSFunction>()) |
michael@0 | 122 | vp.setObject(*maybeClone.as<JSFunction>().originalFunction()); |
michael@0 | 123 | else |
michael@0 | 124 | vp.set(iter.calleev()); |
michael@0 | 125 | |
michael@0 | 126 | if (!cx->compartment()->wrap(cx, vp)) |
michael@0 | 127 | return false; |
michael@0 | 128 | |
michael@0 | 129 | /* |
michael@0 | 130 | * Censor the caller if we don't have full access to it. |
michael@0 | 131 | */ |
michael@0 | 132 | RootedObject caller(cx, &vp.toObject()); |
michael@0 | 133 | if (caller->is<WrapperObject>() && Wrapper::wrapperHandler(caller)->hasSecurityPolicy()) { |
michael@0 | 134 | vp.setNull(); |
michael@0 | 135 | } else if (caller->is<JSFunction>()) { |
michael@0 | 136 | JSFunction *callerFun = &caller->as<JSFunction>(); |
michael@0 | 137 | if (callerFun->isInterpreted() && callerFun->strict()) { |
michael@0 | 138 | JS_ReportErrorFlagsAndNumber(cx, JSREPORT_ERROR, js_GetErrorMessage, nullptr, |
michael@0 | 139 | JSMSG_CALLER_IS_STRICT); |
michael@0 | 140 | return false; |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | return true; |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | MOZ_ASSUME_UNREACHABLE("fun_getProperty"); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | |
michael@0 | 151 | |
michael@0 | 152 | /* NB: no sentinels at ends -- use ArrayLength to bound loops. |
michael@0 | 153 | * Properties censored into [[ThrowTypeError]] in strict mode. */ |
michael@0 | 154 | static const uint16_t poisonPillProps[] = { |
michael@0 | 155 | NAME_OFFSET(arguments), |
michael@0 | 156 | NAME_OFFSET(caller), |
michael@0 | 157 | }; |
michael@0 | 158 | |
michael@0 | 159 | static bool |
michael@0 | 160 | fun_enumerate(JSContext *cx, HandleObject obj) |
michael@0 | 161 | { |
michael@0 | 162 | JS_ASSERT(obj->is<JSFunction>()); |
michael@0 | 163 | |
michael@0 | 164 | RootedId id(cx); |
michael@0 | 165 | bool found; |
michael@0 | 166 | |
michael@0 | 167 | if (!obj->isBoundFunction() && !obj->as<JSFunction>().isArrow()) { |
michael@0 | 168 | id = NameToId(cx->names().prototype); |
michael@0 | 169 | if (!JSObject::hasProperty(cx, obj, id, &found)) |
michael@0 | 170 | return false; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | id = NameToId(cx->names().length); |
michael@0 | 174 | if (!JSObject::hasProperty(cx, obj, id, &found)) |
michael@0 | 175 | return false; |
michael@0 | 176 | |
michael@0 | 177 | id = NameToId(cx->names().name); |
michael@0 | 178 | if (!JSObject::hasProperty(cx, obj, id, &found)) |
michael@0 | 179 | return false; |
michael@0 | 180 | |
michael@0 | 181 | for (unsigned i = 0; i < ArrayLength(poisonPillProps); i++) { |
michael@0 | 182 | const uint16_t offset = poisonPillProps[i]; |
michael@0 | 183 | id = NameToId(AtomStateOffsetToName(cx->names(), offset)); |
michael@0 | 184 | if (!JSObject::hasProperty(cx, obj, id, &found)) |
michael@0 | 185 | return false; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | return true; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | static JSObject * |
michael@0 | 192 | ResolveInterpretedFunctionPrototype(JSContext *cx, HandleObject obj) |
michael@0 | 193 | { |
michael@0 | 194 | #ifdef DEBUG |
michael@0 | 195 | JSFunction *fun = &obj->as<JSFunction>(); |
michael@0 | 196 | JS_ASSERT(fun->isInterpreted()); |
michael@0 | 197 | JS_ASSERT(!fun->isFunctionPrototype()); |
michael@0 | 198 | #endif |
michael@0 | 199 | |
michael@0 | 200 | // Assert that fun is not a compiler-created function object, which |
michael@0 | 201 | // must never leak to script or embedding code and then be mutated. |
michael@0 | 202 | // Also assert that obj is not bound, per the ES5 15.3.4.5 ref above. |
michael@0 | 203 | JS_ASSERT(!IsInternalFunctionObject(obj)); |
michael@0 | 204 | JS_ASSERT(!obj->isBoundFunction()); |
michael@0 | 205 | |
michael@0 | 206 | // Make the prototype object an instance of Object with the same parent as |
michael@0 | 207 | // the function object itself, unless the function is an ES6 generator. In |
michael@0 | 208 | // that case, per the 15 July 2013 ES6 draft, section 15.19.3, its parent is |
michael@0 | 209 | // the GeneratorObjectPrototype singleton. |
michael@0 | 210 | bool isStarGenerator = obj->as<JSFunction>().isStarGenerator(); |
michael@0 | 211 | Rooted<GlobalObject*> global(cx, &obj->global()); |
michael@0 | 212 | JSObject *objProto; |
michael@0 | 213 | if (isStarGenerator) |
michael@0 | 214 | objProto = GlobalObject::getOrCreateStarGeneratorObjectPrototype(cx, global); |
michael@0 | 215 | else |
michael@0 | 216 | objProto = obj->global().getOrCreateObjectPrototype(cx); |
michael@0 | 217 | if (!objProto) |
michael@0 | 218 | return nullptr; |
michael@0 | 219 | const Class *clasp = &JSObject::class_; |
michael@0 | 220 | |
michael@0 | 221 | RootedObject proto(cx, NewObjectWithGivenProto(cx, clasp, objProto, nullptr, SingletonObject)); |
michael@0 | 222 | if (!proto) |
michael@0 | 223 | return nullptr; |
michael@0 | 224 | |
michael@0 | 225 | // Per ES5 15.3.5.2 a user-defined function's .prototype property is |
michael@0 | 226 | // initially non-configurable, non-enumerable, and writable. |
michael@0 | 227 | RootedValue protoVal(cx, ObjectValue(*proto)); |
michael@0 | 228 | if (!JSObject::defineProperty(cx, obj, cx->names().prototype, |
michael@0 | 229 | protoVal, JS_PropertyStub, JS_StrictPropertyStub, |
michael@0 | 230 | JSPROP_PERMANENT)) |
michael@0 | 231 | { |
michael@0 | 232 | return nullptr; |
michael@0 | 233 | } |
michael@0 | 234 | |
michael@0 | 235 | // Per ES5 13.2 the prototype's .constructor property is configurable, |
michael@0 | 236 | // non-enumerable, and writable. However, per the 15 July 2013 ES6 draft, |
michael@0 | 237 | // section 15.19.3, the .prototype of a generator function does not link |
michael@0 | 238 | // back with a .constructor. |
michael@0 | 239 | if (!isStarGenerator) { |
michael@0 | 240 | RootedValue objVal(cx, ObjectValue(*obj)); |
michael@0 | 241 | if (!JSObject::defineProperty(cx, proto, cx->names().constructor, |
michael@0 | 242 | objVal, JS_PropertyStub, JS_StrictPropertyStub, 0)) |
michael@0 | 243 | { |
michael@0 | 244 | return nullptr; |
michael@0 | 245 | } |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | return proto; |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | bool |
michael@0 | 252 | js::FunctionHasResolveHook(const JSAtomState &atomState, PropertyName *name) |
michael@0 | 253 | { |
michael@0 | 254 | if (name == atomState.prototype || name == atomState.length || name == atomState.name) |
michael@0 | 255 | return true; |
michael@0 | 256 | |
michael@0 | 257 | for (unsigned i = 0; i < ArrayLength(poisonPillProps); i++) { |
michael@0 | 258 | const uint16_t offset = poisonPillProps[i]; |
michael@0 | 259 | |
michael@0 | 260 | if (name == AtomStateOffsetToName(atomState, offset)) |
michael@0 | 261 | return true; |
michael@0 | 262 | } |
michael@0 | 263 | |
michael@0 | 264 | return false; |
michael@0 | 265 | } |
michael@0 | 266 | |
michael@0 | 267 | bool |
michael@0 | 268 | js::fun_resolve(JSContext *cx, HandleObject obj, HandleId id, MutableHandleObject objp) |
michael@0 | 269 | { |
michael@0 | 270 | if (!JSID_IS_ATOM(id)) |
michael@0 | 271 | return true; |
michael@0 | 272 | |
michael@0 | 273 | RootedFunction fun(cx, &obj->as<JSFunction>()); |
michael@0 | 274 | |
michael@0 | 275 | if (JSID_IS_ATOM(id, cx->names().prototype)) { |
michael@0 | 276 | /* |
michael@0 | 277 | * Built-in functions do not have a .prototype property per ECMA-262, |
michael@0 | 278 | * or (Object.prototype, Function.prototype, etc.) have that property |
michael@0 | 279 | * created eagerly. |
michael@0 | 280 | * |
michael@0 | 281 | * ES5 15.3.4: the non-native function object named Function.prototype |
michael@0 | 282 | * does not have a .prototype property. |
michael@0 | 283 | * |
michael@0 | 284 | * ES5 15.3.4.5: bound functions don't have a prototype property. The |
michael@0 | 285 | * isBuiltin() test covers this case because bound functions are native |
michael@0 | 286 | * (and thus built-in) functions by definition/construction. |
michael@0 | 287 | * |
michael@0 | 288 | * ES6 19.2.4.3: arrow functions also don't have a prototype property. |
michael@0 | 289 | */ |
michael@0 | 290 | if (fun->isBuiltin() || fun->isArrow() || fun->isFunctionPrototype()) |
michael@0 | 291 | return true; |
michael@0 | 292 | |
michael@0 | 293 | if (!ResolveInterpretedFunctionPrototype(cx, fun)) |
michael@0 | 294 | return false; |
michael@0 | 295 | objp.set(fun); |
michael@0 | 296 | return true; |
michael@0 | 297 | } |
michael@0 | 298 | |
michael@0 | 299 | if (JSID_IS_ATOM(id, cx->names().length) || JSID_IS_ATOM(id, cx->names().name)) { |
michael@0 | 300 | JS_ASSERT(!IsInternalFunctionObject(obj)); |
michael@0 | 301 | |
michael@0 | 302 | RootedValue v(cx); |
michael@0 | 303 | if (JSID_IS_ATOM(id, cx->names().length)) { |
michael@0 | 304 | if (fun->isInterpretedLazy() && !fun->getOrCreateScript(cx)) |
michael@0 | 305 | return false; |
michael@0 | 306 | uint16_t length = fun->hasScript() ? fun->nonLazyScript()->funLength() : |
michael@0 | 307 | fun->nargs() - fun->hasRest(); |
michael@0 | 308 | v.setInt32(length); |
michael@0 | 309 | } else { |
michael@0 | 310 | v.setString(fun->atom() == nullptr ? cx->runtime()->emptyString : fun->atom()); |
michael@0 | 311 | } |
michael@0 | 312 | |
michael@0 | 313 | if (!DefineNativeProperty(cx, fun, id, v, JS_PropertyStub, JS_StrictPropertyStub, |
michael@0 | 314 | JSPROP_PERMANENT | JSPROP_READONLY)) { |
michael@0 | 315 | return false; |
michael@0 | 316 | } |
michael@0 | 317 | objp.set(fun); |
michael@0 | 318 | return true; |
michael@0 | 319 | } |
michael@0 | 320 | |
michael@0 | 321 | for (unsigned i = 0; i < ArrayLength(poisonPillProps); i++) { |
michael@0 | 322 | const uint16_t offset = poisonPillProps[i]; |
michael@0 | 323 | |
michael@0 | 324 | if (JSID_IS_ATOM(id, AtomStateOffsetToName(cx->names(), offset))) { |
michael@0 | 325 | JS_ASSERT(!IsInternalFunctionObject(fun)); |
michael@0 | 326 | |
michael@0 | 327 | PropertyOp getter; |
michael@0 | 328 | StrictPropertyOp setter; |
michael@0 | 329 | unsigned attrs = JSPROP_PERMANENT | JSPROP_SHARED; |
michael@0 | 330 | if (fun->isInterpretedLazy() && !fun->getOrCreateScript(cx)) |
michael@0 | 331 | return false; |
michael@0 | 332 | if (fun->isInterpreted() ? fun->strict() : fun->isBoundFunction()) { |
michael@0 | 333 | JSObject *throwTypeError = fun->global().getThrowTypeError(); |
michael@0 | 334 | |
michael@0 | 335 | getter = CastAsPropertyOp(throwTypeError); |
michael@0 | 336 | setter = CastAsStrictPropertyOp(throwTypeError); |
michael@0 | 337 | attrs |= JSPROP_GETTER | JSPROP_SETTER; |
michael@0 | 338 | } else { |
michael@0 | 339 | getter = fun_getProperty; |
michael@0 | 340 | setter = JS_StrictPropertyStub; |
michael@0 | 341 | } |
michael@0 | 342 | |
michael@0 | 343 | if (!DefineNativeProperty(cx, fun, id, UndefinedHandleValue, getter, setter, attrs)) |
michael@0 | 344 | return false; |
michael@0 | 345 | objp.set(fun); |
michael@0 | 346 | return true; |
michael@0 | 347 | } |
michael@0 | 348 | } |
michael@0 | 349 | |
michael@0 | 350 | return true; |
michael@0 | 351 | } |
michael@0 | 352 | |
michael@0 | 353 | template<XDRMode mode> |
michael@0 | 354 | bool |
michael@0 | 355 | js::XDRInterpretedFunction(XDRState<mode> *xdr, HandleObject enclosingScope, HandleScript enclosingScript, |
michael@0 | 356 | MutableHandleObject objp) |
michael@0 | 357 | { |
michael@0 | 358 | enum FirstWordFlag { |
michael@0 | 359 | HasAtom = 0x1, |
michael@0 | 360 | IsStarGenerator = 0x2, |
michael@0 | 361 | IsLazy = 0x4, |
michael@0 | 362 | HasSingletonType = 0x8 |
michael@0 | 363 | }; |
michael@0 | 364 | |
michael@0 | 365 | /* NB: Keep this in sync with CloneFunctionAndScript. */ |
michael@0 | 366 | RootedAtom atom(xdr->cx()); |
michael@0 | 367 | uint32_t firstword = 0; /* bitmask of FirstWordFlag */ |
michael@0 | 368 | uint32_t flagsword = 0; /* word for argument count and fun->flags */ |
michael@0 | 369 | |
michael@0 | 370 | JSContext *cx = xdr->cx(); |
michael@0 | 371 | RootedFunction fun(cx); |
michael@0 | 372 | RootedScript script(cx); |
michael@0 | 373 | Rooted<LazyScript *> lazy(cx); |
michael@0 | 374 | |
michael@0 | 375 | if (mode == XDR_ENCODE) { |
michael@0 | 376 | fun = &objp->as<JSFunction>(); |
michael@0 | 377 | if (!fun->isInterpreted()) { |
michael@0 | 378 | JSAutoByteString funNameBytes; |
michael@0 | 379 | if (const char *name = GetFunctionNameBytes(cx, fun, &funNameBytes)) { |
michael@0 | 380 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, |
michael@0 | 381 | JSMSG_NOT_SCRIPTED_FUNCTION, name); |
michael@0 | 382 | } |
michael@0 | 383 | return false; |
michael@0 | 384 | } |
michael@0 | 385 | |
michael@0 | 386 | if (fun->atom() || fun->hasGuessedAtom()) |
michael@0 | 387 | firstword |= HasAtom; |
michael@0 | 388 | |
michael@0 | 389 | if (fun->isStarGenerator()) |
michael@0 | 390 | firstword |= IsStarGenerator; |
michael@0 | 391 | |
michael@0 | 392 | if (fun->isInterpretedLazy()) { |
michael@0 | 393 | // This can only happen for re-lazified cloned functions, so this |
michael@0 | 394 | // does not apply to any JSFunction produced by the parser, only to |
michael@0 | 395 | // JSFunction created by the runtime. |
michael@0 | 396 | JS_ASSERT(!fun->lazyScript()->maybeScript()); |
michael@0 | 397 | |
michael@0 | 398 | // Encode a lazy script. |
michael@0 | 399 | firstword |= IsLazy; |
michael@0 | 400 | lazy = fun->lazyScript(); |
michael@0 | 401 | } else { |
michael@0 | 402 | // Encode the script. |
michael@0 | 403 | script = fun->nonLazyScript(); |
michael@0 | 404 | } |
michael@0 | 405 | |
michael@0 | 406 | if (fun->hasSingletonType()) |
michael@0 | 407 | firstword |= HasSingletonType; |
michael@0 | 408 | |
michael@0 | 409 | atom = fun->displayAtom(); |
michael@0 | 410 | flagsword = (fun->nargs() << 16) | fun->flags(); |
michael@0 | 411 | |
michael@0 | 412 | // The environment of any function which is not reused will always be |
michael@0 | 413 | // null, it is later defined when a function is cloned or reused to |
michael@0 | 414 | // mirror the scope chain. |
michael@0 | 415 | JS_ASSERT_IF(fun->hasSingletonType() && |
michael@0 | 416 | !((lazy && lazy->hasBeenCloned()) || (script && script->hasBeenCloned())), |
michael@0 | 417 | fun->environment() == nullptr); |
michael@0 | 418 | } |
michael@0 | 419 | |
michael@0 | 420 | if (!xdr->codeUint32(&firstword)) |
michael@0 | 421 | return false; |
michael@0 | 422 | |
michael@0 | 423 | if ((firstword & HasAtom) && !XDRAtom(xdr, &atom)) |
michael@0 | 424 | return false; |
michael@0 | 425 | if (!xdr->codeUint32(&flagsword)) |
michael@0 | 426 | return false; |
michael@0 | 427 | |
michael@0 | 428 | if (mode == XDR_DECODE) { |
michael@0 | 429 | JSObject *proto = nullptr; |
michael@0 | 430 | if (firstword & IsStarGenerator) { |
michael@0 | 431 | proto = GlobalObject::getOrCreateStarGeneratorFunctionPrototype(cx, cx->global()); |
michael@0 | 432 | if (!proto) |
michael@0 | 433 | return false; |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | gc::AllocKind allocKind = JSFunction::FinalizeKind; |
michael@0 | 437 | if (uint16_t(flagsword) & JSFunction::EXTENDED) |
michael@0 | 438 | allocKind = JSFunction::ExtendedFinalizeKind; |
michael@0 | 439 | fun = NewFunctionWithProto(cx, NullPtr(), nullptr, 0, JSFunction::INTERPRETED, |
michael@0 | 440 | /* parent = */ NullPtr(), NullPtr(), proto, |
michael@0 | 441 | allocKind, TenuredObject); |
michael@0 | 442 | if (!fun) |
michael@0 | 443 | return false; |
michael@0 | 444 | script = nullptr; |
michael@0 | 445 | } |
michael@0 | 446 | |
michael@0 | 447 | if (firstword & IsLazy) { |
michael@0 | 448 | if (!XDRLazyScript(xdr, enclosingScope, enclosingScript, fun, &lazy)) |
michael@0 | 449 | return false; |
michael@0 | 450 | } else { |
michael@0 | 451 | if (!XDRScript(xdr, enclosingScope, enclosingScript, fun, &script)) |
michael@0 | 452 | return false; |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | if (mode == XDR_DECODE) { |
michael@0 | 456 | fun->setArgCount(flagsword >> 16); |
michael@0 | 457 | fun->setFlags(uint16_t(flagsword)); |
michael@0 | 458 | fun->initAtom(atom); |
michael@0 | 459 | if (firstword & IsLazy) { |
michael@0 | 460 | fun->initLazyScript(lazy); |
michael@0 | 461 | } else { |
michael@0 | 462 | fun->initScript(script); |
michael@0 | 463 | script->setFunction(fun); |
michael@0 | 464 | JS_ASSERT(fun->nargs() == script->bindings.numArgs()); |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | bool singleton = firstword & HasSingletonType; |
michael@0 | 468 | if (!JSFunction::setTypeForScriptedFunction(cx, fun, singleton)) |
michael@0 | 469 | return false; |
michael@0 | 470 | objp.set(fun); |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | return true; |
michael@0 | 474 | } |
michael@0 | 475 | |
michael@0 | 476 | template bool |
michael@0 | 477 | js::XDRInterpretedFunction(XDRState<XDR_ENCODE> *, HandleObject, HandleScript, MutableHandleObject); |
michael@0 | 478 | |
michael@0 | 479 | template bool |
michael@0 | 480 | js::XDRInterpretedFunction(XDRState<XDR_DECODE> *, HandleObject, HandleScript, MutableHandleObject); |
michael@0 | 481 | |
michael@0 | 482 | JSObject * |
michael@0 | 483 | js::CloneFunctionAndScript(JSContext *cx, HandleObject enclosingScope, HandleFunction srcFun) |
michael@0 | 484 | { |
michael@0 | 485 | /* NB: Keep this in sync with XDRInterpretedFunction. */ |
michael@0 | 486 | JSObject *cloneProto = nullptr; |
michael@0 | 487 | if (srcFun->isStarGenerator()) { |
michael@0 | 488 | cloneProto = GlobalObject::getOrCreateStarGeneratorFunctionPrototype(cx, cx->global()); |
michael@0 | 489 | if (!cloneProto) |
michael@0 | 490 | return nullptr; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | gc::AllocKind allocKind = JSFunction::FinalizeKind; |
michael@0 | 494 | if (srcFun->isExtended()) |
michael@0 | 495 | allocKind = JSFunction::ExtendedFinalizeKind; |
michael@0 | 496 | RootedFunction clone(cx, NewFunctionWithProto(cx, NullPtr(), nullptr, 0, |
michael@0 | 497 | JSFunction::INTERPRETED, NullPtr(), NullPtr(), |
michael@0 | 498 | cloneProto, allocKind, TenuredObject)); |
michael@0 | 499 | if (!clone) |
michael@0 | 500 | return nullptr; |
michael@0 | 501 | |
michael@0 | 502 | RootedScript srcScript(cx, srcFun->getOrCreateScript(cx)); |
michael@0 | 503 | if (!srcScript) |
michael@0 | 504 | return nullptr; |
michael@0 | 505 | RootedScript clonedScript(cx, CloneScript(cx, enclosingScope, clone, srcScript)); |
michael@0 | 506 | if (!clonedScript) |
michael@0 | 507 | return nullptr; |
michael@0 | 508 | |
michael@0 | 509 | clone->setArgCount(srcFun->nargs()); |
michael@0 | 510 | clone->setFlags(srcFun->flags()); |
michael@0 | 511 | clone->initAtom(srcFun->displayAtom()); |
michael@0 | 512 | clone->initScript(clonedScript); |
michael@0 | 513 | clonedScript->setFunction(clone); |
michael@0 | 514 | if (!JSFunction::setTypeForScriptedFunction(cx, clone)) |
michael@0 | 515 | return nullptr; |
michael@0 | 516 | |
michael@0 | 517 | RootedScript cloneScript(cx, clone->nonLazyScript()); |
michael@0 | 518 | CallNewScriptHook(cx, cloneScript, clone); |
michael@0 | 519 | return clone; |
michael@0 | 520 | } |
michael@0 | 521 | |
michael@0 | 522 | /* |
michael@0 | 523 | * [[HasInstance]] internal method for Function objects: fetch the .prototype |
michael@0 | 524 | * property of its 'this' parameter, and walks the prototype chain of v (only |
michael@0 | 525 | * if v is an object) returning true if .prototype is found. |
michael@0 | 526 | */ |
michael@0 | 527 | static bool |
michael@0 | 528 | fun_hasInstance(JSContext *cx, HandleObject objArg, MutableHandleValue v, bool *bp) |
michael@0 | 529 | { |
michael@0 | 530 | RootedObject obj(cx, objArg); |
michael@0 | 531 | |
michael@0 | 532 | while (obj->is<JSFunction>() && obj->isBoundFunction()) |
michael@0 | 533 | obj = obj->as<JSFunction>().getBoundFunctionTarget(); |
michael@0 | 534 | |
michael@0 | 535 | RootedValue pval(cx); |
michael@0 | 536 | if (!JSObject::getProperty(cx, obj, obj, cx->names().prototype, &pval)) |
michael@0 | 537 | return false; |
michael@0 | 538 | |
michael@0 | 539 | if (pval.isPrimitive()) { |
michael@0 | 540 | /* |
michael@0 | 541 | * Throw a runtime error if instanceof is called on a function that |
michael@0 | 542 | * has a non-object as its .prototype value. |
michael@0 | 543 | */ |
michael@0 | 544 | RootedValue val(cx, ObjectValue(*obj)); |
michael@0 | 545 | js_ReportValueError(cx, JSMSG_BAD_PROTOTYPE, -1, val, js::NullPtr()); |
michael@0 | 546 | return false; |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | RootedObject pobj(cx, &pval.toObject()); |
michael@0 | 550 | bool isDelegate; |
michael@0 | 551 | if (!IsDelegate(cx, pobj, v, &isDelegate)) |
michael@0 | 552 | return false; |
michael@0 | 553 | *bp = isDelegate; |
michael@0 | 554 | return true; |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | inline void |
michael@0 | 558 | JSFunction::trace(JSTracer *trc) |
michael@0 | 559 | { |
michael@0 | 560 | if (isExtended()) { |
michael@0 | 561 | MarkValueRange(trc, ArrayLength(toExtended()->extendedSlots), |
michael@0 | 562 | toExtended()->extendedSlots, "nativeReserved"); |
michael@0 | 563 | } |
michael@0 | 564 | |
michael@0 | 565 | if (atom_) |
michael@0 | 566 | MarkString(trc, &atom_, "atom"); |
michael@0 | 567 | |
michael@0 | 568 | if (isInterpreted()) { |
michael@0 | 569 | // Functions can be be marked as interpreted despite having no script |
michael@0 | 570 | // yet at some points when parsing, and can be lazy with no lazy script |
michael@0 | 571 | // for self-hosted code. |
michael@0 | 572 | if (hasScript() && u.i.s.script_) { |
michael@0 | 573 | // Functions can be relazified under the following conditions: |
michael@0 | 574 | // - their compartment isn't currently executing scripts or being |
michael@0 | 575 | // debugged |
michael@0 | 576 | // - they are not in the self-hosting compartment |
michael@0 | 577 | // - they aren't generators |
michael@0 | 578 | // - they don't have JIT code attached |
michael@0 | 579 | // - they haven't ever been inlined |
michael@0 | 580 | // - they don't have child functions |
michael@0 | 581 | // - they have information for un-lazifying them again later |
michael@0 | 582 | // This information can either be a LazyScript, or the name of a |
michael@0 | 583 | // self-hosted function which can be cloned over again. The latter |
michael@0 | 584 | // is stored in the first extended slot. |
michael@0 | 585 | if (IS_GC_MARKING_TRACER(trc) && !compartment()->hasBeenEntered() && |
michael@0 | 586 | !compartment()->debugMode() && !compartment()->isSelfHosting && |
michael@0 | 587 | u.i.s.script_->isRelazifiable() && (!isSelfHostedBuiltin() || isExtended())) |
michael@0 | 588 | { |
michael@0 | 589 | relazify(trc); |
michael@0 | 590 | } else { |
michael@0 | 591 | MarkScriptUnbarriered(trc, &u.i.s.script_, "script"); |
michael@0 | 592 | } |
michael@0 | 593 | } else if (isInterpretedLazy() && u.i.s.lazy_) { |
michael@0 | 594 | MarkLazyScriptUnbarriered(trc, &u.i.s.lazy_, "lazyScript"); |
michael@0 | 595 | } |
michael@0 | 596 | if (u.i.env_) |
michael@0 | 597 | MarkObjectUnbarriered(trc, &u.i.env_, "fun_callscope"); |
michael@0 | 598 | } |
michael@0 | 599 | } |
michael@0 | 600 | |
michael@0 | 601 | static void |
michael@0 | 602 | fun_trace(JSTracer *trc, JSObject *obj) |
michael@0 | 603 | { |
michael@0 | 604 | obj->as<JSFunction>().trace(trc); |
michael@0 | 605 | } |
michael@0 | 606 | |
michael@0 | 607 | const Class JSFunction::class_ = { |
michael@0 | 608 | js_Function_str, |
michael@0 | 609 | JSCLASS_NEW_RESOLVE | JSCLASS_IMPLEMENTS_BARRIERS | |
michael@0 | 610 | JSCLASS_HAS_CACHED_PROTO(JSProto_Function), |
michael@0 | 611 | JS_PropertyStub, /* addProperty */ |
michael@0 | 612 | JS_DeletePropertyStub, /* delProperty */ |
michael@0 | 613 | JS_PropertyStub, /* getProperty */ |
michael@0 | 614 | JS_StrictPropertyStub, /* setProperty */ |
michael@0 | 615 | fun_enumerate, |
michael@0 | 616 | (JSResolveOp)js::fun_resolve, |
michael@0 | 617 | JS_ConvertStub, |
michael@0 | 618 | nullptr, /* finalize */ |
michael@0 | 619 | nullptr, /* call */ |
michael@0 | 620 | fun_hasInstance, |
michael@0 | 621 | nullptr, /* construct */ |
michael@0 | 622 | fun_trace |
michael@0 | 623 | }; |
michael@0 | 624 | |
michael@0 | 625 | const Class* const js::FunctionClassPtr = &JSFunction::class_; |
michael@0 | 626 | |
michael@0 | 627 | /* Find the body of a function (not including braces). */ |
michael@0 | 628 | bool |
michael@0 | 629 | js::FindBody(JSContext *cx, HandleFunction fun, ConstTwoByteChars chars, size_t length, |
michael@0 | 630 | size_t *bodyStart, size_t *bodyEnd) |
michael@0 | 631 | { |
michael@0 | 632 | // We don't need principals, since those are only used for error reporting. |
michael@0 | 633 | CompileOptions options(cx); |
michael@0 | 634 | options.setFileAndLine("internal-findBody", 0); |
michael@0 | 635 | |
michael@0 | 636 | // For asm.js modules, there's no script. |
michael@0 | 637 | if (fun->hasScript()) |
michael@0 | 638 | options.setVersion(fun->nonLazyScript()->getVersion()); |
michael@0 | 639 | |
michael@0 | 640 | AutoKeepAtoms keepAtoms(cx->perThreadData); |
michael@0 | 641 | TokenStream ts(cx, options, chars.get(), length, nullptr); |
michael@0 | 642 | int nest = 0; |
michael@0 | 643 | bool onward = true; |
michael@0 | 644 | // Skip arguments list. |
michael@0 | 645 | do { |
michael@0 | 646 | switch (ts.getToken()) { |
michael@0 | 647 | case TOK_NAME: |
michael@0 | 648 | case TOK_YIELD: |
michael@0 | 649 | if (nest == 0) |
michael@0 | 650 | onward = false; |
michael@0 | 651 | break; |
michael@0 | 652 | case TOK_LP: |
michael@0 | 653 | nest++; |
michael@0 | 654 | break; |
michael@0 | 655 | case TOK_RP: |
michael@0 | 656 | if (--nest == 0) |
michael@0 | 657 | onward = false; |
michael@0 | 658 | break; |
michael@0 | 659 | case TOK_ERROR: |
michael@0 | 660 | // Must be memory. |
michael@0 | 661 | return false; |
michael@0 | 662 | default: |
michael@0 | 663 | break; |
michael@0 | 664 | } |
michael@0 | 665 | } while (onward); |
michael@0 | 666 | TokenKind tt = ts.getToken(); |
michael@0 | 667 | if (tt == TOK_ARROW) |
michael@0 | 668 | tt = ts.getToken(); |
michael@0 | 669 | if (tt == TOK_ERROR) |
michael@0 | 670 | return false; |
michael@0 | 671 | bool braced = tt == TOK_LC; |
michael@0 | 672 | JS_ASSERT_IF(fun->isExprClosure(), !braced); |
michael@0 | 673 | *bodyStart = ts.currentToken().pos.begin; |
michael@0 | 674 | if (braced) |
michael@0 | 675 | *bodyStart += 1; |
michael@0 | 676 | ConstTwoByteChars end(chars.get() + length, chars.get(), length); |
michael@0 | 677 | if (end[-1] == '}') { |
michael@0 | 678 | end--; |
michael@0 | 679 | } else { |
michael@0 | 680 | JS_ASSERT(!braced); |
michael@0 | 681 | for (; unicode::IsSpaceOrBOM2(end[-1]); end--) |
michael@0 | 682 | ; |
michael@0 | 683 | } |
michael@0 | 684 | *bodyEnd = end - chars; |
michael@0 | 685 | JS_ASSERT(*bodyStart <= *bodyEnd); |
michael@0 | 686 | return true; |
michael@0 | 687 | } |
michael@0 | 688 | |
michael@0 | 689 | JSString * |
michael@0 | 690 | js::FunctionToString(JSContext *cx, HandleFunction fun, bool bodyOnly, bool lambdaParen) |
michael@0 | 691 | { |
michael@0 | 692 | if (fun->isInterpretedLazy() && !fun->getOrCreateScript(cx)) |
michael@0 | 693 | return nullptr; |
michael@0 | 694 | |
michael@0 | 695 | if (IsAsmJSModule(fun)) |
michael@0 | 696 | return AsmJSModuleToString(cx, fun, !lambdaParen); |
michael@0 | 697 | if (IsAsmJSFunction(fun)) |
michael@0 | 698 | return AsmJSFunctionToString(cx, fun); |
michael@0 | 699 | |
michael@0 | 700 | StringBuffer out(cx); |
michael@0 | 701 | RootedScript script(cx); |
michael@0 | 702 | |
michael@0 | 703 | if (fun->hasScript()) { |
michael@0 | 704 | script = fun->nonLazyScript(); |
michael@0 | 705 | if (script->isGeneratorExp()) { |
michael@0 | 706 | if ((!bodyOnly && !out.append("function genexp() {")) || |
michael@0 | 707 | !out.append("\n [generator expression]\n") || |
michael@0 | 708 | (!bodyOnly && !out.append("}"))) |
michael@0 | 709 | { |
michael@0 | 710 | return nullptr; |
michael@0 | 711 | } |
michael@0 | 712 | return out.finishString(); |
michael@0 | 713 | } |
michael@0 | 714 | } |
michael@0 | 715 | if (!bodyOnly) { |
michael@0 | 716 | // If we're not in pretty mode, put parentheses around lambda functions. |
michael@0 | 717 | if (fun->isInterpreted() && !lambdaParen && fun->isLambda() && !fun->isArrow()) { |
michael@0 | 718 | if (!out.append("(")) |
michael@0 | 719 | return nullptr; |
michael@0 | 720 | } |
michael@0 | 721 | if (!fun->isArrow()) { |
michael@0 | 722 | if (!(fun->isStarGenerator() ? out.append("function* ") : out.append("function "))) |
michael@0 | 723 | return nullptr; |
michael@0 | 724 | } |
michael@0 | 725 | if (fun->atom()) { |
michael@0 | 726 | if (!out.append(fun->atom())) |
michael@0 | 727 | return nullptr; |
michael@0 | 728 | } |
michael@0 | 729 | } |
michael@0 | 730 | bool haveSource = fun->isInterpreted() && !fun->isSelfHostedBuiltin(); |
michael@0 | 731 | if (haveSource && !script->scriptSource()->hasSourceData() && |
michael@0 | 732 | !JSScript::loadSource(cx, script->scriptSource(), &haveSource)) |
michael@0 | 733 | { |
michael@0 | 734 | return nullptr; |
michael@0 | 735 | } |
michael@0 | 736 | if (haveSource) { |
michael@0 | 737 | RootedString srcStr(cx, script->sourceData(cx)); |
michael@0 | 738 | if (!srcStr) |
michael@0 | 739 | return nullptr; |
michael@0 | 740 | Rooted<JSFlatString *> src(cx, srcStr->ensureFlat(cx)); |
michael@0 | 741 | if (!src) |
michael@0 | 742 | return nullptr; |
michael@0 | 743 | |
michael@0 | 744 | ConstTwoByteChars chars(src->chars(), src->length()); |
michael@0 | 745 | bool exprBody = fun->isExprClosure(); |
michael@0 | 746 | |
michael@0 | 747 | // The source data for functions created by calling the Function |
michael@0 | 748 | // constructor is only the function's body. This depends on the fact, |
michael@0 | 749 | // asserted below, that in Function("function f() {}"), the inner |
michael@0 | 750 | // function's sourceStart points to the '(', not the 'f'. |
michael@0 | 751 | bool funCon = !fun->isArrow() && |
michael@0 | 752 | script->sourceStart() == 0 && |
michael@0 | 753 | script->sourceEnd() == script->scriptSource()->length() && |
michael@0 | 754 | script->scriptSource()->argumentsNotIncluded(); |
michael@0 | 755 | |
michael@0 | 756 | // Functions created with the constructor can't be arrow functions or |
michael@0 | 757 | // expression closures. |
michael@0 | 758 | JS_ASSERT_IF(funCon, !fun->isArrow()); |
michael@0 | 759 | JS_ASSERT_IF(funCon, !exprBody); |
michael@0 | 760 | JS_ASSERT_IF(!funCon && !fun->isArrow(), src->length() > 0 && chars[0] == '('); |
michael@0 | 761 | |
michael@0 | 762 | // If a function inherits strict mode by having scopes above it that |
michael@0 | 763 | // have "use strict", we insert "use strict" into the body of the |
michael@0 | 764 | // function. This ensures that if the result of toString is evaled, the |
michael@0 | 765 | // resulting function will have the same semantics. |
michael@0 | 766 | bool addUseStrict = script->strict() && !script->explicitUseStrict() && !fun->isArrow(); |
michael@0 | 767 | |
michael@0 | 768 | bool buildBody = funCon && !bodyOnly; |
michael@0 | 769 | if (buildBody) { |
michael@0 | 770 | // This function was created with the Function constructor. We don't |
michael@0 | 771 | // have source for the arguments, so we have to generate that. Part |
michael@0 | 772 | // of bug 755821 should be cobbling the arguments passed into the |
michael@0 | 773 | // Function constructor into the source string. |
michael@0 | 774 | if (!out.append("(")) |
michael@0 | 775 | return nullptr; |
michael@0 | 776 | |
michael@0 | 777 | // Fish out the argument names. |
michael@0 | 778 | BindingVector *localNames = cx->new_<BindingVector>(cx); |
michael@0 | 779 | ScopedJSDeletePtr<BindingVector> freeNames(localNames); |
michael@0 | 780 | if (!FillBindingVector(script, localNames)) |
michael@0 | 781 | return nullptr; |
michael@0 | 782 | for (unsigned i = 0; i < fun->nargs(); i++) { |
michael@0 | 783 | if ((i && !out.append(", ")) || |
michael@0 | 784 | (i == unsigned(fun->nargs() - 1) && fun->hasRest() && !out.append("...")) || |
michael@0 | 785 | !out.append((*localNames)[i].name())) { |
michael@0 | 786 | return nullptr; |
michael@0 | 787 | } |
michael@0 | 788 | } |
michael@0 | 789 | if (!out.append(") {\n")) |
michael@0 | 790 | return nullptr; |
michael@0 | 791 | } |
michael@0 | 792 | if ((bodyOnly && !funCon) || addUseStrict) { |
michael@0 | 793 | // We need to get at the body either because we're only supposed to |
michael@0 | 794 | // return the body or we need to insert "use strict" into the body. |
michael@0 | 795 | size_t bodyStart = 0, bodyEnd; |
michael@0 | 796 | |
michael@0 | 797 | // If the function is defined in the Function constructor, we |
michael@0 | 798 | // already have a body. |
michael@0 | 799 | if (!funCon) { |
michael@0 | 800 | JS_ASSERT(!buildBody); |
michael@0 | 801 | if (!FindBody(cx, fun, chars, src->length(), &bodyStart, &bodyEnd)) |
michael@0 | 802 | return nullptr; |
michael@0 | 803 | } else { |
michael@0 | 804 | bodyEnd = src->length(); |
michael@0 | 805 | } |
michael@0 | 806 | |
michael@0 | 807 | if (addUseStrict) { |
michael@0 | 808 | // Output source up to beginning of body. |
michael@0 | 809 | if (!out.append(chars, bodyStart)) |
michael@0 | 810 | return nullptr; |
michael@0 | 811 | if (exprBody) { |
michael@0 | 812 | // We can't insert a statement into a function with an |
michael@0 | 813 | // expression body. Do what the decompiler did, and insert a |
michael@0 | 814 | // comment. |
michael@0 | 815 | if (!out.append("/* use strict */ ")) |
michael@0 | 816 | return nullptr; |
michael@0 | 817 | } else { |
michael@0 | 818 | if (!out.append("\n\"use strict\";\n")) |
michael@0 | 819 | return nullptr; |
michael@0 | 820 | } |
michael@0 | 821 | } |
michael@0 | 822 | |
michael@0 | 823 | // Output just the body (for bodyOnly) or the body and possibly |
michael@0 | 824 | // closing braces (for addUseStrict). |
michael@0 | 825 | size_t dependentEnd = bodyOnly ? bodyEnd : src->length(); |
michael@0 | 826 | if (!out.append(chars + bodyStart, dependentEnd - bodyStart)) |
michael@0 | 827 | return nullptr; |
michael@0 | 828 | } else { |
michael@0 | 829 | if (!out.append(src)) |
michael@0 | 830 | return nullptr; |
michael@0 | 831 | } |
michael@0 | 832 | if (buildBody) { |
michael@0 | 833 | if (!out.append("\n}")) |
michael@0 | 834 | return nullptr; |
michael@0 | 835 | } |
michael@0 | 836 | if (bodyOnly) { |
michael@0 | 837 | // Slap a semicolon on the end of functions with an expression body. |
michael@0 | 838 | if (exprBody && !out.append(";")) |
michael@0 | 839 | return nullptr; |
michael@0 | 840 | } else if (!lambdaParen && fun->isLambda() && !fun->isArrow()) { |
michael@0 | 841 | if (!out.append(")")) |
michael@0 | 842 | return nullptr; |
michael@0 | 843 | } |
michael@0 | 844 | } else if (fun->isInterpreted() && !fun->isSelfHostedBuiltin()) { |
michael@0 | 845 | if ((!bodyOnly && !out.append("() {\n ")) || |
michael@0 | 846 | !out.append("[sourceless code]") || |
michael@0 | 847 | (!bodyOnly && !out.append("\n}"))) |
michael@0 | 848 | return nullptr; |
michael@0 | 849 | if (!lambdaParen && fun->isLambda() && !fun->isArrow() && !out.append(")")) |
michael@0 | 850 | return nullptr; |
michael@0 | 851 | } else { |
michael@0 | 852 | JS_ASSERT(!fun->isExprClosure()); |
michael@0 | 853 | |
michael@0 | 854 | if ((!bodyOnly && !out.append("() {\n ")) |
michael@0 | 855 | || !out.append("[native code]") |
michael@0 | 856 | || (!bodyOnly && !out.append("\n}"))) |
michael@0 | 857 | { |
michael@0 | 858 | return nullptr; |
michael@0 | 859 | } |
michael@0 | 860 | } |
michael@0 | 861 | return out.finishString(); |
michael@0 | 862 | } |
michael@0 | 863 | |
michael@0 | 864 | JSString * |
michael@0 | 865 | fun_toStringHelper(JSContext *cx, HandleObject obj, unsigned indent) |
michael@0 | 866 | { |
michael@0 | 867 | if (!obj->is<JSFunction>()) { |
michael@0 | 868 | if (obj->is<ProxyObject>()) |
michael@0 | 869 | return Proxy::fun_toString(cx, obj, indent); |
michael@0 | 870 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, |
michael@0 | 871 | JSMSG_INCOMPATIBLE_PROTO, |
michael@0 | 872 | js_Function_str, js_toString_str, |
michael@0 | 873 | "object"); |
michael@0 | 874 | return nullptr; |
michael@0 | 875 | } |
michael@0 | 876 | |
michael@0 | 877 | RootedFunction fun(cx, &obj->as<JSFunction>()); |
michael@0 | 878 | return FunctionToString(cx, fun, false, indent != JS_DONT_PRETTY_PRINT); |
michael@0 | 879 | } |
michael@0 | 880 | |
michael@0 | 881 | static bool |
michael@0 | 882 | fun_toString(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 883 | { |
michael@0 | 884 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 885 | JS_ASSERT(IsFunctionObject(args.calleev())); |
michael@0 | 886 | |
michael@0 | 887 | uint32_t indent = 0; |
michael@0 | 888 | |
michael@0 | 889 | if (args.length() != 0 && !ToUint32(cx, args[0], &indent)) |
michael@0 | 890 | return false; |
michael@0 | 891 | |
michael@0 | 892 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 893 | if (!obj) |
michael@0 | 894 | return false; |
michael@0 | 895 | |
michael@0 | 896 | RootedString str(cx, fun_toStringHelper(cx, obj, indent)); |
michael@0 | 897 | if (!str) |
michael@0 | 898 | return false; |
michael@0 | 899 | |
michael@0 | 900 | args.rval().setString(str); |
michael@0 | 901 | return true; |
michael@0 | 902 | } |
michael@0 | 903 | |
michael@0 | 904 | #if JS_HAS_TOSOURCE |
michael@0 | 905 | static bool |
michael@0 | 906 | fun_toSource(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 907 | { |
michael@0 | 908 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 909 | JS_ASSERT(IsFunctionObject(args.calleev())); |
michael@0 | 910 | |
michael@0 | 911 | RootedObject obj(cx, ToObject(cx, args.thisv())); |
michael@0 | 912 | if (!obj) |
michael@0 | 913 | return false; |
michael@0 | 914 | |
michael@0 | 915 | RootedString str(cx); |
michael@0 | 916 | if (obj->isCallable()) |
michael@0 | 917 | str = fun_toStringHelper(cx, obj, JS_DONT_PRETTY_PRINT); |
michael@0 | 918 | else |
michael@0 | 919 | str = ObjectToSource(cx, obj); |
michael@0 | 920 | |
michael@0 | 921 | if (!str) |
michael@0 | 922 | return false; |
michael@0 | 923 | args.rval().setString(str); |
michael@0 | 924 | return true; |
michael@0 | 925 | } |
michael@0 | 926 | #endif |
michael@0 | 927 | |
michael@0 | 928 | bool |
michael@0 | 929 | js_fun_call(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 930 | { |
michael@0 | 931 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 932 | |
michael@0 | 933 | HandleValue fval = args.thisv(); |
michael@0 | 934 | if (!js_IsCallable(fval)) { |
michael@0 | 935 | ReportIncompatibleMethod(cx, args, &JSFunction::class_); |
michael@0 | 936 | return false; |
michael@0 | 937 | } |
michael@0 | 938 | |
michael@0 | 939 | args.setCallee(fval); |
michael@0 | 940 | args.setThis(args.get(0)); |
michael@0 | 941 | |
michael@0 | 942 | if (args.length() > 0) { |
michael@0 | 943 | for (size_t i = 0; i < args.length() - 1; i++) |
michael@0 | 944 | args[i].set(args[i + 1]); |
michael@0 | 945 | args = CallArgsFromVp(args.length() - 1, vp); |
michael@0 | 946 | } |
michael@0 | 947 | |
michael@0 | 948 | return Invoke(cx, args); |
michael@0 | 949 | } |
michael@0 | 950 | |
michael@0 | 951 | // ES5 15.3.4.3 |
michael@0 | 952 | bool |
michael@0 | 953 | js_fun_apply(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 954 | { |
michael@0 | 955 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 956 | |
michael@0 | 957 | // Step 1. |
michael@0 | 958 | HandleValue fval = args.thisv(); |
michael@0 | 959 | if (!js_IsCallable(fval)) { |
michael@0 | 960 | ReportIncompatibleMethod(cx, args, &JSFunction::class_); |
michael@0 | 961 | return false; |
michael@0 | 962 | } |
michael@0 | 963 | |
michael@0 | 964 | // Step 2. |
michael@0 | 965 | if (args.length() < 2 || args[1].isNullOrUndefined()) |
michael@0 | 966 | return js_fun_call(cx, (args.length() > 0) ? 1 : 0, vp); |
michael@0 | 967 | |
michael@0 | 968 | InvokeArgs args2(cx); |
michael@0 | 969 | |
michael@0 | 970 | // A JS_OPTIMIZED_ARGUMENTS magic value means that 'arguments' flows into |
michael@0 | 971 | // this apply call from a scripted caller and, as an optimization, we've |
michael@0 | 972 | // avoided creating it since apply can simply pull the argument values from |
michael@0 | 973 | // the calling frame (which we must do now). |
michael@0 | 974 | if (args[1].isMagic(JS_OPTIMIZED_ARGUMENTS)) { |
michael@0 | 975 | // Step 3-6. |
michael@0 | 976 | ScriptFrameIter iter(cx); |
michael@0 | 977 | JS_ASSERT(iter.numActualArgs() <= ARGS_LENGTH_MAX); |
michael@0 | 978 | if (!args2.init(iter.numActualArgs())) |
michael@0 | 979 | return false; |
michael@0 | 980 | |
michael@0 | 981 | args2.setCallee(fval); |
michael@0 | 982 | args2.setThis(args[0]); |
michael@0 | 983 | |
michael@0 | 984 | // Steps 7-8. |
michael@0 | 985 | iter.unaliasedForEachActual(cx, CopyTo(args2.array())); |
michael@0 | 986 | } else { |
michael@0 | 987 | // Step 3. |
michael@0 | 988 | if (!args[1].isObject()) { |
michael@0 | 989 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, |
michael@0 | 990 | JSMSG_BAD_APPLY_ARGS, js_apply_str); |
michael@0 | 991 | return false; |
michael@0 | 992 | } |
michael@0 | 993 | |
michael@0 | 994 | // Steps 4-5 (note erratum removing steps originally numbered 5 and 7 in |
michael@0 | 995 | // original version of ES5). |
michael@0 | 996 | RootedObject aobj(cx, &args[1].toObject()); |
michael@0 | 997 | uint32_t length; |
michael@0 | 998 | if (!GetLengthProperty(cx, aobj, &length)) |
michael@0 | 999 | return false; |
michael@0 | 1000 | |
michael@0 | 1001 | // Step 6. |
michael@0 | 1002 | if (length > ARGS_LENGTH_MAX) { |
michael@0 | 1003 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_TOO_MANY_FUN_APPLY_ARGS); |
michael@0 | 1004 | return false; |
michael@0 | 1005 | } |
michael@0 | 1006 | |
michael@0 | 1007 | if (!args2.init(length)) |
michael@0 | 1008 | return false; |
michael@0 | 1009 | |
michael@0 | 1010 | // Push fval, obj, and aobj's elements as args. |
michael@0 | 1011 | args2.setCallee(fval); |
michael@0 | 1012 | args2.setThis(args[0]); |
michael@0 | 1013 | |
michael@0 | 1014 | // Steps 7-8. |
michael@0 | 1015 | if (!GetElements(cx, aobj, length, args2.array())) |
michael@0 | 1016 | return false; |
michael@0 | 1017 | } |
michael@0 | 1018 | |
michael@0 | 1019 | // Step 9. |
michael@0 | 1020 | if (!Invoke(cx, args2)) |
michael@0 | 1021 | return false; |
michael@0 | 1022 | |
michael@0 | 1023 | args.rval().set(args2.rval()); |
michael@0 | 1024 | return true; |
michael@0 | 1025 | } |
michael@0 | 1026 | |
michael@0 | 1027 | static const uint32_t JSSLOT_BOUND_FUNCTION_THIS = 0; |
michael@0 | 1028 | static const uint32_t JSSLOT_BOUND_FUNCTION_ARGS_COUNT = 1; |
michael@0 | 1029 | |
michael@0 | 1030 | static const uint32_t BOUND_FUNCTION_RESERVED_SLOTS = 2; |
michael@0 | 1031 | |
michael@0 | 1032 | inline bool |
michael@0 | 1033 | JSFunction::initBoundFunction(JSContext *cx, HandleValue thisArg, |
michael@0 | 1034 | const Value *args, unsigned argslen) |
michael@0 | 1035 | { |
michael@0 | 1036 | RootedFunction self(cx, this); |
michael@0 | 1037 | |
michael@0 | 1038 | /* |
michael@0 | 1039 | * Convert to a dictionary to set the BOUND_FUNCTION flag and increase |
michael@0 | 1040 | * the slot span to cover the arguments and additional slots for the 'this' |
michael@0 | 1041 | * value and arguments count. |
michael@0 | 1042 | */ |
michael@0 | 1043 | if (!self->toDictionaryMode(cx)) |
michael@0 | 1044 | return false; |
michael@0 | 1045 | |
michael@0 | 1046 | if (!self->setFlag(cx, BaseShape::BOUND_FUNCTION)) |
michael@0 | 1047 | return false; |
michael@0 | 1048 | |
michael@0 | 1049 | if (!JSObject::setSlotSpan(cx, self, BOUND_FUNCTION_RESERVED_SLOTS + argslen)) |
michael@0 | 1050 | return false; |
michael@0 | 1051 | |
michael@0 | 1052 | self->setSlot(JSSLOT_BOUND_FUNCTION_THIS, thisArg); |
michael@0 | 1053 | self->setSlot(JSSLOT_BOUND_FUNCTION_ARGS_COUNT, PrivateUint32Value(argslen)); |
michael@0 | 1054 | |
michael@0 | 1055 | self->initSlotRange(BOUND_FUNCTION_RESERVED_SLOTS, args, argslen); |
michael@0 | 1056 | |
michael@0 | 1057 | return true; |
michael@0 | 1058 | } |
michael@0 | 1059 | |
michael@0 | 1060 | const js::Value & |
michael@0 | 1061 | JSFunction::getBoundFunctionThis() const |
michael@0 | 1062 | { |
michael@0 | 1063 | JS_ASSERT(isBoundFunction()); |
michael@0 | 1064 | |
michael@0 | 1065 | return getSlot(JSSLOT_BOUND_FUNCTION_THIS); |
michael@0 | 1066 | } |
michael@0 | 1067 | |
michael@0 | 1068 | const js::Value & |
michael@0 | 1069 | JSFunction::getBoundFunctionArgument(unsigned which) const |
michael@0 | 1070 | { |
michael@0 | 1071 | JS_ASSERT(isBoundFunction()); |
michael@0 | 1072 | JS_ASSERT(which < getBoundFunctionArgumentCount()); |
michael@0 | 1073 | |
michael@0 | 1074 | return getSlot(BOUND_FUNCTION_RESERVED_SLOTS + which); |
michael@0 | 1075 | } |
michael@0 | 1076 | |
michael@0 | 1077 | size_t |
michael@0 | 1078 | JSFunction::getBoundFunctionArgumentCount() const |
michael@0 | 1079 | { |
michael@0 | 1080 | JS_ASSERT(isBoundFunction()); |
michael@0 | 1081 | |
michael@0 | 1082 | return getSlot(JSSLOT_BOUND_FUNCTION_ARGS_COUNT).toPrivateUint32(); |
michael@0 | 1083 | } |
michael@0 | 1084 | |
michael@0 | 1085 | /* static */ bool |
michael@0 | 1086 | JSFunction::createScriptForLazilyInterpretedFunction(JSContext *cx, HandleFunction fun) |
michael@0 | 1087 | { |
michael@0 | 1088 | JS_ASSERT(fun->isInterpretedLazy()); |
michael@0 | 1089 | |
michael@0 | 1090 | Rooted<LazyScript*> lazy(cx, fun->lazyScriptOrNull()); |
michael@0 | 1091 | if (lazy) { |
michael@0 | 1092 | // Trigger a pre barrier on the lazy script being overwritten. |
michael@0 | 1093 | if (cx->zone()->needsBarrier()) |
michael@0 | 1094 | LazyScript::writeBarrierPre(lazy); |
michael@0 | 1095 | |
michael@0 | 1096 | // Suppress GC for now although we should be able to remove this by |
michael@0 | 1097 | // making 'lazy' a Rooted<LazyScript*> (which requires adding a |
michael@0 | 1098 | // THING_ROOT_LAZY_SCRIPT). |
michael@0 | 1099 | AutoSuppressGC suppressGC(cx); |
michael@0 | 1100 | |
michael@0 | 1101 | RootedScript script(cx, lazy->maybeScript()); |
michael@0 | 1102 | |
michael@0 | 1103 | if (script) { |
michael@0 | 1104 | fun->setUnlazifiedScript(script); |
michael@0 | 1105 | // Remember the lazy script on the compiled script, so it can be |
michael@0 | 1106 | // stored on the function again in case of re-lazification. |
michael@0 | 1107 | // Only functions without inner functions are re-lazified. |
michael@0 | 1108 | if (!lazy->numInnerFunctions()) |
michael@0 | 1109 | script->setLazyScript(lazy); |
michael@0 | 1110 | return true; |
michael@0 | 1111 | } |
michael@0 | 1112 | |
michael@0 | 1113 | if (fun != lazy->functionNonDelazifying()) { |
michael@0 | 1114 | if (!lazy->functionDelazifying(cx)) |
michael@0 | 1115 | return false; |
michael@0 | 1116 | script = lazy->functionNonDelazifying()->nonLazyScript(); |
michael@0 | 1117 | if (!script) |
michael@0 | 1118 | return false; |
michael@0 | 1119 | |
michael@0 | 1120 | fun->setUnlazifiedScript(script); |
michael@0 | 1121 | return true; |
michael@0 | 1122 | } |
michael@0 | 1123 | |
michael@0 | 1124 | // Lazy script caching is only supported for leaf functions. If a |
michael@0 | 1125 | // script with inner functions was returned by the cache, those inner |
michael@0 | 1126 | // functions would be delazified when deep cloning the script, even if |
michael@0 | 1127 | // they have never executed. |
michael@0 | 1128 | // |
michael@0 | 1129 | // Additionally, the lazy script cache is not used during incremental |
michael@0 | 1130 | // GCs, to avoid resurrecting dead scripts after incremental sweeping |
michael@0 | 1131 | // has started. |
michael@0 | 1132 | if (!lazy->numInnerFunctions() && !JS::IsIncrementalGCInProgress(cx->runtime())) { |
michael@0 | 1133 | LazyScriptCache::Lookup lookup(cx, lazy); |
michael@0 | 1134 | cx->runtime()->lazyScriptCache.lookup(lookup, script.address()); |
michael@0 | 1135 | } |
michael@0 | 1136 | |
michael@0 | 1137 | if (script) { |
michael@0 | 1138 | RootedObject enclosingScope(cx, lazy->enclosingScope()); |
michael@0 | 1139 | RootedScript clonedScript(cx, CloneScript(cx, enclosingScope, fun, script)); |
michael@0 | 1140 | if (!clonedScript) |
michael@0 | 1141 | return false; |
michael@0 | 1142 | |
michael@0 | 1143 | clonedScript->setSourceObject(lazy->sourceObject()); |
michael@0 | 1144 | |
michael@0 | 1145 | fun->initAtom(script->functionNonDelazifying()->displayAtom()); |
michael@0 | 1146 | clonedScript->setFunction(fun); |
michael@0 | 1147 | |
michael@0 | 1148 | fun->setUnlazifiedScript(clonedScript); |
michael@0 | 1149 | |
michael@0 | 1150 | CallNewScriptHook(cx, clonedScript, fun); |
michael@0 | 1151 | |
michael@0 | 1152 | if (!lazy->maybeScript()) |
michael@0 | 1153 | lazy->initScript(clonedScript); |
michael@0 | 1154 | return true; |
michael@0 | 1155 | } |
michael@0 | 1156 | |
michael@0 | 1157 | JS_ASSERT(lazy->source()->hasSourceData()); |
michael@0 | 1158 | |
michael@0 | 1159 | // Parse and compile the script from source. |
michael@0 | 1160 | SourceDataCache::AutoHoldEntry holder; |
michael@0 | 1161 | const jschar *chars = lazy->source()->chars(cx, holder); |
michael@0 | 1162 | if (!chars) |
michael@0 | 1163 | return false; |
michael@0 | 1164 | |
michael@0 | 1165 | const jschar *lazyStart = chars + lazy->begin(); |
michael@0 | 1166 | size_t lazyLength = lazy->end() - lazy->begin(); |
michael@0 | 1167 | |
michael@0 | 1168 | if (!frontend::CompileLazyFunction(cx, lazy, lazyStart, lazyLength)) |
michael@0 | 1169 | return false; |
michael@0 | 1170 | |
michael@0 | 1171 | script = fun->nonLazyScript(); |
michael@0 | 1172 | |
michael@0 | 1173 | // Remember the compiled script on the lazy script itself, in case |
michael@0 | 1174 | // there are clones of the function still pointing to the lazy script. |
michael@0 | 1175 | if (!lazy->maybeScript()) |
michael@0 | 1176 | lazy->initScript(script); |
michael@0 | 1177 | |
michael@0 | 1178 | // Try to insert the newly compiled script into the lazy script cache. |
michael@0 | 1179 | if (!lazy->numInnerFunctions()) { |
michael@0 | 1180 | // A script's starting column isn't set by the bytecode emitter, so |
michael@0 | 1181 | // specify this from the lazy script so that if an identical lazy |
michael@0 | 1182 | // script is encountered later a match can be determined. |
michael@0 | 1183 | script->setColumn(lazy->column()); |
michael@0 | 1184 | |
michael@0 | 1185 | LazyScriptCache::Lookup lookup(cx, lazy); |
michael@0 | 1186 | cx->runtime()->lazyScriptCache.insert(lookup, script); |
michael@0 | 1187 | |
michael@0 | 1188 | // Remember the lazy script on the compiled script, so it can be |
michael@0 | 1189 | // stored on the function again in case of re-lazification. |
michael@0 | 1190 | // Only functions without inner functions are re-lazified. |
michael@0 | 1191 | script->setLazyScript(lazy); |
michael@0 | 1192 | } |
michael@0 | 1193 | return true; |
michael@0 | 1194 | } |
michael@0 | 1195 | |
michael@0 | 1196 | /* Lazily cloned self-hosted script. */ |
michael@0 | 1197 | JS_ASSERT(fun->isSelfHostedBuiltin()); |
michael@0 | 1198 | RootedAtom funAtom(cx, &fun->getExtendedSlot(0).toString()->asAtom()); |
michael@0 | 1199 | if (!funAtom) |
michael@0 | 1200 | return false; |
michael@0 | 1201 | Rooted<PropertyName *> funName(cx, funAtom->asPropertyName()); |
michael@0 | 1202 | return cx->runtime()->cloneSelfHostedFunctionScript(cx, funName, fun); |
michael@0 | 1203 | } |
michael@0 | 1204 | |
michael@0 | 1205 | void |
michael@0 | 1206 | JSFunction::relazify(JSTracer *trc) |
michael@0 | 1207 | { |
michael@0 | 1208 | JSScript *script = nonLazyScript(); |
michael@0 | 1209 | JS_ASSERT(script->isRelazifiable()); |
michael@0 | 1210 | JS_ASSERT(!compartment()->hasBeenEntered()); |
michael@0 | 1211 | JS_ASSERT(!compartment()->debugMode()); |
michael@0 | 1212 | |
michael@0 | 1213 | // If the script's canonical function isn't lazy, we have to mark the |
michael@0 | 1214 | // script. Otherwise, the following scenario would leave it unmarked |
michael@0 | 1215 | // and cause it to be swept while a function is still expecting it to be |
michael@0 | 1216 | // valid: |
michael@0 | 1217 | // 1. an incremental GC slice causes the canonical function to relazify |
michael@0 | 1218 | // 2. a clone is used and delazifies the canonical function |
michael@0 | 1219 | // 3. another GC slice causes the clone to relazify |
michael@0 | 1220 | // The result is that no function marks the script, but the canonical |
michael@0 | 1221 | // function expects it to be valid. |
michael@0 | 1222 | if (script->functionNonDelazifying()->hasScript()) |
michael@0 | 1223 | MarkScriptUnbarriered(trc, &u.i.s.script_, "script"); |
michael@0 | 1224 | |
michael@0 | 1225 | flags_ &= ~INTERPRETED; |
michael@0 | 1226 | flags_ |= INTERPRETED_LAZY; |
michael@0 | 1227 | LazyScript *lazy = script->maybeLazyScript(); |
michael@0 | 1228 | u.i.s.lazy_ = lazy; |
michael@0 | 1229 | if (lazy) { |
michael@0 | 1230 | JS_ASSERT(!isSelfHostedBuiltin()); |
michael@0 | 1231 | // If this is the script stored in the lazy script to be cloned |
michael@0 | 1232 | // for un-lazifying other functions, reset it so the script can |
michael@0 | 1233 | // be freed. |
michael@0 | 1234 | if (lazy->maybeScript() == script) |
michael@0 | 1235 | lazy->resetScript(); |
michael@0 | 1236 | MarkLazyScriptUnbarriered(trc, &u.i.s.lazy_, "lazyScript"); |
michael@0 | 1237 | } else { |
michael@0 | 1238 | JS_ASSERT(isSelfHostedBuiltin()); |
michael@0 | 1239 | JS_ASSERT(isExtended()); |
michael@0 | 1240 | JS_ASSERT(getExtendedSlot(0).toString()->isAtom()); |
michael@0 | 1241 | } |
michael@0 | 1242 | } |
michael@0 | 1243 | |
michael@0 | 1244 | /* ES5 15.3.4.5.1 and 15.3.4.5.2. */ |
michael@0 | 1245 | bool |
michael@0 | 1246 | js::CallOrConstructBoundFunction(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1247 | { |
michael@0 | 1248 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1249 | RootedFunction fun(cx, &args.callee().as<JSFunction>()); |
michael@0 | 1250 | JS_ASSERT(fun->isBoundFunction()); |
michael@0 | 1251 | |
michael@0 | 1252 | /* 15.3.4.5.1 step 1, 15.3.4.5.2 step 3. */ |
michael@0 | 1253 | unsigned argslen = fun->getBoundFunctionArgumentCount(); |
michael@0 | 1254 | |
michael@0 | 1255 | if (args.length() + argslen > ARGS_LENGTH_MAX) { |
michael@0 | 1256 | js_ReportAllocationOverflow(cx); |
michael@0 | 1257 | return false; |
michael@0 | 1258 | } |
michael@0 | 1259 | |
michael@0 | 1260 | /* 15.3.4.5.1 step 3, 15.3.4.5.2 step 1. */ |
michael@0 | 1261 | RootedObject target(cx, fun->getBoundFunctionTarget()); |
michael@0 | 1262 | |
michael@0 | 1263 | /* 15.3.4.5.1 step 2. */ |
michael@0 | 1264 | const Value &boundThis = fun->getBoundFunctionThis(); |
michael@0 | 1265 | |
michael@0 | 1266 | InvokeArgs invokeArgs(cx); |
michael@0 | 1267 | if (!invokeArgs.init(args.length() + argslen)) |
michael@0 | 1268 | return false; |
michael@0 | 1269 | |
michael@0 | 1270 | /* 15.3.4.5.1, 15.3.4.5.2 step 4. */ |
michael@0 | 1271 | for (unsigned i = 0; i < argslen; i++) |
michael@0 | 1272 | invokeArgs[i].set(fun->getBoundFunctionArgument(i)); |
michael@0 | 1273 | PodCopy(invokeArgs.array() + argslen, vp + 2, args.length()); |
michael@0 | 1274 | |
michael@0 | 1275 | /* 15.3.4.5.1, 15.3.4.5.2 step 5. */ |
michael@0 | 1276 | invokeArgs.setCallee(ObjectValue(*target)); |
michael@0 | 1277 | |
michael@0 | 1278 | bool constructing = args.isConstructing(); |
michael@0 | 1279 | if (!constructing) |
michael@0 | 1280 | invokeArgs.setThis(boundThis); |
michael@0 | 1281 | |
michael@0 | 1282 | if (constructing ? !InvokeConstructor(cx, invokeArgs) : !Invoke(cx, invokeArgs)) |
michael@0 | 1283 | return false; |
michael@0 | 1284 | |
michael@0 | 1285 | args.rval().set(invokeArgs.rval()); |
michael@0 | 1286 | return true; |
michael@0 | 1287 | } |
michael@0 | 1288 | |
michael@0 | 1289 | static bool |
michael@0 | 1290 | fun_isGenerator(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1291 | { |
michael@0 | 1292 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1293 | JSFunction *fun; |
michael@0 | 1294 | if (!IsFunctionObject(args.thisv(), &fun)) { |
michael@0 | 1295 | args.rval().setBoolean(false); |
michael@0 | 1296 | return true; |
michael@0 | 1297 | } |
michael@0 | 1298 | |
michael@0 | 1299 | args.rval().setBoolean(fun->isGenerator()); |
michael@0 | 1300 | return true; |
michael@0 | 1301 | } |
michael@0 | 1302 | |
michael@0 | 1303 | /* ES5 15.3.4.5. */ |
michael@0 | 1304 | static bool |
michael@0 | 1305 | fun_bind(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1306 | { |
michael@0 | 1307 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1308 | |
michael@0 | 1309 | /* Step 1. */ |
michael@0 | 1310 | Value thisv = args.thisv(); |
michael@0 | 1311 | |
michael@0 | 1312 | /* Step 2. */ |
michael@0 | 1313 | if (!js_IsCallable(thisv)) { |
michael@0 | 1314 | ReportIncompatibleMethod(cx, args, &JSFunction::class_); |
michael@0 | 1315 | return false; |
michael@0 | 1316 | } |
michael@0 | 1317 | |
michael@0 | 1318 | /* Step 3. */ |
michael@0 | 1319 | Value *boundArgs = nullptr; |
michael@0 | 1320 | unsigned argslen = 0; |
michael@0 | 1321 | if (args.length() > 1) { |
michael@0 | 1322 | boundArgs = args.array() + 1; |
michael@0 | 1323 | argslen = args.length() - 1; |
michael@0 | 1324 | } |
michael@0 | 1325 | |
michael@0 | 1326 | /* Steps 7-9. */ |
michael@0 | 1327 | RootedValue thisArg(cx, args.length() >= 1 ? args[0] : UndefinedValue()); |
michael@0 | 1328 | RootedObject target(cx, &thisv.toObject()); |
michael@0 | 1329 | JSObject *boundFunction = js_fun_bind(cx, target, thisArg, boundArgs, argslen); |
michael@0 | 1330 | if (!boundFunction) |
michael@0 | 1331 | return false; |
michael@0 | 1332 | |
michael@0 | 1333 | /* Step 22. */ |
michael@0 | 1334 | args.rval().setObject(*boundFunction); |
michael@0 | 1335 | return true; |
michael@0 | 1336 | } |
michael@0 | 1337 | |
michael@0 | 1338 | JSObject* |
michael@0 | 1339 | js_fun_bind(JSContext *cx, HandleObject target, HandleValue thisArg, |
michael@0 | 1340 | Value *boundArgs, unsigned argslen) |
michael@0 | 1341 | { |
michael@0 | 1342 | /* Steps 15-16. */ |
michael@0 | 1343 | unsigned length = 0; |
michael@0 | 1344 | if (target->is<JSFunction>()) { |
michael@0 | 1345 | unsigned nargs = target->as<JSFunction>().nargs(); |
michael@0 | 1346 | if (nargs > argslen) |
michael@0 | 1347 | length = nargs - argslen; |
michael@0 | 1348 | } |
michael@0 | 1349 | |
michael@0 | 1350 | /* Step 4-6, 10-11. */ |
michael@0 | 1351 | RootedAtom name(cx, target->is<JSFunction>() ? target->as<JSFunction>().atom() : nullptr); |
michael@0 | 1352 | |
michael@0 | 1353 | RootedObject funobj(cx, NewFunction(cx, js::NullPtr(), CallOrConstructBoundFunction, length, |
michael@0 | 1354 | JSFunction::NATIVE_CTOR, target, name)); |
michael@0 | 1355 | if (!funobj) |
michael@0 | 1356 | return nullptr; |
michael@0 | 1357 | |
michael@0 | 1358 | /* NB: Bound functions abuse |parent| to store their target. */ |
michael@0 | 1359 | if (!JSObject::setParent(cx, funobj, target)) |
michael@0 | 1360 | return nullptr; |
michael@0 | 1361 | |
michael@0 | 1362 | if (!funobj->as<JSFunction>().initBoundFunction(cx, thisArg, boundArgs, argslen)) |
michael@0 | 1363 | return nullptr; |
michael@0 | 1364 | |
michael@0 | 1365 | /* Steps 17, 19-21 are handled by fun_resolve. */ |
michael@0 | 1366 | /* Step 18 is the default for new functions. */ |
michael@0 | 1367 | return funobj; |
michael@0 | 1368 | } |
michael@0 | 1369 | |
michael@0 | 1370 | /* |
michael@0 | 1371 | * Report "malformed formal parameter" iff no illegal char or similar scanner |
michael@0 | 1372 | * error was already reported. |
michael@0 | 1373 | */ |
michael@0 | 1374 | static bool |
michael@0 | 1375 | OnBadFormal(JSContext *cx, TokenKind tt) |
michael@0 | 1376 | { |
michael@0 | 1377 | if (tt != TOK_ERROR) |
michael@0 | 1378 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_BAD_FORMAL); |
michael@0 | 1379 | else |
michael@0 | 1380 | JS_ASSERT(cx->isExceptionPending()); |
michael@0 | 1381 | return false; |
michael@0 | 1382 | } |
michael@0 | 1383 | |
michael@0 | 1384 | const JSFunctionSpec js::function_methods[] = { |
michael@0 | 1385 | #if JS_HAS_TOSOURCE |
michael@0 | 1386 | JS_FN(js_toSource_str, fun_toSource, 0,0), |
michael@0 | 1387 | #endif |
michael@0 | 1388 | JS_FN(js_toString_str, fun_toString, 0,0), |
michael@0 | 1389 | JS_FN(js_apply_str, js_fun_apply, 2,0), |
michael@0 | 1390 | JS_FN(js_call_str, js_fun_call, 1,0), |
michael@0 | 1391 | JS_FN("bind", fun_bind, 1,0), |
michael@0 | 1392 | JS_FN("isGenerator", fun_isGenerator,0,0), |
michael@0 | 1393 | JS_FS_END |
michael@0 | 1394 | }; |
michael@0 | 1395 | |
michael@0 | 1396 | static bool |
michael@0 | 1397 | FunctionConstructor(JSContext *cx, unsigned argc, Value *vp, GeneratorKind generatorKind) |
michael@0 | 1398 | { |
michael@0 | 1399 | CallArgs args = CallArgsFromVp(argc, vp); |
michael@0 | 1400 | RootedString arg(cx); // used multiple times below |
michael@0 | 1401 | |
michael@0 | 1402 | /* Block this call if security callbacks forbid it. */ |
michael@0 | 1403 | Rooted<GlobalObject*> global(cx, &args.callee().global()); |
michael@0 | 1404 | if (!GlobalObject::isRuntimeCodeGenEnabled(cx, global)) { |
michael@0 | 1405 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_CSP_BLOCKED_FUNCTION); |
michael@0 | 1406 | return false; |
michael@0 | 1407 | } |
michael@0 | 1408 | |
michael@0 | 1409 | AutoKeepAtoms keepAtoms(cx->perThreadData); |
michael@0 | 1410 | AutoNameVector formals(cx); |
michael@0 | 1411 | |
michael@0 | 1412 | bool hasRest = false; |
michael@0 | 1413 | |
michael@0 | 1414 | bool isStarGenerator = generatorKind == StarGenerator; |
michael@0 | 1415 | JS_ASSERT(generatorKind != LegacyGenerator); |
michael@0 | 1416 | |
michael@0 | 1417 | RootedScript maybeScript(cx); |
michael@0 | 1418 | const char *filename; |
michael@0 | 1419 | unsigned lineno; |
michael@0 | 1420 | JSPrincipals *originPrincipals; |
michael@0 | 1421 | uint32_t pcOffset; |
michael@0 | 1422 | DescribeScriptedCallerForCompilation(cx, &maybeScript, &filename, &lineno, &pcOffset, |
michael@0 | 1423 | &originPrincipals); |
michael@0 | 1424 | |
michael@0 | 1425 | const char *introductionType = "Function"; |
michael@0 | 1426 | if (generatorKind != NotGenerator) |
michael@0 | 1427 | introductionType = "GeneratorFunction"; |
michael@0 | 1428 | |
michael@0 | 1429 | const char *introducerFilename = filename; |
michael@0 | 1430 | if (maybeScript && maybeScript->scriptSource()->introducerFilename()) |
michael@0 | 1431 | introducerFilename = maybeScript->scriptSource()->introducerFilename(); |
michael@0 | 1432 | |
michael@0 | 1433 | CompileOptions options(cx); |
michael@0 | 1434 | options.setOriginPrincipals(originPrincipals) |
michael@0 | 1435 | .setFileAndLine(filename, 1) |
michael@0 | 1436 | .setNoScriptRval(false) |
michael@0 | 1437 | .setCompileAndGo(true) |
michael@0 | 1438 | .setIntroductionInfo(introducerFilename, introductionType, lineno, maybeScript, pcOffset); |
michael@0 | 1439 | |
michael@0 | 1440 | unsigned n = args.length() ? args.length() - 1 : 0; |
michael@0 | 1441 | if (n > 0) { |
michael@0 | 1442 | /* |
michael@0 | 1443 | * Collect the function-argument arguments into one string, separated |
michael@0 | 1444 | * by commas, then make a tokenstream from that string, and scan it to |
michael@0 | 1445 | * get the arguments. We need to throw the full scanner at the |
michael@0 | 1446 | * problem, because the argument string can legitimately contain |
michael@0 | 1447 | * comments and linefeeds. XXX It might be better to concatenate |
michael@0 | 1448 | * everything up into a function definition and pass it to the |
michael@0 | 1449 | * compiler, but doing it this way is less of a delta from the old |
michael@0 | 1450 | * code. See ECMA 15.3.2.1. |
michael@0 | 1451 | */ |
michael@0 | 1452 | size_t args_length = 0; |
michael@0 | 1453 | for (unsigned i = 0; i < n; i++) { |
michael@0 | 1454 | /* Collect the lengths for all the function-argument arguments. */ |
michael@0 | 1455 | arg = ToString<CanGC>(cx, args[i]); |
michael@0 | 1456 | if (!arg) |
michael@0 | 1457 | return false; |
michael@0 | 1458 | args[i].setString(arg); |
michael@0 | 1459 | |
michael@0 | 1460 | /* |
michael@0 | 1461 | * Check for overflow. The < test works because the maximum |
michael@0 | 1462 | * JSString length fits in 2 fewer bits than size_t has. |
michael@0 | 1463 | */ |
michael@0 | 1464 | size_t old_args_length = args_length; |
michael@0 | 1465 | args_length = old_args_length + arg->length(); |
michael@0 | 1466 | if (args_length < old_args_length) { |
michael@0 | 1467 | js_ReportAllocationOverflow(cx); |
michael@0 | 1468 | return false; |
michael@0 | 1469 | } |
michael@0 | 1470 | } |
michael@0 | 1471 | |
michael@0 | 1472 | /* Add 1 for each joining comma and check for overflow (two ways). */ |
michael@0 | 1473 | size_t old_args_length = args_length; |
michael@0 | 1474 | args_length = old_args_length + n - 1; |
michael@0 | 1475 | if (args_length < old_args_length || |
michael@0 | 1476 | args_length >= ~(size_t)0 / sizeof(jschar)) { |
michael@0 | 1477 | js_ReportAllocationOverflow(cx); |
michael@0 | 1478 | return false; |
michael@0 | 1479 | } |
michael@0 | 1480 | |
michael@0 | 1481 | /* |
michael@0 | 1482 | * Allocate a string to hold the concatenated arguments, including room |
michael@0 | 1483 | * for a terminating 0. Mark cx->tempLifeAlloc for later release, to |
michael@0 | 1484 | * free collected_args and its tokenstream in one swoop. |
michael@0 | 1485 | */ |
michael@0 | 1486 | LifoAllocScope las(&cx->tempLifoAlloc()); |
michael@0 | 1487 | jschar *cp = cx->tempLifoAlloc().newArray<jschar>(args_length + 1); |
michael@0 | 1488 | if (!cp) { |
michael@0 | 1489 | js_ReportOutOfMemory(cx); |
michael@0 | 1490 | return false; |
michael@0 | 1491 | } |
michael@0 | 1492 | ConstTwoByteChars collected_args(cp, args_length + 1); |
michael@0 | 1493 | |
michael@0 | 1494 | /* |
michael@0 | 1495 | * Concatenate the arguments into the new string, separated by commas. |
michael@0 | 1496 | */ |
michael@0 | 1497 | for (unsigned i = 0; i < n; i++) { |
michael@0 | 1498 | arg = args[i].toString(); |
michael@0 | 1499 | size_t arg_length = arg->length(); |
michael@0 | 1500 | const jschar *arg_chars = arg->getChars(cx); |
michael@0 | 1501 | if (!arg_chars) |
michael@0 | 1502 | return false; |
michael@0 | 1503 | (void) js_strncpy(cp, arg_chars, arg_length); |
michael@0 | 1504 | cp += arg_length; |
michael@0 | 1505 | |
michael@0 | 1506 | /* Add separating comma or terminating 0. */ |
michael@0 | 1507 | *cp++ = (i + 1 < n) ? ',' : 0; |
michael@0 | 1508 | } |
michael@0 | 1509 | |
michael@0 | 1510 | /* |
michael@0 | 1511 | * Initialize a tokenstream that reads from the given string. No |
michael@0 | 1512 | * StrictModeGetter is needed because this TokenStream won't report any |
michael@0 | 1513 | * strict mode errors. Any strict mode errors which might be reported |
michael@0 | 1514 | * here (duplicate argument names, etc.) will be detected when we |
michael@0 | 1515 | * compile the function body. |
michael@0 | 1516 | */ |
michael@0 | 1517 | TokenStream ts(cx, options, collected_args.get(), args_length, |
michael@0 | 1518 | /* strictModeGetter = */ nullptr); |
michael@0 | 1519 | bool yieldIsValidName = ts.versionNumber() < JSVERSION_1_7 && !isStarGenerator; |
michael@0 | 1520 | |
michael@0 | 1521 | /* The argument string may be empty or contain no tokens. */ |
michael@0 | 1522 | TokenKind tt = ts.getToken(); |
michael@0 | 1523 | if (tt != TOK_EOF) { |
michael@0 | 1524 | for (;;) { |
michael@0 | 1525 | /* |
michael@0 | 1526 | * Check that it's a name. This also implicitly guards against |
michael@0 | 1527 | * TOK_ERROR, which was already reported. |
michael@0 | 1528 | */ |
michael@0 | 1529 | if (hasRest) { |
michael@0 | 1530 | ts.reportError(JSMSG_PARAMETER_AFTER_REST); |
michael@0 | 1531 | return false; |
michael@0 | 1532 | } |
michael@0 | 1533 | |
michael@0 | 1534 | if (tt == TOK_YIELD && yieldIsValidName) |
michael@0 | 1535 | tt = TOK_NAME; |
michael@0 | 1536 | |
michael@0 | 1537 | if (tt != TOK_NAME) { |
michael@0 | 1538 | if (tt == TOK_TRIPLEDOT) { |
michael@0 | 1539 | hasRest = true; |
michael@0 | 1540 | tt = ts.getToken(); |
michael@0 | 1541 | if (tt == TOK_YIELD && yieldIsValidName) |
michael@0 | 1542 | tt = TOK_NAME; |
michael@0 | 1543 | if (tt != TOK_NAME) { |
michael@0 | 1544 | if (tt != TOK_ERROR) |
michael@0 | 1545 | ts.reportError(JSMSG_NO_REST_NAME); |
michael@0 | 1546 | return false; |
michael@0 | 1547 | } |
michael@0 | 1548 | } else { |
michael@0 | 1549 | return OnBadFormal(cx, tt); |
michael@0 | 1550 | } |
michael@0 | 1551 | } |
michael@0 | 1552 | |
michael@0 | 1553 | if (!formals.append(ts.currentName())) |
michael@0 | 1554 | return false; |
michael@0 | 1555 | |
michael@0 | 1556 | /* |
michael@0 | 1557 | * Get the next token. Stop on end of stream. Otherwise |
michael@0 | 1558 | * insist on a comma, get another name, and iterate. |
michael@0 | 1559 | */ |
michael@0 | 1560 | tt = ts.getToken(); |
michael@0 | 1561 | if (tt == TOK_EOF) |
michael@0 | 1562 | break; |
michael@0 | 1563 | if (tt != TOK_COMMA) |
michael@0 | 1564 | return OnBadFormal(cx, tt); |
michael@0 | 1565 | tt = ts.getToken(); |
michael@0 | 1566 | } |
michael@0 | 1567 | } |
michael@0 | 1568 | } |
michael@0 | 1569 | |
michael@0 | 1570 | #ifdef DEBUG |
michael@0 | 1571 | for (unsigned i = 0; i < formals.length(); ++i) { |
michael@0 | 1572 | JSString *str = formals[i]; |
michael@0 | 1573 | JS_ASSERT(str->asAtom().asPropertyName() == formals[i]); |
michael@0 | 1574 | } |
michael@0 | 1575 | #endif |
michael@0 | 1576 | |
michael@0 | 1577 | RootedString str(cx); |
michael@0 | 1578 | if (!args.length()) |
michael@0 | 1579 | str = cx->runtime()->emptyString; |
michael@0 | 1580 | else |
michael@0 | 1581 | str = ToString<CanGC>(cx, args[args.length() - 1]); |
michael@0 | 1582 | if (!str) |
michael@0 | 1583 | return false; |
michael@0 | 1584 | JSLinearString *linear = str->ensureLinear(cx); |
michael@0 | 1585 | if (!linear) |
michael@0 | 1586 | return false; |
michael@0 | 1587 | |
michael@0 | 1588 | JS::Anchor<JSString *> strAnchor(str); |
michael@0 | 1589 | const jschar *chars = linear->chars(); |
michael@0 | 1590 | size_t length = linear->length(); |
michael@0 | 1591 | |
michael@0 | 1592 | /* |
michael@0 | 1593 | * NB: (new Function) is not lexically closed by its caller, it's just an |
michael@0 | 1594 | * anonymous function in the top-level scope that its constructor inhabits. |
michael@0 | 1595 | * Thus 'var x = 42; f = new Function("return x"); print(f())' prints 42, |
michael@0 | 1596 | * and so would a call to f from another top-level's script or function. |
michael@0 | 1597 | */ |
michael@0 | 1598 | RootedAtom anonymousAtom(cx, cx->names().anonymous); |
michael@0 | 1599 | JSObject *proto = nullptr; |
michael@0 | 1600 | if (isStarGenerator) { |
michael@0 | 1601 | proto = GlobalObject::getOrCreateStarGeneratorFunctionPrototype(cx, global); |
michael@0 | 1602 | if (!proto) |
michael@0 | 1603 | return false; |
michael@0 | 1604 | } |
michael@0 | 1605 | RootedFunction fun(cx, NewFunctionWithProto(cx, js::NullPtr(), nullptr, 0, |
michael@0 | 1606 | JSFunction::INTERPRETED_LAMBDA, global, |
michael@0 | 1607 | anonymousAtom, proto, |
michael@0 | 1608 | JSFunction::FinalizeKind, TenuredObject)); |
michael@0 | 1609 | if (!fun) |
michael@0 | 1610 | return false; |
michael@0 | 1611 | |
michael@0 | 1612 | if (!JSFunction::setTypeForScriptedFunction(cx, fun)) |
michael@0 | 1613 | return false; |
michael@0 | 1614 | |
michael@0 | 1615 | if (hasRest) |
michael@0 | 1616 | fun->setHasRest(); |
michael@0 | 1617 | |
michael@0 | 1618 | bool ok; |
michael@0 | 1619 | SourceBufferHolder srcBuf(chars, length, SourceBufferHolder::NoOwnership); |
michael@0 | 1620 | if (isStarGenerator) |
michael@0 | 1621 | ok = frontend::CompileStarGeneratorBody(cx, &fun, options, formals, srcBuf); |
michael@0 | 1622 | else |
michael@0 | 1623 | ok = frontend::CompileFunctionBody(cx, &fun, options, formals, srcBuf); |
michael@0 | 1624 | args.rval().setObject(*fun); |
michael@0 | 1625 | return ok; |
michael@0 | 1626 | } |
michael@0 | 1627 | |
michael@0 | 1628 | bool |
michael@0 | 1629 | js::Function(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1630 | { |
michael@0 | 1631 | return FunctionConstructor(cx, argc, vp, NotGenerator); |
michael@0 | 1632 | } |
michael@0 | 1633 | |
michael@0 | 1634 | bool |
michael@0 | 1635 | js::Generator(JSContext *cx, unsigned argc, Value *vp) |
michael@0 | 1636 | { |
michael@0 | 1637 | return FunctionConstructor(cx, argc, vp, StarGenerator); |
michael@0 | 1638 | } |
michael@0 | 1639 | |
michael@0 | 1640 | bool |
michael@0 | 1641 | JSFunction::isBuiltinFunctionConstructor() |
michael@0 | 1642 | { |
michael@0 | 1643 | return maybeNative() == Function || maybeNative() == Generator; |
michael@0 | 1644 | } |
michael@0 | 1645 | |
michael@0 | 1646 | JSFunction * |
michael@0 | 1647 | js::NewFunction(ExclusiveContext *cx, HandleObject funobjArg, Native native, unsigned nargs, |
michael@0 | 1648 | JSFunction::Flags flags, HandleObject parent, HandleAtom atom, |
michael@0 | 1649 | gc::AllocKind allocKind /* = JSFunction::FinalizeKind */, |
michael@0 | 1650 | NewObjectKind newKind /* = GenericObject */) |
michael@0 | 1651 | { |
michael@0 | 1652 | return NewFunctionWithProto(cx, funobjArg, native, nargs, flags, parent, atom, nullptr, |
michael@0 | 1653 | allocKind, newKind); |
michael@0 | 1654 | } |
michael@0 | 1655 | |
michael@0 | 1656 | JSFunction * |
michael@0 | 1657 | js::NewFunctionWithProto(ExclusiveContext *cx, HandleObject funobjArg, Native native, |
michael@0 | 1658 | unsigned nargs, JSFunction::Flags flags, HandleObject parent, |
michael@0 | 1659 | HandleAtom atom, JSObject *proto, |
michael@0 | 1660 | gc::AllocKind allocKind /* = JSFunction::FinalizeKind */, |
michael@0 | 1661 | NewObjectKind newKind /* = GenericObject */) |
michael@0 | 1662 | { |
michael@0 | 1663 | JS_ASSERT(allocKind == JSFunction::FinalizeKind || allocKind == JSFunction::ExtendedFinalizeKind); |
michael@0 | 1664 | JS_ASSERT(sizeof(JSFunction) <= gc::Arena::thingSize(JSFunction::FinalizeKind)); |
michael@0 | 1665 | JS_ASSERT(sizeof(FunctionExtended) <= gc::Arena::thingSize(JSFunction::ExtendedFinalizeKind)); |
michael@0 | 1666 | |
michael@0 | 1667 | RootedObject funobj(cx, funobjArg); |
michael@0 | 1668 | if (funobj) { |
michael@0 | 1669 | JS_ASSERT(funobj->is<JSFunction>()); |
michael@0 | 1670 | JS_ASSERT(funobj->getParent() == parent); |
michael@0 | 1671 | JS_ASSERT_IF(native, funobj->hasSingletonType()); |
michael@0 | 1672 | } else { |
michael@0 | 1673 | // Don't give asm.js module functions a singleton type since they |
michael@0 | 1674 | // are cloned (via CloneFunctionObjectIfNotSingleton) which assumes |
michael@0 | 1675 | // that hasSingletonType implies isInterpreted. |
michael@0 | 1676 | if (native && !IsAsmJSModuleNative(native)) |
michael@0 | 1677 | newKind = SingletonObject; |
michael@0 | 1678 | funobj = NewObjectWithClassProto(cx, &JSFunction::class_, proto, |
michael@0 | 1679 | SkipScopeParent(parent), allocKind, newKind); |
michael@0 | 1680 | if (!funobj) |
michael@0 | 1681 | return nullptr; |
michael@0 | 1682 | } |
michael@0 | 1683 | RootedFunction fun(cx, &funobj->as<JSFunction>()); |
michael@0 | 1684 | |
michael@0 | 1685 | if (allocKind == JSFunction::ExtendedFinalizeKind) |
michael@0 | 1686 | flags = JSFunction::Flags(flags | JSFunction::EXTENDED); |
michael@0 | 1687 | |
michael@0 | 1688 | /* Initialize all function members. */ |
michael@0 | 1689 | fun->setArgCount(uint16_t(nargs)); |
michael@0 | 1690 | fun->setFlags(flags); |
michael@0 | 1691 | if (fun->isInterpreted()) { |
michael@0 | 1692 | JS_ASSERT(!native); |
michael@0 | 1693 | fun->mutableScript().init(nullptr); |
michael@0 | 1694 | fun->initEnvironment(parent); |
michael@0 | 1695 | } else { |
michael@0 | 1696 | JS_ASSERT(fun->isNative()); |
michael@0 | 1697 | JS_ASSERT(native); |
michael@0 | 1698 | fun->initNative(native, nullptr); |
michael@0 | 1699 | } |
michael@0 | 1700 | if (allocKind == JSFunction::ExtendedFinalizeKind) |
michael@0 | 1701 | fun->initializeExtended(); |
michael@0 | 1702 | fun->initAtom(atom); |
michael@0 | 1703 | |
michael@0 | 1704 | return fun; |
michael@0 | 1705 | } |
michael@0 | 1706 | |
michael@0 | 1707 | JSFunction * |
michael@0 | 1708 | js::CloneFunctionObject(JSContext *cx, HandleFunction fun, HandleObject parent, gc::AllocKind allocKind, |
michael@0 | 1709 | NewObjectKind newKindArg /* = GenericObject */) |
michael@0 | 1710 | { |
michael@0 | 1711 | JS_ASSERT(parent); |
michael@0 | 1712 | JS_ASSERT(!fun->isBoundFunction()); |
michael@0 | 1713 | |
michael@0 | 1714 | bool useSameScript = cx->compartment() == fun->compartment() && |
michael@0 | 1715 | !fun->hasSingletonType() && |
michael@0 | 1716 | !types::UseNewTypeForClone(fun); |
michael@0 | 1717 | |
michael@0 | 1718 | if (!useSameScript && fun->isInterpretedLazy() && !fun->getOrCreateScript(cx)) |
michael@0 | 1719 | return nullptr; |
michael@0 | 1720 | |
michael@0 | 1721 | NewObjectKind newKind = useSameScript ? newKindArg : SingletonObject; |
michael@0 | 1722 | JSObject *cloneProto = nullptr; |
michael@0 | 1723 | if (fun->isStarGenerator()) { |
michael@0 | 1724 | cloneProto = GlobalObject::getOrCreateStarGeneratorFunctionPrototype(cx, cx->global()); |
michael@0 | 1725 | if (!cloneProto) |
michael@0 | 1726 | return nullptr; |
michael@0 | 1727 | } |
michael@0 | 1728 | JSObject *cloneobj = NewObjectWithClassProto(cx, &JSFunction::class_, cloneProto, |
michael@0 | 1729 | SkipScopeParent(parent), allocKind, newKind); |
michael@0 | 1730 | if (!cloneobj) |
michael@0 | 1731 | return nullptr; |
michael@0 | 1732 | RootedFunction clone(cx, &cloneobj->as<JSFunction>()); |
michael@0 | 1733 | |
michael@0 | 1734 | uint16_t flags = fun->flags() & ~JSFunction::EXTENDED; |
michael@0 | 1735 | if (allocKind == JSFunction::ExtendedFinalizeKind) |
michael@0 | 1736 | flags |= JSFunction::EXTENDED; |
michael@0 | 1737 | |
michael@0 | 1738 | clone->setArgCount(fun->nargs()); |
michael@0 | 1739 | clone->setFlags(flags); |
michael@0 | 1740 | if (fun->hasScript()) { |
michael@0 | 1741 | clone->initScript(fun->nonLazyScript()); |
michael@0 | 1742 | clone->initEnvironment(parent); |
michael@0 | 1743 | } else if (fun->isInterpretedLazy()) { |
michael@0 | 1744 | LazyScript *lazy = fun->lazyScriptOrNull(); |
michael@0 | 1745 | clone->initLazyScript(lazy); |
michael@0 | 1746 | clone->initEnvironment(parent); |
michael@0 | 1747 | } else { |
michael@0 | 1748 | clone->initNative(fun->native(), fun->jitInfo()); |
michael@0 | 1749 | } |
michael@0 | 1750 | clone->initAtom(fun->displayAtom()); |
michael@0 | 1751 | |
michael@0 | 1752 | if (allocKind == JSFunction::ExtendedFinalizeKind) { |
michael@0 | 1753 | if (fun->isExtended() && fun->compartment() == cx->compartment()) { |
michael@0 | 1754 | for (unsigned i = 0; i < FunctionExtended::NUM_EXTENDED_SLOTS; i++) |
michael@0 | 1755 | clone->initExtendedSlot(i, fun->getExtendedSlot(i)); |
michael@0 | 1756 | } else { |
michael@0 | 1757 | clone->initializeExtended(); |
michael@0 | 1758 | } |
michael@0 | 1759 | } |
michael@0 | 1760 | |
michael@0 | 1761 | if (useSameScript) { |
michael@0 | 1762 | /* |
michael@0 | 1763 | * Clone the function, reusing its script. We can use the same type as |
michael@0 | 1764 | * the original function provided that its prototype is correct. |
michael@0 | 1765 | */ |
michael@0 | 1766 | if (fun->getProto() == clone->getProto()) |
michael@0 | 1767 | clone->setType(fun->type()); |
michael@0 | 1768 | return clone; |
michael@0 | 1769 | } |
michael@0 | 1770 | |
michael@0 | 1771 | RootedFunction cloneRoot(cx, clone); |
michael@0 | 1772 | |
michael@0 | 1773 | /* |
michael@0 | 1774 | * Across compartments we have to clone the script for interpreted |
michael@0 | 1775 | * functions. Cross-compartment cloning only happens via JSAPI |
michael@0 | 1776 | * (JS_CloneFunctionObject) which dynamically ensures that 'script' has |
michael@0 | 1777 | * no enclosing lexical scope (only the global scope). |
michael@0 | 1778 | */ |
michael@0 | 1779 | if (cloneRoot->isInterpreted() && !CloneFunctionScript(cx, fun, cloneRoot, newKindArg)) |
michael@0 | 1780 | return nullptr; |
michael@0 | 1781 | |
michael@0 | 1782 | return cloneRoot; |
michael@0 | 1783 | } |
michael@0 | 1784 | |
michael@0 | 1785 | JSFunction * |
michael@0 | 1786 | js::DefineFunction(JSContext *cx, HandleObject obj, HandleId id, Native native, |
michael@0 | 1787 | unsigned nargs, unsigned flags, AllocKind allocKind /* = FinalizeKind */, |
michael@0 | 1788 | NewObjectKind newKind /* = GenericObject */) |
michael@0 | 1789 | { |
michael@0 | 1790 | PropertyOp gop; |
michael@0 | 1791 | StrictPropertyOp sop; |
michael@0 | 1792 | |
michael@0 | 1793 | RootedFunction fun(cx); |
michael@0 | 1794 | |
michael@0 | 1795 | if (flags & JSFUN_STUB_GSOPS) { |
michael@0 | 1796 | /* |
michael@0 | 1797 | * JSFUN_STUB_GSOPS is a request flag only, not stored in fun->flags or |
michael@0 | 1798 | * the defined property's attributes. This allows us to encode another, |
michael@0 | 1799 | * internal flag using the same bit, JSFUN_EXPR_CLOSURE -- see jsfun.h |
michael@0 | 1800 | * for more on this. |
michael@0 | 1801 | */ |
michael@0 | 1802 | flags &= ~JSFUN_STUB_GSOPS; |
michael@0 | 1803 | gop = JS_PropertyStub; |
michael@0 | 1804 | sop = JS_StrictPropertyStub; |
michael@0 | 1805 | } else { |
michael@0 | 1806 | gop = nullptr; |
michael@0 | 1807 | sop = nullptr; |
michael@0 | 1808 | } |
michael@0 | 1809 | |
michael@0 | 1810 | JSFunction::Flags funFlags; |
michael@0 | 1811 | if (!native) |
michael@0 | 1812 | funFlags = JSFunction::INTERPRETED_LAZY; |
michael@0 | 1813 | else |
michael@0 | 1814 | funFlags = JSAPIToJSFunctionFlags(flags); |
michael@0 | 1815 | RootedAtom atom(cx, JSID_IS_ATOM(id) ? JSID_TO_ATOM(id) : nullptr); |
michael@0 | 1816 | fun = NewFunction(cx, NullPtr(), native, nargs, funFlags, obj, atom, allocKind, newKind); |
michael@0 | 1817 | if (!fun) |
michael@0 | 1818 | return nullptr; |
michael@0 | 1819 | |
michael@0 | 1820 | RootedValue funVal(cx, ObjectValue(*fun)); |
michael@0 | 1821 | if (!JSObject::defineGeneric(cx, obj, id, funVal, gop, sop, flags & ~JSFUN_FLAGS_MASK)) |
michael@0 | 1822 | return nullptr; |
michael@0 | 1823 | |
michael@0 | 1824 | return fun; |
michael@0 | 1825 | } |
michael@0 | 1826 | |
michael@0 | 1827 | bool |
michael@0 | 1828 | js::IsConstructor(const Value &v) |
michael@0 | 1829 | { |
michael@0 | 1830 | // Step 2. |
michael@0 | 1831 | if (!v.isObject()) |
michael@0 | 1832 | return false; |
michael@0 | 1833 | |
michael@0 | 1834 | // Step 3-4, a bit complex for us, since we have several flavors of |
michael@0 | 1835 | // [[Construct]] internal method. |
michael@0 | 1836 | JSObject &obj = v.toObject(); |
michael@0 | 1837 | if (obj.is<JSFunction>()) { |
michael@0 | 1838 | JSFunction &fun = obj.as<JSFunction>(); |
michael@0 | 1839 | return fun.isNativeConstructor() || fun.isInterpretedConstructor(); |
michael@0 | 1840 | } |
michael@0 | 1841 | return obj.getClass()->construct != nullptr; |
michael@0 | 1842 | } |
michael@0 | 1843 | |
michael@0 | 1844 | void |
michael@0 | 1845 | js::ReportIncompatibleMethod(JSContext *cx, CallReceiver call, const Class *clasp) |
michael@0 | 1846 | { |
michael@0 | 1847 | RootedValue thisv(cx, call.thisv()); |
michael@0 | 1848 | |
michael@0 | 1849 | #ifdef DEBUG |
michael@0 | 1850 | if (thisv.isObject()) { |
michael@0 | 1851 | JS_ASSERT(thisv.toObject().getClass() != clasp || |
michael@0 | 1852 | !thisv.toObject().isNative() || |
michael@0 | 1853 | !thisv.toObject().getProto() || |
michael@0 | 1854 | thisv.toObject().getProto()->getClass() != clasp); |
michael@0 | 1855 | } else if (thisv.isString()) { |
michael@0 | 1856 | JS_ASSERT(clasp != &StringObject::class_); |
michael@0 | 1857 | } else if (thisv.isNumber()) { |
michael@0 | 1858 | JS_ASSERT(clasp != &NumberObject::class_); |
michael@0 | 1859 | } else if (thisv.isBoolean()) { |
michael@0 | 1860 | JS_ASSERT(clasp != &BooleanObject::class_); |
michael@0 | 1861 | } else { |
michael@0 | 1862 | JS_ASSERT(thisv.isUndefined() || thisv.isNull()); |
michael@0 | 1863 | } |
michael@0 | 1864 | #endif |
michael@0 | 1865 | |
michael@0 | 1866 | if (JSFunction *fun = ReportIfNotFunction(cx, call.calleev())) { |
michael@0 | 1867 | JSAutoByteString funNameBytes; |
michael@0 | 1868 | if (const char *funName = GetFunctionNameBytes(cx, fun, &funNameBytes)) { |
michael@0 | 1869 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_PROTO, |
michael@0 | 1870 | clasp->name, funName, InformalValueTypeName(thisv)); |
michael@0 | 1871 | } |
michael@0 | 1872 | } |
michael@0 | 1873 | } |
michael@0 | 1874 | |
michael@0 | 1875 | void |
michael@0 | 1876 | js::ReportIncompatible(JSContext *cx, CallReceiver call) |
michael@0 | 1877 | { |
michael@0 | 1878 | if (JSFunction *fun = ReportIfNotFunction(cx, call.calleev())) { |
michael@0 | 1879 | JSAutoByteString funNameBytes; |
michael@0 | 1880 | if (const char *funName = GetFunctionNameBytes(cx, fun, &funNameBytes)) { |
michael@0 | 1881 | JS_ReportErrorNumber(cx, js_GetErrorMessage, nullptr, JSMSG_INCOMPATIBLE_METHOD, |
michael@0 | 1882 | funName, "method", InformalValueTypeName(call.thisv())); |
michael@0 | 1883 | } |
michael@0 | 1884 | } |
michael@0 | 1885 | } |
michael@0 | 1886 | |
michael@0 | 1887 | bool |
michael@0 | 1888 | JSObject::hasIdempotentProtoChain() const |
michael@0 | 1889 | { |
michael@0 | 1890 | // Return false if obj (or an object on its proto chain) is non-native or |
michael@0 | 1891 | // has a resolve or lookup hook. |
michael@0 | 1892 | JSObject *obj = const_cast<JSObject *>(this); |
michael@0 | 1893 | while (true) { |
michael@0 | 1894 | if (!obj->isNative()) |
michael@0 | 1895 | return false; |
michael@0 | 1896 | |
michael@0 | 1897 | JSResolveOp resolve = obj->getClass()->resolve; |
michael@0 | 1898 | if (resolve != JS_ResolveStub && resolve != (JSResolveOp) js::fun_resolve) |
michael@0 | 1899 | return false; |
michael@0 | 1900 | |
michael@0 | 1901 | if (obj->getOps()->lookupProperty || obj->getOps()->lookupGeneric || obj->getOps()->lookupElement) |
michael@0 | 1902 | return false; |
michael@0 | 1903 | |
michael@0 | 1904 | obj = obj->getProto(); |
michael@0 | 1905 | if (!obj) |
michael@0 | 1906 | return true; |
michael@0 | 1907 | } |
michael@0 | 1908 | |
michael@0 | 1909 | MOZ_ASSUME_UNREACHABLE("Should not get here"); |
michael@0 | 1910 | } |
michael@0 | 1911 | |
michael@0 | 1912 | namespace JS { |
michael@0 | 1913 | namespace detail { |
michael@0 | 1914 | |
michael@0 | 1915 | JS_PUBLIC_API(void) |
michael@0 | 1916 | CheckIsValidConstructible(Value calleev) |
michael@0 | 1917 | { |
michael@0 | 1918 | JSObject *callee = &calleev.toObject(); |
michael@0 | 1919 | if (callee->is<JSFunction>()) |
michael@0 | 1920 | JS_ASSERT(callee->as<JSFunction>().isNativeConstructor()); |
michael@0 | 1921 | else |
michael@0 | 1922 | JS_ASSERT(callee->getClass()->construct != nullptr); |
michael@0 | 1923 | } |
michael@0 | 1924 | |
michael@0 | 1925 | } // namespace detail |
michael@0 | 1926 | } // namespace JS |