| 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 frontend_Stencil_h |
| 8 |
#define frontend_Stencil_h |
| 9 |
|
| 10 |
#include "mozilla/Assertions.h" // MOZ_ASSERT |
| 11 |
#include "mozilla/Maybe.h" // mozilla::{Maybe, Nothing} |
| 12 |
#include "mozilla/MemoryReporting.h" // mozilla::MallocSizeOf |
| 13 |
#include "mozilla/Span.h" // mozilla::Span |
| 14 |
#include "mozilla/Variant.h" // mozilla::Variant |
| 15 |
|
| 16 |
#include <stddef.h> // size_t |
| 17 |
#include <stdint.h> // char16_t, uint8_t, uint16_t, uint32_t |
| 18 |
|
| 19 |
#include "frontend/AbstractScopePtr.h" // AbstractScopePtr, ScopeIndex |
| 20 |
#include "frontend/ObjLiteral.h" // ObjLiteralStencil |
| 21 |
#include "frontend/ParserAtom.h" // TaggedParserAtomIndex |
| 22 |
#include "frontend/ScriptIndex.h" // ScriptIndex |
| 23 |
#include "frontend/TaggedParserAtomIndexHasher.h" // frontend::TaggedParserAtomIndexHasher |
| 24 |
#include "frontend/TypedIndex.h" // TypedIndex |
| 25 |
#include "js/AllocPolicy.h" // SystemAllocPolicy |
| 26 |
#include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin |
| 27 |
#include "js/RefCounted.h" // AtomicRefCounted |
| 28 |
#include "js/RegExpFlags.h" // JS::RegExpFlags |
| 29 |
#include "js/RootingAPI.h" // Handle |
| 30 |
#include "js/TypeDecls.h" // JSContext |
| 31 |
#include "js/UniquePtr.h" // js::UniquePtr |
| 32 |
#include "js/Utility.h" // UniqueTwoByteChars |
| 33 |
#include "js/Vector.h" // js::Vector |
| 34 |
#include "vm/FunctionFlags.h" // FunctionFlags |
| 35 |
#include "vm/Scope.h" // Scope, BaseScopeData, FunctionScope, LexicalScope, VarScope, GlobalScope, EvalScope, ModuleScope |
| 36 |
#include "vm/ScopeKind.h" // ScopeKind |
| 37 |
#include "vm/SharedStencil.h" // ImmutableScriptFlags, GCThingIndex, js::SharedImmutableScriptData, MemberInitializers, SourceExtent |
| 38 |
|
| 39 |
namespace js { |
| 40 |
|
| 41 |
class LifoAlloc; |
| 42 |
class JSONPrinter; |
| 43 |
class RegExpObject; |
| 44 |
|
| 45 |
namespace frontend { |
| 46 |
|
| 47 |
struct CompilationInput; |
| 48 |
struct CompilationStencil; |
| 49 |
struct CompilationAtomCache; |
| 50 |
struct CompilationGCOutput; |
| 51 |
struct CompilationStencilMerger; |
| 52 |
class RegExpStencil; |
| 53 |
class BigIntStencil; |
| 54 |
class StencilXDR; |
| 55 |
|
| 56 |
using BaseParserScopeData = AbstractBaseScopeData<TaggedParserAtomIndex>; |
| 57 |
using ParserBindingName = AbstractBindingName<TaggedParserAtomIndex>; |
| 58 |
|
| 59 |
template <typename Scope> |
| 60 |
using ParserScopeSlotInfo = typename Scope::SlotInfo; |
| 61 |
using ParserGlobalScopeSlotInfo = ParserScopeSlotInfo<GlobalScope>; |
| 62 |
using ParserEvalScopeSlotInfo = ParserScopeSlotInfo<EvalScope>; |
| 63 |
using ParserLexicalScopeSlotInfo = ParserScopeSlotInfo<LexicalScope>; |
| 64 |
using ParserClassBodyScopeSlotInfo = ParserScopeSlotInfo<ClassBodyScope>; |
| 65 |
using ParserFunctionScopeSlotInfo = ParserScopeSlotInfo<FunctionScope>; |
| 66 |
using ParserModuleScopeSlotInfo = ParserScopeSlotInfo<ModuleScope>; |
| 67 |
using ParserVarScopeSlotInfo = ParserScopeSlotInfo<VarScope>; |
| 68 |
|
| 69 |
using ParserBindingIter = AbstractBindingIter<TaggedParserAtomIndex>; |
| 70 |
using ParserPositionalFormalParameterIter = |
| 71 |
AbstractPositionalFormalParameterIter<TaggedParserAtomIndex>; |
| 72 |
|
| 73 |
// [SMDOC] Script Stencil (Frontend Representation) |
| 74 |
// |
| 75 |
// Stencils are the set of data structures capturing the result of parsing and |
| 76 |
// bytecode emission. The Stencil format is a precursor format that is then used |
| 77 |
// to allocate the corresponding scripts on the GC heap that will be used for |
| 78 |
// execution. By decoupling from the GC and other runtime systems, robust |
| 79 |
// caching and speculation systems can be built that are more thread-agnostic |
| 80 |
// and flexible. |
| 81 |
// |
| 82 |
// See: https://bugzil.la/stencil |
| 83 |
// |
| 84 |
// There are numerous data structures that make up the Stencil format. The |
| 85 |
// structures are designed for fast serialization to and from disk by preferring |
| 86 |
// indices over pointers and vectors instead of graphs to allow bulk operations. |
| 87 |
// |
| 88 |
// |
| 89 |
// ParserAtom |
| 90 |
// ---------- |
| 91 |
// Our parser relies on atomized strings as part of its normal operations and so |
| 92 |
// a `ParserAtom` type exists that mirrors the `JSAtom` type but does not |
| 93 |
// involve the garbage collector. This is possible because the lifetime of these |
| 94 |
// ParserAtoms is the lifetime of the Stencil that makes use of them and we do |
| 95 |
// not need finer grained collection. |
| 96 |
// |
| 97 |
// |
| 98 |
// ScriptStencil |
| 99 |
// ------------- |
| 100 |
// The key structures generated by parsing are instances of `ScriptStencil`. |
| 101 |
// There is a `ScriptStencil` for the top-level script and for each inner |
| 102 |
// function. It contains information needed to create the `JSFunction` (if it is |
| 103 |
// a function) and the `BaseScript` (if not asm.js) and may or may not have |
| 104 |
// bytecode. Each `ScriptStencil` may also reference the following Stencil types |
| 105 |
// (similar to the `BaseScript::gcthings()` list): |
| 106 |
// |
| 107 |
// * ParserAtom |
| 108 |
// * ScopeStencil |
| 109 |
// * RegExpStencil |
| 110 |
// * BigIntStencil |
| 111 |
// * ObjLiteralStencil |
| 112 |
// * StencilModuleMetadata |
| 113 |
// |
| 114 |
// |
| 115 |
// CompilationStencil / ExtensibleCompilationStencil |
| 116 |
// ------------------------------------------------- |
| 117 |
// Parsing a single JavaScript file may generate a tree of `ScriptStencil` that |
| 118 |
// we then package up into the `ExtensibleCompilationStencil` type or |
| 119 |
// `CompilationStencil`. They contain a series of vectors/spans segregated by |
| 120 |
// data type for fast processing (a.k.a Data Oriented Design). |
| 121 |
// |
| 122 |
// `ExtensibleCompilationStencil` is mutable type used during parsing, and |
| 123 |
// can be allocated either on stack or heap. |
| 124 |
// `ExtensibleCompilationStencil` holds vectors of stencils. |
| 125 |
// |
| 126 |
// `CompilationStencil` is immutable type used for caching the compilation |
| 127 |
// result, and is allocated on heap with refcount. |
| 128 |
// `CompilationStencil` holds spans of stencils, and it can point either |
| 129 |
// owned data or borrowed data. |
| 130 |
// The borrowed data can be from other `ExtensibleCompilationStencil` or |
| 131 |
// from serialized stencil (XDR) on memory or mmap. |
| 132 |
// |
| 133 |
// Delazifying a function will generate its bytecode but some fields remain |
| 134 |
// unchanged from the initial lazy parse. |
| 135 |
// When we delazify a function that was lazily parsed, we generate a new |
| 136 |
// Stencil at the point too. These delazifications can be merged into the |
| 137 |
// Stencil of the initial parse by using `CompilationStencilMerger`. |
| 138 |
// |
| 139 |
// Conversion from ExtensibleCompilationStencil to CompilationStencil |
| 140 |
// ------------------------------------------------------------------ |
| 141 |
// There are multiple ways to convert from `ExtensibleCompilationStencil` to |
| 142 |
// `CompilationStencil`: |
| 143 |
// |
| 144 |
// 1. Temporarily borrow `ExtensibleCompilationStencil` content and call |
| 145 |
// function that takes `CompilationStencil` reference, and keep using the |
| 146 |
// `ExtensibleCompilationStencil` after that: |
| 147 |
// |
| 148 |
// ExtensibleCompilationStencil extensible = ...; |
| 149 |
// { |
| 150 |
// BorrowingCompilationStencil stencil(extensible); |
| 151 |
// // Use `stencil reference. |
| 152 |
// } |
| 153 |
// |
| 154 |
// 2. Take the ownership of an on-heap ExtensibleCompilationStencil. This makes |
| 155 |
// the `CompilationStencil` self-contained and it's useful for caching: |
| 156 |
// |
| 157 |
// UniquePtr<ExtensibleCompilationStencil> extensible = ...; |
| 158 |
// CompilationStencil stencil(std::move(extensible)); |
| 159 |
// |
| 160 |
// Conversion from CompilationStencil to ExtensibleCompilationStencil |
| 161 |
// ------------------------------------------------------------------ |
| 162 |
// In the same way, there are multiple ways to convert from |
| 163 |
// `CompilationStencil` to `ExtensibleCompilationStencil`: |
| 164 |
// |
| 165 |
// 1. Take the ownership of `CompilationStencil`'s underlying data, Only when |
| 166 |
// stencil owns the data and the refcount is 1: |
| 167 |
// |
| 168 |
// RefPtr<CompilationStencil> stencil = ...; |
| 169 |
// ExtensibleCompilationStencil extensible(...); |
| 170 |
// // NOTE: This is equivalent to cloneFrom below if `stencil` has refcount |
| 171 |
// // more than 2, or it doesn't own the data. |
| 172 |
// extensible.steal(fc, std::move(stencil)); |
| 173 |
// |
| 174 |
// 2. Clone the underlying data. This is slow but safe operation: |
| 175 |
// |
| 176 |
// CompilationStencil stencil = ...; |
| 177 |
// ExtensibleCompilationStencil extensible(...); |
| 178 |
// extensible.cloneFrom(fc, stencil); |
| 179 |
// |
| 180 |
// 3. Take the ownership back from the `CompilationStencil` which is created by |
| 181 |
// taking the ownership of an on-heap `ExtensibleCompilationStencil`: |
| 182 |
// |
| 183 |
// CompilationStencil stencil = ...; |
| 184 |
// ExtensibleCompilationStencil* extensible = stencil.takeOwnedBorrow(); |
| 185 |
// |
| 186 |
// CompilationGCOutput |
| 187 |
// ------------------- |
| 188 |
// When a Stencil is instantiated the equivalent script objects are allocated on |
| 189 |
// the GC heap and their pointers are collected into the `CompilationGCOutput` |
| 190 |
// structure. This is only used temporarily during instantiation. |
| 191 |
// |
| 192 |
// |
| 193 |
// CompilationState |
| 194 |
// ---------------- |
| 195 |
// This is another temporary structure used by the parser while the Stencil is |
| 196 |
// being generated. Once the `CompilationStencil` is complete, this can be |
| 197 |
// released. |
| 198 |
|
| 199 |
// Typed indices for the different stencil elements in the compilation result. |
| 200 |
using RegExpIndex = TypedIndex<RegExpStencil>; |
| 201 |
using BigIntIndex = TypedIndex<BigIntStencil>; |
| 202 |
using ObjLiteralIndex = TypedIndex<ObjLiteralStencil>; |
| 203 |
|
| 204 |
// Index into {ExtensibleCompilationStencil,CompilationStencil}.gcThingData. |
| 205 |
class CompilationGCThingType {}; |
| 206 |
using CompilationGCThingIndex = TypedIndex<CompilationGCThingType>; |
| 207 |
|
| 208 |
// A syntax-checked regular expression string. |
| 209 |
class RegExpStencil { |
| 210 |
friend class StencilXDR; |
| 211 |
|
| 212 |
TaggedParserAtomIndex atom_; |
| 213 |
// Use uint32_t to make this struct fully-packed. |
| 214 |
uint32_t flags_; |
| 215 |
|
| 216 |
friend struct CompilationStencilMerger; |
| 217 |
|
| 218 |
public: |
| 219 |
RegExpStencil() = default; |
| 220 |
|
| 221 |
RegExpStencil(TaggedParserAtomIndex atom, JS::RegExpFlags flags) |
| 222 |
: atom_(atom), flags_(flags.value()) {} |
| 223 |
|
| 224 |
JS::RegExpFlags flags() const { return JS::RegExpFlags(flags_); } |
| 225 |
|
| 226 |
RegExpObject* createRegExp(JSContext* cx, |
| 227 |
const CompilationAtomCache& atomCache) const; |
| 228 |
|
| 229 |
// This is used by `Reflect.parse` when we need the RegExpObject but are not |
| 230 |
// doing a complete instantiation of the CompilationStencil. |
| 231 |
RegExpObject* createRegExpAndEnsureAtom( |
| 232 |
JSContext* cx, FrontendContext* fc, ParserAtomsTable& parserAtoms, |
| 233 |
CompilationAtomCache& atomCache) const; |
| 234 |
|
| 235 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 236 |
void dump() const; |
| 237 |
void dump(JSONPrinter& json, const CompilationStencil* stencil) const; |
| 238 |
void dumpFields(JSONPrinter& json, const CompilationStencil* stencil) const; |
| 239 |
#endif |
| 240 |
}; |
| 241 |
|
| 242 |
// This owns a set of characters guaranteed to parse into a BigInt via |
| 243 |
// ParseBigIntLiteral. Used to avoid allocating the BigInt on the |
| 244 |
// GC heap during parsing. |
| 245 |
class BigIntStencil { |
| 246 |
friend class StencilXDR; |
| 247 |
|
| 248 |
// Source of the BigInt literal. |
| 249 |
// It's not null-terminated, and also trailing 'n' suffix is not included. |
| 250 |
// |
| 251 |
// Int64-sized BigInt values are directly stored inline as int64_t. |
| 252 |
mozilla::Variant<mozilla::Span<char16_t>, int64_t> bigInt_{int64_t{}}; |
| 253 |
|
| 254 |
// Methods used by XDR. |
| 255 |
mozilla::Span<char16_t>& source() { |
| 256 |
if (bigInt_.is<int64_t>()) { |
| 257 |
bigInt_ = mozilla::AsVariant(mozilla::Span<char16_t>{}); |
| 258 |
} |
| 259 |
return bigInt_.as<mozilla::Span<char16_t>>(); |
| 260 |
} |
| 261 |
const mozilla::Span<char16_t>& source() const { |
| 262 |
return bigInt_.as<mozilla::Span<char16_t>>(); |
| 263 |
} |
| 264 |
|
| 265 |
[[nodiscard]] bool initFromChars(FrontendContext* fc, LifoAlloc& alloc, |
| 266 |
mozilla::Span<const char16_t> buf); |
| 267 |
|
| 268 |
public: |
| 269 |
BigIntStencil() = default; |
| 270 |
|
| 271 |
[[nodiscard]] bool init(FrontendContext* fc, LifoAlloc& alloc, |
| 272 |
mozilla::Span<const char16_t> buf); |
| 273 |
|
| 274 |
[[nodiscard]] bool init(FrontendContext* fc, LifoAlloc& alloc, |
| 275 |
const BigIntStencil& other); |
| 276 |
|
| 277 |
BigInt* createBigInt(JSContext* cx) const; |
| 278 |
|
| 279 |
// Methods used by constant-folding. |
| 280 |
bool isZero() const; |
| 281 |
bool inplaceNegate(); |
| 282 |
bool inplaceBitNot(); |
| 283 |
|
| 284 |
#ifdef DEBUG |
| 285 |
bool isContainedIn(const LifoAlloc& alloc) const; |
| 286 |
#endif |
| 287 |
|
| 288 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 289 |
void dump() const; |
| 290 |
void dump(JSONPrinter& json) const; |
| 291 |
void dumpCharsNoQuote(GenericPrinter& out) const; |
| 292 |
#endif |
| 293 |
}; |
| 294 |
|
| 295 |
using BigIntStencilVector = Vector<BigIntStencil, 0, js::SystemAllocPolicy>; |
| 296 |
|
| 297 |
class ScopeStencil { |
| 298 |
friend class StencilXDR; |
| 299 |
friend class InputScope; |
| 300 |
friend class AbstractBindingIter<frontend::TaggedParserAtomIndex>; |
| 301 |
friend struct CompilationStencil; |
| 302 |
friend struct CompilationStencilMerger; |
| 303 |
|
| 304 |
// The enclosing scope. Valid only if HasEnclosing flag is set. |
| 305 |
// compilation applies. |
| 306 |
ScopeIndex enclosing_; |
| 307 |
|
| 308 |
// First frame slot to use, or LOCALNO_LIMIT if none are allowed. |
| 309 |
uint32_t firstFrameSlot_ = UINT32_MAX; |
| 310 |
|
| 311 |
// The number of environment shape's slots. Valid only if |
| 312 |
// HasEnvironmentShape flag is set. |
| 313 |
uint32_t numEnvironmentSlots_; |
| 314 |
|
| 315 |
// Canonical function if this is a FunctionScope. Valid only if |
| 316 |
// kind_ is ScopeKind::Function. |
| 317 |
ScriptIndex functionIndex_; |
| 318 |
|
| 319 |
// The kind determines the corresponding BaseParserScopeData. |
| 320 |
ScopeKind kind_{UINT8_MAX}; |
| 321 |
|
| 322 |
// True if this scope has enclosing scope stencil. Otherwise, the enclosing |
| 323 |
// scope will be read from CompilationInput while instantiating. Self-hosting |
| 324 |
// is a special case and will use `emptyGlobalScope` when there is no |
| 325 |
// enclosing scope stencil. |
| 326 |
static constexpr uint8_t HasEnclosing = 1 << 0; |
| 327 |
|
| 328 |
// If true, an environment Shape must be created. The shape itself may |
| 329 |
// have no slots if the environment may be extensible later. |
| 330 |
static constexpr uint8_t HasEnvironmentShape = 1 << 1; |
| 331 |
|
| 332 |
// True if this is a FunctionScope for an arrow function. |
| 333 |
static constexpr uint8_t IsArrow = 1 << 2; |
| 334 |
|
| 335 |
uint8_t flags_ = 0; |
| 336 |
|
| 337 |
// To make this struct packed, add explicit field for padding. |
| 338 |
uint16_t padding_ = 0; |
| 339 |
|
| 340 |
public: |
| 341 |
// For XDR only. |
| 342 |
ScopeStencil() = default; |
| 343 |
|
| 344 |
ScopeStencil(ScopeKind kind, mozilla::Maybe<ScopeIndex> enclosing, |
| 345 |
uint32_t firstFrameSlot, |
| 346 |
mozilla::Maybe<uint32_t> numEnvironmentSlots, |
| 347 |
mozilla::Maybe<ScriptIndex> functionIndex = mozilla::Nothing(), |
| 348 |
bool isArrow = false) |
| 349 |
: enclosing_(enclosing.valueOr(ScopeIndex(0))), |
| 350 |
firstFrameSlot_(firstFrameSlot), |
| 351 |
numEnvironmentSlots_(numEnvironmentSlots.valueOr(0)), |
| 352 |
functionIndex_(functionIndex.valueOr(ScriptIndex(0))), |
| 353 |
kind_(kind), |
| 354 |
flags_((enclosing.isSome() ? HasEnclosing : 0) | |
| 355 |
(numEnvironmentSlots.isSome() ? HasEnvironmentShape : 0) | |
| 356 |
(isArrow ? IsArrow : 0)) { |
| 357 |
MOZ_ASSERT((kind == ScopeKind::Function) == functionIndex.isSome()); |
| 358 |
// Silence -Wunused-private-field warnings. |
| 359 |
(void)padding_; |
| 360 |
} |
| 361 |
|
| 362 |
private: |
| 363 |
// Create ScopeStencil with `args`, and append ScopeStencil and `data` to |
| 364 |
// `compilationState`, and return the index of them as `indexOut`. |
| 365 |
template <typename... Args> |
| 366 |
static bool appendScopeStencilAndData(FrontendContext* fc, |
| 367 |
CompilationState& compilationState, |
| 368 |
BaseParserScopeData* data, |
| 369 |
ScopeIndex* indexOut, Args&&... args); |
| 370 |
|
| 371 |
public: |
| 372 |
static bool createForFunctionScope( |
| 373 |
FrontendContext* fc, CompilationState& compilationState, |
| 374 |
FunctionScope::ParserData* dataArg, bool hasParameterExprs, |
| 375 |
bool needsEnvironment, ScriptIndex functionIndex, bool isArrow, |
| 376 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index); |
| 377 |
|
| 378 |
static bool createForLexicalScope( |
| 379 |
FrontendContext* fc, CompilationState& compilationState, ScopeKind kind, |
| 380 |
LexicalScope::ParserData* dataArg, uint32_t firstFrameSlot, |
| 381 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index); |
| 382 |
|
| 383 |
static bool createForClassBodyScope( |
| 384 |
FrontendContext* fc, CompilationState& compilationState, ScopeKind kind, |
| 385 |
ClassBodyScope::ParserData* dataArg, uint32_t firstFrameSlot, |
| 386 |
mozilla::Maybe<ScopeIndex> enclosing, ScopeIndex* index); |
| 387 |
|
| 388 |
static bool createForVarScope(FrontendContext* fc, |
| 389 |
CompilationState& compilationState, |
| 390 |
ScopeKind kind, VarScope::ParserData* dataArg, |
| 391 |
uint32_t firstFrameSlot, bool needsEnvironment, |
| 392 |
mozilla::Maybe<ScopeIndex> enclosing, |
| 393 |
ScopeIndex* index); |
| 394 |
|
| 395 |
static bool createForGlobalScope(FrontendContext* fc, |
| 396 |
CompilationState& compilationState, |
| 397 |
ScopeKind kind, |
| 398 |
GlobalScope::ParserData* dataArg, |
| 399 |
ScopeIndex* index); |
| 400 |
|
| 401 |
static bool createForEvalScope(FrontendContext* fc, |
| 402 |
CompilationState& compilationState, |
| 403 |
ScopeKind kind, EvalScope::ParserData* dataArg, |
| 404 |
mozilla::Maybe<ScopeIndex> enclosing, |
| 405 |
ScopeIndex* index); |
| 406 |
|
| 407 |
static bool createForModuleScope(FrontendContext* fc, |
| 408 |
CompilationState& compilationState, |
| 409 |
ModuleScope::ParserData* dataArg, |
| 410 |
mozilla::Maybe<ScopeIndex> enclosing, |
| 411 |
ScopeIndex* index); |
| 412 |
|
| 413 |
static bool createForWithScope(FrontendContext* fc, |
| 414 |
CompilationState& compilationState, |
| 415 |
mozilla::Maybe<ScopeIndex> enclosing, |
| 416 |
ScopeIndex* index); |
| 417 |
|
| 418 |
AbstractScopePtr enclosing(CompilationState& compilationState) const; |
| 419 |
js::Scope* enclosingExistingScope(const CompilationInput& input, |
| 420 |
const CompilationGCOutput& gcOutput) const; |
| 421 |
|
| 422 |
private: |
| 423 |
bool hasEnclosing() const { return flags_ & HasEnclosing; } |
| 424 |
|
| 425 |
ScopeIndex enclosing() const { |
| 426 |
MOZ_ASSERT(hasEnclosing()); |
| 427 |
return enclosing_; |
| 428 |
} |
| 429 |
|
| 430 |
uint32_t firstFrameSlot() const { return firstFrameSlot_; } |
| 431 |
|
| 432 |
bool hasEnvironmentShape() const { return flags_ & HasEnvironmentShape; } |
| 433 |
|
| 434 |
uint32_t numEnvironmentSlots() const { |
| 435 |
MOZ_ASSERT(hasEnvironmentShape()); |
| 436 |
return numEnvironmentSlots_; |
| 437 |
} |
| 438 |
|
| 439 |
bool isFunction() const { return kind_ == ScopeKind::Function; } |
| 440 |
|
| 441 |
ScriptIndex functionIndex() const { return functionIndex_; } |
| 442 |
|
| 443 |
public: |
| 444 |
ScopeKind kind() const { return kind_; } |
| 445 |
|
| 446 |
bool hasEnvironment() const { |
| 447 |
// Check if scope kind alone means we have an env shape, and |
| 448 |
// otherwise check if we have one created. |
| 449 |
return Scope::hasEnvironment(kind(), hasEnvironmentShape()); |
| 450 |
} |
| 451 |
|
| 452 |
bool isArrow() const { return flags_ & IsArrow; } |
| 453 |
|
| 454 |
Scope* createScope(JSContext* cx, CompilationInput& input, |
| 455 |
CompilationGCOutput& gcOutput, |
| 456 |
BaseParserScopeData* baseScopeData) const; |
| 457 |
Scope* createScope(JSContext* cx, CompilationAtomCache& atomCache, |
| 458 |
Handle<Scope*> enclosingScope, |
| 459 |
BaseParserScopeData* baseScopeData) const; |
| 460 |
|
| 461 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 462 |
void dump() const; |
| 463 |
void dump(JSONPrinter& json, const BaseParserScopeData* baseScopeData, |
| 464 |
const CompilationStencil* stencil) const; |
| 465 |
void dumpFields(JSONPrinter& json, const BaseParserScopeData* baseScopeData, |
| 466 |
const CompilationStencil* stencil) const; |
| 467 |
#endif |
| 468 |
|
| 469 |
private: |
| 470 |
// Transfer ownership into a new UniquePtr. |
| 471 |
template <typename SpecificScopeType> |
| 472 |
UniquePtr<typename SpecificScopeType::RuntimeData> createSpecificScopeData( |
| 473 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 474 |
BaseParserScopeData* baseData) const; |
| 475 |
|
| 476 |
template <typename SpecificEnvironmentType> |
| 477 |
[[nodiscard]] bool createSpecificShape( |
| 478 |
JSContext* cx, ScopeKind kind, BaseScopeData* scopeData, |
| 479 |
MutableHandle<SharedShape*> shape) const; |
| 480 |
|
| 481 |
template <typename SpecificScopeType, typename SpecificEnvironmentType> |
| 482 |
Scope* createSpecificScope(JSContext* cx, CompilationAtomCache& atomCache, |
| 483 |
Handle<Scope*> enclosingScope, |
| 484 |
BaseParserScopeData* baseData) const; |
| 485 |
|
| 486 |
template <typename ScopeT> |
| 487 |
static constexpr bool matchScopeKind(ScopeKind kind) { |
| 488 |
switch (kind) { |
| 489 |
case ScopeKind::Function: { |
| 490 |
return std::is_same_v<ScopeT, FunctionScope>; |
| 491 |
} |
| 492 |
case ScopeKind::Lexical: |
| 493 |
case ScopeKind::SimpleCatch: |
| 494 |
case ScopeKind::Catch: |
| 495 |
case ScopeKind::NamedLambda: |
| 496 |
case ScopeKind::StrictNamedLambda: |
| 497 |
case ScopeKind::FunctionLexical: { |
| 498 |
return std::is_same_v<ScopeT, LexicalScope>; |
| 499 |
} |
| 500 |
case ScopeKind::ClassBody: { |
| 501 |
return std::is_same_v<ScopeT, ClassBodyScope>; |
| 502 |
} |
| 503 |
case ScopeKind::FunctionBodyVar: { |
| 504 |
return std::is_same_v<ScopeT, VarScope>; |
| 505 |
} |
| 506 |
case ScopeKind::Global: |
| 507 |
case ScopeKind::NonSyntactic: { |
| 508 |
return std::is_same_v<ScopeT, GlobalScope>; |
| 509 |
} |
| 510 |
case ScopeKind::Eval: |
| 511 |
case ScopeKind::StrictEval: { |
| 512 |
return std::is_same_v<ScopeT, EvalScope>; |
| 513 |
} |
| 514 |
case ScopeKind::Module: { |
| 515 |
return std::is_same_v<ScopeT, ModuleScope>; |
| 516 |
} |
| 517 |
case ScopeKind::With: { |
| 518 |
return std::is_same_v<ScopeT, WithScope>; |
| 519 |
} |
| 520 |
case ScopeKind::WasmFunction: |
| 521 |
case ScopeKind::WasmInstance: { |
| 522 |
return false; |
| 523 |
} |
| 524 |
} |
| 525 |
return false; |
| 526 |
} |
| 527 |
}; |
| 528 |
|
| 529 |
class StencilModuleImportAttribute { |
| 530 |
public: |
| 531 |
TaggedParserAtomIndex key; |
| 532 |
TaggedParserAtomIndex value; |
| 533 |
|
| 534 |
StencilModuleImportAttribute() = default; |
| 535 |
StencilModuleImportAttribute(TaggedParserAtomIndex key, |
| 536 |
TaggedParserAtomIndex value) |
| 537 |
: key(key), value(value) {} |
| 538 |
|
| 539 |
bool operator!=(const StencilModuleImportAttribute& rhs) const { |
| 540 |
return key != rhs.key || value != rhs.value; |
| 541 |
} |
| 542 |
|
| 543 |
bool operator==(const StencilModuleImportAttribute& rhs) const { |
| 544 |
return !(*this != rhs); |
| 545 |
} |
| 546 |
}; |
| 547 |
|
| 548 |
class StencilModuleRequest { |
| 549 |
public: |
| 550 |
TaggedParserAtomIndex specifier; |
| 551 |
TaggedParserAtomIndex firstUnsupportedAttributeKey; |
| 552 |
|
| 553 |
using ImportAttributeVector = |
| 554 |
Vector<StencilModuleImportAttribute, 0, js::SystemAllocPolicy>; |
| 555 |
ImportAttributeVector attributes; |
| 556 |
|
| 557 |
// For XDR only. |
| 558 |
StencilModuleRequest() = default; |
| 559 |
|
| 560 |
explicit StencilModuleRequest(TaggedParserAtomIndex specifier) |
| 561 |
: specifier(specifier) { |
| 562 |
MOZ_ASSERT(specifier); |
| 563 |
} |
| 564 |
|
| 565 |
StencilModuleRequest(const StencilModuleRequest& other) |
| 566 |
: specifier(other.specifier), |
| 567 |
firstUnsupportedAttributeKey(other.firstUnsupportedAttributeKey) { |
| 568 |
AutoEnterOOMUnsafeRegion oomUnsafe; |
| 569 |
if (!attributes.appendAll(other.attributes)) { |
| 570 |
oomUnsafe.crash("StencilModuleRequest::StencilModuleRequest"); |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
StencilModuleRequest(StencilModuleRequest&& other) noexcept |
| 575 |
: specifier(other.specifier), |
| 576 |
firstUnsupportedAttributeKey(other.firstUnsupportedAttributeKey), |
| 577 |
attributes(std::move(other.attributes)) {} |
| 578 |
|
| 579 |
StencilModuleRequest& operator=(StencilModuleRequest& other) { |
| 580 |
specifier = other.specifier; |
| 581 |
firstUnsupportedAttributeKey = other.firstUnsupportedAttributeKey; |
| 582 |
attributes = std::move(other.attributes); |
| 583 |
return *this; |
| 584 |
} |
| 585 |
|
| 586 |
StencilModuleRequest& operator=(StencilModuleRequest&& other) noexcept { |
| 587 |
specifier = other.specifier; |
| 588 |
firstUnsupportedAttributeKey = other.firstUnsupportedAttributeKey; |
| 589 |
attributes = std::move(other.attributes); |
| 590 |
return *this; |
| 591 |
} |
| 592 |
|
| 593 |
bool operator==(const StencilModuleRequest& other) const { |
| 594 |
size_t attrLen = attributes.length(); |
| 595 |
if (specifier != other.specifier || |
| 596 |
firstUnsupportedAttributeKey != other.firstUnsupportedAttributeKey || |
| 597 |
attrLen != other.attributes.length()) { |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
for (size_t i = 0; i < attrLen; i++) { |
| 602 |
if (attributes[i] != other.attributes[i]) { |
| 603 |
return false; |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
return true; |
| 608 |
} |
| 609 |
|
| 610 |
bool operator!=(const StencilModuleRequest& other) const { |
| 611 |
return !(*this == other); |
| 612 |
} |
| 613 |
}; |
| 614 |
|
| 615 |
struct StencilModuleRequestHasher { |
| 616 |
using Key = js::frontend::StencilModuleRequest; |
| 617 |
using Lookup = Key; |
| 618 |
|
| 619 |
static HashNumber hash(const Lookup& l) { |
| 620 |
HashNumber hash = 0; |
| 621 |
size_t attrLen = l.attributes.length(); |
| 622 |
for (size_t i = 0; i < attrLen; i++) { |
| 623 |
hash = mozilla::AddToHash( |
| 624 |
hash, TaggedParserAtomIndexHasher::hash(l.attributes[i].key), |
| 625 |
TaggedParserAtomIndexHasher::hash(l.attributes[i].value)); |
| 626 |
} |
| 627 |
return mozilla::AddToHash(hash, |
| 628 |
TaggedParserAtomIndexHasher::hash(l.specifier)); |
| 629 |
} |
| 630 |
|
| 631 |
static bool match(const Key& k, const Lookup& l) { return k == l; } |
| 632 |
}; |
| 633 |
|
| 634 |
class MaybeModuleRequestIndex { |
| 635 |
static constexpr uint32_t NOTHING = UINT32_MAX; |
| 636 |
|
| 637 |
uint32_t bits = NOTHING; |
| 638 |
|
| 639 |
public: |
| 640 |
MaybeModuleRequestIndex() = default; |
| 641 |
explicit MaybeModuleRequestIndex(uint32_t index) : bits(index) { |
| 642 |
MOZ_ASSERT(isSome()); |
| 643 |
} |
| 644 |
|
| 645 |
MaybeModuleRequestIndex(const MaybeModuleRequestIndex& other) = default; |
| 646 |
MaybeModuleRequestIndex& operator=(const MaybeModuleRequestIndex& other) = |
| 647 |
default; |
| 648 |
|
| 649 |
bool isNothing() const { return bits == NOTHING; } |
| 650 |
bool isSome() const { return !isNothing(); } |
| 651 |
explicit operator bool() const { return isSome(); } |
| 652 |
|
| 653 |
uint32_t value() const { |
| 654 |
MOZ_ASSERT(isSome()); |
| 655 |
return bits; |
| 656 |
} |
| 657 |
|
| 658 |
uint32_t* operator&() { return &bits; } |
| 659 |
}; |
| 660 |
|
| 661 |
// Common type for ImportEntry / ExportEntry / ModuleRequest within frontend. We |
| 662 |
// use a shared stencil class type to simplify serialization. |
| 663 |
// |
| 664 |
// https://tc39.es/ecma262/#importentry-record |
| 665 |
// https://tc39.es/ecma262/#exportentry-record |
| 666 |
// |
| 667 |
// Note: We subdivide the spec's ExportEntry into ExportAs / ExportFrom forms |
| 668 |
// for readability. |
| 669 |
class StencilModuleEntry { |
| 670 |
public: |
| 671 |
// clang-format off |
| 672 |
// |
| 673 |
// | RequestedModule | ImportEntry | ImportNamespaceEntry | ExportAs | ExportFrom | ExportNamespaceFrom | ExportBatchFrom | |
| 674 |
// |----------------------------------------------------------------------------------------------------------------------| |
| 675 |
// moduleRequest | required | required | required | null | required | required | required | |
| 676 |
// localName | null | required | required | required | null | null | null | |
| 677 |
// importName | null | required | null | null | required | null | null | |
| 678 |
// exportName | null | null | null | required | required | required | null | |
| 679 |
// |
| 680 |
// clang-format on |
| 681 |
MaybeModuleRequestIndex moduleRequest; |
| 682 |
TaggedParserAtomIndex localName; |
| 683 |
TaggedParserAtomIndex importName; |
| 684 |
TaggedParserAtomIndex exportName; |
| 685 |
|
| 686 |
// Location used for error messages. If this is for a module request entry |
| 687 |
// then it is the module specifier string, otherwise the import/export spec |
| 688 |
// that failed. Exports may not fill these fields if an error cannot be |
| 689 |
// generated such as `export let x;`. |
| 690 |
|
| 691 |
// Line number (1-origin). |
| 692 |
uint32_t lineno = 0; |
| 693 |
|
| 694 |
// Column number in UTF-16 code units. |
| 695 |
JS::ColumnNumberOneOrigin column; |
| 696 |
|
| 697 |
private: |
| 698 |
StencilModuleEntry(uint32_t lineno, JS::ColumnNumberOneOrigin column) |
| 699 |
: lineno(lineno), column(column) {} |
| 700 |
|
| 701 |
public: |
| 702 |
// For XDR only. |
| 703 |
StencilModuleEntry() = default; |
| 704 |
|
| 705 |
StencilModuleEntry(const StencilModuleEntry& other) |
| 706 |
: moduleRequest(other.moduleRequest), |
| 707 |
localName(other.localName), |
| 708 |
importName(other.importName), |
| 709 |
exportName(other.exportName), |
| 710 |
lineno(other.lineno), |
| 711 |
column(other.column) {} |
| 712 |
|
| 713 |
StencilModuleEntry(StencilModuleEntry&& other) noexcept |
| 714 |
: moduleRequest(other.moduleRequest), |
| 715 |
localName(other.localName), |
| 716 |
importName(other.importName), |
| 717 |
exportName(other.exportName), |
| 718 |
lineno(other.lineno), |
| 719 |
column(other.column) {} |
| 720 |
|
| 721 |
StencilModuleEntry& operator=(StencilModuleEntry& other) { |
| 722 |
moduleRequest = other.moduleRequest; |
| 723 |
localName = other.localName; |
| 724 |
importName = other.importName; |
| 725 |
exportName = other.exportName; |
| 726 |
lineno = other.lineno; |
| 727 |
column = other.column; |
| 728 |
return *this; |
| 729 |
} |
| 730 |
|
| 731 |
StencilModuleEntry& operator=(StencilModuleEntry&& other) noexcept { |
| 732 |
moduleRequest = other.moduleRequest; |
| 733 |
localName = other.localName; |
| 734 |
importName = other.importName; |
| 735 |
exportName = other.exportName; |
| 736 |
lineno = other.lineno; |
| 737 |
column = other.column; |
| 738 |
return *this; |
| 739 |
} |
| 740 |
|
| 741 |
static StencilModuleEntry requestedModule( |
| 742 |
MaybeModuleRequestIndex moduleRequest, uint32_t lineno, |
| 743 |
JS::ColumnNumberOneOrigin column) { |
| 744 |
MOZ_ASSERT(moduleRequest.isSome()); |
| 745 |
StencilModuleEntry entry(lineno, column); |
| 746 |
entry.moduleRequest = moduleRequest; |
| 747 |
return entry; |
| 748 |
} |
| 749 |
|
| 750 |
static StencilModuleEntry importEntry(MaybeModuleRequestIndex moduleRequest, |
| 751 |
TaggedParserAtomIndex localName, |
| 752 |
TaggedParserAtomIndex importName, |
| 753 |
uint32_t lineno, |
| 754 |
JS::ColumnNumberOneOrigin column) { |
| 755 |
MOZ_ASSERT(moduleRequest.isSome()); |
| 756 |
MOZ_ASSERT(localName && importName); |
| 757 |
StencilModuleEntry entry(lineno, column); |
| 758 |
entry.moduleRequest = moduleRequest; |
| 759 |
entry.localName = localName; |
| 760 |
entry.importName = importName; |
| 761 |
return entry; |
| 762 |
} |
| 763 |
|
| 764 |
static StencilModuleEntry importNamespaceEntry( |
| 765 |
MaybeModuleRequestIndex moduleRequest, TaggedParserAtomIndex localName, |
| 766 |
uint32_t lineno, JS::ColumnNumberOneOrigin column) { |
| 767 |
MOZ_ASSERT(moduleRequest.isSome()); |
| 768 |
MOZ_ASSERT(localName); |
| 769 |
StencilModuleEntry entry(lineno, column); |
| 770 |
entry.moduleRequest = moduleRequest; |
| 771 |
entry.localName = localName; |
| 772 |
return entry; |
| 773 |
} |
| 774 |
|
| 775 |
static StencilModuleEntry exportAsEntry(TaggedParserAtomIndex localName, |
| 776 |
TaggedParserAtomIndex exportName, |
| 777 |
uint32_t lineno, |
| 778 |
JS::ColumnNumberOneOrigin column) { |
| 779 |
MOZ_ASSERT(localName && exportName); |
| 780 |
StencilModuleEntry entry(lineno, column); |
| 781 |
entry.localName = localName; |
| 782 |
entry.exportName = exportName; |
| 783 |
return entry; |
| 784 |
} |
| 785 |
|
| 786 |
static StencilModuleEntry exportFromEntry( |
| 787 |
MaybeModuleRequestIndex moduleRequest, TaggedParserAtomIndex importName, |
| 788 |
TaggedParserAtomIndex exportName, uint32_t lineno, |
| 789 |
JS::ColumnNumberOneOrigin column) { |
| 790 |
MOZ_ASSERT(moduleRequest.isSome()); |
| 791 |
MOZ_ASSERT(importName && exportName); |
| 792 |
StencilModuleEntry entry(lineno, column); |
| 793 |
entry.moduleRequest = moduleRequest; |
| 794 |
entry.importName = importName; |
| 795 |
entry.exportName = exportName; |
| 796 |
return entry; |
| 797 |
} |
| 798 |
|
| 799 |
static StencilModuleEntry exportNamespaceFromEntry( |
| 800 |
MaybeModuleRequestIndex moduleRequest, TaggedParserAtomIndex exportName, |
| 801 |
uint32_t lineno, JS::ColumnNumberOneOrigin column) { |
| 802 |
MOZ_ASSERT(moduleRequest.isSome()); |
| 803 |
MOZ_ASSERT(exportName); |
| 804 |
StencilModuleEntry entry(lineno, column); |
| 805 |
entry.moduleRequest = MaybeModuleRequestIndex(moduleRequest); |
| 806 |
entry.exportName = exportName; |
| 807 |
return entry; |
| 808 |
} |
| 809 |
|
| 810 |
static StencilModuleEntry exportBatchFromEntry( |
| 811 |
MaybeModuleRequestIndex moduleRequest, uint32_t lineno, |
| 812 |
JS::ColumnNumberOneOrigin column) { |
| 813 |
MOZ_ASSERT(moduleRequest.isSome()); |
| 814 |
StencilModuleEntry entry(lineno, column); |
| 815 |
entry.moduleRequest = MaybeModuleRequestIndex(moduleRequest); |
| 816 |
return entry; |
| 817 |
} |
| 818 |
}; |
| 819 |
|
| 820 |
// Metadata generated by parsing module scripts, including import/export tables. |
| 821 |
class StencilModuleMetadata |
| 822 |
: public js::AtomicRefCounted<StencilModuleMetadata> { |
| 823 |
public: |
| 824 |
using RequestVector = Vector<StencilModuleRequest, 0, js::SystemAllocPolicy>; |
| 825 |
using EntryVector = Vector<StencilModuleEntry, 0, js::SystemAllocPolicy>; |
| 826 |
|
| 827 |
RequestVector moduleRequests; |
| 828 |
EntryVector requestedModules; |
| 829 |
EntryVector importEntries; |
| 830 |
EntryVector localExportEntries; |
| 831 |
EntryVector indirectExportEntries; |
| 832 |
EntryVector starExportEntries; |
| 833 |
FunctionDeclarationVector functionDecls; |
| 834 |
// Set to true if the module has a top-level await keyword. |
| 835 |
bool isAsync = false; |
| 836 |
|
| 837 |
StencilModuleMetadata() = default; |
| 838 |
|
| 839 |
bool initModule(JSContext* cx, FrontendContext* fc, |
| 840 |
CompilationAtomCache& atomCache, |
| 841 |
JS::Handle<ModuleObject*> module) const; |
| 842 |
|
| 843 |
size_t sizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const { |
| 844 |
return mallocSizeOf(this) + |
| 845 |
requestedModules.sizeOfExcludingThis(mallocSizeOf) + |
| 846 |
importEntries.sizeOfExcludingThis(mallocSizeOf) + |
| 847 |
localExportEntries.sizeOfExcludingThis(mallocSizeOf) + |
| 848 |
indirectExportEntries.sizeOfExcludingThis(mallocSizeOf) + |
| 849 |
starExportEntries.sizeOfExcludingThis(mallocSizeOf) + |
| 850 |
functionDecls.sizeOfExcludingThis(mallocSizeOf); |
| 851 |
} |
| 852 |
|
| 853 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 854 |
void dump() const; |
| 855 |
void dump(JSONPrinter& json, const CompilationStencil* stencil) const; |
| 856 |
void dumpFields(JSONPrinter& json, const CompilationStencil* stencil) const; |
| 857 |
#endif |
| 858 |
|
| 859 |
private: |
| 860 |
bool createModuleRequestObjects( |
| 861 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 862 |
MutableHandle<ModuleRequestVector> output) const; |
| 863 |
bool createRequestedModules( |
| 864 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 865 |
Handle<ModuleRequestVector> moduleRequests, |
| 866 |
MutableHandle<RequestedModuleVector> output) const; |
| 867 |
bool createImportEntries(JSContext* cx, CompilationAtomCache& atomCache, |
| 868 |
Handle<ModuleRequestVector> moduleRequests, |
| 869 |
MutableHandle<ImportEntryVector> output) const; |
| 870 |
bool createExportEntries(JSContext* cx, CompilationAtomCache& atomCache, |
| 871 |
Handle<ModuleRequestVector> moduleRequests, |
| 872 |
const EntryVector& input, |
| 873 |
MutableHandle<ExportEntryVector> output) const; |
| 874 |
ModuleRequestObject* createModuleRequestObject( |
| 875 |
JSContext* cx, CompilationAtomCache& atomCache, |
| 876 |
const StencilModuleRequest& request) const; |
| 877 |
}; |
| 878 |
|
| 879 |
// As an alternative to a ScopeIndex (which references a ScopeStencil), we may |
| 880 |
// instead refer to an existing scope from GlobalObject::emptyGlobalScope(). |
| 881 |
// |
| 882 |
// NOTE: This is only used for the self-hosting global. |
| 883 |
class EmptyGlobalScopeType {}; |
| 884 |
|
| 885 |
// Things pointed by this index all end up being baked into GC things as part |
| 886 |
// of stencil instantiation. |
| 887 |
// |
| 888 |
// 0x0000_0000 Null |
| 889 |
// 0x1YYY_YYYY 28-bit ParserAtom |
| 890 |
// 0x2YYY_YYYY Well-known/static atom (See TaggedParserAtomIndex) |
| 891 |
// 0x3YYY_YYYY 28-bit BigInt |
| 892 |
// 0x4YYY_YYYY 28-bit ObjLiteral |
| 893 |
// 0x5YYY_YYYY 28-bit RegExp |
| 894 |
// 0x6YYY_YYYY 28-bit Scope |
| 895 |
// 0x7YYY_YYYY 28-bit Function |
| 896 |
// 0x8000_0000 EmptyGlobalScope |
| 897 |
class TaggedScriptThingIndex { |
| 898 |
uint32_t data_; |
| 899 |
|
| 900 |
static constexpr size_t IndexBit = TaggedParserAtomIndex::IndexBit; |
| 901 |
static constexpr size_t IndexMask = TaggedParserAtomIndex::IndexMask; |
| 902 |
|
| 903 |
static constexpr size_t TagShift = TaggedParserAtomIndex::TagShift; |
| 904 |
static constexpr size_t TagBit = TaggedParserAtomIndex::TagBit; |
| 905 |
static constexpr size_t TagMask = TaggedParserAtomIndex::TagMask; |
| 906 |
|
| 907 |
public: |
| 908 |
enum class Kind : uint32_t { |
| 909 |
Null = uint32_t(TaggedParserAtomIndex::Kind::Null), |
| 910 |
ParserAtomIndex = uint32_t(TaggedParserAtomIndex::Kind::ParserAtomIndex), |
| 911 |
WellKnown = uint32_t(TaggedParserAtomIndex::Kind::WellKnown), |
| 912 |
BigInt, |
| 913 |
ObjLiteral, |
| 914 |
RegExp, |
| 915 |
Scope, |
| 916 |
Function, |
| 917 |
EmptyGlobalScope, |
| 918 |
}; |
| 919 |
|
| 920 |
private: |
| 921 |
static constexpr uint32_t NullTag = uint32_t(Kind::Null) << TagShift; |
| 922 |
static_assert(NullTag == TaggedParserAtomIndex::NullTag); |
| 923 |
static constexpr uint32_t ParserAtomIndexTag = uint32_t(Kind::ParserAtomIndex) |
| 924 |
<< TagShift; |
| 925 |
static_assert(ParserAtomIndexTag == |
| 926 |
TaggedParserAtomIndex::ParserAtomIndexTag); |
| 927 |
static constexpr uint32_t WellKnownTag = uint32_t(Kind::WellKnown) |
| 928 |
<< TagShift; |
| 929 |
static_assert(WellKnownTag == TaggedParserAtomIndex::WellKnownTag); |
| 930 |
|
| 931 |
static constexpr uint32_t BigIntTag = uint32_t(Kind::BigInt) << TagShift; |
| 932 |
static constexpr uint32_t ObjLiteralTag = uint32_t(Kind::ObjLiteral) |
| 933 |
<< TagShift; |
| 934 |
static constexpr uint32_t RegExpTag = uint32_t(Kind::RegExp) << TagShift; |
| 935 |
static constexpr uint32_t ScopeTag = uint32_t(Kind::Scope) << TagShift; |
| 936 |
static constexpr uint32_t FunctionTag = uint32_t(Kind::Function) << TagShift; |
| 937 |
static constexpr uint32_t EmptyGlobalScopeTag = |
| 938 |
uint32_t(Kind::EmptyGlobalScope) << TagShift; |
| 939 |
|
| 940 |
public: |
| 941 |
static constexpr uint32_t IndexLimit = Bit(IndexBit); |
| 942 |
|
| 943 |
TaggedScriptThingIndex() : data_(NullTag) {} |
| 944 |
|
| 945 |
explicit TaggedScriptThingIndex(TaggedParserAtomIndex index) |
| 946 |
: data_(index.rawData()) {} |
| 947 |
explicit TaggedScriptThingIndex(BigIntIndex index) |
| 948 |
: data_(uint32_t(index) | BigIntTag) { |
| 949 |
MOZ_ASSERT(uint32_t(index) < IndexLimit); |
| 950 |
} |
| 951 |
explicit TaggedScriptThingIndex(ObjLiteralIndex index) |
| 952 |
: data_(uint32_t(index) | ObjLiteralTag) { |
| 953 |
MOZ_ASSERT(uint32_t(index) < IndexLimit); |
| 954 |
} |
| 955 |
explicit TaggedScriptThingIndex(RegExpIndex index) |
| 956 |
: data_(uint32_t(index) | RegExpTag) { |
| 957 |
MOZ_ASSERT(uint32_t(index) < IndexLimit); |
| 958 |
} |
| 959 |
explicit TaggedScriptThingIndex(ScopeIndex index) |
| 960 |
: data_(uint32_t(index) | ScopeTag) { |
| 961 |
MOZ_ASSERT(uint32_t(index) < IndexLimit); |
| 962 |
} |
| 963 |
explicit TaggedScriptThingIndex(ScriptIndex index) |
| 964 |
: data_(uint32_t(index) | FunctionTag) { |
| 965 |
MOZ_ASSERT(uint32_t(index) < IndexLimit); |
| 966 |
} |
| 967 |
explicit TaggedScriptThingIndex(EmptyGlobalScopeType t) |
| 968 |
: data_(EmptyGlobalScopeTag) {} |
| 969 |
|
| 970 |
bool isAtom() const { |
| 971 |
return (data_ & TagMask) == ParserAtomIndexTag || |
| 972 |
(data_ & TagMask) == WellKnownTag; |
| 973 |
} |
| 974 |
bool isNull() const { |
| 975 |
bool result = !data_; |
| 976 |
MOZ_ASSERT_IF(result, (data_ & TagMask) == NullTag); |
| 977 |
return result; |
| 978 |
} |
| 979 |
bool isBigInt() const { return (data_ & TagMask) == BigIntTag; } |
| 980 |
bool isObjLiteral() const { return (data_ & TagMask) == ObjLiteralTag; } |
| 981 |
bool isRegExp() const { return (data_ & TagMask) == RegExpTag; } |
| 982 |
bool isScope() const { return (data_ & TagMask) == ScopeTag; } |
| 983 |
bool isFunction() const { return (data_ & TagMask) == FunctionTag; } |
| 984 |
bool isEmptyGlobalScope() const { |
| 985 |
return (data_ & TagMask) == EmptyGlobalScopeTag; |
| 986 |
} |
| 987 |
|
| 988 |
TaggedParserAtomIndex toAtom() const { |
| 989 |
MOZ_ASSERT(isAtom()); |
| 990 |
return TaggedParserAtomIndex::fromRaw(data_); |
| 991 |
} |
| 992 |
BigIntIndex toBigInt() const { return BigIntIndex(data_ & IndexMask); } |
| 993 |
ObjLiteralIndex toObjLiteral() const { |
| 994 |
return ObjLiteralIndex(data_ & IndexMask); |
| 995 |
} |
| 996 |
RegExpIndex toRegExp() const { return RegExpIndex(data_ & IndexMask); } |
| 997 |
ScopeIndex toScope() const { return ScopeIndex(data_ & IndexMask); } |
| 998 |
ScriptIndex toFunction() const { return ScriptIndex(data_ & IndexMask); } |
| 999 |
|
| 1000 |
TaggedParserAtomIndex toAtomOrNull() const { |
| 1001 |
MOZ_ASSERT(isAtom() || isNull()); |
| 1002 |
return TaggedParserAtomIndex::fromRaw(data_); |
| 1003 |
} |
| 1004 |
|
| 1005 |
uint32_t* rawDataRef() { return &data_; } |
| 1006 |
uint32_t rawData() const { return data_; } |
| 1007 |
|
| 1008 |
Kind tag() const { return Kind((data_ & TagMask) >> TagShift); } |
| 1009 |
|
| 1010 |
bool operator==(const TaggedScriptThingIndex& rhs) const { |
| 1011 |
return data_ == rhs.data_; |
| 1012 |
} |
| 1013 |
}; |
| 1014 |
|
| 1015 |
// Data generated by frontend that will be used to create a js::BaseScript. |
| 1016 |
class ScriptStencil { |
| 1017 |
friend struct CompilationStencilMerger; |
| 1018 |
|
| 1019 |
public: |
| 1020 |
// Fields for BaseScript. |
| 1021 |
// Used by: |
| 1022 |
// * Global script |
| 1023 |
// * Eval |
| 1024 |
// * Module |
| 1025 |
// * non-lazy Function (except asm.js module) |
| 1026 |
// * lazy Function (cannot be asm.js module) |
| 1027 |
|
| 1028 |
// GCThings are stored into |
| 1029 |
// {ExtensibleCompilationStencil,CompilationStencil}.gcThingData, |
| 1030 |
// in [gcThingsOffset, gcThingsOffset + gcThingsLength) range. |
| 1031 |
CompilationGCThingIndex gcThingsOffset; |
| 1032 |
uint32_t gcThingsLength = 0; |
| 1033 |
|
| 1034 |
// Fields for JSFunction. |
| 1035 |
// Used by: |
| 1036 |
// * non-lazy Function |
| 1037 |
// * lazy Function |
| 1038 |
// * asm.js module |
| 1039 |
|
| 1040 |
// The explicit or implicit name of the function. The FunctionFlags indicate |
| 1041 |
// the kind of name. |
| 1042 |
TaggedParserAtomIndex functionAtom; |
| 1043 |
|
| 1044 |
// If this ScriptStencil refers to a lazy child of the function being |
| 1045 |
// compiled, this field holds the child's immediately enclosing scope's index. |
| 1046 |
// Once compilation succeeds, we will store the scope pointed by this in the |
| 1047 |
// child's BaseScript. (Debugger may become confused if lazy scripts refer to |
| 1048 |
// partially initialized enclosing scopes, so we must avoid storing the |
| 1049 |
// scope in the BaseScript until compilation has completed |
| 1050 |
// successfully.) |
| 1051 |
// |
| 1052 |
// OR |
| 1053 |
// |
| 1054 |
// This may be used for self-hosting canonical name (TaggedParserAtomIndex). |
| 1055 |
TaggedScriptThingIndex enclosingScopeOrCanonicalName; |
| 1056 |
|
| 1057 |
// See: `FunctionFlags`. |
| 1058 |
FunctionFlags functionFlags = {}; |
| 1059 |
|
| 1060 |
// This is set by the BytecodeEmitter of the enclosing script when a reference |
| 1061 |
// to this function is generated. |
| 1062 |
static constexpr uint16_t WasEmittedByEnclosingScriptFlag = 1 << 0; |
| 1063 |
|
| 1064 |
// If this is for the root of delazification, this represents |
| 1065 |
// MutableScriptFlagsEnum::AllowRelazify value of the script *after* |
| 1066 |
// delazification. |
| 1067 |
// False otherwise. |
| 1068 |
static constexpr uint16_t AllowRelazifyFlag = 1 << 1; |
| 1069 |
|
| 1070 |
// Set if this is non-lazy script and shared data is created. |
| 1071 |
// The shared data is stored into CompilationStencil.sharedData. |
| 1072 |
static constexpr uint16_t HasSharedDataFlag = 1 << 2; |
| 1073 |
|
| 1074 |
// True if this script is lazy function and has enclosing scope. In that |
| 1075 |
// case, `enclosingScopeOrCanonicalName` will hold the ScopeIndex. |
| 1076 |
static constexpr uint16_t HasLazyFunctionEnclosingScopeIndexFlag = 1 << 3; |
| 1077 |
|
| 1078 |
// True if this script is a self-hosted function with a canonical name |
| 1079 |
// explicitly set. In that case, `enclosingScopeOrCanonicalName` will hold the |
| 1080 |
// TaggedParserAtomIndex. |
| 1081 |
static constexpr uint16_t HasSelfHostedCanonicalName = 1 << 4; |
| 1082 |
|
| 1083 |
uint16_t flags_ = 0; |
| 1084 |
|
| 1085 |
// End of fields. |
| 1086 |
|
| 1087 |
ScriptStencil() = default; |
| 1088 |
|
| 1089 |
bool isFunction() const { |
| 1090 |
bool result = functionFlags.toRaw() != 0x0000; |
| 1091 |
MOZ_ASSERT_IF( |
| 1092 |
result, functionFlags.isAsmJSNative() || functionFlags.hasBaseScript()); |
| 1093 |
return result; |
| 1094 |
} |
| 1095 |
|
| 1096 |
bool hasGCThings() const { return gcThingsLength; } |
| 1097 |
|
| 1098 |
mozilla::Span<TaggedScriptThingIndex> gcthings( |
| 1099 |
const CompilationStencil& stencil) const; |
| 1100 |
|
| 1101 |
bool wasEmittedByEnclosingScript() const { |
| 1102 |
return flags_ & WasEmittedByEnclosingScriptFlag; |
| 1103 |
} |
| 1104 |
|
| 1105 |
void setWasEmittedByEnclosingScript() { |
| 1106 |
flags_ |= WasEmittedByEnclosingScriptFlag; |
| 1107 |
} |
| 1108 |
|
| 1109 |
bool allowRelazify() const { return flags_ & AllowRelazifyFlag; } |
| 1110 |
|
| 1111 |
void setAllowRelazify() { flags_ |= AllowRelazifyFlag; } |
| 1112 |
|
| 1113 |
bool isGhost() const { return functionFlags.isGhost(); } |
| 1114 |
void setIsGhost() { functionFlags.setIsGhost(); } |
| 1115 |
|
| 1116 |
bool hasSharedData() const { return flags_ & HasSharedDataFlag; } |
| 1117 |
|
| 1118 |
void setHasSharedData() { flags_ |= HasSharedDataFlag; } |
| 1119 |
|
| 1120 |
bool hasLazyFunctionEnclosingScopeIndex() const { |
| 1121 |
return flags_ & HasLazyFunctionEnclosingScopeIndexFlag; |
| 1122 |
} |
| 1123 |
|
| 1124 |
bool hasSelfHostedCanonicalName() const { |
| 1125 |
return flags_ & HasSelfHostedCanonicalName; |
| 1126 |
} |
| 1127 |
|
| 1128 |
private: |
| 1129 |
void setHasLazyFunctionEnclosingScopeIndex() { |
| 1130 |
flags_ |= HasLazyFunctionEnclosingScopeIndexFlag; |
| 1131 |
} |
| 1132 |
|
| 1133 |
void setHasSelfHostedCanonicalName() { flags_ |= HasSelfHostedCanonicalName; } |
| 1134 |
|
| 1135 |
public: |
| 1136 |
void setLazyFunctionEnclosingScopeIndex(ScopeIndex index) { |
| 1137 |
MOZ_ASSERT(enclosingScopeOrCanonicalName.isNull()); |
| 1138 |
enclosingScopeOrCanonicalName = TaggedScriptThingIndex(index); |
| 1139 |
setHasLazyFunctionEnclosingScopeIndex(); |
| 1140 |
} |
| 1141 |
|
| 1142 |
void resetHasLazyFunctionEnclosingScopeIndexAfterStencilMerge() { |
| 1143 |
flags_ &= ~HasLazyFunctionEnclosingScopeIndexFlag; |
| 1144 |
enclosingScopeOrCanonicalName = TaggedScriptThingIndex(); |
| 1145 |
} |
| 1146 |
|
| 1147 |
ScopeIndex lazyFunctionEnclosingScopeIndex() const { |
| 1148 |
MOZ_ASSERT(hasLazyFunctionEnclosingScopeIndex()); |
| 1149 |
return enclosingScopeOrCanonicalName.toScope(); |
| 1150 |
} |
| 1151 |
|
| 1152 |
void setSelfHostedCanonicalName(TaggedParserAtomIndex name) { |
| 1153 |
MOZ_ASSERT(enclosingScopeOrCanonicalName.isNull()); |
| 1154 |
enclosingScopeOrCanonicalName = TaggedScriptThingIndex(name); |
| 1155 |
setHasSelfHostedCanonicalName(); |
| 1156 |
} |
| 1157 |
|
| 1158 |
TaggedParserAtomIndex selfHostedCanonicalName() const { |
| 1159 |
MOZ_ASSERT(hasSelfHostedCanonicalName()); |
| 1160 |
return enclosingScopeOrCanonicalName.toAtom(); |
| 1161 |
} |
| 1162 |
|
| 1163 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1164 |
void dump() const; |
| 1165 |
void dump(JSONPrinter& json, const CompilationStencil* stencil) const; |
| 1166 |
void dumpFields(JSONPrinter& json, const CompilationStencil* stencil) const; |
| 1167 |
#endif |
| 1168 |
}; |
| 1169 |
|
| 1170 |
// In addition to ScriptStencil, data generated only while initial-parsing. |
| 1171 |
class ScriptStencilExtra { |
| 1172 |
public: |
| 1173 |
// See `BaseScript::immutableFlags_`. |
| 1174 |
ImmutableScriptFlags immutableFlags; |
| 1175 |
|
| 1176 |
// The location of this script in the source. |
| 1177 |
SourceExtent extent; |
| 1178 |
|
| 1179 |
// See `PrivateScriptData::memberInitializers_`. |
| 1180 |
// This data only valid when `UseMemberInitializers` script flag is true. |
| 1181 |
uint32_t memberInitializers_ = 0; |
| 1182 |
|
| 1183 |
// See `JSFunction::nargs_`. |
| 1184 |
uint16_t nargs = 0; |
| 1185 |
|
| 1186 |
// To make this struct packed, add explicit field for padding. |
| 1187 |
uint16_t padding_ = 0; |
| 1188 |
|
| 1189 |
ScriptStencilExtra() = default; |
| 1190 |
|
| 1191 |
RO_IMMUTABLE_SCRIPT_FLAGS(immutableFlags) |
| 1192 |
|
| 1193 |
void setMemberInitializers(MemberInitializers member) { |
| 1194 |
MOZ_ASSERT(useMemberInitializers()); |
| 1195 |
memberInitializers_ = member.serialize(); |
| 1196 |
} |
| 1197 |
|
| 1198 |
MemberInitializers memberInitializers() const { |
| 1199 |
MOZ_ASSERT(useMemberInitializers()); |
| 1200 |
return MemberInitializers::deserialize(memberInitializers_); |
| 1201 |
} |
| 1202 |
|
| 1203 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1204 |
void dump() const; |
| 1205 |
void dump(JSONPrinter& json) const; |
| 1206 |
void dumpFields(JSONPrinter& json) const; |
| 1207 |
#endif |
| 1208 |
}; |
| 1209 |
|
| 1210 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1211 |
void DumpTaggedParserAtomIndex(js::JSONPrinter& json, |
| 1212 |
TaggedParserAtomIndex taggedIndex, |
| 1213 |
const CompilationStencil* stencil); |
| 1214 |
|
| 1215 |
void DumpTaggedParserAtomIndexNoQuote(GenericPrinter& out, |
| 1216 |
TaggedParserAtomIndex taggedIndex, |
| 1217 |
const CompilationStencil* stencil); |
| 1218 |
#endif |
| 1219 |
|
| 1220 |
} /* namespace frontend */ |
| 1221 |
|
| 1222 |
#if defined(DEBUG) || defined(JS_JITSPEW) |
| 1223 |
void DumpImmutableScriptFlags(js::JSONPrinter& json, |
| 1224 |
ImmutableScriptFlags immutableFlags); |
| 1225 |
void DumpFunctionFlagsItems(js::JSONPrinter& json, FunctionFlags functionFlags); |
| 1226 |
#endif |
| 1227 |
|
| 1228 |
} /* namespace js */ |
| 1229 |
|
| 1230 |
#endif /* frontend_Stencil_h */ |