-
Notifications
You must be signed in to change notification settings - Fork 54
/
Scope.h
1522 lines (1227 loc) · 44.2 KB
/
Scope.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef vm_Scope_h
#define vm_Scope_h
#include "mozilla/Maybe.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/Variant.h"
#include <stddef.h>
#include "jsutil.h"
#include "gc/DeletePolicy.h"
#include "gc/Heap.h"
#include "gc/Policy.h"
#include "js/UbiNode.h"
#include "js/UniquePtr.h"
#include "vm/BytecodeUtil.h"
#include "vm/JSObject.h"
#include "vm/Xdr.h"
namespace js {
class BaseScopeData;
class ModuleObject;
class Scope;
enum class BindingKind : uint8_t {
Import,
FormalParameter,
Var,
Let,
Const,
// So you think named lambda callee names are consts? Nope! They don't
// throw when being assigned to in sloppy mode.
NamedLambdaCallee
};
static inline bool BindingKindIsLexical(BindingKind kind) {
return kind == BindingKind::Let || kind == BindingKind::Const;
}
enum class ScopeKind : uint8_t {
// FunctionScope
Function,
// VarScope
FunctionBodyVar,
ParameterExpressionVar,
// LexicalScope
Lexical,
SimpleCatch,
Catch,
NamedLambda,
StrictNamedLambda,
// WithScope
With,
// EvalScope
Eval,
StrictEval,
// GlobalScope
Global,
NonSyntactic,
// ModuleScope
Module,
// WasmInstanceScope
WasmInstance,
// WasmFunctionScope
WasmFunction
};
static inline bool ScopeKindIsCatch(ScopeKind kind) {
return kind == ScopeKind::SimpleCatch || kind == ScopeKind::Catch;
}
static inline bool ScopeKindIsInBody(ScopeKind kind) {
return kind == ScopeKind::Lexical || kind == ScopeKind::SimpleCatch ||
kind == ScopeKind::Catch || kind == ScopeKind::With ||
kind == ScopeKind::FunctionBodyVar ||
kind == ScopeKind::ParameterExpressionVar;
}
const char* BindingKindString(BindingKind kind);
const char* ScopeKindString(ScopeKind kind);
class BindingName {
// A JSAtom* with its low bit used as a tag for the:
// * whether it is closed over (i.e., exists in the environment shape)
// * whether it is a top-level function binding in global or eval scope,
// instead of var binding (both are in the same range in Scope data)
uintptr_t bits_;
static const uintptr_t ClosedOverFlag = 0x1;
// TODO: We should reuse this bit for let vs class distinction to
// show the better redeclaration error message (bug 1428672).
static const uintptr_t TopLevelFunctionFlag = 0x2;
static const uintptr_t FlagMask = 0x3;
public:
BindingName() : bits_(0) {}
BindingName(JSAtom* name, bool closedOver, bool isTopLevelFunction = false)
: bits_(uintptr_t(name) | (closedOver ? ClosedOverFlag : 0x0) |
(isTopLevelFunction ? TopLevelFunctionFlag : 0x0)) {}
private:
// For fromXDR.
BindingName(JSAtom* name, uint8_t flags) : bits_(uintptr_t(name) | flags) {
static_assert(FlagMask < alignof(JSAtom),
"Flags should fit into unused bits of JSAtom pointer");
MOZ_ASSERT((flags & FlagMask) == flags);
}
public:
static BindingName fromXDR(JSAtom* name, uint8_t flags) {
return BindingName(name, flags);
}
uint8_t flagsForXDR() const { return static_cast<uint8_t>(bits_ & FlagMask); }
JSAtom* name() const { return reinterpret_cast<JSAtom*>(bits_ & ~FlagMask); }
bool closedOver() const { return bits_ & ClosedOverFlag; }
private:
friend class BindingIter;
// This method should be called only for binding names in `vars` range in
// BindingIter.
bool isTopLevelFunction() const { return bits_ & TopLevelFunctionFlag; }
public:
void trace(JSTracer* trc);
};
/**
* The various {Global,Module,...}Scope::Data classes consist of always-present
* bits, then a trailing array of BindingNames. The various Data classes all
* end in a TrailingNamesArray that contains sized/aligned space for *one*
* BindingName. Data instances that contain N BindingNames, are then allocated
* in sizeof(Data) + (space for (N - 1) BindingNames). Because this class's
* |data_| field is properly sized/aligned, the N-BindingName array can start
* at |data_|.
*
* This is concededly a very low-level representation, but we want to only
* allocate once for data+bindings both, and this does so approximately as
* elegantly as C++ allows.
*/
class TrailingNamesArray {
private:
alignas(BindingName) unsigned char data_[sizeof(BindingName)];
private:
// Some versions of GCC treat it as a -Wstrict-aliasing violation (ergo a
// -Werror compile error) to reinterpret_cast<> |data_| to |T*|, even
// through |void*|. Placing the latter cast in these separate functions
// breaks the chain such that affected GCC versions no longer warn/error.
void* ptr() { return data_; }
public:
// Explicitly ensure no one accidentally allocates scope data without
// poisoning its trailing names.
TrailingNamesArray() = delete;
explicit TrailingNamesArray(size_t nameCount) {
if (nameCount) {
AlwaysPoison(&data_, 0xCC, sizeof(BindingName) * nameCount,
MemCheckKind::MakeUndefined);
}
}
BindingName* start() { return reinterpret_cast<BindingName*>(ptr()); }
BindingName& get(size_t i) { return start()[i]; }
BindingName& operator[](size_t i) { return get(i); }
};
class BindingLocation {
public:
enum class Kind {
Global,
Argument,
Frame,
Environment,
Import,
NamedLambdaCallee
};
private:
Kind kind_;
uint32_t slot_;
BindingLocation(Kind kind, uint32_t slot) : kind_(kind), slot_(slot) {}
public:
static BindingLocation Global() {
return BindingLocation(Kind::Global, UINT32_MAX);
}
static BindingLocation Argument(uint16_t slot) {
return BindingLocation(Kind::Argument, slot);
}
static BindingLocation Frame(uint32_t slot) {
MOZ_ASSERT(slot < LOCALNO_LIMIT);
return BindingLocation(Kind::Frame, slot);
}
static BindingLocation Environment(uint32_t slot) {
MOZ_ASSERT(slot < ENVCOORD_SLOT_LIMIT);
return BindingLocation(Kind::Environment, slot);
}
static BindingLocation Import() {
return BindingLocation(Kind::Import, UINT32_MAX);
}
static BindingLocation NamedLambdaCallee() {
return BindingLocation(Kind::NamedLambdaCallee, UINT32_MAX);
}
bool operator==(const BindingLocation& other) const {
return kind_ == other.kind_ && slot_ == other.slot_;
}
bool operator!=(const BindingLocation& other) const {
return !operator==(other);
}
Kind kind() const { return kind_; }
uint32_t slot() const {
MOZ_ASSERT(kind_ == Kind::Frame || kind_ == Kind::Environment);
return slot_;
}
uint16_t argumentSlot() const {
MOZ_ASSERT(kind_ == Kind::Argument);
return mozilla::AssertedCast<uint16_t>(slot_);
}
};
//
// Allow using is<T> and as<T> on Rooted<Scope*> and Handle<Scope*>.
//
template <typename Wrapper>
class WrappedPtrOperations<Scope*, Wrapper> {
public:
template <class U>
JS::Handle<U*> as() const {
const Wrapper& self = *static_cast<const Wrapper*>(this);
MOZ_ASSERT_IF(self, self->template is<U>());
return Handle<U*>::fromMarkedLocation(
reinterpret_cast<U* const*>(self.address()));
}
};
//
// The base class of all Scopes.
//
class Scope : public js::gc::TenuredCell {
friend class GCMarker;
// The enclosing scope or nullptr.
GCPtrScope enclosing_;
// The kind determines data_.
ScopeKind kind_;
// If there are any aliased bindings, the shape for the
// EnvironmentObject. Otherwise nullptr.
GCPtrShape environmentShape_;
protected:
BaseScopeData* data_;
Scope(ScopeKind kind, Scope* enclosing, Shape* environmentShape)
: enclosing_(enclosing),
kind_(kind),
environmentShape_(environmentShape),
data_(nullptr) {}
static Scope* create(JSContext* cx, ScopeKind kind, HandleScope enclosing,
HandleShape envShape);
template <typename ConcreteScope>
static ConcreteScope* create(
JSContext* cx, ScopeKind kind, HandleScope enclosing,
HandleShape envShape,
MutableHandle<UniquePtr<typename ConcreteScope::Data>> data);
template <typename ConcreteScope, XDRMode mode>
static XDRResult XDRSizedBindingNames(
XDRState<mode>* xdr, Handle<ConcreteScope*> scope,
MutableHandle<typename ConcreteScope::Data*> data);
Shape* maybeCloneEnvironmentShape(JSContext* cx);
template <typename ConcreteScope>
void initData(MutableHandle<UniquePtr<typename ConcreteScope::Data>> data);
public:
static const JS::TraceKind TraceKind = JS::TraceKind::Scope;
template <typename T>
bool is() const {
return kind_ == T::classScopeKind_;
}
template <typename T>
T& as() {
MOZ_ASSERT(this->is<T>());
return *static_cast<T*>(this);
}
template <typename T>
const T& as() const {
MOZ_ASSERT(this->is<T>());
return *static_cast<const T*>(this);
}
ScopeKind kind() const { return kind_; }
Scope* enclosing() const { return enclosing_; }
Shape* environmentShape() const { return environmentShape_; }
bool hasEnvironment() const {
switch (kind()) {
case ScopeKind::With:
case ScopeKind::Global:
case ScopeKind::NonSyntactic:
return true;
default:
// If there's a shape, an environment must be created for this scope.
return environmentShape_ != nullptr;
}
}
uint32_t chainLength() const;
uint32_t environmentChainLength() const;
template <typename T>
bool hasOnChain() const {
for (const Scope* it = this; it; it = it->enclosing()) {
if (it->is<T>()) {
return true;
}
}
return false;
}
bool hasOnChain(ScopeKind kind) const {
for (const Scope* it = this; it; it = it->enclosing()) {
if (it->kind() == kind) {
return true;
}
}
return false;
}
static Scope* clone(JSContext* cx, HandleScope scope, HandleScope enclosing);
void traceChildren(JSTracer* trc);
void finalize(FreeOp* fop);
size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
void dump();
};
/** Empty base class for scope Data classes to inherit from. */
class BaseScopeData {};
template <class Data>
inline size_t SizeOfData(uint32_t numBindings) {
static_assert(mozilla::IsBaseOf<BaseScopeData, Data>::value,
"Data must be the correct sort of data, i.e. it must "
"inherit from BaseScopeData");
return sizeof(Data) +
(numBindings ? numBindings - 1 : 0) * sizeof(BindingName);
}
//
// A lexical scope that holds let and const bindings. There are 4 kinds of
// LexicalScopes.
//
// Lexical
// A plain lexical scope.
//
// SimpleCatch
// Holds the single catch parameter of a catch block.
//
// Catch
// Holds the catch parameters (and only the catch parameters) of a catch
// block.
//
// NamedLambda
// StrictNamedLambda
// Holds the single name of the callee for a named lambda expression.
//
// All kinds of LexicalScopes correspond to LexicalEnvironmentObjects on the
// environment chain.
//
class LexicalScope : public Scope {
friend class Scope;
friend class BindingIter;
friend class GCMarker;
public:
// Data is public because it is created by the frontend. See
// Parser<FullParseHandler>::newLexicalScopeData.
struct Data : public BaseScopeData {
// Bindings are sorted by kind in both frames and environments.
//
// lets - [0, constStart)
// consts - [constStart, length)
uint32_t constStart = 0;
uint32_t length = 0;
// Frame slots [0, nextFrameSlot) are live when this is the innermost
// scope.
uint32_t nextFrameSlot = 0;
// The array of tagged JSAtom* names, allocated beyond the end of the
// struct.
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
};
static LexicalScope* create(JSContext* cx, ScopeKind kind, Handle<Data*> data,
uint32_t firstFrameSlot, HandleScope enclosing);
template <XDRMode mode>
static XDRResult XDR(XDRState<mode>* xdr, ScopeKind kind,
HandleScope enclosing, MutableHandleScope scope);
private:
static LexicalScope* createWithData(JSContext* cx, ScopeKind kind,
MutableHandle<UniquePtr<Data>> data,
uint32_t firstFrameSlot,
HandleScope enclosing);
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
static uint32_t nextFrameSlot(Scope* start);
public:
uint32_t firstFrameSlot() const;
uint32_t nextFrameSlot() const { return data().nextFrameSlot; }
// Returns an empty shape for extensible global and non-syntactic lexical
// scopes.
static Shape* getEmptyExtensibleEnvironmentShape(JSContext* cx);
};
template <>
inline bool Scope::is<LexicalScope>() const {
return kind_ == ScopeKind::Lexical || kind_ == ScopeKind::SimpleCatch ||
kind_ == ScopeKind::Catch || kind_ == ScopeKind::NamedLambda ||
kind_ == ScopeKind::StrictNamedLambda;
}
//
// Scope corresponding to a function. Holds formal parameter names, special
// internal names (see FunctionScope::isSpecialName), and, if the function
// parameters contain no expressions that might possibly be evaluated, the
// function's var bindings. For example, in these functions, the FunctionScope
// will store a/b/c bindings but not d/e/f bindings:
//
// function f1(a, b) {
// var c;
// let e;
// const f = 3;
// }
// function f2([a], b = 4, ...c) {
// var d, e, f; // stored in VarScope
// }
//
// Corresponds to CallObject on environment chain.
//
class FunctionScope : public Scope {
friend class GCMarker;
friend class BindingIter;
friend class PositionalFormalParameterIter;
friend class Scope;
static const ScopeKind classScopeKind_ = ScopeKind::Function;
public:
// Data is public because it is created by the
// frontend. See Parser<FullParseHandler>::newFunctionScopeData.
struct Data : public BaseScopeData {
// The canonical function of the scope, as during a scope walk we
// often query properties of the JSFunction (e.g., is the function an
// arrow).
GCPtrFunction canonicalFunction = {};
// If parameter expressions are present, parameters act like lexical
// bindings.
bool hasParameterExprs = false;
// Bindings are sorted by kind in both frames and environments.
//
// Positional formal parameter names are those that are not
// destructured. They may be referred to by argument slots if
// !script()->hasParameterExprs().
//
// An argument slot that needs to be skipped due to being destructured
// or having defaults will have a nullptr name in the name array to
// advance the argument slot.
//
// Rest parameter binding is also included in positional formals.
// This also becomes nullptr if destructuring.
//
// The number of positional formals is equal to function.length if
// there's no rest, function.length+1 otherwise.
//
// Destructuring parameters and destructuring rest are included in
// "other formals" below.
//
// "vars" contains the following:
// * function's top level vars if !script()->hasParameterExprs()
// * special internal names (arguments, .this, .generator) if
// they're used.
//
// positional formals - [0, nonPositionalFormalStart)
// other formals - [nonPositionalParamStart, varStart)
// vars - [varStart, length)
uint16_t nonPositionalFormalStart = 0;
uint16_t varStart = 0;
uint32_t length = 0;
// Frame slots [0, nextFrameSlot) are live when this is the innermost
// scope.
uint32_t nextFrameSlot = 0;
// The array of tagged JSAtom* names, allocated beyond the end of the
// struct.
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
Zone* zone() const;
};
static FunctionScope* create(JSContext* cx, Handle<Data*> data,
bool hasParameterExprs, bool needsEnvironment,
HandleFunction fun, HandleScope enclosing);
static FunctionScope* clone(JSContext* cx, Handle<FunctionScope*> scope,
HandleFunction fun, HandleScope enclosing);
template <XDRMode mode>
static XDRResult XDR(XDRState<mode>* xdr, HandleFunction fun,
HandleScope enclosing, MutableHandleScope scope);
private:
static FunctionScope* createWithData(JSContext* cx,
MutableHandle<UniquePtr<Data>> data,
bool hasParameterExprs,
bool needsEnvironment,
HandleFunction fun,
HandleScope enclosing);
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
public:
uint32_t nextFrameSlot() const { return data().nextFrameSlot; }
JSFunction* canonicalFunction() const { return data().canonicalFunction; }
JSScript* script() const;
bool hasParameterExprs() const { return data().hasParameterExprs; }
uint32_t numPositionalFormalParameters() const {
return data().nonPositionalFormalStart;
}
static bool isSpecialName(JSContext* cx, JSAtom* name);
static Shape* getEmptyEnvironmentShape(JSContext* cx, bool hasParameterExprs);
};
//
// Scope holding only vars. There are 2 kinds of VarScopes.
//
// FunctionBodyVar
// Corresponds to the extra var scope present in functions with parameter
// expressions. See examples in comment above FunctionScope.
//
// ParameterExpressionVar
// Each parameter expression is evaluated in its own var environment. For
// example, f() below will print 'fml', then 'global'. That's right.
//
// var a = 'global';
// function f(x = (eval(`var a = 'fml'`), a), y = a) {
// print(x);
// print(y);
// };
//
// Corresponds to VarEnvironmentObject on environment chain.
//
class VarScope : public Scope {
friend class GCMarker;
friend class BindingIter;
friend class Scope;
public:
// Data is public because it is created by the
// frontend. See Parser<FullParseHandler>::newVarScopeData.
struct Data : public BaseScopeData {
// All bindings are vars.
uint32_t length = 0;
// Frame slots [firstFrameSlot(), nextFrameSlot) are live when this is
// the innermost scope.
uint32_t nextFrameSlot = 0;
// The array of tagged JSAtom* names, allocated beyond the end of the
// struct.
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
};
static VarScope* create(JSContext* cx, ScopeKind kind, Handle<Data*> data,
uint32_t firstFrameSlot, bool needsEnvironment,
HandleScope enclosing);
template <XDRMode mode>
static XDRResult XDR(XDRState<mode>* xdr, ScopeKind kind,
HandleScope enclosing, MutableHandleScope scope);
private:
static VarScope* createWithData(JSContext* cx, ScopeKind kind,
MutableHandle<UniquePtr<Data>> data,
uint32_t firstFrameSlot,
bool needsEnvironment, HandleScope enclosing);
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
public:
uint32_t firstFrameSlot() const;
uint32_t nextFrameSlot() const { return data().nextFrameSlot; }
static Shape* getEmptyEnvironmentShape(JSContext* cx);
};
template <>
inline bool Scope::is<VarScope>() const {
return kind_ == ScopeKind::FunctionBodyVar ||
kind_ == ScopeKind::ParameterExpressionVar;
}
//
// Scope corresponding to both the global object scope and the global lexical
// scope.
//
// Both are extensible and are singletons across <script> tags, so these
// scopes are a fragment of the names in global scope. In other words, two
// global scripts may have two different GlobalScopes despite having the same
// GlobalObject.
//
// There are 2 kinds of GlobalScopes.
//
// Global
// Corresponds to a GlobalObject and its global LexicalEnvironmentObject on
// the environment chain.
//
// NonSyntactic
// Corresponds to a non-GlobalObject created by the embedding on the
// environment chain. This distinction is important for optimizations.
//
class GlobalScope : public Scope {
friend class Scope;
friend class BindingIter;
friend class GCMarker;
public:
// Data is public because it is created by the frontend. See
// Parser<FullParseHandler>::newGlobalScopeData.
struct Data : BaseScopeData {
// Bindings are sorted by kind.
// `vars` includes top-level functions which is distinguished by a bit
// on the BindingName.
//
// vars - [0, letStart)
// lets - [letStart, constStart)
// consts - [constStart, length)
uint32_t letStart = 0;
uint32_t constStart = 0;
uint32_t length = 0;
// The array of tagged JSAtom* names, allocated beyond the end of the
// struct.
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
};
static GlobalScope* create(JSContext* cx, ScopeKind kind, Handle<Data*> data);
static GlobalScope* createEmpty(JSContext* cx, ScopeKind kind) {
return create(cx, kind, nullptr);
}
static GlobalScope* clone(JSContext* cx, Handle<GlobalScope*> scope,
ScopeKind kind);
template <XDRMode mode>
static XDRResult XDR(XDRState<mode>* xdr, ScopeKind kind,
MutableHandleScope scope);
private:
static GlobalScope* createWithData(JSContext* cx, ScopeKind kind,
MutableHandle<UniquePtr<Data>> data);
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
public:
bool isSyntactic() const { return kind() != ScopeKind::NonSyntactic; }
bool hasBindings() const { return data().length > 0; }
};
template <>
inline bool Scope::is<GlobalScope>() const {
return kind_ == ScopeKind::Global || kind_ == ScopeKind::NonSyntactic;
}
//
// Scope of a 'with' statement. Has no bindings.
//
// Corresponds to a WithEnvironmentObject on the environment chain.
class WithScope : public Scope {
friend class Scope;
static const ScopeKind classScopeKind_ = ScopeKind::With;
public:
static WithScope* create(JSContext* cx, HandleScope enclosing);
template <XDRMode mode>
static XDRResult XDR(XDRState<mode>* xdr, HandleScope enclosing,
MutableHandleScope scope);
};
//
// Scope of an eval. Holds var bindings. There are 2 kinds of EvalScopes.
//
// StrictEval
// A strict eval. Corresponds to a VarEnvironmentObject, where its var
// bindings lives.
//
// Eval
// A sloppy eval. This is an empty scope, used only in the frontend, to
// detect redeclaration errors. It has no Environment. Any `var`s declared
// in the eval code are bound on the nearest enclosing var environment.
//
class EvalScope : public Scope {
friend class Scope;
friend class BindingIter;
friend class GCMarker;
public:
// Data is public because it is created by the frontend. See
// Parser<FullParseHandler>::newEvalScopeData.
struct Data : public BaseScopeData {
// All bindings in an eval script are 'var' bindings. The implicit
// lexical scope around the eval is present regardless of strictness
// and is its own LexicalScope.
// `vars` includes top-level functions which is distinguished by a bit
// on the BindingName.
//
// vars - [0, length)
uint32_t varStart = 0;
uint32_t length = 0;
// Frame slots [0, nextFrameSlot) are live when this is the innermost
// scope.
uint32_t nextFrameSlot = 0;
// The array of tagged JSAtom* names, allocated beyond the end of the
// struct.
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
};
static EvalScope* create(JSContext* cx, ScopeKind kind, Handle<Data*> data,
HandleScope enclosing);
template <XDRMode mode>
static XDRResult XDR(XDRState<mode>* xdr, ScopeKind kind,
HandleScope enclosing, MutableHandleScope scope);
private:
static EvalScope* createWithData(JSContext* cx, ScopeKind kind,
MutableHandle<UniquePtr<Data>> data,
HandleScope enclosing);
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
public:
// Starting a scope, the nearest var scope that a direct eval can
// introduce vars on.
static Scope* nearestVarScopeForDirectEval(Scope* scope);
uint32_t nextFrameSlot() const { return data().nextFrameSlot; }
bool strict() const { return kind() == ScopeKind::StrictEval; }
bool hasBindings() const { return data().length > 0; }
bool isNonGlobal() const {
if (strict()) {
return true;
}
return !nearestVarScopeForDirectEval(enclosing())->is<GlobalScope>();
}
static Shape* getEmptyEnvironmentShape(JSContext* cx);
};
template <>
inline bool Scope::is<EvalScope>() const {
return kind_ == ScopeKind::Eval || kind_ == ScopeKind::StrictEval;
}
//
// Scope corresponding to the toplevel script in an ES module.
//
// Like GlobalScopes, these scopes contain both vars and lexical bindings, as
// the treating of imports and exports requires putting them in one scope.
//
// Corresponds to a ModuleEnvironmentObject on the environment chain.
//
class ModuleScope : public Scope {
friend class GCMarker;
friend class BindingIter;
friend class Scope;
static const ScopeKind classScopeKind_ = ScopeKind::Module;
public:
// Data is public because it is created by the frontend. See
// Parser<FullParseHandler>::newModuleScopeData.
struct Data : BaseScopeData {
// The module of the scope.
GCPtr<ModuleObject*> module = {};
// Bindings are sorted by kind.
//
// imports - [0, varStart)
// vars - [varStart, letStart)
// lets - [letStart, constStart)
// consts - [constStart, length)
uint32_t varStart = 0;
uint32_t letStart = 0;
uint32_t constStart = 0;
uint32_t length = 0;
// Frame slots [0, nextFrameSlot) are live when this is the innermost
// scope.
uint32_t nextFrameSlot = 0;
// The array of tagged JSAtom* names, allocated beyond the end of the
// struct.
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
Zone* zone() const;
};
static ModuleScope* create(JSContext* cx, Handle<Data*> data,
Handle<ModuleObject*> module,
HandleScope enclosing);
private:
static ModuleScope* createWithData(JSContext* cx,
MutableHandle<UniquePtr<Data>> data,
Handle<ModuleObject*> module,
HandleScope enclosing);
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
public:
uint32_t nextFrameSlot() const { return data().nextFrameSlot; }
ModuleObject* module() const { return data().module; }
static Shape* getEmptyEnvironmentShape(JSContext* cx);
};
class WasmInstanceScope : public Scope {
friend class BindingIter;
friend class Scope;
friend class GCMarker;
static const ScopeKind classScopeKind_ = ScopeKind::WasmInstance;
public:
struct Data : public BaseScopeData {
uint32_t memoriesStart = 0;
uint32_t globalsStart = 0;
uint32_t length = 0;
uint32_t nextFrameSlot = 0;
// The wasm instance of the scope.
GCPtr<WasmInstanceObject*> instance = {};
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;
void trace(JSTracer* trc);
};
static WasmInstanceScope* create(JSContext* cx, WasmInstanceObject* instance);
private:
Data& data() { return *static_cast<Data*>(data_); }
const Data& data() const { return *static_cast<Data*>(data_); }
public:
WasmInstanceObject* instance() const { return data().instance; }
uint32_t memoriesStart() const { return data().memoriesStart; }
uint32_t globalsStart() const { return data().globalsStart; }
uint32_t namesCount() const { return data().length; }
static Shape* getEmptyEnvironmentShape(JSContext* cx);
};
// Scope corresponding to the wasm function. A WasmFunctionScope is used by
// Debugger only, and not for wasm execution.
//
class WasmFunctionScope : public Scope {
friend class BindingIter;
friend class Scope;
friend class GCMarker;
static const ScopeKind classScopeKind_ = ScopeKind::WasmFunction;
public:
struct Data : public BaseScopeData {
uint32_t length = 0;
uint32_t nextFrameSlot = 0;
uint32_t funcIndex = 0;
TrailingNamesArray trailingNames;
explicit Data(size_t nameCount) : trailingNames(nameCount) {}
Data() = delete;