Source code

Revision control

Other Tools

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
#include "jsfriendapi.h"
8
9
#include "mozilla/Atomics.h"
10
#include "mozilla/Maybe.h"
11
#include "mozilla/PodOperations.h"
12
#include "mozilla/TimeStamp.h"
13
14
#include <stdint.h>
15
16
#include "builtin/BigInt.h"
17
#include "builtin/MapObject.h"
18
#include "builtin/Promise.h"
19
#include "builtin/TestingFunctions.h"
20
#include "gc/GC.h"
21
#include "gc/PublicIterators.h"
22
#include "gc/WeakMap.h"
23
#include "js/CharacterEncoding.h"
24
#include "js/Printf.h"
25
#include "js/Proxy.h"
26
#include "js/Wrapper.h"
27
#include "proxy/DeadObjectProxy.h"
28
#include "util/Poison.h"
29
#include "vm/ArgumentsObject.h"
30
#include "vm/DateObject.h"
31
#include "vm/FrameIter.h" // js::FrameIter
32
#include "vm/JSContext.h"
33
#include "vm/JSObject.h"
34
#include "vm/Printer.h"
35
#include "vm/Realm.h"
36
#include "vm/Time.h"
37
#include "vm/WrapperObject.h"
38
39
#include "gc/Nursery-inl.h"
40
#include "vm/EnvironmentObject-inl.h"
41
#include "vm/JSObject-inl.h"
42
#include "vm/JSScript-inl.h"
43
#include "vm/NativeObject-inl.h"
44
45
using namespace js;
46
47
using mozilla::PodArrayZero;
48
49
JS::RootingContext::RootingContext()
50
: autoGCRooters_(nullptr), realm_(nullptr), zone_(nullptr) {
51
for (auto& stackRootPtr : stackRoots_) {
52
stackRootPtr = nullptr;
53
}
54
55
PodArrayZero(nativeStackLimit);
56
#if JS_STACK_GROWTH_DIRECTION > 0
57
for (int i = 0; i < StackKindCount; i++) {
58
nativeStackLimit[i] = UINTPTR_MAX;
59
}
60
#endif
61
}
62
63
JS_FRIEND_API void JS_SetGrayGCRootsTracer(JSContext* cx, JSTraceDataOp traceOp,
64
void* data) {
65
cx->runtime()->gc.setGrayRootsTracer(traceOp, data);
66
}
67
68
JS_FRIEND_API JSObject* JS_FindCompilationScope(JSContext* cx,
69
HandleObject objArg) {
70
cx->check(objArg);
71
72
RootedObject obj(cx, objArg);
73
74
/*
75
* We unwrap wrappers here. This is a little weird, but it's what's being
76
* asked of us.
77
*/
78
if (obj->is<WrapperObject>()) {
79
obj = UncheckedUnwrap(obj);
80
}
81
82
/*
83
* Get the Window if `obj` is a WindowProxy so that we compile in the
84
* correct (global) scope.
85
*/
86
return ToWindowIfWindowProxy(obj);
87
}
88
89
JS_FRIEND_API JSFunction* JS_GetObjectFunction(JSObject* obj) {
90
if (obj->is<JSFunction>()) {
91
return &obj->as<JSFunction>();
92
}
93
return nullptr;
94
}
95
96
JS_FRIEND_API bool JS_SplicePrototype(JSContext* cx, HandleObject obj,
97
HandleObject proto) {
98
/*
99
* Change the prototype of an object which hasn't been used anywhere
100
* and does not share its type with another object. Unlike JS_SetPrototype,
101
* does not nuke type information for the object.
102
*/
103
CHECK_THREAD(cx);
104
cx->check(obj, proto);
105
106
if (!obj->isSingleton()) {
107
/*
108
* We can see non-singleton objects when trying to splice prototypes
109
* due to mutable __proto__ (ugh).
110
*/
111
return JS_SetPrototype(cx, obj, proto);
112
}
113
114
Rooted<TaggedProto> tagged(cx, TaggedProto(proto));
115
return JSObject::splicePrototype(cx, obj, tagged);
116
}
117
118
JS_FRIEND_API JSObject* JS_NewObjectWithUniqueType(JSContext* cx,
119
const JSClass* clasp,
120
HandleObject proto) {
121
/*
122
* Create our object with a null proto and then splice in the correct proto
123
* after we setSingleton, so that we don't pollute the default
124
* ObjectGroup attached to our proto with information about our object, since
125
* we're not going to be using that ObjectGroup anyway.
126
*/
127
RootedObject obj(
128
cx, NewObjectWithGivenProto(cx, clasp, nullptr, SingletonObject));
129
if (!obj) {
130
return nullptr;
131
}
132
if (!JS_SplicePrototype(cx, obj, proto)) {
133
return nullptr;
134
}
135
return obj;
136
}
137
138
JS_FRIEND_API JSObject* JS_NewObjectWithoutMetadata(
139
JSContext* cx, const JSClass* clasp, JS::Handle<JSObject*> proto) {
140
cx->check(proto);
141
AutoSuppressAllocationMetadataBuilder suppressMetadata(cx);
142
return JS_NewObjectWithGivenProto(cx, clasp, proto);
143
}
144
145
JS_FRIEND_API bool JS::GetIsSecureContext(JS::Realm* realm) {
146
return realm->creationOptions().secureContext();
147
}
148
149
JS_FRIEND_API void js::AssertCompartmentHasSingleRealm(JS::Compartment* comp) {
150
MOZ_RELEASE_ASSERT(comp->realms().length() == 1);
151
}
152
153
JS_FRIEND_API JSPrincipals* JS::GetRealmPrincipals(JS::Realm* realm) {
154
return realm->principals();
155
}
156
157
JS_FRIEND_API void JS::SetRealmPrincipals(JS::Realm* realm,
158
JSPrincipals* principals) {
159
// Short circuit if there's no change.
160
if (principals == realm->principals()) {
161
return;
162
}
163
164
// We'd like to assert that our new principals is always same-origin
165
// with the old one, but JSPrincipals doesn't give us a way to do that.
166
// But we can at least assert that we're not switching between system
167
// and non-system.
168
const JSPrincipals* trusted =
169
realm->runtimeFromMainThread()->trustedPrincipals();
170
bool isSystem = principals && principals == trusted;
171
MOZ_RELEASE_ASSERT(realm->isSystem() == isSystem);
172
173
// Clear out the old principals, if any.
174
if (realm->principals()) {
175
JS_DropPrincipals(TlsContext.get(), realm->principals());
176
realm->setPrincipals(nullptr);
177
}
178
179
// Set up the new principals.
180
if (principals) {
181
JS_HoldPrincipals(principals);
182
realm->setPrincipals(principals);
183
}
184
}
185
186
JS_FRIEND_API JSPrincipals* JS_GetScriptPrincipals(JSScript* script) {
187
return script->principals();
188
}
189
190
JS_FRIEND_API JS::Realm* js::GetScriptRealm(JSScript* script) {
191
return script->realm();
192
}
193
194
JS_FRIEND_API bool JS_ScriptHasMutedErrors(JSScript* script) {
195
return script->mutedErrors();
196
}
197
198
JS_FRIEND_API bool JS_WrapPropertyDescriptor(
199
JSContext* cx, JS::MutableHandle<JS::PropertyDescriptor> desc) {
200
return cx->compartment()->wrap(cx, desc);
201
}
202
203
JS_FRIEND_API void JS_TraceShapeCycleCollectorChildren(JS::CallbackTracer* trc,
204
JS::GCCellPtr shape) {
205
MOZ_ASSERT(shape.is<Shape>());
206
TraceCycleCollectorChildren(trc, &shape.as<Shape>());
207
}
208
209
JS_FRIEND_API void JS_TraceObjectGroupCycleCollectorChildren(
210
JS::CallbackTracer* trc, JS::GCCellPtr group) {
211
MOZ_ASSERT(group.is<ObjectGroup>());
212
TraceCycleCollectorChildren(trc, &group.as<ObjectGroup>());
213
}
214
215
static bool DefineHelpProperty(JSContext* cx, HandleObject obj,
216
const char* prop, const char* value) {
217
RootedAtom atom(cx, Atomize(cx, value, strlen(value)));
218
if (!atom) {
219
return false;
220
}
221
return JS_DefineProperty(cx, obj, prop, atom,
222
JSPROP_READONLY | JSPROP_PERMANENT);
223
}
224
225
JS_FRIEND_API bool JS_DefineFunctionsWithHelp(
226
JSContext* cx, HandleObject obj, const JSFunctionSpecWithHelp* fs) {
227
MOZ_ASSERT(!cx->zone()->isAtomsZone());
228
229
CHECK_THREAD(cx);
230
cx->check(obj);
231
for (; fs->name; fs++) {
232
JSAtom* atom = Atomize(cx, fs->name, strlen(fs->name));
233
if (!atom) {
234
return false;
235
}
236
237
Rooted<jsid> id(cx, AtomToId(atom));
238
RootedFunction fun(cx, DefineFunction(cx, obj, id, fs->call, fs->nargs,
239
fs->flags | JSPROP_RESOLVING));
240
if (!fun) {
241
return false;
242
}
243
244
if (fs->jitInfo) {
245
fun->setJitInfo(fs->jitInfo);
246
}
247
248
if (fs->usage) {
249
if (!DefineHelpProperty(cx, fun, "usage", fs->usage)) {
250
return false;
251
}
252
}
253
254
if (fs->help) {
255
if (!DefineHelpProperty(cx, fun, "help", fs->help)) {
256
return false;
257
}
258
}
259
}
260
261
return true;
262
}
263
264
JS_FRIEND_API bool js::GetBuiltinClass(JSContext* cx, HandleObject obj,
265
ESClass* cls) {
266
if (MOZ_UNLIKELY(obj->is<ProxyObject>())) {
267
return Proxy::getBuiltinClass(cx, obj, cls);
268
}
269
270
if (obj->is<PlainObject>()) {
271
*cls = ESClass::Object;
272
} else if (obj->is<ArrayObject>()) {
273
*cls = ESClass::Array;
274
} else if (obj->is<NumberObject>()) {
275
*cls = ESClass::Number;
276
} else if (obj->is<StringObject>()) {
277
*cls = ESClass::String;
278
} else if (obj->is<BooleanObject>()) {
279
*cls = ESClass::Boolean;
280
} else if (obj->is<RegExpObject>()) {
281
*cls = ESClass::RegExp;
282
} else if (obj->is<ArrayBufferObject>()) {
283
*cls = ESClass::ArrayBuffer;
284
} else if (obj->is<SharedArrayBufferObject>()) {
285
*cls = ESClass::SharedArrayBuffer;
286
} else if (obj->is<DateObject>()) {
287
*cls = ESClass::Date;
288
} else if (obj->is<SetObject>()) {
289
*cls = ESClass::Set;
290
} else if (obj->is<MapObject>()) {
291
*cls = ESClass::Map;
292
} else if (obj->is<PromiseObject>()) {
293
*cls = ESClass::Promise;
294
} else if (obj->is<MapIteratorObject>()) {
295
*cls = ESClass::MapIterator;
296
} else if (obj->is<SetIteratorObject>()) {
297
*cls = ESClass::SetIterator;
298
} else if (obj->is<ArgumentsObject>()) {
299
*cls = ESClass::Arguments;
300
} else if (obj->is<ErrorObject>()) {
301
*cls = ESClass::Error;
302
} else if (obj->is<BigIntObject>()) {
303
*cls = ESClass::BigInt;
304
} else {
305
*cls = ESClass::Other;
306
}
307
308
return true;
309
}
310
311
JS_FRIEND_API bool js::IsArgumentsObject(HandleObject obj) {
312
return obj->is<ArgumentsObject>();
313
}
314
315
JS_FRIEND_API const char* js::ObjectClassName(JSContext* cx, HandleObject obj) {
316
cx->check(obj);
317
return GetObjectClassName(cx, obj);
318
}
319
320
JS_FRIEND_API JS::Zone* js::GetRealmZone(JS::Realm* realm) {
321
return realm->zone();
322
}
323
324
JS_FRIEND_API bool js::IsSystemCompartment(JS::Compartment* comp) {
325
// Realms in the same compartment must either all be system realms or
326
// non-system realms. We assert this in NewRealm and SetRealmPrincipals,
327
// but do an extra sanity check here.
328
MOZ_ASSERT(comp->realms()[0]->isSystem() ==
329
comp->realms().back()->isSystem());
330
return comp->realms()[0]->isSystem();
331
}
332
333
JS_FRIEND_API bool js::IsSystemRealm(JS::Realm* realm) {
334
return realm->isSystem();
335
}
336
337
JS_FRIEND_API bool js::IsSystemZone(Zone* zone) { return zone->isSystem; }
338
339
JS_FRIEND_API bool js::IsAtomsZone(JS::Zone* zone) {
340
return zone->runtimeFromAnyThread()->isAtomsZone(zone);
341
}
342
343
JS_FRIEND_API bool js::IsFunctionObject(JSObject* obj) {
344
return obj->is<JSFunction>();
345
}
346
347
JS_FRIEND_API bool js::UninlinedIsCrossCompartmentWrapper(const JSObject* obj) {
348
return js::IsCrossCompartmentWrapper(obj);
349
}
350
351
JS_FRIEND_API JSObject* js::GetPrototypeNoProxy(JSObject* obj) {
352
MOZ_ASSERT(!obj->is<js::ProxyObject>());
353
return obj->staticPrototype();
354
}
355
356
JS_FRIEND_API void js::AssertSameCompartment(JSContext* cx, JSObject* obj) {
357
cx->check(obj);
358
}
359
360
JS_FRIEND_API void js::AssertSameCompartment(JSContext* cx, JS::HandleValue v) {
361
cx->check(v);
362
}
363
364
#ifdef DEBUG
365
JS_FRIEND_API void js::AssertSameCompartment(JSObject* objA, JSObject* objB) {
366
MOZ_ASSERT(objA->compartment() == objB->compartment());
367
}
368
#endif
369
370
JS_FRIEND_API void js::NotifyAnimationActivity(JSObject* obj) {
371
MOZ_ASSERT(obj->is<GlobalObject>());
372
373
auto timeNow = mozilla::TimeStamp::Now();
374
obj->as<GlobalObject>().realm()->lastAnimationTime = timeNow;
375
obj->runtimeFromMainThread()->lastAnimationTime = timeNow;
376
}
377
378
JS_FRIEND_API uint32_t js::GetObjectSlotSpan(JSObject* obj) {
379
return obj->as<NativeObject>().slotSpan();
380
}
381
382
JS_FRIEND_API bool js::IsObjectInContextCompartment(JSObject* obj,
383
const JSContext* cx) {
384
return obj->compartment() == cx->compartment();
385
}
386
387
JS_FRIEND_API bool js::RunningWithTrustedPrincipals(JSContext* cx) {
388
return cx->runningWithTrustedPrincipals();
389
}
390
391
JS_FRIEND_API JSFunction* js::DefineFunctionWithReserved(
392
JSContext* cx, JSObject* objArg, const char* name, JSNative call,
393
unsigned nargs, unsigned attrs) {
394
RootedObject obj(cx, objArg);
395
MOZ_ASSERT(!cx->zone()->isAtomsZone());
396
CHECK_THREAD(cx);
397
cx->check(obj);
398
JSAtom* atom = Atomize(cx, name, strlen(name));
399
if (!atom) {
400
return nullptr;
401
}
402
Rooted<jsid> id(cx, AtomToId(atom));
403
return DefineFunction(cx, obj, id, call, nargs, attrs,
404
gc::AllocKind::FUNCTION_EXTENDED);
405
}
406
407
JS_FRIEND_API JSFunction* js::NewFunctionWithReserved(JSContext* cx,
408
JSNative native,
409
unsigned nargs,
410
unsigned flags,
411
const char* name) {
412
MOZ_ASSERT(!cx->zone()->isAtomsZone());
413
414
CHECK_THREAD(cx);
415
416
RootedAtom atom(cx);
417
if (name) {
418
atom = Atomize(cx, name, strlen(name));
419
if (!atom) {
420
return nullptr;
421
}
422
}
423
424
return (flags & JSFUN_CONSTRUCTOR)
425
? NewNativeConstructor(cx, native, nargs, atom,
426
gc::AllocKind::FUNCTION_EXTENDED)
427
: NewNativeFunction(cx, native, nargs, atom,
428
gc::AllocKind::FUNCTION_EXTENDED);
429
}
430
431
JS_FRIEND_API JSFunction* js::NewFunctionByIdWithReserved(
432
JSContext* cx, JSNative native, unsigned nargs, unsigned flags, jsid id) {
433
MOZ_ASSERT(JSID_IS_STRING(id));
434
MOZ_ASSERT(!cx->zone()->isAtomsZone());
435
CHECK_THREAD(cx);
436
cx->check(id);
437
438
RootedAtom atom(cx, JSID_TO_ATOM(id));
439
return (flags & JSFUN_CONSTRUCTOR)
440
? NewNativeConstructor(cx, native, nargs, atom,
441
gc::AllocKind::FUNCTION_EXTENDED)
442
: NewNativeFunction(cx, native, nargs, atom,
443
gc::AllocKind::FUNCTION_EXTENDED);
444
}
445
446
JS_FRIEND_API const Value& js::GetFunctionNativeReserved(JSObject* fun,
447
size_t which) {
448
MOZ_ASSERT(fun->as<JSFunction>().isNative());
449
return fun->as<JSFunction>().getExtendedSlot(which);
450
}
451
452
JS_FRIEND_API void js::SetFunctionNativeReserved(JSObject* fun, size_t which,
453
const Value& val) {
454
MOZ_ASSERT(fun->as<JSFunction>().isNative());
455
MOZ_ASSERT_IF(val.isObject(),
456
val.toObject().compartment() == fun->compartment());
457
fun->as<JSFunction>().setExtendedSlot(which, val);
458
}
459
460
JS_FRIEND_API bool js::FunctionHasNativeReserved(JSObject* fun) {
461
MOZ_ASSERT(fun->as<JSFunction>().isNative());
462
return fun->as<JSFunction>().isExtended();
463
}
464
465
JS_FRIEND_API bool js::GetObjectProto(JSContext* cx, JS::Handle<JSObject*> obj,
466
JS::MutableHandle<JSObject*> proto) {
467
cx->check(obj);
468
469
if (IsProxy(obj)) {
470
return JS_GetPrototype(cx, obj, proto);
471
}
472
473
proto.set(reinterpret_cast<const shadow::Object*>(obj.get())->group->proto);
474
return true;
475
}
476
477
JS_FRIEND_API JSObject* js::GetStaticPrototype(JSObject* obj) {
478
MOZ_ASSERT(obj->hasStaticPrototype());
479
return obj->staticPrototype();
480
}
481
482
JS_FRIEND_API bool js::GetRealmOriginalEval(JSContext* cx,
483
MutableHandleObject eval) {
484
return GlobalObject::getOrCreateEval(cx, cx->global(), eval);
485
}
486
487
JS_FRIEND_API void js::SetReservedSlotWithBarrier(JSObject* obj, size_t slot,
488
const js::Value& value) {
489
if (IsProxy(obj)) {
490
obj->as<ProxyObject>().setReservedSlot(slot, value);
491
} else {
492
obj->as<NativeObject>().setSlot(slot, value);
493
}
494
}
495
496
void js::SetPreserveWrapperCallback(JSContext* cx,
497
PreserveWrapperCallback callback) {
498
cx->runtime()->preserveWrapperCallback = callback;
499
}
500
501
JS_FRIEND_API unsigned JS_PCToLineNumber(JSScript* script, jsbytecode* pc,
502
unsigned* columnp) {
503
return PCToLineNumber(script, pc, columnp);
504
}
505
506
JS_FRIEND_API bool JS_IsDeadWrapper(JSObject* obj) {
507
return IsDeadProxyObject(obj);
508
}
509
510
JS_FRIEND_API JSObject* JS_NewDeadWrapper(JSContext* cx, JSObject* origObj) {
511
return NewDeadProxyObject(cx, origObj);
512
}
513
514
void js::TraceWeakMaps(WeakMapTracer* trc) {
515
WeakMapBase::traceAllMappings(trc);
516
}
517
518
extern JS_FRIEND_API bool js::AreGCGrayBitsValid(JSRuntime* rt) {
519
return rt->gc.areGrayBitsValid();
520
}
521
522
JS_FRIEND_API bool js::ZoneGlobalsAreAllGray(JS::Zone* zone) {
523
for (RealmsInZoneIter realm(zone); !realm.done(); realm.next()) {
524
JSObject* obj = realm->unsafeUnbarrieredMaybeGlobal();
525
if (!obj || !JS::ObjectIsMarkedGray(obj)) {
526
return false;
527
}
528
}
529
return true;
530
}
531
532
JS_FRIEND_API bool js::IsCompartmentZoneSweepingOrCompacting(
533
JS::Compartment* comp) {
534
MOZ_ASSERT(comp);
535
return comp->zone()->isGCSweepingOrCompacting();
536
}
537
538
JS_FRIEND_API void js::VisitGrayWrapperTargets(Zone* zone,
539
GCThingCallback callback,
540
void* closure) {
541
for (CompartmentsInZoneIter comp(zone); !comp.done(); comp.next()) {
542
for (Compartment::ObjectWrapperEnum e(comp); !e.empty(); e.popFront()) {
543
JSObject* target = e.front().key();
544
if (target->isMarkedGray()) {
545
JS::AutoSuppressGCAnalysis nogc;
546
callback(closure, JS::GCCellPtr(target));
547
}
548
}
549
}
550
}
551
552
JS_FRIEND_API JSLinearString* js::StringToLinearStringSlow(JSContext* cx,
553
JSString* str) {
554
return str->ensureLinear(cx);
555
}
556
557
JS_FRIEND_API void JS_SetAccumulateTelemetryCallback(
558
JSContext* cx, JSAccumulateTelemetryDataCallback callback) {
559
cx->runtime()->setTelemetryCallback(cx->runtime(), callback);
560
}
561
562
JS_FRIEND_API void JS_SetSetUseCounterCallback(
563
JSContext* cx, JSSetUseCounterCallback callback) {
564
cx->runtime()->setUseCounterCallback(cx->runtime(), callback);
565
}
566
567
JS_FRIEND_API void JS_ReportFirstCompileTime(JS::HandleScript script,
568
mozilla::TimeDuration& parse,
569
mozilla::TimeDuration& emit) {
570
auto ss = script->scriptSource();
571
if (!ss) {
572
return;
573
}
574
parse = ss->parseTime();
575
emit = ss->emitTime();
576
}
577
578
JS_FRIEND_API JSObject* JS_CloneObject(JSContext* cx, HandleObject obj,
579
HandleObject protoArg) {
580
// |obj| might be in a different compartment.
581
cx->check(protoArg);
582
Rooted<TaggedProto> proto(cx, TaggedProto(protoArg.get()));
583
return CloneObject(cx, obj, proto);
584
}
585
586
#if defined(DEBUG) || defined(JS_JITSPEW)
587
588
// We don't want jsfriendapi.h to depend on GenericPrinter,
589
// so these functions are declared directly in the cpp.
590
591
namespace js {
592
593
extern JS_FRIEND_API void DumpString(JSString* str, js::GenericPrinter& out);
594
595
extern JS_FRIEND_API void DumpAtom(JSAtom* atom, js::GenericPrinter& out);
596
597
extern JS_FRIEND_API void DumpObject(JSObject* obj, js::GenericPrinter& out);
598
599
extern JS_FRIEND_API void DumpChars(const char16_t* s, size_t n,
600
js::GenericPrinter& out);
601
602
extern JS_FRIEND_API void DumpValue(const JS::Value& val,
603
js::GenericPrinter& out);
604
605
extern JS_FRIEND_API void DumpId(jsid id, js::GenericPrinter& out);
606
607
extern JS_FRIEND_API void DumpInterpreterFrame(
608
JSContext* cx, js::GenericPrinter& out, InterpreterFrame* start = nullptr);
609
610
} // namespace js
611
612
JS_FRIEND_API void js::DumpString(JSString* str, js::GenericPrinter& out) {
613
str->dump(out);
614
}
615
616
JS_FRIEND_API void js::DumpAtom(JSAtom* atom, js::GenericPrinter& out) {
617
atom->dump(out);
618
}
619
620
JS_FRIEND_API void js::DumpChars(const char16_t* s, size_t n,
621
js::GenericPrinter& out) {
622
out.printf("char16_t * (%p) = ", (void*)s);
623
JSString::dumpChars(s, n, out);
624
out.putChar('\n');
625
}
626
627
JS_FRIEND_API void js::DumpObject(JSObject* obj, js::GenericPrinter& out) {
628
if (!obj) {
629
out.printf("NULL\n");
630
return;
631
}
632
obj->dump(out);
633
}
634
635
JS_FRIEND_API void js::DumpString(JSString* str, FILE* fp) {
636
Fprinter out(fp);
637
js::DumpString(str, out);
638
}
639
640
JS_FRIEND_API void js::DumpAtom(JSAtom* atom, FILE* fp) {
641
Fprinter out(fp);
642
js::DumpAtom(atom, out);
643
}
644
645
JS_FRIEND_API void js::DumpChars(const char16_t* s, size_t n, FILE* fp) {
646
Fprinter out(fp);
647
js::DumpChars(s, n, out);
648
}
649
650
JS_FRIEND_API void js::DumpObject(JSObject* obj, FILE* fp) {
651
Fprinter out(fp);
652
js::DumpObject(obj, out);
653
}
654
655
JS_FRIEND_API void js::DumpId(jsid id, FILE* fp) {
656
Fprinter out(fp);
657
js::DumpId(id, out);
658
}
659
660
JS_FRIEND_API void js::DumpValue(const JS::Value& val, FILE* fp) {
661
Fprinter out(fp);
662
js::DumpValue(val, out);
663
}
664
665
JS_FRIEND_API void js::DumpString(JSString* str) { DumpString(str, stderr); }
666
JS_FRIEND_API void js::DumpAtom(JSAtom* atom) { DumpAtom(atom, stderr); }
667
JS_FRIEND_API void js::DumpObject(JSObject* obj) { DumpObject(obj, stderr); }
668
JS_FRIEND_API void js::DumpChars(const char16_t* s, size_t n) {
669
DumpChars(s, n, stderr);
670
}
671
JS_FRIEND_API void js::DumpValue(const JS::Value& val) {
672
DumpValue(val, stderr);
673
}
674
JS_FRIEND_API void js::DumpId(jsid id) { DumpId(id, stderr); }
675
JS_FRIEND_API void js::DumpInterpreterFrame(JSContext* cx,
676
InterpreterFrame* start) {
677
Fprinter out(stderr);
678
DumpInterpreterFrame(cx, out, start);
679
}
680
JS_FRIEND_API bool js::DumpPC(JSContext* cx) { return DumpPC(cx, stdout); }
681
JS_FRIEND_API bool js::DumpScript(JSContext* cx, JSScript* scriptArg) {
682
return DumpScript(cx, scriptArg, stdout);
683
}
684
685
#endif
686
687
static const char* FormatValue(JSContext* cx, HandleValue v,
688
UniqueChars& bytes) {
689
if (v.isMagic()) {
690
MOZ_ASSERT(v.whyMagic() == JS_OPTIMIZED_OUT ||
691
v.whyMagic() == JS_UNINITIALIZED_LEXICAL);
692
return "[unavailable]";
693
}
694
695
if (IsCallable(v)) {
696
return "[function]";
697
}
698
699
if (v.isObject() && IsCrossCompartmentWrapper(&v.toObject())) {
700
return "[cross-compartment wrapper]";
701
}
702
703
JSString* str;
704
{
705
mozilla::Maybe<AutoRealm> ar;
706
if (v.isObject()) {
707
ar.emplace(cx, &v.toObject());
708
}
709
710
str = ToString<CanGC>(cx, v);
711
if (!str) {
712
return nullptr;
713
}
714
}
715
716
bytes = QuoteString(cx, str, '"');
717
return bytes.get();
718
}
719
720
static bool FormatFrame(JSContext* cx, const FrameIter& iter, Sprinter& sp,
721
int num, bool showArgs, bool showLocals,
722
bool showThisProps) {
723
MOZ_ASSERT(!cx->isExceptionPending());
724
RootedScript script(cx, iter.script());
725
jsbytecode* pc = iter.pc();
726
727
RootedObject envChain(cx, iter.environmentChain(cx));
728
JSAutoRealm ar(cx, envChain);
729
730
const char* filename = script->filename();
731
unsigned column = 0;
732
unsigned lineno = PCToLineNumber(script, pc, &column);
733
RootedFunction fun(cx, iter.maybeCallee(cx));
734
RootedString funname(cx);
735
if (fun) {
736
funname = fun->displayAtom();
737
}
738
739
RootedValue thisVal(cx);
740
if (iter.hasUsableAbstractFramePtr() && iter.isFunctionFrame() && fun &&
741
!fun->isArrow() && !fun->isDerivedClassConstructor() &&
742
!(fun->isBoundFunction() && iter.isConstructing())) {
743
if (!GetFunctionThis(cx, iter.abstractFramePtr(), &thisVal)) {
744
return false;
745
}
746
}
747
748
// print the frame number and function name
749
if (funname) {
750
UniqueChars funbytes = QuoteString(cx, funname);
751
if (!funbytes) {
752
return false;
753
}
754
if (!sp.printf("%d %s(", num, funbytes.get())) {
755
return false;
756
}
757
} else if (fun) {
758
if (!sp.printf("%d anonymous(", num)) {
759
return false;
760
}
761
} else {
762
if (!sp.printf("%d <TOP LEVEL>", num)) {
763
return false;
764
}
765
}
766
767
if (showArgs && iter.hasArgs()) {
768
PositionalFormalParameterIter fi(script);
769
bool first = true;
770
for (unsigned i = 0; i < iter.numActualArgs(); i++) {
771
RootedValue arg(cx);
772
if (i < iter.numFormalArgs() && fi.closedOver()) {
773
if (iter.hasInitialEnvironment(cx)) {
774
arg = iter.callObj(cx).aliasedBinding(fi);
775
} else {
776
arg = MagicValue(JS_OPTIMIZED_OUT);
777
}
778
} else if (iter.hasUsableAbstractFramePtr()) {
779
if (script->analyzedArgsUsage() && script->argsObjAliasesFormals() &&
780
iter.hasArgsObj()) {
781
arg = iter.argsObj().arg(i);
782
} else {
783
arg = iter.unaliasedActual(i, DONT_CHECK_ALIASING);
784
}
785
} else {
786
arg = MagicValue(JS_OPTIMIZED_OUT);
787
}
788
789
UniqueChars valueBytes;
790
const char* value = FormatValue(cx, arg, valueBytes);
791
if (!value) {
792
if (cx->isThrowingOutOfMemory()) {
793
return false;
794
}
795
cx->clearPendingException();
796
}
797
798
UniqueChars nameBytes;
799
const char* name = nullptr;
800
801
if (i < iter.numFormalArgs()) {
802
MOZ_ASSERT(fi.argumentSlot() == i);
803
if (!fi.isDestructured()) {
804
nameBytes = StringToNewUTF8CharsZ(cx, *fi.name());
805
name = nameBytes.get();
806
if (!name) {
807
return false;
808
}
809
} else {
810
name = "(destructured parameter)";
811
}
812
fi++;
813
}
814
815
if (value) {
816
if (!sp.printf("%s%s%s%s%s%s", !first ? ", " : "", name ? name : "",
817
name ? " = " : "", arg.isString() ? "\"" : "", value,
818
arg.isString() ? "\"" : "")) {
819
return false;
820
}
821
822
first = false;
823
} else {
824
if (!sp.put(" <Failed to get argument while inspecting stack "
825
"frame>\n")) {
826
return false;
827
}
828
}
829
}
830
}
831
832
// print filename, line number and column
833
if (!sp.printf("%s [\"%s\":%d:%d]\n", fun ? ")" : "",
834
filename ? filename : "<unknown>", lineno, column)) {
835
return false;
836
}
837
838
// Note: Right now we don't dump the local variables anymore, because
839
// that is hard to support across all the JITs etc.
840
841
// print the value of 'this'
842
if (showLocals) {
843
if (!thisVal.isUndefined()) {
844
RootedString thisValStr(cx, ToString<CanGC>(cx, thisVal));
845
if (!thisValStr) {
846
if (cx->isThrowingOutOfMemory()) {
847
return false;
848
}
849
cx->clearPendingException();
850
}
851
if (thisValStr) {
852
UniqueChars thisValBytes = QuoteString(cx, thisValStr);
853
if (!thisValBytes) {
854
return false;
855
}
856
if (!sp.printf(" this = %s\n", thisValBytes.get())) {
857
return false;
858
}
859
} else {
860
if (!sp.put(" <failed to get 'this' value>\n")) {
861
return false;
862
}
863
}
864
}
865
}
866
867
if (showThisProps && thisVal.isObject()) {
868
RootedObject obj(cx, &thisVal.toObject());
869
870
RootedIdVector keys(cx);
871
if (!GetPropertyKeys(cx, obj, JSITER_OWNONLY, &keys)) {
872
if (cx->isThrowingOutOfMemory()) {
873
return false;
874
}
875
cx->clearPendingException();
876
}
877
878
for (size_t i = 0; i < keys.length(); i++) {
879
RootedId id(cx, keys[i]);
880
RootedValue key(cx, IdToValue(id));
881
RootedValue v(cx);
882
883
if (!GetProperty(cx, obj, obj, id, &v)) {
884
if (cx->isThrowingOutOfMemory()) {
885
return false;
886
}
887
cx->clearPendingException();
888
if (!sp.put(" <Failed to fetch property while inspecting stack "
889
"frame>\n")) {
890
return false;
891
}
892
continue;
893
}
894
895
UniqueChars nameBytes;
896
const char* name = FormatValue(cx, key, nameBytes);
897
if (!name) {
898
if (cx->isThrowingOutOfMemory()) {
899
return false;
900
}
901
cx->clearPendingException();
902
}
903
904
UniqueChars valueBytes;
905
const char* value = FormatValue(cx, v, valueBytes);
906
if (!value) {
907
if (cx->isThrowingOutOfMemory()) {
908
return false;
909
}
910
cx->clearPendingException();
911
}
912
913
if (name && value) {
914
if (!sp.printf(" this.%s = %s%s%s\n", name, v.isString() ? "\"" : "",
915
value, v.isString() ? "\"" : "")) {
916
return false;
917
}
918
} else {
919
if (!sp.put(" <Failed to format values while inspecting stack "
920
"frame>\n")) {
921
return false;
922
}
923
}
924
}
925
}
926
927
MOZ_ASSERT(!cx->isExceptionPending());
928
return true;
929
}
930
931
static bool FormatWasmFrame(JSContext* cx, const FrameIter& iter, Sprinter& sp,
932
int num) {
933
UniqueChars nameStr;
934
if (JSAtom* functionDisplayAtom = iter.maybeFunctionDisplayAtom()) {
935
nameStr = StringToNewUTF8CharsZ(cx, *functionDisplayAtom);
936
if (!nameStr) {
937
return false;
938
}
939
}
940
941
if (!sp.printf("%d %s()", num, nameStr ? nameStr.get() : "<wasm-function>")) {
942
return false;
943
}
944
945
if (!sp.printf(" [\"%s\":wasm-function[%d]:0x%x]\n",
946
iter.filename() ? iter.filename() : "<unknown>",
947
iter.wasmFuncIndex(), iter.wasmBytecodeOffset())) {
948
return false;
949
}
950
951
MOZ_ASSERT(!cx->isExceptionPending());
952
return true;
953
}
954
955
JS_FRIEND_API JS::UniqueChars JS::FormatStackDump(JSContext* cx, bool showArgs,
956
bool showLocals,
957
bool showThisProps) {
958
int num = 0;
959
960
Sprinter sp(cx);
961
if (!sp.init()) {
962
return nullptr;
963
}
964
965
for (AllFramesIter i(cx); !i.done(); ++i) {
966
bool ok = i.hasScript() ? FormatFrame(cx, i, sp, num, showArgs, showLocals,
967
showThisProps)
968
: FormatWasmFrame(cx, i, sp, num);
969
if (!ok) {
970
return nullptr;
971
}
972
num++;
973
}
974
975
if (num == 0) {
976
if (!sp.put("JavaScript stack is empty\n")) {
977
return nullptr;
978
}
979
}
980
981
return sp.release();
982
}
983
984
extern JS_FRIEND_API bool JS::ForceLexicalInitialization(JSContext* cx,
985
HandleObject obj) {
986
AssertHeapIsIdle();
987
CHECK_THREAD(cx);
988
cx->check(obj);
989
990
bool initializedAny = false;
991
NativeObject* nobj = &obj->as<NativeObject>();
992
993
for (Shape::Range<NoGC> r(nobj->lastProperty()); !r.empty(); r.popFront()) {
994
Shape* s = &r.front();
995
Value v = nobj->getSlot(s->slot());
996
if (s->isDataProperty() && v.isMagic() &&
997
v.whyMagic() == JS_UNINITIALIZED_LEXICAL) {
998
nobj->setSlot(s->slot(), UndefinedValue());
999
initializedAny = true;
1000
}
1001
}
1002
return initializedAny;
1003
}
1004
1005
extern JS_FRIEND_API int JS::IsGCPoisoning() {
1006
#ifdef JS_GC_POISONING
1007
return !js::gDisablePoisoning;
1008
#else
1009
return false;
1010
#endif
1011
}
1012
1013
struct DumpHeapTracer final : public JS::CallbackTracer, public WeakMapTracer {
1014
const char* prefix;
1015
FILE* output;
1016
mozilla::MallocSizeOf mallocSizeOf;
1017
1018
DumpHeapTracer(FILE* fp, JSContext* cx, mozilla::MallocSizeOf mallocSizeOf)
1019
: JS::CallbackTracer(cx, DoNotTraceWeakMaps),
1020
js::WeakMapTracer(cx->runtime()),
1021
prefix(""),
1022
output(fp),
1023
mallocSizeOf(mallocSizeOf) {}
1024
1025
private:
1026
void trace(JSObject* map, JS::GCCellPtr key, JS::GCCellPtr value) override {
1027
JSObject* kdelegate = nullptr;
1028
if (key.is<JSObject>()) {
1029
kdelegate = UncheckedUnwrapWithoutExpose(&key.as<JSObject>());
1030
}
1031
1032
fprintf(output, "WeakMapEntry map=%p key=%p keyDelegate=%p value=%p\n", map,
1033
key.asCell(), kdelegate, value.asCell());
1034
}
1035
1036
bool onChild(const JS::GCCellPtr& thing) override;
1037
};
1038
1039
static char MarkDescriptor(void* thing) {
1040
gc::TenuredCell* cell = gc::TenuredCell::fromPointer(thing);
1041
if (cell->isMarkedBlack()) {
1042
return 'B';
1043
}
1044
if (cell->isMarkedGray()) {
1045
return 'G';
1046
}
1047
if (cell->isMarkedAny()) {
1048
return 'X';
1049
}
1050
return 'W';
1051
}
1052
1053
static void DumpHeapVisitZone(JSRuntime* rt, void* data, Zone* zone) {
1054
DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
1055
fprintf(dtrc->output, "# zone %p\n", (void*)zone);
1056
}
1057
1058
static void DumpHeapVisitRealm(JSContext* cx, void* data,
1059
Handle<Realm*> realm) {
1060
char name[1024];
1061
if (auto nameCallback = cx->runtime()->realmNameCallback) {
1062
nameCallback(cx, realm, name, sizeof(name));
1063
} else {
1064
strcpy(name, "<unknown>");
1065
}
1066
1067
DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
1068
fprintf(dtrc->output, "# realm %s [in compartment %p, zone %p]\n", name,
1069
(void*)realm->compartment(), (void*)realm->zone());
1070
}
1071
1072
static void DumpHeapVisitArena(JSRuntime* rt, void* data, gc::Arena* arena,
1073
JS::TraceKind traceKind, size_t thingSize) {
1074
DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
1075
fprintf(dtrc->output, "# arena allockind=%u size=%u\n",
1076
unsigned(arena->getAllocKind()), unsigned(thingSize));
1077
}
1078
1079
static void DumpHeapVisitCell(JSRuntime* rt, void* data, JS::GCCellPtr cellptr,
1080
size_t thingSize) {
1081
DumpHeapTracer* dtrc = static_cast<DumpHeapTracer*>(data);
1082
char cellDesc[1024 * 32];
1083
JS_GetTraceThingInfo(cellDesc, sizeof(cellDesc), dtrc, cellptr.asCell(),
1084
cellptr.kind(), true);
1085
1086
fprintf(dtrc->output, "%p %c %s", cellptr.asCell(),
1087
MarkDescriptor(cellptr.asCell()), cellDesc);
1088
if (dtrc->mallocSizeOf) {
1089
auto size = JS::ubi::Node(cellptr).size(dtrc->mallocSizeOf);
1090
fprintf(dtrc->output, " SIZE:: %" PRIu64 "\n", size);
1091
} else {
1092
fprintf(dtrc->output, "\n");
1093
}
1094
1095
js::TraceChildren(dtrc, cellptr.asCell(), cellptr.kind());
1096
}
1097
1098
bool DumpHeapTracer::onChild(const JS::GCCellPtr& thing) {
1099
if (gc::IsInsideNursery(thing.asCell())) {
1100
return true;
1101
}
1102
1103
char buffer[1024];
1104
getTracingEdgeName(buffer, sizeof(buffer));
1105
fprintf(output, "%s%p %c %s\n", prefix, thing.asCell(),
1106
MarkDescriptor(thing.asCell()), buffer);
1107
return true;
1108
}
1109
1110
void js::DumpHeap(JSContext* cx, FILE* fp,
1111
js::DumpHeapNurseryBehaviour nurseryBehaviour,
1112
mozilla::MallocSizeOf mallocSizeOf) {
1113
if (nurseryBehaviour == js::CollectNurseryBeforeDump) {
1114
cx->runtime()->gc.evictNursery(JS::GCReason::API);
1115
}
1116
1117
DumpHeapTracer dtrc(fp, cx, mallocSizeOf);
1118
1119
fprintf(dtrc.output, "# Roots.\n");
1120
TraceRuntimeWithoutEviction(&dtrc);
1121
1122
fprintf(dtrc.output, "# Weak maps.\n");
1123
WeakMapBase::traceAllMappings(&dtrc);
1124
1125
fprintf(dtrc.output, "==========\n");
1126
1127
dtrc.prefix = "> ";
1128
IterateHeapUnbarriered(cx, &dtrc, DumpHeapVisitZone, DumpHeapVisitRealm,
1129
DumpHeapVisitArena, DumpHeapVisitCell);
1130
1131
fflush(dtrc.output);
1132
}
1133
1134
JS_FRIEND_API void JS::NotifyGCRootsRemoved(JSContext* cx) {
1135
cx->runtime()->gc.notifyRootsRemoved();
1136
}
1137
1138
JS_FRIEND_API JS::Realm* js::GetAnyRealmInZone(JS::Zone* zone) {
1139
if (zone->isAtomsZone()) {
1140
return nullptr;
1141
}
1142
1143
RealmsInZoneIter realm(zone);
1144
MOZ_ASSERT(!realm.done());
1145
return realm.get();
1146
}
1147
1148
JS_FRIEND_API bool js::IsSharableCompartment(JS::Compartment* comp) {
1149
// If this compartment has nuked outgoing wrappers (because all its globals
1150
// got nuked), we won't be able to create any useful CCWs out of it in the
1151
// future, and so we shouldn't use it for any new globals.
1152
if (comp->nukedOutgoingWrappers) {
1153
return false;
1154
}
1155
1156
// If this compartment has no live globals, it might be in the middle of being
1157
// GCed. Don't create any new Realms inside. There's no point to doing that
1158
// anyway, since the idea would be to avoid CCWs from existing Realms in the
1159
// compartment to the new Realm, and there are no existing Realms.
1160
if (!CompartmentHasLiveGlobal(comp)) {
1161
return false;
1162
}
1163
1164
// Good to go.
1165
return true;
1166
}
1167
1168
JS_FRIEND_API JSObject* js::GetTestingFunctions(JSContext* cx) {
1169
RootedObject obj(cx, JS_NewPlainObject(cx));
1170
if (!obj) {
1171
return nullptr;
1172
}
1173
1174
if (!DefineTestingFunctions(cx, obj, false, false)) {
1175
return nullptr;
1176
}
1177
1178
return obj;
1179
}
1180
1181
JS_FRIEND_API void js::SetDOMCallbacks(JSContext* cx,
1182
const DOMCallbacks* callbacks) {
1183
cx->runtime()->DOMcallbacks = callbacks;
1184
}
1185
1186
JS_FRIEND_API const DOMCallbacks* js::GetDOMCallbacks(JSContext* cx) {
1187
return cx->runtime()->DOMcallbacks;
1188
}
1189
1190
static const void* gDOMProxyHandlerFamily = nullptr;
1191
static DOMProxyShadowsCheck gDOMProxyShadowsCheck;
1192
static const void* gDOMRemoteProxyHandlerFamily = nullptr;
1193
1194
JS_FRIEND_API void js::SetDOMProxyInformation(
1195
const void* domProxyHandlerFamily,
1196
DOMProxyShadowsCheck domProxyShadowsCheck,
1197
const void* domRemoteProxyHandlerFamily) {
1198
gDOMProxyHandlerFamily = domProxyHandlerFamily;
1199
gDOMProxyShadowsCheck = domProxyShadowsCheck;
1200
gDOMRemoteProxyHandlerFamily = domRemoteProxyHandlerFamily;
1201
}
1202
1203
const void* js::GetDOMProxyHandlerFamily() { return gDOMProxyHandlerFamily; }
1204
1205
DOMProxyShadowsCheck js::GetDOMProxyShadowsCheck() {
1206
return gDOMProxyShadowsCheck;
1207
}
1208
1209
const void* js::GetDOMRemoteProxyHandlerFamily() {
1210
return gDOMRemoteProxyHandlerFamily;
1211
}
1212
1213
JS_FRIEND_API bool js::IsDOMRemoteProxyObject(JSObject* object) {
1214
return js::IsProxy(object) && js::GetProxyHandler(object)->family() ==
1215
js::GetDOMRemoteProxyHandlerFamily();
1216
}
1217
1218
static XrayJitInfo* gXrayJitInfo = nullptr;
1219
1220
JS_FRIEND_API void js::SetXrayJitInfo(XrayJitInfo* info) {
1221
gXrayJitInfo = info;
1222
}
1223
1224
XrayJitInfo* js::GetXrayJitInfo() { return gXrayJitInfo; }
1225
1226
bool js::detail::IdMatchesAtom(jsid id, JSAtom* atom) {
1227
return id == INTERNED_STRING_TO_JSID(nullptr, atom);
1228
}
1229
1230
bool js::detail::IdMatchesAtom(jsid id, JSString* atom) {
1231
return id == INTERNED_STRING_TO_JSID(nullptr, atom);
1232
}
1233
1234
JS_FRIEND_API void js::PrepareScriptEnvironmentAndInvoke(
1235
JSContext* cx, HandleObject global,
1236
ScriptEnvironmentPreparer::Closure& closure) {
1237
MOZ_ASSERT(!cx->isExceptionPending());
1238
MOZ_ASSERT(global->is<GlobalObject>());
1239
1240
MOZ_RELEASE_ASSERT(
1241
cx->runtime()->scriptEnvironmentPreparer,
1242
"Embedding needs to set a scriptEnvironmentPreparer callback");
1243
1244
cx->runtime()->scriptEnvironmentPreparer->invoke(global, closure);
1245
}
1246
1247
JS_FRIEND_API void js::SetScriptEnvironmentPreparer(
1248
JSContext* cx, ScriptEnvironmentPreparer* preparer) {
1249
cx->runtime()->scriptEnvironmentPreparer = preparer;
1250
}
1251
1252
JS_FRIEND_API void js::SetCTypesActivityCallback(JSContext* cx,
1253
CTypesActivityCallback cb) {
1254
cx->runtime()->ctypesActivityCallback = cb;
1255
}
1256
1257
js::AutoCTypesActivityCallback::AutoCTypesActivityCallback(
1258
JSContext* cx, js::CTypesActivityType beginType,
1259
js::CTypesActivityType endType MOZ_GUARD_OBJECT_NOTIFIER_PARAM_IN_IMPL)
1260
: cx(cx),
1261
callback(cx->runtime()->ctypesActivityCallback),
1262
endType(endType) {
1263
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
1264
1265
if (callback) {
1266
callback(cx, beginType);
1267
}
1268
}
1269
1270
JS_FRIEND_API void js::SetAllocationMetadataBuilder(
1271
JSContext* cx, const AllocationMetadataBuilder* callback) {
1272
cx->realm()->setAllocationMetadataBuilder(callback);
1273
}
1274
1275
JS_FRIEND_API JSObject* js::GetAllocationMetadata(JSObject* obj) {
1276
ObjectWeakMap* map = ObjectRealm::get(obj).objectMetadataTable.get();
1277
if (map) {
1278
return map->lookup(obj);
1279
}
1280
return nullptr;
1281
}
1282
1283
JS_FRIEND_API bool js::ReportIsNotFunction(JSContext* cx, HandleValue v) {
1284
cx->check(v);
1285
return ReportIsNotFunction(cx, v, -1);
1286
}
1287
1288
#ifdef DEBUG
1289
bool js::HasObjectMovedOp(JSObject* obj) {
1290
return !!GetObjectClass(obj)->extObjectMovedOp();
1291
}
1292
#endif
1293
1294
JS_FRIEND_API bool js::ForwardToNative(JSContext* cx, JSNative native,
1295
const CallArgs& args) {
1296
return native(cx, args.length(), args.base());
1297
}
1298
1299
JS_FRIEND_API JSObject* js::ConvertArgsToArray(JSContext* cx,
1300
const CallArgs& args) {
1301
RootedObject argsArray(cx,
1302
NewDenseCopiedArray(cx, args.length(), args.array()));
1303
return argsArray;
1304
}
1305
1306
JS_FRIEND_API JSAtom* js::GetPropertyNameFromPC(JSScript* script,
1307
jsbytecode* pc) {
1308
if (!IsGetPropPC(pc) && !IsSetPropPC(pc)) {
1309
return nullptr;
1310
}
1311
return script->getName(pc);
1312
}
1313
1314
JS_FRIEND_API void js::SetWindowProxyClass(JSContext* cx,
1315
const JSClass* clasp) {
1316
MOZ_ASSERT(!cx->runtime()->maybeWindowProxyClass());
1317
cx->runtime()->setWindowProxyClass(clasp);
1318
}
1319
1320
JS_FRIEND_API void js::SetWindowProxy(JSContext* cx, HandleObject global,
1321
HandleObject windowProxy) {
1322
AssertHeapIsIdle();
1323
CHECK_THREAD(cx);
1324
1325
cx->check(global, windowProxy);
1326
MOZ_ASSERT(IsWindowProxy(windowProxy));
1327
1328
GlobalObject& globalObj = global->as<GlobalObject>();
1329
globalObj.setWindowProxy(windowProxy);
1330
globalObj.lexicalEnvironment().setWindowProxyThisValue(windowProxy);
1331
}
1332
1333
JS_FRIEND_API JSObject* js::ToWindowIfWindowProxy(JSObject* obj) {
1334
if (IsWindowProxy(obj)) {
1335
return &obj->nonCCWGlobal();
1336
}
1337
return obj;
1338
}
1339
1340
JS_FRIEND_API JSObject* js::detail::ToWindowProxyIfWindowSlow(JSObject* obj) {
1341
if (JSObject* windowProxy = obj->as<GlobalObject>().maybeWindowProxy()) {
1342
return windowProxy;
1343
}
1344
return obj;
1345
}
1346
1347
JS_FRIEND_API bool js::IsWindowProxy(JSObject* obj) {
1348
// Note: simply checking `obj == obj->global().windowProxy()` is not
1349
// sufficient: we may have transplanted the window proxy with a CCW.
1350
// Check the Class to ensure we really have a window proxy.
1351
return obj->getClass() ==
1352
obj->runtimeFromAnyThread()->maybeWindowProxyClass();
1353
}
1354
1355
JS_FRIEND_API bool js::detail::IsWindowSlow(JSObject* obj) {
1356
return obj->as<GlobalObject>().maybeWindowProxy();
1357
}
1358
1359
AutoAssertNoContentJS::AutoAssertNoContentJS(JSContext* cx)
1360
: context_(cx), prevAllowContentJS_(cx->runtime()->allowContentJS_) {
1361
cx->runtime()->allowContentJS_ = false;
1362
}
1363
1364
AutoAssertNoContentJS::~AutoAssertNoContentJS() {
1365
context_->runtime()->allowContentJS_ = prevAllowContentJS_;
1366
}
1367
1368
JS_FRIEND_API void js::EnableAccessValidation(JSContext* cx, bool enabled) {
1369
cx->enableAccessValidation = enabled;
1370
}
1371
1372
JS_FRIEND_API void js::SetRealmValidAccessPtr(JSContext* cx,
1373
JS::HandleObject global,
1374
bool* accessp) {
1375
MOZ_ASSERT(global->is<GlobalObject>());
1376
global->as<GlobalObject>().realm()->setValidAccessPtr(accessp);
1377
}
1378
1379
JS_FRIEND_API bool js::SystemZoneAvailable(JSContext* cx) { return true; }
1380
1381
static LogCtorDtor sLogCtor = nullptr;
1382
static LogCtorDtor sLogDtor = nullptr;
1383
1384
JS_FRIEND_API void js::SetLogCtorDtorFunctions(LogCtorDtor ctor,
1385
LogCtorDtor dtor) {
1386
MOZ_ASSERT(!sLogCtor && !sLogDtor);
1387
MOZ_ASSERT(ctor && dtor);
1388
sLogCtor = ctor;
1389
sLogDtor = dtor;
1390
}
1391
1392
JS_FRIEND_API void js::LogCtor(void* self, const char* type, uint32_t sz) {
1393
if (LogCtorDtor fun = sLogCtor) {
1394
fun(self, type, sz);
1395
}
1396
}
1397
1398
JS_FRIEND_API void js::LogDtor(void* self, const char* type, uint32_t sz) {
1399
if (LogCtorDtor fun = sLogDtor) {
1400
fun(self, type, sz);
1401
}
1402
}
1403
1404
JS_FRIEND_API JS::Value js::MaybeGetScriptPrivate(JSObject* object) {
1405
if (!object->is<ScriptSourceObject>()) {
1406
return UndefinedValue();
1407
}
1408
1409
return object->as<ScriptSourceObject>().canonicalPrivate();
1410
}
1411
1412
JS_FRIEND_API uint64_t js::GetGCHeapUsageForObjectZone(JSObject* obj) {
1413
return obj->zone()->gcHeapSize.bytes();
1414
}
1415
1416
#ifdef DEBUG
1417
JS_FRIEND_API bool js::RuntimeIsBeingDestroyed() {
1418
JSRuntime* runtime = TlsContext.get()->runtime();
1419
MOZ_ASSERT(js::CurrentThreadCanAccessRuntime(runtime));
1420
return runtime->isBeingDestroyed();
1421
}
1422
#endif