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
|
/* -*- 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 "jsfriendapi.h"
#include "mozilla/Atomics.h"
#include "mozilla/Maybe.h"
#include "mozilla/PodOperations.h"
#include "mozilla/TimeStamp.h"
#include <stdint.h>
#include "builtin/BigInt.h"
#include "builtin/MapObject.h"
#include "builtin/TestingFunctions.h"
#include "gc/GC.h"
#include "gc/PublicIterators.h"
#include "gc/WeakMap.h"
#include "js/CharacterEncoding.h"
#include "js/experimental/CodeCoverage.h"
#include "js/experimental/CTypes.h" // JS::AutoCTypesActivityCallback, JS::SetCTypesActivityCallback
#include "js/experimental/Intl.h" // JS::AddMoz{DateTimeFormat,DisplayNames}Constructor
#include "js/friend/ErrorMessages.h" // js::GetErrorMessage, JSMSG_*
#include "js/friend/StackLimits.h" // JS_STACK_GROWTH_DIRECTION
#include "js/friend/WindowProxy.h" // js::ToWindowIfWindowProxy
#include "js/Object.h" // JS::GetClass
#include "js/Printf.h"
#include "js/Proxy.h"
#include "js/shadow/Object.h" // JS::shadow::Object
#include "js/String.h" // JS::detail::StringToLinearStringSlow
#include "js/Wrapper.h"
#include "proxy/DeadObjectProxy.h"
#include "util/Poison.h"
#include "vm/ArgumentsObject.h"
#include "vm/BooleanObject.h"
#include "vm/DateObject.h"
#include "vm/ErrorObject.h"
#include "vm/FrameIter.h" // js::FrameIter
#include "vm/JSContext.h"
#include "vm/JSObject.h"
#include "vm/NumberObject.h"
#include "vm/PlainObject.h" // js::PlainObject
#include "vm/Printer.h"
#include "vm/PromiseObject.h" // js::PromiseObject
#include "vm/Realm.h"
#include "vm/StringObject.h"
#include "vm/Time.h"
#include "vm/WrapperObject.h"
#include "gc/Nursery-inl.h"
#include "vm/Compartment-inl.h" // JS::Compartment::wrap
#include "vm/EnvironmentObject-inl.h"
#include "vm/JSObject-inl.h"
#include "vm/JSScript-inl.h"
#include "vm/NativeObject-inl.h"
using namespace js;
using mozilla::PodArrayZero;
JS::RootingContext::RootingContext() : realm_(nullptr), zone_(nullptr) {
for (auto& listHead : stackRoots_) {
listHead = nullptr;
}
for (auto& listHead : autoGCRooters_) {
listHead = nullptr;
}
PodArrayZero(nativeStackLimit);
#if JS_STACK_GROWTH_DIRECTION > 0
for (int i = 0; i < StackKindCount; i++) {
nativeStackLimit[i] = UINTPTR_MAX;
}
#endif
}
JS_FRIEND_API void JS_SetGrayGCRootsTracer(JSContext* cx, JSTraceDataOp traceOp,
void* data) {
cx->runtime()->gc.setGrayRootsTracer(traceOp, data);
}
JS_FRIEND_API JSObject* JS_FindCompilationScope(JSContext* cx,
HandleObject objArg) {
cx->check(objArg);
RootedObject obj(cx, objArg);
/*
* We unwrap wrappers here. This is a little weird, but it's what's being
* asked of us.
*/
if (obj->is<WrapperObject>()) {
obj = UncheckedUnwrap(obj);
}
/*
* Get the Window if `obj` is a WindowProxy so that we compile in the
* correct (global) scope.
*/
return ToWindowIfWindowProxy(obj);
}
JS_FRIEND_API JSFunction* JS_GetObjectFunction(JSObject* obj) {
if (obj->is<JSFunction>()) {
return &obj->as<JSFunction>();
}
return nullptr;
}
JS_FRIEND_API bool JS_SplicePrototype(JSContext* cx, HandleObject global,
HandleObject proto) {
CHECK_THREAD(cx);
cx->check(global, proto);
MOZ_ASSERT(global->is<GlobalObject>());
Rooted<TaggedProto> tagged(cx, TaggedProto(proto));
return GlobalObject::splicePrototype(cx, global.as<GlobalObject>(), tagged);
}
JS_FRIEND_API JSObject* JS_NewObjectWithoutMetadata(
JSContext* cx, const JSClass* clasp, JS::Handle<JSObject*> proto) {
cx->check(proto);
AutoSuppressAllocationMetadataBuilder suppressMetadata(cx);
return JS_NewObjectWithGivenProto(cx, clasp, proto);
}
JS_FRIEND_API bool JS::GetIsSecureContext(JS::Realm* realm) {
return realm->creationOptions().secureContext();
}
JS_FRIEND_API JSPrincipals* JS::GetRealmPrincipals(JS::Realm* realm) {
return realm->principals();
}
JS_FRIEND_API void JS::SetRealmPrincipals(JS::Realm* realm,
JSPrincipals* principals) {
// Short circuit if there's no change.
if (principals == realm->principals()) {
return;
}
// We'd like to assert that our new principals is always same-origin
// with the old one, but JSPrincipals doesn't give us a way to do that.
// But we can at least assert that we're not switching between system
// and non-system.
const JSPrincipals* trusted =
realm->runtimeFromMainThread()->trustedPrincipals();
bool isSystem = principals && principals == trusted;
MOZ_RELEASE_ASSERT(realm->isSystem() == isSystem);
// Clear out the old principals, if any.
if (realm->principals()) {
JS_DropPrincipals(TlsContext.get(), realm->principals());
realm->setPrincipals(nullptr);
}
// Set up the new principals.
if (principals) {
JS_HoldPrincipals(principals);
realm->setPrincipals(principals);
}
}
JS_FRIEND_API JSPrincipals* JS_GetScriptPrincipals(JSScript* script) {
return script->principals();
}
JS_FRIEND_API bool JS_ScriptHasMutedErrors(JSScript* script) {
return script->mutedErrors();
}
JS_FRIEND_API bool JS_WrapPropertyDescriptor(
JSContext* cx, JS::MutableHandle<JS::PropertyDescriptor> desc) {
return cx->compartment()->wrap(cx, desc);
}
JS_FRIEND_API void JS_TraceShapeCycleCollectorChildren(JS::CallbackTracer* trc,
JS::GCCellPtr shape) {
MOZ_ASSERT(shape.is<Shape>());
TraceCycleCollectorChildren(trc, &shape.as<Shape>());
}
JS_FRIEND_API void JS_TraceObjectGroupCycleCollectorChildren(
JS::CallbackTracer* trc, JS::GCCellPtr group) {
MOZ_ASSERT(group.is<ObjectGroup>());
TraceCycleCollectorChildren(trc, &group.as<ObjectGroup>());
}
static bool DefineHelpProperty(JSContext* cx, HandleObject obj,
const char* prop, const char* value) {
RootedAtom atom(cx, Atomize(cx, value, strlen(value)));
if (!atom) {
return false;
}
return JS_DefineProperty(cx, obj, prop, atom,
JSPROP_READONLY | JSPROP_PERMANENT);
}
JS_FRIEND_API bool JS_DefineFunctionsWithHelp(
JSContext* cx, HandleObject obj, const JSFunctionSpecWithHelp* fs) {
MOZ_ASSERT(!cx->zone()->isAtomsZone());
CHECK_THREAD(cx);
cx->check(obj);
for (; fs->name; fs++) {
JSAtom* atom = Atomize(cx, fs->name, strlen(fs->name));
if (!atom) {
return false;
}
Rooted<jsid> id(cx, AtomToId(atom));
RootedFunction fun(cx, DefineFunction(cx, obj, id, fs->call, fs->nargs,
fs->flags | JSPROP_RESOLVING));
if (!fun) {
return false;
}
if (fs->jitInfo) {
fun->setJitInfo(fs->jitInfo);
}
if (fs->usage) {
if (!DefineHelpProperty(cx, fun, "usage", fs->usage)) {
return false;
}
}
if (fs->help) {
if (!DefineHelpProperty(cx, fun, "help", fs->help)) {
return false;
}
}
}
return true;
}
JS_FRIEND_API bool JS::GetBuiltinClass(JSContext* cx, HandleObject obj,
js::ESClass* cls) {
if (MOZ_UNLIKELY(obj->is<ProxyObject>())) {
return Proxy::getBuiltinClass(cx, obj, cls);
}
if (obj->is<PlainObject>()) {
*cls = ESClass::Object;
} else if (obj->is<ArrayObject>()) {
*cls = ESClass::Array;
} else if (obj->is<NumberObject>()) {
*cls = ESClass::Number;
} else if (obj->is<StringObject>()) {
*cls = ESClass::String;
} else if (obj->is<BooleanObject>()) {
*cls = ESClass::Boolean;
} else if (obj->is<RegExpObject>()) {
*cls = ESClass::RegExp;
} else if (obj->is<ArrayBufferObject>()) {
*cls = ESClass::ArrayBuffer;
} else if (obj->is<SharedArrayBufferObject>()) {
*cls = ESClass::SharedArrayBuffer;
} else if (obj->is<DateObject>()) {
*cls = ESClass::Date;
} else if (obj->is<SetObject>()) {
*cls = ESClass::Set;
} else if (obj->is<MapObject>()) {
*cls = ESClass::Map;
} else if (obj->is<PromiseObject>()) {
*cls = ESClass::Promise;
} else if (obj->is<MapIteratorObject>()) {
*cls = ESClass::MapIterator;
} else if (obj->is<SetIteratorObject>()) {
*cls = ESClass::SetIterator;
} else if (obj->is<ArgumentsObject>()) {
*cls = ESClass::Arguments;
} else if (obj->is<ErrorObject>()) {
*cls = ESClass::Error;
} else if (obj->is<BigIntObject>()) {
*cls = ESClass::BigInt;
} else if (obj->is<JSFunction>()) {
*cls = ESClass::Function;
} else {
*cls = ESClass::Other;
}
return true;
}
JS_FRIEND_API bool js::IsArgumentsObject(HandleObject obj) {
return obj->is<ArgumentsObject>();
}
JS_FRIEND_API JS::Zone* js::GetRealmZone(JS::Realm* realm) {
return realm->zone();
}
JS_FRIEND_API bool js::IsSystemCompartment(JS::Compartment* comp) {
// Realms in the same compartment must either all be system realms or
// non-system realms. We assert this in NewRealm and SetRealmPrincipals,
// but do an extra sanity check here.
MOZ_ASSERT(comp->realms()[0]->isSystem() ==
comp->realms().back()->isSystem());
return comp->realms()[0]->isSystem();
}
JS_FRIEND_API bool js::IsSystemRealm(JS::Realm* realm) {
return realm->isSystem();
}
JS_FRIEND_API bool js::IsSystemZone(Zone* zone) { return zone->isSystemZone(); }
JS_FRIEND_API bool js::IsFunctionObject(JSObject* obj) {
return obj->is<JSFunction>();
}
JS_FRIEND_API bool js::IsSavedFrame(JSObject* obj) {
return obj->is<SavedFrame>();
}
JS_FRIEND_API bool js::UninlinedIsCrossCompartmentWrapper(const JSObject* obj) {
return js::IsCrossCompartmentWrapper(obj);
}
JS_FRIEND_API void js::AssertSameCompartment(JSContext* cx, JSObject* obj) {
cx->check(obj);
}
JS_FRIEND_API void js::AssertSameCompartment(JSContext* cx, JS::HandleValue v) {
cx->check(v);
}
#ifdef DEBUG
JS_FRIEND_API void js::AssertSameCompartment(JSObject* objA, JSObject* objB) {
MOZ_ASSERT(objA->compartment() == objB->compartment());
}
#endif
JS_FRIEND_API void js::NotifyAnimationActivity(JSObject* obj) {
MOZ_ASSERT(obj->is<GlobalObject>());
auto timeNow = mozilla::TimeStamp::Now();
obj->as<GlobalObject>().realm()->lastAnimationTime = timeNow;
obj->runtimeFromMainThread()->lastAnimationTime = timeNow;
}
JS_FRIEND_API bool js::IsObjectInContextCompartment(JSObject* obj,
const JSContext* cx) {
return obj->compartment() == cx->compartment();
}
JS_FRIEND_API bool js::detail::RunningWithTrustedPrincipals(JSContext* cx) {
return cx->runningWithTrustedPrincipals();
}
JS_FRIEND_API JSFunction* js::DefineFunctionWithReserved(
JSContext* cx, JSObject* objArg, const char* name, JSNative call,
unsigned nargs, unsigned attrs) {
RootedObject obj(cx, objArg);
MOZ_ASSERT(!cx->zone()->isAtomsZone());
CHECK_THREAD(cx);
cx->check(obj);
JSAtom* atom = Atomize(cx, name, strlen(name));
if (!atom) {
return nullptr;
}
Rooted<jsid> id(cx, AtomToId(atom));
return DefineFunction(cx, obj, id, call, nargs, attrs,
gc::AllocKind::FUNCTION_EXTENDED);
}
JS_FRIEND_API JSFunction* js::NewFunctionWithReserved(JSContext* cx,
JSNative native,
unsigned nargs,
unsigned flags,
const char* name) {
MOZ_ASSERT(!cx->zone()->isAtomsZone());
CHECK_THREAD(cx);
RootedAtom atom(cx);
if (name) {
atom = Atomize(cx, name, strlen(name));
if (!atom) {
return nullptr;
}
}
return (flags & JSFUN_CONSTRUCTOR)
? NewNativeConstructor(cx, native, nargs, atom,
gc::AllocKind::FUNCTION_EXTENDED)
: NewNativeFunction(cx, native, nargs, atom,
gc::AllocKind::FUNCTION_EXTENDED);
}
JS_FRIEND_API JSFunction* js::NewFunctionByIdWithReserved(
JSContext* cx, JSNative native, unsigned nargs, unsigned flags, jsid id) {
MOZ_ASSERT(JSID_IS_STRING(id));
MOZ_ASSERT(!cx->zone()->isAtomsZone());
CHECK_THREAD(cx);
cx->check(id);
RootedAtom atom(cx, JSID_TO_ATOM(id));
return (flags & JSFUN_CONSTRUCTOR)
? NewNativeConstructor(cx, native, nargs, atom,
gc::AllocKind::FUNCTION_EXTENDED)
: NewNativeFunction(cx, native, nargs, atom,
gc::AllocKind::FUNCTION_EXTENDED);
}
JS_FRIEND_API const Value& js::GetFunctionNativeReserved(JSObject* fun,
size_t which) {
MOZ_ASSERT(fun->as<JSFunction>().isNative());
return fun->as<JSFunction>().getExtendedSlot(which);
}
JS_FRIEND_API void js::SetFunctionNativeReserved(JSObject* fun, size_t which,
const Value& val) {
MOZ_ASSERT(fun->as<JSFunction>().isNative());
MOZ_ASSERT_IF(val.isObject(),
val.toObject().compartment() == fun->compartment());
fun->as<JSFunction>().setExtendedSlot(which, val);
}
JS_FRIEND_API bool js::FunctionHasNativeReserved(JSObject* fun) {
MOZ_ASSERT(fun->as<JSFunction>().isNative());
return fun->as<JSFunction>().isExtended();
}
bool js::GetObjectProto(JSContext* cx, JS::Handle<JSObject*> obj,
JS::MutableHandle<JSObject*> proto) {
cx->check(obj);
if (IsProxy(obj)) {
return JS_GetPrototype(cx, obj, proto);
}
proto.set(
reinterpret_cast<const JS::shadow::Object*>(obj.get())->group->proto);
return true;
}
JS_FRIEND_API JSObject* js::GetStaticPrototype(JSObject* obj) {
MOZ_ASSERT(obj->hasStaticPrototype());
return obj->staticPrototype();
}
JS_FRIEND_API bool js::GetRealmOriginalEval(JSContext* cx,
MutableHandleObject eval) {
return GlobalObject::getOrCreateEval(cx, cx->global(), eval);
}
void JS::detail::SetReservedSlotWithBarrier(JSObject* obj, size_t slot,
const Value& value) {
if (IsProxy(obj)) {
obj->as<ProxyObject>().setReservedSlot(slot, value);
} else {
obj->as<NativeObject>().setSlot(slot, value);
}
}
void js::SetPreserveWrapperCallbacks(
JSContext* cx, PreserveWrapperCallback preserveWrapper,
HasReleasedWrapperCallback hasReleasedWrapper) {
cx->runtime()->preserveWrapperCallback = preserveWrapper;
cx->runtime()->hasReleasedWrapperCallback = hasReleasedWrapper;
}
JS_FRIEND_API unsigned JS_PCToLineNumber(JSScript* script, jsbytecode* pc,
unsigned* columnp) {
return PCToLineNumber(script, pc, columnp);
}
JS_FRIEND_API bool JS_IsDeadWrapper(JSObject* obj) {
return IsDeadProxyObject(obj);
}
JS_FRIEND_API JSObject* JS_NewDeadWrapper(JSContext* cx, JSObject* origObj) {
return NewDeadProxyObject(cx, origObj);
}
void js::TraceWeakMaps(WeakMapTracer* trc) {
WeakMapBase::traceAllMappings(trc);
}
extern JS_FRIEND_API bool js::AreGCGrayBitsValid(JSRuntime* rt) {
return rt->gc.areGrayBitsValid();
}
JS_FRIEND_API bool js::ZoneGlobalsAreAllGray(JS::Zone* zone) {
for (RealmsInZoneIter realm(zone); !realm.done(); realm.next()) {
JSObject* obj = realm->unsafeUnbarrieredMaybeGlobal();
if (!obj || !JS::ObjectIsMarkedGray(obj)) {
return false;
}
}
return true;
}
JS_FRIEND_API bool js::IsCompartmentZoneSweepingOrCompacting(
JS::Compartment* comp) {
MOZ_ASSERT(comp);
return comp->zone()->isGCSweepingOrCompacting();
}
JS_FRIEND_API void js::TraceGrayWrapperTargets(JSTracer* trc, Zone* zone) {
JS::AutoSuppressGCAnalysis nogc;
for (CompartmentsInZoneIter comp(zone); !comp.done(); comp.next()) {
for (Compartment::ObjectWrapperEnum e(comp); !e.empty(); e.popFront()) {
JSObject* target = e.front().key();
if (target->isMarkedGray()) {
TraceManuallyBarrieredEdge(trc, &target, "gray CCW target");
MOZ_ASSERT(target == e.front().key());
}
}
}
}
JSLinearString* JS::detail::StringToLinearStringSlow(JSContext* cx,
JSString* str) {
return str->ensureLinear(cx);
}
static bool CopyProxyObject(JSContext* cx, Handle<ProxyObject*> from,
Handle<ProxyObject*> to) {
MOZ_ASSERT(from->getClass() == to->getClass());
if (from->is<WrapperObject>() &&
(Wrapper::wrapperHandler(from)->flags() & Wrapper::CROSS_COMPARTMENT)) {
to->setCrossCompartmentPrivate(GetProxyPrivate(from));
} else {
RootedValue v(cx, GetProxyPrivate(from));
if (!cx->compartment()->wrap(cx, &v)) {
return false;
}
to->setSameCompartmentPrivate(v);
}
MOZ_ASSERT(from->numReservedSlots() == to->numReservedSlots());
RootedValue v(cx);
for (size_t n = 0; n < from->numReservedSlots(); n++) {
v = GetProxyReservedSlot(from, n);
if (!cx->compartment()->wrap(cx, &v)) {
return false;
}
SetProxyReservedSlot(to, n, v);
}
return true;
}
JS_FRIEND_API JSObject* JS_CloneObject(JSContext* cx, HandleObject obj,
HandleObject proto) {
// |obj| might be in a different compartment.
cx->check(proto);
if (!obj->isNative() && !obj->is<ProxyObject>()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_CANT_CLONE_OBJECT);
return nullptr;
}
RootedObject clone(cx);
if (obj->isNative()) {
// JS_CloneObject is used to create the target object for JSObject::swap().
// swap() requires its arguments are tenured, so ensure tenure allocation.
clone = NewTenuredObjectWithGivenProto(cx, obj->getClass(), proto);
if (!clone) {
return nullptr;
}
if (clone->is<JSFunction>() &&
(obj->compartment() != clone->compartment())) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_CANT_CLONE_OBJECT);
return nullptr;
}
if (obj->as<NativeObject>().hasPrivate()) {
clone->as<NativeObject>().setPrivate(
obj->as<NativeObject>().getPrivate());
}
} else {
auto* handler = GetProxyHandler(obj);
// Same as above, require tenure allocation of the clone. This means for
// proxy objects we need to reject nursery allocatable proxies.
if (handler->canNurseryAllocate()) {
JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
JSMSG_CANT_CLONE_OBJECT);
return nullptr;
}
clone = ProxyObject::New(cx, handler, JS::NullHandleValue,
AsTaggedProto(proto), obj->getClass());
if (!clone) {
return nullptr;
}
if (!CopyProxyObject(cx, obj.as<ProxyObject>(), clone.as<ProxyObject>())) {
return nullptr;
}
}
return clone;
}
extern JS_FRIEND_API bool JS::ForceLexicalInitialization(JSContext* cx,
HandleObject obj) {
AssertHeapIsIdle();
CHECK_THREAD(cx);
cx->check(obj);
bool initializedAny = false;
NativeObject* nobj = &obj->as<NativeObject>();
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
Shape* s = &r.front();
Value v = nobj->getSlot(s->slot());
if (s->isDataProperty() && v.isMagic() &&
v.whyMagic() == JS_UNINITIALIZED_LEXICAL) {
nobj->setSlot(s->slot(), UndefinedValue());
initializedAny = true;
}
}
return initializedAny;
}
extern JS_FRIEND_API int JS::IsGCPoisoning() {
#ifdef JS_GC_POISONING
return !js::gDisablePoisoning;
#else
return false;
#endif
}
JS_FRIEND_API void JS::NotifyGCRootsRemoved(JSContext* cx) {
cx->runtime()->gc.notifyRootsRemoved();
}
JS_FRIEND_API JS::Realm* js::GetAnyRealmInZone(JS::Zone* zone) {
if (zone->isAtomsZone()) {
return nullptr;
}
RealmsInZoneIter realm(zone);
MOZ_ASSERT(!realm.done());
return realm.get();
}
JS_FRIEND_API bool js::IsSharableCompartment(JS::Compartment* comp) {
// If this compartment has nuked outgoing wrappers (because all its globals
// got nuked), we won't be able to create any useful CCWs out of it in the
// future, and so we shouldn't use it for any new globals.
if (comp->nukedOutgoingWrappers) {
return false;
}
// If this compartment has no live globals, it might be in the middle of being
// GCed. Don't create any new Realms inside. There's no point to doing that
// anyway, since the idea would be to avoid CCWs from existing Realms in the
// compartment to the new Realm, and there are no existing Realms.
if (!CompartmentHasLiveGlobal(comp)) {
return false;
}
// Good to go.
return true;
}
JS_FRIEND_API JSObject* js::GetTestingFunctions(JSContext* cx) {
RootedObject obj(cx, JS_NewPlainObject(cx));
if (!obj) {
return nullptr;
}
if (!DefineTestingFunctions(cx, obj, false, false)) {
return nullptr;
}
return obj;
}
JS_FRIEND_API void js::SetDOMCallbacks(JSContext* cx,
const DOMCallbacks* callbacks) {
cx->runtime()->DOMcallbacks = callbacks;
}
JS_FRIEND_API const DOMCallbacks* js::GetDOMCallbacks(JSContext* cx) {
return cx->runtime()->DOMcallbacks;
}
JS_FRIEND_API void js::PrepareScriptEnvironmentAndInvoke(
JSContext* cx, HandleObject global,
ScriptEnvironmentPreparer::Closure& closure) {
MOZ_ASSERT(!cx->isExceptionPending());
MOZ_ASSERT(global->is<GlobalObject>());
MOZ_RELEASE_ASSERT(
cx->runtime()->scriptEnvironmentPreparer,
"Embedding needs to set a scriptEnvironmentPreparer callback");
cx->runtime()->scriptEnvironmentPreparer->invoke(global, closure);
}
JS_FRIEND_API void js::SetScriptEnvironmentPreparer(
JSContext* cx, ScriptEnvironmentPreparer* preparer) {
cx->runtime()->scriptEnvironmentPreparer = preparer;
}
JS_FRIEND_API void JS::SetCTypesActivityCallback(JSContext* cx,
CTypesActivityCallback cb) {
cx->runtime()->ctypesActivityCallback = cb;
}
JS::AutoCTypesActivityCallback::AutoCTypesActivityCallback(
JSContext* cx, CTypesActivityType beginType, CTypesActivityType endType)
: cx(cx),
callback(cx->runtime()->ctypesActivityCallback),
endType(endType) {
if (callback) {
callback(cx, beginType);
}
}
JS_FRIEND_API void js::SetAllocationMetadataBuilder(
JSContext* cx, const AllocationMetadataBuilder* callback) {
cx->realm()->setAllocationMetadataBuilder(callback);
}
JS_FRIEND_API JSObject* js::GetAllocationMetadata(JSObject* obj) {
ObjectWeakMap* map = ObjectRealm::get(obj).objectMetadataTable.get();
if (map) {
return map->lookup(obj);
}
return nullptr;
}
JS_FRIEND_API bool js::ReportIsNotFunction(JSContext* cx, HandleValue v) {
cx->check(v);
return ReportIsNotFunction(cx, v, -1);
}
#ifdef DEBUG
bool js::HasObjectMovedOp(JSObject* obj) {
return !!JS::GetClass(obj)->extObjectMovedOp();
}
#endif
JS_FRIEND_API bool js::ForwardToNative(JSContext* cx, JSNative native,
const CallArgs& args) {
return native(cx, args.length(), args.base());
}
AutoAssertNoContentJS::AutoAssertNoContentJS(JSContext* cx)
: context_(cx), prevAllowContentJS_(cx->runtime()->allowContentJS_) {
cx->runtime()->allowContentJS_ = false;
}
AutoAssertNoContentJS::~AutoAssertNoContentJS() {
context_->runtime()->allowContentJS_ = prevAllowContentJS_;
}
JS_FRIEND_API void js::EnableCodeCoverage() { js::coverage::EnableLCov(); }
JS_FRIEND_API JS::Value js::MaybeGetScriptPrivate(JSObject* object) {
if (!object->is<ScriptSourceObject>()) {
return UndefinedValue();
}
return object->as<ScriptSourceObject>().canonicalPrivate();
}
JS_FRIEND_API uint64_t js::GetGCHeapUsageForObjectZone(JSObject* obj) {
return obj->zone()->gcHeapSize.bytes();
}
#ifdef DEBUG
JS_FRIEND_API bool js::RuntimeIsBeingDestroyed() {
JSRuntime* runtime = TlsContext.get()->runtime();
MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtime));
return runtime->isBeingDestroyed();
}
#endif
// No-op implementations of public API that would depend on --with-intl-api
#ifndef JS_HAS_INTL_API
static bool IntlNotEnabled(JSContext* cx) {
JS_ReportErrorNumberASCII(cx, js::GetErrorMessage, nullptr,
JSMSG_SUPPORT_NOT_ENABLED, "Intl");
return false;
}
bool JS::AddMozDateTimeFormatConstructor(JSContext* cx, JS::HandleObject intl) {
return IntlNotEnabled(cx);
}
bool JS::AddMozDisplayNamesConstructor(JSContext* cx, JS::HandleObject intl) {
return IntlNotEnabled(cx);
}
#endif // !JS_HAS_INTL_API
JS_FRIEND_API JS::Zone* js::GetObjectZoneFromAnyThread(const JSObject* obj) {
return MaybeForwarded(obj)->zoneFromAnyThread();
}
|