| 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/Scope.h" |
| 8 |
|
| 9 |
#include <new> |
| 10 |
|
| 11 |
#include "jsnum.h" |
| 12 |
|
| 13 |
#include "frontend/CompilationStencil.h" // ScopeStencilRef, CompilationStencil, CompilationState, CompilationAtomCache |
| 14 |
#include "frontend/ParserAtom.h" // frontend::ParserAtomsTable, frontend::ParserAtom |
| 15 |
#include "frontend/ScriptIndex.h" // ScriptIndex |
| 16 |
#include "frontend/Stencil.h" |
| 17 |
#include "util/StringBuilder.h" |
| 18 |
#include "vm/EnvironmentObject.h" |
| 19 |
#include "vm/ErrorReporting.h" // MaybePrintAndClearPendingException |
| 20 |
#include "vm/JSScript.h" |
| 21 |
#include "wasm/WasmDebug.h" |
| 22 |
#include "wasm/WasmInstance.h" |
| 23 |
|
| 24 |
#include "gc/GCContext-inl.h" |
| 25 |
#include "gc/ObjectKind-inl.h" |
| 26 |
#include "gc/TraceMethods-inl.h" |
| 27 |
#include "vm/JSContext-inl.h" |
| 28 |
#include "wasm/WasmInstance-inl.h" |
| 29 |
|
| 30 |
using namespace js; |
| 31 |
using namespace js::frontend; |
| 32 |
|
| 33 |
const char* js::BindingKindString(BindingKind kind) { |
| 34 |
switch (kind) { |
| 35 |
case BindingKind::Import: |
| 36 |
return "import"; |
| 37 |
case BindingKind::FormalParameter: |
| 38 |
return "formal parameter"; |
| 39 |
case BindingKind::Var: |
| 40 |
return "var"; |
| 41 |
case BindingKind::Let: |
| 42 |
return "let"; |
| 43 |
case BindingKind::Const: |
| 44 |
return "const"; |
| 45 |
case BindingKind::NamedLambdaCallee: |
| 46 |
return "named lambda callee"; |
| 47 |
case BindingKind::Synthetic: |
| 48 |
return "synthetic"; |
| 49 |
case BindingKind::PrivateMethod: |
| 50 |
return "private method"; |
| 51 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 52 |
case BindingKind::Using: |
| 53 |
return "using"; |
| 54 |
#endif |
| 55 |
} |
| 56 |
MOZ_CRASH("Bad BindingKind"); |
| 57 |
} |
| 58 |
|
| 59 |
const char* js::ScopeKindString(ScopeKind kind) { |
| 60 |
switch (kind) { |
| 61 |
case ScopeKind::Function: |
| 62 |
return "function"; |
| 63 |
case ScopeKind::FunctionBodyVar: |
| 64 |
return "function body var"; |
| 65 |
case ScopeKind::Lexical: |
| 66 |
return "lexical"; |
| 67 |
case ScopeKind::SimpleCatch: |
| 68 |
case ScopeKind::Catch: |
| 69 |
return "catch"; |
| 70 |
case ScopeKind::NamedLambda: |
| 71 |
return "named lambda"; |
| 72 |
case ScopeKind::StrictNamedLambda: |
| 73 |
return "strict named lambda"; |
| 74 |
case ScopeKind::FunctionLexical: |
| 75 |
return "function lexical"; |
| 76 |
case ScopeKind::ClassBody: |
| 77 |
return "class body"; |
| 78 |
case ScopeKind::With: |
| 79 |
return "with"; |
| 80 |
case ScopeKind::Eval: |
| 81 |
return "eval"; |
| 82 |
case ScopeKind::StrictEval: |
| 83 |
return "strict eval"; |
| 84 |
case ScopeKind::Global: |
| 85 |
return "global"; |
| 86 |
case ScopeKind::NonSyntactic: |
| 87 |
return "non-syntactic"; |
| 88 |
case ScopeKind::Module: |
| 89 |
return "module"; |
| 90 |
case ScopeKind::WasmInstance: |
| 91 |
return "wasm instance"; |
| 92 |
case ScopeKind::WasmFunction: |
| 93 |
return "wasm function"; |
| 94 |
} |
| 95 |
MOZ_CRASH("Bad ScopeKind"); |
| 96 |
} |
| 97 |
|
| 98 |
SharedShape* js::EmptyEnvironmentShape(JSContext* cx, const JSClass* cls, |
| 99 |
uint32_t numSlots, |
| 100 |
ObjectFlags objectFlags) { |
| 101 |
// Put as many slots into the object header as possible. |
| 102 |
uint32_t numFixed = gc::GetGCKindSlots(gc::GetGCObjectKind(numSlots)); |
| 103 |
return SharedShape::getInitialShape( |
| 104 |
cx, cls, cx->realm(), TaggedProto(nullptr), numFixed, objectFlags); |
| 105 |
} |
| 106 |
|
| 107 |
static bool AddToEnvironmentMap(JSContext* cx, const JSClass* clasp, |
| 108 |
HandleId id, BindingKind bindKind, |
| 109 |
uint32_t slot, |
| 110 |
MutableHandle<SharedPropMap*> map, |
| 111 |
uint32_t* mapLength, ObjectFlags* objectFlags) { |
| 112 |
PropertyFlags propFlags = {PropertyFlag::Enumerable}; |
| 113 |
switch (bindKind) { |
| 114 |
case BindingKind::Const: |
| 115 |
case BindingKind::NamedLambdaCallee: |
| 116 |
// Non-writable. |
| 117 |
break; |
| 118 |
default: |
| 119 |
propFlags.setFlag(PropertyFlag::Writable); |
| 120 |
break; |
| 121 |
} |
| 122 |
|
| 123 |
return SharedPropMap::addPropertyWithKnownSlot(cx, clasp, map, mapLength, id, |
| 124 |
propFlags, slot, objectFlags); |
| 125 |
} |
| 126 |
|
| 127 |
SharedShape* js::CreateEnvironmentShape(JSContext* cx, BindingIter& bi, |
| 128 |
const JSClass* cls, uint32_t numSlots, |
| 129 |
ObjectFlags objectFlags) { |
| 130 |
Rooted<SharedPropMap*> map(cx); |
| 131 |
uint32_t mapLength = 0; |
| 132 |
|
| 133 |
RootedId id(cx); |
| 134 |
for (; bi; bi++) { |
| 135 |
BindingLocation loc = bi.location(); |
| 136 |
if (loc.kind() == BindingLocation::Kind::Environment) { |
| 137 |
JSAtom* name = bi.name(); |
| 138 |
MOZ_ASSERT(AtomIsMarked(cx->zone(), name)); |
| 139 |
id = NameToId(name->asPropertyName()); |
| 140 |
if (!AddToEnvironmentMap(cx, cls, id, bi.kind(), loc.slot(), &map, |
| 141 |
&mapLength, &objectFlags)) { |
| 142 |
return nullptr; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
uint32_t numFixed = gc::GetGCKindSlots(gc::GetGCObjectKind(numSlots)); |
| 148 |
return SharedShape::getInitialOrPropMapShape(cx, cls, cx->realm(), |
| 149 |
TaggedProto(nullptr), numFixed, |
| 150 |
map, mapLength, objectFlags); |
| 151 |
} |
| 152 |
|
| 153 |
SharedShape* js::CreateEnvironmentShapeForSyntheticModule( |
| 154 |
JSContext* cx, const JSClass* cls, uint32_t numSlots, |
| 155 |
Handle<ModuleObject*> module) { |
| 156 |
Rooted<SharedPropMap*> map(cx); |
| 157 |
uint32_t mapLength = 0; |
| 158 |
|
| 159 |
PropertyFlags propFlags = {PropertyFlag::Enumerable}; |
| 160 |
ObjectFlags objectFlags = ModuleEnvironmentObject::OBJECT_FLAGS; |
| 161 |
|
| 162 |
RootedId id(cx); |
| 163 |
uint32_t slotIndex = numSlots; |
| 164 |
|
| 165 |
auto addProperty = [&](PropertyName* name) { |
| 166 |
id = NameToId(name); |
| 167 |
return SharedPropMap::addPropertyWithKnownSlot( |
| 168 |
cx, cls, &map, &mapLength, id, propFlags, slotIndex, &objectFlags); |
| 169 |
}; |
| 170 |
|
| 171 |
// Add internal *namespace* property. |
| 172 |
if (!addProperty(cx->names().star_namespace_star_)) { |
| 173 |
return nullptr; |
| 174 |
} |
| 175 |
slotIndex++; |
| 176 |
|
| 177 |
// Add synthetic exports. |
| 178 |
for (JSAtom* exportName : module->syntheticExportNames()) { |
| 179 |
if (!addProperty(exportName->asPropertyName())) { |
| 180 |
return nullptr; |
| 181 |
} |
| 182 |
slotIndex++; |
| 183 |
} |
| 184 |
|
| 185 |
uint32_t numFixed = gc::GetGCKindSlots(gc::GetGCObjectKind(numSlots)); |
| 186 |
return SharedShape::getInitialOrPropMapShape(cx, cls, cx->realm(), |
| 187 |
TaggedProto(nullptr), numFixed, |
| 188 |
map, mapLength, objectFlags); |
| 189 |
} |
| 190 |
|
| 191 |
template <class DataT> |
| 192 |
inline size_t SizeOfAllocatedData(DataT* data) { |
| 193 |
return SizeOfScopeData<DataT>(data->length); |
| 194 |
} |
| 195 |
|
| 196 |
template <typename ConcreteScope> |
| 197 |
static void MarkParserScopeData(typename ConcreteScope::ParserData* data, |
| 198 |
frontend::CompilationState& compilationState) { |
| 199 |
auto names = GetScopeDataTrailingNames(data); |
| 200 |
for (auto& binding : names) { |
| 201 |
auto index = binding.name(); |
| 202 |
if (!index) { |
| 203 |
continue; |
| 204 |
} |
| 205 |
compilationState.parserAtoms.markUsedByStencil( |
| 206 |
index, frontend::ParserAtom::Atomize::Yes); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
template <typename ConcreteScope, typename EnvironmentT> |
| 211 |
static void PrepareScopeData(ParserBindingIter& bi, |
| 212 |
typename ConcreteScope::ParserData* data, |
| 213 |
uint32_t firstFrameSlot, |
| 214 |
mozilla::Maybe<uint32_t>* envShape) { |
| 215 |
const JSClass* cls = &EnvironmentT::class_; |
| 216 |
|
| 217 |
// Iterate through all bindings. This counts the number of environment |
| 218 |
// slots needed and computes the maximum frame slot. |
| 219 |
while (bi) { |
| 220 |
bi++; |
| 221 |
} |
| 222 |
data->slotInfo.nextFrameSlot = |
| 223 |
bi.canHaveFrameSlots() ? bi.nextFrameSlot() : LOCALNO_LIMIT; |
| 224 |
|
| 225 |
// Make a new environment shape if any environment slots were used. |
| 226 |
if (bi.nextEnvironmentSlot() != JSSLOT_FREE(cls)) { |
| 227 |
envShape->emplace(bi.nextEnvironmentSlot()); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
template <typename ConcreteScope> |
| 232 |
static typename ConcreteScope::ParserData* NewEmptyParserScopeData( |
| 233 |
FrontendContext* fc, LifoAlloc& alloc, uint32_t length = 0) { |
| 234 |
using Data = typename ConcreteScope::ParserData; |
| 235 |
|
| 236 |
size_t dataSize = SizeOfScopeData<Data>(length); |
| 237 |
void* raw = alloc.alloc(dataSize); |
| 238 |
if (!raw) { |
| 239 |
js::ReportOutOfMemory(fc); |
| 240 |
return nullptr; |
| 241 |
} |
| 242 |
|
| 243 |
return new (raw) Data(length); |
| 244 |
} |
| 245 |
|
| 246 |
template <typename ConcreteScope, typename AtomT> |
| 247 |
static UniquePtr<AbstractScopeData<ConcreteScope, AtomT>> NewEmptyScopeData( |
| 248 |
JSContext* cx, uint32_t length = 0) { |
| 249 |
using Data = AbstractScopeData<ConcreteScope, AtomT>; |
| 250 |
|
| 251 |
size_t dataSize = SizeOfScopeData<Data>(length); |
| 252 |
uint8_t* bytes = cx->pod_malloc<uint8_t>(dataSize); |
| 253 |
auto data = reinterpret_cast<Data*>(bytes); |
| 254 |
if (data) { |
| 255 |
new (data) Data(length); |
| 256 |
} |
| 257 |
return UniquePtr<Data>(data); |
| 258 |
} |
| 259 |
|
| 260 |
template <typename ConcreteScope> |
| 261 |
static UniquePtr<typename ConcreteScope::RuntimeData> LiftParserScopeData( |
| 262 |
JSContext* cx, frontend::CompilationAtomCache& atomCache, |
| 263 |
BaseParserScopeData* baseData) { |
| 264 |
using ConcreteData = typename ConcreteScope::RuntimeData; |
| 265 |
|
| 266 |
auto* data = static_cast<typename ConcreteScope::ParserData*>(baseData); |
| 267 |
|
| 268 |
// Convert all scope ParserAtoms to rooted JSAtoms. |
| 269 |
// Rooting is necessary as conversion can gc. |
| 270 |
JS::RootedVector<JSAtom*> jsatoms(cx); |
| 271 |
if (!jsatoms.reserve(data->length)) { |
| 272 |
return nullptr; |
| 273 |
} |
| 274 |
auto names = GetScopeDataTrailingNames(data); |
| 275 |
for (size_t i = 0; i < names.size(); i++) { |
| 276 |
JSAtom* jsatom = nullptr; |
| 277 |
if (names[i].name()) { |
| 278 |
jsatom = atomCache.getExistingAtomAt(cx, names[i].name()); |
| 279 |
MOZ_ASSERT(jsatom); |
| 280 |
} |
| 281 |
jsatoms.infallibleAppend(jsatom); |
| 282 |
} |
| 283 |
|
| 284 |
// Allocate a new scope-data of the right kind. |
| 285 |
UniquePtr<ConcreteData> scopeData( |
| 286 |
NewEmptyScopeData<ConcreteScope, JSAtom>(cx, data->length)); |
| 287 |
if (!scopeData) { |
| 288 |
return nullptr; |
| 289 |
} |
| 290 |
|
| 291 |
// NOTE: There shouldn't be any fallible operation or GC between setting |
| 292 |
// `length` and filling `trailingNames`. |
| 293 |
scopeData.get()->length = data->length; |
| 294 |
|
| 295 |
memcpy(&scopeData.get()->slotInfo, &data->slotInfo, |
| 296 |
sizeof(typename ConcreteScope::SlotInfo)); |
| 297 |
|
| 298 |
// Initialize new scoped names. |
| 299 |
auto namesOut = GetScopeDataTrailingNames(scopeData.get()); |
| 300 |
MOZ_ASSERT(data->length == namesOut.size()); |
| 301 |
for (size_t i = 0; i < namesOut.size(); i++) { |
| 302 |
namesOut[i] = names[i].copyWithNewAtom(jsatoms[i].get()); |
| 303 |
} |
| 304 |
|
| 305 |
return scopeData; |
| 306 |
} |
| 307 |
|
| 308 |
/* static */ |
| 309 |
Scope* Scope::create(JSContext* cx, ScopeKind kind, Handle<Scope*> enclosing, |
| 310 |
Handle<SharedShape*> envShape) { |
| 311 |
return cx->newCell<Scope>(kind, enclosing, envShape); |
| 312 |
} |
| 313 |
|
| 314 |
template <typename ConcreteScope> |
| 315 |
/* static */ |
| 316 |
ConcreteScope* Scope::create( |
| 317 |
JSContext* cx, ScopeKind kind, Handle<Scope*> enclosing, |
| 318 |
Handle<SharedShape*> envShape, |
| 319 |
MutableHandle<UniquePtr<typename ConcreteScope::RuntimeData>> data) { |
| 320 |
Scope* scope = create(cx, kind, enclosing, envShape); |
| 321 |
if (!scope) { |
| 322 |
return nullptr; |
| 323 |
} |
| 324 |
|
| 325 |
// It is an invariant that all Scopes that have data (currently, all |
| 326 |
// ScopeKinds except With) must have non-null data. |
| 327 |
MOZ_ASSERT(data); |
| 328 |
scope->initData<ConcreteScope>(data); |
| 329 |
|
| 330 |
return &scope->as<ConcreteScope>(); |
| 331 |
} |
| 332 |
|
| 333 |
template <typename ConcreteScope> |
| 334 |
inline void Scope::initData( |
| 335 |
MutableHandle<UniquePtr<typename ConcreteScope::RuntimeData>> data) { |
| 336 |
MOZ_ASSERT(!rawData()); |
| 337 |
|
| 338 |
AddCellMemory(this, SizeOfAllocatedData(data.get().get()), |
| 339 |
MemoryUse::ScopeData); |
| 340 |
|
| 341 |
setHeaderPtr(data.get().release()); |
| 342 |
} |
| 343 |
|
| 344 |
void Scope::updateEnvShapeIfRequired(mozilla::Maybe<uint32_t>* envShape, |
| 345 |
bool needsEnvironment) { |
| 346 |
if (envShape->isNothing() && needsEnvironment) { |
| 347 |
uint32_t numSlots = 0; |
| 348 |
envShape->emplace(numSlots); |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
uint32_t Scope::firstFrameSlot() const { |
| 353 |
switch (kind()) { |
| 354 |
case ScopeKind::Lexical: |
| 355 |
case ScopeKind::SimpleCatch: |
| 356 |
case ScopeKind::Catch: |
| 357 |
case ScopeKind::FunctionLexical: |
| 358 |
// For intra-frame scopes, find the enclosing scope's next frame slot. |
| 359 |
MOZ_ASSERT(is<LexicalScope>()); |
| 360 |
return LexicalScope::nextFrameSlot(enclosing()); |
| 361 |
|
| 362 |
case ScopeKind::NamedLambda: |
| 363 |
case ScopeKind::StrictNamedLambda: |
| 364 |
// Named lambda scopes cannot have frame slots. |
| 365 |
return LOCALNO_LIMIT; |
| 366 |
|
| 367 |
case ScopeKind::ClassBody: |
| 368 |
MOZ_ASSERT(is<ClassBodyScope>()); |
| 369 |
return ClassBodyScope::nextFrameSlot(enclosing()); |
| 370 |
|
| 371 |
case ScopeKind::FunctionBodyVar: |
| 372 |
if (enclosing()->is<FunctionScope>()) { |
| 373 |
return enclosing()->as<FunctionScope>().nextFrameSlot(); |
| 374 |
} |
| 375 |
break; |
| 376 |
|
| 377 |
default: |
| 378 |
break; |
| 379 |
} |
| 380 |
return 0; |
| 381 |
} |
| 382 |
|
| 383 |
uint32_t Scope::chainLength() const { |
| 384 |
uint32_t length = 0; |
| 385 |
for (ScopeIter si(const_cast<Scope*>(this)); si; si++) { |
| 386 |
length++; |
| 387 |
} |
| 388 |
return length; |
| 389 |
} |
| 390 |
|
| 391 |
uint32_t Scope::environmentChainLength() const { |
| 392 |
uint32_t length = 0; |
| 393 |
for (ScopeIter si(const_cast<Scope*>(this)); si; si++) { |
| 394 |
if (si.hasSyntacticEnvironment()) { |
| 395 |
length++; |
| 396 |
} |
| 397 |
} |
| 398 |
return length; |
| 399 |
} |
| 400 |
|
| 401 |
void Scope::finalize(JS::GCContext* gcx) { |
| 402 |
MOZ_ASSERT(CurrentThreadIsGCFinalizing()); |
| 403 |
applyScopeDataTyped([this, gcx](auto data) { |
| 404 |
gcx->delete_(this, data, SizeOfAllocatedData(data), MemoryUse::ScopeData); |
| 405 |
}); |
| 406 |
setHeaderPtr(nullptr); |
| 407 |
} |
| 408 |
|
| 409 |
size_t Scope::sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const { |
| 410 |
if (rawData()) { |
| 411 |
return mallocSizeOf(rawData()); |
| 412 |
} |
| 413 |
return 0; |
| 414 |
} |
| 415 |
|
| 416 |
void Scope::dump() { |
| 417 |
JSContext* cx = TlsContext.get(); |
| 418 |
if (!cx) { |
| 419 |
fprintf(stderr, "*** can't get JSContext for current thread\n"); |
| 420 |
return; |
| 421 |
} |
| 422 |
for (Rooted<ScopeIter> si(cx, ScopeIter(this)); si; si++) { |
| 423 |
fprintf(stderr, "- %s [%p]\n", ScopeKindString(si.kind()), si.scope()); |
| 424 |
DumpBindings(cx, si.scope()); |
| 425 |
fprintf(stderr, "\n"); |
| 426 |
} |
| 427 |
fprintf(stderr, "\n"); |
| 428 |
} |
| 429 |
|
| 430 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 431 |
|
| 432 |
/* static */ |
| 433 |
bool Scope::dumpForDisassemble(JSContext* cx, JS::Handle<Scope*> scope, |
| 434 |
GenericPrinter& out, const char* indent) { |
| 435 |
out.put(ScopeKindString(scope->kind())); |
| 436 |
out.put(" {"); |
| 437 |
|
| 438 |
size_t i = 0; |
| 439 |
for (Rooted<BindingIter> bi(cx, BindingIter(scope)); bi; bi++, i++) { |
| 440 |
if (i == 0) { |
| 441 |
out.put("\n"); |
| 442 |
} |
| 443 |
UniqueChars bytes = AtomToPrintableString(cx, bi.name()); |
| 444 |
if (!bytes) { |
| 445 |
return false; |
| 446 |
} |
| 447 |
out.put(indent); |
| 448 |
out.printf(" %2zu: %s %s ", i, BindingKindString(bi.kind()), bytes.get()); |
| 449 |
switch (bi.location().kind()) { |
| 450 |
case BindingLocation::Kind::Global: |
| 451 |
if (bi.isTopLevelFunction()) { |
| 452 |
out.put("(global function)\n"); |
| 453 |
} else { |
| 454 |
out.put("(global)\n"); |
| 455 |
} |
| 456 |
break; |
| 457 |
case BindingLocation::Kind::Argument: |
| 458 |
out.printf("(arg slot %u)\n", bi.location().argumentSlot()); |
| 459 |
break; |
| 460 |
case BindingLocation::Kind::Frame: |
| 461 |
out.printf("(frame slot %u)\n", bi.location().slot()); |
| 462 |
break; |
| 463 |
case BindingLocation::Kind::Environment: |
| 464 |
out.printf("(env slot %u)\n", bi.location().slot()); |
| 465 |
break; |
| 466 |
case BindingLocation::Kind::NamedLambdaCallee: |
| 467 |
out.put("(named lambda callee)\n"); |
| 468 |
break; |
| 469 |
case BindingLocation::Kind::Import: |
| 470 |
out.put("(import)\n"); |
| 471 |
break; |
| 472 |
} |
| 473 |
} |
| 474 |
if (i > 0) { |
| 475 |
out.put(indent); |
| 476 |
} |
| 477 |
out.put("}"); |
| 478 |
|
| 479 |
ScopeIter si(scope); |
| 480 |
si++; |
| 481 |
for (; si; si++) { |
| 482 |
out.put(" -> "); |
| 483 |
out.put(ScopeKindString(si.kind())); |
| 484 |
} |
| 485 |
return true; |
| 486 |
} |
| 487 |
|
| 488 |
#endif /* defined(DEBUG) || defined(JS_JITSPEW) */ |
| 489 |
|
| 490 |
static uint32_t NextFrameSlot(Scope* scope) { |
| 491 |
for (ScopeIter si(scope); si; si++) { |
| 492 |
switch (si.kind()) { |
| 493 |
case ScopeKind::With: |
| 494 |
continue; |
| 495 |
|
| 496 |
case ScopeKind::Function: |
| 497 |
return si.scope()->as<FunctionScope>().nextFrameSlot(); |
| 498 |
|
| 499 |
case ScopeKind::FunctionBodyVar: |
| 500 |
return si.scope()->as<VarScope>().nextFrameSlot(); |
| 501 |
|
| 502 |
case ScopeKind::Lexical: |
| 503 |
case ScopeKind::SimpleCatch: |
| 504 |
case ScopeKind::Catch: |
| 505 |
case ScopeKind::FunctionLexical: |
| 506 |
return si.scope()->as<LexicalScope>().nextFrameSlot(); |
| 507 |
|
| 508 |
case ScopeKind::ClassBody: |
| 509 |
return si.scope()->as<ClassBodyScope>().nextFrameSlot(); |
| 510 |
|
| 511 |
case ScopeKind::NamedLambda: |
| 512 |
case ScopeKind::StrictNamedLambda: |
| 513 |
// Named lambda scopes cannot have frame slots. |
| 514 |
return 0; |
| 515 |
|
| 516 |
case ScopeKind::Eval: |
| 517 |
case ScopeKind::StrictEval: |
| 518 |
return si.scope()->as<EvalScope>().nextFrameSlot(); |
| 519 |
|
| 520 |
case ScopeKind::Global: |
| 521 |
case ScopeKind::NonSyntactic: |
| 522 |
return 0; |
| 523 |
|
| 524 |
case ScopeKind::Module: |
| 525 |
return si.scope()->as<ModuleScope>().nextFrameSlot(); |
| 526 |
|
| 527 |
case ScopeKind::WasmInstance: |
| 528 |
case ScopeKind::WasmFunction: |
| 529 |
// Invalid; MOZ_CRASH below. |
| 530 |
break; |
| 531 |
} |
| 532 |
} |
| 533 |
MOZ_CRASH("Not an enclosing intra-frame Scope"); |
| 534 |
} |
| 535 |
|
| 536 |
/* static */ |
| 537 |
uint32_t LexicalScope::nextFrameSlot(Scope* scope) { |
| 538 |
return NextFrameSlot(scope); |
| 539 |
} |
| 540 |
|
| 541 |
/* static */ |
| 542 |
uint32_t ClassBodyScope::nextFrameSlot(Scope* scope) { |
| 543 |
return NextFrameSlot(scope); |
| 544 |
} |
| 545 |
|
| 546 |
/* static */ |
| 547 |
void LexicalScope::prepareForScopeCreation(ScopeKind kind, |
| 548 |
uint32_t firstFrameSlot, |
| 549 |
LexicalScope::ParserData* data, |
| 550 |
mozilla::Maybe<uint32_t>* envShape) { |
| 551 |
bool isNamedLambda = |
| 552 |
kind == ScopeKind::NamedLambda || kind == ScopeKind::StrictNamedLambda; |
| 553 |
|
| 554 |
MOZ_ASSERT_IF(isNamedLambda, firstFrameSlot == LOCALNO_LIMIT); |
| 555 |
|
| 556 |
ParserBindingIter bi(*data, firstFrameSlot, isNamedLambda); |
| 557 |
PrepareScopeData<LexicalScope, BlockLexicalEnvironmentObject>( |
| 558 |
bi, data, firstFrameSlot, envShape); |
| 559 |
} |
| 560 |
|
| 561 |
/* static */ |
| 562 |
SharedShape* LexicalScope::getEmptyExtensibleEnvironmentShape(JSContext* cx) { |
| 563 |
const JSClass* cls = &LexicalEnvironmentObject::class_; |
| 564 |
return EmptyEnvironmentShape(cx, cls, JSSLOT_FREE(cls), ObjectFlags()); |
| 565 |
} |
| 566 |
|
| 567 |
/* static */ |
| 568 |
void ClassBodyScope::prepareForScopeCreation( |
| 569 |
ScopeKind kind, uint32_t firstFrameSlot, ClassBodyScope::ParserData* data, |
| 570 |
mozilla::Maybe<uint32_t>* envShape) { |
| 571 |
MOZ_ASSERT(kind == ScopeKind::ClassBody); |
| 572 |
|
| 573 |
ParserBindingIter bi(*data, firstFrameSlot); |
| 574 |
PrepareScopeData<ClassBodyScope, BlockLexicalEnvironmentObject>( |
| 575 |
bi, data, firstFrameSlot, envShape); |
| 576 |
} |
| 577 |
|
| 578 |
/* static */ |
| 579 |
void FunctionScope::prepareForScopeCreation( |
| 580 |
FunctionScope::ParserData* data, bool hasParameterExprs, |
| 581 |
bool needsEnvironment, mozilla::Maybe<uint32_t>* envShape) { |
| 582 |
uint32_t firstFrameSlot = 0; |
| 583 |
ParserBindingIter bi(*data, hasParameterExprs); |
| 584 |
PrepareScopeData<FunctionScope, CallObject>(bi, data, firstFrameSlot, |
| 585 |
envShape); |
| 586 |
|
| 587 |
if (hasParameterExprs) { |
| 588 |
data->slotInfo.setHasParameterExprs(); |
| 589 |
} |
| 590 |
|
| 591 |
// An environment may be needed regardless of existence of any closed over |
| 592 |
// bindings: |
| 593 |
// - Extensible scopes (i.e., due to direct eval) |
| 594 |
// - Needing a home object |
| 595 |
// - Being a derived class constructor |
| 596 |
// - Being a generator or async function |
| 597 |
// Also see |FunctionBox::needsExtraBodyVarEnvironmentRegardlessOfBindings()|. |
| 598 |
updateEnvShapeIfRequired(envShape, needsEnvironment); |
| 599 |
} |
| 600 |
|
| 601 |
JSScript* FunctionScope::script() const { |
| 602 |
return canonicalFunction()->nonLazyScript(); |
| 603 |
} |
| 604 |
|
| 605 |
/* static */ |
| 606 |
bool FunctionScope::isSpecialName(frontend::TaggedParserAtomIndex name) { |
| 607 |
return name == frontend::TaggedParserAtomIndex::WellKnown::arguments() || |
| 608 |
name == frontend::TaggedParserAtomIndex::WellKnown::dot_this_() || |
| 609 |
name == frontend::TaggedParserAtomIndex::WellKnown::dot_newTarget_() || |
| 610 |
name == frontend::TaggedParserAtomIndex::WellKnown::dot_generator_(); |
| 611 |
} |
| 612 |
|
| 613 |
/* static */ |
| 614 |
void VarScope::prepareForScopeCreation(ScopeKind kind, |
| 615 |
VarScope::ParserData* data, |
| 616 |
uint32_t firstFrameSlot, |
| 617 |
bool needsEnvironment, |
| 618 |
mozilla::Maybe<uint32_t>* envShape) { |
| 619 |
ParserBindingIter bi(*data, firstFrameSlot); |
| 620 |
PrepareScopeData<VarScope, VarEnvironmentObject>(bi, data, firstFrameSlot, |
| 621 |
envShape); |
| 622 |
|
| 623 |
// An environment may be needed regardless of existence of any closed over |
| 624 |
// bindings: |
| 625 |
// - Extensible scopes (i.e., due to direct eval) |
| 626 |
// - Being a generator |
| 627 |
updateEnvShapeIfRequired(envShape, needsEnvironment); |
| 628 |
} |
| 629 |
|
| 630 |
GlobalScope* GlobalScope::createEmpty(JSContext* cx, ScopeKind kind) { |
| 631 |
Rooted<UniquePtr<RuntimeData>> data( |
| 632 |
cx, NewEmptyScopeData<GlobalScope, JSAtom>(cx)); |
| 633 |
if (!data) { |
| 634 |
return nullptr; |
| 635 |
} |
| 636 |
|
| 637 |
return createWithData(cx, kind, &data); |
| 638 |
} |
| 639 |
|
| 640 |
/* static */ |
| 641 |
GlobalScope* GlobalScope::createWithData( |
| 642 |
JSContext* cx, ScopeKind kind, MutableHandle<UniquePtr<RuntimeData>> data) { |
| 643 |
MOZ_ASSERT(data); |
| 644 |
|
| 645 |
// The global scope has no environment shape. Its environment is the |
| 646 |
// global lexical scope and the global object or non-syntactic objects |
| 647 |
// created by embedding, all of which are not only extensible but may |
| 648 |
// have names on them deleted. |
| 649 |
return Scope::create<GlobalScope>(cx, kind, nullptr, nullptr, data); |
| 650 |
} |
| 651 |
|
| 652 |
/* static */ |
| 653 |
WithScope* WithScope::create(JSContext* cx, Handle<Scope*> enclosing) { |
| 654 |
Scope* scope = Scope::create(cx, ScopeKind::With, enclosing, nullptr); |
| 655 |
return static_cast<WithScope*>(scope); |
| 656 |
} |
| 657 |
|
| 658 |
/* static */ |
| 659 |
void EvalScope::prepareForScopeCreation(ScopeKind scopeKind, |
| 660 |
EvalScope::ParserData* data, |
| 661 |
mozilla::Maybe<uint32_t>* envShape) { |
| 662 |
if (scopeKind == ScopeKind::StrictEval) { |
| 663 |
uint32_t firstFrameSlot = 0; |
| 664 |
ParserBindingIter bi(*data, true); |
| 665 |
PrepareScopeData<EvalScope, VarEnvironmentObject>(bi, data, firstFrameSlot, |
| 666 |
envShape); |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
/* static */ |
| 671 |
Scope* EvalScope::nearestVarScopeForDirectEval(Scope* scope) { |
| 672 |
for (ScopeIter si(scope); si; si++) { |
| 673 |
switch (si.kind()) { |
| 674 |
case ScopeKind::Function: |
| 675 |
case ScopeKind::FunctionBodyVar: |
| 676 |
case ScopeKind::Global: |
| 677 |
case ScopeKind::NonSyntactic: |
| 678 |
return scope; |
| 679 |
default: |
| 680 |
break; |
| 681 |
} |
| 682 |
} |
| 683 |
return nullptr; |
| 684 |
} |
| 685 |
|
| 686 |
ModuleScope::RuntimeData::RuntimeData(size_t length) { |
| 687 |
PoisonNames(this, length); |
| 688 |
} |
| 689 |
|
| 690 |
/* static */ |
| 691 |
void ModuleScope::prepareForScopeCreation(ModuleScope::ParserData* data, |
| 692 |
mozilla::Maybe<uint32_t>* envShape) { |
| 693 |
uint32_t firstFrameSlot = 0; |
| 694 |
ParserBindingIter bi(*data); |
| 695 |
PrepareScopeData<ModuleScope, ModuleEnvironmentObject>( |
| 696 |
bi, data, firstFrameSlot, envShape); |
| 697 |
|
| 698 |
// Modules always need an environment object for now. |
| 699 |
bool needsEnvironment = true; |
| 700 |
updateEnvShapeIfRequired(envShape, needsEnvironment); |
| 701 |
} |
| 702 |
|
| 703 |
template <size_t ArrayLength> |
| 704 |
static JSAtom* GenerateWasmName(JSContext* cx, |
| 705 |
const char (&prefix)[ArrayLength], |
| 706 |
uint32_t index) { |
| 707 |
StringBuilder sb(cx); |
| 708 |
if (!sb.append(prefix)) { |
| 709 |
return nullptr; |
| 710 |
} |
| 711 |
if (!NumberValueToStringBuilder(NumberValue(index), sb)) { |
| 712 |
return nullptr; |
| 713 |
} |
| 714 |
|
| 715 |
return sb.finishAtom(); |
| 716 |
} |
| 717 |
|
| 718 |
static void InitializeTrailingName(AbstractBindingName<JSAtom>* trailingNames, |
| 719 |
size_t i, JSAtom* name) { |
| 720 |
void* trailingName = &trailingNames[i]; |
| 721 |
new (trailingName) BindingName(name, false); |
| 722 |
} |
| 723 |
|
| 724 |
template <class DataT> |
| 725 |
static void InitializeNextTrailingName(const Rooted<UniquePtr<DataT>>& data, |
| 726 |
JSAtom* name) { |
| 727 |
InitializeTrailingName(GetScopeDataTrailingNamesPointer(data.get().get()), |
| 728 |
data->length, name); |
| 729 |
data->length++; |
| 730 |
} |
| 731 |
|
| 732 |
WasmInstanceScope::RuntimeData::RuntimeData(size_t length) { |
| 733 |
PoisonNames(this, length); |
| 734 |
} |
| 735 |
|
| 736 |
/* static */ |
| 737 |
WasmInstanceScope* WasmInstanceScope::create(JSContext* cx, |
| 738 |
WasmInstanceObject* instance) { |
| 739 |
size_t namesCount = 0; |
| 740 |
|
| 741 |
size_t memoriesStart = namesCount; |
| 742 |
size_t memoriesCount = instance->instance().codeMeta().memories.length(); |
| 743 |
namesCount += memoriesCount; |
| 744 |
|
| 745 |
size_t globalsStart = namesCount; |
| 746 |
size_t globalsCount = instance->instance().codeMeta().globals.length(); |
| 747 |
namesCount += globalsCount; |
| 748 |
|
| 749 |
Rooted<UniquePtr<RuntimeData>> data( |
| 750 |
cx, NewEmptyScopeData<WasmInstanceScope, JSAtom>(cx, namesCount)); |
| 751 |
if (!data) { |
| 752 |
return nullptr; |
| 753 |
} |
| 754 |
|
| 755 |
Rooted<WasmInstanceObject*> rootedInstance(cx, instance); |
| 756 |
for (size_t i = 0; i < memoriesCount; i++) { |
| 757 |
JSAtom* wasmName = GenerateWasmName(cx, "memory", i); |
| 758 |
if (!wasmName) { |
| 759 |
return nullptr; |
| 760 |
} |
| 761 |
|
| 762 |
InitializeNextTrailingName(data, wasmName); |
| 763 |
} |
| 764 |
|
| 765 |
for (size_t i = 0; i < globalsCount; i++) { |
| 766 |
JSAtom* wasmName = GenerateWasmName(cx, "global", i); |
| 767 |
if (!wasmName) { |
| 768 |
return nullptr; |
| 769 |
} |
| 770 |
|
| 771 |
InitializeNextTrailingName(data, wasmName); |
| 772 |
} |
| 773 |
|
| 774 |
MOZ_ASSERT(data->length == namesCount); |
| 775 |
|
| 776 |
data->instance.init(rootedInstance); |
| 777 |
data->slotInfo.memoriesStart = memoriesStart; |
| 778 |
data->slotInfo.globalsStart = globalsStart; |
| 779 |
|
| 780 |
Rooted<Scope*> enclosing(cx, &cx->global()->emptyGlobalScope()); |
| 781 |
return Scope::create<WasmInstanceScope>(cx, ScopeKind::WasmInstance, |
| 782 |
enclosing, |
| 783 |
/* envShape = */ nullptr, &data); |
| 784 |
} |
| 785 |
|
| 786 |
/* static */ |
| 787 |
WasmFunctionScope* WasmFunctionScope::create(JSContext* cx, |
| 788 |
Handle<Scope*> enclosing, |
| 789 |
uint32_t funcIndex) { |
| 790 |
MOZ_ASSERT(enclosing->is<WasmInstanceScope>()); |
| 791 |
|
| 792 |
Rooted<WasmFunctionScope*> wasmFunctionScope(cx); |
| 793 |
|
| 794 |
Rooted<WasmInstanceObject*> instance( |
| 795 |
cx, enclosing->as<WasmInstanceScope>().instance()); |
| 796 |
|
| 797 |
// TODO pull the local variable names from the wasm function definition. |
| 798 |
wasm::ValTypeVector locals; |
| 799 |
size_t argsLength; |
| 800 |
wasm::StackResults unusedStackResults; |
| 801 |
if (!instance->instance().debug().debugGetLocalTypes( |
| 802 |
funcIndex, &locals, &argsLength, &unusedStackResults)) { |
| 803 |
return nullptr; |
| 804 |
} |
| 805 |
uint32_t namesCount = locals.length(); |
| 806 |
|
| 807 |
Rooted<UniquePtr<RuntimeData>> data( |
| 808 |
cx, NewEmptyScopeData<WasmFunctionScope, JSAtom>(cx, namesCount)); |
| 809 |
if (!data) { |
| 810 |
return nullptr; |
| 811 |
} |
| 812 |
|
| 813 |
for (size_t i = 0; i < namesCount; i++) { |
| 814 |
JSAtom* wasmName = GenerateWasmName(cx, "var", i); |
| 815 |
if (!wasmName) { |
| 816 |
return nullptr; |
| 817 |
} |
| 818 |
|
| 819 |
InitializeNextTrailingName(data, wasmName); |
| 820 |
} |
| 821 |
MOZ_ASSERT(data->length == namesCount); |
| 822 |
|
| 823 |
return Scope::create<WasmFunctionScope>(cx, ScopeKind::WasmFunction, |
| 824 |
enclosing, |
| 825 |
/* envShape = */ nullptr, &data); |
| 826 |
} |
| 827 |
|
| 828 |
ScopeIter::ScopeIter(JSScript* script) : scope_(script->bodyScope()) {} |
| 829 |
|
| 830 |
bool ScopeIter::hasSyntacticEnvironment() const { |
| 831 |
return scope()->hasEnvironment() && |
| 832 |
scope()->kind() != ScopeKind::NonSyntactic; |
| 833 |
} |
| 834 |
|
| 835 |
AbstractBindingIter<JSAtom>::AbstractBindingIter(ScopeKind kind, |
| 836 |
BaseScopeData* data, |
| 837 |
uint32_t firstFrameSlot) |
| 838 |
: BaseAbstractBindingIter<JSAtom>() { |
| 839 |
switch (kind) { |
| 840 |
case ScopeKind::Lexical: |
| 841 |
case ScopeKind::SimpleCatch: |
| 842 |
case ScopeKind::Catch: |
| 843 |
case ScopeKind::FunctionLexical: |
| 844 |
init(*static_cast<LexicalScope::RuntimeData*>(data), firstFrameSlot, 0); |
| 845 |
break; |
| 846 |
case ScopeKind::NamedLambda: |
| 847 |
case ScopeKind::StrictNamedLambda: |
| 848 |
init(*static_cast<LexicalScope::RuntimeData*>(data), LOCALNO_LIMIT, |
| 849 |
IsNamedLambda); |
| 850 |
break; |
| 851 |
case ScopeKind::ClassBody: |
| 852 |
init(*static_cast<ClassBodyScope::RuntimeData*>(data), firstFrameSlot); |
| 853 |
break; |
| 854 |
case ScopeKind::With: |
| 855 |
// With scopes do not have bindings. |
| 856 |
index_ = length_ = 0; |
| 857 |
MOZ_ASSERT(done()); |
| 858 |
break; |
| 859 |
case ScopeKind::Function: { |
| 860 |
uint8_t flags = IgnoreDestructuredFormalParameters; |
| 861 |
if (static_cast<FunctionScope::RuntimeData*>(data) |
| 862 |
->slotInfo.hasParameterExprs()) { |
| 863 |
flags |= HasFormalParameterExprs; |
| 864 |
} |
| 865 |
init(*static_cast<FunctionScope::RuntimeData*>(data), flags); |
| 866 |
break; |
| 867 |
} |
| 868 |
case ScopeKind::FunctionBodyVar: |
| 869 |
init(*static_cast<VarScope::RuntimeData*>(data), firstFrameSlot); |
| 870 |
break; |
| 871 |
case ScopeKind::Eval: |
| 872 |
case ScopeKind::StrictEval: |
| 873 |
init(*static_cast<EvalScope::RuntimeData*>(data), |
| 874 |
kind == ScopeKind::StrictEval); |
| 875 |
break; |
| 876 |
case ScopeKind::Global: |
| 877 |
case ScopeKind::NonSyntactic: |
| 878 |
init(*static_cast<GlobalScope::RuntimeData*>(data)); |
| 879 |
break; |
| 880 |
case ScopeKind::Module: |
| 881 |
init(*static_cast<ModuleScope::RuntimeData*>(data)); |
| 882 |
break; |
| 883 |
case ScopeKind::WasmInstance: |
| 884 |
init(*static_cast<WasmInstanceScope::RuntimeData*>(data)); |
| 885 |
break; |
| 886 |
case ScopeKind::WasmFunction: |
| 887 |
init(*static_cast<WasmFunctionScope::RuntimeData*>(data)); |
| 888 |
break; |
| 889 |
} |
| 890 |
} |
| 891 |
|
| 892 |
AbstractBindingIter<JSAtom>::AbstractBindingIter(Scope* scope) |
| 893 |
: AbstractBindingIter<JSAtom>(scope->kind(), scope->rawData(), |
| 894 |
scope->firstFrameSlot()) {} |
| 895 |
|
| 896 |
AbstractBindingIter<JSAtom>::AbstractBindingIter(JSScript* script) |
| 897 |
: AbstractBindingIter<JSAtom>(script->bodyScope()) {} |
| 898 |
|
| 899 |
AbstractBindingIter<frontend::TaggedParserAtomIndex>::AbstractBindingIter( |
| 900 |
const frontend::ScopeStencilRef& ref) |
| 901 |
: Base() { |
| 902 |
const ScopeStencil& scope = ref.scope(); |
| 903 |
BaseParserScopeData* data = ref.context_.scopeNames[ref.scopeIndex_]; |
| 904 |
switch (scope.kind()) { |
| 905 |
case ScopeKind::Lexical: |
| 906 |
case ScopeKind::SimpleCatch: |
| 907 |
case ScopeKind::Catch: |
| 908 |
case ScopeKind::FunctionLexical: |
| 909 |
init(*static_cast<LexicalScope::ParserData*>(data), |
| 910 |
scope.firstFrameSlot(), 0); |
| 911 |
break; |
| 912 |
case ScopeKind::NamedLambda: |
| 913 |
case ScopeKind::StrictNamedLambda: |
| 914 |
init(*static_cast<LexicalScope::ParserData*>(data), LOCALNO_LIMIT, |
| 915 |
IsNamedLambda); |
| 916 |
break; |
| 917 |
case ScopeKind::ClassBody: |
| 918 |
init(*static_cast<ClassBodyScope::ParserData*>(data), |
| 919 |
scope.firstFrameSlot()); |
| 920 |
break; |
| 921 |
case ScopeKind::With: |
| 922 |
// With scopes do not have bindings. |
| 923 |
index_ = length_ = 0; |
| 924 |
MOZ_ASSERT(done()); |
| 925 |
break; |
| 926 |
case ScopeKind::Function: { |
| 927 |
uint8_t flags = IgnoreDestructuredFormalParameters; |
| 928 |
if (static_cast<FunctionScope::ParserData*>(data) |
| 929 |
->slotInfo.hasParameterExprs()) { |
| 930 |
flags |= HasFormalParameterExprs; |
| 931 |
} |
| 932 |
init(*static_cast<FunctionScope::ParserData*>(data), flags); |
| 933 |
break; |
| 934 |
} |
| 935 |
case ScopeKind::FunctionBodyVar: |
| 936 |
init(*static_cast<VarScope::ParserData*>(data), scope.firstFrameSlot()); |
| 937 |
break; |
| 938 |
case ScopeKind::Eval: |
| 939 |
case ScopeKind::StrictEval: |
| 940 |
init(*static_cast<EvalScope::ParserData*>(data), |
| 941 |
scope.kind() == ScopeKind::StrictEval); |
| 942 |
break; |
| 943 |
case ScopeKind::Global: |
| 944 |
case ScopeKind::NonSyntactic: |
| 945 |
init(*static_cast<GlobalScope::ParserData*>(data)); |
| 946 |
break; |
| 947 |
case ScopeKind::Module: |
| 948 |
init(*static_cast<ModuleScope::ParserData*>(data)); |
| 949 |
break; |
| 950 |
case ScopeKind::WasmInstance: |
| 951 |
init(*static_cast<WasmInstanceScope::ParserData*>(data)); |
| 952 |
break; |
| 953 |
case ScopeKind::WasmFunction: |
| 954 |
init(*static_cast<WasmFunctionScope::ParserData*>(data)); |
| 955 |
break; |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
template <typename NameT> |
| 960 |
void BaseAbstractBindingIter<NameT>::init( |
| 961 |
LexicalScope::AbstractData<NameT>& data, uint32_t firstFrameSlot, |
| 962 |
uint8_t flags) { |
| 963 |
auto& slotInfo = data.slotInfo; |
| 964 |
|
| 965 |
// Named lambda scopes can only have environment slots. If the callee |
| 966 |
// isn't closed over, it is accessed via JSOp::Callee. |
| 967 |
if (flags & IsNamedLambda) { |
| 968 |
// Named lambda binding is weird. Normal BindingKind ordering rules |
| 969 |
// don't apply. |
| 970 |
init(/* positionalFormalStart= */ 0, |
| 971 |
/* nonPositionalFormalStart= */ 0, |
| 972 |
/* varStart= */ 0, |
| 973 |
/* letStart= */ 0, |
| 974 |
/* constStart= */ 0, |
| 975 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 976 |
/* usingStart= */ data.length, |
| 977 |
#endif |
| 978 |
/* syntheticStart= */ data.length, |
| 979 |
/* privageMethodStart= */ data.length, |
| 980 |
/* flags= */ CanHaveEnvironmentSlots | flags, |
| 981 |
/* firstFrameSlot= */ firstFrameSlot, |
| 982 |
/* firstEnvironmentSlot= */ |
| 983 |
JSSLOT_FREE(&LexicalEnvironmentObject::class_), |
| 984 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 985 |
} else { |
| 986 |
// imports - [0, 0) |
| 987 |
// positional formals - [0, 0) |
| 988 |
// other formals - [0, 0) |
| 989 |
// vars - [0, 0) |
| 990 |
// lets - [0, slotInfo.constStart) |
| 991 |
// consts - [slotInfo.constStart, data.length) |
| 992 |
// synthetic - [data.length, data.length) |
| 993 |
// private methods - [data.length, data.length) |
| 994 |
// |
| 995 |
// If ENABLE_EXPLICIT_RESOURCE_MANAGEMENT is set, the consts range is split |
| 996 |
// into the following: |
| 997 |
// consts - [slotInfo.constStart, slotInfo.usingStart) |
| 998 |
// usings - [slotInfo.usingStart, data.length) |
| 999 |
init(/* positionalFormalStart= */ 0, |
| 1000 |
/* nonPositionalFormalStart= */ 0, |
| 1001 |
/* varStart= */ 0, |
| 1002 |
/* letStart= */ 0, |
| 1003 |
/* constStart= */ slotInfo.constStart, |
| 1004 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1005 |
/* usingStart= */ slotInfo.usingStart, |
| 1006 |
#endif |
| 1007 |
/* syntheticStart= */ data.length, |
| 1008 |
/* privateMethodStart= */ data.length, |
| 1009 |
/* flags= */ CanHaveFrameSlots | CanHaveEnvironmentSlots | flags, |
| 1010 |
/* firstFrameSlot= */ firstFrameSlot, |
| 1011 |
/* firstEnvironmentSlot= */ |
| 1012 |
JSSLOT_FREE(&LexicalEnvironmentObject::class_), |
| 1013 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1014 |
} |
| 1015 |
} |
| 1016 |
|
| 1017 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1018 |
LexicalScope::AbstractData<JSAtom>&, uint32_t, uint8_t); |
| 1019 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1020 |
LexicalScope::AbstractData<frontend::TaggedParserAtomIndex>&, uint32_t, |
| 1021 |
uint8_t); |
| 1022 |
|
| 1023 |
template <typename NameT> |
| 1024 |
void BaseAbstractBindingIter<NameT>::init( |
| 1025 |
ClassBodyScope::AbstractData<NameT>& data, uint32_t firstFrameSlot) { |
| 1026 |
auto& slotInfo = data.slotInfo; |
| 1027 |
|
| 1028 |
// imports - [0, 0) |
| 1029 |
// positional formals - [0, 0) |
| 1030 |
// other formals - [0, 0) |
| 1031 |
// vars - [0, 0) |
| 1032 |
// lets - [0, 0) |
| 1033 |
// consts - [0, 0) |
| 1034 |
// synthetic - [0, slotInfo.privateMethodStart) |
| 1035 |
// private methods - [slotInfo.privateMethodStart, data.length) |
| 1036 |
init(/* positionalFormalStart= */ 0, |
| 1037 |
/* nonPositionalFormalStart= */ 0, |
| 1038 |
/* varStart= */ 0, |
| 1039 |
/* letStart= */ 0, |
| 1040 |
/* constStart= */ 0, |
| 1041 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1042 |
/* usingStart= */ 0, |
| 1043 |
#endif |
| 1044 |
/* syntheticStart= */ 0, |
| 1045 |
/* privateMethodStart= */ slotInfo.privateMethodStart, |
| 1046 |
/* flags= */ CanHaveFrameSlots | CanHaveEnvironmentSlots, |
| 1047 |
/* firstFrameSlot= */ firstFrameSlot, |
| 1048 |
/* firstEnvironmentSlot= */ |
| 1049 |
JSSLOT_FREE(&ClassBodyLexicalEnvironmentObject::class_), |
| 1050 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1051 |
} |
| 1052 |
|
| 1053 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1054 |
ClassBodyScope::AbstractData<JSAtom>&, uint32_t); |
| 1055 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1056 |
ClassBodyScope::AbstractData<frontend::TaggedParserAtomIndex>&, uint32_t); |
| 1057 |
|
| 1058 |
template <typename NameT> |
| 1059 |
void BaseAbstractBindingIter<NameT>::init( |
| 1060 |
FunctionScope::AbstractData<NameT>& data, uint8_t flags) { |
| 1061 |
flags = CanHaveFrameSlots | CanHaveEnvironmentSlots | flags; |
| 1062 |
if (!(flags & HasFormalParameterExprs)) { |
| 1063 |
flags |= CanHaveArgumentSlots; |
| 1064 |
} |
| 1065 |
|
| 1066 |
auto length = data.length; |
| 1067 |
auto& slotInfo = data.slotInfo; |
| 1068 |
|
| 1069 |
// imports - [0, 0) |
| 1070 |
// positional formals - [0, slotInfo.nonPositionalFormalStart) |
| 1071 |
// other formals - [slotInfo.nonPositionalParamStart, slotInfo.varStart) |
| 1072 |
// vars - [slotInfo.varStart, length) |
| 1073 |
// lets - [length, length) |
| 1074 |
// consts - [length, length) |
| 1075 |
// synthetic - [length, length) |
| 1076 |
// private methods - [length, length) |
| 1077 |
init(/* positionalFormalStart= */ 0, |
| 1078 |
/* nonPositionalFormalStart= */ slotInfo.nonPositionalFormalStart, |
| 1079 |
/* varStart= */ slotInfo.varStart, |
| 1080 |
/* letStart= */ length, |
| 1081 |
/* constStart= */ length, |
| 1082 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1083 |
/* usingStart= */ length, |
| 1084 |
#endif |
| 1085 |
/* syntheticStart= */ length, |
| 1086 |
/* privateMethodStart= */ length, |
| 1087 |
/* flags= */ flags, |
| 1088 |
/* firstFrameSlot= */ 0, |
| 1089 |
/* firstEnvironmentSlot= */ JSSLOT_FREE(&CallObject::class_), |
| 1090 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1091 |
} |
| 1092 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1093 |
FunctionScope::AbstractData<JSAtom>&, uint8_t); |
| 1094 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1095 |
FunctionScope::AbstractData<frontend::TaggedParserAtomIndex>&, uint8_t); |
| 1096 |
|
| 1097 |
template <typename NameT> |
| 1098 |
void BaseAbstractBindingIter<NameT>::init(VarScope::AbstractData<NameT>& data, |
| 1099 |
uint32_t firstFrameSlot) { |
| 1100 |
auto length = data.length; |
| 1101 |
|
| 1102 |
// imports - [0, 0) |
| 1103 |
// positional formals - [0, 0) |
| 1104 |
// other formals - [0, 0) |
| 1105 |
// vars - [0, length) |
| 1106 |
// lets - [length, length) |
| 1107 |
// consts - [length, length) |
| 1108 |
// synthetic - [length, length) |
| 1109 |
// private methods - [length, length) |
| 1110 |
init(/* positionalFormalStart= */ 0, |
| 1111 |
/* nonPositionalFormalStart= */ 0, |
| 1112 |
/* varStart= */ 0, |
| 1113 |
/* letStart= */ length, |
| 1114 |
/* constStart= */ length, |
| 1115 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1116 |
/* usingStart= */ length, |
| 1117 |
#endif |
| 1118 |
/* syntheticStart= */ length, |
| 1119 |
/* privateMethodStart= */ length, |
| 1120 |
/* flags= */ CanHaveFrameSlots | CanHaveEnvironmentSlots, |
| 1121 |
/* firstFrameSlot= */ firstFrameSlot, |
| 1122 |
/* firstEnvironmentSlot= */ JSSLOT_FREE(&VarEnvironmentObject::class_), |
| 1123 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1124 |
} |
| 1125 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1126 |
VarScope::AbstractData<JSAtom>&, uint32_t); |
| 1127 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1128 |
VarScope::AbstractData<frontend::TaggedParserAtomIndex>&, uint32_t); |
| 1129 |
|
| 1130 |
template <typename NameT> |
| 1131 |
void BaseAbstractBindingIter<NameT>::init( |
| 1132 |
GlobalScope::AbstractData<NameT>& data) { |
| 1133 |
auto& slotInfo = data.slotInfo; |
| 1134 |
|
| 1135 |
// imports - [0, 0) |
| 1136 |
// positional formals - [0, 0) |
| 1137 |
// other formals - [0, 0) |
| 1138 |
// vars - [0, slotInfo.letStart) |
| 1139 |
// lets - [slotInfo.letStart, slotInfo.constStart) |
| 1140 |
// consts - [slotInfo.constStart, data.length) |
| 1141 |
// synthetic - [data.length, data.length) |
| 1142 |
// private methods - [data.length, data.length) |
| 1143 |
init(/* positionalFormalStart= */ 0, |
| 1144 |
/* nonPositionalFormalStart= */ 0, |
| 1145 |
/* varStart= */ 0, |
| 1146 |
/* letStart= */ slotInfo.letStart, |
| 1147 |
/* constStart= */ slotInfo.constStart, |
| 1148 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1149 |
/* usingStart= */ data.length, |
| 1150 |
#endif |
| 1151 |
/* syntheticStart= */ data.length, |
| 1152 |
/* privateMethoodStart= */ data.length, |
| 1153 |
/* flags= */ CannotHaveSlots, |
| 1154 |
/* firstFrameSlot= */ UINT32_MAX, |
| 1155 |
/* firstEnvironmentSlot= */ UINT32_MAX, |
| 1156 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1157 |
} |
| 1158 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1159 |
GlobalScope::AbstractData<JSAtom>&); |
| 1160 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1161 |
GlobalScope::AbstractData<frontend::TaggedParserAtomIndex>&); |
| 1162 |
|
| 1163 |
template <typename NameT> |
| 1164 |
void BaseAbstractBindingIter<NameT>::init(EvalScope::AbstractData<NameT>& data, |
| 1165 |
bool strict) { |
| 1166 |
uint32_t flags; |
| 1167 |
uint32_t firstFrameSlot; |
| 1168 |
uint32_t firstEnvironmentSlot; |
| 1169 |
if (strict) { |
| 1170 |
flags = CanHaveFrameSlots | CanHaveEnvironmentSlots; |
| 1171 |
firstFrameSlot = 0; |
| 1172 |
firstEnvironmentSlot = JSSLOT_FREE(&VarEnvironmentObject::class_); |
| 1173 |
} else { |
| 1174 |
flags = CannotHaveSlots; |
| 1175 |
firstFrameSlot = UINT32_MAX; |
| 1176 |
firstEnvironmentSlot = UINT32_MAX; |
| 1177 |
} |
| 1178 |
|
| 1179 |
auto length = data.length; |
| 1180 |
|
| 1181 |
// imports - [0, 0) |
| 1182 |
// positional formals - [0, 0) |
| 1183 |
// other formals - [0, 0) |
| 1184 |
// vars - [0, length) |
| 1185 |
// lets - [length, length) |
| 1186 |
// consts - [length, length) |
| 1187 |
// synthetic - [length, length) |
| 1188 |
// private methods - [length, length) |
| 1189 |
init(/* positionalFormalStart= */ 0, |
| 1190 |
/* nonPositionalFormalStart= */ 0, |
| 1191 |
/* varStart= */ 0, |
| 1192 |
/* letStart= */ length, |
| 1193 |
/* constStart= */ length, |
| 1194 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1195 |
/* usingStart= */ length, |
| 1196 |
#endif |
| 1197 |
/* syntheticStart= */ length, |
| 1198 |
/* privateMethodStart= */ length, |
| 1199 |
/* flags= */ flags, |
| 1200 |
/* firstFrameSlot= */ firstFrameSlot, |
| 1201 |
/* firstEnvironmentSlot= */ firstEnvironmentSlot, |
| 1202 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1203 |
} |
| 1204 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1205 |
EvalScope::AbstractData<JSAtom>&, bool); |
| 1206 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1207 |
EvalScope::AbstractData<frontend::TaggedParserAtomIndex>&, bool); |
| 1208 |
|
| 1209 |
template <typename NameT> |
| 1210 |
void BaseAbstractBindingIter<NameT>::init( |
| 1211 |
ModuleScope::AbstractData<NameT>& data) { |
| 1212 |
auto& slotInfo = data.slotInfo; |
| 1213 |
|
| 1214 |
// imports - [0, slotInfo.varStart) |
| 1215 |
// positional formals - [slotInfo.varStart, slotInfo.varStart) |
| 1216 |
// other formals - [slotInfo.varStart, slotInfo.varStart) |
| 1217 |
// vars - [slotInfo.varStart, slotInfo.letStart) |
| 1218 |
// lets - [slotInfo.letStart, slotInfo.constStart) |
| 1219 |
// consts - [slotInfo.constStart, data.length) |
| 1220 |
// synthetic - [data.length, data.length) |
| 1221 |
// private methods - [data.length, data.length) |
| 1222 |
// |
| 1223 |
// If ENABLE_EXPLICIT_RESOURCE_MANAGEMENT is set, the consts range is split |
| 1224 |
// into the following: |
| 1225 |
// consts - [slotInfo.constStart, slotInfo.usingStart) |
| 1226 |
// usings - [slotInfo.usingStart, data.length) |
| 1227 |
init( |
| 1228 |
/* positionalFormalStart= */ slotInfo.varStart, |
| 1229 |
/* nonPositionalFormalStart= */ slotInfo.varStart, |
| 1230 |
/* varStart= */ slotInfo.varStart, |
| 1231 |
/* letStart= */ slotInfo.letStart, |
| 1232 |
/* constStart= */ slotInfo.constStart, |
| 1233 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1234 |
/* usingStart= */ slotInfo.usingStart, |
| 1235 |
#endif |
| 1236 |
/* syntheticStart= */ data.length, |
| 1237 |
/* privateMethodStart= */ data.length, |
| 1238 |
/* flags= */ CanHaveFrameSlots | CanHaveEnvironmentSlots, |
| 1239 |
/* firstFrameSlot= */ 0, |
| 1240 |
/* firstEnvironmentSlot= */ JSSLOT_FREE(&ModuleEnvironmentObject::class_), |
| 1241 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1242 |
} |
| 1243 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1244 |
ModuleScope::AbstractData<JSAtom>&); |
| 1245 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1246 |
ModuleScope::AbstractData<frontend::TaggedParserAtomIndex>&); |
| 1247 |
|
| 1248 |
template <typename NameT> |
| 1249 |
void BaseAbstractBindingIter<NameT>::init( |
| 1250 |
WasmInstanceScope::AbstractData<NameT>& data) { |
| 1251 |
auto length = data.length; |
| 1252 |
|
| 1253 |
// imports - [0, 0) |
| 1254 |
// positional formals - [0, 0) |
| 1255 |
// other formals - [0, 0) |
| 1256 |
// vars - [0, length) |
| 1257 |
// lets - [length, length) |
| 1258 |
// consts - [length, length) |
| 1259 |
// synthetic - [length, length) |
| 1260 |
// private methods - [length, length) |
| 1261 |
init(/* positionalFormalStart= */ 0, |
| 1262 |
/* nonPositionalFormalStart= */ 0, |
| 1263 |
/* varStart= */ 0, |
| 1264 |
/* letStart= */ length, |
| 1265 |
/* constStart= */ length, |
| 1266 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1267 |
/* usingStart= */ length, |
| 1268 |
#endif |
| 1269 |
/* syntheticStart= */ length, |
| 1270 |
/* privateMethodStart= */ length, |
| 1271 |
/* flags= */ CanHaveFrameSlots | CanHaveEnvironmentSlots, |
| 1272 |
/* firstFrameSlot= */ UINT32_MAX, |
| 1273 |
/* firstEnvironmentSlot= */ UINT32_MAX, |
| 1274 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1275 |
} |
| 1276 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1277 |
WasmInstanceScope::AbstractData<JSAtom>&); |
| 1278 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1279 |
WasmInstanceScope::AbstractData<frontend::TaggedParserAtomIndex>&); |
| 1280 |
|
| 1281 |
template <typename NameT> |
| 1282 |
void BaseAbstractBindingIter<NameT>::init( |
| 1283 |
WasmFunctionScope::AbstractData<NameT>& data) { |
| 1284 |
auto length = data.length; |
| 1285 |
|
| 1286 |
// imports - [0, 0) |
| 1287 |
// positional formals - [0, 0) |
| 1288 |
// other formals - [0, 0) |
| 1289 |
// vars - [0, length) |
| 1290 |
// lets - [length, length) |
| 1291 |
// consts - [length, length) |
| 1292 |
// synthetic - [length, length) |
| 1293 |
// private methods - [length, length) |
| 1294 |
init(/* positionalFormalStart = */ 0, |
| 1295 |
/* nonPositionalFormalStart = */ 0, |
| 1296 |
/* varStart= */ 0, |
| 1297 |
/* letStart= */ length, |
| 1298 |
/* constStart= */ length, |
| 1299 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1300 |
/* usingStart= */ length, |
| 1301 |
#endif |
| 1302 |
/* syntheticStart= */ length, |
| 1303 |
/* privateMethodStart= */ length, |
| 1304 |
/* flags= */ CanHaveFrameSlots | CanHaveEnvironmentSlots, |
| 1305 |
/* firstFrameSlot= */ UINT32_MAX, |
| 1306 |
/* firstEnvironmentSlot= */ UINT32_MAX, |
| 1307 |
/* names= */ GetScopeDataTrailingNames(&data)); |
| 1308 |
} |
| 1309 |
template void BaseAbstractBindingIter<JSAtom>::init( |
| 1310 |
WasmFunctionScope::AbstractData<JSAtom>&); |
| 1311 |
template void BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>::init( |
| 1312 |
WasmFunctionScope::AbstractData<frontend::TaggedParserAtomIndex>&); |
| 1313 |
|
| 1314 |
AbstractPositionalFormalParameterIter< |
| 1315 |
JSAtom>::AbstractPositionalFormalParameterIter(Scope* scope) |
| 1316 |
: Base(scope) { |
| 1317 |
// Reinit with flags = 0, i.e., iterate over all positional parameters. |
| 1318 |
if (scope->is<FunctionScope>()) { |
| 1319 |
init(scope->as<FunctionScope>().data(), /* flags = */ 0); |
| 1320 |
} |
| 1321 |
settle(); |
| 1322 |
} |
| 1323 |
|
| 1324 |
AbstractPositionalFormalParameterIter< |
| 1325 |
JSAtom>::AbstractPositionalFormalParameterIter(JSScript* script) |
| 1326 |
: AbstractPositionalFormalParameterIter(script->bodyScope()) {} |
| 1327 |
|
| 1328 |
void js::DumpBindings(JSContext* cx, Scope* scopeArg) { |
| 1329 |
Rooted<Scope*> scope(cx, scopeArg); |
| 1330 |
for (Rooted<BindingIter> bi(cx, BindingIter(scope)); bi; bi++) { |
| 1331 |
UniqueChars bytes = AtomToPrintableString(cx, bi.name()); |
| 1332 |
if (!bytes) { |
| 1333 |
MaybePrintAndClearPendingException(cx); |
| 1334 |
return; |
| 1335 |
} |
| 1336 |
fprintf(stderr, " %s %s ", BindingKindString(bi.kind()), bytes.get()); |
| 1337 |
switch (bi.location().kind()) { |
| 1338 |
case BindingLocation::Kind::Global: |
| 1339 |
if (bi.isTopLevelFunction()) { |
| 1340 |
fprintf(stderr, "global function\n"); |
| 1341 |
} else { |
| 1342 |
fprintf(stderr, "global\n"); |
| 1343 |
} |
| 1344 |
break; |
| 1345 |
case BindingLocation::Kind::Argument: |
| 1346 |
fprintf(stderr, "arg slot %u\n", bi.location().argumentSlot()); |
| 1347 |
break; |
| 1348 |
case BindingLocation::Kind::Frame: |
| 1349 |
fprintf(stderr, "frame slot %u\n", bi.location().slot()); |
| 1350 |
break; |
| 1351 |
case BindingLocation::Kind::Environment: |
| 1352 |
fprintf(stderr, "env slot %u\n", bi.location().slot()); |
| 1353 |
break; |
| 1354 |
case BindingLocation::Kind::NamedLambdaCallee: |
| 1355 |
fprintf(stderr, "named lambda callee\n"); |
| 1356 |
break; |
| 1357 |
case BindingLocation::Kind::Import: |
| 1358 |
fprintf(stderr, "import\n"); |
| 1359 |
break; |
| 1360 |
} |
| 1361 |
} |
| 1362 |
} |
| 1363 |
|
| 1364 |
static JSAtom* GetFrameSlotNameInScope(Scope* scope, uint32_t slot) { |
| 1365 |
for (BindingIter bi(scope); bi; bi++) { |
| 1366 |
BindingLocation loc = bi.location(); |
| 1367 |
if (loc.kind() == BindingLocation::Kind::Frame && loc.slot() == slot) { |
| 1368 |
return bi.name(); |
| 1369 |
} |
| 1370 |
} |
| 1371 |
return nullptr; |
| 1372 |
} |
| 1373 |
|
| 1374 |
JSAtom* js::FrameSlotName(JSScript* script, jsbytecode* pc) { |
| 1375 |
MOZ_ASSERT(IsLocalOp(JSOp(*pc))); |
| 1376 |
uint32_t slot = GET_LOCALNO(pc); |
| 1377 |
MOZ_ASSERT(slot < script->nfixed()); |
| 1378 |
|
| 1379 |
// Look for it in the body scope first. |
| 1380 |
if (JSAtom* name = GetFrameSlotNameInScope(script->bodyScope(), slot)) { |
| 1381 |
return name; |
| 1382 |
} |
| 1383 |
|
| 1384 |
// If this is a function script and there is an extra var scope, look for |
| 1385 |
// it there. |
| 1386 |
if (script->functionHasExtraBodyVarScope()) { |
| 1387 |
if (JSAtom* name = GetFrameSlotNameInScope( |
| 1388 |
script->functionExtraBodyVarScope(), slot)) { |
| 1389 |
return name; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
// If not found, look for it in a lexical scope. |
| 1393 |
for (ScopeIter si(script->innermostScope(pc)); si; si++) { |
| 1394 |
if (!si.scope()->is<LexicalScope>() && !si.scope()->is<ClassBodyScope>()) { |
| 1395 |
continue; |
| 1396 |
} |
| 1397 |
|
| 1398 |
// Is the slot within bounds of the current lexical scope? |
| 1399 |
if (slot < si.scope()->firstFrameSlot()) { |
| 1400 |
continue; |
| 1401 |
} |
| 1402 |
if (slot >= LexicalScope::nextFrameSlot(si.scope())) { |
| 1403 |
break; |
| 1404 |
} |
| 1405 |
|
| 1406 |
// If so, get the name. |
| 1407 |
if (JSAtom* name = GetFrameSlotNameInScope(si.scope(), slot)) { |
| 1408 |
return name; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
|
| 1412 |
MOZ_CRASH("Frame slot not found"); |
| 1413 |
} |
| 1414 |
|
| 1415 |
JS::ubi::Node::Size JS::ubi::Concrete<Scope>::size( |
| 1416 |
mozilla::MallocSizeOf mallocSizeOf) const { |
| 1417 |
return js::gc::Arena::thingSize(get().asTenured().getAllocKind()) + |
| 1418 |
get().sizeOfExcludingThis(mallocSizeOf); |
| 1419 |
} |
| 1420 |
|
| 1421 |
template <typename... Args> |
| 1422 |
/* static */ bool ScopeStencil::appendScopeStencilAndData( |
| 1423 |
FrontendContext* fc, CompilationState& compilationState, |
| 1424 |
BaseParserScopeData* data, ScopeIndex* indexOut, Args&&... args) { |
| 1425 |
*indexOut = ScopeIndex(compilationState.scopeData.length()); |
| 1426 |
if (uint32_t(*indexOut) >= TaggedScriptThingIndex::IndexLimit) { |
| 1427 |
ReportAllocationOverflow(fc); |
| 1428 |
return false; |
| 1429 |
} |
| 1430 |
|
| 1431 |
if (!compilationState.scopeData.emplaceBack(std::forward<Args>(args)...)) { |
| 1432 |
js::ReportOutOfMemory(fc); |
| 1433 |
return false; |
| 1434 |
} |
| 1435 |
if (!compilationState.scopeNames.append(data)) { |
| 1436 |
compilationState.scopeData.popBack(); |
| 1437 |
MOZ_ASSERT(compilationState.scopeData.length() == |
| 1438 |
compilationState.scopeNames.length()); |
| 1439 |
|
| 1440 |
js::ReportOutOfMemory(fc); |
| 1441 |
return false; |
| 1442 |
} |
| 1443 |
|
| 1444 |
return true; |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* static */ |
| 1448 |
bool ScopeStencil::createForFunctionScope( |
| 1449 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1450 |
FunctionScope::ParserData* data, bool hasParameterExprs, |
| 1451 |
bool needsEnvironment, ScriptIndex functionIndex, bool isArrow, |
| 1452 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index) { |
| 1453 |
auto kind = ScopeKind::Function; |
| 1454 |
using ScopeType = FunctionScope; |
| 1455 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1456 |
|
| 1457 |
if (data) { |
| 1458 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1459 |
} else { |
| 1460 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1461 |
if (!data) { |
| 1462 |
return false; |
| 1463 |
} |
| 1464 |
} |
| 1465 |
|
| 1466 |
uint32_t firstFrameSlot = 0; |
| 1467 |
mozilla::Maybe<uint32_t> envShape; |
| 1468 |
FunctionScope::prepareForScopeCreation(data, hasParameterExprs, |
| 1469 |
needsEnvironment, &envShape); |
| 1470 |
|
| 1471 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1472 |
enclosing, firstFrameSlot, envShape, |
| 1473 |
mozilla::Some(functionIndex), isArrow); |
| 1474 |
} |
| 1475 |
|
| 1476 |
/* static */ |
| 1477 |
bool ScopeStencil::createForLexicalScope( |
| 1478 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1479 |
ScopeKind kind, LexicalScope::ParserData* data, uint32_t firstFrameSlot, |
| 1480 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index) { |
| 1481 |
using ScopeType = LexicalScope; |
| 1482 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1483 |
|
| 1484 |
if (data) { |
| 1485 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1486 |
} else { |
| 1487 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1488 |
if (!data) { |
| 1489 |
return false; |
| 1490 |
} |
| 1491 |
} |
| 1492 |
|
| 1493 |
mozilla::Maybe<uint32_t> envShape; |
| 1494 |
ScopeType::prepareForScopeCreation(kind, firstFrameSlot, data, &envShape); |
| 1495 |
|
| 1496 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1497 |
enclosing, firstFrameSlot, envShape); |
| 1498 |
} |
| 1499 |
|
| 1500 |
/* static */ |
| 1501 |
bool ScopeStencil::createForClassBodyScope( |
| 1502 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1503 |
ScopeKind kind, ClassBodyScope::ParserData* data, uint32_t firstFrameSlot, |
| 1504 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index) { |
| 1505 |
using ScopeType = ClassBodyScope; |
| 1506 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1507 |
|
| 1508 |
if (data) { |
| 1509 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1510 |
} else { |
| 1511 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1512 |
if (!data) { |
| 1513 |
return false; |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
mozilla::Maybe<uint32_t> envShape; |
| 1518 |
ScopeType::prepareForScopeCreation(kind, firstFrameSlot, data, &envShape); |
| 1519 |
|
| 1520 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1521 |
enclosing, firstFrameSlot, envShape); |
| 1522 |
} |
| 1523 |
|
| 1524 |
bool ScopeStencil::createForVarScope( |
| 1525 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1526 |
ScopeKind kind, VarScope::ParserData* data, uint32_t firstFrameSlot, |
| 1527 |
bool needsEnvironment, mozilla::Maybe<ScopeIndex> enclosing, |
| 1528 |
ScopeIndex* index) { |
| 1529 |
using ScopeType = VarScope; |
| 1530 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1531 |
|
| 1532 |
if (data) { |
| 1533 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1534 |
} else { |
| 1535 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1536 |
if (!data) { |
| 1537 |
return false; |
| 1538 |
} |
| 1539 |
} |
| 1540 |
|
| 1541 |
mozilla::Maybe<uint32_t> envShape; |
| 1542 |
VarScope::prepareForScopeCreation(kind, data, firstFrameSlot, |
| 1543 |
needsEnvironment, &envShape); |
| 1544 |
|
| 1545 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1546 |
enclosing, firstFrameSlot, envShape); |
| 1547 |
} |
| 1548 |
|
| 1549 |
/* static */ |
| 1550 |
bool ScopeStencil::createForGlobalScope( |
| 1551 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1552 |
ScopeKind kind, GlobalScope::ParserData* data, ScopeIndex* index) { |
| 1553 |
using ScopeType = GlobalScope; |
| 1554 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1555 |
|
| 1556 |
if (data) { |
| 1557 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1558 |
} else { |
| 1559 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1560 |
if (!data) { |
| 1561 |
return false; |
| 1562 |
} |
| 1563 |
} |
| 1564 |
|
| 1565 |
// The global scope has no environment shape. Its environment is the |
| 1566 |
// global lexical scope and the global object or non-syntactic objects |
| 1567 |
// created by embedding, all of which are not only extensible but may |
| 1568 |
// have names on them deleted. |
| 1569 |
uint32_t firstFrameSlot = 0; |
| 1570 |
mozilla::Maybe<uint32_t> envShape; |
| 1571 |
|
| 1572 |
mozilla::Maybe<ScopeIndex> enclosing; |
| 1573 |
|
| 1574 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1575 |
enclosing, firstFrameSlot, envShape); |
| 1576 |
} |
| 1577 |
|
| 1578 |
/* static */ |
| 1579 |
bool ScopeStencil::createForEvalScope( |
| 1580 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1581 |
ScopeKind kind, EvalScope::ParserData* data, |
| 1582 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index) { |
| 1583 |
using ScopeType = EvalScope; |
| 1584 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1585 |
|
| 1586 |
if (data) { |
| 1587 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1588 |
} else { |
| 1589 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1590 |
if (!data) { |
| 1591 |
return false; |
| 1592 |
} |
| 1593 |
} |
| 1594 |
|
| 1595 |
uint32_t firstFrameSlot = 0; |
| 1596 |
mozilla::Maybe<uint32_t> envShape; |
| 1597 |
EvalScope::prepareForScopeCreation(kind, data, &envShape); |
| 1598 |
|
| 1599 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1600 |
enclosing, firstFrameSlot, envShape); |
| 1601 |
} |
| 1602 |
|
| 1603 |
/* static */ |
| 1604 |
bool ScopeStencil::createForModuleScope( |
| 1605 |
FrontendContext* fc, frontend::CompilationState& compilationState, |
| 1606 |
ModuleScope::ParserData* data, mozilla::Maybe<ScopeIndex> enclosing, |
| 1607 |
ScopeIndex* index) { |
| 1608 |
auto kind = ScopeKind::Module; |
| 1609 |
using ScopeType = ModuleScope; |
| 1610 |
MOZ_ASSERT(matchScopeKind<ScopeType>(kind)); |
| 1611 |
|
| 1612 |
if (data) { |
| 1613 |
MarkParserScopeData<ScopeType>(data, compilationState); |
| 1614 |
} else { |
| 1615 |
data = NewEmptyParserScopeData<ScopeType>(fc, compilationState.alloc); |
| 1616 |
if (!data) { |
| 1617 |
return false; |
| 1618 |
} |
| 1619 |
} |
| 1620 |
|
| 1621 |
MOZ_ASSERT(enclosing.isNothing()); |
| 1622 |
|
| 1623 |
// The data that's passed in is from the frontend and is LifoAlloc'd. |
| 1624 |
// Copy it now that we're creating a permanent VM scope. |
| 1625 |
uint32_t firstFrameSlot = 0; |
| 1626 |
mozilla::Maybe<uint32_t> envShape; |
| 1627 |
ModuleScope::prepareForScopeCreation(data, &envShape); |
| 1628 |
|
| 1629 |
return appendScopeStencilAndData(fc, compilationState, data, index, kind, |
| 1630 |
enclosing, firstFrameSlot, envShape); |
| 1631 |
} |
| 1632 |
|
| 1633 |
template <typename SpecificEnvironmentT> |
| 1634 |
bool ScopeStencil::createSpecificShape( |
| 1635 |
JSContext* cx, ScopeKind kind, BaseScopeData* scopeData, |
| 1636 |
MutableHandle<SharedShape*> shape) const { |
| 1637 |
const JSClass* cls = &SpecificEnvironmentT::class_; |
| 1638 |
constexpr ObjectFlags objectFlags = SpecificEnvironmentT::OBJECT_FLAGS; |
| 1639 |
|
| 1640 |
if (hasEnvironmentShape()) { |
| 1641 |
if (numEnvironmentSlots() > 0) { |
| 1642 |
BindingIter bi(kind, scopeData, firstFrameSlot_); |
| 1643 |
shape.set(CreateEnvironmentShape(cx, bi, cls, numEnvironmentSlots(), |
| 1644 |
objectFlags)); |
| 1645 |
return shape; |
| 1646 |
} |
| 1647 |
|
| 1648 |
shape.set(EmptyEnvironmentShape(cx, cls, JSSLOT_FREE(cls), objectFlags)); |
| 1649 |
return shape; |
| 1650 |
} |
| 1651 |
|
| 1652 |
return true; |
| 1653 |
} |
| 1654 |
|
| 1655 |
/* static */ |
| 1656 |
bool ScopeStencil::createForWithScope(FrontendContext* fc, |
| 1657 |
CompilationState& compilationState, |
| 1658 |
mozilla::Maybe<ScopeIndex> enclosing, |
| 1659 |
ScopeIndex* index) { |
| 1660 |
auto kind = ScopeKind::With; |
| 1661 |
MOZ_ASSERT(matchScopeKind<WithScope>(kind)); |
| 1662 |
|
| 1663 |
uint32_t firstFrameSlot = 0; |
| 1664 |
mozilla::Maybe<uint32_t> envShape; |
| 1665 |
|
| 1666 |
return appendScopeStencilAndData(fc, compilationState, nullptr, index, kind, |
| 1667 |
enclosing, firstFrameSlot, envShape); |
| 1668 |
} |
| 1669 |
|
| 1670 |
template <typename SpecificScopeT> |
| 1671 |
UniquePtr<typename SpecificScopeT::RuntimeData> |
| 1672 |
ScopeStencil::createSpecificScopeData(JSContext* cx, |
| 1673 |
CompilationAtomCache& atomCache, |
| 1674 |
BaseParserScopeData* baseData) const { |
| 1675 |
return LiftParserScopeData<SpecificScopeT>(cx, atomCache, baseData); |
| 1676 |
} |
| 1677 |
|
| 1678 |
template <> |
| 1679 |
UniquePtr<FunctionScope::RuntimeData> |
| 1680 |
ScopeStencil::createSpecificScopeData<FunctionScope>( |
| 1681 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1682 |
BaseParserScopeData* baseData) const { |
| 1683 |
// Allocate a new vm function-scope. |
| 1684 |
UniquePtr<FunctionScope::RuntimeData> data = |
| 1685 |
LiftParserScopeData<FunctionScope>(cx, atomCache, baseData); |
| 1686 |
if (!data) { |
| 1687 |
return nullptr; |
| 1688 |
} |
| 1689 |
|
| 1690 |
return data; |
| 1691 |
} |
| 1692 |
|
| 1693 |
template <> |
| 1694 |
UniquePtr<ModuleScope::RuntimeData> |
| 1695 |
ScopeStencil::createSpecificScopeData<ModuleScope>( |
| 1696 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1697 |
BaseParserScopeData* baseData) const { |
| 1698 |
// Allocate a new vm module-scope. |
| 1699 |
UniquePtr<ModuleScope::RuntimeData> data = |
| 1700 |
LiftParserScopeData<ModuleScope>(cx, atomCache, baseData); |
| 1701 |
if (!data) { |
| 1702 |
return nullptr; |
| 1703 |
} |
| 1704 |
|
| 1705 |
return data; |
| 1706 |
} |
| 1707 |
|
| 1708 |
// WithScope does not use binding data. |
| 1709 |
template <> |
| 1710 |
Scope* ScopeStencil::createSpecificScope<WithScope, std::nullptr_t>( |
| 1711 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1712 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const { |
| 1713 |
return Scope::create(cx, ScopeKind::With, enclosingScope, nullptr); |
| 1714 |
} |
| 1715 |
|
| 1716 |
// GlobalScope has bindings but no environment shape. |
| 1717 |
template <> |
| 1718 |
Scope* ScopeStencil::createSpecificScope<GlobalScope, std::nullptr_t>( |
| 1719 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1720 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const { |
| 1721 |
Rooted<UniquePtr<GlobalScope::RuntimeData>> rootedData( |
| 1722 |
cx, createSpecificScopeData<GlobalScope>(cx, atomCache, baseData)); |
| 1723 |
if (!rootedData) { |
| 1724 |
return nullptr; |
| 1725 |
} |
| 1726 |
|
| 1727 |
MOZ_ASSERT(!hasEnclosing()); |
| 1728 |
MOZ_ASSERT(!enclosingScope); |
| 1729 |
|
| 1730 |
// Because we already baked the data here, we needn't do it again. |
| 1731 |
return Scope::create<GlobalScope>(cx, kind(), nullptr, nullptr, &rootedData); |
| 1732 |
} |
| 1733 |
|
| 1734 |
template <typename SpecificScopeT, typename SpecificEnvironmentT> |
| 1735 |
Scope* ScopeStencil::createSpecificScope(JSContext* cx, |
| 1736 |
CompilationAtomCache& atomCache, |
| 1737 |
Handle<Scope*> enclosingScope, |
| 1738 |
BaseParserScopeData* baseData) const { |
| 1739 |
Rooted<UniquePtr<typename SpecificScopeT::RuntimeData>> rootedData( |
| 1740 |
cx, createSpecificScopeData<SpecificScopeT>(cx, atomCache, baseData)); |
| 1741 |
if (!rootedData) { |
| 1742 |
return nullptr; |
| 1743 |
} |
| 1744 |
|
| 1745 |
Rooted<SharedShape*> shape(cx); |
| 1746 |
if (!createSpecificShape<SpecificEnvironmentT>( |
| 1747 |
cx, kind(), rootedData.get().get(), &shape)) { |
| 1748 |
return nullptr; |
| 1749 |
} |
| 1750 |
|
| 1751 |
// Because we already baked the data here, we needn't do it again. |
| 1752 |
return Scope::create<SpecificScopeT>(cx, kind(), enclosingScope, shape, |
| 1753 |
&rootedData); |
| 1754 |
} |
| 1755 |
|
| 1756 |
template Scope* ScopeStencil::createSpecificScope<FunctionScope, CallObject>( |
| 1757 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1758 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const; |
| 1759 |
template Scope* |
| 1760 |
ScopeStencil::createSpecificScope<LexicalScope, BlockLexicalEnvironmentObject>( |
| 1761 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1762 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const; |
| 1763 |
template Scope* ScopeStencil::createSpecificScope< |
| 1764 |
ClassBodyScope, BlockLexicalEnvironmentObject>( |
| 1765 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1766 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const; |
| 1767 |
template Scope* |
| 1768 |
ScopeStencil::createSpecificScope<EvalScope, VarEnvironmentObject>( |
| 1769 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1770 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const; |
| 1771 |
template Scope* |
| 1772 |
ScopeStencil::createSpecificScope<VarScope, VarEnvironmentObject>( |
| 1773 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1774 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const; |
| 1775 |
template Scope* |
| 1776 |
ScopeStencil::createSpecificScope<ModuleScope, ModuleEnvironmentObject>( |
| 1777 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 1778 |
Handle<Scope*> enclosingScope, BaseParserScopeData* baseData) const; |