| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
/* |
| 8 |
* JS function support. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "vm/JSFunction-inl.h" |
| 12 |
|
| 13 |
#include "mozilla/ArrayUtils.h" |
| 14 |
#include "mozilla/Maybe.h" |
| 15 |
#include "mozilla/Range.h" |
| 16 |
|
| 17 |
#include <algorithm> |
| 18 |
#include <string.h> |
| 19 |
|
| 20 |
#include "jsapi.h" |
| 21 |
#include "jstypes.h" |
| 22 |
|
| 23 |
#include "builtin/Array.h" |
| 24 |
#include "builtin/BigInt.h" |
| 25 |
#include "builtin/Object.h" |
| 26 |
#include "builtin/Symbol.h" |
| 27 |
#include "frontend/BytecodeCompiler.h" // frontend::{CompileStandaloneFunction, CompileStandaloneGenerator, CompileStandaloneAsyncFunction, CompileStandaloneAsyncGenerator, DelazifyCanonicalScriptedFunction} |
| 28 |
#include "frontend/FrontendContext.h" // AutoReportFrontendContext, ManualReportFrontendContext |
| 29 |
#include "frontend/Stencil.h" // js::DumpFunctionFlagsItems |
| 30 |
#include "jit/InlinableNatives.h" |
| 31 |
#include "jit/Ion.h" |
| 32 |
#include "js/CallNonGenericMethod.h" |
| 33 |
#include "js/CompilationAndEvaluation.h" |
| 34 |
#include "js/CompileOptions.h" |
| 35 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 36 |
#include "js/friend/StackLimits.h" // js::AutoCheckRecursionLimit |
| 37 |
#include "js/Printer.h" // js::GenericPrinter |
| 38 |
#include "js/PropertySpec.h" |
| 39 |
#include "js/SourceText.h" |
| 40 |
#include "js/StableStringChars.h" |
| 41 |
#include "js/Wrapper.h" |
| 42 |
#include "util/DifferentialTesting.h" |
| 43 |
#include "util/StringBuilder.h" |
| 44 |
#include "util/Text.h" |
| 45 |
#include "vm/BooleanObject.h" |
| 46 |
#include "vm/BoundFunctionObject.h" |
| 47 |
#include "vm/Compartment.h" |
| 48 |
#include "vm/FunctionFlags.h" // js::FunctionFlags |
| 49 |
#include "vm/GeneratorAndAsyncKind.h" // js::GeneratorKind, js::FunctionAsyncKind |
| 50 |
#include "vm/GlobalObject.h" |
| 51 |
#include "vm/Interpreter.h" |
| 52 |
#include "vm/JSAtomUtils.h" // ToAtom |
| 53 |
#include "vm/JSContext.h" |
| 54 |
#include "vm/JSObject.h" |
| 55 |
#include "vm/JSONPrinter.h" // js::JSONPrinter |
| 56 |
#include "vm/JSScript.h" |
| 57 |
#include "vm/NumberObject.h" |
| 58 |
#include "vm/PlainObject.h" // js::PlainObject |
| 59 |
#include "vm/SelfHosting.h" |
| 60 |
#include "vm/Shape.h" |
| 61 |
#include "vm/StringObject.h" |
| 62 |
#include "wasm/AsmJS.h" |
| 63 |
#include "wasm/WasmCode.h" |
| 64 |
#include "wasm/WasmInstance.h" |
| 65 |
#include "vm/Interpreter-inl.h" |
| 66 |
#include "vm/JSScript-inl.h" |
| 67 |
|
| 68 |
using namespace js; |
| 69 |
|
| 70 |
using mozilla::Maybe; |
| 71 |
using mozilla::Some; |
| 72 |
|
| 73 |
using JS::AutoStableStringChars; |
| 74 |
using JS::CompileOptions; |
| 75 |
using JS::SourceText; |
| 76 |
|
| 77 |
static bool fun_enumerate(JSContext* cx, HandleObject obj) { |
| 78 |
MOZ_ASSERT(obj->is<JSFunction>()); |
| 79 |
|
| 80 |
RootedId id(cx); |
| 81 |
bool found; |
| 82 |
|
| 83 |
if (obj->as<JSFunction>().needsPrototypeProperty()) { |
| 84 |
id = NameToId(cx->names().prototype); |
| 85 |
if (!HasOwnProperty(cx, obj, id, &found)) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
if (!obj->as<JSFunction>().hasResolvedLength()) { |
| 91 |
id = NameToId(cx->names().length); |
| 92 |
if (!HasOwnProperty(cx, obj, id, &found)) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (!obj->as<JSFunction>().hasResolvedName()) { |
| 98 |
id = NameToId(cx->names().name); |
| 99 |
if (!HasOwnProperty(cx, obj, id, &found)) { |
| 100 |
return false; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
bool IsFunction(HandleValue v) { |
| 108 |
return v.isObject() && v.toObject().is<JSFunction>(); |
| 109 |
} |
| 110 |
|
| 111 |
static bool AdvanceToActiveCallLinear(JSContext* cx, |
| 112 |
NonBuiltinScriptFrameIter& iter, |
| 113 |
HandleFunction fun) { |
| 114 |
MOZ_ASSERT(!fun->isBuiltin()); |
| 115 |
|
| 116 |
for (; !iter.done(); ++iter) { |
| 117 |
if (!iter.isFunctionFrame()) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
if (iter.matchCallee(cx, fun)) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
} |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
void js::ThrowTypeErrorBehavior(JSContext* cx) { |
| 128 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 129 |
JSMSG_THROW_TYPE_ERROR); |
| 130 |
} |
| 131 |
|
| 132 |
static bool IsSloppyNormalFunction(JSFunction* fun) { |
| 133 |
// FunctionDeclaration or FunctionExpression in sloppy mode. |
| 134 |
if (fun->kind() == FunctionFlags::NormalFunction) { |
| 135 |
if (fun->isBuiltin()) { |
| 136 |
return false; |
| 137 |
} |
| 138 |
|
| 139 |
if (fun->isGenerator() || fun->isAsync()) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
|
| 143 |
MOZ_ASSERT(fun->isInterpreted()); |
| 144 |
return !fun->strict(); |
| 145 |
} |
| 146 |
|
| 147 |
// Or asm.js function in sloppy mode. |
| 148 |
if (fun->kind() == FunctionFlags::AsmJS) { |
| 149 |
return !IsAsmJSStrictModeModuleOrFunction(fun); |
| 150 |
} |
| 151 |
|
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
// Beware: this function can be invoked on *any* function! That includes |
| 156 |
// natives, strict mode functions, bound functions, arrow functions, |
| 157 |
// self-hosted functions and constructors, asm.js functions, functions with |
| 158 |
// destructuring arguments and/or a rest argument, and probably a few more I |
| 159 |
// forgot. Turn back and save yourself while you still can. It's too late for |
| 160 |
// me. |
| 161 |
static bool ArgumentsRestrictions(JSContext* cx, HandleFunction fun) { |
| 162 |
// Throw unless the function is a sloppy, normal function. |
| 163 |
// TODO (bug 1057208): ensure semantics are correct for all possible |
| 164 |
// pairings of callee/caller. |
| 165 |
if (!IsSloppyNormalFunction(fun)) { |
| 166 |
ThrowTypeErrorBehavior(cx); |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
return true; |
| 171 |
} |
| 172 |
|
| 173 |
bool ArgumentsGetterImpl(JSContext* cx, const CallArgs& args) { |
| 174 |
MOZ_ASSERT(IsFunction(args.thisv())); |
| 175 |
|
| 176 |
RootedFunction fun(cx, &args.thisv().toObject().as<JSFunction>()); |
| 177 |
if (!ArgumentsRestrictions(cx, fun)) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
// Function.arguments isn't standard (not even Annex B), so it isn't |
| 182 |
// worth the effort to guarantee that we can always recover it from |
| 183 |
// an Ion frame. Always return null for differential fuzzing. |
| 184 |
if (js::SupportDifferentialTesting()) { |
| 185 |
args.rval().setNull(); |
| 186 |
return true; |
| 187 |
} |
| 188 |
|
| 189 |
// Return null if this function wasn't found on the stack. |
| 190 |
NonBuiltinScriptFrameIter iter(cx); |
| 191 |
if (!AdvanceToActiveCallLinear(cx, iter, fun)) { |
| 192 |
args.rval().setNull(); |
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
Rooted<ArgumentsObject*> argsobj(cx, |
| 197 |
ArgumentsObject::createUnexpected(cx, iter)); |
| 198 |
if (!argsobj) { |
| 199 |
return false; |
| 200 |
} |
| 201 |
|
| 202 |
#ifndef JS_CODEGEN_NONE |
| 203 |
// Disabling compiling of this script in IonMonkey. IonMonkey doesn't |
| 204 |
// guarantee |f.arguments| can be fully recovered, so we try to mitigate |
| 205 |
// observing this behavior by detecting its use early. |
| 206 |
JSScript* script = iter.script(); |
| 207 |
jit::ForbidCompilation(cx, script); |
| 208 |
#endif |
| 209 |
|
| 210 |
args.rval().setObject(*argsobj); |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
static bool ArgumentsGetter(JSContext* cx, unsigned argc, Value* vp) { |
| 215 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 216 |
return CallNonGenericMethod<IsFunction, ArgumentsGetterImpl>(cx, args); |
| 217 |
} |
| 218 |
|
| 219 |
bool ArgumentsSetterImpl(JSContext* cx, const CallArgs& args) { |
| 220 |
MOZ_ASSERT(IsFunction(args.thisv())); |
| 221 |
|
| 222 |
RootedFunction fun(cx, &args.thisv().toObject().as<JSFunction>()); |
| 223 |
if (!ArgumentsRestrictions(cx, fun)) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
// If the function passes the gauntlet, return |undefined|. |
| 228 |
args.rval().setUndefined(); |
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
static bool ArgumentsSetter(JSContext* cx, unsigned argc, Value* vp) { |
| 233 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 234 |
return CallNonGenericMethod<IsFunction, ArgumentsSetterImpl>(cx, args); |
| 235 |
} |
| 236 |
|
| 237 |
// Beware: this function can be invoked on *any* function! That includes |
| 238 |
// natives, strict mode functions, bound functions, arrow functions, |
| 239 |
// self-hosted functions and constructors, asm.js functions, functions with |
| 240 |
// destructuring arguments and/or a rest argument, and probably a few more I |
| 241 |
// forgot. Turn back and save yourself while you still can. It's too late for |
| 242 |
// me. |
| 243 |
static bool CallerRestrictions(JSContext* cx, HandleFunction fun) { |
| 244 |
// Throw unless the function is a sloppy, normal function. |
| 245 |
// TODO (bug 1057208): ensure semantics are correct for all possible |
| 246 |
// pairings of callee/caller. |
| 247 |
if (!IsSloppyNormalFunction(fun)) { |
| 248 |
ThrowTypeErrorBehavior(cx); |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
return true; |
| 253 |
} |
| 254 |
|
| 255 |
bool CallerGetterImpl(JSContext* cx, const CallArgs& args) { |
| 256 |
MOZ_ASSERT(IsFunction(args.thisv())); |
| 257 |
|
| 258 |
// Beware! This function can be invoked on *any* function! It can't |
| 259 |
// assume it'll never be invoked on natives, strict mode functions, bound |
| 260 |
// functions, or anything else that ordinarily has immutable .caller |
| 261 |
// defined with [[ThrowTypeError]]. |
| 262 |
RootedFunction fun(cx, &args.thisv().toObject().as<JSFunction>()); |
| 263 |
if (!CallerRestrictions(cx, fun)) { |
| 264 |
return false; |
| 265 |
} |
| 266 |
|
| 267 |
// Also return null if this function wasn't found on the stack. |
| 268 |
NonBuiltinScriptFrameIter iter(cx); |
| 269 |
if (!AdvanceToActiveCallLinear(cx, iter, fun)) { |
| 270 |
args.rval().setNull(); |
| 271 |
return true; |
| 272 |
} |
| 273 |
|
| 274 |
++iter; |
| 275 |
while (!iter.done() && iter.isEvalFrame()) { |
| 276 |
++iter; |
| 277 |
} |
| 278 |
|
| 279 |
if (iter.done() || !iter.isFunctionFrame()) { |
| 280 |
args.rval().setNull(); |
| 281 |
return true; |
| 282 |
} |
| 283 |
|
| 284 |
RootedObject caller(cx, iter.callee(cx)); |
| 285 |
if (!cx->compartment()->wrap(cx, &caller)) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
// Censor the caller if we don't have full access to it. If we do, but the |
| 290 |
// caller is a function with strict mode code, throw a TypeError per ES5. |
| 291 |
// If we pass these checks, we can return the computed caller. |
| 292 |
{ |
| 293 |
JSObject* callerObj = CheckedUnwrapStatic(caller); |
| 294 |
if (!callerObj) { |
| 295 |
args.rval().setNull(); |
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
if (JS_IsDeadWrapper(callerObj)) { |
| 300 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 301 |
JSMSG_DEAD_OBJECT); |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
JSFunction* callerFun = &callerObj->as<JSFunction>(); |
| 306 |
MOZ_ASSERT(!callerFun->isBuiltin(), |
| 307 |
"non-builtin iterator returned a builtin?"); |
| 308 |
|
| 309 |
if (callerFun->strict() || callerFun->isAsync() || |
| 310 |
callerFun->isGenerator()) { |
| 311 |
args.rval().setNull(); |
| 312 |
return true; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
args.rval().setObject(*caller); |
| 317 |
return true; |
| 318 |
} |
| 319 |
|
| 320 |
static bool CallerGetter(JSContext* cx, unsigned argc, Value* vp) { |
| 321 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 322 |
return CallNonGenericMethod<IsFunction, CallerGetterImpl>(cx, args); |
| 323 |
} |
| 324 |
|
| 325 |
bool CallerSetterImpl(JSContext* cx, const CallArgs& args) { |
| 326 |
MOZ_ASSERT(IsFunction(args.thisv())); |
| 327 |
|
| 328 |
// We just have to return |undefined|, but first we call CallerGetterImpl |
| 329 |
// because we need the same strict-mode and security checks. |
| 330 |
|
| 331 |
if (!CallerGetterImpl(cx, args)) { |
| 332 |
return false; |
| 333 |
} |
| 334 |
|
| 335 |
args.rval().setUndefined(); |
| 336 |
return true; |
| 337 |
} |
| 338 |
|
| 339 |
static bool CallerSetter(JSContext* cx, unsigned argc, Value* vp) { |
| 340 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 341 |
return CallNonGenericMethod<IsFunction, CallerSetterImpl>(cx, args); |
| 342 |
} |
| 343 |
|
| 344 |
static const JSPropertySpec function_properties[] = { |
| 345 |
JS_PSGS("arguments", ArgumentsGetter, ArgumentsSetter, 0), |
| 346 |
JS_PSGS("caller", CallerGetter, CallerSetter, 0), |
| 347 |
JS_PS_END, |
| 348 |
}; |
| 349 |
|
| 350 |
static bool ResolveInterpretedFunctionPrototype(JSContext* cx, |
| 351 |
HandleFunction fun, |
| 352 |
HandleId id) { |
| 353 |
MOZ_ASSERT(fun->isInterpreted() || fun->isAsmJSNative()); |
| 354 |
MOZ_ASSERT(id == NameToId(cx->names().prototype)); |
| 355 |
|
| 356 |
// Assert that fun is not a compiler-created function object, which |
| 357 |
// must never leak to script or embedding code and then be mutated. |
| 358 |
// Also assert that fun is not bound, per the ES5 15.3.4.5 ref above. |
| 359 |
MOZ_ASSERT(!IsInternalFunctionObject(*fun)); |
| 360 |
|
| 361 |
// Make the prototype object an instance of Object with the same parent as |
| 362 |
// the function object itself, unless the function is an ES6 generator. In |
| 363 |
// that case, per the 15 July 2013 ES6 draft, section 15.19.3, its parent is |
| 364 |
// the GeneratorObjectPrototype singleton. |
| 365 |
bool isGenerator = fun->isGenerator(); |
| 366 |
Rooted<GlobalObject*> global(cx, &fun->global()); |
| 367 |
RootedObject objProto(cx); |
| 368 |
if (isGenerator && fun->isAsync()) { |
| 369 |
objProto = GlobalObject::getOrCreateAsyncGeneratorPrototype(cx, global); |
| 370 |
} else if (isGenerator) { |
| 371 |
objProto = GlobalObject::getOrCreateGeneratorObjectPrototype(cx, global); |
| 372 |
} else { |
| 373 |
objProto = &global->getObjectPrototype(); |
| 374 |
} |
| 375 |
if (!objProto) { |
| 376 |
return false; |
| 377 |
} |
| 378 |
|
| 379 |
Rooted<PlainObject*> proto( |
| 380 |
cx, NewPlainObjectWithProto(cx, objProto, TenuredObject)); |
| 381 |
if (!proto) { |
| 382 |
return false; |
| 383 |
} |
| 384 |
|
| 385 |
// Per ES5 13.2 the prototype's .constructor property is configurable, |
| 386 |
// non-enumerable, and writable. However, per the 15 July 2013 ES6 draft, |
| 387 |
// section 15.19.3, the .prototype of a generator function does not link |
| 388 |
// back with a .constructor. |
| 389 |
if (!isGenerator) { |
| 390 |
RootedValue objVal(cx, ObjectValue(*fun)); |
| 391 |
if (!DefineDataProperty(cx, proto, cx->names().constructor, objVal, 0)) { |
| 392 |
return false; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
// Per ES5 15.3.5.2 a user-defined function's .prototype property is |
| 397 |
// initially non-configurable, non-enumerable, and writable. |
| 398 |
RootedValue protoVal(cx, ObjectValue(*proto)); |
| 399 |
return DefineDataProperty(cx, fun, id, protoVal, |
| 400 |
JSPROP_PERMANENT | JSPROP_RESOLVING); |
| 401 |
} |
| 402 |
|
| 403 |
bool JSFunction::needsPrototypeProperty() { |
| 404 |
/* |
| 405 |
* Built-in functions do not have a .prototype property per ECMA-262, |
| 406 |
* or (Object.prototype, Function.prototype, etc.) have that property |
| 407 |
* created eagerly. |
| 408 |
* |
| 409 |
* ES6 9.2.8 MakeConstructor defines the .prototype property on constructors. |
| 410 |
* Generators are not constructors, but they have a .prototype property |
| 411 |
* anyway, according to errata to ES6. See bug 1191486. |
| 412 |
* |
| 413 |
* Thus all of the following don't get a .prototype property: |
| 414 |
* - Methods (that are not class-constructors or generators) |
| 415 |
* - Arrow functions |
| 416 |
* - Function.prototype |
| 417 |
* - Async functions |
| 418 |
*/ |
| 419 |
return !isBuiltin() && (isConstructor() || isGenerator()); |
| 420 |
} |
| 421 |
|
| 422 |
bool JSFunction::hasNonConfigurablePrototypeDataProperty() { |
| 423 |
if (!isBuiltin()) { |
| 424 |
return needsPrototypeProperty(); |
| 425 |
} |
| 426 |
|
| 427 |
if (isSelfHostedBuiltin()) { |
| 428 |
// Self-hosted constructors have a non-configurable .prototype data |
| 429 |
// property. |
| 430 |
if (!isConstructor()) { |
| 431 |
return false; |
| 432 |
} |
| 433 |
#ifdef DEBUG |
| 434 |
PropertyName* prototypeName = |
| 435 |
runtimeFromMainThread()->commonNames->prototype; |
| 436 |
Maybe<PropertyInfo> prop = lookupPure(prototypeName); |
| 437 |
MOZ_ASSERT(prop.isSome()); |
| 438 |
MOZ_ASSERT(prop->isDataProperty()); |
| 439 |
MOZ_ASSERT(!prop->configurable()); |
| 440 |
#endif |
| 441 |
return true; |
| 442 |
} |
| 443 |
|
| 444 |
if (!isConstructor()) { |
| 445 |
// We probably don't have a .prototype property. Avoid the lookup below. |
| 446 |
return false; |
| 447 |
} |
| 448 |
|
| 449 |
PropertyName* prototypeName = runtimeFromMainThread()->commonNames->prototype; |
| 450 |
Maybe<PropertyInfo> prop = lookupPure(prototypeName); |
| 451 |
return prop.isSome() && prop->isDataProperty() && !prop->configurable(); |
| 452 |
} |
| 453 |
|
| 454 |
uint32_t JSFunction::wasmFuncIndex() const { |
| 455 |
MOZ_ASSERT(isWasm() || isAsmJSNative()); |
| 456 |
if (!isNativeWithJitEntry()) { |
| 457 |
uintptr_t tagged = uintptr_t(nativeJitInfoOrInterpretedScript()); |
| 458 |
MOZ_ASSERT(tagged & 1); |
| 459 |
return tagged >> 1; |
| 460 |
} |
| 461 |
return wasmInstance().code().funcIndexFromJitEntry(wasmJitEntry()); |
| 462 |
} |
| 463 |
|
| 464 |
void JSFunction::initWasm(uint32_t funcIndex, wasm::Instance* instance, |
| 465 |
const wasm::SuperTypeVector* superTypeVector, |
| 466 |
void* uncheckedCallEntry) { |
| 467 |
MOZ_ASSERT(isWasm() || isAsmJSNative()); |
| 468 |
MOZ_ASSERT(!isWasmWithJitEntry()); |
| 469 |
MOZ_ASSERT(!nativeJitInfoOrInterpretedScript()); |
| 470 |
|
| 471 |
// Set the func index, see comment on the field for why we set the low bit. |
| 472 |
uintptr_t tagged = (uintptr_t(funcIndex) << 1) | 1; |
| 473 |
setNativeJitInfoOrInterpretedScript(reinterpret_cast<void*>(tagged)); |
| 474 |
// Set the instance |
| 475 |
setExtendedSlot(FunctionExtended::WASM_INSTANCE_SLOT, |
| 476 |
JS::PrivateValue(instance)); |
| 477 |
// Set the super type vector |
| 478 |
setExtendedSlot(FunctionExtended::WASM_STV_SLOT, |
| 479 |
JS::PrivateValue((void*)superTypeVector)); |
| 480 |
// Set the unchecked entry slot |
| 481 |
setExtendedSlot(FunctionExtended::WASM_FUNC_UNCHECKED_ENTRY_SLOT, |
| 482 |
JS::PrivateValue(uncheckedCallEntry)); |
| 483 |
} |
| 484 |
|
| 485 |
void JSFunction::initWasmWithJitEntry( |
| 486 |
void** entry, wasm::Instance* instance, |
| 487 |
const wasm::SuperTypeVector* superTypeVector, void* uncheckedCallEntry) { |
| 488 |
MOZ_ASSERT(*entry); |
| 489 |
MOZ_ASSERT(isWasm()); |
| 490 |
MOZ_ASSERT(!isWasmWithJitEntry()); |
| 491 |
|
| 492 |
// Mark that we have a JIT entry, and initialize it |
| 493 |
setFlags(flags().setNativeJitEntry()); |
| 494 |
setNativeJitInfoOrInterpretedScript(entry); |
| 495 |
|
| 496 |
// Set the instance |
| 497 |
setExtendedSlot(FunctionExtended::WASM_INSTANCE_SLOT, |
| 498 |
JS::PrivateValue(instance)); |
| 499 |
// Set the super type vector |
| 500 |
setExtendedSlot(FunctionExtended::WASM_STV_SLOT, |
| 501 |
JS::PrivateValue((void*)superTypeVector)); |
| 502 |
// Set the unchecked entry slot |
| 503 |
setExtendedSlot(FunctionExtended::WASM_FUNC_UNCHECKED_ENTRY_SLOT, |
| 504 |
JS::PrivateValue(uncheckedCallEntry)); |
| 505 |
|
| 506 |
MOZ_ASSERT(isWasmWithJitEntry()); |
| 507 |
} |
| 508 |
|
| 509 |
void* JSFunction::wasmUncheckedCallEntry() const { |
| 510 |
MOZ_ASSERT(isWasm()); |
| 511 |
return getExtendedSlot(FunctionExtended::WASM_FUNC_UNCHECKED_ENTRY_SLOT) |
| 512 |
.toPrivate(); |
| 513 |
} |
| 514 |
|
| 515 |
void* JSFunction::wasmCheckedCallEntry() const { |
| 516 |
uint8_t* codeRangeBase; |
| 517 |
const wasm::CodeRange* codeRange; |
| 518 |
wasmInstance().code().funcCodeRange(wasmFuncIndex(), &codeRange, |
| 519 |
&codeRangeBase); |
| 520 |
return codeRangeBase + codeRange->funcCheckedCallEntry(); |
| 521 |
} |
| 522 |
|
| 523 |
static bool fun_mayResolve(const JSAtomState& names, jsid id, JSObject*) { |
| 524 |
if (!id.isAtom()) { |
| 525 |
return false; |
| 526 |
} |
| 527 |
|
| 528 |
JSAtom* atom = id.toAtom(); |
| 529 |
return atom == names.prototype || atom == names.length || atom == names.name; |
| 530 |
} |
| 531 |
|
| 532 |
bool JSFunction::getExplicitName(JSContext* cx, |
| 533 |
JS::MutableHandle<JSAtom*> name) { |
| 534 |
if (isAccessorWithLazyName()) { |
| 535 |
JSAtom* accessorName = getAccessorNameForLazy(cx); |
| 536 |
if (!accessorName) { |
| 537 |
return false; |
| 538 |
} |
| 539 |
|
| 540 |
name.set(accessorName); |
| 541 |
return true; |
| 542 |
} |
| 543 |
|
| 544 |
name.set(maybePartialExplicitName()); |
| 545 |
return true; |
| 546 |
} |
| 547 |
|
| 548 |
bool JSFunction::getDisplayAtom(JSContext* cx, |
| 549 |
JS::MutableHandle<JSAtom*> name) { |
| 550 |
if (isAccessorWithLazyName()) { |
| 551 |
JSAtom* accessorName = getAccessorNameForLazy(cx); |
| 552 |
if (!accessorName) { |
| 553 |
return false; |
| 554 |
} |
| 555 |
|
| 556 |
name.set(accessorName); |
| 557 |
return true; |
| 558 |
} |
| 559 |
|
| 560 |
name.set(maybePartialDisplayAtom()); |
| 561 |
return true; |
| 562 |
} |
| 563 |
|
| 564 |
static JSAtom* NameToPrefixedFunctionName(JSContext* cx, JSString* nameStr, |
| 565 |
FunctionPrefixKind prefixKind) { |
| 566 |
MOZ_ASSERT(prefixKind != FunctionPrefixKind::None); |
| 567 |
|
| 568 |
StringBuilder sb(cx); |
| 569 |
if (prefixKind == FunctionPrefixKind::Get) { |
| 570 |
if (!sb.append("get ")) { |
| 571 |
return nullptr; |
| 572 |
} |
| 573 |
} else { |
| 574 |
if (!sb.append("set ")) { |
| 575 |
return nullptr; |
| 576 |
} |
| 577 |
} |
| 578 |
if (!sb.append(nameStr)) { |
| 579 |
return nullptr; |
| 580 |
} |
| 581 |
return sb.finishAtom(); |
| 582 |
} |
| 583 |
|
| 584 |
static JSAtom* NameToFunctionName(JSContext* cx, HandleValue name, |
| 585 |
FunctionPrefixKind prefixKind) { |
| 586 |
MOZ_ASSERT(name.isString() || name.isNumeric()); |
| 587 |
|
| 588 |
if (prefixKind == FunctionPrefixKind::None) { |
| 589 |
return ToAtom<CanGC>(cx, name); |
| 590 |
} |
| 591 |
|
| 592 |
JSString* nameStr = ToString(cx, name); |
| 593 |
if (!nameStr) { |
| 594 |
return nullptr; |
| 595 |
} |
| 596 |
|
| 597 |
return NameToPrefixedFunctionName(cx, nameStr, prefixKind); |
| 598 |
} |
| 599 |
|
| 600 |
JSAtom* JSFunction::getAccessorNameForLazy(JSContext* cx) { |
| 601 |
MOZ_ASSERT(isAccessorWithLazyName()); |
| 602 |
|
| 603 |
JSAtom* name = NameToPrefixedFunctionName( |
| 604 |
cx, rawAtom(), |
| 605 |
isGetter() ? FunctionPrefixKind::Get : FunctionPrefixKind::Set); |
| 606 |
if (!name) { |
| 607 |
return nullptr; |
| 608 |
} |
| 609 |
|
| 610 |
setAtom(name); |
| 611 |
setFlags(flags().clearLazyAccessorName()); |
| 612 |
return name; |
| 613 |
} |
| 614 |
|
| 615 |
static bool fun_resolve(JSContext* cx, HandleObject obj, HandleId id, |
| 616 |
bool* resolvedp) { |
| 617 |
if (!id.isAtom()) { |
| 618 |
return true; |
| 619 |
} |
| 620 |
|
| 621 |
RootedFunction fun(cx, &obj->as<JSFunction>()); |
| 622 |
|
| 623 |
if (id.isAtom(cx->names().prototype)) { |
| 624 |
if (!fun->needsPrototypeProperty()) { |
| 625 |
return true; |
| 626 |
} |
| 627 |
|
| 628 |
if (!ResolveInterpretedFunctionPrototype(cx, fun, id)) { |
| 629 |
return false; |
| 630 |
} |
| 631 |
|
| 632 |
*resolvedp = true; |
| 633 |
return true; |
| 634 |
} |
| 635 |
|
| 636 |
bool isLength = id.isAtom(cx->names().length); |
| 637 |
if (isLength || id.isAtom(cx->names().name)) { |
| 638 |
MOZ_ASSERT(!IsInternalFunctionObject(*obj)); |
| 639 |
|
| 640 |
RootedValue v(cx); |
| 641 |
|
| 642 |
// Since f.length and f.name are configurable, they could be resolved |
| 643 |
// and then deleted: |
| 644 |
// function f(x) {} |
| 645 |
// assertEq(f.length, 1); |
| 646 |
// delete f.length; |
| 647 |
// assertEq(f.name, "f"); |
| 648 |
// delete f.name; |
| 649 |
// Afterwards, asking for f.length or f.name again will cause this |
| 650 |
// resolve hook to run again. Defining the property again the second |
| 651 |
// time through would be a bug. |
| 652 |
// assertEq(f.length, 0); // gets Function.prototype.length! |
| 653 |
// assertEq(f.name, ""); // gets Function.prototype.name! |
| 654 |
// We use the RESOLVED_LENGTH and RESOLVED_NAME flags as a hack to prevent |
| 655 |
// this bug. |
| 656 |
if (isLength) { |
| 657 |
if (fun->hasResolvedLength()) { |
| 658 |
return true; |
| 659 |
} |
| 660 |
|
| 661 |
uint16_t len = 0; |
| 662 |
if (!JSFunction::getUnresolvedLength(cx, fun, &len)) { |
| 663 |
return false; |
| 664 |
} |
| 665 |
v.setInt32(len); |
| 666 |
} else { |
| 667 |
if (fun->hasResolvedName()) { |
| 668 |
return true; |
| 669 |
} |
| 670 |
|
| 671 |
JSAtom* name = fun->getUnresolvedName(cx); |
| 672 |
if (!name) { |
| 673 |
return false; |
| 674 |
} |
| 675 |
v.setString(name); |
| 676 |
} |
| 677 |
|
| 678 |
if (!NativeDefineDataProperty(cx, fun, id, v, |
| 679 |
JSPROP_READONLY | JSPROP_RESOLVING)) { |
| 680 |
return false; |
| 681 |
} |
| 682 |
|
| 683 |
if (isLength) { |
| 684 |
fun->setResolvedLength(); |
| 685 |
} else { |
| 686 |
fun->setResolvedName(); |
| 687 |
} |
| 688 |
|
| 689 |
*resolvedp = true; |
| 690 |
return true; |
| 691 |
} |
| 692 |
|
| 693 |
return true; |
| 694 |
} |
| 695 |
|
| 696 |
/* ES6 (04-25-16) 19.2.3.6 Function.prototype [ @@hasInstance ] */ |
| 697 |
static bool fun_symbolHasInstance(JSContext* cx, unsigned argc, Value* vp) { |
| 698 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 699 |
|
| 700 |
if (args.length() < 1) { |
| 701 |
args.rval().setBoolean(false); |
| 702 |
return true; |
| 703 |
} |
| 704 |
|
| 705 |
/* Step 1. */ |
| 706 |
HandleValue func = args.thisv(); |
| 707 |
|
| 708 |
// Primitives are non-callable and will always return false from |
| 709 |
// OrdinaryHasInstance. |
| 710 |
if (!func.isObject()) { |
| 711 |
args.rval().setBoolean(false); |
| 712 |
return true; |
| 713 |
} |
| 714 |
|
| 715 |
RootedObject obj(cx, &func.toObject()); |
| 716 |
|
| 717 |
/* Step 2. */ |
| 718 |
bool result; |
| 719 |
if (!OrdinaryHasInstance(cx, obj, args[0], &result)) { |
| 720 |
return false; |
| 721 |
} |
| 722 |
|
| 723 |
args.rval().setBoolean(result); |
| 724 |
return true; |
| 725 |
} |
| 726 |
|
| 727 |
/* |
| 728 |
* ES6 (4-25-16) 7.3.19 OrdinaryHasInstance |
| 729 |
*/ |
| 730 |
bool JS::OrdinaryHasInstance(JSContext* cx, HandleObject objArg, HandleValue v, |
| 731 |
bool* bp) { |
| 732 |
AssertHeapIsIdle(); |
| 733 |
cx->check(objArg, v); |
| 734 |
|
| 735 |
RootedObject obj(cx, objArg); |
| 736 |
|
| 737 |
/* Step 1. */ |
| 738 |
if (!obj->isCallable()) { |
| 739 |
*bp = false; |
| 740 |
return true; |
| 741 |
} |
| 742 |
|
| 743 |
/* Step 2. */ |
| 744 |
if (obj->is<BoundFunctionObject>()) { |
| 745 |
/* Steps 2a-b. */ |
| 746 |
AutoCheckRecursionLimit recursion(cx); |
| 747 |
if (!recursion.check(cx)) { |
| 748 |
return false; |
| 749 |
} |
| 750 |
obj = obj->as<BoundFunctionObject>().getTarget(); |
| 751 |
return InstanceofOperator(cx, obj, v, bp); |
| 752 |
} |
| 753 |
|
| 754 |
/* Step 3. */ |
| 755 |
if (!v.isObject()) { |
| 756 |
*bp = false; |
| 757 |
return true; |
| 758 |
} |
| 759 |
|
| 760 |
/* Step 4. */ |
| 761 |
RootedValue pval(cx); |
| 762 |
if (!GetProperty(cx, obj, obj, cx->names().prototype, &pval)) { |
| 763 |
return false; |
| 764 |
} |
| 765 |
|
| 766 |
/* Step 5. */ |
| 767 |
if (pval.isPrimitive()) { |
| 768 |
/* |
| 769 |
* Throw a runtime error if instanceof is called on a function that |
| 770 |
* has a non-object as its .prototype value. |
| 771 |
*/ |
| 772 |
RootedValue val(cx, ObjectValue(*obj)); |
| 773 |
ReportValueError(cx, JSMSG_BAD_PROTOTYPE, -1, val, nullptr); |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
/* Step 6. */ |
| 778 |
RootedObject pobj(cx, &pval.toObject()); |
| 779 |
bool isPrototype; |
| 780 |
if (!IsPrototypeOf(cx, pobj, &v.toObject(), &isPrototype)) { |
| 781 |
return false; |
| 782 |
} |
| 783 |
*bp = isPrototype; |
| 784 |
return true; |
| 785 |
} |
| 786 |
|
| 787 |
inline void JSFunction::trace(JSTracer* trc) { |
| 788 |
// Functions can be be marked as interpreted despite having no script yet at |
| 789 |
// some points when parsing, and can be lazy with no lazy script for |
| 790 |
// self-hosted code. |
| 791 |
MOZ_ASSERT(!getFixedSlot(NativeJitInfoOrInterpretedScriptSlot).isGCThing()); |
| 792 |
if (isInterpreted() && hasBaseScript()) { |
| 793 |
if (BaseScript* script = baseScript()) { |
| 794 |
TraceManuallyBarrieredEdge(trc, &script, "JSFunction script"); |
| 795 |
// Self-hosted scripts are shared with workers but are never relocated. |
| 796 |
// Skip unnecessary writes to prevent the possible data race. |
| 797 |
if (baseScript() != script) { |
| 798 |
HeapSlot& slot = getFixedSlotRef(NativeJitInfoOrInterpretedScriptSlot); |
| 799 |
slot.unbarrieredSet(JS::PrivateValue(script)); |
| 800 |
} |
| 801 |
} |
| 802 |
} |
| 803 |
// wasm/asm.js exported functions need to keep WasmInstantObject alive, |
| 804 |
// access it via WASM_INSTANCE_SLOT extended slot. |
| 805 |
if (isAsmJSNative() || isWasm()) { |
| 806 |
const Value& v = getExtendedSlot(FunctionExtended::WASM_INSTANCE_SLOT); |
| 807 |
if (!v.isUndefined()) { |
| 808 |
auto* instance = static_cast<wasm::Instance*>(v.toPrivate()); |
| 809 |
wasm::TraceInstanceEdge(trc, instance, "JSFunction instance"); |
| 810 |
} |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
static void fun_trace(JSTracer* trc, JSObject* obj) { |
| 815 |
obj->as<JSFunction>().trace(trc); |
| 816 |
} |
| 817 |
|
| 818 |
static JSObject* CreateFunctionConstructor(JSContext* cx, JSProtoKey key) { |
| 819 |
Rooted<GlobalObject*> global(cx, cx->global()); |
| 820 |
RootedObject functionProto(cx, &global->getPrototype(JSProto_Function)); |
| 821 |
|
| 822 |
RootedObject functionCtor( |
| 823 |
cx, NewFunctionWithProto( |
| 824 |
cx, Function, 1, FunctionFlags::NATIVE_CTOR, nullptr, |
| 825 |
Handle<PropertyName*>(cx->names().Function), functionProto, |
| 826 |
gc::AllocKind::FUNCTION, TenuredObject)); |
| 827 |
if (!functionCtor) { |
| 828 |
return nullptr; |
| 829 |
} |
| 830 |
|
| 831 |
return functionCtor; |
| 832 |
} |
| 833 |
|
| 834 |
static bool FunctionPrototype(JSContext* cx, unsigned argc, Value* vp) { |
| 835 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 836 |
args.rval().setUndefined(); |
| 837 |
return true; |
| 838 |
} |
| 839 |
|
| 840 |
static JSObject* CreateFunctionPrototype(JSContext* cx, JSProtoKey key) { |
| 841 |
Rooted<GlobalObject*> self(cx, cx->global()); |
| 842 |
|
| 843 |
RootedObject objectProto(cx, &self->getPrototype(JSProto_Object)); |
| 844 |
|
| 845 |
return NewFunctionWithProto( |
| 846 |
cx, FunctionPrototype, 0, FunctionFlags::NATIVE_FUN, nullptr, |
| 847 |
Handle<PropertyName*>(cx->names().empty_), objectProto, |
| 848 |
gc::AllocKind::FUNCTION, TenuredObject); |
| 849 |
} |
| 850 |
|
| 851 |
JSString* js::FunctionToStringCache::lookup(BaseScript* script) const { |
| 852 |
for (size_t i = 0; i < NumEntries; i++) { |
| 853 |
if (entries_[i].script == script) { |
| 854 |
return entries_[i].string; |
| 855 |
} |
| 856 |
} |
| 857 |
return nullptr; |
| 858 |
} |
| 859 |
|
| 860 |
void js::FunctionToStringCache::put(BaseScript* script, JSString* string) { |
| 861 |
for (size_t i = NumEntries - 1; i > 0; i--) { |
| 862 |
entries_[i] = entries_[i - 1]; |
| 863 |
} |
| 864 |
|
| 865 |
entries_[0].set(script, string); |
| 866 |
} |
| 867 |
|
| 868 |
JSString* js::FunctionToString(JSContext* cx, HandleFunction fun, |
| 869 |
bool isToSource) { |
| 870 |
if (IsAsmJSModule(fun)) { |
| 871 |
return AsmJSModuleToString(cx, fun, isToSource); |
| 872 |
} |
| 873 |
if (IsAsmJSFunction(fun)) { |
| 874 |
return AsmJSFunctionToString(cx, fun); |
| 875 |
} |
| 876 |
|
| 877 |
// Self-hosted built-ins should not expose their source code. |
| 878 |
bool haveSource = fun->isInterpreted() && !fun->isSelfHostedBuiltin(); |
| 879 |
|
| 880 |
// If we're in toSource mode, put parentheses around lambda functions so |
| 881 |
// that eval returns lambda, not function statement. |
| 882 |
bool addParentheses = |
| 883 |
haveSource && isToSource && (fun->isLambda() && !fun->isArrow()); |
| 884 |
|
| 885 |
if (haveSource) { |
| 886 |
if (!ScriptSource::loadSource(cx, fun->baseScript()->scriptSource(), |
| 887 |
&haveSource)) { |
| 888 |
return nullptr; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
// Fast path for the common case, to avoid StringBuilder overhead. |
| 893 |
if (!addParentheses && haveSource) { |
| 894 |
FunctionToStringCache& cache = cx->zone()->functionToStringCache(); |
| 895 |
if (JSString* str = cache.lookup(fun->baseScript())) { |
| 896 |
return str; |
| 897 |
} |
| 898 |
|
| 899 |
BaseScript* script = fun->baseScript(); |
| 900 |
size_t start = script->toStringStart(); |
| 901 |
size_t end = script->toStringEnd(); |
| 902 |
JSString* str = |
| 903 |
(end - start <= ScriptSource::SourceDeflateLimit) |
| 904 |
? script->scriptSource()->substring(cx, start, end) |
| 905 |
: script->scriptSource()->substringDontDeflate(cx, start, end); |
| 906 |
if (!str) { |
| 907 |
return nullptr; |
| 908 |
} |
| 909 |
|
| 910 |
cache.put(fun->baseScript(), str); |
| 911 |
return str; |
| 912 |
} |
| 913 |
|
| 914 |
JSStringBuilder out(cx); |
| 915 |
if (addParentheses) { |
| 916 |
if (!out.append('(')) { |
| 917 |
return nullptr; |
| 918 |
} |
| 919 |
} |
| 920 |
|
| 921 |
if (haveSource) { |
| 922 |
if (!fun->baseScript()->appendSourceDataForToString(cx, out)) { |
| 923 |
return nullptr; |
| 924 |
} |
| 925 |
} else if (!isToSource) { |
| 926 |
// For the toString() output the source representation must match |
| 927 |
// NativeFunction when no source text is available. |
| 928 |
// |
| 929 |
// NativeFunction: |
| 930 |
// function PropertyName[~Yield,~Await]opt ( |
| 931 |
// FormalParameters[~Yield,~Await] ) { [native code] } |
| 932 |
// |
| 933 |
// Additionally, if |fun| is a well-known intrinsic object and is not |
| 934 |
// identified as an anonymous function, the portion of the returned |
| 935 |
// string that would be matched by IdentifierName must be the initial |
| 936 |
// value of the name property of |fun|. |
| 937 |
|
| 938 |
auto hasGetterOrSetterPrefix = [](JSAtom* name) { |
| 939 |
auto hasGetterOrSetterPrefix = [](const auto* chars) { |
| 940 |
return (chars[0] == 'g' || chars[0] == 's') && chars[1] == 'e' && |
| 941 |
chars[2] == 't' && chars[3] == ' '; |
| 942 |
}; |
| 943 |
|
| 944 |
JS::AutoCheckCannotGC nogc; |
| 945 |
return name->length() >= 4 && |
| 946 |
(name->hasLatin1Chars() |
| 947 |
? hasGetterOrSetterPrefix(name->latin1Chars(nogc)) |
| 948 |
: hasGetterOrSetterPrefix(name->twoByteChars(nogc))); |
| 949 |
}; |
| 950 |
|
| 951 |
if (!out.append("function")) { |
| 952 |
return nullptr; |
| 953 |
} |
| 954 |
|
| 955 |
// We don't want to fully parse the function's name here because of |
| 956 |
// performance reasons, so only append the name if we're confident it |
| 957 |
// can be matched as the 'PropertyName' grammar production. |
| 958 |
if (fun->maybePartialExplicitName() && |
| 959 |
(fun->kind() == FunctionFlags::NormalFunction || |
| 960 |
(fun->isBuiltinNative() && (fun->kind() == FunctionFlags::Getter || |
| 961 |
fun->kind() == FunctionFlags::Setter)) || |
| 962 |
fun->kind() == FunctionFlags::Wasm || |
| 963 |
fun->kind() == FunctionFlags::ClassConstructor)) { |
| 964 |
if (!out.append(' ')) { |
| 965 |
return nullptr; |
| 966 |
} |
| 967 |
|
| 968 |
// Built-in getters or setters are classified as one of |
| 969 |
// NormalFunction, Getter, or Setter. Strip any leading "get " or "set " |
| 970 |
// if present. |
| 971 |
JSAtom* name = fun->maybePartialExplicitName(); |
| 972 |
size_t offset = hasGetterOrSetterPrefix(name) ? 4 : 0; |
| 973 |
if (!out.appendSubstring(name, offset, name->length() - offset)) { |
| 974 |
return nullptr; |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
if (!out.append("() {\n [native code]\n}")) { |
| 979 |
return nullptr; |
| 980 |
} |
| 981 |
} else { |
| 982 |
if (fun->isAsync()) { |
| 983 |
if (!out.append("async ")) { |
| 984 |
return nullptr; |
| 985 |
} |
| 986 |
} |
| 987 |
|
| 988 |
if (!fun->isArrow()) { |
| 989 |
if (!out.append("function")) { |
| 990 |
return nullptr; |
| 991 |
} |
| 992 |
|
| 993 |
if (fun->isGenerator()) { |
| 994 |
if (!out.append('*')) { |
| 995 |
return nullptr; |
| 996 |
} |
| 997 |
} |
| 998 |
} |
| 999 |
|
| 1000 |
JS::Rooted<JSAtom*> name(cx); |
| 1001 |
if (!fun->getExplicitName(cx, &name)) { |
| 1002 |
return nullptr; |
| 1003 |
} |
| 1004 |
|
| 1005 |
if (name) { |
| 1006 |
if (!out.append(' ')) { |
| 1007 |
return nullptr; |
| 1008 |
} |
| 1009 |
if (!out.append(name)) { |
| 1010 |
return nullptr; |
| 1011 |
} |
| 1012 |
} |
| 1013 |
|
| 1014 |
if (!out.append("() {\n [native code]\n}")) { |
| 1015 |
return nullptr; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (addParentheses) { |
| 1020 |
if (!out.append(')')) { |
| 1021 |
return nullptr; |
| 1022 |
} |
| 1023 |
} |
| 1024 |
|
| 1025 |
return out.finishString(); |
| 1026 |
} |
| 1027 |
|
| 1028 |
JSString* fun_toStringHelper(JSContext* cx, HandleObject obj, bool isToSource) { |
| 1029 |
if (!obj->is<JSFunction>()) { |
| 1030 |
if (JSFunToStringOp op = obj->getOpsFunToString()) { |
| 1031 |
return op(cx, obj, isToSource); |
| 1032 |
} |
| 1033 |
|
| 1034 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1035 |
JSMSG_INCOMPATIBLE_PROTO, "Function", "toString", |
| 1036 |
"object"); |
| 1037 |
return nullptr; |
| 1038 |
} |
| 1039 |
|
| 1040 |
return FunctionToString(cx, obj.as<JSFunction>(), isToSource); |
| 1041 |
} |
| 1042 |
|
| 1043 |
bool js::fun_toString(JSContext* cx, unsigned argc, Value* vp) { |
| 1044 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1045 |
MOZ_ASSERT(IsFunctionObject(args.calleev())); |
| 1046 |
|
| 1047 |
RootedObject obj(cx, ToObject(cx, args.thisv())); |
| 1048 |
if (!obj) { |
| 1049 |
return false; |
| 1050 |
} |
| 1051 |
|
| 1052 |
JSString* str = fun_toStringHelper(cx, obj, /* isToSource = */ false); |
| 1053 |
if (!str) { |
| 1054 |
return false; |
| 1055 |
} |
| 1056 |
|
| 1057 |
args.rval().setString(str); |
| 1058 |
return true; |
| 1059 |
} |
| 1060 |
|
| 1061 |
static bool fun_toSource(JSContext* cx, unsigned argc, Value* vp) { |
| 1062 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1063 |
MOZ_ASSERT(IsFunctionObject(args.calleev())); |
| 1064 |
|
| 1065 |
RootedObject obj(cx, ToObject(cx, args.thisv())); |
| 1066 |
if (!obj) { |
| 1067 |
return false; |
| 1068 |
} |
| 1069 |
|
| 1070 |
RootedString str(cx); |
| 1071 |
if (obj->isCallable()) { |
| 1072 |
str = fun_toStringHelper(cx, obj, /* isToSource = */ true); |
| 1073 |
} else { |
| 1074 |
str = ObjectToSource(cx, obj); |
| 1075 |
} |
| 1076 |
if (!str) { |
| 1077 |
return false; |
| 1078 |
} |
| 1079 |
|
| 1080 |
args.rval().setString(str); |
| 1081 |
return true; |
| 1082 |
} |
| 1083 |
|
| 1084 |
bool js::fun_call(JSContext* cx, unsigned argc, Value* vp) { |
| 1085 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1086 |
|
| 1087 |
HandleValue func = args.thisv(); |
| 1088 |
|
| 1089 |
// We don't need to do this -- Call would do it for us -- but the error |
| 1090 |
// message is *much* better if we do this here. (Without this, |
| 1091 |
// JSDVG_SEARCH_STACK tries to decompile |func| as if it were |this| in |
| 1092 |
// the scripted caller's frame -- so for example |
| 1093 |
// |
| 1094 |
// Function.prototype.call.call({}); |
| 1095 |
// |
| 1096 |
// would identify |{}| as |this| as being the result of evaluating |
| 1097 |
// |Function.prototype.call| and would conclude, "Function.prototype.call |
| 1098 |
// is not a function". Grotesque.) |
| 1099 |
if (!IsCallable(func)) { |
| 1100 |
ReportIncompatibleMethod(cx, args, &FunctionClass); |
| 1101 |
return false; |
| 1102 |
} |
| 1103 |
|
| 1104 |
size_t argCount = args.length(); |
| 1105 |
if (argCount > 0) { |
| 1106 |
argCount--; // strip off provided |this| |
| 1107 |
} |
| 1108 |
|
| 1109 |
InvokeArgs iargs(cx); |
| 1110 |
if (!iargs.init(cx, argCount)) { |
| 1111 |
return false; |
| 1112 |
} |
| 1113 |
|
| 1114 |
for (size_t i = 0; i < argCount; i++) { |
| 1115 |
iargs[i].set(args[i + 1]); |
| 1116 |
} |
| 1117 |
|
| 1118 |
return Call(cx, func, args.get(0), iargs, args.rval(), CallReason::FunCall); |
| 1119 |
} |
| 1120 |
|
| 1121 |
// ES5 15.3.4.3 |
| 1122 |
bool js::fun_apply(JSContext* cx, unsigned argc, Value* vp) { |
| 1123 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1124 |
|
| 1125 |
// Step 1. |
| 1126 |
// |
| 1127 |
// Note that we must check callability here, not at actual call time, |
| 1128 |
// because extracting argument values from the provided arraylike might |
| 1129 |
// have side effects or throw an exception. |
| 1130 |
HandleValue fval = args.thisv(); |
| 1131 |
if (!IsCallable(fval)) { |
| 1132 |
ReportIncompatibleMethod(cx, args, &FunctionClass); |
| 1133 |
return false; |
| 1134 |
} |
| 1135 |
|
| 1136 |
// Step 2. |
| 1137 |
if (args.length() < 2 || args[1].isNullOrUndefined()) { |
| 1138 |
return fun_call(cx, (args.length() > 0) ? 1 : 0, vp); |
| 1139 |
} |
| 1140 |
|
| 1141 |
// Step 3. |
| 1142 |
if (!args[1].isObject()) { |
| 1143 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1144 |
JSMSG_BAD_APPLY_ARGS, "apply"); |
| 1145 |
return false; |
| 1146 |
} |
| 1147 |
|
| 1148 |
// Steps 4-5 (note erratum removing steps originally numbered 5 and 7 in |
| 1149 |
// original version of ES5). |
| 1150 |
RootedObject aobj(cx, &args[1].toObject()); |
| 1151 |
uint64_t length; |
| 1152 |
if (!GetLengthProperty(cx, aobj, &length)) { |
| 1153 |
return false; |
| 1154 |
} |
| 1155 |
|
| 1156 |
// Step 6. |
| 1157 |
InvokeArgs args2(cx); |
| 1158 |
if (!args2.init(cx, length)) { |
| 1159 |
return false; |
| 1160 |
} |
| 1161 |
|
| 1162 |
MOZ_ASSERT(length <= ARGS_LENGTH_MAX); |
| 1163 |
|
| 1164 |
// Steps 7-8. |
| 1165 |
if (!GetElements(cx, aobj, length, args2.array())) { |
| 1166 |
return false; |
| 1167 |
} |
| 1168 |
|
| 1169 |
// Step 9. |
| 1170 |
return Call(cx, fval, args[0], args2, args.rval(), CallReason::FunCall); |
| 1171 |
} |
| 1172 |
|
| 1173 |
static const JSFunctionSpec function_methods[] = { |
| 1174 |
JS_FN("toSource", fun_toSource, 0, 0), |
| 1175 |
JS_FN("toString", fun_toString, 0, 0), |
| 1176 |
JS_FN("apply", fun_apply, 2, 0), |
| 1177 |
JS_FN("call", fun_call, 1, 0), |
| 1178 |
JS_INLINABLE_FN("bind", BoundFunctionObject::functionBind, 1, 0, |
| 1179 |
FunctionBind), |
| 1180 |
JS_SYM_FN(hasInstance, fun_symbolHasInstance, 1, |
| 1181 |
JSPROP_READONLY | JSPROP_PERMANENT), |
| 1182 |
JS_FS_END, |
| 1183 |
}; |
| 1184 |
|
| 1185 |
static const JSClassOps JSFunctionClassOps = { |
| 1186 |
nullptr, // addProperty |
| 1187 |
nullptr, // delProperty |
| 1188 |
fun_enumerate, // enumerate |
| 1189 |
nullptr, // newEnumerate |
| 1190 |
fun_resolve, // resolve |
| 1191 |
fun_mayResolve, // mayResolve |
| 1192 |
nullptr, // finalize |
| 1193 |
nullptr, // call |
| 1194 |
nullptr, // construct |
| 1195 |
fun_trace, // trace |
| 1196 |
}; |
| 1197 |
|
| 1198 |
static const ClassSpec JSFunctionClassSpec = { |
| 1199 |
CreateFunctionConstructor, CreateFunctionPrototype, nullptr, nullptr, |
| 1200 |
function_methods, function_properties, |
| 1201 |
}; |
| 1202 |
|
| 1203 |
const JSClass js::FunctionClass = { |
| 1204 |
"Function", |
| 1205 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Function) | |
| 1206 |
JSCLASS_HAS_RESERVED_SLOTS(JSFunction::SlotCount), |
| 1207 |
&JSFunctionClassOps, |
| 1208 |
&JSFunctionClassSpec, |
| 1209 |
}; |
| 1210 |
|
| 1211 |
const JSClass js::ExtendedFunctionClass = { |
| 1212 |
"Function", |
| 1213 |
JSCLASS_HAS_CACHED_PROTO(JSProto_Function) | |
| 1214 |
JSCLASS_HAS_RESERVED_SLOTS(FunctionExtended::SlotCount), |
| 1215 |
&JSFunctionClassOps, |
| 1216 |
&JSFunctionClassSpec, |
| 1217 |
}; |
| 1218 |
|
| 1219 |
const JSClass* const js::FunctionClassPtr = &FunctionClass; |
| 1220 |
const JSClass* const js::FunctionExtendedClassPtr = &ExtendedFunctionClass; |
| 1221 |
|
| 1222 |
bool JSFunction::isDerivedClassConstructor() const { |
| 1223 |
bool derived = hasBaseScript() && baseScript()->isDerivedClassConstructor(); |
| 1224 |
MOZ_ASSERT_IF(derived, isClassConstructor()); |
| 1225 |
return derived; |
| 1226 |
} |
| 1227 |
|
| 1228 |
bool JSFunction::isSyntheticFunction() const { |
| 1229 |
bool synthetic = hasBaseScript() && baseScript()->isSyntheticFunction(); |
| 1230 |
MOZ_ASSERT_IF(synthetic, isMethod()); |
| 1231 |
return synthetic; |
| 1232 |
} |
| 1233 |
|
| 1234 |
/* static */ |
| 1235 |
bool JSFunction::delazifyLazilyInterpretedFunction(JSContext* cx, |
| 1236 |
HandleFunction fun) { |
| 1237 |
MOZ_ASSERT(fun->hasBaseScript()); |
| 1238 |
MOZ_ASSERT(cx->compartment() == fun->compartment()); |
| 1239 |
|
| 1240 |
// The function must be same-compartment but might be cross-realm. Make sure |
| 1241 |
// the script is created in the function's realm. |
| 1242 |
AutoRealm ar(cx, fun); |
| 1243 |
|
| 1244 |
Rooted<BaseScript*> lazy(cx, fun->baseScript()); |
| 1245 |
RootedFunction canonicalFun(cx, lazy->function()); |
| 1246 |
|
| 1247 |
// If this function is non-canonical, then use the canonical function first |
| 1248 |
// to get the delazified script. This may result in calling this method |
| 1249 |
// again on the canonical function. This ensures the canonical function is |
| 1250 |
// always non-lazy if any of the clones are non-lazy. |
| 1251 |
if (fun != canonicalFun) { |
| 1252 |
JSScript* script = JSFunction::getOrCreateScript(cx, canonicalFun); |
| 1253 |
if (!script) { |
| 1254 |
return false; |
| 1255 |
} |
| 1256 |
|
| 1257 |
// Delazifying the canonical function should naturally make us non-lazy |
| 1258 |
// because we share a BaseScript with the canonical function. |
| 1259 |
MOZ_ASSERT(fun->hasBytecode()); |
| 1260 |
return true; |
| 1261 |
} |
| 1262 |
|
| 1263 |
// Finally, compile the script if it really doesn't exist. |
| 1264 |
AutoReportFrontendContext fc(cx); |
| 1265 |
if (!frontend::DelazifyCanonicalScriptedFunction(cx, &fc, fun)) { |
| 1266 |
// The frontend shouldn't fail after linking the function and the |
| 1267 |
// non-lazy script together. |
| 1268 |
MOZ_ASSERT(fun->baseScript() == lazy); |
| 1269 |
MOZ_ASSERT(lazy->isReadyForDelazification()); |
| 1270 |
return false; |
| 1271 |
} |
| 1272 |
|
| 1273 |
return true; |
| 1274 |
} |
| 1275 |
|
| 1276 |
/* static */ |
| 1277 |
bool JSFunction::delazifySelfHostedLazyFunction(JSContext* cx, |
| 1278 |
js::HandleFunction fun) { |
| 1279 |
MOZ_ASSERT(cx->compartment() == fun->compartment()); |
| 1280 |
|
| 1281 |
// The function must be same-compartment but might be cross-realm. Make sure |
| 1282 |
// the script is created in the function's realm. |
| 1283 |
AutoRealm ar(cx, fun); |
| 1284 |
|
| 1285 |
/* Lazily cloned self-hosted script. */ |
| 1286 |
MOZ_ASSERT(fun->isSelfHostedBuiltin()); |
| 1287 |
Rooted<PropertyName*> funName(cx, GetClonedSelfHostedFunctionName(fun)); |
| 1288 |
if (!funName) { |
| 1289 |
return false; |
| 1290 |
} |
| 1291 |
return cx->runtime()->delazifySelfHostedFunction(cx, funName, fun); |
| 1292 |
} |
| 1293 |
|
| 1294 |
void JSFunction::maybeRelazify(JSRuntime* rt) { |
| 1295 |
MOZ_ASSERT(!isIncomplete(), "Cannot relazify incomplete functions"); |
| 1296 |
|
| 1297 |
// Don't relazify functions in compartments that are active. |
| 1298 |
Realm* realm = this->realm(); |
| 1299 |
if (!rt->allowRelazificationForTesting) { |
| 1300 |
if (realm->compartment()->gcState.hasEnteredRealm) { |
| 1301 |
return; |
| 1302 |
} |
| 1303 |
|
| 1304 |
MOZ_ASSERT(!realm->hasBeenEnteredIgnoringJit()); |
| 1305 |
} |
| 1306 |
|
| 1307 |
// Don't relazify if the realm is being debugged. The debugger side-tables |
| 1308 |
// such as the set of active breakpoints require bytecode to exist. |
| 1309 |
if (realm->isDebuggee()) { |
| 1310 |
return; |
| 1311 |
} |
| 1312 |
|
| 1313 |
// Don't relazify if we are collecting coverage so that we do not lose count |
| 1314 |
// information. |
| 1315 |
if (coverage::IsLCovEnabled()) { |
| 1316 |
return; |
| 1317 |
} |
| 1318 |
|
| 1319 |
// Check the script's eligibility. |
| 1320 |
JSScript* script = nonLazyScript(); |
| 1321 |
if (!script->allowRelazify()) { |
| 1322 |
return; |
| 1323 |
} |
| 1324 |
MOZ_ASSERT(script->isRelazifiable()); |
| 1325 |
|
| 1326 |
// There must not be any JIT code attached since the relazification process |
| 1327 |
// does not know how to discard it. In general, the GC should discard most JIT |
| 1328 |
// code before attempting relazification. |
| 1329 |
if (script->hasJitScript()) { |
| 1330 |
return; |
| 1331 |
} |
| 1332 |
|
| 1333 |
if (isSelfHostedBuiltin()) { |
| 1334 |
gc::PreWriteBarrier(script); |
| 1335 |
initSelfHostedLazyScript(&rt->selfHostedLazyScript.ref()); |
| 1336 |
} else { |
| 1337 |
script->relazify(rt); |
| 1338 |
} |
| 1339 |
} |
| 1340 |
|
| 1341 |
js::GeneratorKind JSFunction::clonedSelfHostedGeneratorKind() const { |
| 1342 |
MOZ_ASSERT(hasSelfHostedLazyScript()); |
| 1343 |
|
| 1344 |
// This is a lazy clone of a self-hosted builtin. It has no BaseScript, and |
| 1345 |
// `this->flags_` does not contain the generator kind. Consult the |
| 1346 |
// implementation in the self-hosting realm, which has a BaseScript. |
| 1347 |
MOZ_RELEASE_ASSERT(isExtended()); |
| 1348 |
PropertyName* name = GetClonedSelfHostedFunctionName(this); |
| 1349 |
return runtimeFromMainThread()->getSelfHostedFunctionGeneratorKind(name); |
| 1350 |
} |
| 1351 |
|
| 1352 |
// ES2018 draft rev 2aea8f3e617b49df06414eb062ab44fad87661d3 |
| 1353 |
// 19.2.1.1.1 CreateDynamicFunction( constructor, newTarget, kind, args ) |
| 1354 |
static bool CreateDynamicFunction(JSContext* cx, const CallArgs& args, |
| 1355 |
GeneratorKind generatorKind, |
| 1356 |
FunctionAsyncKind asyncKind) { |
| 1357 |
using namespace frontend; |
| 1358 |
|
| 1359 |
// Steps 1-5. |
| 1360 |
bool isGenerator = generatorKind == GeneratorKind::Generator; |
| 1361 |
bool isAsync = asyncKind == FunctionAsyncKind::AsyncFunction; |
| 1362 |
|
| 1363 |
RootedScript maybeScript(cx); |
| 1364 |
const char* filename; |
| 1365 |
uint32_t lineno; |
| 1366 |
bool mutedErrors; |
| 1367 |
uint32_t pcOffset; |
| 1368 |
DescribeScriptedCallerForCompilation(cx, &maybeScript, &filename, &lineno, |
| 1369 |
&pcOffset, &mutedErrors); |
| 1370 |
|
| 1371 |
const char* introductionType = "Function"; |
| 1372 |
if (isAsync) { |
| 1373 |
if (isGenerator) { |
| 1374 |
introductionType = "AsyncGenerator"; |
| 1375 |
} else { |
| 1376 |
introductionType = "AsyncFunction"; |
| 1377 |
} |
| 1378 |
} else if (isGenerator) { |
| 1379 |
introductionType = "GeneratorFunction"; |
| 1380 |
} |
| 1381 |
|
| 1382 |
const char* introducerFilename = filename; |
| 1383 |
if (maybeScript && maybeScript->scriptSource()->introducerFilename()) { |
| 1384 |
introducerFilename = maybeScript->scriptSource()->introducerFilename(); |
| 1385 |
} |
| 1386 |
|
| 1387 |
CompileOptions options(cx); |
| 1388 |
options.setMutedErrors(mutedErrors) |
| 1389 |
.setFileAndLine(filename, 1) |
| 1390 |
.setNoScriptRval(false) |
| 1391 |
.setIntroductionInfo(introducerFilename, introductionType, lineno, |
| 1392 |
pcOffset) |
| 1393 |
.setDeferDebugMetadata(); |
| 1394 |
|
| 1395 |
JSStringBuilder sb(cx); |
| 1396 |
|
| 1397 |
if (isAsync) { |
| 1398 |
if (!sb.append("async ")) { |
| 1399 |
return false; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
if (!sb.append("function")) { |
| 1403 |
return false; |
| 1404 |
} |
| 1405 |
if (isGenerator) { |
| 1406 |
if (!sb.append('*')) { |
| 1407 |
return false; |
| 1408 |
} |
| 1409 |
} |
| 1410 |
|
| 1411 |
if (!sb.append(" anonymous(")) { |
| 1412 |
return false; |
| 1413 |
} |
| 1414 |
|
| 1415 |
JS::RootedVector<JSString*> parameterStrings(cx); |
| 1416 |
JS::RootedVector<Value> parameterArgs(cx); |
| 1417 |
if (args.length() > 1) { |
| 1418 |
RootedString str(cx); |
| 1419 |
|
| 1420 |
// Steps 10, 14.d. |
| 1421 |
unsigned n = args.length() - 1; |
| 1422 |
if (!parameterStrings.reserve(n) || !parameterArgs.reserve(n)) { |
| 1423 |
return false; |
| 1424 |
} |
| 1425 |
|
| 1426 |
for (unsigned i = 0; i < n; i++) { |
| 1427 |
if (!parameterArgs.append(args[i])) { |
| 1428 |
return false; |
| 1429 |
} |
| 1430 |
|
| 1431 |
// Steps 14.a-b, 14.d.i-ii. |
| 1432 |
str = ToString<CanGC>(cx, args[i]); |
| 1433 |
if (!str) { |
| 1434 |
return false; |
| 1435 |
} |
| 1436 |
|
| 1437 |
if (!parameterStrings.append(str)) { |
| 1438 |
return false; |
| 1439 |
} |
| 1440 |
|
| 1441 |
// Steps 14.b, 14.d.iii. |
| 1442 |
if (!sb.append(str)) { |
| 1443 |
return false; |
| 1444 |
} |
| 1445 |
|
| 1446 |
if (i < args.length() - 2) { |
| 1447 |
// Step 14.d.iii. |
| 1448 |
if (!sb.append(',')) { |
| 1449 |
return false; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
} |
| 1453 |
} |
| 1454 |
|
| 1455 |
if (!sb.append('\n')) { |
| 1456 |
return false; |
| 1457 |
} |
| 1458 |
|
| 1459 |
// Remember the position of ")". |
| 1460 |
Maybe<uint32_t> parameterListEnd = Some(uint32_t(sb.length())); |
| 1461 |
static_assert(FunctionConstructorMedialSigils[0] == ')'); |
| 1462 |
|
| 1463 |
if (!sb.append(FunctionConstructorMedialSigils.data(), |
| 1464 |
FunctionConstructorMedialSigils.length())) { |
| 1465 |
return false; |
| 1466 |
} |
| 1467 |
|
| 1468 |
JS::RootedValue bodyArg(cx); |
| 1469 |
RootedString bodyString(cx); |
| 1470 |
if (args.length() > 0) { |
| 1471 |
// Steps 13, 14.e, 15. |
| 1472 |
bodyArg = args[args.length() - 1]; |
| 1473 |
bodyString = ToString<CanGC>(cx, bodyArg); |
| 1474 |
if (!bodyString || !sb.append(bodyString)) { |
| 1475 |
return false; |
| 1476 |
} |
| 1477 |
} |
| 1478 |
|
| 1479 |
if (!sb.append(FunctionConstructorFinalBrace.data(), |
| 1480 |
FunctionConstructorFinalBrace.length())) { |
| 1481 |
return false; |
| 1482 |
} |
| 1483 |
|
| 1484 |
// The parser only accepts two byte strings. |
| 1485 |
if (!sb.ensureTwoByteChars()) { |
| 1486 |
return false; |
| 1487 |
} |
| 1488 |
|
| 1489 |
RootedString functionText(cx, sb.finishString()); |
| 1490 |
if (!functionText) { |
| 1491 |
return false; |
| 1492 |
} |
| 1493 |
|
| 1494 |
// Block this call if security callbacks forbid it. |
| 1495 |
bool canCompileStrings = false; |
| 1496 |
if (!cx->isRuntimeCodeGenEnabled(JS::RuntimeCode::JS, functionText, |
| 1497 |
JS::CompilationType::Function, |
| 1498 |
parameterStrings, bodyString, parameterArgs, |
| 1499 |
bodyArg, &canCompileStrings)) { |
| 1500 |
return false; |
| 1501 |
} |
| 1502 |
if (!canCompileStrings) { |
| 1503 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1504 |
JSMSG_CSP_BLOCKED_FUNCTION); |
| 1505 |
return false; |
| 1506 |
} |
| 1507 |
|
| 1508 |
// Steps 7.a-b, 8.a-b, 9.a-b, 16-28. |
| 1509 |
AutoStableStringChars linearChars(cx); |
| 1510 |
if (!linearChars.initTwoByte(cx, functionText)) { |
| 1511 |
return false; |
| 1512 |
} |
| 1513 |
|
| 1514 |
SourceText<char16_t> srcBuf; |
| 1515 |
if (!srcBuf.initMaybeBorrowed(cx, linearChars)) { |
| 1516 |
return false; |
| 1517 |
} |
| 1518 |
|
| 1519 |
FunctionSyntaxKind syntaxKind = FunctionSyntaxKind::Expression; |
| 1520 |
|
| 1521 |
RootedFunction fun(cx); |
| 1522 |
JSProtoKey protoKey; |
| 1523 |
if (isAsync) { |
| 1524 |
if (isGenerator) { |
| 1525 |
fun = CompileStandaloneAsyncGenerator(cx, options, srcBuf, |
| 1526 |
parameterListEnd, syntaxKind); |
| 1527 |
protoKey = JSProto_AsyncGeneratorFunction; |
| 1528 |
} else { |
| 1529 |
fun = CompileStandaloneAsyncFunction(cx, options, srcBuf, |
| 1530 |
parameterListEnd, syntaxKind); |
| 1531 |
protoKey = JSProto_AsyncFunction; |
| 1532 |
} |
| 1533 |
} else { |
| 1534 |
if (isGenerator) { |
| 1535 |
fun = CompileStandaloneGenerator(cx, options, srcBuf, parameterListEnd, |
| 1536 |
syntaxKind); |
| 1537 |
protoKey = JSProto_GeneratorFunction; |
| 1538 |
} else { |
| 1539 |
fun = CompileStandaloneFunction(cx, options, srcBuf, parameterListEnd, |
| 1540 |
syntaxKind); |
| 1541 |
protoKey = JSProto_Function; |
| 1542 |
} |
| 1543 |
} |
| 1544 |
if (!fun) { |
| 1545 |
return false; |
| 1546 |
} |
| 1547 |
|
| 1548 |
RootedValue undefValue(cx); |
| 1549 |
RootedScript funScript(cx, JS_GetFunctionScript(cx, fun)); |
| 1550 |
JS::InstantiateOptions instantiateOptions(options); |
| 1551 |
if (funScript && |
| 1552 |
!UpdateDebugMetadata(cx, funScript, instantiateOptions, undefValue, |
| 1553 |
nullptr, maybeScript, maybeScript)) { |
| 1554 |
return false; |
| 1555 |
} |
| 1556 |
|
| 1557 |
if (fun->isInterpreted()) { |
| 1558 |
fun->initEnvironment(&cx->global()->lexicalEnvironment()); |
| 1559 |
} |
| 1560 |
|
| 1561 |
// Steps 6, 29. |
| 1562 |
RootedObject proto(cx); |
| 1563 |
if (!GetPrototypeFromBuiltinConstructor(cx, args, protoKey, &proto)) { |
| 1564 |
return false; |
| 1565 |
} |
| 1566 |
|
| 1567 |
// Steps 7.d, 8.d (implicit). |
| 1568 |
// Call SetPrototype if an explicit prototype was given. |
| 1569 |
if (proto && !SetPrototype(cx, fun, proto)) { |
| 1570 |
return false; |
| 1571 |
} |
| 1572 |
|
| 1573 |
// Step 38. |
| 1574 |
args.rval().setObject(*fun); |
| 1575 |
return true; |
| 1576 |
} |
| 1577 |
|
| 1578 |
bool js::Function(JSContext* cx, unsigned argc, Value* vp) { |
| 1579 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1580 |
return CreateDynamicFunction(cx, args, GeneratorKind::NotGenerator, |
| 1581 |
FunctionAsyncKind::SyncFunction); |
| 1582 |
} |
| 1583 |
|
| 1584 |
bool js::Generator(JSContext* cx, unsigned argc, Value* vp) { |
| 1585 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1586 |
return CreateDynamicFunction(cx, args, GeneratorKind::Generator, |
| 1587 |
FunctionAsyncKind::SyncFunction); |
| 1588 |
} |
| 1589 |
|
| 1590 |
bool js::AsyncFunctionConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 1591 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1592 |
return CreateDynamicFunction(cx, args, GeneratorKind::NotGenerator, |
| 1593 |
FunctionAsyncKind::AsyncFunction); |
| 1594 |
} |
| 1595 |
|
| 1596 |
bool js::AsyncGeneratorConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 1597 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1598 |
return CreateDynamicFunction(cx, args, GeneratorKind::Generator, |
| 1599 |
FunctionAsyncKind::AsyncFunction); |
| 1600 |
} |
| 1601 |
|
| 1602 |
bool JSFunction::isBuiltinFunctionConstructor() { |
| 1603 |
return maybeNative() == Function || maybeNative() == Generator; |
| 1604 |
} |
| 1605 |
|
| 1606 |
bool JSFunction::needsExtraBodyVarEnvironment() const { |
| 1607 |
if (isNativeFun()) { |
| 1608 |
return false; |
| 1609 |
} |
| 1610 |
|
| 1611 |
if (!nonLazyScript()->functionHasExtraBodyVarScope()) { |
| 1612 |
return false; |
| 1613 |
} |
| 1614 |
|
| 1615 |
return nonLazyScript()->functionExtraBodyVarScope()->hasEnvironment(); |
| 1616 |
} |
| 1617 |
|
| 1618 |
bool JSFunction::needsNamedLambdaEnvironment() const { |
| 1619 |
if (!isNamedLambda()) { |
| 1620 |
return false; |
| 1621 |
} |
| 1622 |
|
| 1623 |
LexicalScope* scope = nonLazyScript()->maybeNamedLambdaScope(); |
| 1624 |
if (!scope) { |
| 1625 |
return false; |
| 1626 |
} |
| 1627 |
|
| 1628 |
return scope->hasEnvironment(); |
| 1629 |
} |
| 1630 |
|
| 1631 |
bool JSFunction::needsCallObject() const { |
| 1632 |
if (isNativeFun()) { |
| 1633 |
return false; |
| 1634 |
} |
| 1635 |
|
| 1636 |
MOZ_ASSERT(hasBytecode()); |
| 1637 |
|
| 1638 |
// Note: this should be kept in sync with |
| 1639 |
// FunctionBox::needsCallObjectRegardlessOfBindings(). |
| 1640 |
MOZ_ASSERT_IF( |
| 1641 |
baseScript()->funHasExtensibleScope() || isGenerator() || isAsync(), |
| 1642 |
nonLazyScript()->bodyScope()->hasEnvironment()); |
| 1643 |
|
| 1644 |
return nonLazyScript()->bodyScope()->hasEnvironment(); |
| 1645 |
} |
| 1646 |
|
| 1647 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1648 |
void JSFunction::dumpOwnFields(js::JSONPrinter& json) const { |
| 1649 |
if (maybePartialDisplayAtom()) { |
| 1650 |
js::GenericPrinter& out = |
| 1651 |
json.beginStringProperty("maybePartialDisplayAtom"); |
| 1652 |
maybePartialDisplayAtom()->dumpPropertyName(out); |
| 1653 |
json.endStringProperty(); |
| 1654 |
} |
| 1655 |
|
| 1656 |
if (hasBaseScript()) { |
| 1657 |
js::GenericPrinter& out = json.beginStringProperty("baseScript"); |
| 1658 |
baseScript()->dumpStringContent(out); |
| 1659 |
json.endStringProperty(); |
| 1660 |
} |
| 1661 |
|
| 1662 |
json.property("nargs", nargs()); |
| 1663 |
|
| 1664 |
json.beginInlineListProperty("flags"); |
| 1665 |
DumpFunctionFlagsItems(json, flags()); |
| 1666 |
json.endInlineList(); |
| 1667 |
|
| 1668 |
if (isNativeFun()) { |
| 1669 |
json.formatProperty("native", "0x%p", native()); |
| 1670 |
if (hasJitInfo()) { |
| 1671 |
json.formatProperty("jitInfo", "0x%p", jitInfo()); |
| 1672 |
} |
| 1673 |
} |
| 1674 |
} |
| 1675 |
|
| 1676 |
void JSFunction::dumpOwnStringContent(js::GenericPrinter& out) const { |
| 1677 |
if (maybePartialDisplayAtom() && maybePartialDisplayAtom()->length() > 0) { |
| 1678 |
maybePartialDisplayAtom()->dumpPropertyName(out); |
| 1679 |
} else { |
| 1680 |
out.put("(anonymous)"); |
| 1681 |
} |
| 1682 |
|
| 1683 |
if (hasBaseScript()) { |
| 1684 |
out.put(" ("); |
| 1685 |
baseScript()->dumpStringContent(out); |
| 1686 |
out.put(")"); |
| 1687 |
} |
| 1688 |
} |
| 1689 |
#endif |
| 1690 |
|
| 1691 |
#ifdef DEBUG |
| 1692 |
static JSObject* SkipEnvironmentObjects(JSObject* env) { |
| 1693 |
if (!env) { |
| 1694 |
return nullptr; |
| 1695 |
} |
| 1696 |
while (env->is<EnvironmentObject>()) { |
| 1697 |
env = &env->as<EnvironmentObject>().enclosingEnvironment(); |
| 1698 |
} |
| 1699 |
return env; |
| 1700 |
} |
| 1701 |
|
| 1702 |
static bool NewFunctionEnvironmentIsWellFormed(JSContext* cx, |
| 1703 |
HandleObject env) { |
| 1704 |
// Assert that the terminating environment is null, global, or a debug |
| 1705 |
// scope proxy. All other cases of polluting global scope behavior are |
| 1706 |
// handled by EnvironmentObjects (viz. non-syntactic DynamicWithObject and |
| 1707 |
// NonSyntacticVariablesObject). |
| 1708 |
RootedObject terminatingEnv(cx, SkipEnvironmentObjects(env)); |
| 1709 |
return !terminatingEnv || terminatingEnv == cx->global() || |
| 1710 |
terminatingEnv->is<DebugEnvironmentProxy>(); |
| 1711 |
} |
| 1712 |
#endif |
| 1713 |
|
| 1714 |
static inline const JSClass* FunctionClassForAllocKind( |
| 1715 |
gc::AllocKind allocKind) { |
| 1716 |
return (allocKind == gc::AllocKind::FUNCTION) ? FunctionClassPtr |
| 1717 |
: FunctionExtendedClassPtr; |
| 1718 |
} |
| 1719 |
|
| 1720 |
static void AssertClassMatchesAllocKind(const JSClass* clasp, |
| 1721 |
gc::AllocKind kind) { |
| 1722 |
#ifdef DEBUG |
| 1723 |
if (kind == gc::AllocKind::FUNCTION_EXTENDED) { |
| 1724 |
MOZ_ASSERT(clasp == FunctionExtendedClassPtr); |
| 1725 |
} else { |
| 1726 |
MOZ_ASSERT(kind == gc::AllocKind::FUNCTION); |
| 1727 |
MOZ_ASSERT(clasp == FunctionClassPtr); |
| 1728 |
} |
| 1729 |
#endif |
| 1730 |
} |
| 1731 |
|
| 1732 |
static SharedShape* GetFunctionShape(JSContext* cx, const JSClass* clasp, |
| 1733 |
JSObject* proto, gc::AllocKind allocKind) { |
| 1734 |
AssertClassMatchesAllocKind(clasp, allocKind); |
| 1735 |
|
| 1736 |
size_t nfixed = GetGCKindSlots(allocKind); |
| 1737 |
return SharedShape::getInitialShape( |
| 1738 |
cx, clasp, cx->realm(), TaggedProto(proto), nfixed, ObjectFlags()); |
| 1739 |
} |
| 1740 |
|
| 1741 |
SharedShape* GlobalObject::createFunctionShapeWithDefaultProto(JSContext* cx, |
| 1742 |
bool extended) { |
| 1743 |
GlobalObjectData& data = cx->global()->data(); |
| 1744 |
GCPtr<SharedShape*>& shapeRef = |
| 1745 |
extended ? data.extendedFunctionShapeWithDefaultProto |
| 1746 |
: data.functionShapeWithDefaultProto; |
| 1747 |
MOZ_ASSERT(!shapeRef); |
| 1748 |
|
| 1749 |
RootedObject proto(cx, |
| 1750 |
GlobalObject::getOrCreatePrototype(cx, JSProto_Function)); |
| 1751 |
if (!proto) { |
| 1752 |
return nullptr; |
| 1753 |
} |
| 1754 |
|
| 1755 |
// Creating %Function.prototype% can end up initializing the shape. |
| 1756 |
if (shapeRef) { |
| 1757 |
return shapeRef; |
| 1758 |
} |
| 1759 |
|
| 1760 |
gc::AllocKind allocKind = |
| 1761 |
extended ? gc::AllocKind::FUNCTION_EXTENDED : gc::AllocKind::FUNCTION; |
| 1762 |
const JSClass* clasp = FunctionClassForAllocKind(allocKind); |
| 1763 |
|
| 1764 |
SharedShape* shape = GetFunctionShape(cx, clasp, proto, allocKind); |
| 1765 |
if (!shape) { |
| 1766 |
return nullptr; |
| 1767 |
} |
| 1768 |
|
| 1769 |
shapeRef.init(shape); |
| 1770 |
return shape; |
| 1771 |
} |
| 1772 |
|
| 1773 |
JSFunction* js::NewFunctionWithProto( |
| 1774 |
JSContext* cx, Native native, unsigned nargs, FunctionFlags flags, |
| 1775 |
HandleObject enclosingEnv, Handle<JSAtom*> atom, HandleObject proto, |
| 1776 |
gc::AllocKind allocKind /* = AllocKind::FUNCTION */, |
| 1777 |
NewObjectKind newKind /* = GenericObject */) { |
| 1778 |
MOZ_ASSERT(allocKind == gc::AllocKind::FUNCTION || |
| 1779 |
allocKind == gc::AllocKind::FUNCTION_EXTENDED); |
| 1780 |
MOZ_ASSERT_IF(native, !enclosingEnv); |
| 1781 |
MOZ_ASSERT(NewFunctionEnvironmentIsWellFormed(cx, enclosingEnv)); |
| 1782 |
|
| 1783 |
// NOTE: Keep this in sync with `CreateFunctionFast` in Stencil.cpp |
| 1784 |
|
| 1785 |
const JSClass* clasp = FunctionClassForAllocKind(allocKind); |
| 1786 |
|
| 1787 |
Rooted<SharedShape*> shape(cx); |
| 1788 |
if (!proto) { |
| 1789 |
bool extended = (allocKind == gc::AllocKind::FUNCTION_EXTENDED); |
| 1790 |
shape = GlobalObject::getFunctionShapeWithDefaultProto(cx, extended); |
| 1791 |
} else { |
| 1792 |
shape = GetFunctionShape(cx, clasp, proto, allocKind); |
| 1793 |
} |
| 1794 |
if (!shape) { |
| 1795 |
return nullptr; |
| 1796 |
} |
| 1797 |
|
| 1798 |
gc::Heap heap = GetInitialHeap(newKind, clasp); |
| 1799 |
JSFunction* fun = JSFunction::create(cx, allocKind, heap, shape); |
| 1800 |
if (!fun) { |
| 1801 |
return nullptr; |
| 1802 |
} |
| 1803 |
|
| 1804 |
if (allocKind == gc::AllocKind::FUNCTION_EXTENDED) { |
| 1805 |
flags.setIsExtended(); |
| 1806 |
} |
| 1807 |
|
| 1808 |
// Disallow flags that require special union arms to be initialized. |
| 1809 |
MOZ_ASSERT(!flags.hasSelfHostedLazyScript()); |
| 1810 |
MOZ_ASSERT(!flags.isWasmWithJitEntry()); |
| 1811 |
|
| 1812 |
/* Initialize all function members. */ |
| 1813 |
fun->setArgCount(uint16_t(nargs)); |
| 1814 |
fun->setFlags(flags); |
| 1815 |
if (fun->isInterpreted()) { |
| 1816 |
fun->initScript(nullptr); |
| 1817 |
fun->initEnvironment(enclosingEnv); |
| 1818 |
} else { |
| 1819 |
MOZ_ASSERT(fun->isNativeFun()); |
| 1820 |
fun->initNative(native, nullptr); |
| 1821 |
} |
| 1822 |
fun->initAtom(atom); |
| 1823 |
|
| 1824 |
#ifdef DEBUG |
| 1825 |
fun->assertFunctionKindIntegrity(); |
| 1826 |
#endif |
| 1827 |
|
| 1828 |
return fun; |
| 1829 |
} |
| 1830 |
|
| 1831 |
bool js::GetFunctionPrototype(JSContext* cx, js::GeneratorKind generatorKind, |
| 1832 |
js::FunctionAsyncKind asyncKind, |
| 1833 |
js::MutableHandleObject proto) { |
| 1834 |
if (generatorKind == js::GeneratorKind::NotGenerator) { |
| 1835 |
if (asyncKind == js::FunctionAsyncKind::SyncFunction) { |
| 1836 |
proto.set(nullptr); |
| 1837 |
return true; |
| 1838 |
} |
| 1839 |
|
| 1840 |
proto.set( |
| 1841 |
GlobalObject::getOrCreateAsyncFunctionPrototype(cx, cx->global())); |
| 1842 |
} else { |
| 1843 |
if (asyncKind == js::FunctionAsyncKind::SyncFunction) { |
| 1844 |
proto.set(GlobalObject::getOrCreateGeneratorFunctionPrototype( |
| 1845 |
cx, cx->global())); |
| 1846 |
} else { |
| 1847 |
proto.set(GlobalObject::getOrCreateAsyncGenerator(cx, cx->global())); |
| 1848 |
} |
| 1849 |
} |
| 1850 |
return !!proto; |
| 1851 |
} |
| 1852 |
|
| 1853 |
#ifdef DEBUG |
| 1854 |
static bool CanReuseScriptForClone(JS::Realm* realm, HandleFunction fun, |
| 1855 |
HandleObject newEnclosingEnv) { |
| 1856 |
MOZ_ASSERT(fun->isInterpreted()); |
| 1857 |
|
| 1858 |
if (realm != fun->realm()) { |
| 1859 |
return false; |
| 1860 |
} |
| 1861 |
|
| 1862 |
if (newEnclosingEnv->is<GlobalObject>()) { |
| 1863 |
return true; |
| 1864 |
} |
| 1865 |
|
| 1866 |
// Don't need to clone the script if newEnclosingEnv is a syntactic scope, |
| 1867 |
// since in that case we have some actual scope objects on our scope chain and |
| 1868 |
// whatnot; whoever put them there should be responsible for setting our |
| 1869 |
// script's flags appropriately. We hit this case for JSOp::Lambda, for |
| 1870 |
// example. |
| 1871 |
if (IsSyntacticEnvironment(newEnclosingEnv)) { |
| 1872 |
return true; |
| 1873 |
} |
| 1874 |
|
| 1875 |
// We need to clone the script if we're not already marked as having a |
| 1876 |
// non-syntactic scope. The HasNonSyntacticScope flag is not computed for lazy |
| 1877 |
// scripts so fallback to checking the scope chain. |
| 1878 |
BaseScript* script = fun->baseScript(); |
| 1879 |
return script->hasNonSyntacticScope() || |
| 1880 |
script->enclosingScope()->hasOnChain(ScopeKind::NonSyntactic); |
| 1881 |
} |
| 1882 |
#endif |
| 1883 |
|
| 1884 |
static inline JSFunction* NewFunctionClone(JSContext* cx, HandleFunction fun, |
| 1885 |
HandleObject proto, |
| 1886 |
gc::Heap heap = gc::Heap::Default, |
| 1887 |
gc::AllocSite* site = nullptr) { |
| 1888 |
MOZ_ASSERT(cx->realm() == fun->realm()); |
| 1889 |
MOZ_ASSERT(proto); |
| 1890 |
|
| 1891 |
const JSClass* clasp = fun->getClass(); |
| 1892 |
gc::AllocKind allocKind = fun->getAllocKind(); |
| 1893 |
AssertClassMatchesAllocKind(clasp, allocKind); |
| 1894 |
|
| 1895 |
// If |fun| also has |proto| as prototype (the common case) we can reuse its |
| 1896 |
// shape for the clone. This works because |fun| isn't exposed to script. |
| 1897 |
Rooted<SharedShape*> shape(cx); |
| 1898 |
if (fun->staticPrototype() == proto) { |
| 1899 |
shape = fun->sharedShape(); |
| 1900 |
MOZ_ASSERT(shape->propMapLength() == 0); |
| 1901 |
MOZ_ASSERT(shape->objectFlags().isEmpty()); |
| 1902 |
MOZ_ASSERT(shape->realm() == cx->realm()); |
| 1903 |
} else { |
| 1904 |
shape = GetFunctionShape(cx, clasp, proto, allocKind); |
| 1905 |
if (!shape) { |
| 1906 |
return nullptr; |
| 1907 |
} |
| 1908 |
} |
| 1909 |
|
| 1910 |
JSFunction* clone = JSFunction::create(cx, allocKind, heap, shape, site); |
| 1911 |
if (!clone) { |
| 1912 |
return nullptr; |
| 1913 |
} |
| 1914 |
|
| 1915 |
// The following flags shouldn't be set or cloned. |
| 1916 |
MOZ_ASSERT(!fun->flags().hasResolvedLength()); |
| 1917 |
MOZ_ASSERT(!fun->flags().hasResolvedName()); |
| 1918 |
|
| 1919 |
clone->setArgCount(fun->nargs()); |
| 1920 |
clone->setFlags(fun->flags()); |
| 1921 |
|
| 1922 |
// Note: |clone| and |fun| are same-zone so we don't need to call markAtom. |
| 1923 |
clone->initAtom(fun->maybePartialDisplayAtom()); |
| 1924 |
|
| 1925 |
#ifdef DEBUG |
| 1926 |
clone->assertFunctionKindIntegrity(); |
| 1927 |
#endif |
| 1928 |
|
| 1929 |
return clone; |
| 1930 |
} |
| 1931 |
|
| 1932 |
JSFunction* js::CloneFunctionReuseScript(JSContext* cx, HandleFunction fun, |
| 1933 |
HandleObject enclosingEnv, |
| 1934 |
HandleObject proto, gc::Heap heap, |
| 1935 |
gc::AllocSite* site) { |
| 1936 |
MOZ_ASSERT(cx->realm() == fun->realm()); |
| 1937 |
MOZ_ASSERT(NewFunctionEnvironmentIsWellFormed(cx, enclosingEnv)); |
| 1938 |
MOZ_ASSERT(fun->isInterpreted()); |
| 1939 |
MOZ_ASSERT(fun->hasBaseScript()); |
| 1940 |
MOZ_ASSERT(CanReuseScriptForClone(cx->realm(), fun, enclosingEnv)); |
| 1941 |
|
| 1942 |
JSFunction* clone = NewFunctionClone(cx, fun, proto, heap, site); |
| 1943 |
if (!clone) { |
| 1944 |
return nullptr; |
| 1945 |
} |
| 1946 |
|
| 1947 |
BaseScript* base = fun->baseScript(); |
| 1948 |
clone->initScript(base); |
| 1949 |
clone->initEnvironment(enclosingEnv); |
| 1950 |
|
| 1951 |
#ifdef DEBUG |
| 1952 |
// Assert extended slots don't need to be copied. |
| 1953 |
if (fun->isExtended()) { |
| 1954 |
for (unsigned i = 0; i < FunctionExtended::NUM_EXTENDED_SLOTS; i++) { |
| 1955 |
MOZ_ASSERT(fun->getExtendedSlot(i).isUndefined()); |
| 1956 |
MOZ_ASSERT(clone->getExtendedSlot(i).isUndefined()); |
| 1957 |
} |
| 1958 |
} |
| 1959 |
#endif |
| 1960 |
|
| 1961 |
return clone; |
| 1962 |
} |
| 1963 |
|
| 1964 |
JSFunction* js::CloneAsmJSModuleFunction(JSContext* cx, HandleFunction fun) { |
| 1965 |
MOZ_ASSERT(fun->isNativeFun()); |
| 1966 |
MOZ_ASSERT(IsAsmJSModule(fun)); |
| 1967 |
MOZ_ASSERT(fun->isExtended()); |
| 1968 |
MOZ_ASSERT(cx->compartment() == fun->compartment()); |
| 1969 |
|
| 1970 |
RootedObject proto(cx, fun->staticPrototype()); |
| 1971 |
JSFunction* clone = NewFunctionClone(cx, fun, proto); |
| 1972 |
if (!clone) { |
| 1973 |
return nullptr; |
| 1974 |
} |
| 1975 |
|
| 1976 |
MOZ_ASSERT(fun->native() == InstantiateAsmJS); |
| 1977 |
MOZ_ASSERT(!fun->hasJitInfo()); |
| 1978 |
clone->initNative(InstantiateAsmJS, nullptr); |
| 1979 |
|
| 1980 |
JSObject* moduleObj = |
| 1981 |
&fun->getExtendedSlot(FunctionExtended::ASMJS_MODULE_SLOT).toObject(); |
| 1982 |
clone->initExtendedSlot(FunctionExtended::ASMJS_MODULE_SLOT, |
| 1983 |
ObjectValue(*moduleObj)); |
| 1984 |
|
| 1985 |
return clone; |
| 1986 |
} |
| 1987 |
|
| 1988 |
static JSAtom* SymbolToFunctionName(JSContext* cx, JS::Symbol* symbol, |
| 1989 |
FunctionPrefixKind prefixKind) { |
| 1990 |
// Step 4.a. |
| 1991 |
JSAtom* desc = symbol->description(); |
| 1992 |
|
| 1993 |
// Step 4.b, no prefix fastpath. |
| 1994 |
if (!desc && prefixKind == FunctionPrefixKind::None) { |
| 1995 |
return cx->names().empty_; |
| 1996 |
} |
| 1997 |
|
| 1998 |
// Step 5 (reordered). |
| 1999 |
StringBuilder sb(cx); |
| 2000 |
if (prefixKind == FunctionPrefixKind::Get) { |
| 2001 |
if (!sb.append("get ")) { |
| 2002 |
return nullptr; |
| 2003 |
} |
| 2004 |
} else if (prefixKind == FunctionPrefixKind::Set) { |
| 2005 |
if (!sb.append("set ")) { |
| 2006 |
return nullptr; |
| 2007 |
} |
| 2008 |
} |
| 2009 |
|
| 2010 |
// Step 4.b. |
| 2011 |
if (desc) { |
| 2012 |
// Note: Private symbols are wedged in, as implementation wise they're |
| 2013 |
// PrivateNameSymbols with a the source level name as a description |
| 2014 |
// i.e. obj.#f desugars to obj.[PrivateNameSymbol("#f")], however |
| 2015 |
// they don't use the symbol naming, but rather property naming. |
| 2016 |
if (symbol->isPrivateName()) { |
| 2017 |
if (!sb.append(desc)) { |
| 2018 |
return nullptr; |
| 2019 |
} |
| 2020 |
} else { |
| 2021 |
// Step 4.c. |
| 2022 |
if (!sb.append('[') || !sb.append(desc) || !sb.append(']')) { |
| 2023 |
return nullptr; |
| 2024 |
} |
| 2025 |
} |
| 2026 |
} |
| 2027 |
return sb.finishAtom(); |
| 2028 |
} |
| 2029 |
|
| 2030 |
/* |
| 2031 |
* Return an atom for use as the name of a builtin method with the given |
| 2032 |
* property id. |
| 2033 |
* |
| 2034 |
* Function names are always strings. If id is the well-known @@iterator |
| 2035 |
* symbol, this returns "[Symbol.iterator]". If a prefix is supplied the final |
| 2036 |
* name is |prefix + " " + name|. |
| 2037 |
* |
| 2038 |
* Implements steps 3-5 of 9.2.11 SetFunctionName in ES2016. |
| 2039 |
*/ |
| 2040 |
JSAtom* js::IdToFunctionName( |
| 2041 |
JSContext* cx, HandleId id, |
| 2042 |
FunctionPrefixKind prefixKind /* = FunctionPrefixKind::None */) { |
| 2043 |
MOZ_ASSERT(id.isString() || id.isSymbol() || id.isInt()); |
| 2044 |
|
| 2045 |
// No prefix fastpath. |
| 2046 |
if (id.isAtom() && prefixKind == FunctionPrefixKind::None) { |
| 2047 |
return id.toAtom(); |
| 2048 |
} |
| 2049 |
|
| 2050 |
// Step 3 (implicit). |
| 2051 |
|
| 2052 |
// Step 4. |
| 2053 |
if (id.isSymbol()) { |
| 2054 |
return SymbolToFunctionName(cx, id.toSymbol(), prefixKind); |
| 2055 |
} |
| 2056 |
|
| 2057 |
// Step 5. |
| 2058 |
RootedValue idv(cx, IdToValue(id)); |
| 2059 |
return NameToFunctionName(cx, idv, prefixKind); |
| 2060 |
} |
| 2061 |
|
| 2062 |
bool js::SetFunctionName(JSContext* cx, HandleFunction fun, HandleValue name, |
| 2063 |
FunctionPrefixKind prefixKind) { |
| 2064 |
MOZ_ASSERT(name.isString() || name.isSymbol() || name.isNumeric()); |
| 2065 |
|
| 2066 |
// `fun` is a newly created function, so it can't already have an inferred |
| 2067 |
// name. |
| 2068 |
MOZ_ASSERT(!fun->hasInferredName()); |
| 2069 |
|
| 2070 |
// Anonymous functions should neither have an own 'name' property nor a |
| 2071 |
// resolved name at this point. |
| 2072 |
MOZ_ASSERT(!fun->containsPure(cx->names().name)); |
| 2073 |
MOZ_ASSERT(!fun->hasResolvedName()); |
| 2074 |
|
| 2075 |
JSAtom* funName = name.isSymbol() |
| 2076 |
? SymbolToFunctionName(cx, name.toSymbol(), prefixKind) |
| 2077 |
: NameToFunctionName(cx, name, prefixKind); |
| 2078 |
if (!funName) { |
| 2079 |
return false; |
| 2080 |
} |
| 2081 |
|
| 2082 |
fun->setInferredName(funName); |
| 2083 |
|
| 2084 |
return true; |
| 2085 |
} |
| 2086 |
|
| 2087 |
JSFunction* js::DefineFunction( |
| 2088 |
JSContext* cx, HandleObject obj, HandleId id, Native native, unsigned nargs, |
| 2089 |
unsigned flags, gc::AllocKind allocKind /* = AllocKind::FUNCTION */) { |
| 2090 |
Rooted<JSAtom*> atom(cx, IdToFunctionName(cx, id)); |
| 2091 |
if (!atom) { |
| 2092 |
return nullptr; |
| 2093 |
} |
| 2094 |
|
| 2095 |
MOZ_ASSERT(native); |
| 2096 |
|
| 2097 |
RootedFunction fun(cx); |
| 2098 |
if (flags & JSFUN_CONSTRUCTOR) { |
| 2099 |
fun = NewNativeConstructor(cx, native, nargs, atom, allocKind); |
| 2100 |
} else { |
| 2101 |
fun = NewNativeFunction(cx, native, nargs, atom, allocKind); |
| 2102 |
} |
| 2103 |
|
| 2104 |
if (!fun) { |
| 2105 |
return nullptr; |
| 2106 |
} |
| 2107 |
|
| 2108 |
RootedValue funVal(cx, ObjectValue(*fun)); |
| 2109 |
if (!DefineDataProperty(cx, obj, id, funVal, flags & ~JSFUN_FLAGS_MASK)) { |
| 2110 |
return nullptr; |
| 2111 |
} |
| 2112 |
|
| 2113 |
return fun; |
| 2114 |
} |
| 2115 |
|
| 2116 |
void js::ReportIncompatibleMethod(JSContext* cx, const CallArgs& args, |
| 2117 |
const JSClass* clasp) { |
| 2118 |
RootedValue thisv(cx, args.thisv()); |
| 2119 |
|
| 2120 |
#ifdef DEBUG |
| 2121 |
switch (thisv.type()) { |
| 2122 |
case ValueType::Object: |
| 2123 |
MOZ_ASSERT(thisv.toObject().getClass() != clasp || |
| 2124 |
!thisv.toObject().is<NativeObject>() || |
| 2125 |
!thisv.toObject().staticPrototype() || |
| 2126 |
thisv.toObject().staticPrototype()->getClass() != clasp); |
| 2127 |
break; |
| 2128 |
case ValueType::String: |
| 2129 |
MOZ_ASSERT(clasp != &StringObject::class_); |
| 2130 |
break; |
| 2131 |
case ValueType::Double: |
| 2132 |
case ValueType::Int32: |
| 2133 |
MOZ_ASSERT(clasp != &NumberObject::class_); |
| 2134 |
break; |
| 2135 |
case ValueType::Boolean: |
| 2136 |
MOZ_ASSERT(clasp != &BooleanObject::class_); |
| 2137 |
break; |
| 2138 |
case ValueType::Symbol: |
| 2139 |
MOZ_ASSERT(clasp != &SymbolObject::class_); |
| 2140 |
break; |
| 2141 |
case ValueType::BigInt: |
| 2142 |
MOZ_ASSERT(clasp != &BigIntObject::class_); |
| 2143 |
break; |
| 2144 |
case ValueType::Undefined: |
| 2145 |
case ValueType::Null: |
| 2146 |
break; |
| 2147 |
case ValueType::Magic: |
| 2148 |
case ValueType::PrivateGCThing: |
| 2149 |
MOZ_CRASH("unexpected type"); |
| 2150 |
} |
| 2151 |
#endif |
| 2152 |
|
| 2153 |
if (JSFunction* fun = ReportIfNotFunction(cx, args.calleev())) { |
| 2154 |
UniqueChars funNameBytes; |
| 2155 |
if (const char* funName = GetFunctionNameBytes(cx, fun, &funNameBytes)) { |
| 2156 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 2157 |
JSMSG_INCOMPATIBLE_PROTO, clasp->name, funName, |
| 2158 |
InformalValueTypeName(thisv)); |
| 2159 |
} |
| 2160 |
} |
| 2161 |
} |
| 2162 |
|
| 2163 |
void js::ReportIncompatible(JSContext* cx, const CallArgs& args) { |
| 2164 |
if (JSFunction* fun = ReportIfNotFunction(cx, args.calleev())) { |
| 2165 |
UniqueChars funNameBytes; |
| 2166 |
if (const char* funName = GetFunctionNameBytes(cx, fun, &funNameBytes)) { |
| 2167 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 2168 |
JSMSG_INCOMPATIBLE_METHOD, funName, "method", |
| 2169 |
InformalValueTypeName(args.thisv())); |
| 2170 |
} |
| 2171 |
} |
| 2172 |
} |
| 2173 |
|
| 2174 |
namespace JS { |
| 2175 |
namespace detail { |
| 2176 |
|
| 2177 |
JS_PUBLIC_API void CheckIsValidConstructible(const Value& calleev) { |
| 2178 |
MOZ_ASSERT(calleev.toObject().isConstructor()); |
| 2179 |
} |
| 2180 |
|
| 2181 |
} // namespace detail |
| 2182 |
} // namespace JS |