| 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_Scope_h |
| 8 |
#define vm_Scope_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT, MOZ_ASSERT_IF |
| 11 |
#include "mozilla/Attributes.h" // MOZ_IMPLICIT, MOZ_INIT_OUTSIDE_CTOR, MOZ_STACK_CLASS |
| 12 |
#include "mozilla/Casting.h" // mozilla::AssertedCast |
| 13 |
#include "mozilla/Maybe.h" // mozilla::Maybe |
| 14 |
#include "mozilla/MemoryReporting.h" // mozilla::MallocSizeOf |
| 15 |
#include "mozilla/Span.h" // mozilla::Span |
| 16 |
|
| 17 |
#include <algorithm> // std::fill_n |
| 18 |
#include <stddef.h> // size_t |
| 19 |
#include <stdint.h> // uint8_t, uint16_t, uint32_t, uintptr_t |
| 20 |
#include <type_traits> // std::is_same_v, std::is_base_of_v |
| 21 |
|
| 22 |
#include "builtin/ModuleObject.h" // ModuleObject, Handle<ModuleObject*> |
| 23 |
#include "frontend/ParserAtom.h" // frontend::TaggedParserAtomIndex |
| 24 |
#include "gc/Barrier.h" // GCPtr |
| 25 |
#include "gc/Cell.h" // TenuredCellWithNonGCPointer |
| 26 |
#include "js/GCPolicyAPI.h" // GCPolicy, IgnoreGCPolicy |
| 27 |
#include "js/HeapAPI.h" // CellFlagBitsReservedForGC |
| 28 |
#include "js/RootingAPI.h" // Handle, MutableHandle |
| 29 |
#include "js/TraceKind.h" // JS::TraceKind |
| 30 |
#include "js/TypeDecls.h" // HandleFunction |
| 31 |
#include "js/UbiNode.h" // ubi::* |
| 32 |
#include "js/UniquePtr.h" // UniquePtr |
| 33 |
#include "util/Poison.h" // AlwaysPoison, JS_SCOPE_DATA_TRAILING_NAMES_PATTERN, MemCheckKind |
| 34 |
#include "vm/JSFunction.h" // JSFunction |
| 35 |
#include "vm/ScopeKind.h" // ScopeKind |
| 36 |
#include "vm/Shape.h" // Shape |
| 37 |
#include "wasm/WasmJS.h" // WasmInstanceObject |
| 38 |
|
| 39 |
class JSAtom; |
| 40 |
class JSScript; |
| 41 |
class JSTracer; |
| 42 |
struct JSContext; |
| 43 |
|
| 44 |
namespace js { |
| 45 |
|
| 46 |
class JS_PUBLIC_API GenericPrinter; |
| 47 |
|
| 48 |
namespace frontend { |
| 49 |
class ScopeStencil; |
| 50 |
struct ScopeStencilRef; |
| 51 |
class RuntimeScopeBindingCache; |
| 52 |
} // namespace frontend |
| 53 |
|
| 54 |
template <typename NameT> |
| 55 |
class AbstractBaseScopeData; |
| 56 |
|
| 57 |
template <typename NameT> |
| 58 |
class BaseAbstractBindingIter; |
| 59 |
|
| 60 |
template <typename NameT> |
| 61 |
class AbstractBindingIter; |
| 62 |
|
| 63 |
template <typename NameT> |
| 64 |
class AbstractPositionalFormalParameterIter; |
| 65 |
|
| 66 |
using BindingIter = AbstractBindingIter<JSAtom>; |
| 67 |
|
| 68 |
class AbstractScopePtr; |
| 69 |
|
| 70 |
static inline bool ScopeKindIsCatch(ScopeKind kind) { |
| 71 |
return kind == ScopeKind::SimpleCatch || kind == ScopeKind::Catch; |
| 72 |
} |
| 73 |
|
| 74 |
static inline bool ScopeKindIsInBody(ScopeKind kind) { |
| 75 |
return kind == ScopeKind::Lexical || kind == ScopeKind::SimpleCatch || |
| 76 |
kind == ScopeKind::Catch || kind == ScopeKind::With || |
| 77 |
kind == ScopeKind::FunctionLexical || |
| 78 |
kind == ScopeKind::FunctionBodyVar || kind == ScopeKind::ClassBody; |
| 79 |
} |
| 80 |
|
| 81 |
const char* BindingKindString(BindingKind kind); |
| 82 |
const char* ScopeKindString(ScopeKind kind); |
| 83 |
|
| 84 |
template <typename NameT> |
| 85 |
class AbstractBindingName; |
| 86 |
|
| 87 |
template <> |
| 88 |
class AbstractBindingName<JSAtom> { |
| 89 |
public: |
| 90 |
using NameT = JSAtom; |
| 91 |
using NamePointerT = NameT*; |
| 92 |
|
| 93 |
private: |
| 94 |
// A JSAtom* with its low bit used as a tag for the: |
| 95 |
// * whether it is closed over (i.e., exists in the environment shape) |
| 96 |
// * whether it is a top-level function binding in global or eval scope, |
| 97 |
// instead of var binding (both are in the same range in Scope data) |
| 98 |
uintptr_t bits_; |
| 99 |
|
| 100 |
static constexpr uintptr_t ClosedOverFlag = 0x1; |
| 101 |
// TODO: We should reuse this bit for let vs class distinction to |
| 102 |
// show the better redeclaration error message (bug 1428672). |
| 103 |
static constexpr uintptr_t TopLevelFunctionFlag = 0x2; |
| 104 |
static constexpr uintptr_t FlagMask = 0x3; |
| 105 |
|
| 106 |
public: |
| 107 |
AbstractBindingName() : bits_(0) {} |
| 108 |
|
| 109 |
AbstractBindingName(NameT* name, bool closedOver, |
| 110 |
bool isTopLevelFunction = false) |
| 111 |
: bits_(uintptr_t(name) | (closedOver ? ClosedOverFlag : 0x0) | |
| 112 |
(isTopLevelFunction ? TopLevelFunctionFlag : 0x0)) {} |
| 113 |
|
| 114 |
NamePointerT name() const { |
| 115 |
return reinterpret_cast<NameT*>(bits_ & ~FlagMask); |
| 116 |
} |
| 117 |
|
| 118 |
bool closedOver() const { return bits_ & ClosedOverFlag; } |
| 119 |
|
| 120 |
private: |
| 121 |
friend class BaseAbstractBindingIter<NameT>; |
| 122 |
|
| 123 |
// This method should be called only for binding names in `vars` range in |
| 124 |
// BindingIter. |
| 125 |
bool isTopLevelFunction() const { return bits_ & TopLevelFunctionFlag; } |
| 126 |
|
| 127 |
public: |
| 128 |
void trace(JSTracer* trc) { |
| 129 |
if (JSAtom* atom = name()) { |
| 130 |
TraceManuallyBarrieredEdge(trc, &atom, "binding name"); |
| 131 |
} |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
template <> |
| 136 |
class AbstractBindingName<frontend::TaggedParserAtomIndex> { |
| 137 |
uint32_t bits_; |
| 138 |
|
| 139 |
using TaggedParserAtomIndex = frontend::TaggedParserAtomIndex; |
| 140 |
|
| 141 |
public: |
| 142 |
using NameT = TaggedParserAtomIndex; |
| 143 |
using NamePointerT = NameT; |
| 144 |
|
| 145 |
private: |
| 146 |
static constexpr size_t TaggedIndexBit = TaggedParserAtomIndex::IndexBit + 2; |
| 147 |
|
| 148 |
static constexpr size_t FlagShift = TaggedIndexBit; |
| 149 |
static constexpr size_t FlagBit = 2; |
| 150 |
static constexpr uint32_t FlagMask = BitMask(FlagBit) << FlagShift; |
| 151 |
|
| 152 |
static constexpr uint32_t ClosedOverFlag = 1 << FlagShift; |
| 153 |
static constexpr uint32_t TopLevelFunctionFlag = 2 << FlagShift; |
| 154 |
|
| 155 |
public: |
| 156 |
AbstractBindingName() : bits_(TaggedParserAtomIndex::NullTag) { |
| 157 |
// TaggedParserAtomIndex's tags shouldn't overlap with flags. |
| 158 |
static_assert((TaggedParserAtomIndex::NullTag & FlagMask) == 0); |
| 159 |
static_assert((TaggedParserAtomIndex::ParserAtomIndexTag & FlagMask) == 0); |
| 160 |
static_assert((TaggedParserAtomIndex::WellKnownTag & FlagMask) == 0); |
| 161 |
} |
| 162 |
|
| 163 |
AbstractBindingName(TaggedParserAtomIndex name, bool closedOver, |
| 164 |
bool isTopLevelFunction = false) |
| 165 |
: bits_(name.rawData() | (closedOver ? ClosedOverFlag : 0x0) | |
| 166 |
(isTopLevelFunction ? TopLevelFunctionFlag : 0x0)) {} |
| 167 |
|
| 168 |
public: |
| 169 |
NamePointerT name() const { |
| 170 |
return TaggedParserAtomIndex::fromRaw(bits_ & ~FlagMask); |
| 171 |
} |
| 172 |
|
| 173 |
bool closedOver() const { return bits_ & ClosedOverFlag; } |
| 174 |
|
| 175 |
AbstractBindingName<JSAtom> copyWithNewAtom(JSAtom* newName) const { |
| 176 |
return AbstractBindingName<JSAtom>(newName, closedOver(), |
| 177 |
isTopLevelFunction()); |
| 178 |
} |
| 179 |
|
| 180 |
void updateNameAfterStencilMerge(TaggedParserAtomIndex name) { |
| 181 |
bits_ = (bits_ & FlagMask) | name.rawData(); |
| 182 |
} |
| 183 |
|
| 184 |
private: |
| 185 |
friend class BaseAbstractBindingIter<TaggedParserAtomIndex>; |
| 186 |
friend class frontend::ScopeStencil; |
| 187 |
|
| 188 |
// This method should be called only for binding names in `vars` range in |
| 189 |
// BindingIter. |
| 190 |
bool isTopLevelFunction() const { return bits_ & TopLevelFunctionFlag; } |
| 191 |
}; |
| 192 |
|
| 193 |
using BindingName = AbstractBindingName<JSAtom>; |
| 194 |
|
| 195 |
static inline void TraceBindingNames(JSTracer* trc, BindingName* names, |
| 196 |
uint32_t length) { |
| 197 |
for (uint32_t i = 0; i < length; i++) { |
| 198 |
JSAtom* name = names[i].name(); |
| 199 |
MOZ_ASSERT(name); |
| 200 |
TraceManuallyBarrieredEdge(trc, &name, "scope name"); |
| 201 |
} |
| 202 |
}; |
| 203 |
static inline void TraceNullableBindingNames(JSTracer* trc, BindingName* names, |
| 204 |
uint32_t length) { |
| 205 |
for (uint32_t i = 0; i < length; i++) { |
| 206 |
if (JSAtom* name = names[i].name()) { |
| 207 |
TraceManuallyBarrieredEdge(trc, &name, "scope name"); |
| 208 |
} |
| 209 |
} |
| 210 |
}; |
| 211 |
|
| 212 |
const size_t ScopeDataAlignBytes = size_t(1) << gc::CellFlagBitsReservedForGC; |
| 213 |
|
| 214 |
/** |
| 215 |
* Base class for scope {Runtime,Parser}Data classes to inherit from. |
| 216 |
* |
| 217 |
* `js::Scope` stores a pointer to RuntimeData classes in their first word, so |
| 218 |
* they must be suitably aligned to allow storing GC flags in the low bits. |
| 219 |
*/ |
| 220 |
template <typename NameT> |
| 221 |
class AbstractBaseScopeData { |
| 222 |
public: |
| 223 |
using NameType = NameT; |
| 224 |
|
| 225 |
// The length of names after specialized ScopeData subclasses. |
| 226 |
uint32_t length = 0; |
| 227 |
}; |
| 228 |
|
| 229 |
template <typename ScopeDataT> |
| 230 |
static inline void AssertDerivedScopeData() { |
| 231 |
static_assert( |
| 232 |
!std::is_same_v<ScopeDataT, |
| 233 |
AbstractBaseScopeData<typename ScopeDataT::NameType>>, |
| 234 |
"ScopeDataT shouldn't be AbstractBaseScopeData"); |
| 235 |
static_assert( |
| 236 |
std::is_base_of_v<AbstractBaseScopeData<typename ScopeDataT::NameType>, |
| 237 |
ScopeDataT>, |
| 238 |
"ScopeDataT should be subclass of AbstractBaseScopeData"); |
| 239 |
} |
| 240 |
|
| 241 |
template <typename ScopeDataT> |
| 242 |
static inline size_t GetOffsetOfScopeDataTrailingNames() { |
| 243 |
AssertDerivedScopeData<ScopeDataT>(); |
| 244 |
return sizeof(ScopeDataT); |
| 245 |
} |
| 246 |
|
| 247 |
template <typename ScopeDataT> |
| 248 |
static inline AbstractBindingName<typename ScopeDataT::NameType>* |
| 249 |
GetScopeDataTrailingNamesPointer(ScopeDataT* data) { |
| 250 |
AssertDerivedScopeData<ScopeDataT>(); |
| 251 |
return reinterpret_cast<AbstractBindingName<typename ScopeDataT::NameType>*>( |
| 252 |
data + 1); |
| 253 |
} |
| 254 |
|
| 255 |
template <typename ScopeDataT> |
| 256 |
static inline const AbstractBindingName<typename ScopeDataT::NameType>* |
| 257 |
GetScopeDataTrailingNamesPointer(const ScopeDataT* data) { |
| 258 |
AssertDerivedScopeData<ScopeDataT>(); |
| 259 |
return reinterpret_cast< |
| 260 |
const AbstractBindingName<typename ScopeDataT::NameType>*>(data + 1); |
| 261 |
} |
| 262 |
|
| 263 |
template <typename ScopeDataT> |
| 264 |
static inline mozilla::Span<AbstractBindingName<typename ScopeDataT::NameType>> |
| 265 |
GetScopeDataTrailingNames(ScopeDataT* data) { |
| 266 |
return mozilla::Span(GetScopeDataTrailingNamesPointer(data), data->length); |
| 267 |
} |
| 268 |
|
| 269 |
template <typename ScopeDataT> |
| 270 |
static inline mozilla::Span< |
| 271 |
const AbstractBindingName<typename ScopeDataT::NameType>> |
| 272 |
GetScopeDataTrailingNames(const ScopeDataT* data) { |
| 273 |
return mozilla::Span(GetScopeDataTrailingNamesPointer(data), data->length); |
| 274 |
} |
| 275 |
|
| 276 |
using BaseScopeData = AbstractBaseScopeData<JSAtom>; |
| 277 |
|
| 278 |
inline void PoisonNames(AbstractBindingName<JSAtom>* data, uint32_t length) { |
| 279 |
AlwaysPoison(data, JS_SCOPE_DATA_TRAILING_NAMES_PATTERN, |
| 280 |
sizeof(AbstractBindingName<JSAtom>) * length, |
| 281 |
MemCheckKind::MakeUndefined); |
| 282 |
} |
| 283 |
|
| 284 |
// frontend::TaggedParserAtomIndex doesn't require poison value. |
| 285 |
// Fill with null value instead. |
| 286 |
inline void PoisonNames( |
| 287 |
AbstractBindingName<frontend::TaggedParserAtomIndex>* data, |
| 288 |
uint32_t length) { |
| 289 |
std::fill_n(data, length, |
| 290 |
AbstractBindingName<frontend::TaggedParserAtomIndex>()); |
| 291 |
} |
| 292 |
|
| 293 |
template <typename ScopeDataT> |
| 294 |
static inline void PoisonNames(ScopeDataT* data, uint32_t length) { |
| 295 |
if (length) { |
| 296 |
PoisonNames(GetScopeDataTrailingNamesPointer(data), length); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
// |
| 301 |
// Allow using is<T> and as<T> on Rooted<Scope*> and Handle<Scope*>. |
| 302 |
// |
| 303 |
template <typename Wrapper> |
| 304 |
class WrappedPtrOperations<Scope*, Wrapper> { |
| 305 |
public: |
| 306 |
template <class U> |
| 307 |
JS::Handle<U*> as() const { |
| 308 |
const Wrapper& self = *static_cast<const Wrapper*>(this); |
| 309 |
MOZ_ASSERT_IF(self, self->template is<U>()); |
| 310 |
return Handle<U*>::fromMarkedLocation( |
| 311 |
reinterpret_cast<U* const*>(self.address())); |
| 312 |
} |
| 313 |
}; |
| 314 |
|
| 315 |
// |
| 316 |
// The base class of all Scopes. |
| 317 |
// |
| 318 |
class Scope : public gc::TenuredCellWithNonGCPointer<BaseScopeData> { |
| 319 |
friend class GCMarker; |
| 320 |
friend class frontend::ScopeStencil; |
| 321 |
friend class js::AbstractBindingIter<JSAtom>; |
| 322 |
friend class js::frontend::RuntimeScopeBindingCache; |
| 323 |
friend class gc::CellAllocator; |
| 324 |
|
| 325 |
protected: |
| 326 |
// The raw data pointer, stored in the cell header. |
| 327 |
BaseScopeData* rawData() { return headerPtr(); } |
| 328 |
const BaseScopeData* rawData() const { return headerPtr(); } |
| 329 |
|
| 330 |
// The kind determines data_. |
| 331 |
const ScopeKind kind_; |
| 332 |
|
| 333 |
// If there are any aliased bindings, the shape for the |
| 334 |
// EnvironmentObject. Otherwise nullptr. |
| 335 |
const GCPtr<SharedShape*> environmentShape_; |
| 336 |
|
| 337 |
// The enclosing scope or nullptr. |
| 338 |
GCPtr<Scope*> enclosingScope_; |
| 339 |
|
| 340 |
Scope(ScopeKind kind, Scope* enclosing, SharedShape* environmentShape) |
| 341 |
: TenuredCellWithNonGCPointer(nullptr), |
| 342 |
kind_(kind), |
| 343 |
environmentShape_(environmentShape), |
| 344 |
enclosingScope_(enclosing) {} |
| 345 |
|
| 346 |
static Scope* create(JSContext* cx, ScopeKind kind, Handle<Scope*> enclosing, |
| 347 |
Handle<SharedShape*> envShape); |
| 348 |
|
| 349 |
template <typename ConcreteScope> |
| 350 |
void initData( |
| 351 |
MutableHandle<UniquePtr<typename ConcreteScope::RuntimeData>> data); |
| 352 |
|
| 353 |
template <typename F> |
| 354 |
void applyScopeDataTyped(F&& f); |
| 355 |
|
| 356 |
static void updateEnvShapeIfRequired(mozilla::Maybe<uint32_t>* envShape, |
| 357 |
bool needsEnvironment); |
| 358 |
|
| 359 |
public: |
| 360 |
template <typename ConcreteScope> |
| 361 |
static ConcreteScope* create( |
| 362 |
JSContext* cx, ScopeKind kind, Handle<Scope*> enclosing, |
| 363 |
Handle<SharedShape*> envShape, |
| 364 |
MutableHandle<UniquePtr<typename ConcreteScope::RuntimeData>> data); |
| 365 |
|
| 366 |
static const JS::TraceKind TraceKind = JS::TraceKind::Scope; |
| 367 |
|
| 368 |
template <typename T> |
| 369 |
bool is() const { |
| 370 |
return kind_ == T::classScopeKind_; |
| 371 |
} |
| 372 |
|
| 373 |
template <typename T> |
| 374 |
T& as() { |
| 375 |
MOZ_ASSERT(this->is<T>()); |
| 376 |
return *static_cast<T*>(this); |
| 377 |
} |
| 378 |
|
| 379 |
template <typename T> |
| 380 |
const T& as() const { |
| 381 |
MOZ_ASSERT(this->is<T>()); |
| 382 |
return *static_cast<const T*>(this); |
| 383 |
} |
| 384 |
|
| 385 |
ScopeKind kind() const { return kind_; } |
| 386 |
|
| 387 |
bool isNamedLambda() const { |
| 388 |
return kind() == ScopeKind::NamedLambda || |
| 389 |
kind() == ScopeKind::StrictNamedLambda; |
| 390 |
} |
| 391 |
|
| 392 |
SharedShape* environmentShape() const { return environmentShape_; } |
| 393 |
|
| 394 |
Scope* enclosing() const { return enclosingScope_; } |
| 395 |
|
| 396 |
static bool hasEnvironment(ScopeKind kind, bool hasEnvironmentShape = false) { |
| 397 |
switch (kind) { |
| 398 |
case ScopeKind::With: |
| 399 |
case ScopeKind::Global: |
| 400 |
case ScopeKind::NonSyntactic: |
| 401 |
return true; |
| 402 |
default: |
| 403 |
// If there's a shape, an environment must be created for this scope. |
| 404 |
return hasEnvironmentShape; |
| 405 |
} |
| 406 |
} |
| 407 |
|
| 408 |
bool hasEnvironment() const { |
| 409 |
return hasEnvironment(kind_, !!environmentShape()); |
| 410 |
} |
| 411 |
|
| 412 |
uint32_t firstFrameSlot() const; |
| 413 |
|
| 414 |
uint32_t chainLength() const; |
| 415 |
uint32_t environmentChainLength() const; |
| 416 |
|
| 417 |
template <typename T> |
| 418 |
bool hasOnChain() const { |
| 419 |
for (const Scope* it = this; it; it = it->enclosing()) { |
| 420 |
if (it->is<T>()) { |
| 421 |
return true; |
| 422 |
} |
| 423 |
} |
| 424 |
return false; |
| 425 |
} |
| 426 |
|
| 427 |
bool hasOnChain(ScopeKind kind) const { |
| 428 |
for (const Scope* it = this; it; it = it->enclosing()) { |
| 429 |
if (it->kind() == kind) { |
| 430 |
return true; |
| 431 |
} |
| 432 |
} |
| 433 |
return false; |
| 434 |
} |
| 435 |
|
| 436 |
void traceChildren(JSTracer* trc); |
| 437 |
void finalize(JS::GCContext* gcx); |
| 438 |
|
| 439 |
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
| 440 |
|
| 441 |
void dump(); |
| 442 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 443 |
static bool dumpForDisassemble(JSContext* cx, JS::Handle<Scope*> scope, |
| 444 |
GenericPrinter& out, const char* indent); |
| 445 |
#endif /* defined(DEBUG) || defined(JS_JITSPEW) */ |
| 446 |
}; |
| 447 |
|
| 448 |
template <class DataT> |
| 449 |
inline size_t SizeOfScopeData(uint32_t length) { |
| 450 |
using BindingT = AbstractBindingName<typename DataT::NameType>; |
| 451 |
return GetOffsetOfScopeDataTrailingNames<DataT>() + length * sizeof(BindingT); |
| 452 |
} |
| 453 |
|
| 454 |
// |
| 455 |
// A useful typedef for selecting between a gc-aware wrappers |
| 456 |
// around pointers to BaseScopeData-derived types, and around raw |
| 457 |
// pointer wrappers around BaseParserScopeData-derived types. |
| 458 |
// |
| 459 |
template <typename ScopeT, typename AtomT> |
| 460 |
using AbstractScopeData = typename ScopeT::template AbstractData<AtomT>; |
| 461 |
|
| 462 |
// Binding names are stored from `this+1`. |
| 463 |
// Make sure the class aligns the binding name size. |
| 464 |
template <typename SlotInfo> |
| 465 |
struct alignas(alignof(AbstractBindingName<frontend::TaggedParserAtomIndex>)) |
| 466 |
ParserScopeData |
| 467 |
: public AbstractBaseScopeData<frontend::TaggedParserAtomIndex> { |
| 468 |
SlotInfo slotInfo; |
| 469 |
|
| 470 |
explicit ParserScopeData(size_t length) { PoisonNames(this, length); } |
| 471 |
ParserScopeData() = delete; |
| 472 |
}; |
| 473 |
|
| 474 |
// RuntimeScopeData has 2 requirements: |
| 475 |
// * It aligns with `BindingName`, that is stored after `this+1` |
| 476 |
// * It aligns with ScopeDataAlignBytes, in order to put it in the first |
| 477 |
// word of `js::Scope` |
| 478 |
static_assert(alignof(BindingName) <= ScopeDataAlignBytes); |
| 479 |
template <typename SlotInfo> |
| 480 |
struct alignas(ScopeDataAlignBytes) RuntimeScopeData |
| 481 |
: public AbstractBaseScopeData<JSAtom> { |
| 482 |
SlotInfo slotInfo; |
| 483 |
|
| 484 |
explicit RuntimeScopeData(size_t length) { PoisonNames(this, length); } |
| 485 |
RuntimeScopeData() = delete; |
| 486 |
|
| 487 |
void trace(JSTracer* trc); |
| 488 |
}; |
| 489 |
|
| 490 |
// |
| 491 |
// A lexical scope that holds let and const bindings. There are 4 kinds of |
| 492 |
// LexicalScopes. |
| 493 |
// |
| 494 |
// Lexical |
| 495 |
// A plain lexical scope. |
| 496 |
// |
| 497 |
// SimpleCatch |
| 498 |
// Holds the single catch parameter of a catch block. |
| 499 |
// |
| 500 |
// Catch |
| 501 |
// Holds the catch parameters (and only the catch parameters) of a catch |
| 502 |
// block. |
| 503 |
// |
| 504 |
// NamedLambda |
| 505 |
// StrictNamedLambda |
| 506 |
// Holds the single name of the callee for a named lambda expression. |
| 507 |
// |
| 508 |
// All kinds of LexicalScopes correspond to LexicalEnvironmentObjects on the |
| 509 |
// environment chain. |
| 510 |
// |
| 511 |
class LexicalScope : public Scope { |
| 512 |
friend class Scope; |
| 513 |
friend class AbstractBindingIter<JSAtom>; |
| 514 |
friend class GCMarker; |
| 515 |
friend class frontend::ScopeStencil; |
| 516 |
|
| 517 |
public: |
| 518 |
struct SlotInfo { |
| 519 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 520 |
// scope. |
| 521 |
uint32_t nextFrameSlot = 0; |
| 522 |
|
| 523 |
// Bindings are sorted by kind in both frames and environments. |
| 524 |
// |
| 525 |
// lets - [0, constStart) |
| 526 |
// consts - [constStart, length) |
| 527 |
uint32_t constStart = 0; |
| 528 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 529 |
// consts - [constStart, usingStart) |
| 530 |
// usings - [usingStart, length) |
| 531 |
uint32_t usingStart = 0; |
| 532 |
#endif |
| 533 |
}; |
| 534 |
|
| 535 |
using RuntimeData = RuntimeScopeData<SlotInfo>; |
| 536 |
using ParserData = ParserScopeData<SlotInfo>; |
| 537 |
|
| 538 |
template <typename NameT> |
| 539 |
using AbstractData = |
| 540 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 541 |
RuntimeData, ParserData>; |
| 542 |
|
| 543 |
private: |
| 544 |
static void prepareForScopeCreation(ScopeKind kind, uint32_t firstFrameSlot, |
| 545 |
LexicalScope::ParserData* data, |
| 546 |
mozilla::Maybe<uint32_t>* envShape); |
| 547 |
|
| 548 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 549 |
const RuntimeData& data() const { |
| 550 |
return *static_cast<const RuntimeData*>(rawData()); |
| 551 |
} |
| 552 |
|
| 553 |
public: |
| 554 |
static uint32_t nextFrameSlot(Scope* scope); |
| 555 |
|
| 556 |
uint32_t nextFrameSlot() const { return data().slotInfo.nextFrameSlot; } |
| 557 |
|
| 558 |
// Returns an empty shape for extensible global and non-syntactic lexical |
| 559 |
// scopes. |
| 560 |
static SharedShape* getEmptyExtensibleEnvironmentShape(JSContext* cx); |
| 561 |
}; |
| 562 |
|
| 563 |
template <> |
| 564 |
inline bool Scope::is<LexicalScope>() const { |
| 565 |
return kind_ == ScopeKind::Lexical || kind_ == ScopeKind::SimpleCatch || |
| 566 |
kind_ == ScopeKind::Catch || kind_ == ScopeKind::NamedLambda || |
| 567 |
kind_ == ScopeKind::StrictNamedLambda || |
| 568 |
kind_ == ScopeKind::FunctionLexical; |
| 569 |
} |
| 570 |
|
| 571 |
// The body scope of a JS class, containing only synthetic bindings for private |
| 572 |
// class members. (The binding for the class name, `C` in the example below, is |
| 573 |
// in another scope, a `LexicalScope`, that encloses the `ClassBodyScope`.) |
| 574 |
// Example: |
| 575 |
// |
| 576 |
// class C { |
| 577 |
// #f = 0; |
| 578 |
// #m() { |
| 579 |
// return this.#f++; |
| 580 |
// } |
| 581 |
// } |
| 582 |
// |
| 583 |
// This class has a ClassBodyScope with four synthetic bindings: |
| 584 |
// - `#f` (private name) |
| 585 |
// - `#m` (private name) |
| 586 |
// - `#m.method` (function object) |
| 587 |
// - `.privateBrand` (the class's private brand) |
| 588 |
class ClassBodyScope : public Scope { |
| 589 |
friend class Scope; |
| 590 |
friend class AbstractBindingIter<JSAtom>; |
| 591 |
friend class GCMarker; |
| 592 |
friend class frontend::ScopeStencil; |
| 593 |
friend class AbstractScopePtr; |
| 594 |
|
| 595 |
static const ScopeKind classScopeKind_ = ScopeKind::ClassBody; |
| 596 |
|
| 597 |
public: |
| 598 |
struct SlotInfo { |
| 599 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 600 |
// scope. |
| 601 |
uint32_t nextFrameSlot = 0; |
| 602 |
|
| 603 |
// Bindings are sorted by kind in both frames and environments. |
| 604 |
// |
| 605 |
// synthetic - [0, privateMethodStart) |
| 606 |
// privateMethod - [privateMethodStart, length) |
| 607 |
uint32_t privateMethodStart = 0; |
| 608 |
}; |
| 609 |
|
| 610 |
using RuntimeData = RuntimeScopeData<SlotInfo>; |
| 611 |
using ParserData = ParserScopeData<SlotInfo>; |
| 612 |
|
| 613 |
template <typename NameT> |
| 614 |
using AbstractData = |
| 615 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 616 |
RuntimeData, ParserData>; |
| 617 |
|
| 618 |
private: |
| 619 |
static void prepareForScopeCreation(ScopeKind kind, uint32_t firstFrameSlot, |
| 620 |
ClassBodyScope::ParserData* data, |
| 621 |
mozilla::Maybe<uint32_t>* envShape); |
| 622 |
|
| 623 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 624 |
const RuntimeData& data() const { |
| 625 |
return *static_cast<const RuntimeData*>(rawData()); |
| 626 |
} |
| 627 |
|
| 628 |
public: |
| 629 |
static uint32_t nextFrameSlot(Scope* scope); |
| 630 |
|
| 631 |
uint32_t nextFrameSlot() const { return data().slotInfo.nextFrameSlot; } |
| 632 |
|
| 633 |
// Returns an empty shape for extensible global and non-syntactic lexical |
| 634 |
// scopes. |
| 635 |
static SharedShape* getEmptyExtensibleEnvironmentShape(JSContext* cx); |
| 636 |
}; |
| 637 |
|
| 638 |
// |
| 639 |
// Scope corresponding to a function. Holds formal parameter names, special |
| 640 |
// internal names (see FunctionScope::isSpecialName), and, if the function |
| 641 |
// parameters contain no expressions that might possibly be evaluated, the |
| 642 |
// function's var bindings. For example, in these functions, the FunctionScope |
| 643 |
// will store a/b/c bindings but not d/e/f bindings: |
| 644 |
// |
| 645 |
// function f1(a, b) { |
| 646 |
// var c; |
| 647 |
// let e; |
| 648 |
// const f = 3; |
| 649 |
// } |
| 650 |
// function f2([a], b = 4, ...c) { |
| 651 |
// var d, e, f; // stored in VarScope |
| 652 |
// } |
| 653 |
// |
| 654 |
// Corresponds to CallObject on environment chain. |
| 655 |
// |
| 656 |
class FunctionScope : public Scope { |
| 657 |
friend class GCMarker; |
| 658 |
friend class AbstractBindingIter<JSAtom>; |
| 659 |
friend class AbstractPositionalFormalParameterIter<JSAtom>; |
| 660 |
friend class Scope; |
| 661 |
friend class AbstractScopePtr; |
| 662 |
static const ScopeKind classScopeKind_ = ScopeKind::Function; |
| 663 |
|
| 664 |
public: |
| 665 |
struct SlotInfo { |
| 666 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 667 |
// scope. |
| 668 |
uint32_t nextFrameSlot = 0; |
| 669 |
|
| 670 |
// Flag bits. |
| 671 |
// This uses uint32_t in order to make this struct packed. |
| 672 |
uint32_t flags = 0; |
| 673 |
|
| 674 |
// If parameter expressions are present, parameters act like lexical |
| 675 |
// bindings. |
| 676 |
static constexpr uint32_t HasParameterExprsFlag = 1; |
| 677 |
|
| 678 |
// Bindings are sorted by kind in both frames and environments. |
| 679 |
// |
| 680 |
// Positional formal parameter names are those that are not |
| 681 |
// destructured. They may be referred to by argument slots if |
| 682 |
// !script()->hasParameterExprs(). |
| 683 |
// |
| 684 |
// An argument slot that needs to be skipped due to being destructured |
| 685 |
// or having defaults will have a nullptr name in the name array to |
| 686 |
// advance the argument slot. |
| 687 |
// |
| 688 |
// Rest parameter binding is also included in positional formals. |
| 689 |
// This also becomes nullptr if destructuring. |
| 690 |
// |
| 691 |
// The number of positional formals is equal to function.length if |
| 692 |
// there's no rest, function.length+1 otherwise. |
| 693 |
// |
| 694 |
// Destructuring parameters and destructuring rest are included in |
| 695 |
// "other formals" below. |
| 696 |
// |
| 697 |
// "vars" contains the following: |
| 698 |
// * function's top level vars if !script()->hasParameterExprs() |
| 699 |
// * special internal names (arguments, .this, .generator) if |
| 700 |
// they're used. |
| 701 |
// |
| 702 |
// positional formals - [0, nonPositionalFormalStart) |
| 703 |
// other formals - [nonPositionalParamStart, varStart) |
| 704 |
// vars - [varStart, length) |
| 705 |
uint16_t nonPositionalFormalStart = 0; |
| 706 |
uint16_t varStart = 0; |
| 707 |
|
| 708 |
bool hasParameterExprs() const { return flags & HasParameterExprsFlag; } |
| 709 |
void setHasParameterExprs() { flags |= HasParameterExprsFlag; } |
| 710 |
}; |
| 711 |
|
| 712 |
struct alignas(ScopeDataAlignBytes) RuntimeData |
| 713 |
: public AbstractBaseScopeData<JSAtom> { |
| 714 |
SlotInfo slotInfo; |
| 715 |
// The canonical function of the scope, as during a scope walk we |
| 716 |
// often query properties of the JSFunction (e.g., is the function an |
| 717 |
// arrow). |
| 718 |
GCPtr<JSFunction*> canonicalFunction = {}; |
| 719 |
|
| 720 |
explicit RuntimeData(size_t length) { PoisonNames(this, length); } |
| 721 |
RuntimeData() = delete; |
| 722 |
|
| 723 |
void trace(JSTracer* trc); |
| 724 |
}; |
| 725 |
|
| 726 |
using ParserData = ParserScopeData<SlotInfo>; |
| 727 |
|
| 728 |
template <typename NameT> |
| 729 |
using AbstractData = |
| 730 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 731 |
RuntimeData, ParserData>; |
| 732 |
|
| 733 |
static void prepareForScopeCreation(FunctionScope::ParserData* data, |
| 734 |
bool hasParameterExprs, |
| 735 |
bool needsEnvironment, |
| 736 |
mozilla::Maybe<uint32_t>* envShape); |
| 737 |
|
| 738 |
private: |
| 739 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 740 |
|
| 741 |
const RuntimeData& data() const { |
| 742 |
return *static_cast<const RuntimeData*>(rawData()); |
| 743 |
} |
| 744 |
|
| 745 |
public: |
| 746 |
uint32_t nextFrameSlot() const { return data().slotInfo.nextFrameSlot; } |
| 747 |
|
| 748 |
JSFunction* canonicalFunction() const { return data().canonicalFunction; } |
| 749 |
void initCanonicalFunction(JSFunction* fun) { |
| 750 |
data().canonicalFunction.init(fun); |
| 751 |
} |
| 752 |
|
| 753 |
JSScript* script() const; |
| 754 |
|
| 755 |
bool hasParameterExprs() const { return data().slotInfo.hasParameterExprs(); } |
| 756 |
|
| 757 |
uint32_t numPositionalFormalParameters() const { |
| 758 |
return data().slotInfo.nonPositionalFormalStart; |
| 759 |
} |
| 760 |
|
| 761 |
static bool isSpecialName(frontend::TaggedParserAtomIndex name); |
| 762 |
}; |
| 763 |
|
| 764 |
// |
| 765 |
// Scope holding only vars. There is a single kind of VarScopes. |
| 766 |
// |
| 767 |
// FunctionBodyVar |
| 768 |
// Corresponds to the extra var scope present in functions with parameter |
| 769 |
// expressions. See examples in comment above FunctionScope. |
| 770 |
// |
| 771 |
// Corresponds to VarEnvironmentObject on environment chain. |
| 772 |
// |
| 773 |
class VarScope : public Scope { |
| 774 |
friend class GCMarker; |
| 775 |
friend class AbstractBindingIter<JSAtom>; |
| 776 |
friend class Scope; |
| 777 |
friend class frontend::ScopeStencil; |
| 778 |
|
| 779 |
public: |
| 780 |
struct SlotInfo { |
| 781 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 782 |
// scope. |
| 783 |
uint32_t nextFrameSlot = 0; |
| 784 |
|
| 785 |
// All bindings are vars. |
| 786 |
// |
| 787 |
// vars - [0, length) |
| 788 |
}; |
| 789 |
|
| 790 |
using RuntimeData = RuntimeScopeData<SlotInfo>; |
| 791 |
using ParserData = ParserScopeData<SlotInfo>; |
| 792 |
|
| 793 |
template <typename NameT> |
| 794 |
using AbstractData = |
| 795 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 796 |
RuntimeData, ParserData>; |
| 797 |
|
| 798 |
private: |
| 799 |
static void prepareForScopeCreation(ScopeKind kind, |
| 800 |
VarScope::ParserData* data, |
| 801 |
uint32_t firstFrameSlot, |
| 802 |
bool needsEnvironment, |
| 803 |
mozilla::Maybe<uint32_t>* envShape); |
| 804 |
|
| 805 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 806 |
|
| 807 |
const RuntimeData& data() const { |
| 808 |
return *static_cast<const RuntimeData*>(rawData()); |
| 809 |
} |
| 810 |
|
| 811 |
public: |
| 812 |
uint32_t nextFrameSlot() const { return data().slotInfo.nextFrameSlot; } |
| 813 |
}; |
| 814 |
|
| 815 |
template <> |
| 816 |
inline bool Scope::is<VarScope>() const { |
| 817 |
return kind_ == ScopeKind::FunctionBodyVar; |
| 818 |
} |
| 819 |
|
| 820 |
// |
| 821 |
// Scope corresponding to both the global object scope and the global lexical |
| 822 |
// scope. |
| 823 |
// |
| 824 |
// Both are extensible and are singletons across <script> tags, so these |
| 825 |
// scopes are a fragment of the names in global scope. In other words, two |
| 826 |
// global scripts may have two different GlobalScopes despite having the same |
| 827 |
// GlobalObject. |
| 828 |
// |
| 829 |
// There are 2 kinds of GlobalScopes. |
| 830 |
// |
| 831 |
// Global |
| 832 |
// Corresponds to a GlobalObject and its GlobalLexicalEnvironmentObject on |
| 833 |
// the environment chain. |
| 834 |
// |
| 835 |
// NonSyntactic |
| 836 |
// Corresponds to a non-GlobalObject created by the embedding on the |
| 837 |
// environment chain. This distinction is important for optimizations. |
| 838 |
// |
| 839 |
class GlobalScope : public Scope { |
| 840 |
friend class Scope; |
| 841 |
friend class AbstractBindingIter<JSAtom>; |
| 842 |
friend class GCMarker; |
| 843 |
|
| 844 |
public: |
| 845 |
struct SlotInfo { |
| 846 |
// Bindings are sorted by kind. |
| 847 |
// `vars` includes top-level functions which is distinguished by a bit |
| 848 |
// on the BindingName. |
| 849 |
// |
| 850 |
// vars - [0, letStart) |
| 851 |
// lets - [letStart, constStart) |
| 852 |
// consts - [constStart, length) |
| 853 |
uint32_t letStart = 0; |
| 854 |
uint32_t constStart = 0; |
| 855 |
}; |
| 856 |
|
| 857 |
using RuntimeData = RuntimeScopeData<SlotInfo>; |
| 858 |
using ParserData = ParserScopeData<SlotInfo>; |
| 859 |
|
| 860 |
template <typename NameT> |
| 861 |
using AbstractData = |
| 862 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 863 |
RuntimeData, ParserData>; |
| 864 |
|
| 865 |
static GlobalScope* createEmpty(JSContext* cx, ScopeKind kind); |
| 866 |
|
| 867 |
private: |
| 868 |
static GlobalScope* createWithData( |
| 869 |
JSContext* cx, ScopeKind kind, |
| 870 |
MutableHandle<UniquePtr<RuntimeData>> data); |
| 871 |
|
| 872 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 873 |
|
| 874 |
const RuntimeData& data() const { |
| 875 |
return *static_cast<const RuntimeData*>(rawData()); |
| 876 |
} |
| 877 |
|
| 878 |
public: |
| 879 |
bool isSyntactic() const { return kind() != ScopeKind::NonSyntactic; } |
| 880 |
|
| 881 |
bool hasBindings() const { return data().length > 0; } |
| 882 |
}; |
| 883 |
|
| 884 |
template <> |
| 885 |
inline bool Scope::is<GlobalScope>() const { |
| 886 |
return kind_ == ScopeKind::Global || kind_ == ScopeKind::NonSyntactic; |
| 887 |
} |
| 888 |
|
| 889 |
// |
| 890 |
// Scope of a 'with' statement. Has no bindings. |
| 891 |
// |
| 892 |
// Corresponds to a WithEnvironmentObject on the environment chain. |
| 893 |
class WithScope : public Scope { |
| 894 |
friend class Scope; |
| 895 |
friend class AbstractScopePtr; |
| 896 |
static const ScopeKind classScopeKind_ = ScopeKind::With; |
| 897 |
|
| 898 |
public: |
| 899 |
static WithScope* create(JSContext* cx, Handle<Scope*> enclosing); |
| 900 |
}; |
| 901 |
|
| 902 |
// |
| 903 |
// Scope of an eval. Holds var bindings. There are 2 kinds of EvalScopes. |
| 904 |
// |
| 905 |
// StrictEval |
| 906 |
// A strict eval. Corresponds to a VarEnvironmentObject, where its var |
| 907 |
// bindings lives. |
| 908 |
// |
| 909 |
// Eval |
| 910 |
// A sloppy eval. This is an empty scope, used only in the frontend, to |
| 911 |
// detect redeclaration errors. It has no Environment. Any `var`s declared |
| 912 |
// in the eval code are bound on the nearest enclosing var environment. |
| 913 |
// |
| 914 |
class EvalScope : public Scope { |
| 915 |
friend class Scope; |
| 916 |
friend class AbstractBindingIter<JSAtom>; |
| 917 |
friend class GCMarker; |
| 918 |
friend class frontend::ScopeStencil; |
| 919 |
|
| 920 |
public: |
| 921 |
struct SlotInfo { |
| 922 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 923 |
// scope. |
| 924 |
uint32_t nextFrameSlot = 0; |
| 925 |
|
| 926 |
// All bindings in an eval script are 'var' bindings. The implicit |
| 927 |
// lexical scope around the eval is present regardless of strictness |
| 928 |
// and is its own LexicalScope. |
| 929 |
// `vars` includes top-level functions which is distinguished by a bit |
| 930 |
// on the BindingName. |
| 931 |
// |
| 932 |
// vars - [0, length) |
| 933 |
}; |
| 934 |
|
| 935 |
using RuntimeData = RuntimeScopeData<SlotInfo>; |
| 936 |
using ParserData = ParserScopeData<SlotInfo>; |
| 937 |
|
| 938 |
template <typename NameT> |
| 939 |
using AbstractData = |
| 940 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 941 |
RuntimeData, ParserData>; |
| 942 |
|
| 943 |
private: |
| 944 |
static void prepareForScopeCreation(ScopeKind scopeKind, |
| 945 |
EvalScope::ParserData* data, |
| 946 |
mozilla::Maybe<uint32_t>* envShape); |
| 947 |
|
| 948 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 949 |
|
| 950 |
const RuntimeData& data() const { |
| 951 |
return *static_cast<const RuntimeData*>(rawData()); |
| 952 |
} |
| 953 |
|
| 954 |
public: |
| 955 |
// Starting a scope, the nearest var scope that a direct eval can |
| 956 |
// introduce vars on. |
| 957 |
static Scope* nearestVarScopeForDirectEval(Scope* scope); |
| 958 |
|
| 959 |
uint32_t nextFrameSlot() const { return data().slotInfo.nextFrameSlot; } |
| 960 |
|
| 961 |
bool strict() const { return kind() == ScopeKind::StrictEval; } |
| 962 |
|
| 963 |
bool hasBindings() const { return data().length > 0; } |
| 964 |
|
| 965 |
bool isNonGlobal() const { |
| 966 |
if (strict()) { |
| 967 |
return true; |
| 968 |
} |
| 969 |
return !nearestVarScopeForDirectEval(enclosing())->is<GlobalScope>(); |
| 970 |
} |
| 971 |
}; |
| 972 |
|
| 973 |
template <> |
| 974 |
inline bool Scope::is<EvalScope>() const { |
| 975 |
return kind_ == ScopeKind::Eval || kind_ == ScopeKind::StrictEval; |
| 976 |
} |
| 977 |
|
| 978 |
// |
| 979 |
// Scope corresponding to the toplevel script in an ES module. |
| 980 |
// |
| 981 |
// Like GlobalScopes, these scopes contain both vars and lexical bindings, as |
| 982 |
// the treating of imports and exports requires putting them in one scope. |
| 983 |
// |
| 984 |
// Corresponds to a ModuleEnvironmentObject on the environment chain. |
| 985 |
// |
| 986 |
class ModuleScope : public Scope { |
| 987 |
friend class GCMarker; |
| 988 |
friend class AbstractBindingIter<JSAtom>; |
| 989 |
friend class Scope; |
| 990 |
friend class AbstractScopePtr; |
| 991 |
friend class frontend::ScopeStencil; |
| 992 |
static const ScopeKind classScopeKind_ = ScopeKind::Module; |
| 993 |
|
| 994 |
public: |
| 995 |
struct SlotInfo { |
| 996 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 997 |
// scope. |
| 998 |
uint32_t nextFrameSlot = 0; |
| 999 |
|
| 1000 |
// Bindings are sorted by kind. |
| 1001 |
// |
| 1002 |
// imports - [0, varStart) |
| 1003 |
// vars - [varStart, letStart) |
| 1004 |
// lets - [letStart, constStart) |
| 1005 |
// consts - [constStart, length) |
| 1006 |
uint32_t varStart = 0; |
| 1007 |
uint32_t letStart = 0; |
| 1008 |
uint32_t constStart = 0; |
| 1009 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1010 |
// consts - [constStart, usingStart) |
| 1011 |
// usings - [usingStart, length) |
| 1012 |
uint32_t usingStart = 0; |
| 1013 |
#endif |
| 1014 |
}; |
| 1015 |
|
| 1016 |
struct alignas(ScopeDataAlignBytes) RuntimeData |
| 1017 |
: public AbstractBaseScopeData<JSAtom> { |
| 1018 |
SlotInfo slotInfo; |
| 1019 |
// The module of the scope. |
| 1020 |
GCPtr<ModuleObject*> module = {}; |
| 1021 |
|
| 1022 |
explicit RuntimeData(size_t length); |
| 1023 |
RuntimeData() = delete; |
| 1024 |
|
| 1025 |
void trace(JSTracer* trc); |
| 1026 |
}; |
| 1027 |
|
| 1028 |
using ParserData = ParserScopeData<SlotInfo>; |
| 1029 |
|
| 1030 |
template <typename NameT> |
| 1031 |
using AbstractData = |
| 1032 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 1033 |
RuntimeData, ParserData>; |
| 1034 |
|
| 1035 |
private: |
| 1036 |
static void prepareForScopeCreation(ModuleScope::ParserData* data, |
| 1037 |
mozilla::Maybe<uint32_t>* envShape); |
| 1038 |
|
| 1039 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 1040 |
|
| 1041 |
const RuntimeData& data() const { |
| 1042 |
return *static_cast<const RuntimeData*>(rawData()); |
| 1043 |
} |
| 1044 |
|
| 1045 |
public: |
| 1046 |
uint32_t nextFrameSlot() const { return data().slotInfo.nextFrameSlot; } |
| 1047 |
|
| 1048 |
ModuleObject* module() const { return data().module; } |
| 1049 |
void initModule(ModuleObject* mod) { return data().module.init(mod); } |
| 1050 |
|
| 1051 |
// Off-thread compilation needs to calculate environmentChainLength for |
| 1052 |
// an emptyGlobalScope where the global may not be available. |
| 1053 |
static const size_t EnclosingEnvironmentChainLength = 1; |
| 1054 |
}; |
| 1055 |
|
| 1056 |
class WasmInstanceScope : public Scope { |
| 1057 |
friend class AbstractBindingIter<JSAtom>; |
| 1058 |
friend class Scope; |
| 1059 |
friend class GCMarker; |
| 1060 |
friend class AbstractScopePtr; |
| 1061 |
static const ScopeKind classScopeKind_ = ScopeKind::WasmInstance; |
| 1062 |
|
| 1063 |
public: |
| 1064 |
struct SlotInfo { |
| 1065 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 1066 |
// scope. |
| 1067 |
uint32_t nextFrameSlot = 0; |
| 1068 |
|
| 1069 |
// Bindings list the WASM memories and globals. |
| 1070 |
// |
| 1071 |
// memories - [0, globalsStart) |
| 1072 |
// globals - [globalsStart, length) |
| 1073 |
uint32_t memoriesStart = 0; |
| 1074 |
uint32_t globalsStart = 0; |
| 1075 |
}; |
| 1076 |
|
| 1077 |
struct alignas(ScopeDataAlignBytes) RuntimeData |
| 1078 |
: public AbstractBaseScopeData<JSAtom> { |
| 1079 |
SlotInfo slotInfo; |
| 1080 |
// The wasm instance of the scope. |
| 1081 |
GCPtr<WasmInstanceObject*> instance = {}; |
| 1082 |
|
| 1083 |
explicit RuntimeData(size_t length); |
| 1084 |
RuntimeData() = delete; |
| 1085 |
|
| 1086 |
void trace(JSTracer* trc); |
| 1087 |
}; |
| 1088 |
|
| 1089 |
using ParserData = ParserScopeData<SlotInfo>; |
| 1090 |
|
| 1091 |
template <typename NameT> |
| 1092 |
using AbstractData = |
| 1093 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 1094 |
RuntimeData, ParserData>; |
| 1095 |
|
| 1096 |
static WasmInstanceScope* create(JSContext* cx, WasmInstanceObject* instance); |
| 1097 |
|
| 1098 |
private: |
| 1099 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 1100 |
|
| 1101 |
const RuntimeData& data() const { |
| 1102 |
return *static_cast<const RuntimeData*>(rawData()); |
| 1103 |
} |
| 1104 |
|
| 1105 |
public: |
| 1106 |
WasmInstanceObject* instance() const { return data().instance; } |
| 1107 |
|
| 1108 |
uint32_t memoriesStart() const { return data().slotInfo.memoriesStart; } |
| 1109 |
|
| 1110 |
uint32_t globalsStart() const { return data().slotInfo.globalsStart; } |
| 1111 |
|
| 1112 |
uint32_t namesCount() const { return data().length; } |
| 1113 |
}; |
| 1114 |
|
| 1115 |
// Scope corresponding to the wasm function. A WasmFunctionScope is used by |
| 1116 |
// Debugger only, and not for wasm execution. |
| 1117 |
// |
| 1118 |
class WasmFunctionScope : public Scope { |
| 1119 |
friend class AbstractBindingIter<JSAtom>; |
| 1120 |
friend class Scope; |
| 1121 |
friend class GCMarker; |
| 1122 |
friend class AbstractScopePtr; |
| 1123 |
static const ScopeKind classScopeKind_ = ScopeKind::WasmFunction; |
| 1124 |
|
| 1125 |
public: |
| 1126 |
struct SlotInfo { |
| 1127 |
// Frame slots [0, nextFrameSlot) are live when this is the innermost |
| 1128 |
// scope. |
| 1129 |
uint32_t nextFrameSlot = 0; |
| 1130 |
|
| 1131 |
// Bindings are the local variable names. |
| 1132 |
// |
| 1133 |
// vars - [0, length) |
| 1134 |
}; |
| 1135 |
|
| 1136 |
using RuntimeData = RuntimeScopeData<SlotInfo>; |
| 1137 |
using ParserData = ParserScopeData<SlotInfo>; |
| 1138 |
|
| 1139 |
template <typename NameT> |
| 1140 |
using AbstractData = |
| 1141 |
typename std::conditional_t<std::is_same<NameT, JSAtom>::value, |
| 1142 |
RuntimeData, ParserData>; |
| 1143 |
|
| 1144 |
static WasmFunctionScope* create(JSContext* cx, Handle<Scope*> enclosing, |
| 1145 |
uint32_t funcIndex); |
| 1146 |
|
| 1147 |
private: |
| 1148 |
RuntimeData& data() { return *static_cast<RuntimeData*>(rawData()); } |
| 1149 |
|
| 1150 |
const RuntimeData& data() const { |
| 1151 |
return *static_cast<const RuntimeData*>(rawData()); |
| 1152 |
} |
| 1153 |
}; |
| 1154 |
|
| 1155 |
template <typename F> |
| 1156 |
void Scope::applyScopeDataTyped(F&& f) { |
| 1157 |
switch (kind()) { |
| 1158 |
case ScopeKind::Function: { |
| 1159 |
f(&as<FunctionScope>().data()); |
| 1160 |
break; |
| 1161 |
case ScopeKind::FunctionBodyVar: |
| 1162 |
f(&as<VarScope>().data()); |
| 1163 |
break; |
| 1164 |
case ScopeKind::Lexical: |
| 1165 |
case ScopeKind::SimpleCatch: |
| 1166 |
case ScopeKind::Catch: |
| 1167 |
case ScopeKind::NamedLambda: |
| 1168 |
case ScopeKind::StrictNamedLambda: |
| 1169 |
case ScopeKind::FunctionLexical: |
| 1170 |
f(&as<LexicalScope>().data()); |
| 1171 |
break; |
| 1172 |
case ScopeKind::ClassBody: |
| 1173 |
f(&as<ClassBodyScope>().data()); |
| 1174 |
break; |
| 1175 |
case ScopeKind::With: |
| 1176 |
// With scopes do not have data. |
| 1177 |
break; |
| 1178 |
case ScopeKind::Eval: |
| 1179 |
case ScopeKind::StrictEval: |
| 1180 |
f(&as<EvalScope>().data()); |
| 1181 |
break; |
| 1182 |
case ScopeKind::Global: |
| 1183 |
case ScopeKind::NonSyntactic: |
| 1184 |
f(&as<GlobalScope>().data()); |
| 1185 |
break; |
| 1186 |
case ScopeKind::Module: |
| 1187 |
f(&as<ModuleScope>().data()); |
| 1188 |
break; |
| 1189 |
case ScopeKind::WasmInstance: |
| 1190 |
f(&as<WasmInstanceScope>().data()); |
| 1191 |
break; |
| 1192 |
case ScopeKind::WasmFunction: |
| 1193 |
f(&as<WasmFunctionScope>().data()); |
| 1194 |
break; |
| 1195 |
} |
| 1196 |
} |
| 1197 |
} |
| 1198 |
|
| 1199 |
// |
| 1200 |
// An iterator for a Scope's bindings. This is the source of truth for frame |
| 1201 |
// and environment object layout. |
| 1202 |
// |
| 1203 |
// It may be placed in GC containers; for example: |
| 1204 |
// |
| 1205 |
// for (Rooted<BindingIter> bi(cx, BindingIter(scope)); bi; bi++) { |
| 1206 |
// use(bi); |
| 1207 |
// SomeMayGCOperation(); |
| 1208 |
// use(bi); |
| 1209 |
// } |
| 1210 |
// |
| 1211 |
template <typename NameT> |
| 1212 |
class BaseAbstractBindingIter { |
| 1213 |
protected: |
| 1214 |
// Bindings are sorted by kind. Because different Scopes have differently |
| 1215 |
// laid out {Runtime,Parser}Data for packing, BindingIter must handle all |
| 1216 |
// binding kinds. |
| 1217 |
// |
| 1218 |
// Kind ranges: |
| 1219 |
// |
| 1220 |
// imports - [0, positionalFormalStart) |
| 1221 |
// positional formals - [positionalFormalStart, nonPositionalFormalStart) |
| 1222 |
// other formals - [nonPositionalParamStart, varStart) |
| 1223 |
// vars - [varStart, letStart) |
| 1224 |
// lets - [letStart, constStart) |
| 1225 |
// consts - [constStart, syntheticStart) |
| 1226 |
// synthetic - [syntheticStart, privateMethodStart) |
| 1227 |
// private methods = [privateMethodStart, length) |
| 1228 |
// |
| 1229 |
// If ENABLE_EXPLICIT_RESOURCE_MANAGEMENT is set, the consts range is split |
| 1230 |
// into the following: |
| 1231 |
// consts - [constStart, usingStart) |
| 1232 |
// usings - [usingStart, syntheticStart) |
| 1233 |
// |
| 1234 |
// Access method when not closed over: |
| 1235 |
// |
| 1236 |
// imports - name |
| 1237 |
// positional formals - argument slot |
| 1238 |
// other formals - frame slot |
| 1239 |
// vars - frame slot |
| 1240 |
// lets - frame slot |
| 1241 |
// consts - frame slot |
| 1242 |
// synthetic - frame slot |
| 1243 |
// private methods - frame slot |
| 1244 |
// |
| 1245 |
// Access method when closed over: |
| 1246 |
// |
| 1247 |
// imports - name |
| 1248 |
// positional formals - environment slot or name |
| 1249 |
// other formals - environment slot or name |
| 1250 |
// vars - environment slot or name |
| 1251 |
// lets - environment slot or name |
| 1252 |
// consts - environment slot or name |
| 1253 |
// synthetic - environment slot or name |
| 1254 |
// private methods - environment slot or name |
| 1255 |
MOZ_INIT_OUTSIDE_CTOR uint32_t positionalFormalStart_; |
| 1256 |
MOZ_INIT_OUTSIDE_CTOR uint32_t nonPositionalFormalStart_; |
| 1257 |
MOZ_INIT_OUTSIDE_CTOR uint32_t varStart_; |
| 1258 |
MOZ_INIT_OUTSIDE_CTOR uint32_t letStart_; |
| 1259 |
MOZ_INIT_OUTSIDE_CTOR uint32_t constStart_; |
| 1260 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1261 |
MOZ_INIT_OUTSIDE_CTOR uint32_t usingStart_; |
| 1262 |
#endif |
| 1263 |
MOZ_INIT_OUTSIDE_CTOR uint32_t syntheticStart_; |
| 1264 |
MOZ_INIT_OUTSIDE_CTOR uint32_t privateMethodStart_; |
| 1265 |
MOZ_INIT_OUTSIDE_CTOR uint32_t length_; |
| 1266 |
|
| 1267 |
MOZ_INIT_OUTSIDE_CTOR uint32_t index_; |
| 1268 |
|
| 1269 |
enum Flags : uint8_t { |
| 1270 |
CannotHaveSlots = 0, |
| 1271 |
CanHaveArgumentSlots = 1 << 0, |
| 1272 |
CanHaveFrameSlots = 1 << 1, |
| 1273 |
CanHaveEnvironmentSlots = 1 << 2, |
| 1274 |
|
| 1275 |
// See comment in settle below. |
| 1276 |
HasFormalParameterExprs = 1 << 3, |
| 1277 |
IgnoreDestructuredFormalParameters = 1 << 4, |
| 1278 |
|
| 1279 |
// Truly I hate named lambdas. |
| 1280 |
IsNamedLambda = 1 << 5 |
| 1281 |
}; |
| 1282 |
|
| 1283 |
static const uint8_t CanHaveSlotsMask = 0x7; |
| 1284 |
|
| 1285 |
MOZ_INIT_OUTSIDE_CTOR uint8_t flags_; |
| 1286 |
MOZ_INIT_OUTSIDE_CTOR uint16_t argumentSlot_; |
| 1287 |
MOZ_INIT_OUTSIDE_CTOR uint32_t frameSlot_; |
| 1288 |
MOZ_INIT_OUTSIDE_CTOR uint32_t environmentSlot_; |
| 1289 |
|
| 1290 |
MOZ_INIT_OUTSIDE_CTOR AbstractBindingName<NameT>* names_; |
| 1291 |
|
| 1292 |
void init(uint32_t positionalFormalStart, uint32_t nonPositionalFormalStart, |
| 1293 |
uint32_t varStart, uint32_t letStart, uint32_t constStart, |
| 1294 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1295 |
uint32_t usingStart, |
| 1296 |
#endif |
| 1297 |
uint32_t syntheticStart, uint32_t privateMethodStart, uint8_t flags, |
| 1298 |
uint32_t firstFrameSlot, uint32_t firstEnvironmentSlot, |
| 1299 |
mozilla::Span<AbstractBindingName<NameT>> names) { |
| 1300 |
positionalFormalStart_ = positionalFormalStart; |
| 1301 |
nonPositionalFormalStart_ = nonPositionalFormalStart; |
| 1302 |
varStart_ = varStart; |
| 1303 |
letStart_ = letStart; |
| 1304 |
constStart_ = constStart; |
| 1305 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1306 |
usingStart_ = usingStart; |
| 1307 |
#endif |
| 1308 |
syntheticStart_ = syntheticStart; |
| 1309 |
privateMethodStart_ = privateMethodStart; |
| 1310 |
length_ = names.size(); |
| 1311 |
|
| 1312 |
index_ = 0; |
| 1313 |
flags_ = flags; |
| 1314 |
argumentSlot_ = 0; |
| 1315 |
frameSlot_ = firstFrameSlot; |
| 1316 |
environmentSlot_ = firstEnvironmentSlot; |
| 1317 |
names_ = names.data(); |
| 1318 |
|
| 1319 |
settle(); |
| 1320 |
} |
| 1321 |
|
| 1322 |
void init(LexicalScope::AbstractData<NameT>& data, uint32_t firstFrameSlot, |
| 1323 |
uint8_t flags); |
| 1324 |
|
| 1325 |
void init(ClassBodyScope::AbstractData<NameT>& data, uint32_t firstFrameSlot); |
| 1326 |
void init(FunctionScope::AbstractData<NameT>& data, uint8_t flags); |
| 1327 |
|
| 1328 |
void init(VarScope::AbstractData<NameT>& data, uint32_t firstFrameSlot); |
| 1329 |
void init(GlobalScope::AbstractData<NameT>& data); |
| 1330 |
void init(EvalScope::AbstractData<NameT>& data, bool strict); |
| 1331 |
void init(ModuleScope::AbstractData<NameT>& data); |
| 1332 |
void init(WasmInstanceScope::AbstractData<NameT>& data); |
| 1333 |
void init(WasmFunctionScope::AbstractData<NameT>& data); |
| 1334 |
|
| 1335 |
bool hasFormalParameterExprs() const { |
| 1336 |
return flags_ & HasFormalParameterExprs; |
| 1337 |
} |
| 1338 |
|
| 1339 |
bool ignoreDestructuredFormalParameters() const { |
| 1340 |
return flags_ & IgnoreDestructuredFormalParameters; |
| 1341 |
} |
| 1342 |
|
| 1343 |
bool isNamedLambda() const { return flags_ & IsNamedLambda; } |
| 1344 |
|
| 1345 |
void increment() { |
| 1346 |
MOZ_ASSERT(!done()); |
| 1347 |
if (flags_ & CanHaveSlotsMask) { |
| 1348 |
if (canHaveArgumentSlots()) { |
| 1349 |
if (index_ < nonPositionalFormalStart_) { |
| 1350 |
MOZ_ASSERT(index_ >= positionalFormalStart_); |
| 1351 |
argumentSlot_++; |
| 1352 |
} |
| 1353 |
} |
| 1354 |
if (closedOver()) { |
| 1355 |
// Imports must not be given known slots. They are |
| 1356 |
// indirect bindings. |
| 1357 |
MOZ_ASSERT(kind() != BindingKind::Import); |
| 1358 |
MOZ_ASSERT(canHaveEnvironmentSlots()); |
| 1359 |
environmentSlot_++; |
| 1360 |
} else if (canHaveFrameSlots()) { |
| 1361 |
// Usually positional formal parameters don't have frame |
| 1362 |
// slots, except when there are parameter expressions, in |
| 1363 |
// which case they act like lets. |
| 1364 |
if (index_ >= nonPositionalFormalStart_ || |
| 1365 |
(hasFormalParameterExprs() && name())) { |
| 1366 |
frameSlot_++; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
} |
| 1370 |
index_++; |
| 1371 |
} |
| 1372 |
|
| 1373 |
void settle() { |
| 1374 |
if (ignoreDestructuredFormalParameters()) { |
| 1375 |
while (!done() && !name()) { |
| 1376 |
increment(); |
| 1377 |
} |
| 1378 |
} |
| 1379 |
} |
| 1380 |
|
| 1381 |
BaseAbstractBindingIter() = default; |
| 1382 |
|
| 1383 |
public: |
| 1384 |
BaseAbstractBindingIter(LexicalScope::AbstractData<NameT>& data, |
| 1385 |
uint32_t firstFrameSlot, bool isNamedLambda) { |
| 1386 |
init(data, firstFrameSlot, isNamedLambda ? IsNamedLambda : 0); |
| 1387 |
} |
| 1388 |
|
| 1389 |
BaseAbstractBindingIter(ClassBodyScope::AbstractData<NameT>& data, |
| 1390 |
uint32_t firstFrameSlot) { |
| 1391 |
init(data, firstFrameSlot); |
| 1392 |
} |
| 1393 |
|
| 1394 |
BaseAbstractBindingIter(FunctionScope::AbstractData<NameT>& data, |
| 1395 |
bool hasParameterExprs) { |
| 1396 |
init(data, IgnoreDestructuredFormalParameters | |
| 1397 |
(hasParameterExprs ? HasFormalParameterExprs : 0)); |
| 1398 |
} |
| 1399 |
|
| 1400 |
BaseAbstractBindingIter(VarScope::AbstractData<NameT>& data, |
| 1401 |
uint32_t firstFrameSlot) { |
| 1402 |
init(data, firstFrameSlot); |
| 1403 |
} |
| 1404 |
|
| 1405 |
explicit BaseAbstractBindingIter(GlobalScope::AbstractData<NameT>& data) { |
| 1406 |
init(data); |
| 1407 |
} |
| 1408 |
|
| 1409 |
explicit BaseAbstractBindingIter(ModuleScope::AbstractData<NameT>& data) { |
| 1410 |
init(data); |
| 1411 |
} |
| 1412 |
|
| 1413 |
explicit BaseAbstractBindingIter( |
| 1414 |
WasmFunctionScope::AbstractData<NameT>& data) { |
| 1415 |
init(data); |
| 1416 |
} |
| 1417 |
|
| 1418 |
BaseAbstractBindingIter(EvalScope::AbstractData<NameT>& data, bool strict) { |
| 1419 |
init(data, strict); |
| 1420 |
} |
| 1421 |
|
| 1422 |
MOZ_IMPLICIT BaseAbstractBindingIter( |
| 1423 |
const BaseAbstractBindingIter<NameT>& bi) = default; |
| 1424 |
|
| 1425 |
bool done() const { return index_ == length_; } |
| 1426 |
|
| 1427 |
explicit operator bool() const { return !done(); } |
| 1428 |
|
| 1429 |
void operator++(int) { |
| 1430 |
increment(); |
| 1431 |
settle(); |
| 1432 |
} |
| 1433 |
|
| 1434 |
bool isLast() const { |
| 1435 |
MOZ_ASSERT(!done()); |
| 1436 |
return index_ + 1 == length_; |
| 1437 |
} |
| 1438 |
|
| 1439 |
bool canHaveArgumentSlots() const { return flags_ & CanHaveArgumentSlots; } |
| 1440 |
|
| 1441 |
bool canHaveFrameSlots() const { return flags_ & CanHaveFrameSlots; } |
| 1442 |
|
| 1443 |
bool canHaveEnvironmentSlots() const { |
| 1444 |
return flags_ & CanHaveEnvironmentSlots; |
| 1445 |
} |
| 1446 |
|
| 1447 |
typename AbstractBindingName<NameT>::NamePointerT name() const { |
| 1448 |
MOZ_ASSERT(!done()); |
| 1449 |
return names_[index_].name(); |
| 1450 |
} |
| 1451 |
|
| 1452 |
bool closedOver() const { |
| 1453 |
MOZ_ASSERT(!done()); |
| 1454 |
return names_[index_].closedOver(); |
| 1455 |
} |
| 1456 |
|
| 1457 |
BindingLocation location() const { |
| 1458 |
MOZ_ASSERT(!done()); |
| 1459 |
if (!(flags_ & CanHaveSlotsMask)) { |
| 1460 |
return BindingLocation::Global(); |
| 1461 |
} |
| 1462 |
if (index_ < positionalFormalStart_) { |
| 1463 |
return BindingLocation::Import(); |
| 1464 |
} |
| 1465 |
if (closedOver()) { |
| 1466 |
MOZ_ASSERT(canHaveEnvironmentSlots()); |
| 1467 |
return BindingLocation::Environment(environmentSlot_); |
| 1468 |
} |
| 1469 |
if (index_ < nonPositionalFormalStart_ && canHaveArgumentSlots()) { |
| 1470 |
return BindingLocation::Argument(argumentSlot_); |
| 1471 |
} |
| 1472 |
if (canHaveFrameSlots()) { |
| 1473 |
return BindingLocation::Frame(frameSlot_); |
| 1474 |
} |
| 1475 |
MOZ_ASSERT(isNamedLambda()); |
| 1476 |
return BindingLocation::NamedLambdaCallee(); |
| 1477 |
} |
| 1478 |
|
| 1479 |
BindingKind kind() const { |
| 1480 |
MOZ_ASSERT(!done()); |
| 1481 |
if (index_ < positionalFormalStart_) { |
| 1482 |
return BindingKind::Import; |
| 1483 |
} |
| 1484 |
if (index_ < varStart_) { |
| 1485 |
// When the parameter list has expressions, the parameters act |
| 1486 |
// like lexical bindings and have TDZ. |
| 1487 |
if (hasFormalParameterExprs()) { |
| 1488 |
return BindingKind::Let; |
| 1489 |
} |
| 1490 |
return BindingKind::FormalParameter; |
| 1491 |
} |
| 1492 |
if (index_ < letStart_) { |
| 1493 |
return BindingKind::Var; |
| 1494 |
} |
| 1495 |
if (index_ < constStart_) { |
| 1496 |
return BindingKind::Let; |
| 1497 |
} |
| 1498 |
#ifdef ENABLE_EXPLICIT_RESOURCE_MANAGEMENT |
| 1499 |
if (index_ < usingStart_) { |
| 1500 |
return isNamedLambda() ? BindingKind::NamedLambdaCallee |
| 1501 |
: BindingKind::Const; |
| 1502 |
} |
| 1503 |
if (index_ < syntheticStart_) { |
| 1504 |
return BindingKind::Using; |
| 1505 |
} |
| 1506 |
#else |
| 1507 |
if (index_ < syntheticStart_) { |
| 1508 |
return isNamedLambda() ? BindingKind::NamedLambdaCallee |
| 1509 |
: BindingKind::Const; |
| 1510 |
} |
| 1511 |
#endif |
| 1512 |
if (index_ < privateMethodStart_) { |
| 1513 |
return BindingKind::Synthetic; |
| 1514 |
} |
| 1515 |
return BindingKind::PrivateMethod; |
| 1516 |
} |
| 1517 |
|
| 1518 |
js::frontend::NameLocation nameLocation() const { |
| 1519 |
using js::frontend::NameLocation; |
| 1520 |
|
| 1521 |
BindingKind bindKind = kind(); |
| 1522 |
BindingLocation bl = location(); |
| 1523 |
switch (bl.kind()) { |
| 1524 |
case BindingLocation::Kind::Global: |
| 1525 |
return NameLocation::Global(bindKind); |
| 1526 |
case BindingLocation::Kind::Argument: |
| 1527 |
return NameLocation::ArgumentSlot(bl.argumentSlot()); |
| 1528 |
case BindingLocation::Kind::Frame: |
| 1529 |
return NameLocation::FrameSlot(bindKind, bl.slot()); |
| 1530 |
case BindingLocation::Kind::Environment: |
| 1531 |
return NameLocation::EnvironmentCoordinate(bindKind, 0, bl.slot()); |
| 1532 |
case BindingLocation::Kind::Import: |
| 1533 |
return NameLocation::Import(); |
| 1534 |
case BindingLocation::Kind::NamedLambdaCallee: |
| 1535 |
return NameLocation::NamedLambdaCallee(); |
| 1536 |
} |
| 1537 |
MOZ_CRASH("Bad BindingKind"); |
| 1538 |
} |
| 1539 |
|
| 1540 |
bool isTopLevelFunction() const { |
| 1541 |
MOZ_ASSERT(!done()); |
| 1542 |
bool result = names_[index_].isTopLevelFunction(); |
| 1543 |
MOZ_ASSERT_IF(result, kind() == BindingKind::Var); |
| 1544 |
return result; |
| 1545 |
} |
| 1546 |
|
| 1547 |
bool hasArgumentSlot() const { |
| 1548 |
MOZ_ASSERT(!done()); |
| 1549 |
if (hasFormalParameterExprs()) { |
| 1550 |
return false; |
| 1551 |
} |
| 1552 |
return index_ >= positionalFormalStart_ && |
| 1553 |
index_ < nonPositionalFormalStart_; |
| 1554 |
} |
| 1555 |
|
| 1556 |
uint16_t argumentSlot() const { |
| 1557 |
MOZ_ASSERT(canHaveArgumentSlots()); |
| 1558 |
return mozilla::AssertedCast<uint16_t>(index_); |
| 1559 |
} |
| 1560 |
|
| 1561 |
uint32_t nextFrameSlot() const { |
| 1562 |
MOZ_ASSERT(canHaveFrameSlots()); |
| 1563 |
return frameSlot_; |
| 1564 |
} |
| 1565 |
|
| 1566 |
uint32_t nextEnvironmentSlot() const { |
| 1567 |
MOZ_ASSERT(canHaveEnvironmentSlots()); |
| 1568 |
return environmentSlot_; |
| 1569 |
} |
| 1570 |
}; |
| 1571 |
|
| 1572 |
template <typename NameT> |
| 1573 |
class AbstractBindingIter; |
| 1574 |
|
| 1575 |
template <> |
| 1576 |
class AbstractBindingIter<JSAtom> : public BaseAbstractBindingIter<JSAtom> { |
| 1577 |
using Base = BaseAbstractBindingIter<JSAtom>; |
| 1578 |
|
| 1579 |
public: |
| 1580 |
AbstractBindingIter(ScopeKind kind, BaseScopeData* data, |
| 1581 |
uint32_t firstFrameSlot); |
| 1582 |
|
| 1583 |
explicit AbstractBindingIter(Scope* scope); |
| 1584 |
explicit AbstractBindingIter(JSScript* script); |
| 1585 |
|
| 1586 |
using Base::Base; |
| 1587 |
|
| 1588 |
inline void trace(JSTracer* trc) { |
| 1589 |
TraceNullableBindingNames(trc, names_, length_); |
| 1590 |
} |
| 1591 |
}; |
| 1592 |
|
| 1593 |
template <> |
| 1594 |
class AbstractBindingIter<frontend::TaggedParserAtomIndex> |
| 1595 |
: public BaseAbstractBindingIter<frontend::TaggedParserAtomIndex> { |
| 1596 |
using Base = BaseAbstractBindingIter<frontend::TaggedParserAtomIndex>; |
| 1597 |
|
| 1598 |
public: |
| 1599 |
explicit AbstractBindingIter(const frontend::ScopeStencilRef& ref); |
| 1600 |
|
| 1601 |
using Base::Base; |
| 1602 |
}; |
| 1603 |
|
| 1604 |
void DumpBindings(JSContext* cx, Scope* scope); |
| 1605 |
JSAtom* FrameSlotName(JSScript* script, jsbytecode* pc); |
| 1606 |
|
| 1607 |
SharedShape* EmptyEnvironmentShape(JSContext* cx, const JSClass* cls, |
| 1608 |
uint32_t numSlots, ObjectFlags objectFlags); |
| 1609 |
|
| 1610 |
template <class T> |
| 1611 |
SharedShape* EmptyEnvironmentShape(JSContext* cx) { |
| 1612 |
return EmptyEnvironmentShape(cx, &T::class_, T::RESERVED_SLOTS, |
| 1613 |
T::OBJECT_FLAGS); |
| 1614 |
} |
| 1615 |
|
| 1616 |
// |
| 1617 |
// PositionalFormalParameterIter is a refinement BindingIter that only iterates |
| 1618 |
// over positional formal parameters of a function. |
| 1619 |
// |
| 1620 |
template <typename NameT> |
| 1621 |
class BasePositionalFormalParamterIter : public AbstractBindingIter<NameT> { |
| 1622 |
using Base = AbstractBindingIter<NameT>; |
| 1623 |
|
| 1624 |
protected: |
| 1625 |
void settle() { |
| 1626 |
if (this->index_ >= this->nonPositionalFormalStart_) { |
| 1627 |
this->index_ = this->length_; |
| 1628 |
} |
| 1629 |
} |
| 1630 |
|
| 1631 |
public: |
| 1632 |
using Base::Base; |
| 1633 |
|
| 1634 |
void operator++(int) { |
| 1635 |
Base::operator++(1); |
| 1636 |
settle(); |
| 1637 |
} |
| 1638 |
|
| 1639 |
bool isDestructured() const { return !this->name(); } |
| 1640 |
}; |
| 1641 |
|
| 1642 |
template <typename NameT> |
| 1643 |
class AbstractPositionalFormalParameterIter; |
| 1644 |
|
| 1645 |
template <> |
| 1646 |
class AbstractPositionalFormalParameterIter<JSAtom> |
| 1647 |
: public BasePositionalFormalParamterIter<JSAtom> { |
| 1648 |
using Base = BasePositionalFormalParamterIter<JSAtom>; |
| 1649 |
|
| 1650 |
public: |
| 1651 |
explicit AbstractPositionalFormalParameterIter(Scope* scope); |
| 1652 |
explicit AbstractPositionalFormalParameterIter(JSScript* script); |
| 1653 |
|
| 1654 |
using Base::Base; |
| 1655 |
}; |
| 1656 |
|
| 1657 |
template <> |
| 1658 |
class AbstractPositionalFormalParameterIter<frontend::TaggedParserAtomIndex> |
| 1659 |
: public BasePositionalFormalParamterIter<frontend::TaggedParserAtomIndex> { |
| 1660 |
using Base = |
| 1661 |
BasePositionalFormalParamterIter<frontend::TaggedParserAtomIndex>; |
| 1662 |
|
| 1663 |
public: |
| 1664 |
AbstractPositionalFormalParameterIter( |
| 1665 |
FunctionScope::AbstractData<frontend::TaggedParserAtomIndex>& data, |
| 1666 |
bool hasParameterExprs) |
| 1667 |
: Base(data, hasParameterExprs) { |
| 1668 |
settle(); |
| 1669 |
} |
| 1670 |
|
| 1671 |
using Base::Base; |
| 1672 |
}; |
| 1673 |
|
| 1674 |
using PositionalFormalParameterIter = |
| 1675 |
AbstractPositionalFormalParameterIter<JSAtom>; |
| 1676 |
|
| 1677 |
// |
| 1678 |
// Iterator for walking the scope chain. |
| 1679 |
// |
| 1680 |
// It may be placed in GC containers; for example: |
| 1681 |
// |
| 1682 |
// for (Rooted<ScopeIter> si(cx, ScopeIter(scope)); si; si++) { |
| 1683 |
// use(si); |
| 1684 |
// SomeMayGCOperation(); |
| 1685 |
// use(si); |
| 1686 |
// } |
| 1687 |
// |
| 1688 |
class MOZ_STACK_CLASS ScopeIter { |
| 1689 |
Scope* scope_; |
| 1690 |
|
| 1691 |
public: |
| 1692 |
explicit ScopeIter(Scope* scope) : scope_(scope) {} |
| 1693 |
|
| 1694 |
explicit ScopeIter(JSScript* script); |
| 1695 |
|
| 1696 |
explicit ScopeIter(const ScopeIter& si) = default; |
| 1697 |
|
| 1698 |
bool done() const { return !scope_; } |
| 1699 |
|
| 1700 |
explicit operator bool() const { return !done(); } |
| 1701 |
|
| 1702 |
void operator++(int) { |
| 1703 |
MOZ_ASSERT(!done()); |
| 1704 |
scope_ = scope_->enclosing(); |
| 1705 |
} |
| 1706 |
|
| 1707 |
Scope* scope() const { |
| 1708 |
MOZ_ASSERT(!done()); |
| 1709 |
return scope_; |
| 1710 |
} |
| 1711 |
|
| 1712 |
ScopeKind kind() const { |
| 1713 |
MOZ_ASSERT(!done()); |
| 1714 |
return scope_->kind(); |
| 1715 |
} |
| 1716 |
|
| 1717 |
// Returns the shape of the environment if it is known. It is possible to |
| 1718 |
// hasSyntacticEnvironment and to have no known shape, e.g., eval. |
| 1719 |
SharedShape* environmentShape() const { return scope()->environmentShape(); } |
| 1720 |
|
| 1721 |
// Returns whether this scope has a syntactic environment (i.e., an |
| 1722 |
// Environment that isn't a non-syntactic With or NonSyntacticVariables) |
| 1723 |
// on the environment chain. |
| 1724 |
bool hasSyntacticEnvironment() const; |
| 1725 |
|
| 1726 |
void trace(JSTracer* trc) { |
| 1727 |
if (scope_) { |
| 1728 |
TraceRoot(trc, &scope_, "scope iter scope"); |
| 1729 |
} |
| 1730 |
} |
| 1731 |
}; |
| 1732 |
|
| 1733 |
// |
| 1734 |
// Specializations of Rooted containers for the iterators. |
| 1735 |
// |
| 1736 |
|
| 1737 |
template <typename Wrapper> |
| 1738 |
class WrappedPtrOperations<BindingIter, Wrapper> { |
| 1739 |
const BindingIter& iter() const { |
| 1740 |
return static_cast<const Wrapper*>(this)->get(); |
| 1741 |
} |
| 1742 |
|
| 1743 |
public: |
| 1744 |
bool done() const { return iter().done(); } |
| 1745 |
explicit operator bool() const { return !done(); } |
| 1746 |
bool isLast() const { return iter().isLast(); } |
| 1747 |
bool canHaveArgumentSlots() const { return iter().canHaveArgumentSlots(); } |
| 1748 |
bool canHaveFrameSlots() const { return iter().canHaveFrameSlots(); } |
| 1749 |
bool canHaveEnvironmentSlots() const { |
| 1750 |
return iter().canHaveEnvironmentSlots(); |
| 1751 |
} |
| 1752 |
JSAtom* name() const { return iter().name(); } |
| 1753 |
bool closedOver() const { return iter().closedOver(); } |
| 1754 |
BindingLocation location() const { return iter().location(); } |
| 1755 |
BindingKind kind() const { return iter().kind(); } |
| 1756 |
bool isTopLevelFunction() const { return iter().isTopLevelFunction(); } |
| 1757 |
bool hasArgumentSlot() const { return iter().hasArgumentSlot(); } |
| 1758 |
uint16_t argumentSlot() const { return iter().argumentSlot(); } |
| 1759 |
uint32_t nextFrameSlot() const { return iter().nextFrameSlot(); } |
| 1760 |
uint32_t nextEnvironmentSlot() const { return iter().nextEnvironmentSlot(); } |
| 1761 |
}; |
| 1762 |
|
| 1763 |
template <typename Wrapper> |
| 1764 |
class MutableWrappedPtrOperations<BindingIter, Wrapper> |
| 1765 |
: public WrappedPtrOperations<BindingIter, Wrapper> { |
| 1766 |
BindingIter& iter() { return static_cast<Wrapper*>(this)->get(); } |
| 1767 |
|
| 1768 |
public: |
| 1769 |
void operator++(int) { iter().operator++(1); } |
| 1770 |
}; |
| 1771 |
|
| 1772 |
template <typename Wrapper> |
| 1773 |
class WrappedPtrOperations<ScopeIter, Wrapper> { |
| 1774 |
const ScopeIter& iter() const { |
| 1775 |
return static_cast<const Wrapper*>(this)->get(); |
| 1776 |
} |
| 1777 |
|
| 1778 |
public: |
| 1779 |
bool done() const { return iter().done(); } |
| 1780 |
explicit operator bool() const { return !done(); } |
| 1781 |
Scope* scope() const { return iter().scope(); } |
| 1782 |
ScopeKind kind() const { return iter().kind(); } |
| 1783 |
SharedShape* environmentShape() const { return iter().environmentShape(); } |
| 1784 |
bool hasSyntacticEnvironment() const { |
| 1785 |
return iter().hasSyntacticEnvironment(); |
| 1786 |
} |
| 1787 |
}; |
| 1788 |
|
| 1789 |
template <typename Wrapper> |
| 1790 |
class MutableWrappedPtrOperations<ScopeIter, Wrapper> |
| 1791 |
: public WrappedPtrOperations<ScopeIter, Wrapper> { |
| 1792 |
ScopeIter& iter() { return static_cast<Wrapper*>(this)->get(); } |
| 1793 |
|
| 1794 |
public: |
| 1795 |
void operator++(int) { iter().operator++(1); } |
| 1796 |
}; |
| 1797 |
|
| 1798 |
SharedShape* CreateEnvironmentShape(JSContext* cx, BindingIter& bi, |
| 1799 |
const JSClass* cls, uint32_t numSlots, |
| 1800 |
ObjectFlags objectFlags); |
| 1801 |
|
| 1802 |
SharedShape* CreateEnvironmentShapeForSyntheticModule( |
| 1803 |
JSContext* cx, const JSClass* cls, uint32_t numSlots, |
| 1804 |
Handle<ModuleObject*> module); |
| 1805 |
|
| 1806 |
SharedShape* EmptyEnvironmentShape(JSContext* cx, const JSClass* cls, |
| 1807 |
uint32_t numSlots, ObjectFlags objectFlags); |
| 1808 |
|
| 1809 |
static inline size_t GetOffsetOfParserScopeDataTrailingNames(ScopeKind kind) { |
| 1810 |
switch (kind) { |
| 1811 |
// FunctionScope |
| 1812 |
case ScopeKind::Function: |
| 1813 |
return GetOffsetOfScopeDataTrailingNames<FunctionScope::ParserData>(); |
| 1814 |
|
| 1815 |
// VarScope |
| 1816 |
case ScopeKind::FunctionBodyVar: |
| 1817 |
return GetOffsetOfScopeDataTrailingNames<VarScope::ParserData>(); |
| 1818 |
|
| 1819 |
// LexicalScope |
| 1820 |
case ScopeKind::Lexical: |
| 1821 |
case ScopeKind::SimpleCatch: |
| 1822 |
case ScopeKind::Catch: |
| 1823 |
case ScopeKind::NamedLambda: |
| 1824 |
case ScopeKind::StrictNamedLambda: |
| 1825 |
case ScopeKind::FunctionLexical: |
| 1826 |
return GetOffsetOfScopeDataTrailingNames<LexicalScope::ParserData>(); |
| 1827 |
|
| 1828 |
// ClassBodyScope |
| 1829 |
case ScopeKind::ClassBody: |
| 1830 |
return GetOffsetOfScopeDataTrailingNames<ClassBodyScope::ParserData>(); |
| 1831 |
|
| 1832 |
// EvalScope |
| 1833 |
case ScopeKind::Eval: |
| 1834 |
case ScopeKind::StrictEval: |
| 1835 |
return GetOffsetOfScopeDataTrailingNames<EvalScope::ParserData>(); |
| 1836 |
|
| 1837 |
// GlobalScope |
| 1838 |
case ScopeKind::Global: |
| 1839 |
case ScopeKind::NonSyntactic: |
| 1840 |
return GetOffsetOfScopeDataTrailingNames<GlobalScope::ParserData>(); |
| 1841 |
|
| 1842 |
// ModuleScope |
| 1843 |
case ScopeKind::Module: |
| 1844 |
return GetOffsetOfScopeDataTrailingNames<ModuleScope::ParserData>(); |
| 1845 |
|
| 1846 |
// WasmInstanceScope |
| 1847 |
case ScopeKind::WasmInstance: |
| 1848 |
return GetOffsetOfScopeDataTrailingNames<WasmInstanceScope::ParserData>(); |
| 1849 |
|
| 1850 |
// WasmFunctionScope |
| 1851 |
case ScopeKind::WasmFunction: |
| 1852 |
return GetOffsetOfScopeDataTrailingNames<WasmFunctionScope::ParserData>(); |
| 1853 |
|
| 1854 |
// WithScope doesn't have ScopeData. |
| 1855 |
case ScopeKind::With: |
| 1856 |
default: |
| 1857 |
MOZ_CRASH("Unexpected ScopeKind"); |
| 1858 |
} |
| 1859 |
|
| 1860 |
return 0; |
| 1861 |
} |
| 1862 |
|
| 1863 |
inline size_t SizeOfParserScopeData(ScopeKind kind, uint32_t length) { |
| 1864 |
return GetOffsetOfParserScopeDataTrailingNames(kind) + |
| 1865 |
sizeof(AbstractBindingName<frontend::TaggedParserAtomIndex>) * length; |
| 1866 |
} |
| 1867 |
|
| 1868 |
inline mozilla::Span<AbstractBindingName<frontend::TaggedParserAtomIndex>> |
| 1869 |
GetParserScopeDataTrailingNames( |
| 1870 |
ScopeKind kind, |
| 1871 |
AbstractBaseScopeData<frontend::TaggedParserAtomIndex>* data) { |
| 1872 |
return mozilla::Span( |
| 1873 |
reinterpret_cast<AbstractBindingName<frontend::TaggedParserAtomIndex>*>( |
| 1874 |
uintptr_t(data) + GetOffsetOfParserScopeDataTrailingNames(kind)), |
| 1875 |
data->length); |
| 1876 |
} |
| 1877 |
|
| 1878 |
} // namespace js |
| 1879 |
|
| 1880 |
namespace JS { |
| 1881 |
|
| 1882 |
template <> |
| 1883 |
struct GCPolicy<js::ScopeKind> : public IgnoreGCPolicy<js::ScopeKind> {}; |
| 1884 |
|
| 1885 |
template <typename T> |
| 1886 |
struct ScopeDataGCPolicy : public NonGCPointerPolicy<T> {}; |
| 1887 |
|
| 1888 |
#define DEFINE_SCOPE_DATA_GCPOLICY(Data) \ |
| 1889 |
template <> \ |
| 1890 |
struct MapTypeToRootKind<Data*> { \ |
| 1891 |
static const RootKind kind = RootKind::Traceable; \ |
| 1892 |
}; \ |
| 1893 |
template <> \ |
| 1894 |
struct GCPolicy<Data*> : public ScopeDataGCPolicy<Data*> {} |
| 1895 |
|
| 1896 |
DEFINE_SCOPE_DATA_GCPOLICY(js::LexicalScope::RuntimeData); |
| 1897 |
DEFINE_SCOPE_DATA_GCPOLICY(js::ClassBodyScope::RuntimeData); |
| 1898 |
DEFINE_SCOPE_DATA_GCPOLICY(js::FunctionScope::RuntimeData); |
| 1899 |
DEFINE_SCOPE_DATA_GCPOLICY(js::VarScope::RuntimeData); |
| 1900 |
DEFINE_SCOPE_DATA_GCPOLICY(js::GlobalScope::RuntimeData); |
| 1901 |
DEFINE_SCOPE_DATA_GCPOLICY(js::EvalScope::RuntimeData); |
| 1902 |
DEFINE_SCOPE_DATA_GCPOLICY(js::ModuleScope::RuntimeData); |
| 1903 |
DEFINE_SCOPE_DATA_GCPOLICY(js::WasmFunctionScope::RuntimeData); |
| 1904 |
|
| 1905 |
#undef DEFINE_SCOPE_DATA_GCPOLICY |
| 1906 |
|
| 1907 |
namespace ubi { |
| 1908 |
|
| 1909 |
template <> |
| 1910 |
class Concrete<js::Scope> : TracerConcrete<js::Scope> { |
| 1911 |
protected: |
| 1912 |
explicit Concrete(js::Scope* ptr) : TracerConcrete<js::Scope>(ptr) {} |
| 1913 |
|
| 1914 |
public: |
| 1915 |
static void construct(void* storage, js::Scope* ptr) { |
| 1916 |
new (storage) Concrete(ptr); |
| 1917 |
} |
| 1918 |
|
| 1919 |
CoarseType coarseType() const final { return CoarseType::Script; } |
| 1920 |
|
| 1921 |
Size size(mozilla::MallocSizeOf mallocSizeOf) const override; |
| 1922 |
|
| 1923 |
const char16_t* typeName() const override { return concreteTypeName; } |
| 1924 |
static const char16_t concreteTypeName[]; |
| 1925 |
}; |
| 1926 |
|
| 1927 |
} // namespace ubi |
| 1928 |
} // namespace JS |
| 1929 |
|
| 1930 |
#endif // vm_Scope_h |