Debian Codegraph @

js/src/debugger/Object.cpp

raw
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 "debugger/Object-inl.h"
8
9 #include "mozilla/Maybe.h" // for Maybe, Nothing, Some
10 #include "mozilla/Range.h" // for Range
11 #include "mozilla/Result.h" // for Result
12 #include "mozilla/Vector.h" // for Vector
13
14 #include <algorithm>
15 #include <string.h> // for size_t, strlen
16 #include <type_traits> // for remove_reference<>::type
17 #include <utility> // for move
18
19 #include "jsapi.h" // for CallArgs, RootedObject, Rooted
20
21 #include "builtin/Array.h" // for NewDenseCopiedArray
22 #include "builtin/Promise.h" // for PromiseReactionRecordBuilder
23 #include "debugger/Debugger.h" // for Completion, Debugger
24 #include "debugger/Frame.h" // for DebuggerFrame
25 #include "debugger/NoExecute.h" // for LeaveDebuggeeNoExecute
26 #include "debugger/Script.h" // for DebuggerScript
27 #include "debugger/Source.h" // for DebuggerSource
28 #include "gc/Tracer.h" // for TraceManuallyBarrieredCrossCompartmentEdge
29 #include "js/ColumnNumber.h" // JS::ColumnNumberOneOrigin
30 #include "js/CompilationAndEvaluation.h" // for Compile
31 #include "js/Conversions.h" // for ToObject
32 #include "js/experimental/JitInfo.h" // for JSJitInfo
33 #include "js/friend/ErrorMessages.h" // for GetErrorMessage, JSMSG_*
34 #include "js/friend/WindowProxy.h" // for IsWindow, IsWindowProxy, ToWindowIfWindowProxy
35 #include "js/HeapAPI.h" // for IsInsideNursery
36 #include "js/Promise.h" // for PromiseState
37 #include "js/PropertyAndElement.h" // for JS_GetProperty
38 #include "js/Proxy.h" // for PropertyDescriptor
39 #include "js/SourceText.h" // for SourceText
40 #include "js/StableStringChars.h" // for AutoStableStringChars
41 #include "js/String.h" // for JS::StringHasLatin1Chars
42 #include "proxy/ScriptedProxyHandler.h" // for ScriptedProxyHandler
43 #include "vm/ArgumentsObject.h" // for ARGS_LENGTH_MAX
44 #include "vm/ArrayObject.h" // for ArrayObject
45 #include "vm/AsyncFunction.h" // for AsyncGeneratorObject
46 #include "vm/AsyncIteration.h" // for AsyncFunctionGeneratorObject
47 #include "vm/BoundFunctionObject.h" // for BoundFunctionObject
48 #include "vm/BytecodeUtil.h" // for JSDVG_SEARCH_STACK
49 #include "vm/Compartment.h" // for Compartment
50 #include "vm/EnvironmentObject.h" // for GetDebugEnvironmentForFunction
51 #include "vm/ErrorObject.h" // for JSObject::is, ErrorObject
52 #include "vm/GeneratorObject.h" // for AbstractGeneratorObject
53 #include "vm/GlobalObject.h" // for JSObject::is, GlobalObject
54 #include "vm/Interpreter.h" // for Call
55 #include "vm/JSAtomUtils.h" // for Atomize, AtomizeString
56 #include "vm/JSContext.h" // for JSContext, ReportValueError
57 #include "vm/JSFunction.h" // for JSFunction
58 #include "vm/JSObject.h" // for GenericObject, NewObjectKind
59 #include "vm/JSScript.h" // for JSScript
60 #include "vm/NativeObject.h" // for NativeObject, JSObject::is
61 #include "vm/ObjectOperations.h" // for DefineProperty
62 #include "vm/PlainObject.h" // for js::PlainObject
63 #include "vm/PromiseObject.h" // for js::PromiseObject
64 #include "vm/Realm.h" // for AutoRealm, ErrorCopier, Realm
65 #include "vm/Runtime.h" // for JSAtomState
66 #include "vm/SavedFrame.h" // for SavedFrame
67 #include "vm/Scope.h" // for PositionalFormalParameterIter
68 #include "vm/SelfHosting.h" // for GetClonedSelfHostedFunctionName
69 #include "vm/Shape.h" // for Shape
70 #include "vm/Stack.h" // for InvokeArgs
71 #include "vm/StringType.h" // for JSAtom, PropertyName
72 #include "vm/WrapperObject.h" // for JSObject::is, WrapperObject
73
74 #include "gc/StableCellHasher-inl.h"
75 #include "vm/Compartment-inl.h" // for Compartment::wrap
76 #include "vm/JSObject-inl.h" // for GetObjectClassName, InitClass, NewObjectWithGivenProtoAndKind, ToPropertyKey
77 #include "vm/NativeObject-inl.h" // for NativeObject::global
78 #include "vm/ObjectOperations-inl.h" // for DeleteProperty, GetProperty
79 #include "vm/Realm-inl.h" // for AutoRealm::AutoRealm
80
81 using namespace js;
82
83 using JS::AutoStableStringChars;
84 using mozilla::Maybe;
85 using mozilla::Nothing;
86 using mozilla::Some;
87
88 const JSClassOps DebuggerObject::classOps_ = {
89 nullptr, // addProperty
90 nullptr, // delProperty
91 nullptr, // enumerate
92 nullptr, // newEnumerate
93 nullptr, // resolve
94 nullptr, // mayResolve
95 nullptr, // finalize
96 nullptr, // call
97 nullptr, // construct
98 CallTraceMethod<DebuggerObject>, // trace
99 };
100
101 const JSClass DebuggerObject::class_ = {
102 "Object",
103 JSCLASS_HAS_RESERVED_SLOTS(RESERVED_SLOTS),
104 &classOps_,
105 };
106
107 void DebuggerObject::trace(JSTracer* trc) {
108 // There is a barrier on private pointers, so the Unbarriered marking
109 // is okay.
110 if (JSObject* referent = maybeReferent()) {
111 TraceManuallyBarrieredCrossCompartmentEdge(trc, this, &referent,
112 "Debugger.Object referent");
113 if (referent != maybeReferent()) {
114 setReservedSlotGCThingAsPrivateUnbarriered(OBJECT_SLOT, referent);
115 }
116 }
117 }
118
119 static DebuggerObject* DebuggerObject_checkThis(JSContext* cx,
120 const CallArgs& args) {
121 JSObject* thisobj = RequireObject(cx, args.thisv());
122 if (!thisobj) {
123 return nullptr;
124 }
125 if (!thisobj->is<DebuggerObject>()) {
126 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
127 JSMSG_INCOMPATIBLE_PROTO, "Debugger.Object",
128 "method", thisobj->getClass()->name);
129 return nullptr;
130 }
131
132 return &thisobj->as<DebuggerObject>();
133 }
134
135 /* static */
136 bool DebuggerObject::construct(JSContext* cx, unsigned argc, Value* vp) {
137 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_NO_CONSTRUCTOR,
138 "Debugger.Object");
139 return false;
140 }
141
142 struct MOZ_STACK_CLASS DebuggerObject::CallData {
143 JSContext* cx;
144 const CallArgs& args;
145
146 Handle<DebuggerObject*> object;
147 RootedObject referent;
148
149 CallData(JSContext* cx, const CallArgs& args, Handle<DebuggerObject*> obj)
150 : cx(cx), args(args), object(obj), referent(cx, obj->referent()) {}
151
152 // JSNative properties
153 bool callableGetter();
154 bool isBoundFunctionGetter();
155 bool isArrowFunctionGetter();
156 bool isAsyncFunctionGetter();
157 bool isClassConstructorGetter();
158 bool isGeneratorFunctionGetter();
159 bool protoGetter();
160 bool classGetter();
161 bool nameGetter();
162 bool displayNameGetter();
163 bool parameterNamesGetter();
164 bool scriptGetter();
165 bool environmentGetter();
166 bool boundTargetFunctionGetter();
167 bool boundThisGetter();
168 bool boundArgumentsGetter();
169 bool allocationSiteGetter();
170 bool isErrorGetter();
171 bool isMutedErrorGetter();
172 bool errorMessageNameGetter();
173 bool errorNotesGetter();
174 bool errorLineNumberGetter();
175 bool errorColumnNumberGetter();
176 bool isProxyGetter();
177 bool proxyTargetGetter();
178 bool proxyHandlerGetter();
179 bool isPromiseGetter();
180 bool promiseStateGetter();
181 bool promiseValueGetter();
182 bool promiseReasonGetter();
183 bool promiseLifetimeGetter();
184 bool promiseTimeToResolutionGetter();
185 bool promiseAllocationSiteGetter();
186 bool promiseResolutionSiteGetter();
187 bool promiseIDGetter();
188 bool promiseDependentPromisesGetter();
189
190 // JSNative methods
191 bool isExtensibleMethod();
192 bool isSealedMethod();
193 bool isFrozenMethod();
194 bool getPropertyMethod();
195 bool setPropertyMethod();
196 bool getOwnPropertyNamesMethod();
197 bool getOwnPropertyNamesLengthMethod();
198 bool getOwnPropertySymbolsMethod();
199 bool getOwnPrivatePropertiesMethod();
200 bool getOwnPropertyDescriptorMethod();
201 bool preventExtensionsMethod();
202 bool sealMethod();
203 bool freezeMethod();
204 bool definePropertyMethod();
205 bool definePropertiesMethod();
206 bool deletePropertyMethod();
207 bool callMethod();
208 bool applyMethod();
209 bool asEnvironmentMethod();
210 bool forceLexicalInitializationByNameMethod();
211 bool executeInGlobalMethod();
212 bool executeInGlobalWithBindingsMethod();
213 bool createSource();
214 bool makeDebuggeeValueMethod();
215 bool isSameNativeMethod();
216 bool isSameNativeWithJitInfoMethod();
217 bool isNativeGetterWithJitInfo();
218 bool unsafeDereferenceMethod();
219 bool unwrapMethod();
220 bool getPromiseReactionsMethod();
221
222 using Method = bool (CallData::*)();
223
224 template <Method MyMethod>
225 static bool ToNative(JSContext* cx, unsigned argc, Value* vp);
226 };
227
228 template <DebuggerObject::CallData::Method MyMethod>
229 /* static */
230 bool DebuggerObject::CallData::ToNative(JSContext* cx, unsigned argc,
231 Value* vp) {
232 CallArgs args = CallArgsFromVp(argc, vp);
233
234 Rooted<DebuggerObject*> obj(cx, DebuggerObject_checkThis(cx, args));
235 if (!obj) {
236 return false;
237 }
238
239 CallData data(cx, args, obj);
240 return (data.*MyMethod)();
241 }
242
243 bool DebuggerObject::CallData::callableGetter() {
244 args.rval().setBoolean(object->isCallable());
245 return true;
246 }
247
248 bool DebuggerObject::CallData::isBoundFunctionGetter() {
249 args.rval().setBoolean(object->isBoundFunction());
250 return true;
251 }
252
253 bool DebuggerObject::CallData::isArrowFunctionGetter() {
254 if (!object->isDebuggeeFunction()) {
255 args.rval().setUndefined();
256 return true;
257 }
258
259 args.rval().setBoolean(object->isArrowFunction());
260 return true;
261 }
262
263 bool DebuggerObject::CallData::isAsyncFunctionGetter() {
264 if (!object->isDebuggeeFunction()) {
265 args.rval().setUndefined();
266 return true;
267 }
268
269 args.rval().setBoolean(object->isAsyncFunction());
270 return true;
271 }
272
273 bool DebuggerObject::CallData::isGeneratorFunctionGetter() {
274 if (!object->isDebuggeeFunction()) {
275 args.rval().setUndefined();
276 return true;
277 }
278
279 args.rval().setBoolean(object->isGeneratorFunction());
280 return true;
281 }
282
283 bool DebuggerObject::CallData::isClassConstructorGetter() {
284 if (!object->isDebuggeeFunction()) {
285 args.rval().setUndefined();
286 return true;
287 }
288
289 args.rval().setBoolean(object->isClassConstructor());
290 return true;
291 }
292
293 bool DebuggerObject::CallData::protoGetter() {
294 Rooted<DebuggerObject*> result(cx);
295 if (!DebuggerObject::getPrototypeOf(cx, object, &result)) {
296 return false;
297 }
298
299 args.rval().setObjectOrNull(result);
300 return true;
301 }
302
303 bool DebuggerObject::CallData::classGetter() {
304 RootedString result(cx);
305 if (!DebuggerObject::getClassName(cx, object, &result)) {
306 return false;
307 }
308
309 args.rval().setString(result);
310 return true;
311 }
312
313 bool DebuggerObject::CallData::nameGetter() {
314 if (!object->isFunction() && !object->isBoundFunction()) {
315 args.rval().setUndefined();
316 return true;
317 }
318
319 JS::Rooted<JSAtom*> result(cx);
320 if (!object->name(cx, &result)) {
321 return false;
322 }
323
324 if (result) {
325 args.rval().setString(result);
326 } else {
327 args.rval().setUndefined();
328 }
329 return true;
330 }
331
332 bool DebuggerObject::CallData::displayNameGetter() {
333 if (!object->isFunction() && !object->isBoundFunction()) {
334 args.rval().setUndefined();
335 return true;
336 }
337
338 JS::Rooted<JSAtom*> result(cx);
339 if (!object->displayName(cx, &result)) {
340 return false;
341 }
342 if (result) {
343 args.rval().setString(result);
344 } else {
345 args.rval().setUndefined();
346 }
347 return true;
348 }
349
350 bool DebuggerObject::CallData::parameterNamesGetter() {
351 if (!object->isDebuggeeFunction()) {
352 args.rval().setUndefined();
353 return true;
354 }
355
356 RootedFunction referent(cx, &object->referent()->as<JSFunction>());
357
358 ArrayObject* arr = GetFunctionParameterNamesArray(cx, referent);
359 if (!arr) {
360 return false;
361 }
362
363 args.rval().setObject(*arr);
364 return true;
365 }
366
367 bool DebuggerObject::CallData::scriptGetter() {
368 Debugger* dbg = object->owner();
369
370 if (!referent->is<JSFunction>()) {
371 args.rval().setUndefined();
372 return true;
373 }
374
375 RootedFunction fun(cx, &referent->as<JSFunction>());
376 if (!IsInterpretedNonSelfHostedFunction(fun)) {
377 args.rval().setUndefined();
378 return true;
379 }
380
381 RootedScript script(cx, GetOrCreateFunctionScript(cx, fun));
382 if (!script) {
383 return false;
384 }
385
386 // Only hand out debuggee scripts.
387 if (!dbg->observesScript(script)) {
388 args.rval().setNull();
389 return true;
390 }
391
392 Rooted<DebuggerScript*> scriptObject(cx, dbg->wrapScript(cx, script));
393 if (!scriptObject) {
394 return false;
395 }
396
397 args.rval().setObject(*scriptObject);
398 return true;
399 }
400
401 bool DebuggerObject::CallData::environmentGetter() {
402 Debugger* dbg = object->owner();
403
404 // Don't bother switching compartments just to check obj's type and get its
405 // env.
406 if (!referent->is<JSFunction>()) {
407 args.rval().setUndefined();
408 return true;
409 }
410
411 RootedFunction fun(cx, &referent->as<JSFunction>());
412 if (!IsInterpretedNonSelfHostedFunction(fun)) {
413 args.rval().setUndefined();
414 return true;
415 }
416
417 // Only hand out environments of debuggee functions.
418 if (!dbg->observesGlobal(&fun->global())) {
419 args.rval().setNull();
420 return true;
421 }
422
423 Rooted<Env*> env(cx);
424 {
425 AutoRealm ar(cx, fun);
426 env = GetDebugEnvironmentForFunction(cx, fun);
427 if (!env) {
428 return false;
429 }
430 }
431
432 return dbg->wrapEnvironment(cx, env, args.rval());
433 }
434
435 bool DebuggerObject::CallData::boundTargetFunctionGetter() {
436 if (!object->isDebuggeeBoundFunction()) {
437 args.rval().setUndefined();
438 return true;
439 }
440
441 Rooted<DebuggerObject*> result(cx);
442 if (!DebuggerObject::getBoundTargetFunction(cx, object, &result)) {
443 return false;
444 }
445
446 args.rval().setObject(*result);
447 return true;
448 }
449
450 bool DebuggerObject::CallData::boundThisGetter() {
451 if (!object->isDebuggeeBoundFunction()) {
452 args.rval().setUndefined();
453 return true;
454 }
455
456 return DebuggerObject::getBoundThis(cx, object, args.rval());
457 }
458
459 bool DebuggerObject::CallData::boundArgumentsGetter() {
460 if (!object->isDebuggeeBoundFunction()) {
461 args.rval().setUndefined();
462 return true;
463 }
464
465 Rooted<ValueVector> result(cx, ValueVector(cx));
466 if (!DebuggerObject::getBoundArguments(cx, object, &result)) {
467 return false;
468 }
469
470 RootedObject obj(cx,
471 NewDenseCopiedArray(cx, result.length(), result.begin()));
472 if (!obj) {
473 return false;
474 }
475
476 args.rval().setObject(*obj);
477 return true;
478 }
479
480 bool DebuggerObject::CallData::allocationSiteGetter() {
481 RootedObject result(cx);
482 if (!DebuggerObject::getAllocationSite(cx, object, &result)) {
483 return false;
484 }
485
486 args.rval().setObjectOrNull(result);
487 return true;
488 }
489
490 // Returns the "name" field (see js/public/friend/ErrorNumbers.msg), which may
491 // be used as a unique identifier, for any error object with a JSErrorReport or
492 // undefined if the object has no JSErrorReport.
493 bool DebuggerObject::CallData::errorMessageNameGetter() {
494 RootedString result(cx);
495 if (!DebuggerObject::getErrorMessageName(cx, object, &result)) {
496 return false;
497 }
498
499 if (result) {
500 args.rval().setString(result);
501 } else {
502 args.rval().setUndefined();
503 }
504 return true;
505 }
506
507 bool DebuggerObject::CallData::isErrorGetter() {
508 args.rval().setBoolean(object->isError());
509 return true;
510 }
511
512 bool DebuggerObject::CallData::isMutedErrorGetter() {
513 args.rval().setBoolean(object->isMutedError(cx));
514 return true;
515 }
516
517 bool DebuggerObject::CallData::errorNotesGetter() {
518 return DebuggerObject::getErrorNotes(cx, object, args.rval());
519 }
520
521 bool DebuggerObject::CallData::errorLineNumberGetter() {
522 return DebuggerObject::getErrorLineNumber(cx, object, args.rval());
523 }
524
525 bool DebuggerObject::CallData::errorColumnNumberGetter() {
526 return DebuggerObject::getErrorColumnNumber(cx, object, args.rval());
527 }
528
529 bool DebuggerObject::CallData::isProxyGetter() {
530 args.rval().setBoolean(object->isScriptedProxy());
531 return true;
532 }
533
534 bool DebuggerObject::CallData::proxyTargetGetter() {
535 if (!object->isScriptedProxy()) {
536 args.rval().setUndefined();
537 return true;
538 }
539
540 Rooted<DebuggerObject*> result(cx);
541 if (!DebuggerObject::getScriptedProxyTarget(cx, object, &result)) {
542 return false;
543 }
544
545 args.rval().setObjectOrNull(result);
546 return true;
547 }
548
549 bool DebuggerObject::CallData::proxyHandlerGetter() {
550 if (!object->isScriptedProxy()) {
551 args.rval().setUndefined();
552 return true;
553 }
554 Rooted<DebuggerObject*> result(cx);
555 if (!DebuggerObject::getScriptedProxyHandler(cx, object, &result)) {
556 return false;
557 }
558
559 args.rval().setObjectOrNull(result);
560 return true;
561 }
562
563 bool DebuggerObject::CallData::isPromiseGetter() {
564 args.rval().setBoolean(object->isPromise());
565 return true;
566 }
567
568 bool DebuggerObject::CallData::promiseStateGetter() {
569 if (!DebuggerObject::requirePromise(cx, object)) {
570 return false;
571 }
572
573 RootedValue result(cx);
574 switch (object->promiseState()) {
575 case JS::PromiseState::Pending:
576 result.setString(cx->names().pending);
577 break;
578 case JS::PromiseState::Fulfilled:
579 result.setString(cx->names().fulfilled);
580 break;
581 case JS::PromiseState::Rejected:
582 result.setString(cx->names().rejected);
583 break;
584 }
585
586 args.rval().set(result);
587 return true;
588 }
589
590 bool DebuggerObject::CallData::promiseValueGetter() {
591 if (!DebuggerObject::requirePromise(cx, object)) {
592 return false;
593 }
594
595 if (object->promiseState() != JS::PromiseState::Fulfilled) {
596 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
597 JSMSG_DEBUG_PROMISE_NOT_FULFILLED);
598 return false;
599 }
600
601 return DebuggerObject::getPromiseValue(cx, object, args.rval());
602 ;
603 }
604
605 bool DebuggerObject::CallData::promiseReasonGetter() {
606 if (!DebuggerObject::requirePromise(cx, object)) {
607 return false;
608 }
609
610 if (object->promiseState() != JS::PromiseState::Rejected) {
611 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
612 JSMSG_DEBUG_PROMISE_NOT_REJECTED);
613 return false;
614 }
615
616 return DebuggerObject::getPromiseReason(cx, object, args.rval());
617 }
618
619 bool DebuggerObject::CallData::promiseLifetimeGetter() {
620 if (!DebuggerObject::requirePromise(cx, object)) {
621 return false;
622 }
623
624 args.rval().setNumber(object->promiseLifetime());
625 return true;
626 }
627
628 bool DebuggerObject::CallData::promiseTimeToResolutionGetter() {
629 if (!DebuggerObject::requirePromise(cx, object)) {
630 return false;
631 }
632
633 if (object->promiseState() == JS::PromiseState::Pending) {
634 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
635 JSMSG_DEBUG_PROMISE_NOT_RESOLVED);
636 return false;
637 }
638
639 args.rval().setNumber(object->promiseTimeToResolution());
640 return true;
641 }
642
643 static PromiseObject* EnsurePromise(JSContext* cx, HandleObject referent) {
644 // We only care about promises, so CheckedUnwrapStatic is OK.
645 RootedObject obj(cx, CheckedUnwrapStatic(referent));
646 if (!obj) {
647 ReportAccessDenied(cx);
648 return nullptr;
649 }
650 if (!obj->is<PromiseObject>()) {
651 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
652 JSMSG_NOT_EXPECTED_TYPE, "Debugger", "Promise",
653 obj->getClass()->name);
654 return nullptr;
655 }
656 return &obj->as<PromiseObject>();
657 }
658
659 bool DebuggerObject::CallData::promiseAllocationSiteGetter() {
660 Rooted<PromiseObject*> promise(cx, EnsurePromise(cx, referent));
661 if (!promise) {
662 return false;
663 }
664
665 RootedObject allocSite(cx, promise->allocationSite());
666 if (!allocSite) {
667 args.rval().setNull();
668 return true;
669 }
670
671 if (!cx->compartment()->wrap(cx, &allocSite)) {
672 return false;
673 }
674 args.rval().set(ObjectValue(*allocSite));
675 return true;
676 }
677
678 bool DebuggerObject::CallData::promiseResolutionSiteGetter() {
679 Rooted<PromiseObject*> promise(cx, EnsurePromise(cx, referent));
680 if (!promise) {
681 return false;
682 }
683
684 if (promise->state() == JS::PromiseState::Pending) {
685 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
686 JSMSG_DEBUG_PROMISE_NOT_RESOLVED);
687 return false;
688 }
689
690 RootedObject resolutionSite(cx, promise->resolutionSite());
691 if (!resolutionSite) {
692 args.rval().setNull();
693 return true;
694 }
695
696 if (!cx->compartment()->wrap(cx, &resolutionSite)) {
697 return false;
698 }
699 args.rval().set(ObjectValue(*resolutionSite));
700 return true;
701 }
702
703 bool DebuggerObject::CallData::promiseIDGetter() {
704 Rooted<PromiseObject*> promise(cx, EnsurePromise(cx, referent));
705 if (!promise) {
706 return false;
707 }
708
709 args.rval().setNumber(double(promise->getID()));
710 return true;
711 }
712
713 bool DebuggerObject::CallData::promiseDependentPromisesGetter() {
714 Debugger* dbg = object->owner();
715
716 Rooted<PromiseObject*> promise(cx, EnsurePromise(cx, referent));
717 if (!promise) {
718 return false;
719 }
720
721 Rooted<GCVector<Value>> values(cx, GCVector<Value>(cx));
722 {
723 JSAutoRealm ar(cx, promise);
724 if (!promise->dependentPromises(cx, &values)) {
725 return false;
726 }
727 }
728 for (size_t i = 0; i < values.length(); i++) {
729 if (!dbg->wrapDebuggeeValue(cx, values[i])) {
730 return false;
731 }
732 }
733 Rooted<ArrayObject*> promises(cx);
734 if (values.length() == 0) {
735 promises = NewDenseEmptyArray(cx);
736 } else {
737 promises = NewDenseCopiedArray(cx, values.length(), values[0].address());
738 }
739 if (!promises) {
740 return false;
741 }
742 args.rval().setObject(*promises);
743 return true;
744 }
745
746 bool DebuggerObject::CallData::isExtensibleMethod() {
747 bool result;
748 if (!DebuggerObject::isExtensible(cx, object, result)) {
749 return false;
750 }
751
752 args.rval().setBoolean(result);
753 return true;
754 }
755
756 bool DebuggerObject::CallData::isSealedMethod() {
757 bool result;
758 if (!DebuggerObject::isSealed(cx, object, result)) {
759 return false;
760 }
761
762 args.rval().setBoolean(result);
763 return true;
764 }
765
766 bool DebuggerObject::CallData::isFrozenMethod() {
767 bool result;
768 if (!DebuggerObject::isFrozen(cx, object, result)) {
769 return false;
770 }
771
772 args.rval().setBoolean(result);
773 return true;
774 }
775
776 bool DebuggerObject::CallData::getOwnPropertyNamesMethod() {
777 RootedIdVector ids(cx);
778 if (!DebuggerObject::getOwnPropertyNames(cx, object, &ids)) {
779 return false;
780 }
781
782 JSObject* obj = IdVectorToArray(cx, ids);
783 if (!obj) {
784 return false;
785 }
786
787 args.rval().setObject(*obj);
788 return true;
789 }
790
791 bool DebuggerObject::CallData::getOwnPropertyNamesLengthMethod() {
792 size_t ownPropertiesLength;
793 if (!DebuggerObject::getOwnPropertyNamesLength(cx, object,
794 &ownPropertiesLength)) {
795 return false;
796 }
797
798 args.rval().setNumber(ownPropertiesLength);
799 return true;
800 }
801
802 bool DebuggerObject::CallData::getOwnPropertySymbolsMethod() {
803 RootedIdVector ids(cx);
804 if (!DebuggerObject::getOwnPropertySymbols(cx, object, &ids)) {
805 return false;
806 }
807
808 JSObject* obj = IdVectorToArray(cx, ids);
809 if (!obj) {
810 return false;
811 }
812
813 args.rval().setObject(*obj);
814 return true;
815 }
816
817 bool DebuggerObject::CallData::getOwnPrivatePropertiesMethod() {
818 RootedIdVector ids(cx);
819 if (!DebuggerObject::getOwnPrivateProperties(cx, object, &ids)) {
820 return false;
821 }
822
823 JSObject* obj = IdVectorToArray(cx, ids);
824 if (!obj) {
825 return false;
826 }
827
828 args.rval().setObject(*obj);
829 return true;
830 }
831
832 bool DebuggerObject::CallData::getOwnPropertyDescriptorMethod() {
833 RootedId id(cx);
834 if (!ToPropertyKey(cx, args.get(0), &id)) {
835 return false;
836 }
837
838 Rooted<Maybe<PropertyDescriptor>> desc(cx);
839 if (!DebuggerObject::getOwnPropertyDescriptor(cx, object, id, &desc)) {
840 return false;
841 }
842
843 return JS::FromPropertyDescriptor(cx, desc, args.rval());
844 }
845
846 bool DebuggerObject::CallData::preventExtensionsMethod() {
847 if (!DebuggerObject::preventExtensions(cx, object)) {
848 return false;
849 }
850
851 args.rval().setUndefined();
852 return true;
853 }
854
855 bool DebuggerObject::CallData::sealMethod() {
856 if (!DebuggerObject::seal(cx, object)) {
857 return false;
858 }
859
860 args.rval().setUndefined();
861 return true;
862 }
863
864 bool DebuggerObject::CallData::freezeMethod() {
865 if (!DebuggerObject::freeze(cx, object)) {
866 return false;
867 }
868
869 args.rval().setUndefined();
870 return true;
871 }
872
873 bool DebuggerObject::CallData::definePropertyMethod() {
874 if (!args.requireAtLeast(cx, "Debugger.Object.defineProperty", 2)) {
875 return false;
876 }
877
878 RootedId id(cx);
879 if (!ToPropertyKey(cx, args[0], &id)) {
880 return false;
881 }
882
883 Rooted<PropertyDescriptor> desc(cx);
884 if (!ToPropertyDescriptor(cx, args[1], false, &desc)) {
885 return false;
886 }
887
888 if (!DebuggerObject::defineProperty(cx, object, id, desc)) {
889 return false;
890 }
891
892 args.rval().setUndefined();
893 return true;
894 }
895
896 bool DebuggerObject::CallData::definePropertiesMethod() {
897 if (!args.requireAtLeast(cx, "Debugger.Object.defineProperties", 1)) {
898 return false;
899 }
900
901 RootedValue arg(cx, args[0]);
902 RootedObject props(cx, ToObject(cx, arg));
903 if (!props) {
904 return false;
905 }
906 RootedIdVector ids(cx);
907 Rooted<PropertyDescriptorVector> descs(cx, PropertyDescriptorVector(cx));
908 if (!ReadPropertyDescriptors(cx, props, false, &ids, &descs)) {
909 return false;
910 }
911 Rooted<IdVector> ids2(cx, IdVector(cx));
912 if (!ids2.append(ids.begin(), ids.end())) {
913 return false;
914 }
915
916 if (!DebuggerObject::defineProperties(cx, object, ids2, descs)) {
917 return false;
918 }
919
920 args.rval().setUndefined();
921 return true;
922 }
923
924 /*
925 * This does a non-strict delete, as a matter of API design. The case where the
926 * property is non-configurable isn't necessarily exceptional here.
927 */
928 bool DebuggerObject::CallData::deletePropertyMethod() {
929 RootedId id(cx);
930 if (!ToPropertyKey(cx, args.get(0), &id)) {
931 return false;
932 }
933
934 ObjectOpResult result;
935 if (!DebuggerObject::deleteProperty(cx, object, id, result)) {
936 return false;
937 }
938
939 args.rval().setBoolean(result.ok());
940 return true;
941 }
942
943 bool DebuggerObject::CallData::callMethod() {
944 RootedValue thisv(cx, args.get(0));
945
946 Rooted<ValueVector> nargs(cx, ValueVector(cx));
947 if (args.length() >= 2) {
948 if (!nargs.growBy(args.length() - 1)) {
949 return false;
950 }
951 for (size_t i = 1; i < args.length(); ++i) {
952 nargs[i - 1].set(args[i]);
953 }
954 }
955
956 Rooted<Maybe<Completion>> completion(
957 cx, DebuggerObject::call(cx, object, thisv, nargs));
958 if (!completion.get()) {
959 return false;
960 }
961
962 return completion->buildCompletionValue(cx, object->owner(), args.rval());
963 }
964
965 bool DebuggerObject::CallData::getPropertyMethod() {
966 Debugger* dbg = object->owner();
967
968 RootedId id(cx);
969 if (!ToPropertyKey(cx, args.get(0), &id)) {
970 return false;
971 }
972
973 RootedValue receiver(cx,
974 args.length() < 2 ? ObjectValue(*object) : args.get(1));
975
976 Rooted<Completion> comp(cx);
977 JS_TRY_VAR_OR_RETURN_FALSE(cx, comp, getProperty(cx, object, id, receiver));
978 return comp.get().buildCompletionValue(cx, dbg, args.rval());
979 }
980
981 bool DebuggerObject::CallData::setPropertyMethod() {
982 Debugger* dbg = object->owner();
983
984 RootedId id(cx);
985 if (!ToPropertyKey(cx, args.get(0), &id)) {
986 return false;
987 }
988
989 RootedValue value(cx, args.get(1));
990
991 RootedValue receiver(cx,
992 args.length() < 3 ? ObjectValue(*object) : args.get(2));
993
994 Rooted<Completion> comp(cx);
995 JS_TRY_VAR_OR_RETURN_FALSE(cx, comp,
996 setProperty(cx, object, id, value, receiver));
997 return comp.get().buildCompletionValue(cx, dbg, args.rval());
998 }
999
1000 bool DebuggerObject::CallData::applyMethod() {
1001 RootedValue thisv(cx, args.get(0));
1002
1003 Rooted<ValueVector> nargs(cx, ValueVector(cx));
1004 if (args.length() >= 2 && !args[1].isNullOrUndefined()) {
1005 if (!args[1].isObject()) {
1006 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1007 JSMSG_BAD_APPLY_ARGS, "apply");
1008 return false;
1009 }
1010
1011 RootedObject argsobj(cx, &args[1].toObject());
1012
1013 uint64_t argc = 0;
1014 if (!GetLengthProperty(cx, argsobj, &argc)) {
1015 return false;
1016 }
1017 argc = std::min(argc, uint64_t(ARGS_LENGTH_MAX));
1018
1019 if (!nargs.growBy(argc) || !GetElements(cx, argsobj, argc, nargs.begin())) {
1020 return false;
1021 }
1022 }
1023
1024 Rooted<Maybe<Completion>> completion(
1025 cx, DebuggerObject::call(cx, object, thisv, nargs));
1026 if (!completion.get()) {
1027 return false;
1028 }
1029
1030 return completion->buildCompletionValue(cx, object->owner(), args.rval());
1031 }
1032
1033 static void EnterDebuggeeObjectRealm(JSContext* cx, Maybe<AutoRealm>& ar,
1034 JSObject* referent) {
1035 // |referent| may be a cross-compartment wrapper and CCWs normally
1036 // shouldn't be used with AutoRealm, but here we use an arbitrary realm for
1037 // now because we don't really have another option.
1038 ar.emplace(cx, referent->maybeCCWRealm()->maybeGlobal());
1039 }
1040
1041 static bool RequireGlobalObject(JSContext* cx, HandleValue dbgobj,
1042 HandleObject referent) {
1043 RootedObject obj(cx, referent);
1044
1045 if (!obj->is<GlobalObject>()) {
1046 const char* isWrapper = "";
1047 const char* isWindowProxy = "";
1048
1049 // Help the poor programmer by pointing out wrappers around globals...
1050 if (obj->is<WrapperObject>()) {
1051 obj = js::UncheckedUnwrap(obj);
1052 isWrapper = "a wrapper around ";
1053 }
1054
1055 // ... and WindowProxies around Windows.
1056 if (IsWindowProxy(obj)) {
1057 obj = ToWindowIfWindowProxy(obj);
1058 isWindowProxy = "a WindowProxy referring to ";
1059 }
1060
1061 if (obj->is<GlobalObject>()) {
1062 ReportValueError(cx, JSMSG_DEBUG_WRAPPER_IN_WAY, JSDVG_SEARCH_STACK,
1063 dbgobj, nullptr, isWrapper, isWindowProxy);
1064 } else {
1065 ReportValueError(cx, JSMSG_DEBUG_BAD_REFERENT, JSDVG_SEARCH_STACK, dbgobj,
1066 nullptr, "a global object");
1067 }
1068 return false;
1069 }
1070
1071 return true;
1072 }
1073
1074 bool DebuggerObject::CallData::asEnvironmentMethod() {
1075 Debugger* dbg = object->owner();
1076
1077 if (!RequireGlobalObject(cx, args.thisv(), referent)) {
1078 return false;
1079 }
1080
1081 Rooted<Env*> env(cx);
1082 {
1083 AutoRealm ar(cx, referent);
1084 env = GetDebugEnvironmentForGlobalLexicalEnvironment(cx);
1085 if (!env) {
1086 return false;
1087 }
1088 }
1089
1090 return dbg->wrapEnvironment(cx, env, args.rval());
1091 }
1092
1093 // Lookup a binding on the referent's global scope and change it to undefined
1094 // if it is an uninitialized lexical, otherwise do nothing. The method's
1095 // JavaScript return value is true _only_ when an uninitialized lexical has been
1096 // altered, otherwise it is false.
1097 bool DebuggerObject::CallData::forceLexicalInitializationByNameMethod() {
1098 if (!args.requireAtLeast(
1099 cx, "Debugger.Object.prototype.forceLexicalInitializationByName",
1100 1)) {
1101 return false;
1102 }
1103
1104 if (!DebuggerObject::requireGlobal(cx, object)) {
1105 return false;
1106 }
1107
1108 RootedId id(cx);
1109 if (!ValueToIdentifier(cx, args[0], &id)) {
1110 return false;
1111 }
1112
1113 bool result;
1114 if (!DebuggerObject::forceLexicalInitializationByName(cx, object, id,
1115 result)) {
1116 return false;
1117 }
1118
1119 args.rval().setBoolean(result);
1120 return true;
1121 }
1122
1123 bool DebuggerObject::CallData::executeInGlobalMethod() {
1124 if (!args.requireAtLeast(cx, "Debugger.Object.prototype.executeInGlobal",
1125 1)) {
1126 return false;
1127 }
1128
1129 if (!DebuggerObject::requireGlobal(cx, object)) {
1130 return false;
1131 }
1132
1133 AutoStableStringChars stableChars(cx);
1134 if (!ValueToStableChars(cx, "Debugger.Object.prototype.executeInGlobal",
1135 args[0], stableChars)) {
1136 return false;
1137 }
1138 mozilla::Range<const char16_t> chars = stableChars.twoByteRange();
1139
1140 EvalOptions options(EvalOptions::EnvKind::Global);
1141 if (!ParseEvalOptions(cx, args.get(1), options)) {
1142 return false;
1143 }
1144
1145 Rooted<Completion> comp(cx);
1146 JS_TRY_VAR_OR_RETURN_FALSE(
1147 cx, comp,
1148 DebuggerObject::executeInGlobal(cx, object, chars, nullptr, options));
1149 return comp.get().buildCompletionValue(cx, object->owner(), args.rval());
1150 }
1151
1152 bool DebuggerObject::CallData::executeInGlobalWithBindingsMethod() {
1153 if (!args.requireAtLeast(
1154 cx, "Debugger.Object.prototype.executeInGlobalWithBindings", 2)) {
1155 return false;
1156 }
1157
1158 if (!DebuggerObject::requireGlobal(cx, object)) {
1159 return false;
1160 }
1161
1162 AutoStableStringChars stableChars(cx);
1163 if (!ValueToStableChars(
1164 cx, "Debugger.Object.prototype.executeInGlobalWithBindings", args[0],
1165 stableChars)) {
1166 return false;
1167 }
1168 mozilla::Range<const char16_t> chars = stableChars.twoByteRange();
1169
1170 RootedObject bindings(cx, RequireObject(cx, args[1]));
1171 if (!bindings) {
1172 return false;
1173 }
1174
1175 EvalOptions options(EvalOptions::EnvKind::GlobalWithExtraOuterBindings);
1176 if (!ParseEvalOptions(cx, args.get(2), options)) {
1177 return false;
1178 }
1179
1180 Rooted<Completion> comp(cx);
1181 JS_TRY_VAR_OR_RETURN_FALSE(
1182 cx, comp,
1183 DebuggerObject::executeInGlobal(cx, object, chars, bindings, options));
1184 return comp.get().buildCompletionValue(cx, object->owner(), args.rval());
1185 }
1186
1187 // Copy a narrow or wide string to a vector, appending a null terminator.
1188 template <typename T>
1189 static bool CopyStringToVector(JSContext* cx, JSString* str, Vector<T>& chars) {
1190 JSLinearString* linear = str->ensureLinear(cx);
1191 if (!linear) {
1192 return false;
1193 }
1194 if (!chars.appendN(0, linear->length() + 1)) {
1195 return false;
1196 }
1197 CopyChars(chars.begin(), *linear);
1198 return true;
1199 }
1200
1201 bool DebuggerObject::CallData::createSource() {
1202 if (!args.requireAtLeast(cx, "Debugger.Object.prototype.createSource", 1)) {
1203 return false;
1204 }
1205
1206 if (!DebuggerObject::requireGlobal(cx, object)) {
1207 return false;
1208 }
1209
1210 Debugger* dbg = object->owner();
1211 if (!dbg->isDebuggeeUnbarriered(referent->as<GlobalObject>().realm())) {
1212 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
1213 JSMSG_DEBUG_NOT_DEBUGGEE, "Debugger.Object",
1214 "global");
1215 return false;
1216 }
1217
1218 RootedObject options(cx, ToObject(cx, args[0]));
1219 if (!options) {
1220 return false;
1221 }
1222
1223 RootedValue v(cx);
1224 if (!JS_GetProperty(cx, options, "text", &v)) {
1225 return false;
1226 }
1227
1228 RootedString text(cx, ToString<CanGC>(cx, v));
1229 if (!text) {
1230 return false;
1231 }
1232
1233 if (!JS_GetProperty(cx, options, "url", &v)) {
1234 return false;
1235 }
1236
1237 RootedString url(cx, ToString<CanGC>(cx, v));
1238 if (!url) {
1239 return false;
1240 }
1241
1242 if (!JS_GetProperty(cx, options, "startLine", &v)) {
1243 return false;
1244 }
1245
1246 uint32_t startLine;
1247 if (!ToUint32(cx, v, &startLine)) {
1248 return false;
1249 }
1250
1251 if (!JS_GetProperty(cx, options, "startColumn", &v)) {
1252 return false;
1253 }
1254
1255 uint32_t startColumn;
1256 if (!ToUint32(cx, v, &startColumn)) {
1257 return false;
1258 }
1259 if (startColumn == 0) {
1260 startColumn = 1;
1261 }
1262
1263 if (!JS_GetProperty(cx, options, "sourceMapURL", &v)) {
1264 return false;
1265 }
1266
1267 RootedString sourceMapURL(cx);
1268 if (!v.isUndefined()) {
1269 sourceMapURL = ToString<CanGC>(cx, v);
1270 if (!sourceMapURL) {
1271 return false;
1272 }
1273 }
1274
1275 if (!JS_GetProperty(cx, options, "isScriptElement", &v)) {
1276 return false;
1277 }
1278
1279 bool isScriptElement = ToBoolean(v);
1280
1281 if (!JS_GetProperty(cx, options, "forceEnableAsmJS", &v)) {
1282 return false;
1283 }
1284
1285 bool forceEnableAsmJS = ToBoolean(v);
1286
1287 RootedScript script(cx);
1288 {
1289 AutoRealm ar(cx, referent);
1290
1291 JS::CompileOptions compileOptions(cx);
1292 compileOptions.lineno = startLine;
1293 compileOptions.column = JS::ColumnNumberOneOrigin(startColumn);
1294 if (forceEnableAsmJS) {
1295 compileOptions.setAsmJSOption(JS::AsmJSOption::Enabled);
1296 }
1297
1298 if (!JS::StringHasLatin1Chars(url)) {
1299 JS_ReportErrorASCII(cx, "URL must be a narrow string");
1300 return false;
1301 }
1302
1303 UniqueChars urlChars = JS_EncodeStringToUTF8(cx, url);
1304 if (!urlChars) {
1305 return false;
1306 }
1307 compileOptions.setFile(urlChars.get());
1308
1309 Vector<char16_t> sourceMapURLChars(cx);
1310 if (sourceMapURL) {
1311 if (!CopyStringToVector(cx, sourceMapURL, sourceMapURLChars)) {
1312 return false;
1313 }
1314 compileOptions.setSourceMapURL(sourceMapURLChars.begin());
1315 }
1316
1317 if (isScriptElement) {
1318 // The introduction type must be a statically allocated string.
1319 compileOptions.setIntroductionType("inlineScript");
1320 }
1321
1322 AutoStableStringChars linearChars(cx);
1323 if (!linearChars.initTwoByte(cx, text)) {
1324 return false;
1325 }
1326 JS::SourceText<char16_t> srcBuf;
1327 if (!srcBuf.initMaybeBorrowed(cx, linearChars)) {
1328 return false;
1329 }
1330
1331 script = JS::Compile(cx, compileOptions, srcBuf);
1332 if (!script) {
1333 return false;
1334 }
1335 }
1336
1337 Rooted<ScriptSourceObject*> sso(cx, script->sourceObject());
1338 RootedObject wrapped(cx, dbg->wrapSource(cx, sso));
1339 if (!wrapped) {
1340 return false;
1341 }
1342
1343 args.rval().setObject(*wrapped);
1344 return true;
1345 }
1346
1347 bool DebuggerObject::CallData::makeDebuggeeValueMethod() {
1348 if (!args.requireAtLeast(cx, "Debugger.Object.prototype.makeDebuggeeValue",
1349 1)) {
1350 return false;
1351 }
1352
1353 return DebuggerObject::makeDebuggeeValue(cx, object, args[0], args.rval());
1354 }
1355
1356 bool DebuggerObject::CallData::isSameNativeMethod() {
1357 if (!args.requireAtLeast(cx, "Debugger.Object.prototype.isSameNative", 1)) {
1358 return false;
1359 }
1360
1361 return DebuggerObject::isSameNative(cx, object, args[0], CheckJitInfo::No,
1362 args.rval());
1363 }
1364
1365 bool DebuggerObject::CallData::isSameNativeWithJitInfoMethod() {
1366 if (!args.requireAtLeast(
1367 cx, "Debugger.Object.prototype.isSameNativeWithJitInfo", 1)) {
1368 return false;
1369 }
1370
1371 return DebuggerObject::isSameNative(cx, object, args[0], CheckJitInfo::Yes,
1372 args.rval());
1373 }
1374
1375 bool DebuggerObject::CallData::isNativeGetterWithJitInfo() {
1376 return DebuggerObject::isNativeGetterWithJitInfo(cx, object, args.rval());
1377 }
1378
1379 bool DebuggerObject::CallData::unsafeDereferenceMethod() {
1380 RootedObject result(cx);
1381 if (!DebuggerObject::unsafeDereference(cx, object, &result)) {
1382 return false;
1383 }
1384
1385 args.rval().setObject(*result);
1386 return true;
1387 }
1388
1389 bool DebuggerObject::CallData::unwrapMethod() {
1390 Rooted<DebuggerObject*> result(cx);
1391 if (!DebuggerObject::unwrap(cx, object, &result)) {
1392 return false;
1393 }
1394
1395 args.rval().setObjectOrNull(result);
1396 return true;
1397 }
1398
1399 struct DebuggerObject::PromiseReactionRecordBuilder
1400 : js::PromiseReactionRecordBuilder {
1401 Debugger* dbg;
1402 Handle<ArrayObject*> records;
1403
1404 PromiseReactionRecordBuilder(Debugger* dbg, Handle<ArrayObject*> records)
1405 : dbg(dbg), records(records) {}
1406
1407 bool then(JSContext* cx, HandleObject resolve, HandleObject reject,
1408 HandleObject result) override {
1409 Rooted<PlainObject*> record(cx, NewPlainObject(cx));
1410 if (!record) {
1411 return false;
1412 }
1413
1414 if (!setIfNotNull(cx, record, cx->names().resolve, resolve) ||
1415 !setIfNotNull(cx, record, cx->names().reject, reject) ||
1416 !setIfNotNull(cx, record, cx->names().result, result)) {
1417 return false;
1418 }
1419
1420 return push(cx, record);
1421 }
1422
1423 bool direct(JSContext* cx, Handle<PromiseObject*> unwrappedPromise) override {
1424 RootedValue v(cx, ObjectValue(*unwrappedPromise));
1425 return dbg->wrapDebuggeeValue(cx, &v) && push(cx, v);
1426 }
1427
1428 bool asyncFunction(
1429 JSContext* cx,
1430 Handle<AsyncFunctionGeneratorObject*> unwrappedGenerator) override {
1431 return maybePushGenerator(cx, unwrappedGenerator);
1432 }
1433
1434 bool asyncGenerator(
1435 JSContext* cx,
1436 Handle<AsyncGeneratorObject*> unwrappedGenerator) override {
1437 return maybePushGenerator(cx, unwrappedGenerator);
1438 }
1439
1440 private:
1441 bool push(JSContext* cx, HandleObject record) {
1442 RootedValue recordVal(cx, ObjectValue(*record));
1443 return push(cx, recordVal);
1444 }
1445
1446 bool push(JSContext* cx, HandleValue recordVal) {
1447 return NewbornArrayPush(cx, records, recordVal);
1448 }
1449
1450 bool maybePushGenerator(JSContext* cx,
1451 Handle<AbstractGeneratorObject*> unwrappedGenerator) {
1452 Rooted<DebuggerFrame*> frame(cx);
1453 if (unwrappedGenerator->isClosed()) {
1454 // If the generator is closed, we can't generate a DebuggerFrame for it,
1455 // so we ignore it.
1456 return true;
1457 }
1458 if (!unwrappedGenerator->realm()->isDebuggee()) {
1459 // Caller can keep the reference to the debugger object even after
1460 // removing the realm from debuggee. Do nothing for this case.
1461 return true;
1462 }
1463 return dbg->getFrame(cx, unwrappedGenerator, &frame) && push(cx, frame);
1464 }
1465
1466 bool setIfNotNull(JSContext* cx, Handle<PlainObject*> obj,
1467 Handle<PropertyName*> name, HandleObject prop) {
1468 if (!prop) {
1469 return true;
1470 }
1471
1472 RootedValue v(cx, ObjectValue(*prop));
1473 if (!dbg->wrapDebuggeeValue(cx, &v) ||
1474 !DefineDataProperty(cx, obj, name, v)) {
1475 return false;
1476 }
1477
1478 return true;
1479 }
1480 };
1481
1482 bool DebuggerObject::CallData::getPromiseReactionsMethod() {
1483 Debugger* dbg = object->owner();
1484
1485 Rooted<PromiseObject*> unwrappedPromise(cx, EnsurePromise(cx, referent));
1486 if (!unwrappedPromise) {
1487 return false;
1488 }
1489
1490 Rooted<ArrayObject*> holder(cx, NewDenseEmptyArray(cx));
1491 if (!holder) {
1492 return false;
1493 }
1494
1495 PromiseReactionRecordBuilder builder(dbg, holder);
1496 if (!unwrappedPromise->forEachReactionRecord(cx, builder)) {
1497 return false;
1498 }
1499
1500 args.rval().setObject(*builder.records);
1501 return true;
1502 }
1503
1504 const JSPropertySpec DebuggerObject::properties_[] = {
1505 JS_DEBUG_PSG("callable", callableGetter),
1506 JS_DEBUG_PSG("isBoundFunction", isBoundFunctionGetter),
1507 JS_DEBUG_PSG("isArrowFunction", isArrowFunctionGetter),
1508 JS_DEBUG_PSG("isGeneratorFunction", isGeneratorFunctionGetter),
1509 JS_DEBUG_PSG("isAsyncFunction", isAsyncFunctionGetter),
1510 JS_DEBUG_PSG("isClassConstructor", isClassConstructorGetter),
1511 JS_DEBUG_PSG("proto", protoGetter),
1512 JS_DEBUG_PSG("class", classGetter),
1513 JS_DEBUG_PSG("name", nameGetter),
1514 JS_DEBUG_PSG("displayName", displayNameGetter),
1515 JS_DEBUG_PSG("parameterNames", parameterNamesGetter),
1516 JS_DEBUG_PSG("script", scriptGetter),
1517 JS_DEBUG_PSG("environment", environmentGetter),
1518 JS_DEBUG_PSG("boundTargetFunction", boundTargetFunctionGetter),
1519 JS_DEBUG_PSG("boundThis", boundThisGetter),
1520 JS_DEBUG_PSG("boundArguments", boundArgumentsGetter),
1521 JS_DEBUG_PSG("allocationSite", allocationSiteGetter),
1522 JS_DEBUG_PSG("isError", isErrorGetter),
1523 JS_DEBUG_PSG("isMutedError", isMutedErrorGetter),
1524 JS_DEBUG_PSG("errorMessageName", errorMessageNameGetter),
1525 JS_DEBUG_PSG("errorNotes", errorNotesGetter),
1526 JS_DEBUG_PSG("errorLineNumber", errorLineNumberGetter),
1527 JS_DEBUG_PSG("errorColumnNumber", errorColumnNumberGetter),
1528 JS_DEBUG_PSG("isProxy", isProxyGetter),
1529 JS_DEBUG_PSG("proxyTarget", proxyTargetGetter),
1530 JS_DEBUG_PSG("proxyHandler", proxyHandlerGetter),
1531 JS_PS_END,
1532 };
1533
1534 const JSPropertySpec DebuggerObject::promiseProperties_[] = {
1535 JS_DEBUG_PSG("isPromise", isPromiseGetter),
1536 JS_DEBUG_PSG("promiseState", promiseStateGetter),
1537 JS_DEBUG_PSG("promiseValue", promiseValueGetter),
1538 JS_DEBUG_PSG("promiseReason", promiseReasonGetter),
1539 JS_DEBUG_PSG("promiseLifetime", promiseLifetimeGetter),
1540 JS_DEBUG_PSG("promiseTimeToResolution", promiseTimeToResolutionGetter),
1541 JS_DEBUG_PSG("promiseAllocationSite", promiseAllocationSiteGetter),
1542 JS_DEBUG_PSG("promiseResolutionSite", promiseResolutionSiteGetter),
1543 JS_DEBUG_PSG("promiseID", promiseIDGetter),
1544 JS_DEBUG_PSG("promiseDependentPromises", promiseDependentPromisesGetter),
1545 JS_PS_END,
1546 };
1547
1548 const JSFunctionSpec DebuggerObject::methods_[] = {
1549 JS_DEBUG_FN("isExtensible", isExtensibleMethod, 0),
1550 JS_DEBUG_FN("isSealed", isSealedMethod, 0),
1551 JS_DEBUG_FN("isFrozen", isFrozenMethod, 0),
1552 JS_DEBUG_FN("getProperty", getPropertyMethod, 0),
1553 JS_DEBUG_FN("setProperty", setPropertyMethod, 0),
1554 JS_DEBUG_FN("getOwnPropertyNames", getOwnPropertyNamesMethod, 0),
1555 JS_DEBUG_FN("getOwnPropertyNamesLength", getOwnPropertyNamesLengthMethod,
1556 0),
1557 JS_DEBUG_FN("getOwnPropertySymbols", getOwnPropertySymbolsMethod, 0),
1558 JS_DEBUG_FN("getOwnPrivateProperties", getOwnPrivatePropertiesMethod, 0),
1559 JS_DEBUG_FN("getOwnPropertyDescriptor", getOwnPropertyDescriptorMethod, 1),
1560 JS_DEBUG_FN("preventExtensions", preventExtensionsMethod, 0),
1561 JS_DEBUG_FN("seal", sealMethod, 0),
1562 JS_DEBUG_FN("freeze", freezeMethod, 0),
1563 JS_DEBUG_FN("defineProperty", definePropertyMethod, 2),
1564 JS_DEBUG_FN("defineProperties", definePropertiesMethod, 1),
1565 JS_DEBUG_FN("deleteProperty", deletePropertyMethod, 1),
1566 JS_DEBUG_FN("call", callMethod, 0),
1567 JS_DEBUG_FN("apply", applyMethod, 0),
1568 JS_DEBUG_FN("asEnvironment", asEnvironmentMethod, 0),
1569 JS_DEBUG_FN("forceLexicalInitializationByName",
1570 forceLexicalInitializationByNameMethod, 1),
1571 JS_DEBUG_FN("executeInGlobal", executeInGlobalMethod, 1),
1572 JS_DEBUG_FN("executeInGlobalWithBindings",
1573 executeInGlobalWithBindingsMethod, 2),
1574 JS_DEBUG_FN("createSource", createSource, 1),
1575 JS_DEBUG_FN("makeDebuggeeValue", makeDebuggeeValueMethod, 1),
1576 JS_DEBUG_FN("isSameNative", isSameNativeMethod, 1),
1577 JS_DEBUG_FN("isSameNativeWithJitInfo", isSameNativeWithJitInfoMethod, 1),
1578 JS_DEBUG_FN("isNativeGetterWithJitInfo", isNativeGetterWithJitInfo, 1),
1579 JS_DEBUG_FN("unsafeDereference", unsafeDereferenceMethod, 0),
1580 JS_DEBUG_FN("unwrap", unwrapMethod, 0),
1581 JS_DEBUG_FN("getPromiseReactions", getPromiseReactionsMethod, 0),
1582 JS_FS_END,
1583 };
1584
1585 /* static */
1586 NativeObject* DebuggerObject::initClass(JSContext* cx,
1587 Handle<GlobalObject*> global,
1588 HandleObject debugCtor) {
1589 Rooted<NativeObject*> objectProto(
1590 cx, InitClass(cx, debugCtor, nullptr, nullptr, "Object", construct, 0,
1591 properties_, methods_, nullptr, nullptr));
1592
1593 if (!objectProto) {
1594 return nullptr;
1595 }
1596
1597 if (!DefinePropertiesAndFunctions(cx, objectProto, promiseProperties_,
1598 nullptr)) {
1599 return nullptr;
1600 }
1601
1602 return objectProto;
1603 }
1604
1605 /* static */
1606 DebuggerObject* DebuggerObject::create(JSContext* cx, HandleObject proto,
1607 HandleObject referent,
1608 Handle<NativeObject*> debugger) {
1609 DebuggerObject* obj =
1610 IsInsideNursery(referent)
1611 ? NewObjectWithGivenProto<DebuggerObject>(cx, proto)
1612 : NewTenuredObjectWithGivenProto<DebuggerObject>(cx, proto);
1613 if (!obj) {
1614 return nullptr;
1615 }
1616
1617 obj->setReservedSlotGCThingAsPrivate(OBJECT_SLOT, referent);
1618 obj->setReservedSlot(OWNER_SLOT, ObjectValue(*debugger));
1619
1620 return obj;
1621 }
1622
1623 bool DebuggerObject::isCallable() const { return referent()->isCallable(); }
1624
1625 bool DebuggerObject::isFunction() const { return referent()->is<JSFunction>(); }
1626
1627 bool DebuggerObject::isDebuggeeFunction() const {
1628 return referent()->is<JSFunction>() &&
1629 owner()->observesGlobal(&referent()->as<JSFunction>().global());
1630 }
1631
1632 bool DebuggerObject::isBoundFunction() const {
1633 return referent()->is<BoundFunctionObject>();
1634 }
1635
1636 bool DebuggerObject::isDebuggeeBoundFunction() const {
1637 return referent()->is<BoundFunctionObject>() &&
1638 owner()->observesGlobal(
1639 &referent()->as<BoundFunctionObject>().global());
1640 }
1641
1642 bool DebuggerObject::isArrowFunction() const {
1643 MOZ_ASSERT(isDebuggeeFunction());
1644
1645 return referent()->as<JSFunction>().isArrow();
1646 }
1647
1648 bool DebuggerObject::isAsyncFunction() const {
1649 MOZ_ASSERT(isDebuggeeFunction());
1650
1651 return referent()->as<JSFunction>().isAsync();
1652 }
1653
1654 bool DebuggerObject::isGeneratorFunction() const {
1655 MOZ_ASSERT(isDebuggeeFunction());
1656
1657 return referent()->as<JSFunction>().isGenerator();
1658 }
1659
1660 bool DebuggerObject::isClassConstructor() const {
1661 MOZ_ASSERT(isDebuggeeFunction());
1662
1663 return referent()->as<JSFunction>().isClassConstructor();
1664 }
1665
1666 bool DebuggerObject::isGlobal() const { return referent()->is<GlobalObject>(); }
1667
1668 bool DebuggerObject::isScriptedProxy() const {
1669 return js::IsScriptedProxy(referent());
1670 }
1671
1672 bool DebuggerObject::isPromise() const {
1673 JSObject* referent = this->referent();
1674
1675 if (IsCrossCompartmentWrapper(referent)) {
1676 // We only care about promises, so CheckedUnwrapStatic is OK.
1677 referent = CheckedUnwrapStatic(referent);
1678 if (!referent) {
1679 return false;
1680 }
1681 }
1682
1683 return referent->is<PromiseObject>();
1684 }
1685
1686 bool DebuggerObject::isError() const {
1687 JSObject* referent = this->referent();
1688
1689 if (IsCrossCompartmentWrapper(referent)) {
1690 // We only check for error classes, so CheckedUnwrapStatic is OK.
1691 referent = CheckedUnwrapStatic(referent);
1692 if (!referent) {
1693 return false;
1694 }
1695 }
1696
1697 return referent->is<ErrorObject>();
1698 }
1699
1700 bool DebuggerObject::isMutedError(JSContext* cx) const {
1701 JS::Rooted<JSObject*> referent(cx, this->referent());
1702
1703 if (IsCrossCompartmentWrapper(referent)) {
1704 // We only check for error classes, so CheckedUnwrapStatic is OK.
1705 referent = CheckedUnwrapStatic(referent);
1706 if (!referent) {
1707 return false;
1708 }
1709 }
1710
1711 if (!referent->is<ErrorObject>()) {
1712 return false;
1713 }
1714
1715 JSErrorReport* report = referent->as<ErrorObject>().getErrorReport();
1716 if (!report) {
1717 // If the error report was missing, isMuted field can never be true,
1718 // and just returning false is OK.
1719 return false;
1720 }
1721
1722 return report->isMuted;
1723 }
1724
1725 /* static */
1726 bool DebuggerObject::getClassName(JSContext* cx, Handle<DebuggerObject*> object,
1727 MutableHandleString result) {
1728 RootedObject referent(cx, object->referent());
1729
1730 const char* className;
1731 {
1732 Maybe<AutoRealm> ar;
1733 EnterDebuggeeObjectRealm(cx, ar, referent);
1734 className = GetObjectClassName(cx, referent);
1735 }
1736
1737 JSAtom* str = Atomize(cx, className, strlen(className));
1738 if (!str) {
1739 return false;
1740 }
1741
1742 result.set(str);
1743 return true;
1744 }
1745
1746 bool DebuggerObject::name(JSContext* cx,
1747 JS::MutableHandle<JSAtom*> result) const {
1748 if (isFunction()) {
1749 JSFunction* fun = &referent()->as<JSFunction>();
1750 if (!fun->isAccessorWithLazyName()) {
1751 result.set(fun->fullExplicitName());
1752 if (result) {
1753 cx->markAtom(result);
1754 }
1755 return true;
1756 }
1757
1758 {
1759 Maybe<AutoRealm> ar;
1760 EnterDebuggeeObjectRealm(cx, ar, fun);
1761
1762 result.set(fun->getAccessorNameForLazy(cx));
1763 if (!result) {
1764 return false;
1765 }
1766 }
1767 cx->markAtom(result);
1768 return true;
1769 }
1770
1771 MOZ_ASSERT(isBoundFunction());
1772
1773 // Bound functions have a configurable `name` data property and currently
1774 // don't store the original name. Try a pure lookup to get this name and if
1775 // this fails use "bound".
1776 Rooted<BoundFunctionObject*> bound(cx,
1777 &referent()->as<BoundFunctionObject>());
1778 {
1779 Maybe<AutoRealm> ar;
1780 EnterDebuggeeObjectRealm(cx, ar, bound);
1781
1782 Value v;
1783 bool found;
1784 if (GetOwnPropertyPure(cx, bound, NameToId(cx->names().name), &v, &found) &&
1785 found && v.isString()) {
1786 result.set(AtomizeString(cx, v.toString()));
1787 if (!result) {
1788 return false;
1789 }
1790 } else {
1791 result.set(cx->names().bound);
1792 }
1793 }
1794
1795 cx->markAtom(result);
1796 return true;
1797 }
1798
1799 bool DebuggerObject::displayName(JSContext* cx,
1800 JS::MutableHandle<JSAtom*> result) const {
1801 if (isFunction()) {
1802 {
1803 JS::Rooted<JSFunction*> fun(cx, &referent()->as<JSFunction>());
1804
1805 Maybe<AutoRealm> ar;
1806 EnterDebuggeeObjectRealm(cx, ar, fun);
1807
1808 if (!fun->getDisplayAtom(cx, result)) {
1809 return false;
1810 }
1811 }
1812 if (result) {
1813 cx->markAtom(result);
1814 }
1815 return true;
1816 }
1817
1818 MOZ_ASSERT(isBoundFunction());
1819 return name(cx, result);
1820 }
1821
1822 JS::PromiseState DebuggerObject::promiseState() const {
1823 return promise()->state();
1824 }
1825
1826 double DebuggerObject::promiseLifetime() const { return promise()->lifetime(); }
1827
1828 double DebuggerObject::promiseTimeToResolution() const {
1829 MOZ_ASSERT(promiseState() != JS::PromiseState::Pending);
1830
1831 return promise()->timeToResolution();
1832 }
1833
1834 /* static */
1835 bool DebuggerObject::getBoundTargetFunction(
1836 JSContext* cx, Handle<DebuggerObject*> object,
1837 MutableHandle<DebuggerObject*> result) {
1838 MOZ_ASSERT(object->isBoundFunction());
1839
1840 Rooted<BoundFunctionObject*> referent(
1841 cx, &object->referent()->as<BoundFunctionObject>());
1842 Debugger* dbg = object->owner();
1843
1844 RootedObject target(cx, referent->getTarget());
1845 return dbg->wrapDebuggeeObject(cx, target, result);
1846 }
1847
1848 /* static */
1849 bool DebuggerObject::getBoundThis(JSContext* cx, Handle<DebuggerObject*> object,
1850 MutableHandleValue result) {
1851 MOZ_ASSERT(object->isBoundFunction());
1852
1853 Rooted<BoundFunctionObject*> referent(
1854 cx, &object->referent()->as<BoundFunctionObject>());
1855 Debugger* dbg = object->owner();
1856
1857 result.set(referent->getBoundThis());
1858 return dbg->wrapDebuggeeValue(cx, result);
1859 }
1860
1861 /* static */
1862 bool DebuggerObject::getBoundArguments(JSContext* cx,
1863 Handle<DebuggerObject*> object,
1864 MutableHandle<ValueVector> result) {
1865 MOZ_ASSERT(object->isBoundFunction());
1866
1867 Rooted<BoundFunctionObject*> referent(
1868 cx, &object->referent()->as<BoundFunctionObject>());
1869 Debugger* dbg = object->owner();
1870
1871 size_t length = referent->numBoundArgs();
1872 if (!result.resize(length)) {
1873 return false;
1874 }
1875 for (size_t i = 0; i < length; i++) {
1876 result[i].set(referent->getBoundArg(i));
1877 if (!dbg->wrapDebuggeeValue(cx, result[i])) {
1878 return false;
1879 }
1880 }
1881 return true;
1882 }
1883
1884 /* static */
1885 SavedFrame* Debugger::getObjectAllocationSite(JSObject& obj) {
1886 JSObject* metadata = GetAllocationMetadata(&obj);
1887 if (!metadata) {
1888 return nullptr;
1889 }
1890
1891 MOZ_ASSERT(!metadata->is<WrapperObject>());
1892 return metadata->is<SavedFrame>() ? &metadata->as<SavedFrame>() : nullptr;
1893 }
1894
1895 /* static */
1896 bool DebuggerObject::getAllocationSite(JSContext* cx,
1897 Handle<DebuggerObject*> object,
1898 MutableHandleObject result) {
1899 RootedObject referent(cx, object->referent());
1900
1901 RootedObject allocSite(cx, Debugger::getObjectAllocationSite(*referent));
1902 if (!cx->compartment()->wrap(cx, &allocSite)) {
1903 return false;
1904 }
1905
1906 result.set(allocSite);
1907 return true;
1908 }
1909
1910 /* static */
1911 bool DebuggerObject::getErrorReport(JSContext* cx, HandleObject maybeError,
1912 JSErrorReport*& report) {
1913 JSObject* obj = maybeError;
1914 if (IsCrossCompartmentWrapper(obj)) {
1915 /* We only care about Error objects, so CheckedUnwrapStatic is OK. */
1916 obj = CheckedUnwrapStatic(obj);
1917 }
1918
1919 if (!obj) {
1920 ReportAccessDenied(cx);
1921 return false;
1922 }
1923
1924 if (!obj->is<ErrorObject>()) {
1925 report = nullptr;
1926 return true;
1927 }
1928
1929 report = obj->as<ErrorObject>().getErrorReport();
1930 return true;
1931 }
1932
1933 /* static */
1934 bool DebuggerObject::getErrorMessageName(JSContext* cx,
1935 Handle<DebuggerObject*> object,
1936 MutableHandleString result) {
1937 RootedObject referent(cx, object->referent());
1938 JSErrorReport* report;
1939 if (!getErrorReport(cx, referent, report)) {
1940 return false;
1941 }
1942
1943 if (!report || !report->errorMessageName) {
1944 result.set(nullptr);
1945 return true;
1946 }
1947
1948 RootedString str(cx, JS_NewStringCopyZ(cx, report->errorMessageName));
1949 if (!str) {
1950 return false;
1951 }
1952 result.set(str);
1953 return true;
1954 }
1955
1956 /* static */
1957 bool DebuggerObject::getErrorNotes(JSContext* cx,
1958 Handle<DebuggerObject*> object,
1959 MutableHandleValue result) {
1960 RootedObject referent(cx, object->referent());
1961 JSErrorReport* report;
1962 if (!getErrorReport(cx, referent, report)) {
1963 return false;
1964 }
1965
1966 if (!report) {
1967 result.setUndefined();
1968 return true;
1969 }
1970
1971 RootedObject errorNotesArray(cx, CreateErrorNotesArray(cx, report));
1972 if (!errorNotesArray) {
1973 return false;
1974 }
1975
1976 if (!cx->compartment()->wrap(cx, &errorNotesArray)) {
1977 return false;
1978 }
1979 result.setObject(*errorNotesArray);
1980 return true;
1981 }
1982
1983 /* static */
1984 bool DebuggerObject::getErrorLineNumber(JSContext* cx,
1985 Handle<DebuggerObject*> object,
1986 MutableHandleValue result) {
1987 RootedObject referent(cx, object->referent());
1988 JSErrorReport* report;
1989 if (!getErrorReport(cx, referent, report)) {
1990 return false;
1991 }
1992
1993 if (!report) {
1994 result.setUndefined();
1995 return true;
1996 }
1997
1998 result.setNumber(report->lineno);
1999 return true;
2000 }
2001
2002 /* static */
2003 bool DebuggerObject::getErrorColumnNumber(JSContext* cx,
2004 Handle<DebuggerObject*> object,
2005 MutableHandleValue result) {
2006 RootedObject referent(cx, object->referent());
2007 JSErrorReport* report;
2008 if (!getErrorReport(cx, referent, report)) {
2009 return false;
2010 }
2011
2012 if (!report) {
2013 result.setUndefined();
2014 return true;
2015 }
2016
2017 result.setNumber(report->column.oneOriginValue());
2018 return true;
2019 }
2020
2021 /* static */
2022 bool DebuggerObject::getPromiseValue(JSContext* cx,
2023 Handle<DebuggerObject*> object,
2024 MutableHandleValue result) {
2025 MOZ_ASSERT(object->promiseState() == JS::PromiseState::Fulfilled);
2026
2027 result.set(object->promise()->value());
2028 return object->owner()->wrapDebuggeeValue(cx, result);
2029 }
2030
2031 /* static */
2032 bool DebuggerObject::getPromiseReason(JSContext* cx,
2033 Handle<DebuggerObject*> object,
2034 MutableHandleValue result) {
2035 MOZ_ASSERT(object->promiseState() == JS::PromiseState::Rejected);
2036
2037 result.set(object->promise()->reason());
2038 return object->owner()->wrapDebuggeeValue(cx, result);
2039 }
2040
2041 /* static */
2042 bool DebuggerObject::isExtensible(JSContext* cx, Handle<DebuggerObject*> object,
2043 bool& result) {
2044 RootedObject referent(cx, object->referent());
2045
2046 Maybe<AutoRealm> ar;
2047 EnterDebuggeeObjectRealm(cx, ar, referent);
2048
2049 ErrorCopier ec(ar);
2050 return IsExtensible(cx, referent, &result);
2051 }
2052
2053 /* static */
2054 bool DebuggerObject::isSealed(JSContext* cx, Handle<DebuggerObject*> object,
2055 bool& result) {
2056 RootedObject referent(cx, object->referent());
2057
2058 Maybe<AutoRealm> ar;
2059 EnterDebuggeeObjectRealm(cx, ar, referent);
2060
2061 ErrorCopier ec(ar);
2062 return TestIntegrityLevel(cx, referent, IntegrityLevel::Sealed, &result);
2063 }
2064
2065 /* static */
2066 bool DebuggerObject::isFrozen(JSContext* cx, Handle<DebuggerObject*> object,
2067 bool& result) {
2068 RootedObject referent(cx, object->referent());
2069
2070 Maybe<AutoRealm> ar;
2071 EnterDebuggeeObjectRealm(cx, ar, referent);
2072
2073 ErrorCopier ec(ar);
2074 return TestIntegrityLevel(cx, referent, IntegrityLevel::Frozen, &result);
2075 }
2076
2077 /* static */
2078 bool DebuggerObject::getPrototypeOf(JSContext* cx,
2079 Handle<DebuggerObject*> object,
2080 MutableHandle<DebuggerObject*> result) {
2081 RootedObject referent(cx, object->referent());
2082 Debugger* dbg = object->owner();
2083
2084 RootedObject proto(cx);
2085 {
2086 Maybe<AutoRealm> ar;
2087 EnterDebuggeeObjectRealm(cx, ar, referent);
2088 if (!GetPrototype(cx, referent, &proto)) {
2089 return false;
2090 }
2091 }
2092
2093 return dbg->wrapNullableDebuggeeObject(cx, proto, result);
2094 }
2095
2096 /* static */
2097 bool DebuggerObject::getOwnPropertyNames(JSContext* cx,
2098 Handle<DebuggerObject*> object,
2099 MutableHandleIdVector result) {
2100 MOZ_ASSERT(result.empty());
2101
2102 RootedObject referent(cx, object->referent());
2103 {
2104 Maybe<AutoRealm> ar;
2105 EnterDebuggeeObjectRealm(cx, ar, referent);
2106
2107 ErrorCopier ec(ar);
2108 if (!GetPropertyKeys(cx, referent, JSITER_OWNONLY | JSITER_HIDDEN,
2109 result)) {
2110 return false;
2111 }
2112 }
2113
2114 for (size_t i = 0; i < result.length(); i++) {
2115 cx->markId(result[i]);
2116 }
2117
2118 return true;
2119 }
2120
2121 /* static */
2122 bool DebuggerObject::getOwnPropertyNamesLength(JSContext* cx,
2123 Handle<DebuggerObject*> object,
2124 size_t* result) {
2125 RootedObject referent(cx, object->referent());
2126
2127 RootedIdVector ids(cx);
2128 {
2129 Maybe<AutoRealm> ar;
2130 EnterDebuggeeObjectRealm(cx, ar, referent);
2131
2132 ErrorCopier ec(ar);
2133 if (!GetPropertyKeys(cx, referent, JSITER_OWNONLY | JSITER_HIDDEN, &ids)) {
2134 return false;
2135 }
2136 }
2137
2138 *result = ids.length();
2139 return true;
2140 }
2141
2142 static bool GetSymbolPropertyKeys(JSContext* cx, Handle<DebuggerObject*> object,
2143 JS::MutableHandleIdVector props,
2144 bool includePrivate) {
2145 RootedObject referent(cx, object->referent());
2146
2147 {
2148 Maybe<AutoRealm> ar;
2149 EnterDebuggeeObjectRealm(cx, ar, referent);
2150
2151 ErrorCopier ec(ar);
2152
2153 unsigned flags =
2154 JSITER_OWNONLY | JSITER_HIDDEN | JSITER_SYMBOLS | JSITER_SYMBOLSONLY;
2155 if (includePrivate) {
2156 flags = flags | JSITER_PRIVATE;
2157 }
2158 if (!GetPropertyKeys(cx, referent, flags, props)) {
2159 return false;
2160 }
2161 }
2162
2163 return true;
2164 }
2165
2166 /* static */
2167 bool DebuggerObject::getOwnPropertySymbols(JSContext* cx,
2168 Handle<DebuggerObject*> object,
2169 MutableHandleIdVector result) {
2170 MOZ_ASSERT(result.empty());
2171
2172 if (!GetSymbolPropertyKeys(cx, object, result, false)) {
2173 return false;
2174 }
2175
2176 for (size_t i = 0; i < result.length(); i++) {
2177 cx->markAtom(result[i].toSymbol());
2178 }
2179
2180 return true;
2181 }
2182
2183 /* static */
2184 bool DebuggerObject::getOwnPrivateProperties(JSContext* cx,
2185 Handle<DebuggerObject*> object,
2186 MutableHandleIdVector result) {
2187 MOZ_ASSERT(result.empty());
2188
2189 if (!GetSymbolPropertyKeys(cx, object, result, true)) {
2190 return false;
2191 }
2192
2193 result.eraseIf([](PropertyKey key) {
2194 if (!key.isPrivateName()) {
2195 return true;
2196 }
2197 // Private *methods* create a Private Brand, a special private name
2198 // stamped onto the symbol, to indicate it is possible to execute private
2199 // methods from the class on this object. We don't want to return such
2200 // items here, so we check if we're dealing with a private property, e.g.
2201 // the Symbol description starts with a "#" character.
2202 JSAtom* privateDescription = key.toSymbol()->description();
2203 if (privateDescription->length() == 0) {
2204 return true;
2205 }
2206 char16_t firstChar = privateDescription->latin1OrTwoByteChar(0);
2207 return firstChar != '#';
2208 });
2209
2210 for (size_t i = 0; i < result.length(); i++) {
2211 cx->markAtom(result[i].toSymbol());
2212 }
2213
2214 return true;
2215 }
2216
2217 /* static */
2218 bool DebuggerObject::getOwnPropertyDescriptor(
2219 JSContext* cx, Handle<DebuggerObject*> object, HandleId id,
2220 MutableHandle<Maybe<PropertyDescriptor>> desc_) {
2221 RootedObject referent(cx, object->referent());
2222 Debugger* dbg = object->owner();
2223
2224 // Bug: This can cause the debuggee to run!
2225 {
2226 Maybe<AutoRealm> ar;
2227 EnterDebuggeeObjectRealm(cx, ar, referent);
2228
2229 cx->markId(id);
2230
2231 ErrorCopier ec(ar);
2232 if (!GetOwnPropertyDescriptor(cx, referent, id, desc_)) {
2233 return false;
2234 }
2235 }
2236
2237 if (desc_.isSome()) {
2238 Rooted<PropertyDescriptor> desc(cx, *desc_);
2239
2240 if (desc.hasValue()) {
2241 // Rewrap the debuggee values in desc for the debugger.
2242 if (!dbg->wrapDebuggeeValue(cx, desc.value())) {
2243 return false;
2244 }
2245 }
2246 if (desc.hasGetter()) {
2247 RootedValue get(cx, ObjectOrNullValue(desc.getter()));
2248 if (!dbg->wrapDebuggeeValue(cx, &get)) {
2249 return false;
2250 }
2251 desc.setGetter(get.toObjectOrNull());
2252 }
2253 if (desc.hasSetter()) {
2254 RootedValue set(cx, ObjectOrNullValue(desc.setter()));
2255 if (!dbg->wrapDebuggeeValue(cx, &set)) {
2256 return false;
2257 }
2258 desc.setSetter(set.toObjectOrNull());
2259 }
2260
2261 desc_.set(mozilla::Some(desc.get()));
2262 }
2263
2264 return true;
2265 }
2266
2267 /* static */
2268 bool DebuggerObject::preventExtensions(JSContext* cx,
2269 Handle<DebuggerObject*> object) {
2270 RootedObject referent(cx, object->referent());
2271
2272 Maybe<AutoRealm> ar;
2273 EnterDebuggeeObjectRealm(cx, ar, referent);
2274
2275 ErrorCopier ec(ar);
2276 return PreventExtensions(cx, referent);
2277 }
2278
2279 /* static */
2280 bool DebuggerObject::seal(JSContext* cx, Handle<DebuggerObject*> object) {
2281 RootedObject referent(cx, object->referent());
2282
2283 Maybe<AutoRealm> ar;
2284 EnterDebuggeeObjectRealm(cx, ar, referent);
2285
2286 ErrorCopier ec(ar);
2287 return SetIntegrityLevel(cx, referent, IntegrityLevel::Sealed);
2288 }
2289
2290 /* static */
2291 bool DebuggerObject::freeze(JSContext* cx, Handle<DebuggerObject*> object) {
2292 RootedObject referent(cx, object->referent());
2293
2294 Maybe<AutoRealm> ar;
2295 EnterDebuggeeObjectRealm(cx, ar, referent);
2296
2297 ErrorCopier ec(ar);
2298 return SetIntegrityLevel(cx, referent, IntegrityLevel::Frozen);
2299 }
2300
2301 /* static */
2302 bool DebuggerObject::defineProperty(JSContext* cx,
2303 Handle<DebuggerObject*> object, HandleId id,
2304 Handle<PropertyDescriptor> desc_) {
2305 RootedObject referent(cx, object->referent());
2306 Debugger* dbg = object->owner();
2307
2308 Rooted<PropertyDescriptor> desc(cx, desc_);
2309 if (!dbg->unwrapPropertyDescriptor(cx, referent, &desc)) {
2310 return false;
2311 }
2312 JS_TRY_OR_RETURN_FALSE(cx, CheckPropertyDescriptorAccessors(cx, desc));
2313
2314 Maybe<AutoRealm> ar;
2315 EnterDebuggeeObjectRealm(cx, ar, referent);
2316
2317 if (!cx->compartment()->wrap(cx, &desc)) {
2318 return false;
2319 }
2320 cx->markId(id);
2321
2322 ErrorCopier ec(ar);
2323 return DefineProperty(cx, referent, id, desc);
2324 }
2325
2326 /* static */
2327 bool DebuggerObject::defineProperties(JSContext* cx,
2328 Handle<DebuggerObject*> object,
2329 Handle<IdVector> ids,
2330 Handle<PropertyDescriptorVector> descs_) {
2331 RootedObject referent(cx, object->referent());
2332 Debugger* dbg = object->owner();
2333
2334 Rooted<PropertyDescriptorVector> descs(cx, PropertyDescriptorVector(cx));
2335 if (!descs.append(descs_.begin(), descs_.end())) {
2336 return false;
2337 }
2338 for (size_t i = 0; i < descs.length(); i++) {
2339 if (!dbg->unwrapPropertyDescriptor(cx, referent, descs[i])) {
2340 return false;
2341 }
2342 JS_TRY_OR_RETURN_FALSE(cx, CheckPropertyDescriptorAccessors(cx, descs[i]));
2343 }
2344
2345 Maybe<AutoRealm> ar;
2346 EnterDebuggeeObjectRealm(cx, ar, referent);
2347
2348 for (size_t i = 0; i < descs.length(); i++) {
2349 if (!cx->compartment()->wrap(cx, descs[i])) {
2350 return false;
2351 }
2352 cx->markId(ids[i]);
2353 }
2354
2355 ErrorCopier ec(ar);
2356 for (size_t i = 0; i < descs.length(); i++) {
2357 if (!DefineProperty(cx, referent, ids[i], descs[i])) {
2358 return false;
2359 }
2360 }
2361
2362 return true;
2363 }
2364
2365 /* static */
2366 bool DebuggerObject::deleteProperty(JSContext* cx,
2367 Handle<DebuggerObject*> object, HandleId id,
2368 ObjectOpResult& result) {
2369 RootedObject referent(cx, object->referent());
2370
2371 Maybe<AutoRealm> ar;
2372 EnterDebuggeeObjectRealm(cx, ar, referent);
2373
2374 cx->markId(id);
2375
2376 ErrorCopier ec(ar);
2377 return DeleteProperty(cx, referent, id, result);
2378 }
2379
2380 /* static */
2381 Result<Completion> DebuggerObject::getProperty(JSContext* cx,
2382 Handle<DebuggerObject*> object,
2383 HandleId id,
2384 HandleValue receiver_) {
2385 RootedObject referent(cx, object->referent());
2386 Debugger* dbg = object->owner();
2387
2388 // Unwrap Debugger.Objects. This happens in the debugger's compartment since
2389 // that is where any exceptions must be reported.
2390 RootedValue receiver(cx, receiver_);
2391 if (!dbg->unwrapDebuggeeValue(cx, &receiver)) {
2392 return cx->alreadyReportedError();
2393 }
2394
2395 // Enter the debuggee compartment and rewrap all input value for that
2396 // compartment. (Rewrapping always takes place in the destination
2397 // compartment.)
2398 Maybe<AutoRealm> ar;
2399 EnterDebuggeeObjectRealm(cx, ar, referent);
2400 if (!cx->compartment()->wrap(cx, &referent) ||
2401 !cx->compartment()->wrap(cx, &receiver)) {
2402 return cx->alreadyReportedError();
2403 }
2404 cx->markId(id);
2405
2406 LeaveDebuggeeNoExecute nnx(cx);
2407
2408 RootedValue result(cx);
2409 bool ok = GetProperty(cx, referent, receiver, id, &result);
2410 return Completion::fromJSResult(cx, ok, result);
2411 }
2412
2413 /* static */
2414 Result<Completion> DebuggerObject::setProperty(JSContext* cx,
2415 Handle<DebuggerObject*> object,
2416 HandleId id, HandleValue value_,
2417 HandleValue receiver_) {
2418 RootedObject referent(cx, object->referent());
2419 Debugger* dbg = object->owner();
2420
2421 // Unwrap Debugger.Objects. This happens in the debugger's compartment since
2422 // that is where any exceptions must be reported.
2423 RootedValue value(cx, value_);
2424 RootedValue receiver(cx, receiver_);
2425 if (!dbg->unwrapDebuggeeValue(cx, &value) ||
2426 !dbg->unwrapDebuggeeValue(cx, &receiver)) {
2427 return cx->alreadyReportedError();
2428 }
2429
2430 // Enter the debuggee compartment and rewrap all input value for that
2431 // compartment. (Rewrapping always takes place in the destination
2432 // compartment.)
2433 Maybe<AutoRealm> ar;
2434 EnterDebuggeeObjectRealm(cx, ar, referent);
2435 if (!cx->compartment()->wrap(cx, &referent) ||
2436 !cx->compartment()->wrap(cx, &value) ||
2437 !cx->compartment()->wrap(cx, &receiver)) {
2438 return cx->alreadyReportedError();
2439 }
2440 cx->markId(id);
2441
2442 LeaveDebuggeeNoExecute nnx(cx);
2443
2444 ObjectOpResult opResult;
2445 bool ok = SetProperty(cx, referent, id, value, receiver, opResult);
2446
2447 return Completion::fromJSResult(cx, ok, BooleanValue(ok && opResult.ok()));
2448 }
2449
2450 /* static */
2451 Maybe<Completion> DebuggerObject::call(JSContext* cx,
2452 Handle<DebuggerObject*> object,
2453 HandleValue thisv_,
2454 Handle<ValueVector> args) {
2455 RootedObject referent(cx, object->referent());
2456 Debugger* dbg = object->owner();
2457
2458 if (!referent->isCallable()) {
2459 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
2460 JSMSG_INCOMPATIBLE_PROTO, "Debugger.Object",
2461 "call", referent->getClass()->name);
2462 return Nothing();
2463 }
2464
2465 RootedValue calleev(cx, ObjectValue(*referent));
2466
2467 // Unwrap Debugger.Objects. This happens in the debugger's compartment since
2468 // that is where any exceptions must be reported.
2469 RootedValue thisv(cx, thisv_);
2470 if (!dbg->unwrapDebuggeeValue(cx, &thisv)) {
2471 return Nothing();
2472 }
2473 Rooted<ValueVector> args2(cx, ValueVector(cx));
2474 if (!args2.append(args.begin(), args.end())) {
2475 return Nothing();
2476 }
2477 for (size_t i = 0; i < args2.length(); ++i) {
2478 if (!dbg->unwrapDebuggeeValue(cx, args2[i])) {
2479 return Nothing();
2480 }
2481 }
2482
2483 // Enter the debuggee compartment and rewrap all input value for that
2484 // compartment. (Rewrapping always takes place in the destination
2485 // compartment.)
2486 Maybe<AutoRealm> ar;
2487 EnterDebuggeeObjectRealm(cx, ar, referent);
2488 if (!cx->compartment()->wrap(cx, &calleev) ||
2489 !cx->compartment()->wrap(cx, &thisv)) {
2490 return Nothing();
2491 }
2492 for (size_t i = 0; i < args2.length(); ++i) {
2493 if (!cx->compartment()->wrap(cx, args2[i])) {
2494 return Nothing();
2495 }
2496 }
2497
2498 // Note whether we are in an evaluation that might invoke the OnNativeCall
2499 // hook, so that the JITs will be disabled.
2500 Maybe<AutoNoteExclusiveDebuggerOnEval> noteEvaluation;
2501 if (dbg->isExclusiveDebuggerOnEval()) {
2502 noteEvaluation.emplace(cx, dbg);
2503 }
2504
2505 // Call the function.
2506 LeaveDebuggeeNoExecute nnx(cx);
2507
2508 RootedValue result(cx);
2509 bool ok;
2510 {
2511 InvokeArgs invokeArgs(cx);
2512
2513 ok = invokeArgs.init(cx, args2.length());
2514 if (ok) {
2515 for (size_t i = 0; i < args2.length(); ++i) {
2516 invokeArgs[i].set(args2[i]);
2517 }
2518
2519 ok = js::Call(cx, calleev, thisv, invokeArgs, &result);
2520 }
2521 }
2522
2523 Rooted<Completion> completion(cx, Completion::fromJSResult(cx, ok, result));
2524 ar.reset();
2525 return Some(std::move(completion.get()));
2526 }
2527
2528 /* static */
2529 bool DebuggerObject::forceLexicalInitializationByName(
2530 JSContext* cx, Handle<DebuggerObject*> object, HandleId id, bool& result) {
2531 if (!id.isString()) {
2532 JS_ReportErrorNumberASCII(
2533 cx, GetErrorMessage, nullptr, JSMSG_NOT_EXPECTED_TYPE,
2534 "Debugger.Object.prototype.forceLexicalInitializationByName", "string",
2535 InformalValueTypeName(IdToValue(id)));
2536 return false;
2537 }
2538
2539 MOZ_ASSERT(object->isGlobal());
2540
2541 Rooted<GlobalObject*> referent(cx, &object->referent()->as<GlobalObject>());
2542
2543 // Shape::search can end up allocating a new BaseShape in Shape::cachify so
2544 // we need to be in the right compartment here.
2545 Maybe<AutoRealm> ar;
2546 EnterDebuggeeObjectRealm(cx, ar, referent);
2547
2548 RootedObject globalLexical(cx, &referent->lexicalEnvironment());
2549 RootedObject pobj(cx);
2550 PropertyResult prop;
2551 if (!LookupProperty(cx, globalLexical, id, &pobj, &prop)) {
2552 return false;
2553 }
2554
2555 result = false;
2556 if (prop.isFound()) {
2557 MOZ_ASSERT(prop.isNativeProperty());
2558 PropertyInfo propInfo = prop.propertyInfo();
2559 Value v = globalLexical->as<NativeObject>().getSlot(propInfo.slot());
2560 if (propInfo.isDataProperty() && v.isMagic() &&
2561 v.whyMagic() == JS_UNINITIALIZED_LEXICAL) {
2562 globalLexical->as<NativeObject>().setSlot(propInfo.slot(),
2563 UndefinedValue());
2564 result = true;
2565 }
2566 }
2567
2568 return true;
2569 }
2570
2571 /* static */
2572 Result<Completion> DebuggerObject::executeInGlobal(
2573 JSContext* cx, Handle<DebuggerObject*> object,
2574 mozilla::Range<const char16_t> chars, HandleObject bindings,
2575 const EvalOptions& options) {
2576 MOZ_ASSERT(object->isGlobal());
2577
2578 Rooted<GlobalObject*> referent(cx, &object->referent()->as<GlobalObject>());
2579 Debugger* dbg = object->owner();
2580
2581 RootedObject globalLexical(cx, &referent->lexicalEnvironment());
2582 return DebuggerGenericEval(cx, chars, bindings, options, dbg, globalLexical,
2583 nullptr);
2584 }
2585
2586 /* static */
2587 bool DebuggerObject::makeDebuggeeValue(JSContext* cx,
2588 Handle<DebuggerObject*> object,
2589 HandleValue value_,
2590 MutableHandleValue result) {
2591 RootedObject referent(cx, object->referent());
2592 Debugger* dbg = object->owner();
2593
2594 RootedValue value(cx, value_);
2595
2596 // Non-objects are already debuggee values.
2597 if (value.isObject()) {
2598 // Enter this Debugger.Object's referent's compartment, and wrap the
2599 // argument as appropriate for references from there.
2600 {
2601 Maybe<AutoRealm> ar;
2602 EnterDebuggeeObjectRealm(cx, ar, referent);
2603 if (!cx->compartment()->wrap(cx, &value)) {
2604 return false;
2605 }
2606 }
2607
2608 // Back in the debugger's compartment, produce a new Debugger.Object
2609 // instance referring to the wrapped argument.
2610 if (!dbg->wrapDebuggeeValue(cx, &value)) {
2611 return false;
2612 }
2613 }
2614
2615 result.set(value);
2616 return true;
2617 }
2618
2619 static JSFunction* EnsureNativeFunction(const Value& value) {
2620 if (!value.isObject() || !value.toObject().is<JSFunction>()) {
2621 return nullptr;
2622 }
2623
2624 JSFunction* fun = &value.toObject().as<JSFunction>();
2625 if (!fun->isNativeFun()) {
2626 return nullptr;
2627 }
2628
2629 return fun;
2630 }
2631
2632 static JSAtom* MaybeGetSelfHostedFunctionName(const Value& v) {
2633 if (!v.isObject() || !v.toObject().is<JSFunction>()) {
2634 return nullptr;
2635 }
2636
2637 JSFunction* fun = &v.toObject().as<JSFunction>();
2638 if (!fun->isSelfHostedBuiltin()) {
2639 return nullptr;
2640 }
2641
2642 return GetClonedSelfHostedFunctionName(fun);
2643 }
2644
2645 static bool IsSameNative(JSFunction* a, JSFunction* b,
2646 DebuggerObject::CheckJitInfo checkJitInfo) {
2647 if (a->native() != b->native()) {
2648 return false;
2649 }
2650
2651 if (checkJitInfo == DebuggerObject::CheckJitInfo::No) {
2652 return true;
2653 }
2654
2655 // Both function should agree with the existence of JitInfo.
2656
2657 if (a->hasJitInfo() != b->hasJitInfo()) {
2658 return false;
2659 }
2660
2661 if (!a->hasJitInfo()) {
2662 return true;
2663 }
2664
2665 if (a->jitInfo() == b->jitInfo()) {
2666 return true;
2667 }
2668
2669 return false;
2670 }
2671
2672 /* static */
2673 bool DebuggerObject::isSameNative(JSContext* cx, Handle<DebuggerObject*> object,
2674 HandleValue value, CheckJitInfo checkJitInfo,
2675 MutableHandleValue result) {
2676 RootedValue referentValue(cx, ObjectValue(*object->referent()));
2677
2678 RootedValue nonCCWValue(
2679 cx, value.isObject() ? ObjectValue(*UncheckedUnwrap(&value.toObject()))
2680 : value);
2681
2682 RootedFunction fun(cx, EnsureNativeFunction(nonCCWValue));
2683 if (!fun) {
2684 Rooted<JSAtom*> selfHostedName(cx,
2685 MaybeGetSelfHostedFunctionName(nonCCWValue));
2686 if (!selfHostedName) {
2687 JS_ReportErrorASCII(cx, "Need native function");
2688 return false;
2689 }
2690
2691 result.setBoolean(selfHostedName ==
2692 MaybeGetSelfHostedFunctionName(referentValue));
2693 return true;
2694 }
2695
2696 RootedFunction referentFun(cx, EnsureNativeFunction(referentValue));
2697
2698 result.setBoolean(referentFun &&
2699 IsSameNative(referentFun, fun, checkJitInfo));
2700 return true;
2701 }
2702
2703 static bool IsNativeGetterWithJitInfo(JSFunction* fun) {
2704 return fun->isNativeFun() && fun->hasJitInfo() &&
2705 fun->jitInfo()->type() == JSJitInfo::Getter;
2706 }
2707
2708 /* static */
2709 bool DebuggerObject::isNativeGetterWithJitInfo(JSContext* cx,
2710 Handle<DebuggerObject*> object,
2711 MutableHandleValue result) {
2712 RootedValue referentValue(cx, ObjectValue(*object->referent()));
2713 RootedFunction referentFun(cx, EnsureNativeFunction(referentValue));
2714 result.setBoolean(referentFun && IsNativeGetterWithJitInfo(referentFun));
2715 return true;
2716 }
2717
2718 /* static */
2719 bool DebuggerObject::unsafeDereference(JSContext* cx,
2720 Handle<DebuggerObject*> object,
2721 MutableHandleObject result) {
2722 RootedObject referent(cx, object->referent());
2723
2724 if (!cx->compartment()->wrap(cx, &referent)) {
2725 return false;
2726 }
2727
2728 // Wrapping should return the WindowProxy.
2729 MOZ_ASSERT(!IsWindow(referent));
2730
2731 result.set(referent);
2732 return true;
2733 }
2734
2735 /* static */
2736 bool DebuggerObject::unwrap(JSContext* cx, Handle<DebuggerObject*> object,
2737 MutableHandle<DebuggerObject*> result) {
2738 RootedObject referent(cx, object->referent());
2739 Debugger* dbg = object->owner();
2740
2741 RootedObject unwrapped(cx, UnwrapOneCheckedStatic(referent));
2742
2743 // Don't allow unwrapping to create a D.O whose referent is in an
2744 // invisible-to-Debugger compartment. (If our referent is a *wrapper* to such,
2745 // and the wrapper is in a visible compartment, that's fine.)
2746 if (unwrapped && unwrapped->compartment()->invisibleToDebugger()) {
2747 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
2748 JSMSG_DEBUG_INVISIBLE_COMPARTMENT);
2749 return false;
2750 }
2751
2752 return dbg->wrapNullableDebuggeeObject(cx, unwrapped, result);
2753 }
2754
2755 /* static */
2756 bool DebuggerObject::requireGlobal(JSContext* cx,
2757 Handle<DebuggerObject*> object) {
2758 if (!object->isGlobal()) {
2759 RootedObject referent(cx, object->referent());
2760
2761 const char* isWrapper = "";
2762 const char* isWindowProxy = "";
2763
2764 // Help the poor programmer by pointing out wrappers around globals...
2765 if (referent->is<WrapperObject>()) {
2766 referent = js::UncheckedUnwrap(referent);
2767 isWrapper = "a wrapper around ";
2768 }
2769
2770 // ... and WindowProxies around Windows.
2771 if (IsWindowProxy(referent)) {
2772 referent = ToWindowIfWindowProxy(referent);
2773 isWindowProxy = "a WindowProxy referring to ";
2774 }
2775
2776 RootedValue dbgobj(cx, ObjectValue(*object));
2777 if (referent->is<GlobalObject>()) {
2778 ReportValueError(cx, JSMSG_DEBUG_WRAPPER_IN_WAY, JSDVG_SEARCH_STACK,
2779 dbgobj, nullptr, isWrapper, isWindowProxy);
2780 } else {
2781 ReportValueError(cx, JSMSG_DEBUG_BAD_REFERENT, JSDVG_SEARCH_STACK, dbgobj,
2782 nullptr, "a global object");
2783 }
2784 return false;
2785 }
2786
2787 return true;
2788 }
2789
2790 /* static */
2791 bool DebuggerObject::requirePromise(JSContext* cx,
2792 Handle<DebuggerObject*> object) {
2793 RootedObject referent(cx, object->referent());
2794
2795 if (IsCrossCompartmentWrapper(referent)) {
2796 /* We only care about promises, so CheckedUnwrapStatic is OK. */
2797 referent = CheckedUnwrapStatic(referent);
2798 if (!referent) {
2799 ReportAccessDenied(cx);
2800 return false;
2801 }
2802 }
2803
2804 if (!referent->is<PromiseObject>()) {
2805 JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
2806 JSMSG_NOT_EXPECTED_TYPE, "Debugger", "Promise",
2807 object->getClass()->name);
2808 return false;
2809 }
2810
2811 return true;
2812 }
2813
2814 /* static */
2815 bool DebuggerObject::getScriptedProxyTarget(
2816 JSContext* cx, Handle<DebuggerObject*> object,
2817 MutableHandle<DebuggerObject*> result) {
2818 MOZ_ASSERT(object->isScriptedProxy());
2819 RootedObject referent(cx, object->referent());
2820 Debugger* dbg = object->owner();
2821 RootedObject unwrapped(cx, js::GetProxyTargetObject(referent));
2822
2823 return dbg->wrapNullableDebuggeeObject(cx, unwrapped, result);
2824 }
2825
2826 /* static */
2827 bool DebuggerObject::getScriptedProxyHandler(
2828 JSContext* cx, Handle<DebuggerObject*> object,
2829 MutableHandle<DebuggerObject*> result) {
2830 MOZ_ASSERT(object->isScriptedProxy());
2831 RootedObject referent(cx, object->referent());
2832 Debugger* dbg = object->owner();
2833 RootedObject unwrapped(cx, ScriptedProxyHandler::handlerObject(referent));
2834 return dbg->wrapNullableDebuggeeObject(cx, unwrapped, result);
2835 }