| 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 |
#ifndef vm_GlobalObject_h |
| 8 |
#define vm_GlobalObject_h |
| 9 |
|
| 10 |
#include "js/GlobalObject.h" |
| 11 |
|
| 12 |
#include "mozilla/Assertions.h" |
| 13 |
#include "mozilla/EnumeratedArray.h" |
| 14 |
|
| 15 |
#include <stdint.h> |
| 16 |
#include <type_traits> |
| 17 |
|
| 18 |
#include "jsexn.h" |
| 19 |
#include "jsfriendapi.h" |
| 20 |
#include "jspubtd.h" |
| 21 |
#include "jstypes.h" |
| 22 |
#include "NamespaceImports.h" |
| 23 |
|
| 24 |
#include "gc/AllocKind.h" |
| 25 |
#include "js/CallArgs.h" |
| 26 |
#include "js/Class.h" |
| 27 |
#include "js/ErrorReport.h" |
| 28 |
#include "js/PropertyDescriptor.h" |
| 29 |
#include "js/RootingAPI.h" |
| 30 |
#include "js/TypeDecls.h" |
| 31 |
#include "js/Value.h" |
| 32 |
#include "vm/ArrayObject.h" |
| 33 |
#include "vm/JSAtomState.h" |
| 34 |
#include "vm/JSContext.h" |
| 35 |
#include "vm/JSFunction.h" |
| 36 |
#include "vm/JSObject.h" |
| 37 |
#include "vm/NativeObject.h" |
| 38 |
#include "vm/Realm.h" |
| 39 |
#include "vm/RegExpShared.h" |
| 40 |
#include "vm/Shape.h" |
| 41 |
#include "vm/StringType.h" |
| 42 |
|
| 43 |
struct JSFunctionSpec; |
| 44 |
class JSJitInfo; |
| 45 |
struct JSPrincipals; |
| 46 |
struct JSPropertySpec; |
| 47 |
|
| 48 |
namespace JS { |
| 49 |
class JS_PUBLIC_API RealmOptions; |
| 50 |
}; |
| 51 |
|
| 52 |
namespace js { |
| 53 |
|
| 54 |
class ArgumentsObject; |
| 55 |
class GlobalScope; |
| 56 |
class GlobalLexicalEnvironmentObject; |
| 57 |
class MapObject; |
| 58 |
class PlainObject; |
| 59 |
class PropertyIteratorObject; |
| 60 |
class RegExpStatics; |
| 61 |
class SetObject; |
| 62 |
|
| 63 |
namespace gc { |
| 64 |
class FinalizationRegistryGlobalData; |
| 65 |
} // namespace gc |
| 66 |
|
| 67 |
// Fixed slot capacities for PlainObjects. The global has a cached Shape for |
| 68 |
// PlainObject with default prototype for each of these values. |
| 69 |
enum class PlainObjectSlotsKind { |
| 70 |
Slots0, |
| 71 |
Slots2, |
| 72 |
Slots4, |
| 73 |
Slots8, |
| 74 |
Slots12, |
| 75 |
Slots16, |
| 76 |
Limit |
| 77 |
}; |
| 78 |
|
| 79 |
static PlainObjectSlotsKind PlainObjectSlotsKindFromAllocKind( |
| 80 |
gc::AllocKind kind) { |
| 81 |
switch (kind) { |
| 82 |
case gc::AllocKind::OBJECT0: |
| 83 |
return PlainObjectSlotsKind::Slots0; |
| 84 |
case gc::AllocKind::OBJECT2: |
| 85 |
return PlainObjectSlotsKind::Slots2; |
| 86 |
case gc::AllocKind::OBJECT4: |
| 87 |
return PlainObjectSlotsKind::Slots4; |
| 88 |
case gc::AllocKind::OBJECT8: |
| 89 |
return PlainObjectSlotsKind::Slots8; |
| 90 |
case gc::AllocKind::OBJECT12: |
| 91 |
return PlainObjectSlotsKind::Slots12; |
| 92 |
case gc::AllocKind::OBJECT16: |
| 93 |
return PlainObjectSlotsKind::Slots16; |
| 94 |
default: |
| 95 |
break; |
| 96 |
} |
| 97 |
MOZ_CRASH("Invalid kind"); |
| 98 |
} |
| 99 |
|
| 100 |
// Data attached to a GlobalObject. This is freed when clearing the Realm's |
| 101 |
// global_ only because this way we don't need to add a finalizer to all |
| 102 |
// GlobalObject JSClasses. |
| 103 |
class GlobalObjectData { |
| 104 |
friend class js::GlobalObject; |
| 105 |
|
| 106 |
GlobalObjectData(const GlobalObjectData&) = delete; |
| 107 |
void operator=(const GlobalObjectData&) = delete; |
| 108 |
|
| 109 |
public: |
| 110 |
explicit GlobalObjectData(Zone* zone); |
| 111 |
|
| 112 |
~GlobalObjectData(); |
| 113 |
|
| 114 |
// The original values for built-in constructors (with their prototype |
| 115 |
// objects) based on JSProtoKey. |
| 116 |
// |
| 117 |
// This is necessary to implement spec language speaking in terms of "the |
| 118 |
// original Array prototype object", or "as if by the expression new Array()" |
| 119 |
// referring to the original Array constructor. The actual (writable and even |
| 120 |
// deletable) Object, Array, &c. properties are not stored here. |
| 121 |
struct ConstructorWithProto { |
| 122 |
GCPtr<JSObject*> constructor; |
| 123 |
GCPtr<JSObject*> prototype; |
| 124 |
}; |
| 125 |
using CtorArray = mozilla::EnumeratedArray<JSProtoKey, ConstructorWithProto, |
| 126 |
size_t(JSProto_LIMIT)>; |
| 127 |
CtorArray builtinConstructors; |
| 128 |
|
| 129 |
// Built-in prototypes for this global. Note that this is different from the |
| 130 |
// set of built-in constructors/prototypes based on JSProtoKey. |
| 131 |
enum class ProtoKind { |
| 132 |
ArrayIteratorProto, |
| 133 |
StringIteratorProto, |
| 134 |
RegExpStringIteratorProto, |
| 135 |
GeneratorObjectProto, |
| 136 |
AsyncIteratorProto, |
| 137 |
AsyncFromSyncIteratorProto, |
| 138 |
AsyncGeneratorProto, |
| 139 |
MapIteratorProto, |
| 140 |
SetIteratorProto, |
| 141 |
WrapForValidIteratorProto, |
| 142 |
IteratorHelperProto, |
| 143 |
AsyncIteratorHelperProto, |
| 144 |
SegmentsProto, |
| 145 |
SegmentIteratorProto, |
| 146 |
#ifdef NIGHTLY_BUILD |
| 147 |
IteratorRangeProto, |
| 148 |
#endif |
| 149 |
Limit |
| 150 |
}; |
| 151 |
using ProtoArray = mozilla::EnumeratedArray<ProtoKind, GCPtr<JSObject*>, |
| 152 |
size_t(ProtoKind::Limit)>; |
| 153 |
ProtoArray builtinProtos; |
| 154 |
|
| 155 |
GCPtr<GlobalScope*> emptyGlobalScope; |
| 156 |
|
| 157 |
// The lexical environment for global let/const/class bindings. |
| 158 |
GCPtr<GlobalLexicalEnvironmentObject*> lexicalEnvironment; |
| 159 |
|
| 160 |
// The WindowProxy associated with this global. |
| 161 |
GCPtr<JSObject*> windowProxy; |
| 162 |
|
| 163 |
// Functions and other top-level values for self-hosted code. The "computed" |
| 164 |
// holder is used as the target of `SetIntrinsic` calls, but the same property |
| 165 |
// may also be cached on the normal intrinsics holder for `GetIntrinsic`. |
| 166 |
GCPtr<NativeObject*> intrinsicsHolder; |
| 167 |
GCPtr<NativeObject*> computedIntrinsicsHolder; |
| 168 |
|
| 169 |
// List of source URLs for this realm. This is used by the debugger. |
| 170 |
GCPtr<ArrayObject*> sourceURLsHolder; |
| 171 |
|
| 172 |
// Realm-specific object that can be used as key in WeakMaps. |
| 173 |
GCPtr<PlainObject*> realmKeyObject; |
| 174 |
|
| 175 |
// The unique %ThrowTypeError% function for this global. |
| 176 |
GCPtr<JSFunction*> throwTypeError; |
| 177 |
|
| 178 |
// The unique %eval% function (for indirect eval) for this global. |
| 179 |
GCPtr<JSFunction*> eval; |
| 180 |
|
| 181 |
// Empty iterator object used for for-in with null/undefined. |
| 182 |
GCPtr<PropertyIteratorObject*> emptyIterator; |
| 183 |
|
| 184 |
// Cached shape for new arrays with Array.prototype as prototype. |
| 185 |
GCPtr<SharedShape*> arrayShapeWithDefaultProto; |
| 186 |
|
| 187 |
// Shape for PlainObject with %Object.prototype% as proto, for each object |
| 188 |
// AllocKind. |
| 189 |
using PlainObjectShapeArray = |
| 190 |
mozilla::EnumeratedArray<PlainObjectSlotsKind, GCPtr<SharedShape*>, |
| 191 |
size_t(PlainObjectSlotsKind::Limit)>; |
| 192 |
PlainObjectShapeArray plainObjectShapesWithDefaultProto; |
| 193 |
|
| 194 |
// Shape for JSFunction with %Function.prototype% as proto, for both |
| 195 |
// non-extended and extended functions. |
| 196 |
GCPtr<SharedShape*> functionShapeWithDefaultProto; |
| 197 |
GCPtr<SharedShape*> extendedFunctionShapeWithDefaultProto; |
| 198 |
|
| 199 |
// Shape for BoundFunctionObject with %Function.prototype% as proto. |
| 200 |
GCPtr<SharedShape*> boundFunctionShapeWithDefaultProto; |
| 201 |
|
| 202 |
// Shape for RegExpObject with %RegExp.prototype% as proto. |
| 203 |
GCPtr<SharedShape*> regExpShapeWithDefaultProto; |
| 204 |
|
| 205 |
// Global state for regular expressions. |
| 206 |
RegExpRealm regExpRealm; |
| 207 |
|
| 208 |
GCPtr<ArgumentsObject*> mappedArgumentsTemplate; |
| 209 |
GCPtr<ArgumentsObject*> unmappedArgumentsTemplate; |
| 210 |
|
| 211 |
// Template objects to speed up allocation of Map/Set objects. |
| 212 |
GCPtr<MapObject*> mapObjectTemplate; |
| 213 |
GCPtr<SetObject*> setObjectTemplate; |
| 214 |
|
| 215 |
GCPtr<PlainObject*> iterResultTemplate; |
| 216 |
GCPtr<PlainObject*> iterResultWithoutPrototypeTemplate; |
| 217 |
|
| 218 |
// Lazily initialized script source object to use for scripts cloned from the |
| 219 |
// self-hosting stencil. |
| 220 |
GCPtr<ScriptSourceObject*> selfHostingScriptSource; |
| 221 |
|
| 222 |
UniquePtr<gc::FinalizationRegistryGlobalData> finalizationRegistryData; |
| 223 |
|
| 224 |
// The number of times that one of the following has occurred: |
| 225 |
// 1. A property of this GlobalObject is deleted. |
| 226 |
// 2. A data property of this GlobalObject is converted to an accessor, |
| 227 |
// or vice versa. |
| 228 |
// 3. A property is defined on the global lexical that shadows a property on |
| 229 |
// this GlobalObject. |
| 230 |
uint32_t generationCount = 0; |
| 231 |
|
| 232 |
// Whether the |globalThis| property has been resolved on the global object. |
| 233 |
bool globalThisResolved = false; |
| 234 |
|
| 235 |
void trace(JSTracer* trc, GlobalObject* global); |
| 236 |
void addSizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf, |
| 237 |
JS::ClassInfo* info) const; |
| 238 |
|
| 239 |
static constexpr size_t offsetOfLexicalEnvironment() { |
| 240 |
static_assert(sizeof(lexicalEnvironment) == sizeof(uintptr_t), |
| 241 |
"JIT code assumes field is pointer-sized"); |
| 242 |
return offsetof(GlobalObjectData, lexicalEnvironment); |
| 243 |
} |
| 244 |
static constexpr size_t offsetOfRegExpRealm() { |
| 245 |
return offsetof(GlobalObjectData, regExpRealm); |
| 246 |
} |
| 247 |
}; |
| 248 |
|
| 249 |
class GlobalObject : public NativeObject { |
| 250 |
enum : unsigned { |
| 251 |
GLOBAL_DATA_SLOT = JSCLASS_GLOBAL_APPLICATION_SLOTS, |
| 252 |
|
| 253 |
// Total reserved-slot count for global objects. |
| 254 |
RESERVED_SLOTS |
| 255 |
}; |
| 256 |
|
| 257 |
// The slot count must be in the public API for JSCLASS_GLOBAL_FLAGS, and |
| 258 |
// we won't expose GlobalObject, so just assert that the two values are |
| 259 |
// synchronized. |
| 260 |
static_assert(JSCLASS_GLOBAL_SLOT_COUNT == RESERVED_SLOTS, |
| 261 |
"global object slot counts are inconsistent"); |
| 262 |
|
| 263 |
// Ensure GlobalObjectData is only one dereference away. |
| 264 |
static_assert(GLOBAL_DATA_SLOT < MAX_FIXED_SLOTS, |
| 265 |
"GlobalObjectData should be stored in a fixed slot for " |
| 266 |
"performance reasons"); |
| 267 |
|
| 268 |
public: |
| 269 |
using ProtoKind = GlobalObjectData::ProtoKind; |
| 270 |
|
| 271 |
private: |
| 272 |
GlobalObjectData* maybeData() { |
| 273 |
Value v = getReservedSlot(GLOBAL_DATA_SLOT); |
| 274 |
return static_cast<GlobalObjectData*>(v.toPrivate()); |
| 275 |
} |
| 276 |
const GlobalObjectData* maybeData() const { |
| 277 |
Value v = getReservedSlot(GLOBAL_DATA_SLOT); |
| 278 |
return static_cast<const GlobalObjectData*>(v.toPrivate()); |
| 279 |
} |
| 280 |
|
| 281 |
GlobalObjectData& data() { return *maybeData(); } |
| 282 |
const GlobalObjectData& data() const { return *maybeData(); } |
| 283 |
|
| 284 |
void initBuiltinProto(ProtoKind kind, JSObject* proto) { |
| 285 |
MOZ_ASSERT(proto); |
| 286 |
// Catch double-initialization; however if this is too much of a burden due |
| 287 |
// to OOM handling it could be removed. |
| 288 |
MOZ_ASSERT(!hasBuiltinProto(kind)); |
| 289 |
data().builtinProtos[kind].init(proto); |
| 290 |
} |
| 291 |
|
| 292 |
void setBuiltinProto(ProtoKind kind, JSObject* proto) { |
| 293 |
MOZ_ASSERT(proto); |
| 294 |
data().builtinProtos[kind].set(proto); |
| 295 |
} |
| 296 |
|
| 297 |
bool hasBuiltinProto(ProtoKind kind) const { |
| 298 |
return bool(data().builtinProtos[kind]); |
| 299 |
} |
| 300 |
JSObject& getBuiltinProto(ProtoKind kind) const { |
| 301 |
MOZ_ASSERT(hasBuiltinProto(kind)); |
| 302 |
return *data().builtinProtos[kind]; |
| 303 |
} |
| 304 |
|
| 305 |
public: |
| 306 |
GlobalLexicalEnvironmentObject& lexicalEnvironment() { |
| 307 |
return *data().lexicalEnvironment; |
| 308 |
} |
| 309 |
GlobalScope& emptyGlobalScope() const; |
| 310 |
|
| 311 |
void traceData(JSTracer* trc, GlobalObject* global) { |
| 312 |
data().trace(trc, global); |
| 313 |
} |
| 314 |
void releaseData(JS::GCContext* gcx); |
| 315 |
|
| 316 |
void addSizeOfData(mozilla::MallocSizeOf mallocSizeOf, |
| 317 |
JS::ClassInfo* info) const { |
| 318 |
if (maybeData()) { |
| 319 |
data().addSizeOfIncludingThis(mallocSizeOf, info); |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
void setOriginalEval(JSFunction* evalFun) { |
| 324 |
MOZ_ASSERT(!data().eval); |
| 325 |
data().eval.init(evalFun); |
| 326 |
} |
| 327 |
|
| 328 |
bool hasConstructor(JSProtoKey key) const { |
| 329 |
return bool(data().builtinConstructors[key].constructor); |
| 330 |
} |
| 331 |
JSObject& getConstructor(JSProtoKey key) const { |
| 332 |
MOZ_ASSERT(hasConstructor(key)); |
| 333 |
return *maybeGetConstructor(key); |
| 334 |
} |
| 335 |
|
| 336 |
static bool skipDeselectedConstructor(JSContext* cx, JSProtoKey key); |
| 337 |
|
| 338 |
private: |
| 339 |
enum class IfClassIsDisabled { DoNothing, Throw }; |
| 340 |
|
| 341 |
static bool resolveConstructor(JSContext* cx, Handle<GlobalObject*> global, |
| 342 |
JSProtoKey key, IfClassIsDisabled mode); |
| 343 |
|
| 344 |
public: |
| 345 |
static bool ensureConstructor(JSContext* cx, Handle<GlobalObject*> global, |
| 346 |
JSProtoKey key) { |
| 347 |
if (global->isStandardClassResolved(key)) { |
| 348 |
return true; |
| 349 |
} |
| 350 |
return resolveConstructor(cx, global, key, IfClassIsDisabled::Throw); |
| 351 |
} |
| 352 |
|
| 353 |
static JSObject* getOrCreateConstructor(JSContext* cx, JSProtoKey key) { |
| 354 |
MOZ_ASSERT(key != JSProto_Null); |
| 355 |
Handle<GlobalObject*> global = cx->global(); |
| 356 |
if (!GlobalObject::ensureConstructor(cx, global, key)) { |
| 357 |
return nullptr; |
| 358 |
} |
| 359 |
return &global->getConstructor(key); |
| 360 |
} |
| 361 |
|
| 362 |
static JSObject* getOrCreatePrototype(JSContext* cx, JSProtoKey key) { |
| 363 |
MOZ_ASSERT(key != JSProto_Null); |
| 364 |
Handle<GlobalObject*> global = cx->global(); |
| 365 |
if (!GlobalObject::ensureConstructor(cx, global, key)) { |
| 366 |
return nullptr; |
| 367 |
} |
| 368 |
return &global->getPrototype(key); |
| 369 |
} |
| 370 |
|
| 371 |
static JS::Handle<JSObject*> getOrCreatePrototypeHandle(JSContext* cx, |
| 372 |
JSProtoKey key) { |
| 373 |
MOZ_ASSERT(key != JSProto_Null); |
| 374 |
Handle<GlobalObject*> global = cx->global(); |
| 375 |
if (!GlobalObject::ensureConstructor(cx, global, key)) { |
| 376 |
return nullptr; |
| 377 |
} |
| 378 |
return global->getPrototypeHandle(key); |
| 379 |
} |
| 380 |
|
| 381 |
JSObject* maybeGetConstructor(JSProtoKey protoKey) const { |
| 382 |
MOZ_ASSERT(JSProto_Null < protoKey); |
| 383 |
MOZ_ASSERT(protoKey < JSProto_LIMIT); |
| 384 |
return data().builtinConstructors[protoKey].constructor; |
| 385 |
} |
| 386 |
|
| 387 |
template <typename T> |
| 388 |
T* maybeGetConstructor(JSProtoKey protoKey) const { |
| 389 |
JSObject* ctor = maybeGetConstructor(protoKey); |
| 390 |
return ctor ? &ctor->as<T>() : nullptr; |
| 391 |
} |
| 392 |
|
| 393 |
JSObject* maybeGetPrototype(JSProtoKey protoKey) const { |
| 394 |
MOZ_ASSERT(JSProto_Null < protoKey); |
| 395 |
MOZ_ASSERT(protoKey < JSProto_LIMIT); |
| 396 |
return data().builtinConstructors[protoKey].prototype; |
| 397 |
} |
| 398 |
|
| 399 |
template <typename T> |
| 400 |
T* maybeGetPrototype(JSProtoKey protoKey) const { |
| 401 |
JSObject* proto = maybeGetPrototype(protoKey); |
| 402 |
return proto ? &proto->as<T>() : nullptr; |
| 403 |
} |
| 404 |
|
| 405 |
static bool maybeResolveGlobalThis(JSContext* cx, |
| 406 |
Handle<GlobalObject*> global, |
| 407 |
bool* resolved); |
| 408 |
|
| 409 |
void setConstructor(JSProtoKey key, JSObject* obj) { |
| 410 |
MOZ_ASSERT(obj); |
| 411 |
data().builtinConstructors[key].constructor = obj; |
| 412 |
} |
| 413 |
|
| 414 |
bool hasPrototype(JSProtoKey key) const { |
| 415 |
return bool(data().builtinConstructors[key].prototype); |
| 416 |
} |
| 417 |
JSObject& getPrototype(JSProtoKey key) const { |
| 418 |
MOZ_ASSERT(hasPrototype(key)); |
| 419 |
return *maybeGetPrototype(key); |
| 420 |
} |
| 421 |
|
| 422 |
JS::Handle<JSObject*> getPrototypeHandle(JSProtoKey protoKey) const { |
| 423 |
MOZ_ASSERT(hasPrototype(protoKey)); |
| 424 |
MOZ_ASSERT(JSProto_Null < protoKey); |
| 425 |
MOZ_ASSERT(protoKey < JSProto_LIMIT); |
| 426 |
return Handle<JSObject*>::fromMarkedLocation( |
| 427 |
&data().builtinConstructors[protoKey].prototype.get()); |
| 428 |
} |
| 429 |
|
| 430 |
void setPrototype(JSProtoKey key, JSObject* obj) { |
| 431 |
MOZ_ASSERT(obj); |
| 432 |
data().builtinConstructors[key].prototype = obj; |
| 433 |
} |
| 434 |
|
| 435 |
/* |
| 436 |
* Lazy standard classes need a way to indicate they have been initialized. |
| 437 |
* Otherwise, when we delete them, we might accidentally recreate them via |
| 438 |
* a lazy initialization. We use the presence of an object in the constructor |
| 439 |
* array to indicate that they've been initialized. |
| 440 |
* |
| 441 |
* Note: A few builtin objects, like JSON and Math, are not constructors, |
| 442 |
* so getConstructor is a bit of a misnomer. |
| 443 |
*/ |
| 444 |
bool isStandardClassResolved(JSProtoKey key) const { |
| 445 |
return hasConstructor(key); |
| 446 |
} |
| 447 |
|
| 448 |
private: |
| 449 |
bool classIsInitialized(JSProtoKey key) const { |
| 450 |
bool inited = hasConstructor(key); |
| 451 |
MOZ_ASSERT(inited == hasPrototype(key)); |
| 452 |
return inited; |
| 453 |
} |
| 454 |
|
| 455 |
bool functionObjectClassesInitialized() const { |
| 456 |
bool inited = classIsInitialized(JSProto_Function); |
| 457 |
MOZ_ASSERT(inited == classIsInitialized(JSProto_Object)); |
| 458 |
return inited; |
| 459 |
} |
| 460 |
|
| 461 |
// Disallow use of unqualified JSObject::create in GlobalObject. |
| 462 |
static GlobalObject* create(...) = delete; |
| 463 |
|
| 464 |
friend struct ::JSRuntime; |
| 465 |
static GlobalObject* createInternal(JSContext* cx, const JSClass* clasp); |
| 466 |
|
| 467 |
public: |
| 468 |
static GlobalObject* new_(JSContext* cx, const JSClass* clasp, |
| 469 |
JSPrincipals* principals, |
| 470 |
JS::OnNewGlobalHookOption hookOption, |
| 471 |
const JS::RealmOptions& options); |
| 472 |
|
| 473 |
/* |
| 474 |
* Create a constructor function with the specified name and length using |
| 475 |
* ctor, a method which creates objects with the given class. |
| 476 |
*/ |
| 477 |
static JSFunction* createConstructor( |
| 478 |
JSContext* cx, JSNative ctor, JSAtom* name, unsigned length, |
| 479 |
gc::AllocKind kind = gc::AllocKind::FUNCTION, |
| 480 |
const JSJitInfo* jitInfo = nullptr); |
| 481 |
|
| 482 |
/* |
| 483 |
* Create an object to serve as [[Prototype]] for instances of the given |
| 484 |
* class, using |Object.prototype| as its [[Prototype]]. Users creating |
| 485 |
* prototype objects with particular internal structure (e.g. reserved |
| 486 |
* slots guaranteed to contain values of particular types) must immediately |
| 487 |
* complete the minimal initialization to make the returned object safe to |
| 488 |
* touch. |
| 489 |
*/ |
| 490 |
static NativeObject* createBlankPrototype( |
| 491 |
JSContext* cx, Handle<GlobalObject*> global, const JSClass* clasp, |
| 492 |
ObjectFlags objFlags = ObjectFlags()); |
| 493 |
|
| 494 |
/* |
| 495 |
* Identical to createBlankPrototype, but uses proto as the [[Prototype]] |
| 496 |
* of the returned blank prototype. |
| 497 |
*/ |
| 498 |
static NativeObject* createBlankPrototypeInheriting(JSContext* cx, |
| 499 |
const JSClass* clasp, |
| 500 |
HandleObject proto); |
| 501 |
|
| 502 |
template <typename T> |
| 503 |
static T* createBlankPrototypeInheriting(JSContext* cx, HandleObject proto) { |
| 504 |
NativeObject* res = createBlankPrototypeInheriting(cx, &T::class_, proto); |
| 505 |
return res ? &res->template as<T>() : nullptr; |
| 506 |
} |
| 507 |
|
| 508 |
template <typename T> |
| 509 |
static T* createBlankPrototype(JSContext* cx, Handle<GlobalObject*> global, |
| 510 |
ObjectFlags objFlags = ObjectFlags()) { |
| 511 |
NativeObject* res = createBlankPrototype(cx, global, &T::class_, objFlags); |
| 512 |
return res ? &res->template as<T>() : nullptr; |
| 513 |
} |
| 514 |
|
| 515 |
// Object, Function, and eval are eagerly resolved when creating the global. |
| 516 |
JSObject& getObjectPrototype() { |
| 517 |
MOZ_ASSERT(functionObjectClassesInitialized()); |
| 518 |
return getPrototype(JSProto_Object); |
| 519 |
} |
| 520 |
Handle<JSObject*> getObjectPrototypeHandle() { |
| 521 |
MOZ_ASSERT(functionObjectClassesInitialized()); |
| 522 |
return getPrototypeHandle(JSProto_Object); |
| 523 |
} |
| 524 |
JSObject& getFunctionConstructor() { |
| 525 |
MOZ_ASSERT(functionObjectClassesInitialized()); |
| 526 |
return getConstructor(JSProto_Function); |
| 527 |
} |
| 528 |
JSObject& getFunctionPrototype() { |
| 529 |
MOZ_ASSERT(functionObjectClassesInitialized()); |
| 530 |
return getPrototype(JSProto_Function); |
| 531 |
} |
| 532 |
Handle<JSObject*> getFunctionPrototypeHandle() { |
| 533 |
MOZ_ASSERT(functionObjectClassesInitialized()); |
| 534 |
return getPrototypeHandle(JSProto_Function); |
| 535 |
} |
| 536 |
JSFunction& getEvalFunction() { |
| 537 |
MOZ_ASSERT(data().eval); |
| 538 |
return *data().eval; |
| 539 |
} |
| 540 |
|
| 541 |
static NativeObject* getOrCreateArrayPrototype(JSContext* cx, |
| 542 |
Handle<GlobalObject*> global) { |
| 543 |
if (!ensureConstructor(cx, global, JSProto_Array)) { |
| 544 |
return nullptr; |
| 545 |
} |
| 546 |
return &global->getPrototype(JSProto_Array).as<NativeObject>(); |
| 547 |
} |
| 548 |
|
| 549 |
NativeObject* maybeGetArrayPrototype() { |
| 550 |
if (classIsInitialized(JSProto_Array)) { |
| 551 |
return &getPrototype(JSProto_Array).as<NativeObject>(); |
| 552 |
} |
| 553 |
return nullptr; |
| 554 |
} |
| 555 |
|
| 556 |
static JSObject* getOrCreateBooleanPrototype(JSContext* cx, |
| 557 |
Handle<GlobalObject*> global) { |
| 558 |
if (!ensureConstructor(cx, global, JSProto_Boolean)) { |
| 559 |
return nullptr; |
| 560 |
} |
| 561 |
return &global->getPrototype(JSProto_Boolean); |
| 562 |
} |
| 563 |
|
| 564 |
static JSObject* getOrCreateNumberPrototype(JSContext* cx, |
| 565 |
Handle<GlobalObject*> global) { |
| 566 |
if (!ensureConstructor(cx, global, JSProto_Number)) { |
| 567 |
return nullptr; |
| 568 |
} |
| 569 |
return &global->getPrototype(JSProto_Number); |
| 570 |
} |
| 571 |
|
| 572 |
static JSObject* getOrCreateStringPrototype(JSContext* cx, |
| 573 |
Handle<GlobalObject*> global) { |
| 574 |
if (!ensureConstructor(cx, global, JSProto_String)) { |
| 575 |
return nullptr; |
| 576 |
} |
| 577 |
return &global->getPrototype(JSProto_String); |
| 578 |
} |
| 579 |
|
| 580 |
static JSObject* getOrCreateSymbolPrototype(JSContext* cx, |
| 581 |
Handle<GlobalObject*> global) { |
| 582 |
if (!ensureConstructor(cx, global, JSProto_Symbol)) { |
| 583 |
return nullptr; |
| 584 |
} |
| 585 |
return &global->getPrototype(JSProto_Symbol); |
| 586 |
} |
| 587 |
|
| 588 |
static JSObject* getOrCreateBigIntPrototype(JSContext* cx, |
| 589 |
Handle<GlobalObject*> global) { |
| 590 |
if (!ensureConstructor(cx, global, JSProto_BigInt)) { |
| 591 |
return nullptr; |
| 592 |
} |
| 593 |
return &global->getPrototype(JSProto_BigInt); |
| 594 |
} |
| 595 |
|
| 596 |
static JSObject* getOrCreatePromisePrototype(JSContext* cx, |
| 597 |
Handle<GlobalObject*> global) { |
| 598 |
if (!ensureConstructor(cx, global, JSProto_Promise)) { |
| 599 |
return nullptr; |
| 600 |
} |
| 601 |
return &global->getPrototype(JSProto_Promise); |
| 602 |
} |
| 603 |
|
| 604 |
static JSObject* getOrCreateRegExpPrototype(JSContext* cx, |
| 605 |
Handle<GlobalObject*> global) { |
| 606 |
if (!ensureConstructor(cx, global, JSProto_RegExp)) { |
| 607 |
return nullptr; |
| 608 |
} |
| 609 |
return &global->getPrototype(JSProto_RegExp); |
| 610 |
} |
| 611 |
|
| 612 |
static JSObject* getOrCreateRegExpConstructor(JSContext* cx, |
| 613 |
Handle<GlobalObject*> global) { |
| 614 |
if (!ensureConstructor(cx, global, JSProto_RegExp)) { |
| 615 |
return nullptr; |
| 616 |
} |
| 617 |
return &global->getConstructor(JSProto_RegExp); |
| 618 |
} |
| 619 |
|
| 620 |
JSObject* maybeGetRegExpPrototype() { |
| 621 |
if (classIsInitialized(JSProto_RegExp)) { |
| 622 |
return &getPrototype(JSProto_RegExp); |
| 623 |
} |
| 624 |
return nullptr; |
| 625 |
} |
| 626 |
|
| 627 |
static JSObject* getOrCreateSavedFramePrototype( |
| 628 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 629 |
if (!ensureConstructor(cx, global, JSProto_SavedFrame)) { |
| 630 |
return nullptr; |
| 631 |
} |
| 632 |
return &global->getPrototype(JSProto_SavedFrame); |
| 633 |
} |
| 634 |
|
| 635 |
static JSObject* getOrCreateArrayBufferConstructor( |
| 636 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 637 |
if (!ensureConstructor(cx, global, JSProto_ArrayBuffer)) { |
| 638 |
return nullptr; |
| 639 |
} |
| 640 |
return &global->getConstructor(JSProto_ArrayBuffer); |
| 641 |
} |
| 642 |
|
| 643 |
static JSObject* getOrCreateArrayBufferPrototype( |
| 644 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 645 |
if (!ensureConstructor(cx, global, JSProto_ArrayBuffer)) { |
| 646 |
return nullptr; |
| 647 |
} |
| 648 |
return &global->getPrototype(JSProto_ArrayBuffer); |
| 649 |
} |
| 650 |
|
| 651 |
static JSObject* getOrCreateSharedArrayBufferPrototype( |
| 652 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 653 |
if (!ensureConstructor(cx, global, JSProto_SharedArrayBuffer)) { |
| 654 |
return nullptr; |
| 655 |
} |
| 656 |
return &global->getPrototype(JSProto_SharedArrayBuffer); |
| 657 |
} |
| 658 |
|
| 659 |
static JSObject* getOrCreateSharedArrayBufferConstructor( |
| 660 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 661 |
if (!ensureConstructor(cx, global, JSProto_SharedArrayBuffer)) { |
| 662 |
return nullptr; |
| 663 |
} |
| 664 |
return &global->getConstructor(JSProto_SharedArrayBuffer); |
| 665 |
} |
| 666 |
|
| 667 |
static JSObject* getOrCreateCustomErrorPrototype(JSContext* cx, |
| 668 |
Handle<GlobalObject*> global, |
| 669 |
JSExnType exnType) { |
| 670 |
JSProtoKey key = GetExceptionProtoKey(exnType); |
| 671 |
if (!ensureConstructor(cx, global, key)) { |
| 672 |
return nullptr; |
| 673 |
} |
| 674 |
return &global->getPrototype(key); |
| 675 |
} |
| 676 |
|
| 677 |
static JSFunction* getOrCreateErrorConstructor(JSContext* cx, |
| 678 |
Handle<GlobalObject*> global) { |
| 679 |
if (!ensureConstructor(cx, global, JSProto_Error)) { |
| 680 |
return nullptr; |
| 681 |
} |
| 682 |
return &global->getConstructor(JSProto_Error).as<JSFunction>(); |
| 683 |
} |
| 684 |
|
| 685 |
static JSObject* getOrCreateErrorPrototype(JSContext* cx, |
| 686 |
Handle<GlobalObject*> global) { |
| 687 |
return getOrCreateCustomErrorPrototype(cx, global, JSEXN_ERR); |
| 688 |
} |
| 689 |
|
| 690 |
static JSFunction* getOrCreateTypedArrayConstructor( |
| 691 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 692 |
if (!ensureConstructor(cx, global, JSProto_TypedArray)) { |
| 693 |
return nullptr; |
| 694 |
} |
| 695 |
return &global->getConstructor(JSProto_TypedArray).as<JSFunction>(); |
| 696 |
} |
| 697 |
|
| 698 |
static JSObject* getOrCreateTypedArrayPrototype( |
| 699 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 700 |
if (!ensureConstructor(cx, global, JSProto_TypedArray)) { |
| 701 |
return nullptr; |
| 702 |
} |
| 703 |
return &global->getPrototype(JSProto_TypedArray); |
| 704 |
} |
| 705 |
|
| 706 |
private: |
| 707 |
using ObjectInitOp = bool (*)(JSContext*, Handle<GlobalObject*>); |
| 708 |
using ObjectInitWithTagOp = bool (*)(JSContext*, Handle<GlobalObject*>, |
| 709 |
Handle<JSAtom*>); |
| 710 |
|
| 711 |
static JSObject* getOrCreateBuiltinProto(JSContext* cx, |
| 712 |
Handle<GlobalObject*> global, |
| 713 |
ProtoKind kind, ObjectInitOp init) { |
| 714 |
if (JSObject* proto = global->maybeBuiltinProto(kind)) { |
| 715 |
return proto; |
| 716 |
} |
| 717 |
|
| 718 |
return createBuiltinProto(cx, global, kind, init); |
| 719 |
} |
| 720 |
|
| 721 |
static JSObject* getOrCreateBuiltinProto(JSContext* cx, |
| 722 |
Handle<GlobalObject*> global, |
| 723 |
ProtoKind kind, Handle<JSAtom*> tag, |
| 724 |
ObjectInitWithTagOp init) { |
| 725 |
if (JSObject* proto = global->maybeBuiltinProto(kind)) { |
| 726 |
return proto; |
| 727 |
} |
| 728 |
|
| 729 |
return createBuiltinProto(cx, global, kind, tag, init); |
| 730 |
} |
| 731 |
|
| 732 |
static JSObject* createBuiltinProto(JSContext* cx, |
| 733 |
Handle<GlobalObject*> global, |
| 734 |
ProtoKind kind, ObjectInitOp init); |
| 735 |
static JSObject* createBuiltinProto(JSContext* cx, |
| 736 |
Handle<GlobalObject*> global, |
| 737 |
ProtoKind kind, Handle<JSAtom*> tag, |
| 738 |
ObjectInitWithTagOp init); |
| 739 |
|
| 740 |
static JSObject* createIteratorPrototype(JSContext* cx, |
| 741 |
Handle<GlobalObject*> global); |
| 742 |
|
| 743 |
public: |
| 744 |
JSObject* maybeBuiltinProto(ProtoKind kind) const { |
| 745 |
return data().builtinProtos[kind]; |
| 746 |
} |
| 747 |
|
| 748 |
NativeObject* maybeGetIteratorPrototype() { |
| 749 |
if (!hasPrototype(JSProto_Iterator)) { |
| 750 |
return nullptr; |
| 751 |
} |
| 752 |
return &(getPrototype(JSProto_Iterator).as<NativeObject>()); |
| 753 |
} |
| 754 |
|
| 755 |
static JSObject* getOrCreateIteratorPrototype(JSContext* cx, |
| 756 |
Handle<GlobalObject*> global) { |
| 757 |
if (!ensureConstructor(cx, global, JSProto_Iterator)) { |
| 758 |
return nullptr; |
| 759 |
} |
| 760 |
return &global->getPrototype(JSProto_Iterator); |
| 761 |
} |
| 762 |
|
| 763 |
static NativeObject* getOrCreateArrayIteratorPrototype( |
| 764 |
JSContext* cx, Handle<GlobalObject*> global); |
| 765 |
|
| 766 |
NativeObject* maybeGetArrayIteratorPrototype() { |
| 767 |
if (JSObject* obj = maybeBuiltinProto(ProtoKind::ArrayIteratorProto)) { |
| 768 |
return &obj->as<NativeObject>(); |
| 769 |
} |
| 770 |
return nullptr; |
| 771 |
} |
| 772 |
|
| 773 |
static JSObject* getOrCreateStringIteratorPrototype( |
| 774 |
JSContext* cx, Handle<GlobalObject*> global); |
| 775 |
|
| 776 |
static JSObject* getOrCreateRegExpStringIteratorPrototype( |
| 777 |
JSContext* cx, Handle<GlobalObject*> global); |
| 778 |
|
| 779 |
#ifdef NIGHTLY_BUILD |
| 780 |
static JSObject* getOrCreateIteratorRangePrototype( |
| 781 |
JSContext* cx, Handle<GlobalObject*> global); |
| 782 |
#endif |
| 783 |
|
| 784 |
void setGeneratorObjectPrototype(JSObject* obj) { |
| 785 |
setBuiltinProto(ProtoKind::GeneratorObjectProto, obj); |
| 786 |
} |
| 787 |
|
| 788 |
static JSObject* getOrCreateGeneratorObjectPrototype( |
| 789 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 790 |
if (!ensureConstructor(cx, global, JSProto_GeneratorFunction)) { |
| 791 |
return nullptr; |
| 792 |
} |
| 793 |
return &global->getBuiltinProto(ProtoKind::GeneratorObjectProto); |
| 794 |
} |
| 795 |
|
| 796 |
static JSObject* getOrCreateGeneratorFunctionPrototype( |
| 797 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 798 |
if (!ensureConstructor(cx, global, JSProto_GeneratorFunction)) { |
| 799 |
return nullptr; |
| 800 |
} |
| 801 |
return &global->getPrototype(JSProto_GeneratorFunction); |
| 802 |
} |
| 803 |
|
| 804 |
static JSObject* getOrCreateGeneratorFunction(JSContext* cx, |
| 805 |
Handle<GlobalObject*> global) { |
| 806 |
if (!ensureConstructor(cx, global, JSProto_GeneratorFunction)) { |
| 807 |
return nullptr; |
| 808 |
} |
| 809 |
return &global->getConstructor(JSProto_GeneratorFunction); |
| 810 |
} |
| 811 |
|
| 812 |
static JSObject* getOrCreateAsyncFunctionPrototype( |
| 813 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 814 |
if (!ensureConstructor(cx, global, JSProto_AsyncFunction)) { |
| 815 |
return nullptr; |
| 816 |
} |
| 817 |
return &global->getPrototype(JSProto_AsyncFunction); |
| 818 |
} |
| 819 |
|
| 820 |
static JSObject* getOrCreateAsyncFunction(JSContext* cx, |
| 821 |
Handle<GlobalObject*> global) { |
| 822 |
if (!ensureConstructor(cx, global, JSProto_AsyncFunction)) { |
| 823 |
return nullptr; |
| 824 |
} |
| 825 |
return &global->getConstructor(JSProto_AsyncFunction); |
| 826 |
} |
| 827 |
|
| 828 |
static JSObject* createAsyncIteratorPrototype(JSContext* cx, |
| 829 |
Handle<GlobalObject*> global); |
| 830 |
|
| 831 |
static JSObject* getOrCreateAsyncIteratorPrototype( |
| 832 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 833 |
if (JSObject* proto = |
| 834 |
global->maybeBuiltinProto(ProtoKind::AsyncIteratorProto)) { |
| 835 |
return proto; |
| 836 |
} |
| 837 |
return createAsyncIteratorPrototype(cx, global); |
| 838 |
} |
| 839 |
|
| 840 |
static JSObject* getOrCreateAsyncFromSyncIteratorPrototype( |
| 841 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 842 |
return getOrCreateBuiltinProto(cx, global, |
| 843 |
ProtoKind::AsyncFromSyncIteratorProto, |
| 844 |
initAsyncFromSyncIteratorProto); |
| 845 |
} |
| 846 |
|
| 847 |
static JSObject* getOrCreateAsyncGenerator(JSContext* cx, |
| 848 |
Handle<GlobalObject*> global) { |
| 849 |
if (!ensureConstructor(cx, global, JSProto_AsyncGeneratorFunction)) { |
| 850 |
return nullptr; |
| 851 |
} |
| 852 |
return &global->getPrototype(JSProto_AsyncGeneratorFunction); |
| 853 |
} |
| 854 |
|
| 855 |
static JSObject* getOrCreateAsyncGeneratorFunction( |
| 856 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 857 |
if (!ensureConstructor(cx, global, JSProto_AsyncGeneratorFunction)) { |
| 858 |
return nullptr; |
| 859 |
} |
| 860 |
return &global->getConstructor(JSProto_AsyncGeneratorFunction); |
| 861 |
} |
| 862 |
|
| 863 |
void setAsyncGeneratorPrototype(JSObject* obj) { |
| 864 |
initBuiltinProto(ProtoKind::AsyncGeneratorProto, obj); |
| 865 |
} |
| 866 |
|
| 867 |
static JSObject* getOrCreateAsyncGeneratorPrototype( |
| 868 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 869 |
if (!ensureConstructor(cx, global, JSProto_AsyncGeneratorFunction)) { |
| 870 |
return nullptr; |
| 871 |
} |
| 872 |
return &global->getBuiltinProto(ProtoKind::AsyncGeneratorProto); |
| 873 |
} |
| 874 |
|
| 875 |
static JSObject* getOrCreateMapIteratorPrototype( |
| 876 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 877 |
return getOrCreateBuiltinProto(cx, global, ProtoKind::MapIteratorProto, |
| 878 |
initMapIteratorProto); |
| 879 |
} |
| 880 |
|
| 881 |
static JSObject* getOrCreateSetIteratorPrototype( |
| 882 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 883 |
return getOrCreateBuiltinProto(cx, global, ProtoKind::SetIteratorProto, |
| 884 |
initSetIteratorProto); |
| 885 |
} |
| 886 |
|
| 887 |
static JSObject* getOrCreateSegmentsPrototype(JSContext* cx, |
| 888 |
Handle<GlobalObject*> global) { |
| 889 |
return getOrCreateBuiltinProto(cx, global, ProtoKind::SegmentsProto, |
| 890 |
initSegmentsProto); |
| 891 |
} |
| 892 |
|
| 893 |
static JSObject* getOrCreateSegmentIteratorPrototype( |
| 894 |
JSContext* cx, Handle<GlobalObject*> global) { |
| 895 |
return getOrCreateBuiltinProto(cx, global, ProtoKind::SegmentIteratorProto, |
| 896 |
initSegmentIteratorProto); |
| 897 |
} |
| 898 |
|
| 899 |
static JSObject* getOrCreateDataViewPrototype(JSContext* cx, |
| 900 |
Handle<GlobalObject*> global) { |
| 901 |
if (!ensureConstructor(cx, global, JSProto_DataView)) { |
| 902 |
return nullptr; |
| 903 |
} |
| 904 |
return &global->getPrototype(JSProto_DataView); |
| 905 |
} |
| 906 |
|
| 907 |
static JSObject* getOrCreatePromiseConstructor(JSContext* cx, |
| 908 |
Handle<GlobalObject*> global) { |
| 909 |
if (!ensureConstructor(cx, global, JSProto_Promise)) { |
| 910 |
return nullptr; |
| 911 |
} |
| 912 |
return &global->getConstructor(JSProto_Promise); |
| 913 |
} |
| 914 |
|
| 915 |
static NativeObject* getOrCreateWrapForValidIteratorPrototype( |
| 916 |
JSContext* cx, Handle<GlobalObject*> global); |
| 917 |
|
| 918 |
static NativeObject* getOrCreateIteratorHelperPrototype( |
| 919 |
JSContext* cx, Handle<GlobalObject*> global); |
| 920 |
|
| 921 |
static NativeObject* getOrCreateAsyncIteratorHelperPrototype( |
| 922 |
JSContext* cx, Handle<GlobalObject*> global); |
| 923 |
static bool initAsyncIteratorHelperProto(JSContext* cx, |
| 924 |
Handle<GlobalObject*> global); |
| 925 |
|
| 926 |
NativeObject& getIntrinsicsHolder() const { |
| 927 |
MOZ_ASSERT(data().intrinsicsHolder); |
| 928 |
return *data().intrinsicsHolder; |
| 929 |
} |
| 930 |
|
| 931 |
static bool createIntrinsicsHolder(JSContext* cx, |
| 932 |
Handle<GlobalObject*> global); |
| 933 |
|
| 934 |
NativeObject* getComputedIntrinsicsHolder() { |
| 935 |
return data().computedIntrinsicsHolder; |
| 936 |
} |
| 937 |
void setComputedIntrinsicsHolder(NativeObject* holder) { |
| 938 |
data().computedIntrinsicsHolder = holder; |
| 939 |
} |
| 940 |
|
| 941 |
// If a self-hosting intrinsic with the given |name| exists, it's stored in |
| 942 |
// |*vp| and this function returns true. Else it returns false. |
| 943 |
bool maybeGetIntrinsicValue(PropertyName* name, Value* vp, JSContext* cx) { |
| 944 |
NativeObject& holder = getIntrinsicsHolder(); |
| 945 |
|
| 946 |
if (mozilla::Maybe<PropertyInfo> prop = holder.lookup(cx, name)) { |
| 947 |
*vp = holder.getSlot(prop->slot()); |
| 948 |
return true; |
| 949 |
} |
| 950 |
|
| 951 |
return false; |
| 952 |
} |
| 953 |
|
| 954 |
static bool getIntrinsicValue(JSContext* cx, Handle<GlobalObject*> global, |
| 955 |
Handle<PropertyName*> name, |
| 956 |
MutableHandleValue value) { |
| 957 |
// `undefined` in self-hosted JS code should be emitted as JSOp::Undefined. |
| 958 |
MOZ_ASSERT(name != cx->names().undefined); |
| 959 |
|
| 960 |
if (global->maybeGetIntrinsicValue(name, value.address(), cx)) { |
| 961 |
return true; |
| 962 |
} |
| 963 |
return getIntrinsicValueSlow(cx, global, name, value); |
| 964 |
} |
| 965 |
|
| 966 |
static bool getIntrinsicValueSlow(JSContext* cx, Handle<GlobalObject*> global, |
| 967 |
Handle<PropertyName*> name, |
| 968 |
MutableHandleValue value); |
| 969 |
|
| 970 |
static bool addIntrinsicValue(JSContext* cx, Handle<GlobalObject*> global, |
| 971 |
Handle<PropertyName*> name, HandleValue value); |
| 972 |
|
| 973 |
static inline bool setIntrinsicValue(JSContext* cx, |
| 974 |
Handle<GlobalObject*> global, |
| 975 |
Handle<PropertyName*> name, |
| 976 |
HandleValue value); |
| 977 |
|
| 978 |
static bool getSelfHostedFunction(JSContext* cx, Handle<GlobalObject*> global, |
| 979 |
Handle<PropertyName*> selfHostedName, |
| 980 |
Handle<JSAtom*> name, unsigned nargs, |
| 981 |
MutableHandleValue funVal); |
| 982 |
|
| 983 |
static RegExpStatics* getRegExpStatics(JSContext* cx, |
| 984 |
Handle<GlobalObject*> global); |
| 985 |
|
| 986 |
static JSObject* getOrCreateThrowTypeError(JSContext* cx, |
| 987 |
Handle<GlobalObject*> global); |
| 988 |
|
| 989 |
RegExpRealm& regExpRealm() { return data().regExpRealm; } |
| 990 |
|
| 991 |
// Infallibly test whether the given value is the eval function for this |
| 992 |
// global. |
| 993 |
bool valueIsEval(const Value& val); |
| 994 |
|
| 995 |
static ArgumentsObject* getOrCreateArgumentsTemplateObject(JSContext* cx, |
| 996 |
bool mapped); |
| 997 |
ArgumentsObject* maybeArgumentsTemplateObject(bool mapped) const; |
| 998 |
|
| 999 |
static MapObject* getOrCreateMapTemplateObject(JSContext* cx); |
| 1000 |
static SetObject* getOrCreateSetTemplateObject(JSContext* cx); |
| 1001 |
|
| 1002 |
static const size_t IterResultObjectValueSlot = 0; |
| 1003 |
static const size_t IterResultObjectDoneSlot = 1; |
| 1004 |
static js::PlainObject* getOrCreateIterResultTemplateObject(JSContext* cx); |
| 1005 |
static js::PlainObject* getOrCreateIterResultWithoutPrototypeTemplateObject( |
| 1006 |
JSContext* cx); |
| 1007 |
|
| 1008 |
private: |
| 1009 |
enum class WithObjectPrototype { No, Yes }; |
| 1010 |
static js::PlainObject* createIterResultTemplateObject( |
| 1011 |
JSContext* cx, WithObjectPrototype withProto); |
| 1012 |
|
| 1013 |
public: |
| 1014 |
static ScriptSourceObject* getOrCreateSelfHostingScriptSourceObject( |
| 1015 |
JSContext* cx, Handle<GlobalObject*> global); |
| 1016 |
|
| 1017 |
// Implemented in vm/Iteration.cpp. |
| 1018 |
template <ProtoKind Kind, const JSClass* ProtoClass, |
| 1019 |
const JSFunctionSpec* Methods, const bool needsFuseProperty = false> |
| 1020 |
static bool initObjectIteratorProto(JSContext* cx, |
| 1021 |
Handle<GlobalObject*> global, |
| 1022 |
Handle<JSAtom*> tag); |
| 1023 |
|
| 1024 |
// Implemented in vm/AsyncIteration.cpp. |
| 1025 |
static bool initAsyncIteratorProto(JSContext* cx, |
| 1026 |
Handle<GlobalObject*> global); |
| 1027 |
static bool initAsyncFromSyncIteratorProto(JSContext* cx, |
| 1028 |
Handle<GlobalObject*> global); |
| 1029 |
|
| 1030 |
// Implemented in builtin/MapObject.cpp. |
| 1031 |
static bool initMapIteratorProto(JSContext* cx, Handle<GlobalObject*> global); |
| 1032 |
static bool initSetIteratorProto(JSContext* cx, Handle<GlobalObject*> global); |
| 1033 |
|
| 1034 |
// Implemented in builtin/intl/Segmenter.cpp. |
| 1035 |
static bool initSegmentsProto(JSContext* cx, Handle<GlobalObject*> global); |
| 1036 |
static bool initSegmentIteratorProto(JSContext* cx, |
| 1037 |
Handle<GlobalObject*> global); |
| 1038 |
|
| 1039 |
static bool initStandardClasses(JSContext* cx, Handle<GlobalObject*> global); |
| 1040 |
|
| 1041 |
// Disallow GC as it may mutate the vector. |
| 1042 |
Realm::DebuggerVector& getDebuggers(const JS::AutoRequireNoGC& nogc) const { |
| 1043 |
return realm()->getDebuggers(nogc); |
| 1044 |
} |
| 1045 |
bool hasDebuggers() const { return realm()->hasDebuggers(); } |
| 1046 |
|
| 1047 |
JSObject* maybeWindowProxy() const { return data().windowProxy; } |
| 1048 |
|
| 1049 |
void setWindowProxy(JSObject* windowProxy) { |
| 1050 |
// Note: the global must always be associated with the same WindowProxy. |
| 1051 |
// CacheIR optimizations rely on this by baking in the WindowProxy for the |
| 1052 |
// global. |
| 1053 |
MOZ_ASSERT(!data().windowProxy); |
| 1054 |
data().windowProxy.init(windowProxy); |
| 1055 |
} |
| 1056 |
|
| 1057 |
ArrayObject* getSourceURLsHolder() const { return data().sourceURLsHolder; } |
| 1058 |
|
| 1059 |
void setSourceURLsHolder(ArrayObject* holder) { |
| 1060 |
data().sourceURLsHolder = holder; |
| 1061 |
} |
| 1062 |
void clearSourceURLSHolder() { setSourceURLsHolder(nullptr); } |
| 1063 |
|
| 1064 |
SharedShape* maybeArrayShapeWithDefaultProto() const { |
| 1065 |
return data().arrayShapeWithDefaultProto; |
| 1066 |
} |
| 1067 |
|
| 1068 |
static SharedShape* getArrayShapeWithDefaultProto(JSContext* cx) { |
| 1069 |
if (SharedShape* shape = cx->global()->data().arrayShapeWithDefaultProto; |
| 1070 |
MOZ_LIKELY(shape)) { |
| 1071 |
return shape; |
| 1072 |
} |
| 1073 |
return createArrayShapeWithDefaultProto(cx); |
| 1074 |
} |
| 1075 |
static SharedShape* createArrayShapeWithDefaultProto(JSContext* cx); |
| 1076 |
|
| 1077 |
static SharedShape* getPlainObjectShapeWithDefaultProto(JSContext* cx, |
| 1078 |
gc::AllocKind kind) { |
| 1079 |
PlainObjectSlotsKind slotsKind = PlainObjectSlotsKindFromAllocKind(kind); |
| 1080 |
SharedShape* shape = |
| 1081 |
cx->global()->data().plainObjectShapesWithDefaultProto[slotsKind]; |
| 1082 |
if (MOZ_LIKELY(shape)) { |
| 1083 |
return shape; |
| 1084 |
} |
| 1085 |
return createPlainObjectShapeWithDefaultProto(cx, kind); |
| 1086 |
} |
| 1087 |
static SharedShape* createPlainObjectShapeWithDefaultProto( |
| 1088 |
JSContext* cx, gc::AllocKind kind); |
| 1089 |
|
| 1090 |
static SharedShape* getEmptyPlainObjectShape(JSContext* cx) { |
| 1091 |
const PlainObjectSlotsKind kind = PlainObjectSlotsKind::Slots0; |
| 1092 |
SharedShape* shape = |
| 1093 |
cx->global()->data().plainObjectShapesWithDefaultProto[kind]; |
| 1094 |
MOZ_ASSERT(shape); // This is created on initialization. |
| 1095 |
return shape; |
| 1096 |
} |
| 1097 |
|
| 1098 |
static SharedShape* getFunctionShapeWithDefaultProto(JSContext* cx, |
| 1099 |
bool extended) { |
| 1100 |
GlobalObjectData& data = cx->global()->data(); |
| 1101 |
SharedShape* shape = extended ? data.extendedFunctionShapeWithDefaultProto |
| 1102 |
: data.functionShapeWithDefaultProto; |
| 1103 |
if (MOZ_LIKELY(shape)) { |
| 1104 |
return shape; |
| 1105 |
} |
| 1106 |
return createFunctionShapeWithDefaultProto(cx, extended); |
| 1107 |
} |
| 1108 |
static SharedShape* createFunctionShapeWithDefaultProto(JSContext* cx, |
| 1109 |
bool extended); |
| 1110 |
|
| 1111 |
SharedShape* maybeBoundFunctionShapeWithDefaultProto() const { |
| 1112 |
return data().boundFunctionShapeWithDefaultProto; |
| 1113 |
} |
| 1114 |
void setBoundFunctionShapeWithDefaultProto(SharedShape* shape) { |
| 1115 |
data().boundFunctionShapeWithDefaultProto = shape; |
| 1116 |
} |
| 1117 |
|
| 1118 |
SharedShape* maybeRegExpShapeWithDefaultProto() const { |
| 1119 |
return data().regExpShapeWithDefaultProto; |
| 1120 |
} |
| 1121 |
void setRegExpShapeWithDefaultProto(SharedShape* shape) { |
| 1122 |
data().regExpShapeWithDefaultProto = shape; |
| 1123 |
} |
| 1124 |
|
| 1125 |
static PropertyIteratorObject* getOrCreateEmptyIterator(JSContext* cx); |
| 1126 |
|
| 1127 |
// Returns an object that represents the realm, used by embedder. |
| 1128 |
static JSObject* getOrCreateRealmKeyObject(JSContext* cx, |
| 1129 |
Handle<GlobalObject*> global); |
| 1130 |
|
| 1131 |
gc::FinalizationRegistryGlobalData* getOrCreateFinalizationRegistryData(); |
| 1132 |
gc::FinalizationRegistryGlobalData* maybeFinalizationRegistryData() const { |
| 1133 |
return data().finalizationRegistryData.get(); |
| 1134 |
} |
| 1135 |
|
| 1136 |
static size_t offsetOfGlobalDataSlot() { |
| 1137 |
return getFixedSlotOffset(GLOBAL_DATA_SLOT); |
| 1138 |
} |
| 1139 |
|
| 1140 |
uint32_t generationCount() const { return data().generationCount; } |
| 1141 |
const void* addressOfGenerationCount() const { |
| 1142 |
return &data().generationCount; |
| 1143 |
} |
| 1144 |
void bumpGenerationCount() { |
| 1145 |
MOZ_RELEASE_ASSERT(data().generationCount < UINT32_MAX); |
| 1146 |
data().generationCount++; |
| 1147 |
} |
| 1148 |
}; |
| 1149 |
|
| 1150 |
/* |
| 1151 |
* Unless otherwise specified, define ctor.prototype = proto as non-enumerable, |
| 1152 |
* non-configurable, and non-writable; and define proto.constructor = ctor as |
| 1153 |
* non-enumerable but configurable and writable. |
| 1154 |
*/ |
| 1155 |
extern bool LinkConstructorAndPrototype( |
| 1156 |
JSContext* cx, JSObject* ctor, JSObject* proto, |
| 1157 |
unsigned prototypeAttrs = JSPROP_PERMANENT | JSPROP_READONLY, |
| 1158 |
unsigned constructorAttrs = 0); |
| 1159 |
|
| 1160 |
/* |
| 1161 |
* Define properties and/or functions on any object. Either ps or fs, or both, |
| 1162 |
* may be null. |
| 1163 |
*/ |
| 1164 |
extern bool DefinePropertiesAndFunctions(JSContext* cx, HandleObject obj, |
| 1165 |
const JSPropertySpec* ps, |
| 1166 |
const JSFunctionSpec* fs); |
| 1167 |
|
| 1168 |
extern bool DefineToStringTag(JSContext* cx, HandleObject obj, JSAtom* tag); |
| 1169 |
|
| 1170 |
/* |
| 1171 |
* Convenience templates to generic constructor and prototype creation functions |
| 1172 |
* for ClassSpecs. |
| 1173 |
*/ |
| 1174 |
|
| 1175 |
template <JSNative ctor, unsigned length, gc::AllocKind kind, |
| 1176 |
const JSJitInfo* jitInfo = nullptr> |
| 1177 |
JSObject* GenericCreateConstructor(JSContext* cx, JSProtoKey key) { |
| 1178 |
// Note - We duplicate the trick from ClassName() so that we don't need to |
| 1179 |
// include vm/JSAtomUtils-inl.h here. |
| 1180 |
PropertyName* name = (&cx->names().Null)[key]; |
| 1181 |
return GlobalObject::createConstructor(cx, ctor, name, length, kind, jitInfo); |
| 1182 |
} |
| 1183 |
|
| 1184 |
template <typename T> |
| 1185 |
JSObject* GenericCreatePrototype(JSContext* cx, JSProtoKey key) { |
| 1186 |
static_assert( |
| 1187 |
!std::is_same_v<T, PlainObject>, |
| 1188 |
"creating Object.prototype is very special and isn't handled here"); |
| 1189 |
MOZ_ASSERT(&T::class_ == ProtoKeyToClass(key), |
| 1190 |
"type mismatch--probably too much copy/paste in your ClassSpec"); |
| 1191 |
MOZ_ASSERT( |
| 1192 |
InheritanceProtoKeyForStandardClass(key) == JSProto_Object, |
| 1193 |
"subclasses (of anything but Object) can't use GenericCreatePrototype"); |
| 1194 |
return GlobalObject::createBlankPrototype(cx, cx->global(), &T::protoClass_); |
| 1195 |
} |
| 1196 |
|
| 1197 |
// Which object(s) should be marked as having a fuse property in |
| 1198 |
// GenericFinishInit. |
| 1199 |
enum class WhichHasFuseProperty { |
| 1200 |
Proto, |
| 1201 |
ProtoAndCtor, |
| 1202 |
}; |
| 1203 |
|
| 1204 |
template <WhichHasFuseProperty FuseProperty> |
| 1205 |
inline bool GenericFinishInit(JSContext* cx, HandleObject ctor, |
| 1206 |
HandleObject proto) { |
| 1207 |
if constexpr (FuseProperty == WhichHasFuseProperty::ProtoAndCtor) { |
| 1208 |
if (!JSObject::setHasFuseProperty(cx, ctor)) { |
| 1209 |
return false; |
| 1210 |
} |
| 1211 |
} |
| 1212 |
return JSObject::setHasFuseProperty(cx, proto); |
| 1213 |
} |
| 1214 |
|
| 1215 |
inline JSProtoKey StandardProtoKeyOrNull(const JSObject* obj) { |
| 1216 |
return JSCLASS_CACHED_PROTO_KEY(obj->getClass()); |
| 1217 |
} |
| 1218 |
|
| 1219 |
JSObject* NewTenuredObjectWithFunctionPrototype(JSContext* cx, |
| 1220 |
Handle<GlobalObject*> global); |
| 1221 |
|
| 1222 |
} // namespace js |
| 1223 |
|
| 1224 |
template <> |
| 1225 |
inline bool JSObject::is<js::GlobalObject>() const { |
| 1226 |
return !!(getClass()->flags & JSCLASS_IS_GLOBAL); |
| 1227 |
} |
| 1228 |
|
| 1229 |
#endif /* vm_GlobalObject_h */ |