-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEnvironmentObject.cpp
4066 lines (3500 loc) · 132 KB
/
EnvironmentObject.cpp
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/. */
#include "vm/EnvironmentObject-inl.h"
#include "builtin/ModuleObject.h"
#include "gc/Policy.h"
#include "vm/ArgumentsObject.h"
#include "vm/AsyncFunction.h"
#include "vm/GlobalObject.h"
#include "vm/Iteration.h"
#include "vm/JSObject.h"
#include "vm/ProxyObject.h"
#include "vm/Realm.h"
#include "vm/Shape.h"
#include "vm/Xdr.h"
#include "wasm/WasmInstance.h"
#ifdef DEBUG
# include "vm/BytecodeIterator.h"
# include "vm/BytecodeLocation.h"
#endif
#include "gc/Marking-inl.h"
#include "vm/JSAtom-inl.h"
#include "vm/JSScript-inl.h"
#include "vm/NativeObject-inl.h"
#include "vm/ObjectGroup-inl.h" // JSObject::setSingleton
#include "vm/Stack-inl.h"
#include "vm/TypeInference-inl.h"
#ifdef DEBUG
# include "vm/BytecodeIterator-inl.h"
# include "vm/BytecodeLocation-inl.h"
#endif
using namespace js;
using RootedArgumentsObject = Rooted<ArgumentsObject*>;
using MutableHandleArgumentsObject = MutableHandle<ArgumentsObject*>;
/*****************************************************************************/
Shape* js::EnvironmentCoordinateToEnvironmentShape(JSScript* script,
jsbytecode* pc) {
MOZ_ASSERT(JOF_OPTYPE(JSOp(*pc)) == JOF_ENVCOORD);
ScopeIter si(script->innermostScope(pc));
uint32_t hops = EnvironmentCoordinate(pc).hops();
while (true) {
MOZ_ASSERT(!si.done());
if (si.hasSyntacticEnvironment()) {
if (!hops) {
break;
}
hops--;
}
si++;
}
return si.environmentShape();
}
PropertyName* js::EnvironmentCoordinateNameSlow(JSScript* script,
jsbytecode* pc) {
Shape* shape = EnvironmentCoordinateToEnvironmentShape(script, pc);
EnvironmentCoordinate ec(pc);
Shape::Range<NoGC> r(shape);
while (r.front().slot() != ec.slot()) {
r.popFront();
}
jsid id = r.front().propidRaw();
/* Beware nameless destructuring formal. */
if (!JSID_IS_ATOM(id)) {
return script->runtimeFromAnyThread()->commonNames->empty;
}
return JSID_TO_ATOM(id)->asPropertyName();
}
/*****************************************************************************/
template <typename T>
static T* CreateEnvironmentObject(JSContext* cx, HandleShape shape,
HandleObjectGroup group, gc::InitialHeap heap,
IsSingletonEnv isSingleton) {
static_assert(std::is_base_of_v<EnvironmentObject, T>,
"T must be an EnvironmentObject");
// All environment objects can be background-finalized.
gc::AllocKind allocKind = gc::GetGCObjectKind(shape->numFixedSlots());
MOZ_ASSERT(CanChangeToBackgroundAllocKind(allocKind, &T::class_));
allocKind = gc::ForegroundToBackgroundAllocKind(allocKind);
JSObject* obj;
JS_TRY_VAR_OR_RETURN_NULL(
cx, obj, NativeObject::create(cx, allocKind, heap, shape, group));
if (isSingleton == IsSingletonEnv::Yes) {
RootedObject objRoot(cx, obj);
if (!JSObject::setSingleton(cx, objRoot)) {
return nullptr;
}
obj = objRoot;
} else {
// Don't track property types for non-singleton environment objects. The
// JITs don't use them anyway.
MarkObjectGroupUnknownProperties(cx, group);
}
return &obj->as<T>();
}
// As above, but the caller does not have to pass the group.
template <typename T>
static T* CreateEnvironmentObject(JSContext* cx, HandleShape shape,
gc::InitialHeap heap,
IsSingletonEnv isSingleton) {
RootedObjectGroup group(
cx, ObjectGroup::defaultNewGroup(cx, &T::class_, TaggedProto(nullptr)));
if (!group) {
return nullptr;
}
return CreateEnvironmentObject<T>(cx, shape, group, heap, isSingleton);
}
// Helper function for simple environment objects that don't need the overloads
// above.
template <typename T>
static T* CreateEnvironmentObject(JSContext* cx, HandleShape shape,
NewObjectKind newKind = GenericObject) {
RootedObjectGroup group(
cx, ObjectGroup::defaultNewGroup(cx, &T::class_, TaggedProto(nullptr)));
if (!group) {
return nullptr;
}
gc::InitialHeap heap = GetInitialHeap(newKind, group);
return CreateEnvironmentObject<T>(cx, shape, group, heap, IsSingletonEnv::No);
}
CallObject* CallObject::create(JSContext* cx, HandleShape shape,
HandleObjectGroup group) {
MOZ_ASSERT(!group->singleton());
gc::InitialHeap heap = GetInitialHeap(GenericObject, group);
return CreateEnvironmentObject<CallObject>(cx, shape, group, heap,
IsSingletonEnv::No);
}
/*
* Create a CallObject for a JSScript that is not initialized to any particular
* callsite. This object can either be initialized (with an enclosing scope and
* callee) or used as a template for jit compilation.
*/
CallObject* CallObject::createTemplateObject(JSContext* cx, HandleScript script,
HandleObject enclosing,
gc::InitialHeap heap) {
Rooted<FunctionScope*> scope(cx, &script->bodyScope()->as<FunctionScope>());
RootedShape shape(cx, scope->environmentShape());
MOZ_ASSERT(shape->getObjectClass() == &class_);
// The JITs assume the result is nursery allocated unless we collected the
// nursery, so don't change |heap| here.
auto* callObj =
CreateEnvironmentObject<CallObject>(cx, shape, heap, IsSingletonEnv::No);
if (!callObj) {
return nullptr;
}
callObj->initEnclosingEnvironment(enclosing);
if (scope->hasParameterExprs()) {
// If there are parameter expressions, all parameters are lexical and
// have TDZ.
for (BindingIter bi(script->bodyScope()); bi; bi++) {
BindingLocation loc = bi.location();
if (loc.kind() == BindingLocation::Kind::Environment &&
BindingKindIsLexical(bi.kind())) {
callObj->initSlot(loc.slot(), MagicValue(JS_UNINITIALIZED_LEXICAL));
}
}
}
return callObj;
}
/*
* Construct a call object for the given bindings. If this is a call object
* for a function invocation, callee should be the function being called.
* Otherwise it must be a call object for eval of strict mode code, and callee
* must be null.
*/
CallObject* CallObject::create(JSContext* cx, HandleFunction callee,
HandleObject enclosing) {
RootedScript script(cx, callee->nonLazyScript());
gc::InitialHeap heap = gc::DefaultHeap;
CallObject* callobj =
CallObject::createTemplateObject(cx, script, enclosing, heap);
if (!callobj) {
return nullptr;
}
callobj->initFixedSlot(CALLEE_SLOT, ObjectValue(*callee));
return callobj;
}
CallObject* CallObject::create(JSContext* cx, AbstractFramePtr frame) {
MOZ_ASSERT(frame.isFunctionFrame());
cx->check(frame);
RootedObject envChain(cx, frame.environmentChain());
RootedFunction callee(cx, frame.callee());
CallObject* callobj = create(cx, callee, envChain);
if (!callobj) {
return nullptr;
}
if (!frame.script()->bodyScope()->as<FunctionScope>().hasParameterExprs()) {
// If there are no defaults, copy the aliased arguments into the call
// object manually. If there are defaults, bytecode is generated to do
// the copying.
for (PositionalFormalParameterIter fi(frame.script()); fi; fi++) {
if (!fi.closedOver()) {
continue;
}
callobj->setAliasedBinding(
cx, fi,
frame.unaliasedFormal(fi.argumentSlot(), DONT_CHECK_ALIASING));
}
}
return callobj;
}
CallObject* CallObject::createHollowForDebug(JSContext* cx,
HandleFunction callee) {
MOZ_ASSERT(!callee->needsCallObject());
RootedScript script(cx, callee->nonLazyScript());
Rooted<FunctionScope*> scope(cx, &script->bodyScope()->as<FunctionScope>());
RootedShape shape(cx, FunctionScope::getEmptyEnvironmentShape(cx));
if (!shape) {
return nullptr;
}
RootedObjectGroup group(
cx, ObjectGroup::defaultNewGroup(cx, &class_, TaggedProto(nullptr)));
if (!group) {
return nullptr;
}
Rooted<CallObject*> callobj(cx, create(cx, shape, group));
if (!callobj) {
return nullptr;
}
// This environment's enclosing link is never used: the
// DebugEnvironmentProxy that refers to this scope carries its own
// enclosing link, which is what Debugger uses to construct the tree of
// Debugger.Environment objects.
callobj->initEnclosingEnvironment(&cx->global()->lexicalEnvironment());
callobj->initFixedSlot(CALLEE_SLOT, ObjectValue(*callee));
RootedValue optimizedOut(cx, MagicValue(JS_OPTIMIZED_OUT));
RootedId id(cx);
for (Rooted<BindingIter> bi(cx, BindingIter(script)); bi; bi++) {
id = NameToId(bi.name()->asPropertyName());
if (!SetProperty(cx, callobj, id, optimizedOut)) {
return nullptr;
}
}
return callobj;
}
const JSClass CallObject::class_ = {
"Call", JSCLASS_HAS_RESERVED_SLOTS(CallObject::RESERVED_SLOTS)};
/*****************************************************************************/
/* static */
VarEnvironmentObject* VarEnvironmentObject::create(JSContext* cx,
HandleShape shape,
HandleObject enclosing,
gc::InitialHeap heap) {
MOZ_ASSERT(shape->getObjectClass() == &class_);
auto* env = CreateEnvironmentObject<VarEnvironmentObject>(cx, shape);
if (!env) {
return nullptr;
}
MOZ_ASSERT(!env->inDictionaryMode());
env->initEnclosingEnvironment(enclosing);
return env;
}
/* static */
VarEnvironmentObject* VarEnvironmentObject::create(JSContext* cx,
HandleScope scope,
AbstractFramePtr frame) {
#ifdef DEBUG
if (frame.isEvalFrame()) {
MOZ_ASSERT(scope->is<EvalScope>() && scope == frame.script()->bodyScope());
MOZ_ASSERT_IF(frame.isInterpreterFrame(),
cx->interpreterFrame() == frame.asInterpreterFrame());
MOZ_ASSERT_IF(frame.isInterpreterFrame(),
cx->interpreterRegs().pc == frame.script()->code());
} else {
MOZ_ASSERT(frame.environmentChain());
MOZ_ASSERT_IF(
frame.callee()->needsCallObject(),
&frame.environmentChain()->as<CallObject>().callee() == frame.callee());
}
#endif
RootedScript script(cx, frame.script());
RootedObject envChain(cx, frame.environmentChain());
RootedShape shape(cx, scope->environmentShape());
VarEnvironmentObject* env = create(cx, shape, envChain, gc::DefaultHeap);
if (!env) {
return nullptr;
}
env->initScope(scope);
return env;
}
/* static */
VarEnvironmentObject* VarEnvironmentObject::createHollowForDebug(
JSContext* cx, Handle<VarScope*> scope) {
MOZ_ASSERT(!scope->hasEnvironment());
RootedShape shape(cx, VarScope::getEmptyEnvironmentShape(cx));
if (!shape) {
return nullptr;
}
// This environment's enclosing link is never used: the
// DebugEnvironmentProxy that refers to this scope carries its own
// enclosing link, which is what Debugger uses to construct the tree of
// Debugger.Environment objects.
RootedObject enclosingEnv(cx, &cx->global()->lexicalEnvironment());
Rooted<VarEnvironmentObject*> env(
cx, create(cx, shape, enclosingEnv, gc::TenuredHeap));
if (!env) {
return nullptr;
}
RootedValue optimizedOut(cx, MagicValue(JS_OPTIMIZED_OUT));
RootedId id(cx);
for (Rooted<BindingIter> bi(cx, BindingIter(scope)); bi; bi++) {
id = NameToId(bi.name()->asPropertyName());
if (!SetProperty(cx, env, id, optimizedOut)) {
return nullptr;
}
}
env->initScope(scope);
return env;
}
const JSClass VarEnvironmentObject::class_ = {
"Var", JSCLASS_HAS_RESERVED_SLOTS(VarEnvironmentObject::RESERVED_SLOTS)};
/*****************************************************************************/
const ObjectOps ModuleEnvironmentObject::objectOps_ = {
ModuleEnvironmentObject::lookupProperty, // lookupProperty
nullptr, // defineProperty
ModuleEnvironmentObject::hasProperty, // hasProperty
ModuleEnvironmentObject::getProperty, // getProperty
ModuleEnvironmentObject::setProperty, // setProperty
ModuleEnvironmentObject::
getOwnPropertyDescriptor, // getOwnPropertyDescriptor
ModuleEnvironmentObject::deleteProperty, // deleteProperty
nullptr, // getElements
nullptr, // funToString
};
const JSClassOps ModuleEnvironmentObject::classOps_ = {
nullptr, // addProperty
nullptr, // delProperty
nullptr, // enumerate
ModuleEnvironmentObject::newEnumerate, // newEnumerate
nullptr, // resolve
nullptr, // mayResolve
nullptr, // finalize
nullptr, // call
nullptr, // hasInstance
nullptr, // construct
nullptr, // trace
};
const JSClass ModuleEnvironmentObject::class_ = {
"ModuleEnvironmentObject",
JSCLASS_HAS_RESERVED_SLOTS(ModuleEnvironmentObject::RESERVED_SLOTS),
&ModuleEnvironmentObject::classOps_,
JS_NULL_CLASS_SPEC,
JS_NULL_CLASS_EXT,
&ModuleEnvironmentObject::objectOps_};
/* static */
ModuleEnvironmentObject* ModuleEnvironmentObject::create(
JSContext* cx, HandleModuleObject module) {
RootedScript script(cx, module->script());
RootedShape shape(cx,
script->bodyScope()->as<ModuleScope>().environmentShape());
MOZ_ASSERT(shape->getObjectClass() == &class_);
RootedModuleEnvironmentObject env(
cx, CreateEnvironmentObject<ModuleEnvironmentObject>(cx, shape,
TenuredObject));
if (!env) {
return nullptr;
}
env->initReservedSlot(MODULE_SLOT, ObjectValue(*module));
// Initialize this early so that we can manipulate the env object without
// causing assertions.
env->initEnclosingEnvironment(&cx->global()->lexicalEnvironment());
// Initialize all lexical bindings and imports as uninitialized. Imports
// get uninitialized because they have a special TDZ for cyclic imports.
for (BindingIter bi(script); bi; bi++) {
BindingLocation loc = bi.location();
if (loc.kind() == BindingLocation::Kind::Environment &&
BindingKindIsLexical(bi.kind())) {
env->initSlot(loc.slot(), MagicValue(JS_UNINITIALIZED_LEXICAL));
}
}
// It is not be possible to add or remove bindings from a module environment
// after this point as module code is always strict.
#ifdef DEBUG
for (Shape::Range<NoGC> r(env->lastProperty()); !r.empty(); r.popFront()) {
MOZ_ASSERT(!r.front().configurable());
}
MOZ_ASSERT(env->lastProperty()->getObjectFlags() & BaseShape::NOT_EXTENSIBLE);
MOZ_ASSERT(!env->inDictionaryMode());
#endif
return env;
}
ModuleObject& ModuleEnvironmentObject::module() const {
return getReservedSlot(MODULE_SLOT).toObject().as<ModuleObject>();
}
IndirectBindingMap& ModuleEnvironmentObject::importBindings() const {
return module().importBindings();
}
bool ModuleEnvironmentObject::createImportBinding(JSContext* cx,
HandleAtom importName,
HandleModuleObject module,
HandleAtom localName) {
RootedId importNameId(cx, AtomToId(importName));
RootedId localNameId(cx, AtomToId(localName));
RootedModuleEnvironmentObject env(cx, &module->initialEnvironment());
if (!importBindings().put(cx, importNameId, env, localNameId)) {
return false;
}
return true;
}
bool ModuleEnvironmentObject::hasImportBinding(HandlePropertyName name) {
return importBindings().has(NameToId(name));
}
bool ModuleEnvironmentObject::lookupImport(jsid name,
ModuleEnvironmentObject** envOut,
Shape** shapeOut) {
return importBindings().lookup(name, envOut, shapeOut);
}
void ModuleEnvironmentObject::fixEnclosingEnvironmentAfterRealmMerge(
GlobalObject& global) {
setEnclosingEnvironment(&global.lexicalEnvironment());
}
/* static */
bool ModuleEnvironmentObject::lookupProperty(
JSContext* cx, HandleObject obj, HandleId id, MutableHandleObject objp,
MutableHandle<PropertyResult> propp) {
const IndirectBindingMap& bindings =
obj->as<ModuleEnvironmentObject>().importBindings();
Shape* shape;
ModuleEnvironmentObject* env;
if (bindings.lookup(id, &env, &shape)) {
objp.set(env);
propp.setNativeProperty(shape);
return true;
}
RootedNativeObject target(cx, &obj->as<NativeObject>());
if (!NativeLookupOwnProperty<CanGC>(cx, target, id, propp)) {
return false;
}
objp.set(obj);
return true;
}
/* static */
bool ModuleEnvironmentObject::hasProperty(JSContext* cx, HandleObject obj,
HandleId id, bool* foundp) {
if (obj->as<ModuleEnvironmentObject>().importBindings().has(id)) {
*foundp = true;
return true;
}
RootedNativeObject self(cx, &obj->as<NativeObject>());
return NativeHasProperty(cx, self, id, foundp);
}
/* static */
bool ModuleEnvironmentObject::getProperty(JSContext* cx, HandleObject obj,
HandleValue receiver, HandleId id,
MutableHandleValue vp) {
const IndirectBindingMap& bindings =
obj->as<ModuleEnvironmentObject>().importBindings();
Shape* shape;
ModuleEnvironmentObject* env;
if (bindings.lookup(id, &env, &shape)) {
vp.set(env->getSlot(shape->slot()));
return true;
}
RootedNativeObject self(cx, &obj->as<NativeObject>());
return NativeGetProperty(cx, self, receiver, id, vp);
}
/* static */
bool ModuleEnvironmentObject::setProperty(JSContext* cx, HandleObject obj,
HandleId id, HandleValue v,
HandleValue receiver,
JS::ObjectOpResult& result) {
RootedModuleEnvironmentObject self(cx, &obj->as<ModuleEnvironmentObject>());
if (self->importBindings().has(id)) {
return result.failReadOnly();
}
return NativeSetProperty<Qualified>(cx, self, id, v, receiver, result);
}
/* static */
bool ModuleEnvironmentObject::getOwnPropertyDescriptor(
JSContext* cx, HandleObject obj, HandleId id,
MutableHandle<PropertyDescriptor> desc) {
const IndirectBindingMap& bindings =
obj->as<ModuleEnvironmentObject>().importBindings();
Shape* shape;
ModuleEnvironmentObject* env;
if (bindings.lookup(id, &env, &shape)) {
desc.setAttributes(JSPROP_ENUMERATE | JSPROP_PERMANENT);
desc.object().set(obj);
RootedValue value(cx, env->getSlot(shape->slot()));
desc.setValue(value);
desc.assertComplete();
return true;
}
RootedNativeObject self(cx, &obj->as<NativeObject>());
return NativeGetOwnPropertyDescriptor(cx, self, id, desc);
}
/* static */
bool ModuleEnvironmentObject::deleteProperty(JSContext* cx, HandleObject obj,
HandleId id,
ObjectOpResult& result) {
return result.failCantDelete();
}
/* static */
bool ModuleEnvironmentObject::newEnumerate(JSContext* cx, HandleObject obj,
MutableHandleIdVector properties,
bool enumerableOnly) {
RootedModuleEnvironmentObject self(cx, &obj->as<ModuleEnvironmentObject>());
const IndirectBindingMap& bs(self->importBindings());
MOZ_ASSERT(properties.length() == 0);
size_t count = bs.count() + self->slotSpan() - RESERVED_SLOTS;
if (!properties.reserve(count)) {
ReportOutOfMemory(cx);
return false;
}
bs.forEachExportedName([&](jsid name) { properties.infallibleAppend(name); });
for (Shape::Range<NoGC> r(self->lastProperty()); !r.empty(); r.popFront()) {
properties.infallibleAppend(r.front().propid());
}
MOZ_ASSERT(properties.length() == count);
return true;
}
/*****************************************************************************/
const JSClass WasmInstanceEnvironmentObject::class_ = {
"WasmInstance",
JSCLASS_HAS_RESERVED_SLOTS(WasmInstanceEnvironmentObject::RESERVED_SLOTS)};
/* static */
WasmInstanceEnvironmentObject*
WasmInstanceEnvironmentObject::createHollowForDebug(
JSContext* cx, Handle<WasmInstanceScope*> scope) {
RootedShape shape(cx, scope->getEmptyEnvironmentShape(cx));
if (!shape) {
return nullptr;
}
auto* env = CreateEnvironmentObject<WasmInstanceEnvironmentObject>(cx, shape);
if (!env) {
return nullptr;
}
env->initEnclosingEnvironment(&cx->global()->lexicalEnvironment());
env->initReservedSlot(SCOPE_SLOT, PrivateGCThingValue(scope));
return env;
}
/*****************************************************************************/
const JSClass WasmFunctionCallObject::class_ = {
"WasmCall",
JSCLASS_HAS_RESERVED_SLOTS(WasmFunctionCallObject::RESERVED_SLOTS)};
/* static */
WasmFunctionCallObject* WasmFunctionCallObject::createHollowForDebug(
JSContext* cx, HandleObject enclosing, Handle<WasmFunctionScope*> scope) {
RootedShape shape(cx, scope->getEmptyEnvironmentShape(cx));
if (!shape) {
return nullptr;
}
auto* callobj = CreateEnvironmentObject<WasmFunctionCallObject>(cx, shape);
if (!callobj) {
return nullptr;
}
callobj->initEnclosingEnvironment(enclosing);
callobj->initReservedSlot(SCOPE_SLOT, PrivateGCThingValue(scope));
return callobj;
}
/*****************************************************************************/
WithEnvironmentObject* WithEnvironmentObject::create(JSContext* cx,
HandleObject object,
HandleObject enclosing,
Handle<WithScope*> scope) {
RootedShape shape(cx, EmptyEnvironmentShape(cx, &class_, JSSLOT_FREE(&class_),
/* baseShapeFlags = */ 0));
if (!shape) {
return nullptr;
}
auto* obj = CreateEnvironmentObject<WithEnvironmentObject>(cx, shape);
if (!obj) {
return nullptr;
}
JSObject* thisObj = GetThisObject(object);
obj->initEnclosingEnvironment(enclosing);
obj->initReservedSlot(OBJECT_SLOT, ObjectValue(*object));
obj->initReservedSlot(THIS_SLOT, ObjectValue(*thisObj));
if (scope) {
obj->initReservedSlot(SCOPE_SLOT, PrivateGCThingValue(scope));
} else {
obj->initReservedSlot(SCOPE_SLOT, NullValue());
}
return obj;
}
WithEnvironmentObject* WithEnvironmentObject::createNonSyntactic(
JSContext* cx, HandleObject object, HandleObject enclosing) {
return create(cx, object, enclosing, nullptr);
}
static inline bool IsUnscopableDotName(JSContext* cx, HandleId id) {
return JSID_IS_ATOM(id, cx->names().dotThis);
}
#ifdef DEBUG
static bool IsInternalDotName(JSContext* cx, HandleId id) {
return JSID_IS_ATOM(id, cx->names().dotThis) ||
JSID_IS_ATOM(id, cx->names().dotGenerator) ||
JSID_IS_ATOM(id, cx->names().dotInitializers) ||
JSID_IS_ATOM(id, cx->names().dotFieldKeys) ||
JSID_IS_ATOM(id, cx->names().dotStaticInitializers) ||
JSID_IS_ATOM(id, cx->names().dotStaticFieldKeys);
}
#endif
/* Implements ES6 8.1.1.2.1 HasBinding steps 7-9. */
static bool CheckUnscopables(JSContext* cx, HandleObject obj, HandleId id,
bool* scopable) {
RootedId unscopablesId(
cx,
SYMBOL_TO_JSID(cx->wellKnownSymbols().get(JS::SymbolCode::unscopables)));
RootedValue v(cx);
if (!GetProperty(cx, obj, obj, unscopablesId, &v)) {
return false;
}
if (v.isObject()) {
RootedObject unscopablesObj(cx, &v.toObject());
if (!GetProperty(cx, unscopablesObj, unscopablesObj, id, &v)) {
return false;
}
*scopable = !ToBoolean(v);
} else {
*scopable = true;
}
return true;
}
static bool with_LookupProperty(JSContext* cx, HandleObject obj, HandleId id,
MutableHandleObject objp,
MutableHandle<PropertyResult> propp) {
// SpiderMonkey-specific: consider the internal '.this' name to be unscopable.
if (IsUnscopableDotName(cx, id)) {
objp.set(nullptr);
propp.setNotFound();
return true;
}
// Other internal dot-names shouldn't even end up in with-environments.
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
if (!LookupProperty(cx, actual, id, objp, propp)) {
return false;
}
if (propp) {
bool scopable;
if (!CheckUnscopables(cx, actual, id, &scopable)) {
return false;
}
if (!scopable) {
objp.set(nullptr);
propp.setNotFound();
}
}
return true;
}
static bool with_DefineProperty(JSContext* cx, HandleObject obj, HandleId id,
Handle<PropertyDescriptor> desc,
ObjectOpResult& result) {
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
return DefineProperty(cx, actual, id, desc, result);
}
static bool with_HasProperty(JSContext* cx, HandleObject obj, HandleId id,
bool* foundp) {
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
// ES 8.1.1.2.1 step 3-5.
if (!HasProperty(cx, actual, id, foundp)) {
return false;
}
if (!*foundp) {
return true;
}
// Steps 7-10. (Step 6 is a no-op.)
return CheckUnscopables(cx, actual, id, foundp);
}
static bool with_GetProperty(JSContext* cx, HandleObject obj,
HandleValue receiver, HandleId id,
MutableHandleValue vp) {
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
RootedValue actualReceiver(cx, receiver);
if (receiver.isObject() && &receiver.toObject() == obj) {
actualReceiver.setObject(*actual);
}
return GetProperty(cx, actual, actualReceiver, id, vp);
}
static bool with_SetProperty(JSContext* cx, HandleObject obj, HandleId id,
HandleValue v, HandleValue receiver,
ObjectOpResult& result) {
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
RootedValue actualReceiver(cx, receiver);
if (receiver.isObject() && &receiver.toObject() == obj) {
actualReceiver.setObject(*actual);
}
return SetProperty(cx, actual, id, v, actualReceiver, result);
}
static bool with_GetOwnPropertyDescriptor(
JSContext* cx, HandleObject obj, HandleId id,
MutableHandle<PropertyDescriptor> desc) {
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
return GetOwnPropertyDescriptor(cx, actual, id, desc);
}
static bool with_DeleteProperty(JSContext* cx, HandleObject obj, HandleId id,
ObjectOpResult& result) {
MOZ_ASSERT(!IsInternalDotName(cx, id));
RootedObject actual(cx, &obj->as<WithEnvironmentObject>().object());
return DeleteProperty(cx, actual, id, result);
}
static const ObjectOps WithEnvironmentObjectOps = {
with_LookupProperty, // lookupProperty
with_DefineProperty, // defineProperty
with_HasProperty, // hasProperty
with_GetProperty, // getProperty
with_SetProperty, // setProperty
with_GetOwnPropertyDescriptor, // getOwnPropertyDescriptor
with_DeleteProperty, // deleteProperty
nullptr, // getElements
nullptr, // funToString
};
const JSClass WithEnvironmentObject::class_ = {
"With",
JSCLASS_HAS_RESERVED_SLOTS(WithEnvironmentObject::RESERVED_SLOTS),
JS_NULL_CLASS_OPS,
JS_NULL_CLASS_SPEC,
JS_NULL_CLASS_EXT,
&WithEnvironmentObjectOps};
/* static */
NonSyntacticVariablesObject* NonSyntacticVariablesObject::create(
JSContext* cx) {
RootedShape shape(cx, EmptyEnvironmentShape(cx, &class_, JSSLOT_FREE(&class_),
/* baseShapeFlags = */ 0));
if (!shape) {
return nullptr;
}
Rooted<NonSyntacticVariablesObject*> obj(
cx, CreateEnvironmentObject<NonSyntacticVariablesObject>(cx, shape,
TenuredObject));
if (!obj) {
return nullptr;
}
MOZ_ASSERT(obj->isUnqualifiedVarObj());
if (!JSObject::setQualifiedVarObj(cx, obj)) {
return nullptr;
}
obj->initEnclosingEnvironment(&cx->global()->lexicalEnvironment());
return obj;
}
const JSClass NonSyntacticVariablesObject::class_ = {
"NonSyntacticVariablesObject",
JSCLASS_HAS_RESERVED_SLOTS(NonSyntacticVariablesObject::RESERVED_SLOTS)};
bool js::CreateNonSyntacticEnvironmentChain(JSContext* cx,
HandleObjectVector envChain,
MutableHandleObject env,
MutableHandleScope scope) {
RootedObject globalLexical(cx, &cx->global()->lexicalEnvironment());
if (!CreateObjectsForEnvironmentChain(cx, envChain, globalLexical, env)) {
return false;
}
if (!envChain.empty()) {
scope.set(GlobalScope::createEmpty(cx, ScopeKind::NonSyntactic));
if (!scope) {
return false;
}
// The XPConnect subscript loader, which may pass in its own
// environments to load scripts in, expects the environment chain to
// be the holder of "var" declarations. In SpiderMonkey, such objects
// are called "qualified varobjs", the "qualified" part meaning the
// declaration was qualified by "var". There is only sadness.
//
// See JSObject::isQualifiedVarObj.
if (!JSObject::setQualifiedVarObj(cx, env)) {
return false;
}
// Also get a non-syntactic lexical environment to capture 'let' and
// 'const' bindings. To persist lexical bindings, we have a 1-1
// mapping with the final unwrapped environment object (the
// environment that stores the 'var' bindings) and the lexical
// environment.
//
// TODOshu: disallow the subscript loader from using non-distinguished
// objects as dynamic scopes.
env.set(ObjectRealm::get(env).getOrCreateNonSyntacticLexicalEnvironment(
cx, env));
if (!env) {
return false;
}
} else {
scope.set(&cx->global()->emptyGlobalScope());
}
return true;
}
/*****************************************************************************/
/* static */
LexicalEnvironmentObject* LexicalEnvironmentObject::createTemplateObject(
JSContext* cx, HandleShape shape, HandleObject enclosing,
gc::InitialHeap heap, IsSingletonEnv isSingleton) {
MOZ_ASSERT(shape->getObjectClass() == &LexicalEnvironmentObject::class_);
// The JITs assume the result is nursery allocated unless we collected the
// nursery, so don't change |heap| here.
auto* env = CreateEnvironmentObject<LexicalEnvironmentObject>(cx, shape, heap,
isSingleton);
if (!env) {
return nullptr;
}
MOZ_ASSERT(!env->inDictionaryMode());
if (enclosing) {
env->initEnclosingEnvironment(enclosing);
}
return env;
}
/* static */
LexicalEnvironmentObject* LexicalEnvironmentObject::create(
JSContext* cx, Handle<LexicalScope*> scope, HandleObject enclosing,
gc::InitialHeap heap) {
cx->check(enclosing);
MOZ_ASSERT(scope->hasEnvironment());
RootedShape shape(cx, scope->environmentShape());
LexicalEnvironmentObject* env =
createTemplateObject(cx, shape, enclosing, heap, IsSingletonEnv::No);
if (!env) {
return nullptr;
}
// All lexical bindings start off uninitialized for TDZ.
uint32_t lastSlot = shape->slot();
MOZ_ASSERT(lastSlot == env->lastProperty()->slot());
for (uint32_t slot = JSSLOT_FREE(&class_); slot <= lastSlot; slot++) {
env->initSlot(slot, MagicValue(JS_UNINITIALIZED_LEXICAL));
}
env->initScopeUnchecked(scope);
return env;
}
/* static */
LexicalEnvironmentObject* LexicalEnvironmentObject::createForFrame(
JSContext* cx, Handle<LexicalScope*> scope, AbstractFramePtr frame) {
RootedObject enclosing(cx, frame.environmentChain());
return create(cx, scope, enclosing, gc::DefaultHeap);
}
/* static */
LexicalEnvironmentObject* LexicalEnvironmentObject::createGlobal(
JSContext* cx, Handle<GlobalObject*> global) {
MOZ_ASSERT(global);
RootedShape shape(cx, LexicalScope::getEmptyExtensibleEnvironmentShape(cx));
if (!shape) {
return nullptr;
}
LexicalEnvironmentObject* env =
LexicalEnvironmentObject::createTemplateObject(
cx, shape, global, gc::TenuredHeap, IsSingletonEnv::Yes);
if (!env) {
return nullptr;
}
env->initThisObject(global);
return env;
}