| 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 |
#include "vm/SelfHosting.h" |
| 8 |
|
| 9 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 10 |
# include "builtin/AsyncDisposableStackObject.h" |
| 11 |
# include "builtin/DisposableStackObject.h" |
| 12 |
#endif |
| 13 |
#include "mozilla/BinarySearch.h" |
| 14 |
#include "mozilla/Casting.h" |
| 15 |
#include "mozilla/Maybe.h" |
| 16 |
#include "mozilla/ScopeExit.h" // mozilla::MakeScopeExit |
| 17 |
#include "mozilla/Utf8.h" // mozilla::Utf8Unit |
| 18 |
|
| 19 |
#include <algorithm> |
| 20 |
#include <iterator> |
| 21 |
|
| 22 |
#include "jsfriendapi.h" |
| 23 |
#include "jsmath.h" |
| 24 |
#include "jsnum.h" |
| 25 |
#include "selfhosted.out.h" |
| 26 |
|
| 27 |
#include "builtin/Array.h" |
| 28 |
#include "builtin/BigInt.h" |
| 29 |
#ifdef JS_HAS_INTL_API |
| 30 |
# include "builtin/intl/Collator.h" |
| 31 |
# include "builtin/intl/DateTimeFormat.h" |
| 32 |
# include "builtin/intl/DisplayNames.h" |
| 33 |
# include "builtin/intl/DurationFormat.h" |
| 34 |
# include "builtin/intl/IntlObject.h" |
| 35 |
# include "builtin/intl/ListFormat.h" |
| 36 |
# include "builtin/intl/Locale.h" |
| 37 |
# include "builtin/intl/NumberFormat.h" |
| 38 |
# include "builtin/intl/PluralRules.h" |
| 39 |
# include "builtin/intl/RelativeTimeFormat.h" |
| 40 |
# include "builtin/intl/Segmenter.h" |
| 41 |
#endif |
| 42 |
#include "builtin/MapObject.h" |
| 43 |
#include "builtin/Object.h" |
| 44 |
#include "builtin/Promise.h" |
| 45 |
#include "builtin/Reflect.h" |
| 46 |
#include "builtin/RegExp.h" |
| 47 |
#include "builtin/SelfHostingDefines.h" |
| 48 |
#include "builtin/String.h" |
| 49 |
#ifdef JS_HAS_INTL_API |
| 50 |
# include "builtin/temporal/Duration.h" |
| 51 |
#endif |
| 52 |
#include "builtin/WeakMapObject.h" |
| 53 |
#include "frontend/BytecodeCompiler.h" // CompileGlobalScriptToStencil |
| 54 |
#include "frontend/CompilationStencil.h" // js::frontend::CompilationStencil |
| 55 |
#include "frontend/FrontendContext.h" // AutoReportFrontendContext |
| 56 |
#include "frontend/StencilXdr.h" // js::EncodeStencil, js::DecodeStencil |
| 57 |
#include "jit/AtomicOperations.h" |
| 58 |
#include "jit/BaselineJIT.h" |
| 59 |
#include "jit/InlinableNatives.h" |
| 60 |
#include "jit/TrampolineNatives.h" |
| 61 |
#include "js/CompilationAndEvaluation.h" |
| 62 |
#include "js/Conversions.h" |
| 63 |
#include "js/ErrorReport.h" // JS::PrintError |
| 64 |
#include "js/experimental/JSStencil.h" |
| 65 |
#include "js/experimental/TypedData.h" // JS_GetArrayBufferViewType |
| 66 |
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_* |
| 67 |
#include "js/HashTable.h" |
| 68 |
#include "js/Printer.h" |
| 69 |
#include "js/PropertySpec.h" |
| 70 |
#include "js/ScalarType.h" // js::Scalar::Type |
| 71 |
#include "js/SourceText.h" // JS::SourceText |
| 72 |
#include "js/TracingAPI.h" |
| 73 |
#include "js/Transcoding.h" |
| 74 |
#include "js/Warnings.h" // JS::{,Set}WarningReporter |
| 75 |
#include "js/Wrapper.h" |
| 76 |
#include "vm/ArgumentsObject.h" |
| 77 |
#include "vm/AsyncFunction.h" |
| 78 |
#include "vm/AsyncIteration.h" |
| 79 |
#include "vm/BigIntType.h" |
| 80 |
#include "vm/Compression.h" |
| 81 |
#include "vm/DateObject.h" |
| 82 |
#include "vm/ErrorReporting.h" // js::MaybePrintAndClearPendingException |
| 83 |
#include "vm/Float16.h" |
| 84 |
#include "vm/FrameIter.h" // js::ScriptFrameIter |
| 85 |
#include "vm/GeneratorObject.h" |
| 86 |
#include "vm/Interpreter.h" |
| 87 |
#include "vm/Iteration.h" |
| 88 |
#include "vm/JSAtomUtils.h" // Atomize |
| 89 |
#include "vm/JSContext.h" |
| 90 |
#include "vm/JSFunction.h" |
| 91 |
#include "vm/JSObject.h" |
| 92 |
#include "vm/Logging.h" |
| 93 |
#include "vm/PlainObject.h" // js::PlainObject |
| 94 |
#include "vm/Realm.h" |
| 95 |
#include "vm/RegExpObject.h" |
| 96 |
#include "vm/StringType.h" |
| 97 |
#include "vm/ToSource.h" // js::ValueToSource |
| 98 |
#include "vm/TypedArrayObject.h" |
| 99 |
#include "vm/Uint8Clamped.h" |
| 100 |
#include "vm/WrapperObject.h" |
| 101 |
|
| 102 |
#include "vm/Compartment-inl.h" |
| 103 |
#include "vm/JSAtomUtils-inl.h" // PrimitiveValueToId |
| 104 |
#include "vm/JSFunction-inl.h" |
| 105 |
#include "vm/JSObject-inl.h" |
| 106 |
#include "vm/NativeObject-inl.h" |
| 107 |
#include "vm/TypedArrayObject-inl.h" |
| 108 |
|
| 109 |
using namespace js; |
| 110 |
using namespace js::selfhosted; |
| 111 |
|
| 112 |
using JS::CompileOptions; |
| 113 |
|
| 114 |
static bool intrinsic_ToObject(JSContext* cx, unsigned argc, Value* vp) { |
| 115 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 116 |
JSObject* obj = ToObject(cx, args[0]); |
| 117 |
if (!obj) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
args.rval().setObject(*obj); |
| 121 |
return true; |
| 122 |
} |
| 123 |
|
| 124 |
static bool intrinsic_IsObject(JSContext* cx, unsigned argc, Value* vp) { |
| 125 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 126 |
Value val = args[0]; |
| 127 |
bool isObject = val.isObject(); |
| 128 |
args.rval().setBoolean(isObject); |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
static bool intrinsic_IsArray(JSContext* cx, unsigned argc, Value* vp) { |
| 133 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 134 |
MOZ_ASSERT(args.length() == 1); |
| 135 |
RootedValue val(cx, args[0]); |
| 136 |
if (val.isObject()) { |
| 137 |
RootedObject obj(cx, &val.toObject()); |
| 138 |
bool isArray = false; |
| 139 |
if (!IsArray(cx, obj, &isArray)) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
args.rval().setBoolean(isArray); |
| 143 |
} else { |
| 144 |
args.rval().setBoolean(false); |
| 145 |
} |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
static bool intrinsic_IsCrossRealmArrayConstructor(JSContext* cx, unsigned argc, |
| 150 |
Value* vp) { |
| 151 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 152 |
MOZ_ASSERT(args.length() == 1); |
| 153 |
MOZ_ASSERT(args[0].isObject()); |
| 154 |
|
| 155 |
bool result = false; |
| 156 |
if (!IsCrossRealmArrayConstructor(cx, &args[0].toObject(), &result)) { |
| 157 |
return false; |
| 158 |
} |
| 159 |
args.rval().setBoolean(result); |
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
static bool intrinsic_ToLength(JSContext* cx, unsigned argc, Value* vp) { |
| 164 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 165 |
MOZ_ASSERT(args.length() == 1); |
| 166 |
|
| 167 |
// Inline fast path for the common case. |
| 168 |
if (args[0].isInt32()) { |
| 169 |
int32_t i = args[0].toInt32(); |
| 170 |
args.rval().setInt32(i < 0 ? 0 : i); |
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
uint64_t length = 0; |
| 175 |
if (!ToLength(cx, args[0], &length)) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
args.rval().setNumber(double(length)); |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
static bool intrinsic_ToInteger(JSContext* cx, unsigned argc, Value* vp) { |
| 184 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 185 |
double result; |
| 186 |
if (!ToInteger(cx, args[0], &result)) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
args.rval().setNumber(result); |
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
static bool intrinsic_ToSource(JSContext* cx, unsigned argc, Value* vp) { |
| 194 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 195 |
JSString* str = ValueToSource(cx, args[0]); |
| 196 |
if (!str) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
args.rval().setString(str); |
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
static bool intrinsic_ToPropertyKey(JSContext* cx, unsigned argc, Value* vp) { |
| 204 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 205 |
RootedId id(cx); |
| 206 |
if (!ToPropertyKey(cx, args[0], &id)) { |
| 207 |
return false; |
| 208 |
} |
| 209 |
|
| 210 |
args.rval().set(IdToValue(id)); |
| 211 |
return true; |
| 212 |
} |
| 213 |
|
| 214 |
static bool intrinsic_IsCallable(JSContext* cx, unsigned argc, Value* vp) { |
| 215 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 216 |
args.rval().setBoolean(IsCallable(args[0])); |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
static bool intrinsic_IsConstructor(JSContext* cx, unsigned argc, Value* vp) { |
| 221 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 222 |
MOZ_ASSERT(args.length() == 1); |
| 223 |
args.rval().setBoolean(IsConstructor(args[0])); |
| 224 |
return true; |
| 225 |
} |
| 226 |
|
| 227 |
template <typename T> |
| 228 |
static bool intrinsic_IsInstanceOfBuiltin(JSContext* cx, unsigned argc, |
| 229 |
Value* vp) { |
| 230 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 231 |
MOZ_ASSERT(args.length() == 1); |
| 232 |
MOZ_ASSERT(args[0].isObject()); |
| 233 |
|
| 234 |
args.rval().setBoolean(args[0].toObject().is<T>()); |
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
template <typename T> |
| 239 |
static bool intrinsic_GuardToBuiltin(JSContext* cx, unsigned argc, Value* vp) { |
| 240 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 241 |
MOZ_ASSERT(args.length() == 1); |
| 242 |
MOZ_ASSERT(args[0].isObject()); |
| 243 |
|
| 244 |
if (args[0].toObject().is<T>()) { |
| 245 |
args.rval().setObject(args[0].toObject()); |
| 246 |
return true; |
| 247 |
} |
| 248 |
args.rval().setNull(); |
| 249 |
return true; |
| 250 |
} |
| 251 |
|
| 252 |
template <typename T> |
| 253 |
static bool intrinsic_IsWrappedInstanceOfBuiltin(JSContext* cx, unsigned argc, |
| 254 |
Value* vp) { |
| 255 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 256 |
MOZ_ASSERT(args.length() == 1); |
| 257 |
MOZ_ASSERT(args[0].isObject()); |
| 258 |
|
| 259 |
JSObject* obj = &args[0].toObject(); |
| 260 |
if (!obj->is<WrapperObject>()) { |
| 261 |
args.rval().setBoolean(false); |
| 262 |
return true; |
| 263 |
} |
| 264 |
|
| 265 |
JSObject* unwrapped = CheckedUnwrapDynamic(obj, cx); |
| 266 |
if (!unwrapped) { |
| 267 |
ReportAccessDenied(cx); |
| 268 |
return false; |
| 269 |
} |
| 270 |
|
| 271 |
args.rval().setBoolean(unwrapped->is<T>()); |
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
template <typename T> |
| 276 |
static bool intrinsic_IsPossiblyWrappedInstanceOfBuiltin(JSContext* cx, |
| 277 |
unsigned argc, |
| 278 |
Value* vp) { |
| 279 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 280 |
MOZ_ASSERT(args.length() == 1); |
| 281 |
MOZ_ASSERT(args[0].isObject()); |
| 282 |
|
| 283 |
JSObject* obj = CheckedUnwrapDynamic(&args[0].toObject(), cx); |
| 284 |
if (!obj) { |
| 285 |
ReportAccessDenied(cx); |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
args.rval().setBoolean(obj->is<T>()); |
| 290 |
return true; |
| 291 |
} |
| 292 |
|
| 293 |
static bool intrinsic_SubstringKernel(JSContext* cx, unsigned argc, Value* vp) { |
| 294 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 295 |
MOZ_ASSERT(args[0].isString()); |
| 296 |
MOZ_RELEASE_ASSERT(args[1].isInt32()); |
| 297 |
MOZ_RELEASE_ASSERT(args[2].isInt32()); |
| 298 |
|
| 299 |
RootedString str(cx, args[0].toString()); |
| 300 |
int32_t begin = args[1].toInt32(); |
| 301 |
int32_t length = args[2].toInt32(); |
| 302 |
|
| 303 |
JSString* substr = SubstringKernel(cx, str, begin, length); |
| 304 |
if (!substr) { |
| 305 |
return false; |
| 306 |
} |
| 307 |
|
| 308 |
args.rval().setString(substr); |
| 309 |
return true; |
| 310 |
} |
| 311 |
|
| 312 |
static bool intrinsic_CanOptimizeStringProtoSymbolLookup(JSContext* cx, |
| 313 |
unsigned argc, |
| 314 |
Value* vp) { |
| 315 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 316 |
MOZ_ASSERT(args.length() == 0); |
| 317 |
|
| 318 |
bool optimizable = |
| 319 |
cx->realm()->realmFuses.optimizeStringPrototypeSymbolsFuse.intact(); |
| 320 |
args.rval().setBoolean(optimizable); |
| 321 |
return true; |
| 322 |
} |
| 323 |
|
| 324 |
static void ThrowErrorWithType(JSContext* cx, JSExnType type, |
| 325 |
const CallArgs& args) { |
| 326 |
MOZ_RELEASE_ASSERT(args[0].isInt32()); |
| 327 |
uint32_t errorNumber = args[0].toInt32(); |
| 328 |
|
| 329 |
#ifdef DEBUG |
| 330 |
const JSErrorFormatString* efs = GetErrorMessage(nullptr, errorNumber); |
| 331 |
MOZ_ASSERT(efs->argCount == args.length() - 1); |
| 332 |
MOZ_ASSERT(efs->exnType == type, |
| 333 |
"error-throwing intrinsic and error number are inconsistent"); |
| 334 |
#endif |
| 335 |
|
| 336 |
UniqueChars errorArgs[3]; |
| 337 |
for (unsigned i = 1; i < 4 && i < args.length(); i++) { |
| 338 |
HandleValue val = args[i]; |
| 339 |
if (val.isInt32() || val.isString()) { |
| 340 |
JSString* str = ToString<CanGC>(cx, val); |
| 341 |
if (!str) { |
| 342 |
return; |
| 343 |
} |
| 344 |
errorArgs[i - 1] = QuoteString(cx, str); |
| 345 |
} else { |
| 346 |
errorArgs[i - 1] = |
| 347 |
DecompileValueGenerator(cx, JSDVG_SEARCH_STACK, val, nullptr); |
| 348 |
} |
| 349 |
if (!errorArgs[i - 1]) { |
| 350 |
return; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, errorNumber, |
| 355 |
errorArgs[0].get(), errorArgs[1].get(), |
| 356 |
errorArgs[2].get()); |
| 357 |
} |
| 358 |
|
| 359 |
static bool intrinsic_ThrowRangeError(JSContext* cx, unsigned argc, Value* vp) { |
| 360 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 361 |
MOZ_ASSERT(args.length() >= 1); |
| 362 |
|
| 363 |
ThrowErrorWithType(cx, JSEXN_RANGEERR, args); |
| 364 |
return false; |
| 365 |
} |
| 366 |
|
| 367 |
static bool intrinsic_ThrowTypeError(JSContext* cx, unsigned argc, Value* vp) { |
| 368 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 369 |
MOZ_ASSERT(args.length() >= 1); |
| 370 |
|
| 371 |
ThrowErrorWithType(cx, JSEXN_TYPEERR, args); |
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
static bool intrinsic_ThrowAggregateError(JSContext* cx, unsigned argc, |
| 376 |
Value* vp) { |
| 377 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 378 |
MOZ_ASSERT(args.length() >= 1); |
| 379 |
|
| 380 |
ThrowErrorWithType(cx, JSEXN_AGGREGATEERR, args); |
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
static bool intrinsic_ThrowInternalError(JSContext* cx, unsigned argc, |
| 385 |
Value* vp) { |
| 386 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 387 |
MOZ_ASSERT(args.length() >= 1); |
| 388 |
|
| 389 |
ThrowErrorWithType(cx, JSEXN_INTERNALERR, args); |
| 390 |
return false; |
| 391 |
} |
| 392 |
|
| 393 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 394 |
static bool intrinsic_CreateSuppressedError(JSContext* cx, unsigned argc, |
| 395 |
Value* vp) { |
| 396 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 397 |
MOZ_ASSERT(args.length() == 2); |
| 398 |
|
| 399 |
JS::Rooted<JS::Value> error(cx, args[0]); |
| 400 |
JS::Rooted<JS::Value> suppressed(cx, args[1]); |
| 401 |
|
| 402 |
ErrorObject* suppressedError = CreateSuppressedError(cx, error, suppressed); |
| 403 |
if (!suppressedError) { |
| 404 |
return false; |
| 405 |
} |
| 406 |
args.rval().setObject(*suppressedError); |
| 407 |
return true; |
| 408 |
} |
| 409 |
#endif |
| 410 |
|
| 411 |
/** |
| 412 |
* Handles an assertion failure in self-hosted code just like an assertion |
| 413 |
* failure in C++ code. Information about the failure can be provided in |
| 414 |
* args[0]. |
| 415 |
*/ |
| 416 |
static bool intrinsic_AssertionFailed(JSContext* cx, unsigned argc, Value* vp) { |
| 417 |
#ifdef DEBUG |
| 418 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 419 |
if (args.length() > 0) { |
| 420 |
// try to dump the informative string |
| 421 |
JSString* str = ToString<CanGC>(cx, args[0]); |
| 422 |
if (str) { |
| 423 |
js::Fprinter out(stderr); |
| 424 |
out.put("Self-hosted JavaScript assertion info: "); |
| 425 |
str->dumpCharsNoQuote(out); |
| 426 |
out.putChar('\n'); |
| 427 |
} |
| 428 |
} |
| 429 |
#endif |
| 430 |
MOZ_ASSERT(false); |
| 431 |
return false; |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Dumps a message to stderr, after stringifying it. Doesn't append a newline. |
| 436 |
*/ |
| 437 |
static bool intrinsic_DumpMessage(JSContext* cx, unsigned argc, Value* vp) { |
| 438 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 439 |
#ifdef DEBUG |
| 440 |
if (args.length() > 0) { |
| 441 |
// try to dump the informative string |
| 442 |
js::Fprinter out(stderr); |
| 443 |
JSString* str = ToString<CanGC>(cx, args[0]); |
| 444 |
if (str) { |
| 445 |
str->dumpCharsNoQuote(out); |
| 446 |
out.putChar('\n'); |
| 447 |
} else { |
| 448 |
cx->recoverFromOutOfMemory(); |
| 449 |
} |
| 450 |
} |
| 451 |
#endif |
| 452 |
args.rval().setUndefined(); |
| 453 |
return true; |
| 454 |
} |
| 455 |
|
| 456 |
/* |
| 457 |
* Used to decompile values in the nearest non-builtin stack frame, falling |
| 458 |
* back to decompiling in the current frame. Helpful for printing higher-order |
| 459 |
* function arguments. |
| 460 |
* |
| 461 |
* The user must supply the argument number of the value in question; it |
| 462 |
* _cannot_ be automatically determined. |
| 463 |
*/ |
| 464 |
static bool intrinsic_DecompileArg(JSContext* cx, unsigned argc, Value* vp) { |
| 465 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 466 |
MOZ_ASSERT(args.length() == 2); |
| 467 |
MOZ_RELEASE_ASSERT(args[0].isInt32()); |
| 468 |
|
| 469 |
HandleValue value = args[1]; |
| 470 |
JSString* str = DecompileArgument(cx, args[0].toInt32(), value); |
| 471 |
if (!str) { |
| 472 |
return false; |
| 473 |
} |
| 474 |
args.rval().setString(str); |
| 475 |
return true; |
| 476 |
} |
| 477 |
|
| 478 |
static bool intrinsic_DefineDataProperty(JSContext* cx, unsigned argc, |
| 479 |
Value* vp) { |
| 480 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 481 |
|
| 482 |
// When DefineDataProperty is called with 3 arguments, it's compiled to |
| 483 |
// JSOp::InitElem in the bytecode emitter so we shouldn't get here. |
| 484 |
MOZ_ASSERT(args.length() == 4); |
| 485 |
MOZ_ASSERT(args[0].isObject()); |
| 486 |
MOZ_RELEASE_ASSERT(args[3].isInt32()); |
| 487 |
|
| 488 |
RootedObject obj(cx, &args[0].toObject()); |
| 489 |
RootedId id(cx); |
| 490 |
if (!ToPropertyKey(cx, args[1], &id)) { |
| 491 |
return false; |
| 492 |
} |
| 493 |
RootedValue value(cx, args[2]); |
| 494 |
|
| 495 |
JS::PropertyAttributes attrs; |
| 496 |
unsigned attributes = args[3].toInt32(); |
| 497 |
|
| 498 |
MOZ_ASSERT(bool(attributes & ATTR_ENUMERABLE) != |
| 499 |
bool(attributes & ATTR_NONENUMERABLE), |
| 500 |
"DefineDataProperty must receive either ATTR_ENUMERABLE xor " |
| 501 |
"ATTR_NONENUMERABLE"); |
| 502 |
if (attributes & ATTR_ENUMERABLE) { |
| 503 |
attrs += JS::PropertyAttribute::Enumerable; |
| 504 |
} |
| 505 |
|
| 506 |
MOZ_ASSERT(bool(attributes & ATTR_CONFIGURABLE) != |
| 507 |
bool(attributes & ATTR_NONCONFIGURABLE), |
| 508 |
"DefineDataProperty must receive either ATTR_CONFIGURABLE xor " |
| 509 |
"ATTR_NONCONFIGURABLE"); |
| 510 |
if (attributes & ATTR_CONFIGURABLE) { |
| 511 |
attrs += JS::PropertyAttribute::Configurable; |
| 512 |
} |
| 513 |
|
| 514 |
MOZ_ASSERT( |
| 515 |
bool(attributes & ATTR_WRITABLE) != bool(attributes & ATTR_NONWRITABLE), |
| 516 |
"DefineDataProperty must receive either ATTR_WRITABLE xor " |
| 517 |
"ATTR_NONWRITABLE"); |
| 518 |
if (attributes & ATTR_WRITABLE) { |
| 519 |
attrs += JS::PropertyAttribute::Writable; |
| 520 |
} |
| 521 |
|
| 522 |
Rooted<PropertyDescriptor> desc(cx, PropertyDescriptor::Data(value, attrs)); |
| 523 |
if (!DefineProperty(cx, obj, id, desc)) { |
| 524 |
return false; |
| 525 |
} |
| 526 |
|
| 527 |
args.rval().setUndefined(); |
| 528 |
return true; |
| 529 |
} |
| 530 |
|
| 531 |
static bool intrinsic_DefineProperty(JSContext* cx, unsigned argc, Value* vp) { |
| 532 |
// _DefineProperty(object, propertyKey, attributes, |
| 533 |
// valueOrGetter, setter, strict) |
| 534 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 535 |
MOZ_ASSERT(args.length() == 6); |
| 536 |
MOZ_ASSERT(args[0].isObject()); |
| 537 |
MOZ_ASSERT(args[1].isString() || args[1].isNumber() || args[1].isSymbol()); |
| 538 |
MOZ_RELEASE_ASSERT(args[2].isInt32()); |
| 539 |
MOZ_ASSERT(args[5].isBoolean()); |
| 540 |
|
| 541 |
RootedObject obj(cx, &args[0].toObject()); |
| 542 |
RootedId id(cx); |
| 543 |
if (!PrimitiveValueToId<CanGC>(cx, args[1], &id)) { |
| 544 |
return false; |
| 545 |
} |
| 546 |
|
| 547 |
Rooted<PropertyDescriptor> desc(cx, PropertyDescriptor::Empty()); |
| 548 |
|
| 549 |
unsigned attributes = args[2].toInt32(); |
| 550 |
if (attributes & (ATTR_ENUMERABLE | ATTR_NONENUMERABLE)) { |
| 551 |
desc.setEnumerable(attributes & ATTR_ENUMERABLE); |
| 552 |
} |
| 553 |
|
| 554 |
if (attributes & (ATTR_CONFIGURABLE | ATTR_NONCONFIGURABLE)) { |
| 555 |
desc.setConfigurable(attributes & ATTR_CONFIGURABLE); |
| 556 |
} |
| 557 |
|
| 558 |
if (attributes & (ATTR_WRITABLE | ATTR_NONWRITABLE)) { |
| 559 |
desc.setWritable(attributes & ATTR_WRITABLE); |
| 560 |
} |
| 561 |
|
| 562 |
// When args[4] is |null|, the data descriptor has a value component. |
| 563 |
if ((attributes & DATA_DESCRIPTOR_KIND) && args[4].isNull()) { |
| 564 |
desc.setValue(args[3]); |
| 565 |
} |
| 566 |
|
| 567 |
if (attributes & ACCESSOR_DESCRIPTOR_KIND) { |
| 568 |
Value getter = args[3]; |
| 569 |
if (getter.isObject()) { |
| 570 |
desc.setGetter(&getter.toObject()); |
| 571 |
} else if (getter.isUndefined()) { |
| 572 |
desc.setGetter(nullptr); |
| 573 |
} else { |
| 574 |
MOZ_ASSERT(getter.isNull()); |
| 575 |
} |
| 576 |
|
| 577 |
Value setter = args[4]; |
| 578 |
if (setter.isObject()) { |
| 579 |
desc.setSetter(&setter.toObject()); |
| 580 |
} else if (setter.isUndefined()) { |
| 581 |
desc.setSetter(nullptr); |
| 582 |
} else { |
| 583 |
MOZ_ASSERT(setter.isNull()); |
| 584 |
} |
| 585 |
} |
| 586 |
|
| 587 |
desc.assertValid(); |
| 588 |
|
| 589 |
ObjectOpResult result; |
| 590 |
if (!DefineProperty(cx, obj, id, desc, result)) { |
| 591 |
return false; |
| 592 |
} |
| 593 |
|
| 594 |
bool strict = args[5].toBoolean(); |
| 595 |
if (strict && !result.ok()) { |
| 596 |
// We need to tell our caller Object.defineProperty, |
| 597 |
// that this operation failed, without actually throwing |
| 598 |
// for web-compatibility reasons. |
| 599 |
if (result.failureCode() == JSMSG_CANT_DEFINE_WINDOW_NC) { |
| 600 |
args.rval().setBoolean(false); |
| 601 |
return true; |
| 602 |
} |
| 603 |
|
| 604 |
return result.reportError(cx, obj, id); |
| 605 |
} |
| 606 |
|
| 607 |
args.rval().setBoolean(result.ok()); |
| 608 |
return true; |
| 609 |
} |
| 610 |
|
| 611 |
static bool intrinsic_UnsafeSetReservedSlot(JSContext* cx, unsigned argc, |
| 612 |
Value* vp) { |
| 613 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 614 |
MOZ_ASSERT(args.length() == 3); |
| 615 |
MOZ_ASSERT(args[0].isObject()); |
| 616 |
MOZ_RELEASE_ASSERT(args[1].isInt32()); |
| 617 |
MOZ_ASSERT(args[1].toInt32() >= 0); |
| 618 |
|
| 619 |
uint32_t slot = uint32_t(args[1].toInt32()); |
| 620 |
args[0].toObject().as<NativeObject>().setReservedSlot(slot, args[2]); |
| 621 |
args.rval().setUndefined(); |
| 622 |
return true; |
| 623 |
} |
| 624 |
|
| 625 |
static bool intrinsic_UnsafeGetReservedSlot(JSContext* cx, unsigned argc, |
| 626 |
Value* vp) { |
| 627 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 628 |
MOZ_ASSERT(args.length() == 2); |
| 629 |
MOZ_ASSERT(args[0].isObject()); |
| 630 |
MOZ_RELEASE_ASSERT(args[1].isInt32()); |
| 631 |
MOZ_ASSERT(args[1].toInt32() >= 0); |
| 632 |
|
| 633 |
uint32_t slot = uint32_t(args[1].toInt32()); |
| 634 |
args.rval().set(args[0].toObject().as<NativeObject>().getReservedSlot(slot)); |
| 635 |
return true; |
| 636 |
} |
| 637 |
|
| 638 |
static bool intrinsic_UnsafeGetObjectFromReservedSlot(JSContext* cx, |
| 639 |
unsigned argc, |
| 640 |
Value* vp) { |
| 641 |
if (!intrinsic_UnsafeGetReservedSlot(cx, argc, vp)) { |
| 642 |
return false; |
| 643 |
} |
| 644 |
MOZ_ASSERT(vp->isObject()); |
| 645 |
return true; |
| 646 |
} |
| 647 |
|
| 648 |
static bool intrinsic_UnsafeGetInt32FromReservedSlot(JSContext* cx, |
| 649 |
unsigned argc, Value* vp) { |
| 650 |
if (!intrinsic_UnsafeGetReservedSlot(cx, argc, vp)) { |
| 651 |
return false; |
| 652 |
} |
| 653 |
MOZ_ASSERT(vp->isInt32()); |
| 654 |
return true; |
| 655 |
} |
| 656 |
|
| 657 |
static bool intrinsic_UnsafeGetStringFromReservedSlot(JSContext* cx, |
| 658 |
unsigned argc, |
| 659 |
Value* vp) { |
| 660 |
if (!intrinsic_UnsafeGetReservedSlot(cx, argc, vp)) { |
| 661 |
return false; |
| 662 |
} |
| 663 |
MOZ_ASSERT(vp->isString()); |
| 664 |
return true; |
| 665 |
} |
| 666 |
|
| 667 |
static bool intrinsic_ThisTimeValue(JSContext* cx, unsigned argc, Value* vp) { |
| 668 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 669 |
MOZ_ASSERT(args.length() == 1); |
| 670 |
MOZ_ASSERT(args[0].isInt32()); |
| 671 |
|
| 672 |
const char* name = nullptr; |
| 673 |
|
| 674 |
int32_t method = args[0].toInt32(); |
| 675 |
if (method == DATE_METHOD_LOCALE_TIME_STRING) { |
| 676 |
name = "toLocaleTimeString"; |
| 677 |
} else if (method == DATE_METHOD_LOCALE_DATE_STRING) { |
| 678 |
name = "toLocaleDateString"; |
| 679 |
} else { |
| 680 |
MOZ_ASSERT(method == DATE_METHOD_LOCALE_STRING); |
| 681 |
name = "toLocaleString"; |
| 682 |
} |
| 683 |
|
| 684 |
auto* unwrapped = UnwrapAndTypeCheckThis<DateObject>(cx, args, name); |
| 685 |
if (!unwrapped) { |
| 686 |
return false; |
| 687 |
} |
| 688 |
|
| 689 |
args.rval().set(unwrapped->UTCTime()); |
| 690 |
return true; |
| 691 |
} |
| 692 |
|
| 693 |
static bool intrinsic_IsPackedArray(JSContext* cx, unsigned argc, Value* vp) { |
| 694 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 695 |
MOZ_ASSERT(args.length() == 1); |
| 696 |
MOZ_ASSERT(args[0].isObject()); |
| 697 |
args.rval().setBoolean(IsPackedArray(&args[0].toObject())); |
| 698 |
return true; |
| 699 |
} |
| 700 |
|
| 701 |
bool js::intrinsic_NewArrayIterator(JSContext* cx, unsigned argc, Value* vp) { |
| 702 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 703 |
MOZ_ASSERT(args.length() == 0); |
| 704 |
|
| 705 |
JSObject* obj = NewArrayIterator(cx); |
| 706 |
if (!obj) { |
| 707 |
return false; |
| 708 |
} |
| 709 |
|
| 710 |
args.rval().setObject(*obj); |
| 711 |
return true; |
| 712 |
} |
| 713 |
|
| 714 |
static bool intrinsic_ArrayIteratorPrototypeOptimizable(JSContext* cx, |
| 715 |
unsigned argc, |
| 716 |
Value* vp) { |
| 717 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 718 |
MOZ_ASSERT(args.length() == 0); |
| 719 |
|
| 720 |
bool optimized = HasOptimizableArrayIteratorPrototype(cx); |
| 721 |
args.rval().setBoolean(optimized); |
| 722 |
return true; |
| 723 |
} |
| 724 |
|
| 725 |
static bool intrinsic_GetNextMapEntryForIterator(JSContext* cx, unsigned argc, |
| 726 |
Value* vp) { |
| 727 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 728 |
MOZ_ASSERT(args.length() == 2); |
| 729 |
MOZ_ASSERT(args[0].toObject().is<MapIteratorObject>()); |
| 730 |
MOZ_ASSERT(args[1].isObject()); |
| 731 |
|
| 732 |
MapIteratorObject* mapIterator = &args[0].toObject().as<MapIteratorObject>(); |
| 733 |
ArrayObject* result = &args[1].toObject().as<ArrayObject>(); |
| 734 |
|
| 735 |
args.rval().setBoolean(MapIteratorObject::next(mapIterator, result)); |
| 736 |
return true; |
| 737 |
} |
| 738 |
|
| 739 |
static bool intrinsic_CreateMapIterationResultPair(JSContext* cx, unsigned argc, |
| 740 |
Value* vp) { |
| 741 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 742 |
MOZ_ASSERT(args.length() == 0); |
| 743 |
|
| 744 |
JSObject* result = MapIteratorObject::createResultPair(cx); |
| 745 |
if (!result) { |
| 746 |
return false; |
| 747 |
} |
| 748 |
|
| 749 |
args.rval().setObject(*result); |
| 750 |
return true; |
| 751 |
} |
| 752 |
|
| 753 |
static bool intrinsic_GetNextSetEntryForIterator(JSContext* cx, unsigned argc, |
| 754 |
Value* vp) { |
| 755 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 756 |
MOZ_ASSERT(args.length() == 2); |
| 757 |
MOZ_ASSERT(args[0].toObject().is<SetIteratorObject>()); |
| 758 |
MOZ_ASSERT(args[1].isObject()); |
| 759 |
|
| 760 |
SetIteratorObject* setIterator = &args[0].toObject().as<SetIteratorObject>(); |
| 761 |
ArrayObject* result = &args[1].toObject().as<ArrayObject>(); |
| 762 |
|
| 763 |
args.rval().setBoolean(SetIteratorObject::next(setIterator, result)); |
| 764 |
return true; |
| 765 |
} |
| 766 |
|
| 767 |
static bool intrinsic_CreateSetIterationResult(JSContext* cx, unsigned argc, |
| 768 |
Value* vp) { |
| 769 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 770 |
MOZ_ASSERT(args.length() == 0); |
| 771 |
|
| 772 |
JSObject* result = SetIteratorObject::createResult(cx); |
| 773 |
if (!result) { |
| 774 |
return false; |
| 775 |
} |
| 776 |
|
| 777 |
args.rval().setObject(*result); |
| 778 |
return true; |
| 779 |
} |
| 780 |
|
| 781 |
bool js::intrinsic_NewStringIterator(JSContext* cx, unsigned argc, Value* vp) { |
| 782 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 783 |
MOZ_ASSERT(args.length() == 0); |
| 784 |
|
| 785 |
JSObject* obj = NewStringIterator(cx); |
| 786 |
if (!obj) { |
| 787 |
return false; |
| 788 |
} |
| 789 |
|
| 790 |
args.rval().setObject(*obj); |
| 791 |
return true; |
| 792 |
} |
| 793 |
|
| 794 |
bool js::intrinsic_NewRegExpStringIterator(JSContext* cx, unsigned argc, |
| 795 |
Value* vp) { |
| 796 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 797 |
MOZ_ASSERT(args.length() == 0); |
| 798 |
|
| 799 |
JSObject* obj = NewRegExpStringIterator(cx); |
| 800 |
if (!obj) { |
| 801 |
return false; |
| 802 |
} |
| 803 |
|
| 804 |
args.rval().setObject(*obj); |
| 805 |
return true; |
| 806 |
} |
| 807 |
|
| 808 |
js::PropertyName* js::GetClonedSelfHostedFunctionName(const JSFunction* fun) { |
| 809 |
if (!fun->isExtended()) { |
| 810 |
return nullptr; |
| 811 |
} |
| 812 |
Value name = fun->getExtendedSlot(LAZY_FUNCTION_NAME_SLOT); |
| 813 |
if (!name.isString()) { |
| 814 |
return nullptr; |
| 815 |
} |
| 816 |
return name.toString()->asAtom().asPropertyName(); |
| 817 |
} |
| 818 |
|
| 819 |
bool js::IsExtendedUnclonedSelfHostedFunctionName(JSAtom* name) { |
| 820 |
if (name->length() < 2) { |
| 821 |
return false; |
| 822 |
} |
| 823 |
return name->latin1OrTwoByteChar(0) == |
| 824 |
ExtendedUnclonedSelfHostedFunctionNamePrefix; |
| 825 |
} |
| 826 |
|
| 827 |
void js::SetClonedSelfHostedFunctionName(JSFunction* fun, |
| 828 |
js::PropertyName* name) { |
| 829 |
fun->setExtendedSlot(LAZY_FUNCTION_NAME_SLOT, StringValue(name)); |
| 830 |
} |
| 831 |
|
| 832 |
static bool intrinsic_GeneratorObjectIsClosed(JSContext* cx, unsigned argc, |
| 833 |
Value* vp) { |
| 834 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 835 |
MOZ_ASSERT(args.length() == 1); |
| 836 |
MOZ_ASSERT(args[0].isObject()); |
| 837 |
|
| 838 |
GeneratorObject* genObj = &args[0].toObject().as<GeneratorObject>(); |
| 839 |
args.rval().setBoolean(genObj->isClosed()); |
| 840 |
return true; |
| 841 |
} |
| 842 |
|
| 843 |
static bool intrinsic_IsSuspendedGenerator(JSContext* cx, unsigned argc, |
| 844 |
Value* vp) { |
| 845 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 846 |
MOZ_ASSERT(args.length() == 1); |
| 847 |
|
| 848 |
if (!args[0].isObject() || !args[0].toObject().is<GeneratorObject>()) { |
| 849 |
args.rval().setBoolean(false); |
| 850 |
return true; |
| 851 |
} |
| 852 |
|
| 853 |
GeneratorObject& genObj = args[0].toObject().as<GeneratorObject>(); |
| 854 |
args.rval().setBoolean(!genObj.isClosed() && genObj.isSuspended()); |
| 855 |
return true; |
| 856 |
} |
| 857 |
|
| 858 |
static bool intrinsic_GeneratorIsRunning(JSContext* cx, unsigned argc, |
| 859 |
Value* vp) { |
| 860 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 861 |
MOZ_ASSERT(args.length() == 1); |
| 862 |
MOZ_ASSERT(args[0].isObject()); |
| 863 |
|
| 864 |
GeneratorObject* genObj = &args[0].toObject().as<GeneratorObject>(); |
| 865 |
args.rval().setBoolean(genObj->isRunning()); |
| 866 |
return true; |
| 867 |
} |
| 868 |
|
| 869 |
static bool intrinsic_GeneratorSetClosed(JSContext* cx, unsigned argc, |
| 870 |
Value* vp) { |
| 871 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 872 |
MOZ_ASSERT(args.length() == 1); |
| 873 |
MOZ_ASSERT(args[0].isObject()); |
| 874 |
|
| 875 |
GeneratorObject* genObj = &args[0].toObject().as<GeneratorObject>(); |
| 876 |
genObj->setClosed(cx); |
| 877 |
return true; |
| 878 |
} |
| 879 |
|
| 880 |
static void AssertNonNegativeInteger(const Value& v) { |
| 881 |
MOZ_ASSERT(v.isNumber()); |
| 882 |
MOZ_ASSERT(v.toNumber() >= 0); |
| 883 |
MOZ_ASSERT(v.toNumber() < DOUBLE_INTEGRAL_PRECISION_LIMIT); |
| 884 |
MOZ_ASSERT(JS::ToInteger(v.toNumber()) == v.toNumber()); |
| 885 |
} |
| 886 |
|
| 887 |
static bool intrinsic_IsTypedArrayConstructor(JSContext* cx, unsigned argc, |
| 888 |
Value* vp) { |
| 889 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 890 |
MOZ_ASSERT(args.length() == 1); |
| 891 |
MOZ_ASSERT(args[0].isObject()); |
| 892 |
|
| 893 |
args.rval().setBoolean(js::IsTypedArrayConstructor(&args[0].toObject())); |
| 894 |
return true; |
| 895 |
} |
| 896 |
|
| 897 |
static bool intrinsic_TypedArrayBuffer(JSContext* cx, unsigned argc, |
| 898 |
Value* vp) { |
| 899 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 900 |
MOZ_ASSERT(args.length() == 1); |
| 901 |
MOZ_ASSERT(args[0].toObject().is<TypedArrayObject>()); |
| 902 |
|
| 903 |
Rooted<TypedArrayObject*> tarray(cx, |
| 904 |
&args[0].toObject().as<TypedArrayObject>()); |
| 905 |
if (!TypedArrayObject::ensureHasBuffer(cx, tarray)) { |
| 906 |
return false; |
| 907 |
} |
| 908 |
|
| 909 |
args.rval().set(tarray->bufferValue()); |
| 910 |
return true; |
| 911 |
} |
| 912 |
|
| 913 |
static bool intrinsic_TypedArrayByteOffset(JSContext* cx, unsigned argc, |
| 914 |
Value* vp) { |
| 915 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 916 |
MOZ_ASSERT(args.length() == 1); |
| 917 |
MOZ_ASSERT(args[0].toObject().is<TypedArrayObject>()); |
| 918 |
|
| 919 |
auto* tarr = &args[0].toObject().as<TypedArrayObject>(); |
| 920 |
args.rval().setNumber(tarr->byteOffsetMaybeOutOfBounds()); |
| 921 |
return true; |
| 922 |
} |
| 923 |
|
| 924 |
static bool intrinsic_TypedArrayElementSize(JSContext* cx, unsigned argc, |
| 925 |
Value* vp) { |
| 926 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 927 |
MOZ_ASSERT(args.length() == 1); |
| 928 |
MOZ_ASSERT(args[0].toObject().is<TypedArrayObject>()); |
| 929 |
|
| 930 |
unsigned size = |
| 931 |
TypedArrayElemSize(args[0].toObject().as<TypedArrayObject>().type()); |
| 932 |
MOZ_ASSERT(size == 1 || size == 2 || size == 4 || size == 8); |
| 933 |
|
| 934 |
args.rval().setInt32(mozilla::AssertedCast<int32_t>(size)); |
| 935 |
return true; |
| 936 |
} |
| 937 |
|
| 938 |
// Return the value of [[ArrayLength]] internal slot of the TypedArray |
| 939 |
static bool intrinsic_TypedArrayLength(JSContext* cx, unsigned argc, |
| 940 |
Value* vp) { |
| 941 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 942 |
MOZ_ASSERT(args.length() == 1); |
| 943 |
MOZ_ASSERT(args[0].toObject().is<TypedArrayObject>()); |
| 944 |
|
| 945 |
auto* tarr = &args[0].toObject().as<TypedArrayObject>(); |
| 946 |
|
| 947 |
mozilla::Maybe<size_t> length = tarr->length(); |
| 948 |
if (!length) { |
| 949 |
// Return zero for detached buffers to match JIT code. |
| 950 |
if (tarr->hasDetachedBuffer()) { |
| 951 |
args.rval().setInt32(0); |
| 952 |
return true; |
| 953 |
} |
| 954 |
|
| 955 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 956 |
JSMSG_TYPED_ARRAY_RESIZED_BOUNDS); |
| 957 |
return false; |
| 958 |
} |
| 959 |
|
| 960 |
args.rval().setNumber(*length); |
| 961 |
return true; |
| 962 |
} |
| 963 |
|
| 964 |
// Return the value of [[ArrayLength]] internal slot of the TypedArray. If the |
| 965 |
// length is out-of-bounds, always return zero. |
| 966 |
static bool intrinsic_TypedArrayLengthZeroOnOutOfBounds(JSContext* cx, |
| 967 |
unsigned argc, |
| 968 |
Value* vp) { |
| 969 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 970 |
MOZ_ASSERT(args.length() == 1); |
| 971 |
MOZ_ASSERT(args[0].toObject().is<TypedArrayObject>()); |
| 972 |
|
| 973 |
auto* tarr = &args[0].toObject().as<TypedArrayObject>(); |
| 974 |
args.rval().setNumber(tarr->length().valueOr(0)); |
| 975 |
return true; |
| 976 |
} |
| 977 |
|
| 978 |
static bool intrinsic_PossiblyWrappedTypedArrayLength(JSContext* cx, |
| 979 |
unsigned argc, |
| 980 |
Value* vp) { |
| 981 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 982 |
MOZ_ASSERT(args.length() == 1); |
| 983 |
MOZ_ASSERT(args[0].isObject()); |
| 984 |
|
| 985 |
TypedArrayObject* obj = args[0].toObject().maybeUnwrapAs<TypedArrayObject>(); |
| 986 |
if (!obj) { |
| 987 |
ReportAccessDenied(cx); |
| 988 |
return false; |
| 989 |
} |
| 990 |
|
| 991 |
mozilla::Maybe<size_t> length = obj->length(); |
| 992 |
if (!length) { |
| 993 |
// Return zero for detached buffers to match JIT code. |
| 994 |
if (obj->hasDetachedBuffer()) { |
| 995 |
args.rval().setInt32(0); |
| 996 |
return true; |
| 997 |
} |
| 998 |
|
| 999 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1000 |
JSMSG_TYPED_ARRAY_RESIZED_BOUNDS); |
| 1001 |
return false; |
| 1002 |
} |
| 1003 |
|
| 1004 |
args.rval().setNumber(*length); |
| 1005 |
return true; |
| 1006 |
} |
| 1007 |
|
| 1008 |
static bool intrinsic_PossiblyWrappedTypedArrayHasDetachedBuffer(JSContext* cx, |
| 1009 |
unsigned argc, |
| 1010 |
Value* vp) { |
| 1011 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1012 |
MOZ_ASSERT(args.length() == 1); |
| 1013 |
MOZ_ASSERT(args[0].isObject()); |
| 1014 |
|
| 1015 |
TypedArrayObject* obj = args[0].toObject().maybeUnwrapAs<TypedArrayObject>(); |
| 1016 |
if (!obj) { |
| 1017 |
ReportAccessDenied(cx); |
| 1018 |
return false; |
| 1019 |
} |
| 1020 |
|
| 1021 |
bool detached = obj->hasDetachedBuffer(); |
| 1022 |
args.rval().setBoolean(detached); |
| 1023 |
return true; |
| 1024 |
} |
| 1025 |
|
| 1026 |
static bool intrinsic_TypedArrayIsAutoLength(JSContext* cx, unsigned argc, |
| 1027 |
Value* vp) { |
| 1028 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1029 |
MOZ_ASSERT(args.length() == 1); |
| 1030 |
MOZ_ASSERT(args[0].toObject().is<TypedArrayObject>()); |
| 1031 |
|
| 1032 |
JSObject* obj = &args[0].toObject(); |
| 1033 |
bool isAutoLength = obj->is<ResizableTypedArrayObject>() && |
| 1034 |
obj->as<ResizableTypedArrayObject>().isAutoLength(); |
| 1035 |
args.rval().setBoolean(isAutoLength); |
| 1036 |
return true; |
| 1037 |
} |
| 1038 |
|
| 1039 |
// Extract the TypedArrayObject* underlying |obj| and return it. This method, |
| 1040 |
// in a TOTALLY UNSAFE manner, completely violates the normal compartment |
| 1041 |
// boundaries, returning an object not necessarily in the current compartment |
| 1042 |
// or in |obj|'s compartment. |
| 1043 |
// |
| 1044 |
// All callers of this method are expected to sigil this TypedArrayObject*, and |
| 1045 |
// all values and information derived from it, with an "unsafe" prefix, to |
| 1046 |
// indicate the extreme caution required when dealing with such values. |
| 1047 |
// |
| 1048 |
// If calling code discipline ever fails to be maintained, it's gonna have a |
| 1049 |
// bad time. |
| 1050 |
static TypedArrayObject* DangerouslyUnwrapTypedArray(JSContext* cx, |
| 1051 |
JSObject* obj) { |
| 1052 |
// An unwrapped pointer to an object potentially on the other side of a |
| 1053 |
// compartment boundary! Isn't this such fun? |
| 1054 |
TypedArrayObject* unwrapped = obj->maybeUnwrapAs<TypedArrayObject>(); |
| 1055 |
if (!unwrapped) { |
| 1056 |
ReportAccessDenied(cx); |
| 1057 |
return nullptr; |
| 1058 |
} |
| 1059 |
|
| 1060 |
// Be super-duper careful using this, as we've just punched through |
| 1061 |
// the compartment boundary, and things like buffer() on this aren't |
| 1062 |
// same-compartment with anything else in the calling method. |
| 1063 |
return unwrapped; |
| 1064 |
} |
| 1065 |
|
| 1066 |
static bool intrinsic_TypedArrayBitwiseSlice(JSContext* cx, unsigned argc, |
| 1067 |
Value* vp) { |
| 1068 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1069 |
MOZ_ASSERT(args.length() == 4); |
| 1070 |
MOZ_ASSERT(args[0].isObject()); |
| 1071 |
MOZ_ASSERT(args[1].isObject()); |
| 1072 |
AssertNonNegativeInteger(args[2]); |
| 1073 |
AssertNonNegativeInteger(args[3]); |
| 1074 |
|
| 1075 |
Rooted<TypedArrayObject*> source(cx, |
| 1076 |
&args[0].toObject().as<TypedArrayObject>()); |
| 1077 |
|
| 1078 |
auto sourceLength = source->length(); |
| 1079 |
if (!sourceLength) { |
| 1080 |
if (source->hasDetachedBuffer()) { |
| 1081 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1082 |
JSMSG_TYPED_ARRAY_DETACHED); |
| 1083 |
} else { |
| 1084 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1085 |
JSMSG_TYPED_ARRAY_RESIZED_BOUNDS); |
| 1086 |
} |
| 1087 |
return false; |
| 1088 |
} |
| 1089 |
MOZ_ASSERT(!source->hasDetachedBuffer()); |
| 1090 |
|
| 1091 |
// As directed by |DangerouslyUnwrapTypedArray|, sigil this pointer and all |
| 1092 |
// variables derived from it to counsel extreme caution here. |
| 1093 |
Rooted<TypedArrayObject*> unsafeTypedArrayCrossCompartment(cx); |
| 1094 |
unsafeTypedArrayCrossCompartment = |
| 1095 |
DangerouslyUnwrapTypedArray(cx, &args[1].toObject()); |
| 1096 |
if (!unsafeTypedArrayCrossCompartment) { |
| 1097 |
return false; |
| 1098 |
} |
| 1099 |
MOZ_ASSERT(!unsafeTypedArrayCrossCompartment->hasDetachedBuffer()); |
| 1100 |
|
| 1101 |
// The specification requires us to perform bitwise copying when |sourceType| |
| 1102 |
// and |targetType| are the same (ES2017, §22.2.3.24, step 15). Additionally, |
| 1103 |
// as an optimization, we can also perform bitwise copying when |sourceType| |
| 1104 |
// and |targetType| have compatible bit-level representations. |
| 1105 |
Scalar::Type sourceType = source->type(); |
| 1106 |
if (!CanUseBitwiseCopy(unsafeTypedArrayCrossCompartment->type(), |
| 1107 |
sourceType)) { |
| 1108 |
args.rval().setBoolean(false); |
| 1109 |
return true; |
| 1110 |
} |
| 1111 |
|
| 1112 |
size_t sourceOffset = size_t(args[2].toNumber()); |
| 1113 |
size_t count = size_t(args[3].toNumber()); |
| 1114 |
|
| 1115 |
MOZ_ASSERT(count > 0); |
| 1116 |
MOZ_ASSERT(count <= unsafeTypedArrayCrossCompartment->length().valueOr(0)); |
| 1117 |
|
| 1118 |
size_t elementSize = TypedArrayElemSize(sourceType); |
| 1119 |
MOZ_ASSERT(elementSize == |
| 1120 |
TypedArrayElemSize(unsafeTypedArrayCrossCompartment->type())); |
| 1121 |
|
| 1122 |
SharedMem<uint8_t*> sourceData = |
| 1123 |
source->dataPointerEither().cast<uint8_t*>() + sourceOffset * elementSize; |
| 1124 |
|
| 1125 |
SharedMem<uint8_t*> unsafeTargetDataCrossCompartment = |
| 1126 |
unsafeTypedArrayCrossCompartment->dataPointerEither().cast<uint8_t*>(); |
| 1127 |
|
| 1128 |
size_t byteLength = std::min(count, *sourceLength) * elementSize; |
| 1129 |
|
| 1130 |
// The same-type case requires exact copying preserving the bit-level |
| 1131 |
// encoding of the source data, so use memcpy if possible. If source and |
| 1132 |
// target are the same buffer, we can't use memcpy (or memmove), because |
| 1133 |
// the specification requires sequential copying of the values. This case |
| 1134 |
// is only possible if a @@species constructor created a specifically |
| 1135 |
// crafted typed array. It won't happen in normal code and hence doesn't |
| 1136 |
// need to be optimized. |
| 1137 |
if (!TypedArrayObject::sameBuffer(source, unsafeTypedArrayCrossCompartment)) { |
| 1138 |
if (source->isSharedMemory() || |
| 1139 |
unsafeTypedArrayCrossCompartment->isSharedMemory()) { |
| 1140 |
jit::AtomicOperations::memcpySafeWhenRacy( |
| 1141 |
unsafeTargetDataCrossCompartment, sourceData, byteLength); |
| 1142 |
} else { |
| 1143 |
memcpy(unsafeTargetDataCrossCompartment.unwrapUnshared(), |
| 1144 |
sourceData.unwrapUnshared(), byteLength); |
| 1145 |
} |
| 1146 |
} else { |
| 1147 |
using namespace jit; |
| 1148 |
|
| 1149 |
for (; byteLength > 0; byteLength--) { |
| 1150 |
AtomicOperations::storeSafeWhenRacy( |
| 1151 |
unsafeTargetDataCrossCompartment++, |
| 1152 |
AtomicOperations::loadSafeWhenRacy(sourceData++)); |
| 1153 |
} |
| 1154 |
} |
| 1155 |
|
| 1156 |
args.rval().setBoolean(true); |
| 1157 |
return true; |
| 1158 |
} |
| 1159 |
|
| 1160 |
static bool intrinsic_TypedArrayInitFromPackedArray(JSContext* cx, |
| 1161 |
unsigned argc, Value* vp) { |
| 1162 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1163 |
MOZ_ASSERT(args.length() == 2); |
| 1164 |
MOZ_ASSERT(args[0].isObject()); |
| 1165 |
MOZ_ASSERT(args[1].isObject()); |
| 1166 |
|
| 1167 |
Rooted<FixedLengthTypedArrayObject*> target( |
| 1168 |
cx, &args[0].toObject().as<FixedLengthTypedArrayObject>()); |
| 1169 |
MOZ_ASSERT(!target->hasDetachedBuffer()); |
| 1170 |
MOZ_ASSERT(!target->isSharedMemory()); |
| 1171 |
|
| 1172 |
Rooted<ArrayObject*> source(cx, &args[1].toObject().as<ArrayObject>()); |
| 1173 |
MOZ_ASSERT(IsPackedArray(source)); |
| 1174 |
MOZ_ASSERT(source->length() == target->length()); |
| 1175 |
|
| 1176 |
switch (target->type()) { |
| 1177 |
#define INIT_TYPED_ARRAY(_, T, N) \ |
| 1178 |
case Scalar::N: { \ |
| 1179 |
if (!ElementSpecific<T, UnsharedOps>::initFromIterablePackedArray( \ |
| 1180 |
cx, target, source)) { \ |
| 1181 |
return false; \ |
| 1182 |
} \ |
| 1183 |
break; \ |
| 1184 |
} |
| 1185 |
JS_FOR_EACH_TYPED_ARRAY(INIT_TYPED_ARRAY) |
| 1186 |
#undef INIT_TYPED_ARRAY |
| 1187 |
|
| 1188 |
default: |
| 1189 |
MOZ_CRASH( |
| 1190 |
"TypedArrayInitFromPackedArray with a typed array with bogus type"); |
| 1191 |
} |
| 1192 |
|
| 1193 |
args.rval().setUndefined(); |
| 1194 |
return true; |
| 1195 |
} |
| 1196 |
|
| 1197 |
template <bool ForTest> |
| 1198 |
static bool intrinsic_RegExpBuiltinExec(JSContext* cx, unsigned argc, |
| 1199 |
Value* vp) { |
| 1200 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1201 |
MOZ_ASSERT(args.length() == 2); |
| 1202 |
MOZ_ASSERT(args[0].isObject()); |
| 1203 |
MOZ_ASSERT(args[0].toObject().is<RegExpObject>()); |
| 1204 |
MOZ_ASSERT(args[1].isString()); |
| 1205 |
|
| 1206 |
Rooted<RegExpObject*> obj(cx, &args[0].toObject().as<RegExpObject>()); |
| 1207 |
Rooted<JSString*> string(cx, args[1].toString()); |
| 1208 |
return RegExpBuiltinExec(cx, obj, string, ForTest, args.rval()); |
| 1209 |
} |
| 1210 |
|
| 1211 |
template <bool ForTest> |
| 1212 |
static bool intrinsic_RegExpExec(JSContext* cx, unsigned argc, Value* vp) { |
| 1213 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1214 |
MOZ_ASSERT(args.length() == 2); |
| 1215 |
MOZ_ASSERT(args[0].isObject()); |
| 1216 |
MOZ_ASSERT(args[1].isString()); |
| 1217 |
|
| 1218 |
Rooted<JSObject*> obj(cx, &args[0].toObject()); |
| 1219 |
Rooted<JSString*> string(cx, args[1].toString()); |
| 1220 |
return RegExpExec(cx, obj, string, ForTest, args.rval()); |
| 1221 |
} |
| 1222 |
|
| 1223 |
static bool intrinsic_RegExpCreate(JSContext* cx, unsigned argc, Value* vp) { |
| 1224 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1225 |
|
| 1226 |
MOZ_ASSERT(args.length() == 1 || args.length() == 2); |
| 1227 |
MOZ_ASSERT_IF(args.length() == 2, |
| 1228 |
args[1].isString() || args[1].isUndefined()); |
| 1229 |
MOZ_ASSERT(!args.isConstructing()); |
| 1230 |
|
| 1231 |
return RegExpCreate(cx, args[0], args.get(1), args.rval()); |
| 1232 |
} |
| 1233 |
|
| 1234 |
static bool intrinsic_RegExpGetSubstitution(JSContext* cx, unsigned argc, |
| 1235 |
Value* vp) { |
| 1236 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1237 |
MOZ_ASSERT(args.length() == 6); |
| 1238 |
|
| 1239 |
Rooted<ArrayObject*> matchResult(cx, &args[0].toObject().as<ArrayObject>()); |
| 1240 |
|
| 1241 |
Rooted<JSLinearString*> string(cx, args[1].toString()->ensureLinear(cx)); |
| 1242 |
if (!string) { |
| 1243 |
return false; |
| 1244 |
} |
| 1245 |
|
| 1246 |
int32_t position = int32_t(args[2].toNumber()); |
| 1247 |
MOZ_ASSERT(position >= 0); |
| 1248 |
|
| 1249 |
Rooted<JSLinearString*> replacement(cx, args[3].toString()->ensureLinear(cx)); |
| 1250 |
if (!replacement) { |
| 1251 |
return false; |
| 1252 |
} |
| 1253 |
|
| 1254 |
int32_t firstDollarIndex = int32_t(args[4].toNumber()); |
| 1255 |
MOZ_ASSERT(firstDollarIndex >= 0); |
| 1256 |
|
| 1257 |
RootedValue namedCaptures(cx, args[5]); |
| 1258 |
MOZ_ASSERT(namedCaptures.isUndefined() || namedCaptures.isObject()); |
| 1259 |
|
| 1260 |
return RegExpGetSubstitution(cx, matchResult, string, size_t(position), |
| 1261 |
replacement, size_t(firstDollarIndex), |
| 1262 |
namedCaptures, args.rval()); |
| 1263 |
} |
| 1264 |
|
| 1265 |
static bool intrinsic_RegExpHasCaptureGroups(JSContext* cx, unsigned argc, |
| 1266 |
Value* vp) { |
| 1267 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1268 |
MOZ_ASSERT(args.length() == 2); |
| 1269 |
MOZ_ASSERT(args[0].isObject()); |
| 1270 |
MOZ_ASSERT(args[1].isString()); |
| 1271 |
|
| 1272 |
Rooted<RegExpObject*> obj(cx, &args[0].toObject().as<RegExpObject>()); |
| 1273 |
Rooted<JSString*> string(cx, args[1].toString()); |
| 1274 |
|
| 1275 |
bool result; |
| 1276 |
if (!RegExpHasCaptureGroups(cx, obj, string, &result)) { |
| 1277 |
return false; |
| 1278 |
} |
| 1279 |
|
| 1280 |
args.rval().setBoolean(result); |
| 1281 |
return true; |
| 1282 |
} |
| 1283 |
|
| 1284 |
static bool intrinsic_StringReplaceString(JSContext* cx, unsigned argc, |
| 1285 |
Value* vp) { |
| 1286 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1287 |
MOZ_ASSERT(args.length() == 3); |
| 1288 |
|
| 1289 |
RootedString string(cx, args[0].toString()); |
| 1290 |
RootedString pattern(cx, args[1].toString()); |
| 1291 |
RootedString replacement(cx, args[2].toString()); |
| 1292 |
JSString* result = str_replace_string_raw(cx, string, pattern, replacement); |
| 1293 |
if (!result) { |
| 1294 |
return false; |
| 1295 |
} |
| 1296 |
|
| 1297 |
args.rval().setString(result); |
| 1298 |
return true; |
| 1299 |
} |
| 1300 |
|
| 1301 |
static bool intrinsic_RegExpSymbolProtocolOnPrimitiveCounter(JSContext* cx, |
| 1302 |
unsigned argc, |
| 1303 |
Value* vp) { |
| 1304 |
// This telemetry is to assess compatibility for tc39/ecma262#3009 and |
| 1305 |
// can later be removed (Bug 1953619). |
| 1306 |
cx->runtime()->setUseCounter( |
| 1307 |
cx->global(), JSUseCounter::REGEXP_SYMBOL_PROTOCOL_ON_PRIMITIVE); |
| 1308 |
return true; |
| 1309 |
} |
| 1310 |
|
| 1311 |
static bool intrinsic_StringReplaceAllString(JSContext* cx, unsigned argc, |
| 1312 |
Value* vp) { |
| 1313 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1314 |
MOZ_ASSERT(args.length() == 3); |
| 1315 |
|
| 1316 |
RootedString string(cx, args[0].toString()); |
| 1317 |
RootedString pattern(cx, args[1].toString()); |
| 1318 |
RootedString replacement(cx, args[2].toString()); |
| 1319 |
JSString* result = |
| 1320 |
str_replaceAll_string_raw(cx, string, pattern, replacement); |
| 1321 |
if (!result) { |
| 1322 |
return false; |
| 1323 |
} |
| 1324 |
|
| 1325 |
args.rval().setString(result); |
| 1326 |
return true; |
| 1327 |
} |
| 1328 |
|
| 1329 |
static bool intrinsic_StringSplitString(JSContext* cx, unsigned argc, |
| 1330 |
Value* vp) { |
| 1331 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1332 |
MOZ_ASSERT(args.length() == 2); |
| 1333 |
|
| 1334 |
RootedString string(cx, args[0].toString()); |
| 1335 |
RootedString sep(cx, args[1].toString()); |
| 1336 |
|
| 1337 |
JSObject* aobj = StringSplitString(cx, string, sep, INT32_MAX); |
| 1338 |
if (!aobj) { |
| 1339 |
return false; |
| 1340 |
} |
| 1341 |
|
| 1342 |
args.rval().setObject(*aobj); |
| 1343 |
return true; |
| 1344 |
} |
| 1345 |
|
| 1346 |
static bool intrinsic_StringSplitStringLimit(JSContext* cx, unsigned argc, |
| 1347 |
Value* vp) { |
| 1348 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1349 |
MOZ_ASSERT(args.length() == 3); |
| 1350 |
|
| 1351 |
RootedString string(cx, args[0].toString()); |
| 1352 |
RootedString sep(cx, args[1].toString()); |
| 1353 |
|
| 1354 |
// args[2] should be already in UInt32 range, but it could be double typed, |
| 1355 |
// because of Ion optimization. |
| 1356 |
uint32_t limit = uint32_t(args[2].toNumber()); |
| 1357 |
MOZ_ASSERT(limit > 0, |
| 1358 |
"Zero limit case is already handled in self-hosted code."); |
| 1359 |
|
| 1360 |
JSObject* aobj = StringSplitString(cx, string, sep, limit); |
| 1361 |
if (!aobj) { |
| 1362 |
return false; |
| 1363 |
} |
| 1364 |
|
| 1365 |
args.rval().setObject(*aobj); |
| 1366 |
return true; |
| 1367 |
} |
| 1368 |
|
| 1369 |
bool CallSelfHostedNonGenericMethod(JSContext* cx, const CallArgs& args) { |
| 1370 |
// This function is called when a self-hosted method is invoked on a |
| 1371 |
// wrapper object, like a CrossCompartmentWrapper. The last argument is |
| 1372 |
// the name of the self-hosted function. The other arguments are the |
| 1373 |
// arguments to pass to this function. |
| 1374 |
|
| 1375 |
MOZ_ASSERT(args.length() > 0); |
| 1376 |
Rooted<PropertyName*> name( |
| 1377 |
cx, args[args.length() - 1].toString()->asAtom().asPropertyName()); |
| 1378 |
|
| 1379 |
InvokeArgs args2(cx); |
| 1380 |
if (!args2.init(cx, args.length() - 1)) { |
| 1381 |
return false; |
| 1382 |
} |
| 1383 |
|
| 1384 |
for (size_t i = 0; i < args.length() - 1; i++) { |
| 1385 |
args2[i].set(args[i]); |
| 1386 |
} |
| 1387 |
|
| 1388 |
return CallSelfHostedFunction(cx, name, args.thisv(), args2, args.rval()); |
| 1389 |
} |
| 1390 |
|
| 1391 |
#ifdef DEBUG |
| 1392 |
bool js::CallSelfHostedFunction(JSContext* cx, const char* name, |
| 1393 |
HandleValue thisv, const AnyInvokeArgs& args, |
| 1394 |
MutableHandleValue rval) { |
| 1395 |
JSAtom* funAtom = Atomize(cx, name, strlen(name)); |
| 1396 |
if (!funAtom) { |
| 1397 |
return false; |
| 1398 |
} |
| 1399 |
Rooted<PropertyName*> funName(cx, funAtom->asPropertyName()); |
| 1400 |
return CallSelfHostedFunction(cx, funName, thisv, args, rval); |
| 1401 |
} |
| 1402 |
#endif |
| 1403 |
|
| 1404 |
bool js::CallSelfHostedFunction(JSContext* cx, Handle<PropertyName*> name, |
| 1405 |
HandleValue thisv, const AnyInvokeArgs& args, |
| 1406 |
MutableHandleValue rval) { |
| 1407 |
RootedValue fun(cx); |
| 1408 |
if (!GlobalObject::getIntrinsicValue(cx, cx->global(), name, &fun)) { |
| 1409 |
return false; |
| 1410 |
} |
| 1411 |
MOZ_ASSERT(fun.toObject().is<JSFunction>()); |
| 1412 |
|
| 1413 |
return Call(cx, fun, thisv, args, rval); |
| 1414 |
} |
| 1415 |
|
| 1416 |
template <typename T> |
| 1417 |
bool Is(HandleValue v) { |
| 1418 |
return v.isObject() && v.toObject().is<T>(); |
| 1419 |
} |
| 1420 |
|
| 1421 |
template <IsAcceptableThis Test> |
| 1422 |
static bool CallNonGenericSelfhostedMethod(JSContext* cx, unsigned argc, |
| 1423 |
Value* vp) { |
| 1424 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1425 |
return CallNonGenericMethod<Test, CallSelfHostedNonGenericMethod>(cx, args); |
| 1426 |
} |
| 1427 |
|
| 1428 |
bool js::IsCallSelfHostedNonGenericMethod(NativeImpl impl) { |
| 1429 |
return impl == CallSelfHostedNonGenericMethod; |
| 1430 |
} |
| 1431 |
|
| 1432 |
bool js::ReportIncompatibleSelfHostedMethod(JSContext* cx, |
| 1433 |
Handle<Value> thisValue) { |
| 1434 |
// The contract for this function is the same as |
| 1435 |
// CallSelfHostedNonGenericMethod. The normal ReportIncompatible function |
| 1436 |
// doesn't work for selfhosted functions, because they always call the |
| 1437 |
// different CallXXXMethodIfWrapped methods, which would be reported as the |
| 1438 |
// called function instead. |
| 1439 |
|
| 1440 |
// Lookup the selfhosted method that was invoked. But skip over |
| 1441 |
// internal self-hosted function frames, because those are never the |
| 1442 |
// actual self-hosted callee from external code. We can't just skip |
| 1443 |
// self-hosted things until we find a non-self-hosted one because of cases |
| 1444 |
// like array.sort(somethingSelfHosted), where we want to report the error |
| 1445 |
// in the somethingSelfHosted, not in the sort() call. |
| 1446 |
|
| 1447 |
static const char* const internalNames[] = { |
| 1448 |
"EnsureTypedArrayWithArrayBuffer", |
| 1449 |
"RegExpSearchSlowPath", |
| 1450 |
"RegExpReplaceSlowPath", |
| 1451 |
"RegExpMatchSlowPath", |
| 1452 |
}; |
| 1453 |
|
| 1454 |
ScriptFrameIter iter(cx); |
| 1455 |
MOZ_ASSERT(iter.isFunctionFrame()); |
| 1456 |
|
| 1457 |
while (!iter.done()) { |
| 1458 |
MOZ_ASSERT(iter.callee(cx)->isSelfHostedOrIntrinsic()); |
| 1459 |
UniqueChars funNameBytes; |
| 1460 |
const char* funName = |
| 1461 |
GetFunctionNameBytes(cx, iter.callee(cx), &funNameBytes); |
| 1462 |
if (!funName) { |
| 1463 |
return false; |
| 1464 |
} |
| 1465 |
if (std::all_of( |
| 1466 |
std::begin(internalNames), std::end(internalNames), |
| 1467 |
[funName](auto* name) { return strcmp(funName, name) != 0; })) { |
| 1468 |
JS_ReportErrorNumberUTF8(cx, GetErrorMessage, nullptr, |
| 1469 |
JSMSG_INCOMPATIBLE_METHOD, funName, "method", |
| 1470 |
InformalValueTypeName(thisValue)); |
| 1471 |
return false; |
| 1472 |
} |
| 1473 |
++iter; |
| 1474 |
} |
| 1475 |
|
| 1476 |
MOZ_ASSERT_UNREACHABLE("How did we not find a useful self-hosted frame?"); |
| 1477 |
return false; |
| 1478 |
} |
| 1479 |
|
| 1480 |
#ifdef JS_HAS_INTL_API |
| 1481 |
/** |
| 1482 |
* Returns the default locale as a well-formed, but not necessarily |
| 1483 |
* canonicalized, BCP-47 language tag. |
| 1484 |
*/ |
| 1485 |
static bool intrinsic_RuntimeDefaultLocale(JSContext* cx, unsigned argc, |
| 1486 |
Value* vp) { |
| 1487 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1488 |
MOZ_ASSERT(args.length() == 0); |
| 1489 |
|
| 1490 |
const char* locale = cx->realm()->getLocale(); |
| 1491 |
if (!locale) { |
| 1492 |
return false; |
| 1493 |
} |
| 1494 |
|
| 1495 |
JSString* jslocale = NewStringCopyZ<CanGC>(cx, locale); |
| 1496 |
if (!jslocale) { |
| 1497 |
return false; |
| 1498 |
} |
| 1499 |
|
| 1500 |
args.rval().setString(jslocale); |
| 1501 |
return true; |
| 1502 |
} |
| 1503 |
|
| 1504 |
static bool intrinsic_IsRuntimeDefaultLocale(JSContext* cx, unsigned argc, |
| 1505 |
Value* vp) { |
| 1506 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1507 |
MOZ_ASSERT(args.length() == 1); |
| 1508 |
MOZ_ASSERT(args[0].isString() || args[0].isUndefined()); |
| 1509 |
|
| 1510 |
// |undefined| is the default value when the Intl runtime caches haven't |
| 1511 |
// yet been initialized. Handle it the same way as a cache miss. |
| 1512 |
if (args[0].isUndefined()) { |
| 1513 |
args.rval().setBoolean(false); |
| 1514 |
return true; |
| 1515 |
} |
| 1516 |
|
| 1517 |
const char* locale = cx->realm()->getLocale(); |
| 1518 |
if (!locale) { |
| 1519 |
return false; |
| 1520 |
} |
| 1521 |
|
| 1522 |
JSLinearString* str = args[0].toString()->ensureLinear(cx); |
| 1523 |
if (!str) { |
| 1524 |
return false; |
| 1525 |
} |
| 1526 |
|
| 1527 |
bool equals = StringEqualsAscii(str, locale); |
| 1528 |
args.rval().setBoolean(equals); |
| 1529 |
return true; |
| 1530 |
} |
| 1531 |
#endif // JS_HAS_INTL_API |
| 1532 |
|
| 1533 |
static bool intrinsic_ConstructFunction(JSContext* cx, unsigned argc, |
| 1534 |
Value* vp) { |
| 1535 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1536 |
MOZ_ASSERT(args.length() == 3); |
| 1537 |
MOZ_ASSERT(IsConstructor(args[0])); |
| 1538 |
MOZ_ASSERT(IsConstructor(args[1])); |
| 1539 |
MOZ_ASSERT(args[2].toObject().is<ArrayObject>()); |
| 1540 |
|
| 1541 |
Rooted<ArrayObject*> argsList(cx, &args[2].toObject().as<ArrayObject>()); |
| 1542 |
uint32_t len = argsList->length(); |
| 1543 |
ConstructArgs constructArgs(cx); |
| 1544 |
if (!constructArgs.init(cx, len)) { |
| 1545 |
return false; |
| 1546 |
} |
| 1547 |
for (uint32_t index = 0; index < len; index++) { |
| 1548 |
constructArgs[index].set(argsList->getDenseElement(index)); |
| 1549 |
} |
| 1550 |
|
| 1551 |
RootedObject res(cx); |
| 1552 |
if (!Construct(cx, args[0], constructArgs, args[1], &res)) { |
| 1553 |
return false; |
| 1554 |
} |
| 1555 |
|
| 1556 |
args.rval().setObject(*res); |
| 1557 |
return true; |
| 1558 |
} |
| 1559 |
|
| 1560 |
static bool intrinsic_IsConstructing(JSContext* cx, unsigned argc, Value* vp) { |
| 1561 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1562 |
MOZ_ASSERT(args.length() == 0); |
| 1563 |
|
| 1564 |
ScriptFrameIter iter(cx); |
| 1565 |
bool isConstructing = iter.isConstructing(); |
| 1566 |
args.rval().setBoolean(isConstructing); |
| 1567 |
return true; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static bool intrinsic_ConstructorForTypedArray(JSContext* cx, unsigned argc, |
| 1571 |
Value* vp) { |
| 1572 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1573 |
MOZ_ASSERT(args.length() == 1); |
| 1574 |
MOZ_ASSERT(args[0].isObject()); |
| 1575 |
|
| 1576 |
auto* object = UnwrapAndDowncastValue<TypedArrayObject>(cx, args[0]); |
| 1577 |
if (!object) { |
| 1578 |
return false; |
| 1579 |
} |
| 1580 |
|
| 1581 |
JSProtoKey protoKey = StandardProtoKeyOrNull(object); |
| 1582 |
MOZ_ASSERT(protoKey); |
| 1583 |
|
| 1584 |
// While it may seem like an invariant that in any compartment, |
| 1585 |
// seeing a typed array object implies that the TypedArray constructor |
| 1586 |
// for that type is initialized on the compartment's global, this is not |
| 1587 |
// the case. When we construct a typed array given a cross-compartment |
| 1588 |
// ArrayBuffer, we put the constructed TypedArray in the same compartment |
| 1589 |
// as the ArrayBuffer. Since we use the prototype from the initial |
| 1590 |
// compartment, and never call the constructor in the ArrayBuffer's |
| 1591 |
// compartment from script, we are not guaranteed to have initialized |
| 1592 |
// the constructor. |
| 1593 |
JSObject* ctor = GlobalObject::getOrCreateConstructor(cx, protoKey); |
| 1594 |
if (!ctor) { |
| 1595 |
return false; |
| 1596 |
} |
| 1597 |
|
| 1598 |
args.rval().setObject(*ctor); |
| 1599 |
return true; |
| 1600 |
} |
| 1601 |
|
| 1602 |
static bool intrinsic_PromiseResolve(JSContext* cx, unsigned argc, Value* vp) { |
| 1603 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1604 |
MOZ_ASSERT(args.length() == 2); |
| 1605 |
|
| 1606 |
RootedObject constructor(cx, &args[0].toObject()); |
| 1607 |
JSObject* promise = js::PromiseResolve(cx, constructor, args[1]); |
| 1608 |
if (!promise) { |
| 1609 |
return false; |
| 1610 |
} |
| 1611 |
|
| 1612 |
args.rval().setObject(*promise); |
| 1613 |
return true; |
| 1614 |
} |
| 1615 |
|
| 1616 |
static bool intrinsic_CopyDataPropertiesOrGetOwnKeys(JSContext* cx, |
| 1617 |
unsigned argc, Value* vp) { |
| 1618 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1619 |
MOZ_ASSERT(args.length() == 3); |
| 1620 |
MOZ_ASSERT(args[0].isObject()); |
| 1621 |
MOZ_ASSERT(args[1].isObject()); |
| 1622 |
MOZ_ASSERT(args[2].isObjectOrNull()); |
| 1623 |
|
| 1624 |
RootedObject target(cx, &args[0].toObject()); |
| 1625 |
RootedObject from(cx, &args[1].toObject()); |
| 1626 |
RootedObject excludedItems(cx, args[2].toObjectOrNull()); |
| 1627 |
|
| 1628 |
if (from->is<NativeObject>() && target->is<PlainObject>() && |
| 1629 |
(!excludedItems || excludedItems->is<PlainObject>())) { |
| 1630 |
bool optimized; |
| 1631 |
if (!CopyDataPropertiesNative( |
| 1632 |
cx, target.as<PlainObject>(), from.as<NativeObject>(), |
| 1633 |
(excludedItems ? excludedItems.as<PlainObject>() : nullptr), |
| 1634 |
&optimized)) { |
| 1635 |
return false; |
| 1636 |
} |
| 1637 |
|
| 1638 |
if (optimized) { |
| 1639 |
args.rval().setNull(); |
| 1640 |
return true; |
| 1641 |
} |
| 1642 |
} |
| 1643 |
|
| 1644 |
return GetOwnPropertyKeys( |
| 1645 |
cx, from, JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS, args.rval()); |
| 1646 |
} |
| 1647 |
|
| 1648 |
static bool intrinsic_ToBigInt(JSContext* cx, unsigned argc, Value* vp) { |
| 1649 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1650 |
MOZ_ASSERT(args.length() == 1); |
| 1651 |
BigInt* res = ToBigInt(cx, args[0]); |
| 1652 |
if (!res) { |
| 1653 |
return false; |
| 1654 |
} |
| 1655 |
args.rval().setBigInt(res); |
| 1656 |
return true; |
| 1657 |
} |
| 1658 |
|
| 1659 |
static bool intrinsic_NumberToBigInt(JSContext* cx, unsigned argc, Value* vp) { |
| 1660 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1661 |
MOZ_ASSERT(args.length() == 1); |
| 1662 |
MOZ_ASSERT(args[0].isNumber()); |
| 1663 |
BigInt* res = NumberToBigInt(cx, args[0].toNumber()); |
| 1664 |
if (!res) { |
| 1665 |
return false; |
| 1666 |
} |
| 1667 |
args.rval().setBigInt(res); |
| 1668 |
return true; |
| 1669 |
} |
| 1670 |
|
| 1671 |
static bool intrinsic_BigIntToNumber(JSContext* cx, unsigned argc, Value* vp) { |
| 1672 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1673 |
MOZ_ASSERT(args.length() == 1); |
| 1674 |
MOZ_ASSERT(args[0].isBigInt()); |
| 1675 |
args.rval().setNumber(BigInt::numberValue(args[0].toBigInt())); |
| 1676 |
return true; |
| 1677 |
} |
| 1678 |
|
| 1679 |
static bool intrinsic_NewWrapForValidIterator(JSContext* cx, unsigned argc, |
| 1680 |
Value* vp) { |
| 1681 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1682 |
MOZ_ASSERT(args.length() == 0); |
| 1683 |
|
| 1684 |
JSObject* obj = NewWrapForValidIterator(cx); |
| 1685 |
if (!obj) { |
| 1686 |
return false; |
| 1687 |
} |
| 1688 |
|
| 1689 |
args.rval().setObject(*obj); |
| 1690 |
return true; |
| 1691 |
} |
| 1692 |
|
| 1693 |
static bool intrinsic_NewIteratorHelper(JSContext* cx, unsigned argc, |
| 1694 |
Value* vp) { |
| 1695 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1696 |
MOZ_ASSERT(args.length() == 0); |
| 1697 |
|
| 1698 |
JSObject* obj = NewIteratorHelper(cx); |
| 1699 |
if (!obj) { |
| 1700 |
return false; |
| 1701 |
} |
| 1702 |
|
| 1703 |
args.rval().setObject(*obj); |
| 1704 |
return true; |
| 1705 |
} |
| 1706 |
|
| 1707 |
static bool intrinsic_NewAsyncIteratorHelper(JSContext* cx, unsigned argc, |
| 1708 |
Value* vp) { |
| 1709 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1710 |
MOZ_ASSERT(args.length() == 0); |
| 1711 |
|
| 1712 |
JSObject* obj = NewAsyncIteratorHelper(cx); |
| 1713 |
if (!obj) { |
| 1714 |
return false; |
| 1715 |
} |
| 1716 |
|
| 1717 |
args.rval().setObject(*obj); |
| 1718 |
return true; |
| 1719 |
} |
| 1720 |
|
| 1721 |
#ifdef NIGHTLY_BUILD |
| 1722 |
static bool intrinsic_NewIteratorRange(JSContext* cx, unsigned argc, |
| 1723 |
Value* vp) { |
| 1724 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1725 |
MOZ_ASSERT(args.length() == 0); |
| 1726 |
|
| 1727 |
JSObject* obj = NewIteratorRange(cx); |
| 1728 |
if (!obj) { |
| 1729 |
return false; |
| 1730 |
} |
| 1731 |
|
| 1732 |
args.rval().setObject(*obj); |
| 1733 |
return true; |
| 1734 |
} |
| 1735 |
#endif |
| 1736 |
|
| 1737 |
static JSObject* NewIteratorRecord(JSContext* cx, HandleObject iterator, |
| 1738 |
HandleValue nextMethod) { |
| 1739 |
gc::AllocKind allocKind = gc::GetGCObjectKind(3); |
| 1740 |
Rooted<PlainObject*> obj( |
| 1741 |
cx, NewPlainObjectWithProtoAndAllocKind(cx, nullptr, allocKind)); |
| 1742 |
if (!obj) { |
| 1743 |
return nullptr; |
| 1744 |
} |
| 1745 |
|
| 1746 |
RootedId propid(cx, NameToId(cx->names().iterator)); |
| 1747 |
RootedValue value(cx, ObjectValue(*iterator)); |
| 1748 |
if (!NativeDefineDataProperty(cx, obj, propid, value, JSPROP_ENUMERATE)) { |
| 1749 |
return nullptr; |
| 1750 |
} |
| 1751 |
|
| 1752 |
propid = NameToId(cx->names().nextMethod); |
| 1753 |
value.set(nextMethod); |
| 1754 |
if (!NativeDefineDataProperty(cx, obj, propid, value, JSPROP_ENUMERATE)) { |
| 1755 |
return nullptr; |
| 1756 |
} |
| 1757 |
|
| 1758 |
propid = NameToId(cx->names().done); |
| 1759 |
value.setBoolean(false); |
| 1760 |
if (!NativeDefineDataProperty(cx, obj, propid, value, JSPROP_ENUMERATE)) { |
| 1761 |
return nullptr; |
| 1762 |
} |
| 1763 |
|
| 1764 |
return obj; |
| 1765 |
} |
| 1766 |
|
| 1767 |
static bool intrinsic_CreateAsyncFromSyncIterator(JSContext* cx, unsigned argc, |
| 1768 |
Value* vp) { |
| 1769 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1770 |
MOZ_ASSERT(args.length() == 2); |
| 1771 |
|
| 1772 |
RootedObject iterator(cx, &args[0].toObject()); |
| 1773 |
RootedObject asyncIterator( |
| 1774 |
cx, CreateAsyncFromSyncIterator(cx, iterator, args[1])); |
| 1775 |
if (!asyncIterator) { |
| 1776 |
return false; |
| 1777 |
} |
| 1778 |
|
| 1779 |
RootedValue nextMethod(cx); |
| 1780 |
if (!GetProperty(cx, asyncIterator, asyncIterator, cx->names().next, |
| 1781 |
&nextMethod)) { |
| 1782 |
return false; |
| 1783 |
} |
| 1784 |
|
| 1785 |
RootedObject iteratorRecord(cx, |
| 1786 |
NewIteratorRecord(cx, asyncIterator, nextMethod)); |
| 1787 |
if (!iteratorRecord) { |
| 1788 |
return false; |
| 1789 |
} |
| 1790 |
|
| 1791 |
args.rval().setObject(*iteratorRecord); |
| 1792 |
return true; |
| 1793 |
} |
| 1794 |
|
| 1795 |
static bool intrinsic_NoPrivateGetter(JSContext* cx, unsigned argc, Value* vp) { |
| 1796 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1797 |
MOZ_ASSERT(args.length() == 0); |
| 1798 |
|
| 1799 |
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, |
| 1800 |
JSMSG_PRIVATE_SETTER_ONLY); |
| 1801 |
|
| 1802 |
args.rval().setUndefined(); |
| 1803 |
return false; |
| 1804 |
} |
| 1805 |
|
| 1806 |
static bool intrinsic_newList(JSContext* cx, unsigned argc, js::Value* vp) { |
| 1807 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1808 |
MOZ_ASSERT(args.length() == 0); |
| 1809 |
|
| 1810 |
ArrayObject* list = NewArrayWithNullProto(cx); |
| 1811 |
if (!list) { |
| 1812 |
return false; |
| 1813 |
} |
| 1814 |
|
| 1815 |
args.rval().setObject(*list); |
| 1816 |
return true; |
| 1817 |
} |
| 1818 |
|
| 1819 |
#ifdef JS_HAS_INTL_API |
| 1820 |
static bool intrinsic_ToTemporalDuration(JSContext* cx, unsigned argc, |
| 1821 |
js::Value* vp) { |
| 1822 |
CallArgs args = CallArgsFromVp(argc, vp); |
| 1823 |
MOZ_ASSERT(args.length() == 1); |
| 1824 |
|
| 1825 |
using namespace temporal; |
| 1826 |
|
| 1827 |
// Return |null| if Temporal is disabled. |
| 1828 |
if (!JS::Prefs::experimental_temporal()) { |
| 1829 |
args.rval().setNull(); |
| 1830 |
return true; |
| 1831 |
} |
| 1832 |
|
| 1833 |
Duration duration; |
| 1834 |
if (!ToTemporalDuration(cx, args.get(0), &duration)) { |
| 1835 |
return false; |
| 1836 |
} |
| 1837 |
|
| 1838 |
// Normalize -0 to +0 by adding zero. |
| 1839 |
duration.years += +0.0; |
| 1840 |
duration.months += +0.0; |
| 1841 |
duration.weeks += +0.0; |
| 1842 |
duration.days += +0.0; |
| 1843 |
duration.hours += +0.0; |
| 1844 |
duration.minutes += +0.0; |
| 1845 |
duration.seconds += +0.0; |
| 1846 |
duration.milliseconds += +0.0; |
| 1847 |
duration.microseconds += +0.0; |
| 1848 |
duration.nanoseconds += +0.0; |
| 1849 |
|
| 1850 |
// Create a new plain object to ensure there are no observable calls to |
| 1851 |
// `Temporal.Duration.prototype` getters. |
| 1852 |
|
| 1853 |
Rooted<IdValueVector> properties(cx, cx); |
| 1854 |
if (!properties.emplaceBack(NameToId(cx->names().years), |
| 1855 |
NumberValue(duration.years))) { |
| 1856 |
return false; |
| 1857 |
} |
| 1858 |
if (!properties.emplaceBack(NameToId(cx->names().months), |
| 1859 |
NumberValue(duration.months))) { |
| 1860 |
return false; |
| 1861 |
} |
| 1862 |
if (!properties.emplaceBack(NameToId(cx->names().weeks), |
| 1863 |
NumberValue(duration.weeks))) { |
| 1864 |
return false; |
| 1865 |
} |
| 1866 |
if (!properties.emplaceBack(NameToId(cx->names().days), |
| 1867 |
NumberValue(duration.days))) { |
| 1868 |
return false; |
| 1869 |
} |
| 1870 |
if (!properties.emplaceBack(NameToId(cx->names().hours), |
| 1871 |
NumberValue(duration.hours))) { |
| 1872 |
return false; |
| 1873 |
} |
| 1874 |
if (!properties.emplaceBack(NameToId(cx->names().minutes), |
| 1875 |
NumberValue(duration.minutes))) { |
| 1876 |
return false; |
| 1877 |
} |
| 1878 |
if (!properties.emplaceBack(NameToId(cx->names().seconds), |
| 1879 |
NumberValue(duration.seconds))) { |
| 1880 |
return false; |
| 1881 |
} |
| 1882 |
if (!properties.emplaceBack(NameToId(cx->names().milliseconds), |
| 1883 |
NumberValue(duration.milliseconds))) { |
| 1884 |
return false; |
| 1885 |
} |
| 1886 |
if (!properties.emplaceBack(NameToId(cx->names().microseconds), |
| 1887 |
NumberValue(duration.microseconds))) { |
| 1888 |
return false; |
| 1889 |
} |
| 1890 |
if (!properties.emplaceBack(NameToId(cx->names().nanoseconds), |
| 1891 |
NumberValue(duration.nanoseconds))) { |
| 1892 |
return false; |
| 1893 |
} |
| 1894 |
|
| 1895 |
auto* result = NewPlainObjectWithUniqueNames(cx, properties); |
| 1896 |
if (!result) { |
| 1897 |
return false; |
| 1898 |
} |
| 1899 |
|
| 1900 |
args.rval().setObject(*result); |
| 1901 |
return true; |
| 1902 |
} |
| 1903 |
#endif |
| 1904 |
|
| 1905 |
static const JSFunctionSpec intrinsic_functions[] = { |
| 1906 |
// Intrinsic helper functions |
| 1907 |
JS_INLINABLE_FN("ArrayIteratorPrototypeOptimizable", |
| 1908 |
intrinsic_ArrayIteratorPrototypeOptimizable, 0, 0, |
| 1909 |
IntrinsicArrayIteratorPrototypeOptimizable), |
| 1910 |
JS_FN("AssertionFailed", intrinsic_AssertionFailed, 1, 0), |
| 1911 |
JS_FN("BigIntToNumber", intrinsic_BigIntToNumber, 1, 0), |
| 1912 |
JS_FN("CallArrayIteratorMethodIfWrapped", |
| 1913 |
CallNonGenericSelfhostedMethod<Is<ArrayIteratorObject>>, 2, 0), |
| 1914 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1915 |
JS_FN("CallAsyncDisposableStackMethodIfWrapped", |
| 1916 |
CallNonGenericSelfhostedMethod<Is<AsyncDisposableStackObject>>, 2, 0), |
| 1917 |
#endif |
| 1918 |
JS_FN("CallAsyncIteratorHelperMethodIfWrapped", |
| 1919 |
CallNonGenericSelfhostedMethod<Is<AsyncIteratorHelperObject>>, 2, 0), |
| 1920 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1921 |
JS_FN("CallDisposableStackMethodIfWrapped", |
| 1922 |
CallNonGenericSelfhostedMethod<Is<DisposableStackObject>>, 2, 0), |
| 1923 |
#endif |
| 1924 |
JS_FN("CallGeneratorMethodIfWrapped", |
| 1925 |
CallNonGenericSelfhostedMethod<Is<GeneratorObject>>, 2, 0), |
| 1926 |
JS_FN("CallIteratorHelperMethodIfWrapped", |
| 1927 |
CallNonGenericSelfhostedMethod<Is<IteratorHelperObject>>, 2, 0), |
| 1928 |
#ifdef NIGHTLY_BUILD |
| 1929 |
JS_FN("CallIteratorRangeMethodIfWrapped", |
| 1930 |
CallNonGenericSelfhostedMethod<Is<IteratorRangeObject>>, 2, 0), |
| 1931 |
#endif |
| 1932 |
JS_FN("CallMapIteratorMethodIfWrapped", |
| 1933 |
CallNonGenericSelfhostedMethod<Is<MapIteratorObject>>, 2, 0), |
| 1934 |
JS_FN("CallMapMethodIfWrapped", |
| 1935 |
CallNonGenericSelfhostedMethod<Is<MapObject>>, 2, 0), |
| 1936 |
JS_FN("CallRegExpMethodIfWrapped", |
| 1937 |
CallNonGenericSelfhostedMethod<Is<RegExpObject>>, 2, 0), |
| 1938 |
JS_FN("CallRegExpStringIteratorMethodIfWrapped", |
| 1939 |
CallNonGenericSelfhostedMethod<Is<RegExpStringIteratorObject>>, 2, 0), |
| 1940 |
JS_FN("CallSetIteratorMethodIfWrapped", |
| 1941 |
CallNonGenericSelfhostedMethod<Is<SetIteratorObject>>, 2, 0), |
| 1942 |
JS_FN("CallSetMethodIfWrapped", |
| 1943 |
CallNonGenericSelfhostedMethod<Is<SetObject>>, 2, 0), |
| 1944 |
JS_FN("CallStringIteratorMethodIfWrapped", |
| 1945 |
CallNonGenericSelfhostedMethod<Is<StringIteratorObject>>, 2, 0), |
| 1946 |
JS_FN("CallTypedArrayMethodIfWrapped", |
| 1947 |
CallNonGenericSelfhostedMethod<Is<TypedArrayObject>>, 2, 0), |
| 1948 |
JS_FN("CallWeakMapMethodIfWrapped", |
| 1949 |
CallNonGenericSelfhostedMethod<Is<WeakMapObject>>, 2, 0), |
| 1950 |
JS_FN("CallWrapForValidIteratorMethodIfWrapped", |
| 1951 |
CallNonGenericSelfhostedMethod<Is<WrapForValidIteratorObject>>, 2, 0), |
| 1952 |
JS_INLINABLE_FN("CanOptimizeArraySpecies", |
| 1953 |
intrinsic_CanOptimizeArraySpecies, 1, 0, |
| 1954 |
IntrinsicCanOptimizeArraySpecies), |
| 1955 |
JS_INLINABLE_FN("CanOptimizeStringProtoSymbolLookup", |
| 1956 |
intrinsic_CanOptimizeStringProtoSymbolLookup, 0, 0, |
| 1957 |
IntrinsicCanOptimizeStringProtoSymbolLookup), |
| 1958 |
JS_FN("ConstructFunction", intrinsic_ConstructFunction, 2, 0), |
| 1959 |
JS_FN("ConstructorForTypedArray", intrinsic_ConstructorForTypedArray, 1, 0), |
| 1960 |
JS_FN("CopyDataPropertiesOrGetOwnKeys", |
| 1961 |
intrinsic_CopyDataPropertiesOrGetOwnKeys, 3, 0), |
| 1962 |
JS_FN("CreateAsyncFromSyncIterator", intrinsic_CreateAsyncFromSyncIterator, |
| 1963 |
2, 0), |
| 1964 |
JS_FN("CreateMapIterationResultPair", |
| 1965 |
intrinsic_CreateMapIterationResultPair, 0, 0), |
| 1966 |
JS_FN("CreateSetIterationResult", intrinsic_CreateSetIterationResult, 0, 0), |
| 1967 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1968 |
JS_FN("CreateSuppressedError", intrinsic_CreateSuppressedError, 2, 0), |
| 1969 |
#endif |
| 1970 |
JS_FN("DecompileArg", intrinsic_DecompileArg, 2, 0), |
| 1971 |
JS_FN("DefineDataProperty", intrinsic_DefineDataProperty, 4, 0), |
| 1972 |
JS_FN("DefineProperty", intrinsic_DefineProperty, 6, 0), |
| 1973 |
JS_FN("DumpMessage", intrinsic_DumpMessage, 1, 0), |
| 1974 |
JS_FN("FlatStringMatch", FlatStringMatch, 2, 0), |
| 1975 |
JS_FN("FlatStringSearch", FlatStringSearch, 2, 0), |
| 1976 |
JS_FN("GeneratorIsRunning", intrinsic_GeneratorIsRunning, 1, 0), |
| 1977 |
JS_FN("GeneratorObjectIsClosed", intrinsic_GeneratorObjectIsClosed, 1, 0), |
| 1978 |
JS_FN("GeneratorSetClosed", intrinsic_GeneratorSetClosed, 1, 0), |
| 1979 |
JS_FN("GetElemBaseForLambda", intrinsic_GetElemBaseForLambda, 1, 0), |
| 1980 |
JS_INLINABLE_FN("GetFirstDollarIndex", GetFirstDollarIndex, 1, 0, |
| 1981 |
GetFirstDollarIndex), |
| 1982 |
JS_INLINABLE_FN("GetNextMapEntryForIterator", |
| 1983 |
intrinsic_GetNextMapEntryForIterator, 2, 0, |
| 1984 |
IntrinsicGetNextMapEntryForIterator), |
| 1985 |
JS_INLINABLE_FN("GetNextSetEntryForIterator", |
| 1986 |
intrinsic_GetNextSetEntryForIterator, 2, 0, |
| 1987 |
IntrinsicGetNextSetEntryForIterator), |
| 1988 |
JS_FN("GetOwnPropertyDescriptorToArray", GetOwnPropertyDescriptorToArray, 2, |
| 1989 |
0), |
| 1990 |
JS_FN("GetStringDataProperty", intrinsic_GetStringDataProperty, 2, 0), |
| 1991 |
JS_INLINABLE_FN("GuardToArrayBuffer", |
| 1992 |
intrinsic_GuardToBuiltin<ArrayBufferObject>, 1, 0, |
| 1993 |
IntrinsicGuardToArrayBuffer), |
| 1994 |
JS_INLINABLE_FN("GuardToArrayIterator", |
| 1995 |
intrinsic_GuardToBuiltin<ArrayIteratorObject>, 1, 0, |
| 1996 |
IntrinsicGuardToArrayIterator), |
| 1997 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1998 |
JS_INLINABLE_FN("GuardToAsyncDisposableStackHelper", |
| 1999 |
intrinsic_GuardToBuiltin<AsyncDisposableStackObject>, 1, 0, |
| 2000 |
IntrinsicGuardToAsyncDisposableStack), |
| 2001 |
#endif |
| 2002 |
JS_INLINABLE_FN("GuardToAsyncIteratorHelper", |
| 2003 |
intrinsic_GuardToBuiltin<AsyncIteratorHelperObject>, 1, 0, |
| 2004 |
IntrinsicGuardToAsyncIteratorHelper), |
| 2005 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 2006 |
JS_INLINABLE_FN("GuardToDisposableStackHelper", |
| 2007 |
intrinsic_GuardToBuiltin<DisposableStackObject>, 1, 0, |
| 2008 |
IntrinsicGuardToDisposableStack), |
| 2009 |
#endif |
| 2010 |
JS_INLINABLE_FN("GuardToIteratorHelper", |
| 2011 |
intrinsic_GuardToBuiltin<IteratorHelperObject>, 1, 0, |
| 2012 |
IntrinsicGuardToIteratorHelper), |
| 2013 |
#ifdef NIGHTLY_BUILD |
| 2014 |
JS_INLINABLE_FN("GuardToIteratorRange", |
| 2015 |
intrinsic_GuardToBuiltin<IteratorRangeObject>, 1, 0, |
| 2016 |
IntrinsicGuardToIteratorRange), |
| 2017 |
#endif |
| 2018 |
JS_INLINABLE_FN("GuardToMapIterator", |
| 2019 |
intrinsic_GuardToBuiltin<MapIteratorObject>, 1, 0, |
| 2020 |
IntrinsicGuardToMapIterator), |
| 2021 |
JS_INLINABLE_FN("GuardToMapObject", intrinsic_GuardToBuiltin<MapObject>, 1, |
| 2022 |
0, IntrinsicGuardToMapObject), |
| 2023 |
JS_INLINABLE_FN("GuardToRegExpStringIterator", |
| 2024 |
intrinsic_GuardToBuiltin<RegExpStringIteratorObject>, 1, 0, |
| 2025 |
IntrinsicGuardToRegExpStringIterator), |
| 2026 |
JS_INLINABLE_FN("GuardToSetIterator", |
| 2027 |
intrinsic_GuardToBuiltin<SetIteratorObject>, 1, 0, |
| 2028 |
IntrinsicGuardToSetIterator), |
| 2029 |
JS_INLINABLE_FN("GuardToSetObject", intrinsic_GuardToBuiltin<SetObject>, 1, |
| 2030 |
0, IntrinsicGuardToSetObject), |
| 2031 |
JS_INLINABLE_FN("GuardToSharedArrayBuffer", |
| 2032 |
intrinsic_GuardToBuiltin<SharedArrayBufferObject>, 1, 0, |
| 2033 |
IntrinsicGuardToSharedArrayBuffer), |
| 2034 |
JS_INLINABLE_FN("GuardToStringIterator", |
| 2035 |
intrinsic_GuardToBuiltin<StringIteratorObject>, 1, 0, |
| 2036 |
IntrinsicGuardToStringIterator), |
| 2037 |
JS_FN("GuardToWeakMapObject", intrinsic_GuardToBuiltin<WeakMapObject>, 1, |
| 2038 |
0), |
| 2039 |
JS_INLINABLE_FN("GuardToWrapForValidIterator", |
| 2040 |
intrinsic_GuardToBuiltin<WrapForValidIteratorObject>, 1, 0, |
| 2041 |
IntrinsicGuardToWrapForValidIterator), |
| 2042 |
JS_FN("IntrinsicAsyncGeneratorNext", AsyncGeneratorNext, 1, 0), |
| 2043 |
JS_FN("IntrinsicAsyncGeneratorReturn", AsyncGeneratorReturn, 1, 0), |
| 2044 |
JS_FN("IntrinsicAsyncGeneratorThrow", AsyncGeneratorThrow, 1, 0), |
| 2045 |
JS_INLINABLE_FN("IsArray", intrinsic_IsArray, 1, 0, ArrayIsArray), |
| 2046 |
JS_FN("IsAsyncFunctionGeneratorObject", |
| 2047 |
intrinsic_IsInstanceOfBuiltin<AsyncFunctionGeneratorObject>, 1, 0), |
| 2048 |
JS_FN("IsAsyncGeneratorObject", |
| 2049 |
intrinsic_IsInstanceOfBuiltin<AsyncGeneratorObject>, 1, 0), |
| 2050 |
JS_INLINABLE_FN("IsCallable", intrinsic_IsCallable, 1, 0, |
| 2051 |
IntrinsicIsCallable), |
| 2052 |
JS_INLINABLE_FN("IsConstructing", intrinsic_IsConstructing, 0, 0, |
| 2053 |
IntrinsicIsConstructing), |
| 2054 |
JS_INLINABLE_FN("IsConstructor", intrinsic_IsConstructor, 1, 0, |
| 2055 |
IntrinsicIsConstructor), |
| 2056 |
JS_INLINABLE_FN("IsCrossRealmArrayConstructor", |
| 2057 |
intrinsic_IsCrossRealmArrayConstructor, 1, 0, |
| 2058 |
IntrinsicIsCrossRealmArrayConstructor), |
| 2059 |
JS_FN("IsGeneratorObject", intrinsic_IsInstanceOfBuiltin<GeneratorObject>, |
| 2060 |
1, 0), |
| 2061 |
JS_INLINABLE_FN("IsObject", intrinsic_IsObject, 1, 0, IntrinsicIsObject), |
| 2062 |
JS_INLINABLE_FN("IsOptimizableRegExpObject", IsOptimizableRegExpObject, 1, |
| 2063 |
0, IsOptimizableRegExpObject), |
| 2064 |
JS_INLINABLE_FN("IsPackedArray", intrinsic_IsPackedArray, 1, 0, |
| 2065 |
IntrinsicIsPackedArray), |
| 2066 |
JS_INLINABLE_FN("IsPossiblyWrappedRegExpObject", |
| 2067 |
intrinsic_IsPossiblyWrappedInstanceOfBuiltin<RegExpObject>, |
| 2068 |
1, 0, IsPossiblyWrappedRegExpObject), |
| 2069 |
JS_INLINABLE_FN( |
| 2070 |
"IsPossiblyWrappedTypedArray", |
| 2071 |
intrinsic_IsPossiblyWrappedInstanceOfBuiltin<TypedArrayObject>, 1, 0, |
| 2072 |
IntrinsicIsPossiblyWrappedTypedArray), |
| 2073 |
JS_INLINABLE_FN("IsRegExpObject", |
| 2074 |
intrinsic_IsInstanceOfBuiltin<RegExpObject>, 1, 0, |
| 2075 |
IsRegExpObject), |
| 2076 |
JS_INLINABLE_FN("IsRegExpPrototypeOptimizable", |
| 2077 |
IsRegExpPrototypeOptimizable, 0, 0, |
| 2078 |
IsRegExpPrototypeOptimizable), |
| 2079 |
JS_INLINABLE_FN("IsSuspendedGenerator", intrinsic_IsSuspendedGenerator, 1, |
| 2080 |
0, IntrinsicIsSuspendedGenerator), |
| 2081 |
JS_INLINABLE_FN("IsTypedArray", |
| 2082 |
intrinsic_IsInstanceOfBuiltin<TypedArrayObject>, 1, 0, |
| 2083 |
IntrinsicIsTypedArray), |
| 2084 |
JS_INLINABLE_FN("IsTypedArrayConstructor", |
| 2085 |
intrinsic_IsTypedArrayConstructor, 1, 0, |
| 2086 |
IntrinsicIsTypedArrayConstructor), |
| 2087 |
JS_INLINABLE_FN("NewArrayIterator", intrinsic_NewArrayIterator, 0, 0, |
| 2088 |
IntrinsicNewArrayIterator), |
| 2089 |
JS_FN("NewAsyncIteratorHelper", intrinsic_NewAsyncIteratorHelper, 0, 0), |
| 2090 |
JS_FN("NewIteratorHelper", intrinsic_NewIteratorHelper, 0, 0), |
| 2091 |
#ifdef NIGHTLY_BUILD |
| 2092 |
JS_FN("NewIteratorRange", intrinsic_NewIteratorRange, 0, 0), |
| 2093 |
#endif |
| 2094 |
JS_INLINABLE_FN("NewRegExpStringIterator", |
| 2095 |
intrinsic_NewRegExpStringIterator, 0, 0, |
| 2096 |
IntrinsicNewRegExpStringIterator), |
| 2097 |
JS_INLINABLE_FN("NewStringIterator", intrinsic_NewStringIterator, 0, 0, |
| 2098 |
IntrinsicNewStringIterator), |
| 2099 |
JS_FN("NewWrapForValidIterator", intrinsic_NewWrapForValidIterator, 0, 0), |
| 2100 |
JS_FN("NoPrivateGetter", intrinsic_NoPrivateGetter, 1, 0), |
| 2101 |
JS_FN("NumberToBigInt", intrinsic_NumberToBigInt, 1, 0), |
| 2102 |
JS_FN("PossiblyWrappedTypedArrayHasDetachedBuffer", |
| 2103 |
intrinsic_PossiblyWrappedTypedArrayHasDetachedBuffer, 1, 0), |
| 2104 |
JS_INLINABLE_FN("PossiblyWrappedTypedArrayLength", |
| 2105 |
intrinsic_PossiblyWrappedTypedArrayLength, 1, 0, |
| 2106 |
IntrinsicPossiblyWrappedTypedArrayLength), |
| 2107 |
JS_FN("PromiseResolve", intrinsic_PromiseResolve, 2, 0), |
| 2108 |
JS_INLINABLE_FN("RegExpBuiltinExec", intrinsic_RegExpBuiltinExec<false>, 2, |
| 2109 |
0, IntrinsicRegExpBuiltinExec), |
| 2110 |
JS_INLINABLE_FN("RegExpBuiltinExecForTest", |
| 2111 |
intrinsic_RegExpBuiltinExec<true>, 2, 0, |
| 2112 |
IntrinsicRegExpBuiltinExecForTest), |
| 2113 |
JS_FN("RegExpConstructRaw", regexp_construct_raw_flags, 2, 0), |
| 2114 |
JS_FN("RegExpCreate", intrinsic_RegExpCreate, 2, 0), |
| 2115 |
JS_INLINABLE_FN("RegExpExec", intrinsic_RegExpExec<false>, 2, 0, |
| 2116 |
IntrinsicRegExpExec), |
| 2117 |
JS_INLINABLE_FN("RegExpExecForTest", intrinsic_RegExpExec<true>, 2, 0, |
| 2118 |
IntrinsicRegExpExecForTest), |
| 2119 |
JS_FN("RegExpGetSubstitution", intrinsic_RegExpGetSubstitution, 5, 0), |
| 2120 |
JS_INLINABLE_FN("RegExpHasCaptureGroups", intrinsic_RegExpHasCaptureGroups, |
| 2121 |
2, 0, RegExpHasCaptureGroups), |
| 2122 |
JS_INLINABLE_FN("RegExpMatcher", RegExpMatcher, 3, 0, RegExpMatcher), |
| 2123 |
JS_INLINABLE_FN("RegExpSearcher", RegExpSearcher, 3, 0, RegExpSearcher), |
| 2124 |
JS_INLINABLE_FN("RegExpSearcherLastLimit", RegExpSearcherLastLimit, 0, 0, |
| 2125 |
RegExpSearcherLastLimit), |
| 2126 |
JS_FN("RegExpSymbolProtocolOnPrimitiveCounter", |
| 2127 |
intrinsic_RegExpSymbolProtocolOnPrimitiveCounter, 0, 0), |
| 2128 |
JS_INLINABLE_FN("SameValue", js::obj_is, 2, 0, ObjectIs), |
| 2129 |
JS_FN("SetCopy", SetObject::copy, 1, 0), |
| 2130 |
JS_FN("StringReplaceAllString", intrinsic_StringReplaceAllString, 3, 0), |
| 2131 |
JS_INLINABLE_FN("StringReplaceString", intrinsic_StringReplaceString, 3, 0, |
| 2132 |
IntrinsicStringReplaceString), |
| 2133 |
JS_INLINABLE_FN("StringSplitString", intrinsic_StringSplitString, 2, 0, |
| 2134 |
IntrinsicStringSplitString), |
| 2135 |
JS_FN("StringSplitStringLimit", intrinsic_StringSplitStringLimit, 3, 0), |
| 2136 |
JS_INLINABLE_FN("SubstringKernel", intrinsic_SubstringKernel, 3, 0, |
| 2137 |
IntrinsicSubstringKernel), |
| 2138 |
JS_FN("ThisNumberValueForToLocaleString", ThisNumberValueForToLocaleString, |
| 2139 |
0, 0), |
| 2140 |
JS_INLINABLE_FN("ThisTimeValue", intrinsic_ThisTimeValue, 1, 0, |
| 2141 |
IntrinsicThisTimeValue), |
| 2142 |
JS_FN("ThrowAggregateError", intrinsic_ThrowAggregateError, 4, 0), |
| 2143 |
JS_FN("ThrowInternalError", intrinsic_ThrowInternalError, 4, 0), |
| 2144 |
JS_FN("ThrowRangeError", intrinsic_ThrowRangeError, 4, 0), |
| 2145 |
JS_FN("ThrowTypeError", intrinsic_ThrowTypeError, 4, 0), |
| 2146 |
JS_FN("ToBigInt", intrinsic_ToBigInt, 1, 0), |
| 2147 |
JS_INLINABLE_FN("ToInteger", intrinsic_ToInteger, 1, 0, IntrinsicToInteger), |
| 2148 |
JS_INLINABLE_FN("ToLength", intrinsic_ToLength, 1, 0, IntrinsicToLength), |
| 2149 |
JS_INLINABLE_FN("ToObject", intrinsic_ToObject, 1, 0, IntrinsicToObject), |
| 2150 |
JS_FN("ToPropertyKey", intrinsic_ToPropertyKey, 1, 0), |
| 2151 |
JS_FN("ToSource", intrinsic_ToSource, 1, 0), |
| 2152 |
#ifdef JS_HAS_INTL_API |
| 2153 |
JS_FN("ToTemporalDuration", intrinsic_ToTemporalDuration, 1, 0), |
| 2154 |
#endif |
| 2155 |
JS_FN("TypedArrayBitwiseSlice", intrinsic_TypedArrayBitwiseSlice, 4, 0), |
| 2156 |
JS_FN("TypedArrayBuffer", intrinsic_TypedArrayBuffer, 1, 0), |
| 2157 |
JS_INLINABLE_FN("TypedArrayByteOffset", intrinsic_TypedArrayByteOffset, 1, |
| 2158 |
0, IntrinsicTypedArrayByteOffset), |
| 2159 |
JS_INLINABLE_FN("TypedArrayElementSize", intrinsic_TypedArrayElementSize, 1, |
| 2160 |
0, IntrinsicTypedArrayElementSize), |
| 2161 |
JS_FN("TypedArrayInitFromPackedArray", |
| 2162 |
intrinsic_TypedArrayInitFromPackedArray, 2, 0), |
| 2163 |
JS_FN("TypedArrayIsAutoLength", intrinsic_TypedArrayIsAutoLength, 1, 0), |
| 2164 |
JS_INLINABLE_FN("TypedArrayLength", intrinsic_TypedArrayLength, 1, 0, |
| 2165 |
IntrinsicTypedArrayLength), |
| 2166 |
JS_INLINABLE_FN("TypedArrayLengthZeroOnOutOfBounds", |
| 2167 |
intrinsic_TypedArrayLengthZeroOnOutOfBounds, 1, 0, |
| 2168 |
IntrinsicTypedArrayLengthZeroOnOutOfBounds), |
| 2169 |
JS_INLINABLE_FN("UnsafeGetInt32FromReservedSlot", |
| 2170 |
intrinsic_UnsafeGetInt32FromReservedSlot, 2, 0, |
| 2171 |
IntrinsicUnsafeGetInt32FromReservedSlot), |
| 2172 |
JS_INLINABLE_FN("UnsafeGetObjectFromReservedSlot", |
| 2173 |
intrinsic_UnsafeGetObjectFromReservedSlot, 2, 0, |
| 2174 |
IntrinsicUnsafeGetObjectFromReservedSlot), |
| 2175 |
JS_INLINABLE_FN("UnsafeGetReservedSlot", intrinsic_UnsafeGetReservedSlot, 2, |
| 2176 |
0, IntrinsicUnsafeGetReservedSlot), |
| 2177 |
JS_INLINABLE_FN("UnsafeGetStringFromReservedSlot", |
| 2178 |
intrinsic_UnsafeGetStringFromReservedSlot, 2, 0, |
| 2179 |
IntrinsicUnsafeGetStringFromReservedSlot), |
| 2180 |
JS_INLINABLE_FN("UnsafeSetReservedSlot", intrinsic_UnsafeSetReservedSlot, 3, |
| 2181 |
0, IntrinsicUnsafeSetReservedSlot), |
| 2182 |
|
| 2183 |
// Intrinsics and standard functions used by Intl API implementation. |
| 2184 |
#ifdef JS_HAS_INTL_API |
| 2185 |
JS_FN("intl_BestAvailableLocale", intl_BestAvailableLocale, 3, 0), |
| 2186 |
JS_FN("intl_CallCollatorMethodIfWrapped", |
| 2187 |
CallNonGenericSelfhostedMethod<Is<CollatorObject>>, 2, 0), |
| 2188 |
JS_FN("intl_CallDateTimeFormatMethodIfWrapped", |
| 2189 |
CallNonGenericSelfhostedMethod<Is<DateTimeFormatObject>>, 2, 0), |
| 2190 |
JS_FN("intl_CallDisplayNamesMethodIfWrapped", |
| 2191 |
CallNonGenericSelfhostedMethod<Is<DisplayNamesObject>>, 2, 0), |
| 2192 |
JS_FN("intl_CallDurationFormatMethodIfWrapped", |
| 2193 |
CallNonGenericSelfhostedMethod<Is<DurationFormatObject>>, 2, 0), |
| 2194 |
JS_FN("intl_CallListFormatMethodIfWrapped", |
| 2195 |
CallNonGenericSelfhostedMethod<Is<ListFormatObject>>, 2, 0), |
| 2196 |
JS_FN("intl_CallNumberFormatMethodIfWrapped", |
| 2197 |
CallNonGenericSelfhostedMethod<Is<NumberFormatObject>>, 2, 0), |
| 2198 |
JS_FN("intl_CallPluralRulesMethodIfWrapped", |
| 2199 |
CallNonGenericSelfhostedMethod<Is<PluralRulesObject>>, 2, 0), |
| 2200 |
JS_FN("intl_CallRelativeTimeFormatMethodIfWrapped", |
| 2201 |
CallNonGenericSelfhostedMethod<Is<RelativeTimeFormatObject>>, 2, 0), |
| 2202 |
JS_FN("intl_CallSegmentIteratorMethodIfWrapped", |
| 2203 |
CallNonGenericSelfhostedMethod<Is<SegmentIteratorObject>>, 2, 0), |
| 2204 |
JS_FN("intl_CallSegmenterMethodIfWrapped", |
| 2205 |
CallNonGenericSelfhostedMethod<Is<SegmenterObject>>, 2, 0), |
| 2206 |
JS_FN("intl_CallSegmentsMethodIfWrapped", |
| 2207 |
CallNonGenericSelfhostedMethod<Is<SegmentsObject>>, 2, 0), |
| 2208 |
JS_FN("intl_Collator", intl_Collator, 2, 0), |
| 2209 |
JS_FN("intl_CompareStrings", intl_CompareStrings, 3, 0), |
| 2210 |
JS_FN("intl_ComputeDisplayName", intl_ComputeDisplayName, 6, 0), |
| 2211 |
JS_FN("intl_CreateDateTimeFormat", intl_CreateDateTimeFormat, 4, 0), |
| 2212 |
JS_FN("intl_CreateSegmentIterator", intl_CreateSegmentIterator, 1, 0), |
| 2213 |
JS_FN("intl_CreateSegmentsObject", intl_CreateSegmentsObject, 2, 0), |
| 2214 |
JS_FN("intl_FindNextSegmentBoundaries", intl_FindNextSegmentBoundaries, 1, |
| 2215 |
0), |
| 2216 |
JS_FN("intl_FindSegmentBoundaries", intl_FindSegmentBoundaries, 2, 0), |
| 2217 |
JS_FN("intl_FormatDateTime", intl_FormatDateTime, 2, 0), |
| 2218 |
JS_FN("intl_FormatDateTimeRange", intl_FormatDateTimeRange, 4, 0), |
| 2219 |
JS_FN("intl_FormatList", intl_FormatList, 3, 0), |
| 2220 |
JS_FN("intl_FormatNumber", intl_FormatNumber, 3, 0), |
| 2221 |
JS_FN("intl_FormatNumberRange", intl_FormatNumberRange, 4, 0), |
| 2222 |
JS_FN("intl_FormatRelativeTime", intl_FormatRelativeTime, 4, 0), |
| 2223 |
JS_FN("intl_GetCalendarInfo", intl_GetCalendarInfo, 1, 0), |
| 2224 |
JS_FN("intl_GetPluralCategories", intl_GetPluralCategories, 1, 0), |
| 2225 |
JS_FN("intl_GetTimeSeparator", intl_GetTimeSeparator, 2, 0), |
| 2226 |
JS_INLINABLE_FN("intl_GuardToCollator", |
| 2227 |
intrinsic_GuardToBuiltin<CollatorObject>, 1, 0, |
| 2228 |
IntlGuardToCollator), |
| 2229 |
JS_INLINABLE_FN("intl_GuardToDateTimeFormat", |
| 2230 |
intrinsic_GuardToBuiltin<DateTimeFormatObject>, 1, 0, |
| 2231 |
IntlGuardToDateTimeFormat), |
| 2232 |
JS_INLINABLE_FN("intl_GuardToDisplayNames", |
| 2233 |
intrinsic_GuardToBuiltin<DisplayNamesObject>, 1, 0, |
| 2234 |
IntlGuardToDisplayNames), |
| 2235 |
JS_INLINABLE_FN("intl_GuardToDurationFormat", |
| 2236 |
intrinsic_GuardToBuiltin<DurationFormatObject>, 1, 0, |
| 2237 |
IntlGuardToDurationFormat), |
| 2238 |
JS_INLINABLE_FN("intl_GuardToListFormat", |
| 2239 |
intrinsic_GuardToBuiltin<ListFormatObject>, 1, 0, |
| 2240 |
IntlGuardToListFormat), |
| 2241 |
JS_INLINABLE_FN("intl_GuardToNumberFormat", |
| 2242 |
intrinsic_GuardToBuiltin<NumberFormatObject>, 1, 0, |
| 2243 |
IntlGuardToNumberFormat), |
| 2244 |
JS_INLINABLE_FN("intl_GuardToPluralRules", |
| 2245 |
intrinsic_GuardToBuiltin<PluralRulesObject>, 1, 0, |
| 2246 |
IntlGuardToPluralRules), |
| 2247 |
JS_INLINABLE_FN("intl_GuardToRelativeTimeFormat", |
| 2248 |
intrinsic_GuardToBuiltin<RelativeTimeFormatObject>, 1, 0, |
| 2249 |
IntlGuardToRelativeTimeFormat), |
| 2250 |
JS_INLINABLE_FN("intl_GuardToSegmentIterator", |
| 2251 |
intrinsic_GuardToBuiltin<SegmentIteratorObject>, 1, 0, |
| 2252 |
IntlGuardToSegmentIterator), |
| 2253 |
JS_INLINABLE_FN("intl_GuardToSegmenter", |
| 2254 |
intrinsic_GuardToBuiltin<SegmenterObject>, 1, 0, |
| 2255 |
IntlGuardToSegmenter), |
| 2256 |
JS_INLINABLE_FN("intl_GuardToSegments", |
| 2257 |
intrinsic_GuardToBuiltin<SegmentsObject>, 1, 0, |
| 2258 |
IntlGuardToSegments), |
| 2259 |
JS_FN("intl_IsRuntimeDefaultLocale", intrinsic_IsRuntimeDefaultLocale, 1, |
| 2260 |
0), |
| 2261 |
JS_FN("intl_IsValidTimeZoneName", intl_IsValidTimeZoneName, 1, 0), |
| 2262 |
JS_FN("intl_IsWrappedDateTimeFormat", |
| 2263 |
intrinsic_IsWrappedInstanceOfBuiltin<DateTimeFormatObject>, 1, 0), |
| 2264 |
JS_FN("intl_IsWrappedNumberFormat", |
| 2265 |
intrinsic_IsWrappedInstanceOfBuiltin<NumberFormatObject>, 1, 0), |
| 2266 |
JS_FN("intl_NumberFormat", intl_NumberFormat, 2, 0), |
| 2267 |
JS_FN("intl_RuntimeDefaultLocale", intrinsic_RuntimeDefaultLocale, 0, 0), |
| 2268 |
JS_FN("intl_SelectPluralRule", intl_SelectPluralRule, 2, 0), |
| 2269 |
JS_FN("intl_SelectPluralRuleRange", intl_SelectPluralRuleRange, 3, 0), |
| 2270 |
JS_FN("intl_SupportedValuesOf", intl_SupportedValuesOf, 1, 0), |
| 2271 |
JS_FN("intl_TryValidateAndCanonicalizeLanguageTag", |
| 2272 |
intl_TryValidateAndCanonicalizeLanguageTag, 1, 0), |
| 2273 |
JS_FN("intl_ValidateAndCanonicalizeLanguageTag", |
| 2274 |
intl_ValidateAndCanonicalizeLanguageTag, 2, 0), |
| 2275 |
JS_FN("intl_ValidateAndCanonicalizeUnicodeExtensionType", |
| 2276 |
intl_ValidateAndCanonicalizeUnicodeExtensionType, 3, 0), |
| 2277 |
JS_FN("intl_availableCalendars", intl_availableCalendars, 1, 0), |
| 2278 |
JS_FN("intl_availableCollations", intl_availableCollations, 1, 0), |
| 2279 |
# if DEBUG || MOZ_SYSTEM_ICU |
| 2280 |
JS_FN("intl_availableMeasurementUnits", intl_availableMeasurementUnits, 0, |
| 2281 |
0), |
| 2282 |
# endif |
| 2283 |
JS_FN("intl_canonicalizeTimeZone", intl_canonicalizeTimeZone, 1, 0), |
| 2284 |
JS_FN("intl_defaultCalendar", intl_defaultCalendar, 1, 0), |
| 2285 |
JS_FN("intl_defaultTimeZone", intl_defaultTimeZone, 0, 0), |
| 2286 |
JS_FN("intl_defaultTimeZoneOffset", intl_defaultTimeZoneOffset, 0, 0), |
| 2287 |
JS_FN("intl_isDefaultTimeZone", intl_isDefaultTimeZone, 1, 0), |
| 2288 |
JS_FN("intl_isIgnorePunctuation", intl_isIgnorePunctuation, 1, 0), |
| 2289 |
JS_FN("intl_isUpperCaseFirst", intl_isUpperCaseFirst, 1, 0), |
| 2290 |
JS_FN("intl_numberingSystem", intl_numberingSystem, 1, 0), |
| 2291 |
JS_FN("intl_resolveDateTimeFormatComponents", |
| 2292 |
intl_resolveDateTimeFormatComponents, 3, 0), |
| 2293 |
JS_FN("intl_supportedLocaleOrFallback", intl_supportedLocaleOrFallback, 1, |
| 2294 |
0), |
| 2295 |
JS_FN("intl_toLocaleLowerCase", intl_toLocaleLowerCase, 2, 0), |
| 2296 |
JS_FN("intl_toLocaleUpperCase", intl_toLocaleUpperCase, 2, 0), |
| 2297 |
#endif // JS_HAS_INTL_API |
| 2298 |
|
| 2299 |
// Standard builtins used by self-hosting. |
| 2300 |
JS_FN("new_List", intrinsic_newList, 0, 0), |
| 2301 |
JS_INLINABLE_FN("std_Array", array_construct, 1, 0, Array), |
| 2302 |
JS_FN("std_Array_includes", array_includes, 1, 0), |
| 2303 |
JS_FN("std_Array_indexOf", array_indexOf, 1, 0), |
| 2304 |
JS_FN("std_Array_lastIndexOf", array_lastIndexOf, 1, 0), |
| 2305 |
JS_INLINABLE_FN("std_Array_pop", array_pop, 0, 0, ArrayPop), |
| 2306 |
JS_TRAMPOLINE_FN("std_Array_sort", array_sort, 1, 0, ArraySort), |
| 2307 |
JS_FN("std_BigInt_valueOf", BigIntObject::valueOf, 0, 0), |
| 2308 |
JS_FN("std_Function_apply", fun_apply, 2, 0), |
| 2309 |
JS_FN("std_Map_entries", MapObject::entries, 0, 0), |
| 2310 |
JS_FN("std_Map_get", MapObject::get, 1, 0), |
| 2311 |
JS_FN("std_Map_has", MapObject::has, 1, 0), |
| 2312 |
JS_FN("std_Map_set", MapObject::set, 2, 0), |
| 2313 |
JS_INLINABLE_FN("std_Math_abs", math_abs, 1, 0, MathAbs), |
| 2314 |
JS_INLINABLE_FN("std_Math_floor", math_floor, 1, 0, MathFloor), |
| 2315 |
JS_INLINABLE_FN("std_Math_max", math_max, 2, 0, MathMax), |
| 2316 |
JS_INLINABLE_FN("std_Math_min", math_min, 2, 0, MathMin), |
| 2317 |
JS_INLINABLE_FN("std_Math_sign", math_sign, 1, 0, MathSign), |
| 2318 |
JS_INLINABLE_FN("std_Math_trunc", math_trunc, 1, 0, MathTrunc), |
| 2319 |
JS_INLINABLE_FN("std_Object_create", obj_create, 2, 0, ObjectCreate), |
| 2320 |
JS_INLINABLE_FN("std_Object_isPrototypeOf", obj_isPrototypeOf, 1, 0, |
| 2321 |
ObjectIsPrototypeOf), |
| 2322 |
JS_FN("std_Object_propertyIsEnumerable", obj_propertyIsEnumerable, 1, 0), |
| 2323 |
JS_FN("std_Object_setProto", obj_setProto, 1, 0), |
| 2324 |
JS_FN("std_Object_toString", obj_toString, 0, 0), |
| 2325 |
JS_INLINABLE_FN("std_Reflect_getPrototypeOf", Reflect_getPrototypeOf, 1, 0, |
| 2326 |
ReflectGetPrototypeOf), |
| 2327 |
JS_FN("std_Reflect_isExtensible", Reflect_isExtensible, 1, 0), |
| 2328 |
JS_FN("std_Reflect_ownKeys", Reflect_ownKeys, 1, 0), |
| 2329 |
JS_FN("std_Set_add", SetObject::add, 1, 0), |
| 2330 |
JS_FN("std_Set_delete", SetObject::delete_, 1, 0), |
| 2331 |
JS_INLINABLE_FN("std_Set_has", SetObject::has, 1, 0, SetHas), |
| 2332 |
JS_INLINABLE_FN("std_Set_size", SetObject::size, 1, 0, SetSize), |
| 2333 |
JS_FN("std_Set_values", SetObject::values, 0, 0), |
| 2334 |
JS_INLINABLE_FN("std_String_charCodeAt", str_charCodeAt, 1, 0, |
| 2335 |
StringCharCodeAt), |
| 2336 |
JS_INLINABLE_FN("std_String_codePointAt", str_codePointAt, 1, 0, |
| 2337 |
StringCodePointAt), |
| 2338 |
JS_INLINABLE_FN("std_String_endsWith", str_endsWith, 1, 0, StringEndsWith), |
| 2339 |
JS_INLINABLE_FN("std_String_fromCharCode", str_fromCharCode, 1, 0, |
| 2340 |
StringFromCharCode), |
| 2341 |
JS_INLINABLE_FN("std_String_fromCodePoint", str_fromCodePoint, 1, 0, |
| 2342 |
StringFromCodePoint), |
| 2343 |
JS_FN("std_String_includes", str_includes, 1, 0), |
| 2344 |
JS_INLINABLE_FN("std_String_indexOf", str_indexOf, 1, 0, StringIndexOf), |
| 2345 |
JS_INLINABLE_FN("std_String_startsWith", str_startsWith, 1, 0, |
| 2346 |
StringStartsWith), |
| 2347 |
JS_TRAMPOLINE_FN("std_TypedArray_sort", TypedArrayObject::sort, 1, 0, |
| 2348 |
TypedArraySort), |
| 2349 |
JS_FN("std_WeakMap_get", WeakMapObject::get, 1, 0), |
| 2350 |
JS_FN("std_WeakMap_has", WeakMapObject::has, 1, 0), |
| 2351 |
JS_FN("std_WeakMap_set", WeakMapObject::set, 2, 0), |
| 2352 |
|
| 2353 |
JS_FS_END, |
| 2354 |
}; |
| 2355 |
|
| 2356 |
#ifdef DEBUG |
| 2357 |
|
| 2358 |
static void CheckSelfHostedIntrinsics() { |
| 2359 |
// The `intrinsic_functions` list must be sorted so that we can use |
| 2360 |
// mozilla::BinarySearch to do lookups on demand. |
| 2361 |
const char* prev = ""; |
| 2362 |
for (JSFunctionSpec spec : intrinsic_functions) { |
| 2363 |
if (spec.name.string()) { |
| 2364 |
MOZ_ASSERT(strcmp(prev, spec.name.string()) < 0, |
| 2365 |
"Self-hosted intrinsics must be sorted"); |
| 2366 |
prev = spec.name.string(); |
| 2367 |
} |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
class CheckTenuredTracer : public JS::CallbackTracer { |
| 2372 |
HashSet<gc::Cell*, DefaultHasher<gc::Cell*>, SystemAllocPolicy> visited; |
| 2373 |
Vector<JS::GCCellPtr, 0, SystemAllocPolicy> stack; |
| 2374 |
|
| 2375 |
public: |
| 2376 |
explicit CheckTenuredTracer(JSRuntime* rt) : JS::CallbackTracer(rt) {} |
| 2377 |
void check() { |
| 2378 |
while (!stack.empty()) { |
| 2379 |
JS::TraceChildren(this, stack.popCopy()); |
| 2380 |
} |
| 2381 |
} |
| 2382 |
void onChild(JS::GCCellPtr thing, const char* name) override { |
| 2383 |
gc::Cell* cell = thing.asCell(); |
| 2384 |
MOZ_RELEASE_ASSERT(cell->isTenured(), "Expected tenured cell"); |
| 2385 |
if (!visited.has(cell)) { |
| 2386 |
if (!visited.put(cell) || !stack.append(thing)) { |
| 2387 |
// Ignore OOM. This can happen during fuzzing. |
| 2388 |
return; |
| 2389 |
} |
| 2390 |
} |
| 2391 |
} |
| 2392 |
}; |
| 2393 |
|
| 2394 |
static void CheckSelfHostingDataIsTenured(JSRuntime* rt) { |
| 2395 |
// Check everything is tenured as we don't trace it when collecting the |
| 2396 |
// nursery. |
| 2397 |
CheckTenuredTracer trc(rt); |
| 2398 |
rt->traceSelfHostingStencil(&trc); |
| 2399 |
trc.check(); |
| 2400 |
} |
| 2401 |
|
| 2402 |
#endif |
| 2403 |
|
| 2404 |
const JSFunctionSpec* js::FindIntrinsicSpec(js::PropertyName* name) { |
| 2405 |
size_t limit = std::size(intrinsic_functions) - 1; |
| 2406 |
MOZ_ASSERT(!intrinsic_functions[limit].name); |
| 2407 |
|
| 2408 |
MOZ_ASSERT(name->hasLatin1Chars()); |
| 2409 |
|
| 2410 |
JS::AutoCheckCannotGC nogc; |
| 2411 |
const char* chars = reinterpret_cast<const char*>(name->latin1Chars(nogc)); |
| 2412 |
size_t len = name->length(); |
| 2413 |
|
| 2414 |
// NOTE: CheckSelfHostedIntrinsics checks that the intrinsic_functions list is |
| 2415 |
// sorted appropriately so that we can use binary search here. |
| 2416 |
|
| 2417 |
size_t loc = 0; |
| 2418 |
bool match = mozilla::BinarySearchIf( |
| 2419 |
intrinsic_functions, 0, limit, |
| 2420 |
[chars, len](const JSFunctionSpec& spec) { |
| 2421 |
// The spec string is null terminated but the `name` string is not, so |
| 2422 |
// compare chars up until the length of `name`. Since the `name` string |
| 2423 |
// does not contain any nulls, seeing the null terminator of the spec |
| 2424 |
// string will terminate the loop appropriately. A final comparison |
| 2425 |
// against null is needed to determine if the spec string has an extra |
| 2426 |
// suffix. |
| 2427 |
const char* spec_chars = spec.name.string(); |
| 2428 |
for (size_t i = 0; i < len; ++i) { |
| 2429 |
if (auto cmp_result = int(chars[i]) - int(spec_chars[i])) { |
| 2430 |
return cmp_result; |
| 2431 |
} |
| 2432 |
} |
| 2433 |
return int('\0') - int(spec_chars[len]); |
| 2434 |
}, |
| 2435 |
&loc); |
| 2436 |
if (match) { |
| 2437 |
return &intrinsic_functions[loc]; |
| 2438 |
} |
| 2439 |
return nullptr; |
| 2440 |
} |
| 2441 |
|
| 2442 |
void js::FillSelfHostingCompileOptions(CompileOptions& options) { |
| 2443 |
/* |
| 2444 |
* In self-hosting mode, scripts use JSOp::GetIntrinsic instead of |
| 2445 |
* JSOp::GetName or JSOp::GetGName to access unbound variables. |
| 2446 |
* JSOp::GetIntrinsic does a name lookup on a special object, whose |
| 2447 |
* properties are filled in lazily upon first access for a given global. |
| 2448 |
* |
| 2449 |
* As that object is inaccessible to client code, the lookups are |
| 2450 |
* guaranteed to return the original objects, ensuring safe implementation |
| 2451 |
* of self-hosted builtins. |
| 2452 |
* |
| 2453 |
* Additionally, the special syntax callFunction(fun, receiver, ...args) |
| 2454 |
* is supported, for which bytecode is emitted that invokes |fun| with |
| 2455 |
* |receiver| as the this-object and ...args as the arguments. |
| 2456 |
*/ |
| 2457 |
options.setIntroductionType("self-hosted"); |
| 2458 |
options.setFileAndLine("self-hosted", 1); |
| 2459 |
options.setSkipFilenameValidation(true); |
| 2460 |
options.setSelfHostingMode(true); |
| 2461 |
options.setForceFullParse(); |
| 2462 |
options.setForceStrictMode(); |
| 2463 |
options.setDiscardSource(); |
| 2464 |
options.setIsRunOnce(true); |
| 2465 |
options.setNoScriptRval(true); |
| 2466 |
} |
| 2467 |
|
| 2468 |
// Report all errors and warnings to stderr because it is too early in the |
| 2469 |
// startup process for any other error reporting to be used, and we don't want |
| 2470 |
// errors in self-hosted code to be silently swallowed. |
| 2471 |
class MOZ_STACK_CLASS AutoPrintSelfHostingFrontendContext |
| 2472 |
: public FrontendContext { |
| 2473 |
JSContext* cx_; |
| 2474 |
|
| 2475 |
public: |
| 2476 |
explicit AutoPrintSelfHostingFrontendContext(JSContext* cx) : cx_(cx) { |
| 2477 |
setCurrentJSContext(cx_); |
| 2478 |
} |
| 2479 |
~AutoPrintSelfHostingFrontendContext() { |
| 2480 |
// TODO: Remove this once JSContext is removed from frontend. |
| 2481 |
MaybePrintAndClearPendingException(cx_); |
| 2482 |
|
| 2483 |
if (hadOutOfMemory()) { |
| 2484 |
fprintf(stderr, "Out of memory\n"); |
| 2485 |
} |
| 2486 |
|
| 2487 |
if (maybeError()) { |
| 2488 |
JS::PrintError(stderr, &*maybeError(), true); |
| 2489 |
} |
| 2490 |
for (CompileError& error : warnings()) { |
| 2491 |
JS::PrintError(stderr, &error, true); |
| 2492 |
} |
| 2493 |
if (hadOverRecursed()) { |
| 2494 |
fprintf(stderr, "Over recursed\n"); |
| 2495 |
} |
| 2496 |
if (hadAllocationOverflow()) { |
| 2497 |
fprintf(stderr, "Allocation overflow\n"); |
| 2498 |
} |
| 2499 |
} |
| 2500 |
}; |
| 2501 |
|
| 2502 |
[[nodiscard]] static bool InitSelfHostingFromStencil( |
| 2503 |
JSContext* cx, frontend::CompilationAtomCache& atomCache, |
| 2504 |
const frontend::CompilationStencil& stencil) { |
| 2505 |
// Build the JSAtom -> ScriptIndexRange mapping and save on the runtime. |
| 2506 |
{ |
| 2507 |
auto& scriptMap = cx->runtime()->selfHostScriptMap.ref(); |
| 2508 |
|
| 2509 |
// We don't easily know the number of top-level functions, so use the total |
| 2510 |
// number of stencil functions instead. There is very little nesting of |
| 2511 |
// functions in self-hosted code so this is a good approximation. |
| 2512 |
size_t numSelfHostedScripts = stencil.scriptData.size(); |
| 2513 |
if (!scriptMap.reserve(numSelfHostedScripts)) { |
| 2514 |
ReportOutOfMemory(cx); |
| 2515 |
return false; |
| 2516 |
} |
| 2517 |
|
| 2518 |
auto topLevelThings = |
| 2519 |
stencil.scriptData[frontend::CompilationStencil::TopLevelIndex] |
| 2520 |
.gcthings(stencil); |
| 2521 |
|
| 2522 |
// Iterate over the (named) top-level functions. We record the ScriptIndex |
| 2523 |
// as well as the ScriptIndex of the next top-level function. Scripts |
| 2524 |
// between these two indices are the inner functions of the first one. We |
| 2525 |
// only record named scripts here since they are what might be looked up. |
| 2526 |
Rooted<JSAtom*> prevAtom(cx); |
| 2527 |
frontend::ScriptIndex prevIndex; |
| 2528 |
for (frontend::TaggedScriptThingIndex thing : topLevelThings) { |
| 2529 |
if (!thing.isFunction()) { |
| 2530 |
continue; |
| 2531 |
} |
| 2532 |
|
| 2533 |
frontend::ScriptIndex index = thing.toFunction(); |
| 2534 |
const auto& script = stencil.scriptData[index]; |
| 2535 |
|
| 2536 |
if (prevAtom) { |
| 2537 |
frontend::ScriptIndexRange range{prevIndex, index}; |
| 2538 |
scriptMap.putNewInfallible(prevAtom, range); |
| 2539 |
} |
| 2540 |
|
| 2541 |
prevAtom = script.functionAtom |
| 2542 |
? atomCache.getExistingAtomAt(cx, script.functionAtom) |
| 2543 |
: nullptr; |
| 2544 |
prevIndex = index; |
| 2545 |
} |
| 2546 |
if (prevAtom) { |
| 2547 |
frontend::ScriptIndexRange range{ |
| 2548 |
prevIndex, frontend::ScriptIndex(stencil.scriptData.size())}; |
| 2549 |
scriptMap.putNewInfallible(prevAtom, range); |
| 2550 |
} |
| 2551 |
|
| 2552 |
// We over-estimated the capacity of `scriptMap`, so check that the estimate |
| 2553 |
// hasn't drifted too hasn't drifted too far since this was written. If this |
| 2554 |
// assert fails, we may need a new way to size the `scriptMap`. |
| 2555 |
MOZ_ASSERT(numSelfHostedScripts < (scriptMap.count() * 1.15)); |
| 2556 |
} |
| 2557 |
|
| 2558 |
#ifdef DEBUG |
| 2559 |
// Check that the list of intrinsics is well-formed. |
| 2560 |
CheckSelfHostedIntrinsics(); |
| 2561 |
CheckSelfHostingDataIsTenured(cx->runtime()); |
| 2562 |
#endif |
| 2563 |
|
| 2564 |
return true; |
| 2565 |
} |
| 2566 |
|
| 2567 |
bool JSRuntime::initSelfHostingStencil(JSContext* cx, |
| 2568 |
JS::SelfHostedCache xdrCache, |
| 2569 |
JS::SelfHostedWriter xdrWriter) { |
| 2570 |
if (parentRuntime) { |
| 2571 |
MOZ_RELEASE_ASSERT( |
| 2572 |
parentRuntime->hasInitializedSelfHosting(), |
| 2573 |
"Parent runtime must initialize self-hosting before workers"); |
| 2574 |
|
| 2575 |
selfHostStencilInput_ = parentRuntime->selfHostStencilInput_; |
| 2576 |
selfHostStencil_ = parentRuntime->selfHostStencil_; |
| 2577 |
return true; |
| 2578 |
} |
| 2579 |
auto start = mozilla::TimeStamp::Now(); |
| 2580 |
|
| 2581 |
// Variables used to instantiate scripts. |
| 2582 |
CompileOptions options(cx); |
| 2583 |
FillSelfHostingCompileOptions(options); |
| 2584 |
|
| 2585 |
// Try initializing from Stencil XDR. |
| 2586 |
AutoPrintSelfHostingFrontendContext fc(cx); |
| 2587 |
if (xdrCache.Length() > 0) { |
| 2588 |
// Allow the VM to directly use bytecode from the XDR buffer without |
| 2589 |
// copying it. The buffer must outlive all runtimes (including workers). |
| 2590 |
options.borrowBuffer = true; |
| 2591 |
options.usePinnedBytecode = true; |
| 2592 |
|
| 2593 |
Rooted<UniquePtr<frontend::CompilationInput>> input( |
| 2594 |
cx, cx->new_<frontend::CompilationInput>(options)); |
| 2595 |
if (!input) { |
| 2596 |
return false; |
| 2597 |
} |
| 2598 |
{ |
| 2599 |
AutoReportFrontendContext fc(cx); |
| 2600 |
if (!input->initForSelfHostingGlobal(&fc)) { |
| 2601 |
return false; |
| 2602 |
} |
| 2603 |
} |
| 2604 |
|
| 2605 |
JS::DecodeOptions decodeOption(options); |
| 2606 |
RefPtr<frontend::CompilationStencil> stencil; |
| 2607 |
JS::TranscodeResult result = |
| 2608 |
js::DecodeStencil(&fc, decodeOption, xdrCache, getter_AddRefs(stencil)); |
| 2609 |
if (result == JS::TranscodeResult::Ok) { |
| 2610 |
MOZ_ASSERT(input->atomCache.empty()); |
| 2611 |
|
| 2612 |
MOZ_ASSERT(!hasSelfHostStencil()); |
| 2613 |
|
| 2614 |
// Move it to the runtime. |
| 2615 |
setSelfHostingStencil(&input, std::move(stencil)); |
| 2616 |
|
| 2617 |
auto end = mozilla::TimeStamp::Now(); |
| 2618 |
JS_LOG(startup, Info, |
| 2619 |
"Used XDR for process self-hosted startup. Took %f us", |
| 2620 |
(end - start).ToMicroseconds()); |
| 2621 |
return true; |
| 2622 |
} |
| 2623 |
} |
| 2624 |
|
| 2625 |
// If script wasn't generated, it means XDR was either not provided or that it |
| 2626 |
// failed the decoding phase. Parse from text as before. |
| 2627 |
uint32_t srcLen = GetRawScriptsSize(); |
| 2628 |
const unsigned char* compressed = compressedSources; |
| 2629 |
uint32_t compressedLen = GetCompressedSize(); |
| 2630 |
auto src = cx->make_pod_array<char>(srcLen); |
| 2631 |
if (!src) { |
| 2632 |
return false; |
| 2633 |
} |
| 2634 |
if (!DecompressString(compressed, compressedLen, |
| 2635 |
reinterpret_cast<unsigned char*>(src.get()), srcLen)) { |
| 2636 |
return false; |
| 2637 |
} |
| 2638 |
|
| 2639 |
JS::SourceText<mozilla::Utf8Unit> srcBuf; |
| 2640 |
if (!srcBuf.init(cx, std::move(src), srcLen)) { |
| 2641 |
return false; |
| 2642 |
} |
| 2643 |
|
| 2644 |
Rooted<UniquePtr<frontend::CompilationInput>> input( |
| 2645 |
cx, cx->new_<frontend::CompilationInput>(options)); |
| 2646 |
if (!input) { |
| 2647 |
return false; |
| 2648 |
} |
| 2649 |
frontend::NoScopeBindingCache scopeCache; |
| 2650 |
RefPtr<frontend::CompilationStencil> stencil = |
| 2651 |
frontend::CompileGlobalScriptToStencilWithInput( |
| 2652 |
cx, &fc, cx->tempLifoAlloc(), *input, &scopeCache, srcBuf, |
| 2653 |
ScopeKind::Global); |
| 2654 |
if (!stencil) { |
| 2655 |
return false; |
| 2656 |
} |
| 2657 |
|
| 2658 |
mozilla::TimeDuration xdrDuration; |
| 2659 |
// Serialize the stencil to XDR. |
| 2660 |
if (xdrWriter) { |
| 2661 |
auto encodeStart = mozilla::TimeStamp::Now(); |
| 2662 |
JS::TranscodeBuffer xdrBuffer; |
| 2663 |
JS::TranscodeResult result = js::EncodeStencil(cx, stencil, xdrBuffer); |
| 2664 |
if (result != JS::TranscodeResult::Ok) { |
| 2665 |
JS_ReportErrorASCII(cx, "Encoding failure"); |
| 2666 |
return false; |
| 2667 |
} |
| 2668 |
|
| 2669 |
if (!xdrWriter(cx, xdrBuffer)) { |
| 2670 |
return false; |
| 2671 |
} |
| 2672 |
auto encodeEnd = mozilla::TimeStamp::Now(); |
| 2673 |
xdrDuration = (encodeEnd - encodeStart); |
| 2674 |
JS_LOG(startup, Info, "Saved XDR Buffer. Took %f us", |
| 2675 |
xdrDuration.ToMicroseconds()); |
| 2676 |
} |
| 2677 |
|
| 2678 |
MOZ_ASSERT(input->atomCache.empty()); |
| 2679 |
|
| 2680 |
MOZ_ASSERT(!hasSelfHostStencil()); |
| 2681 |
|
| 2682 |
// Move it to the runtime. |
| 2683 |
setSelfHostingStencil(&input, std::move(stencil)); |
| 2684 |
|
| 2685 |
auto end = mozilla::TimeStamp::Now(); |
| 2686 |
JS_LOG(startup, Info, |
| 2687 |
"Used source text for process self-hosted startup. Took %f us (%f us " |
| 2688 |
"XDR encode)", |
| 2689 |
(end - start).ToMicroseconds(), xdrDuration.ToMicroseconds()); |
| 2690 |
return true; |
| 2691 |
} |
| 2692 |
|
| 2693 |
void JSRuntime::setSelfHostingStencil( |
| 2694 |
MutableHandle<UniquePtr<frontend::CompilationInput>> input, |
| 2695 |
RefPtr<frontend::CompilationStencil>&& stencil) { |
| 2696 |
MOZ_ASSERT(!selfHostStencilInput_); |
| 2697 |
MOZ_ASSERT(!selfHostStencil_); |
| 2698 |
|
| 2699 |
selfHostStencilInput_ = input.release(); |
| 2700 |
selfHostStencil_ = stencil.forget().take(); |
| 2701 |
|
| 2702 |
#ifdef DEBUG |
| 2703 |
CheckSelfHostingDataIsTenured(this); |
| 2704 |
#endif |
| 2705 |
} |
| 2706 |
|
| 2707 |
bool JSRuntime::initSelfHostingFromStencil(JSContext* cx) { |
| 2708 |
return InitSelfHostingFromStencil( |
| 2709 |
cx, cx->runtime()->selfHostStencilInput_->atomCache, |
| 2710 |
*cx->runtime()->selfHostStencil_); |
| 2711 |
} |
| 2712 |
|
| 2713 |
void JSRuntime::finishSelfHosting() { |
| 2714 |
if (!parentRuntime) { |
| 2715 |
js_delete(selfHostStencilInput_.ref()); |
| 2716 |
if (selfHostStencil_) { |
| 2717 |
// delete selfHostStencil_ by decrementing the ref-count of the last |
| 2718 |
// instance. |
| 2719 |
RefPtr<frontend::CompilationStencil> stencil; |
| 2720 |
*getter_AddRefs(stencil) = selfHostStencil_; |
| 2721 |
MOZ_ASSERT(!stencil->hasMultipleReference()); |
| 2722 |
} |
| 2723 |
} |
| 2724 |
|
| 2725 |
selfHostStencilInput_ = nullptr; |
| 2726 |
selfHostStencil_ = nullptr; |
| 2727 |
|
| 2728 |
selfHostScriptMap.ref().clear(); |
| 2729 |
clearSelfHostedJitCache(); |
| 2730 |
} |
| 2731 |
|
| 2732 |
void JSRuntime::clearSelfHostedJitCache() { |
| 2733 |
for (auto iter = selfHostJitCache.ref().iter(); !iter.done(); iter.next()) { |
| 2734 |
jit::BaselineScript* baselineScript = iter.get().value(); |
| 2735 |
jit::BaselineScript::Destroy(gcContext(), baselineScript); |
| 2736 |
} |
| 2737 |
selfHostJitCache.ref().clear(); |
| 2738 |
} |
| 2739 |
|
| 2740 |
void JSRuntime::traceSelfHostingStencil(JSTracer* trc) { |
| 2741 |
if (selfHostStencilInput_.ref()) { |
| 2742 |
selfHostStencilInput_->trace(trc); |
| 2743 |
} |
| 2744 |
selfHostScriptMap.ref().trace(trc); |
| 2745 |
selfHostJitCache.ref().trace(trc); |
| 2746 |
} |
| 2747 |
|
| 2748 |
GeneratorKind JSRuntime::getSelfHostedFunctionGeneratorKind( |
| 2749 |
js::PropertyName* name) { |
| 2750 |
frontend::ScriptIndex index = getSelfHostedScriptIndexRange(name)->start; |
| 2751 |
auto flags = selfHostStencil().scriptExtra[index].immutableFlags; |
| 2752 |
return flags.hasFlag(js::ImmutableScriptFlagsEnum::IsGenerator) |
| 2753 |
? GeneratorKind::Generator |
| 2754 |
: GeneratorKind::NotGenerator; |
| 2755 |
} |
| 2756 |
|
| 2757 |
// Returns the ScriptSourceObject to use for cloned self-hosted scripts in the |
| 2758 |
// current realm. |
| 2759 |
ScriptSourceObject* js::SelfHostingScriptSourceObject(JSContext* cx) { |
| 2760 |
return GlobalObject::getOrCreateSelfHostingScriptSourceObject(cx, |
| 2761 |
cx->global()); |
| 2762 |
} |
| 2763 |
|
| 2764 |
/* static */ |
| 2765 |
ScriptSourceObject* GlobalObject::getOrCreateSelfHostingScriptSourceObject( |
| 2766 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 2767 |
MOZ_ASSERT(cx->global() == global); |
| 2768 |
|
| 2769 |
if (ScriptSourceObject* sso = global->data().selfHostingScriptSource) { |
| 2770 |
return sso; |
| 2771 |
} |
| 2772 |
|
| 2773 |
CompileOptions options(cx); |
| 2774 |
FillSelfHostingCompileOptions(options); |
| 2775 |
|
| 2776 |
RefPtr<ScriptSource> source(cx->new_<ScriptSource>()); |
| 2777 |
if (!source) { |
| 2778 |
return nullptr; |
| 2779 |
} |
| 2780 |
|
| 2781 |
Rooted<ScriptSourceObject*> sourceObject(cx); |
| 2782 |
{ |
| 2783 |
AutoReportFrontendContext fc(cx); |
| 2784 |
if (!source->initFromOptions(&fc, options)) { |
| 2785 |
return nullptr; |
| 2786 |
} |
| 2787 |
|
| 2788 |
sourceObject = ScriptSourceObject::create(cx, source.get()); |
| 2789 |
if (!sourceObject) { |
| 2790 |
return nullptr; |
| 2791 |
} |
| 2792 |
|
| 2793 |
JS::InstantiateOptions instantiateOptions(options); |
| 2794 |
if (!ScriptSourceObject::initFromOptions(cx, sourceObject, |
| 2795 |
instantiateOptions)) { |
| 2796 |
return nullptr; |
| 2797 |
} |
| 2798 |
|
| 2799 |
global->data().selfHostingScriptSource.init(sourceObject); |
| 2800 |
} |
| 2801 |
|
| 2802 |
return sourceObject; |
| 2803 |
} |
| 2804 |
|
| 2805 |
bool JSRuntime::delazifySelfHostedFunction(JSContext* cx, |
| 2806 |
Handle<PropertyName*> name, |
| 2807 |
HandleFunction targetFun) { |
| 2808 |
MOZ_ASSERT(targetFun->isExtended()); |
| 2809 |
MOZ_ASSERT(targetFun->hasSelfHostedLazyScript()); |
| 2810 |
|
| 2811 |
auto indexRange = *getSelfHostedScriptIndexRange(name); |
| 2812 |
auto& stencil = cx->runtime()->selfHostStencil(); |
| 2813 |
|
| 2814 |
if (!stencil.delazifySelfHostedFunction( |
| 2815 |
cx, cx->runtime()->selfHostStencilInput().atomCache, indexRange, name, |
| 2816 |
targetFun)) { |
| 2817 |
return false; |
| 2818 |
} |
| 2819 |
|
| 2820 |
// Relazifiable self-hosted functions may be relazified later into a |
| 2821 |
// SelfHostedLazyScript, dropping the BaseScript entirely. This only applies |
| 2822 |
// to named function being delazified. Inner functions used by self-hosting |
| 2823 |
// are never relazified. |
| 2824 |
BaseScript* targetScript = targetFun->baseScript(); |
| 2825 |
if (targetScript->isRelazifiable()) { |
| 2826 |
targetScript->setAllowRelazify(); |
| 2827 |
} |
| 2828 |
|
| 2829 |
return true; |
| 2830 |
} |
| 2831 |
|
| 2832 |
mozilla::Maybe<frontend::ScriptIndexRange> |
| 2833 |
JSRuntime::getSelfHostedScriptIndexRange(js::PropertyName* name) { |
| 2834 |
if (parentRuntime) { |
| 2835 |
return parentRuntime->getSelfHostedScriptIndexRange(name); |
| 2836 |
} |
| 2837 |
MOZ_ASSERT(name->isPermanentAndMayBeShared()); |
| 2838 |
if (auto ptr = selfHostScriptMap.ref().readonlyThreadsafeLookup(name)) { |
| 2839 |
return mozilla::Some(ptr->value()); |
| 2840 |
} |
| 2841 |
return mozilla::Nothing(); |
| 2842 |
} |
| 2843 |
|
| 2844 |
static bool GetComputedIntrinsic(JSContext* cx, Handle<PropertyName*> name, |
| 2845 |
MutableHandleValue vp) { |
| 2846 |
// If the intrinsic was not in hardcoded set, run the top-level of the |
| 2847 |
// selfhosted script. This will generate values and call `SetIntrinsic` to |
| 2848 |
// save them on a special "computed intrinsics holder". We then can check for |
| 2849 |
// our required values and cache on the normal intrinsics holder. |
| 2850 |
|
| 2851 |
Rooted<NativeObject*> computedIntrinsicsHolder( |
| 2852 |
cx, cx->global()->getComputedIntrinsicsHolder()); |
| 2853 |
if (!computedIntrinsicsHolder) { |
| 2854 |
auto computedIntrinsicHolderGuard = mozilla::MakeScopeExit( |
| 2855 |
[cx]() { cx->global()->setComputedIntrinsicsHolder(nullptr); }); |
| 2856 |
|
| 2857 |
// Instantiate a script in current realm from the shared Stencil. |
| 2858 |
JSRuntime* runtime = cx->runtime(); |
| 2859 |
RootedScript script( |
| 2860 |
cx, runtime->selfHostStencil().instantiateSelfHostedTopLevelForRealm( |
| 2861 |
cx, runtime->selfHostStencilInput())); |
| 2862 |
if (!script) { |
| 2863 |
return false; |
| 2864 |
} |
| 2865 |
|
| 2866 |
// Attach the computed intrinsics holder to the global now to capture |
| 2867 |
// generated values. |
| 2868 |
computedIntrinsicsHolder = |
| 2869 |
NewPlainObjectWithProto(cx, nullptr, TenuredObject); |
| 2870 |
if (!computedIntrinsicsHolder) { |
| 2871 |
return false; |
| 2872 |
} |
| 2873 |
cx->global()->setComputedIntrinsicsHolder(computedIntrinsicsHolder); |
| 2874 |
|
| 2875 |
// Disable the interrupt callback while executing the top-level script. |
| 2876 |
// This prevents recursive calls to GetComputedIntrinsic through the |
| 2877 |
// interrupt callback. |
| 2878 |
bool hadInterruptsDisabled = JS_DisableInterruptCallback(cx); |
| 2879 |
auto resetInterrupts = mozilla::MakeScopeExit( |
| 2880 |
[&]() { JS_ResetInterruptCallback(cx, hadInterruptsDisabled); }); |
| 2881 |
|
| 2882 |
// Attempt to execute the top-level script. If they fails to run to |
| 2883 |
// successful completion, throw away the holder to avoid a partial |
| 2884 |
// initialization state. |
| 2885 |
if (!JS_ExecuteScript(cx, script)) { |
| 2886 |
return false; |
| 2887 |
} |
| 2888 |
|
| 2889 |
// Successfully ran the self-host top-level in current realm, so these |
| 2890 |
// computed intrinsic values are now source of truth for the realm. |
| 2891 |
computedIntrinsicHolderGuard.release(); |
| 2892 |
} |
| 2893 |
|
| 2894 |
// Cache the individual intrinsic on the standard holder object so that we |
| 2895 |
// only have to look for it in one place when performing `GetIntrinsic`. |
| 2896 |
mozilla::Maybe<PropertyInfo> prop = |
| 2897 |
computedIntrinsicsHolder->lookup(cx, name); |
| 2898 |
#ifdef DEBUG |
| 2899 |
if (!prop) { |
| 2900 |
Fprinter out(stderr); |
| 2901 |
out.printf("SelfHosted intrinsic not found: "); |
| 2902 |
name->dumpPropertyName(out); |
| 2903 |
out.printf("\n"); |
| 2904 |
} |
| 2905 |
#endif |
| 2906 |
MOZ_RELEASE_ASSERT(prop, "SelfHosted intrinsic not found"); |
| 2907 |
RootedValue value(cx, computedIntrinsicsHolder->getSlot(prop->slot())); |
| 2908 |
return GlobalObject::addIntrinsicValue(cx, cx->global(), name, value); |
| 2909 |
} |
| 2910 |
|
| 2911 |
bool JSRuntime::getSelfHostedValue(JSContext* cx, Handle<PropertyName*> name, |
| 2912 |
MutableHandleValue vp) { |
| 2913 |
// If the self-hosted value we want is a function in the stencil, instantiate |
| 2914 |
// a lazy self-hosted function for it. This is typical when a self-hosted |
| 2915 |
// function calls other self-hosted helper functions. |
| 2916 |
if (auto index = getSelfHostedScriptIndexRange(name)) { |
| 2917 |
JSFunction* fun = |
| 2918 |
cx->runtime()->selfHostStencil().instantiateSelfHostedLazyFunction( |
| 2919 |
cx, cx->runtime()->selfHostStencilInput().atomCache, index->start, |
| 2920 |
name); |
| 2921 |
if (!fun) { |
| 2922 |
return false; |
| 2923 |
} |
| 2924 |
vp.setObject(*fun); |
| 2925 |
return true; |
| 2926 |
} |
| 2927 |
|
| 2928 |
return GetComputedIntrinsic(cx, name, vp); |
| 2929 |
} |
| 2930 |
|
| 2931 |
void JSRuntime::assertSelfHostedFunctionHasCanonicalName( |
| 2932 |
Handle<PropertyName*> name) { |
| 2933 |
#ifdef DEBUG |
| 2934 |
frontend::ScriptIndex index = getSelfHostedScriptIndexRange(name)->start; |
| 2935 |
MOZ_ASSERT(selfHostStencil().scriptData[index].hasSelfHostedCanonicalName()); |
| 2936 |
#endif |
| 2937 |
} |
| 2938 |
|
| 2939 |
bool js::IsSelfHostedFunctionWithName(JSFunction* fun, JSAtom* name) { |
| 2940 |
return fun->isSelfHostedBuiltin() && fun->isExtended() && |
| 2941 |
GetClonedSelfHostedFunctionName(fun) == name; |
| 2942 |
} |
| 2943 |
|
| 2944 |
bool js::IsSelfHostedFunctionWithName(const Value& v, JSAtom* name) { |
| 2945 |
if (!v.isObject() || !v.toObject().is<JSFunction>()) { |
| 2946 |
return false; |
| 2947 |
} |
| 2948 |
JSFunction* fun = &v.toObject().as<JSFunction>(); |
| 2949 |
return IsSelfHostedFunctionWithName(fun, name); |
| 2950 |
} |
| 2951 |
|
| 2952 |
static_assert( |
| 2953 |
JSString::MAX_LENGTH <= INT32_MAX, |
| 2954 |
"StringIteratorNext in builtin/String.js assumes the stored index " |
| 2955 |
"into the string is an Int32Value"); |
| 2956 |
|
| 2957 |
static_assert(JSString::MAX_LENGTH == MAX_STRING_LENGTH, |
| 2958 |
"JSString::MAX_LENGTH matches self-hosted constant for maximum " |
| 2959 |
"string length"); |
| 2960 |
|
| 2961 |
static_assert(ARGS_LENGTH_MAX == MAX_ARGS_LENGTH, |
| 2962 |
"ARGS_LENGTH_MAX matches self-hosted constant for maximum " |
| 2963 |
"arguments length"); |