summaryrefslogtreecommitdiffstats
path: root/dom/bindings/Exceptions.cpp
blob: 6e61c4ee95fcb609d0a55641c536377dfef5533d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/Exceptions.h"

#include "js/RootingAPI.h"
#include "js/TypeDecls.h"
#include "jsapi.h"
#include "js/SavedFrameAPI.h"
#include "xpcpublic.h"
#include "mozilla/CycleCollectedJSContext.h"
#include "mozilla/HoldDropJSObjects.h"
#include "mozilla/dom/BindingUtils.h"
#include "mozilla/dom/DOMException.h"
#include "mozilla/dom/ScriptSettings.h"
#include "nsJSPrincipals.h"
#include "nsPIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"
#include "XPCWrapper.h"
#include "WorkerPrivate.h"
#include "nsContentUtils.h"

namespace mozilla::dom {

// Throw the given exception value if it's safe.  If it's not safe, then
// synthesize and throw a new exception value for NS_ERROR_UNEXPECTED.  The
// incoming value must be in the compartment of aCx.  This function guarantees
// that an exception is pending on aCx when it returns.
static void ThrowExceptionValueIfSafe(JSContext* aCx,
                                      JS::Handle<JS::Value> exnVal,
                                      Exception* aOriginalException) {
  MOZ_ASSERT(aOriginalException);

  if (!exnVal.isObject()) {
    JS_SetPendingException(aCx, exnVal);
    return;
  }

  JS::Rooted<JSObject*> exnObj(aCx, &exnVal.toObject());
  MOZ_ASSERT(js::IsObjectInContextCompartment(exnObj, aCx),
             "exnObj needs to be in the right compartment for the "
             "CheckedUnwrapDynamic thing to make sense");

  // aCx's current Realm is where we're throwing, so using it in the
  // CheckedUnwrapDynamic check makes sense.
  if (js::CheckedUnwrapDynamic(exnObj, aCx)) {
    // This is an object we're allowed to work with, so just go ahead and throw
    // it.
    JS_SetPendingException(aCx, exnVal);
    return;
  }

  // We could probably Throw(aCx, NS_ERROR_UNEXPECTED) here, and it would do the
  // right thing due to there not being an existing exception on the runtime at
  // this point, but it's clearer to explicitly do the thing we want done.  This
  // is also why we don't just call ThrowExceptionObject on the Exception we
  // create: it would do the right thing, but that fact is not obvious.
  RefPtr<Exception> syntheticException = CreateException(NS_ERROR_UNEXPECTED);
  JS::Rooted<JS::Value> syntheticVal(aCx);
  if (!GetOrCreateDOMReflector(aCx, syntheticException, &syntheticVal)) {
    return;
  }
  MOZ_ASSERT(
      syntheticVal.isObject() && !js::IsWrapper(&syntheticVal.toObject()),
      "Must have a reflector here, not a wrapper");
  JS_SetPendingException(aCx, syntheticVal);
}

void ThrowExceptionObject(JSContext* aCx, Exception* aException) {
  JS::Rooted<JS::Value> thrown(aCx);

  // If we stored the original thrown JS value in the exception
  // (see XPCConvert::ConstructException) and we are in a web context
  // (i.e., not chrome), rethrow the original value. This only applies to JS
  // implemented components so we only need to check for this on the main
  // thread.
  if (NS_IsMainThread() && !nsContentUtils::IsCallerChrome() &&
      aException->StealJSVal(thrown.address())) {
    // Now check for the case when thrown is a number which matches
    // aException->GetResult().  This would indicate that what actually got
    // thrown was an nsresult value.  In that situation, we should go back
    // through dom::Throw with that nsresult value, because it will make sure to
    // create the right sort of Exception or DOMException, with the right
    // global.
    if (thrown.isNumber()) {
      nsresult exceptionResult = aException->GetResult();
      if (double(exceptionResult) == thrown.toNumber()) {
        Throw(aCx, exceptionResult);
        return;
      }
    }
    if (!JS_WrapValue(aCx, &thrown)) {
      return;
    }
    ThrowExceptionValueIfSafe(aCx, thrown, aException);
    return;
  }

  if (!GetOrCreateDOMReflector(aCx, aException, &thrown)) {
    return;
  }

  ThrowExceptionValueIfSafe(aCx, thrown, aException);
}

bool Throw(JSContext* aCx, nsresult aRv, const nsACString& aMessage) {
  if (aRv == NS_ERROR_UNCATCHABLE_EXCEPTION) {
    // Nuke any existing exception on aCx, to make sure we're uncatchable.
    JS_ClearPendingException(aCx);
    return false;
  }

  if (JS_IsExceptionPending(aCx)) {
    // Don't clobber the existing exception.
    return false;
  }

  CycleCollectedJSContext* context = CycleCollectedJSContext::Get();
  RefPtr<Exception> existingException = context->GetPendingException();
  // Make sure to clear the pending exception now.  Either we're going to reuse
  // it (and we already grabbed it), or we plan to throw something else and this
  // pending exception is no longer relevant.
  context->SetPendingException(nullptr);

  // Ignore the pending exception if we have a non-default message passed in.
  if (aMessage.IsEmpty() && existingException) {
    if (aRv == existingException->GetResult()) {
      // Reuse the existing exception.
      ThrowExceptionObject(aCx, existingException);
      return false;
    }
  }

  RefPtr<Exception> finalException = CreateException(aRv, aMessage);
  MOZ_ASSERT(finalException);

  ThrowExceptionObject(aCx, finalException);
  return false;
}

void ThrowAndReport(nsPIDOMWindowInner* aWindow, nsresult aRv) {
  MOZ_ASSERT(aRv != NS_ERROR_UNCATCHABLE_EXCEPTION,
             "Doesn't make sense to report uncatchable exceptions!");
  AutoJSAPI jsapi;
  if (NS_WARN_IF(!jsapi.Init(aWindow))) {
    return;
  }

  Throw(jsapi.cx(), aRv);
}

already_AddRefed<Exception> CreateException(nsresult aRv,
                                            const nsACString& aMessage) {
  // Do we use DOM exceptions for this error code?
  switch (NS_ERROR_GET_MODULE(aRv)) {
    case NS_ERROR_MODULE_DOM:
    case NS_ERROR_MODULE_SVG:
    case NS_ERROR_MODULE_DOM_FILE:
    case NS_ERROR_MODULE_DOM_XPATH:
    case NS_ERROR_MODULE_DOM_INDEXEDDB:
    case NS_ERROR_MODULE_DOM_FILEHANDLE:
    case NS_ERROR_MODULE_DOM_ANIM:
    case NS_ERROR_MODULE_DOM_PUSH:
    case NS_ERROR_MODULE_DOM_MEDIA:
      if (aMessage.IsEmpty()) {
        return DOMException::Create(aRv);
      }
      return DOMException::Create(aRv, aMessage);
    default:
      break;
  }

  // If not, use the default.
  RefPtr<Exception> exception =
      new Exception(aMessage, aRv, ""_ns, nullptr, nullptr);
  return exception.forget();
}

already_AddRefed<nsIStackFrame> GetCurrentJSStack(int32_t aMaxDepth) {
  // is there a current context available?
  JSContext* cx = nsContentUtils::GetCurrentJSContext();

  if (!cx || !js::GetContextRealm(cx)) {
    return nullptr;
  }

  static const unsigned MAX_FRAMES = 100;
  if (aMaxDepth < 0) {
    aMaxDepth = MAX_FRAMES;
  }

  JS::StackCapture captureMode =
      aMaxDepth == 0 ? JS::StackCapture(JS::AllFrames())
                     : JS::StackCapture(JS::MaxFrames(aMaxDepth));

  return dom::exceptions::CreateStack(cx, std::move(captureMode));
}

namespace exceptions {

class JSStackFrame final : public nsIStackFrame, public xpc::JSStackFrameBase {
 public:
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(JSStackFrame)
  NS_DECL_NSISTACKFRAME

  // aStack must not be null.
  explicit JSStackFrame(JS::Handle<JSObject*> aStack);

 private:
  virtual ~JSStackFrame();

  void Clear() override { mStack = nullptr; }

  // Remove this frame from the per-realm list of live frames,
  // and clear out the stack pointer.
  void UnregisterAndClear();

  JS::Heap<JSObject*> mStack;
  nsString mFormattedStack;

  nsCOMPtr<nsIStackFrame> mCaller;
  nsCOMPtr<nsIStackFrame> mAsyncCaller;
  nsString mFilename;
  nsString mFunname;
  nsString mAsyncCause;
  int32_t mSourceId;
  int32_t mLineno;
  int32_t mColNo;

  bool mFilenameInitialized;
  bool mFunnameInitialized;
  bool mSourceIdInitialized;
  bool mLinenoInitialized;
  bool mColNoInitialized;
  bool mAsyncCauseInitialized;
  bool mAsyncCallerInitialized;
  bool mCallerInitialized;
  bool mFormattedStackInitialized;
};

JSStackFrame::JSStackFrame(JS::Handle<JSObject*> aStack)
    : mStack(aStack),
      mSourceId(0),
      mLineno(0),
      mColNo(0),
      mFilenameInitialized(false),
      mFunnameInitialized(false),
      mSourceIdInitialized(false),
      mLinenoInitialized(false),
      mColNoInitialized(false),
      mAsyncCauseInitialized(false),
      mAsyncCallerInitialized(false),
      mCallerInitialized(false),
      mFormattedStackInitialized(false) {
  MOZ_ASSERT(mStack);
  MOZ_ASSERT(JS::IsUnwrappedSavedFrame(mStack));

  mozilla::HoldJSObjects(this);

  xpc::RegisterJSStackFrame(js::GetNonCCWObjectRealm(aStack), this);
}

JSStackFrame::~JSStackFrame() {
  UnregisterAndClear();
  mozilla::DropJSObjects(this);
}

void JSStackFrame::UnregisterAndClear() {
  if (!mStack) {
    return;
  }

  xpc::UnregisterJSStackFrame(js::GetNonCCWObjectRealm(mStack), this);
  Clear();
}

NS_IMPL_CYCLE_COLLECTION_CLASS(JSStackFrame)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(JSStackFrame)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mCaller)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mAsyncCaller)
  tmp->UnregisterAndClear();
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(JSStackFrame)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCaller)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAsyncCaller)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(JSStackFrame)
  NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mStack)
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(JSStackFrame)
NS_IMPL_CYCLE_COLLECTING_RELEASE(JSStackFrame)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(JSStackFrame)
  NS_INTERFACE_MAP_ENTRY(nsIStackFrame)
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

// Helper method to determine the JSPrincipals* to pass to JS SavedFrame APIs.
//
// @argument aStack the stack we're working with; must be non-null.
// @argument [out] aCanCache whether we can use cached JSStackFrame values.
static JSPrincipals* GetPrincipalsForStackGetter(JSContext* aCx,
                                                 JS::Handle<JSObject*> aStack,
                                                 bool* aCanCache) {
  MOZ_ASSERT(JS::IsUnwrappedSavedFrame(aStack));

  JSPrincipals* currentPrincipals =
      JS::GetRealmPrincipals(js::GetContextRealm(aCx));
  JSPrincipals* stackPrincipals =
      JS::GetRealmPrincipals(js::GetNonCCWObjectRealm(aStack));

  // Fast path for when the principals are equal. This check is also necessary
  // for workers: no nsIPrincipal there so we can't use the code below.
  if (currentPrincipals == stackPrincipals) {
    *aCanCache = true;
    return stackPrincipals;
  }

  MOZ_ASSERT(NS_IsMainThread());

  if (nsJSPrincipals::get(currentPrincipals)
          ->Subsumes(nsJSPrincipals::get(stackPrincipals))) {
    // The current principals subsume the stack's principals. In this case use
    // the stack's principals: the idea is that this way devtools code that's
    // asking an exception object for a stack to display will end up with the
    // stack the web developer would see via doing .stack in a web page, with
    // Firefox implementation details excluded.

    // Because we use the stack's principals and don't rely on the current
    // context realm, we can use cached values.
    *aCanCache = true;
    return stackPrincipals;
  }

  // The stack was captured in more-privileged code, so use the less privileged
  // principals. Don't use cached values because we don't want these values to
  // depend on the current realm/principals.
  *aCanCache = false;
  return currentPrincipals;
}

// Helper method to get the value of a stack property, if it's not already
// cached.  This will make sure we skip the cache if the property value depends
// on the (current) context's realm/principals.
//
// @argument aStack the stack we're working with; must be non-null.
// @argument aPropGetter the getter function to call.
// @argument aIsCached whether we've cached this property's value before.
//
// @argument [out] aCanCache whether the value can get cached.
// @argument [out] aUseCachedValue if true, just use the cached value.
// @argument [out] aValue the value we got from the stack.
template <typename ReturnType, typename GetterOutParamType>
static void GetValueIfNotCached(
    JSContext* aCx, const JS::Heap<JSObject*>& aStack,
    JS::SavedFrameResult (*aPropGetter)(JSContext*, JSPrincipals*,
                                        JS::Handle<JSObject*>,
                                        GetterOutParamType,
                                        JS::SavedFrameSelfHosted),
    bool aIsCached, bool* aCanCache, bool* aUseCachedValue, ReturnType aValue) {
  MOZ_ASSERT(aStack);
  MOZ_ASSERT(JS::IsUnwrappedSavedFrame(aStack));

  JS::Rooted<JSObject*> stack(aCx, aStack);

  JSPrincipals* principals = GetPrincipalsForStackGetter(aCx, stack, aCanCache);
  if (*aCanCache && aIsCached) {
    *aUseCachedValue = true;
    return;
  }

  *aUseCachedValue = false;

  aPropGetter(aCx, principals, stack, aValue,
              JS::SavedFrameSelfHosted::Exclude);
}

NS_IMETHODIMP JSStackFrame::GetFilenameXPCOM(JSContext* aCx,
                                             nsAString& aFilename) {
  GetFilename(aCx, aFilename);
  return NS_OK;
}

void JSStackFrame::GetFilename(JSContext* aCx, nsAString& aFilename) {
  if (!mStack) {
    aFilename.Truncate();
    return;
  }

  JS::Rooted<JSString*> filename(aCx);
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameSource,
                      mFilenameInitialized, &canCache, &useCachedValue,
                      &filename);
  if (useCachedValue) {
    aFilename = mFilename;
    return;
  }

  nsAutoJSString str;
  if (!str.init(aCx, filename)) {
    JS_ClearPendingException(aCx);
    aFilename.Truncate();
    return;
  }
  aFilename = str;

  if (canCache) {
    mFilename = str;
    mFilenameInitialized = true;
  }
}

NS_IMETHODIMP
JSStackFrame::GetNameXPCOM(JSContext* aCx, nsAString& aFunction) {
  GetName(aCx, aFunction);
  return NS_OK;
}

void JSStackFrame::GetName(JSContext* aCx, nsAString& aFunction) {
  if (!mStack) {
    aFunction.Truncate();
    return;
  }

  JS::Rooted<JSString*> name(aCx);
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameFunctionDisplayName,
                      mFunnameInitialized, &canCache, &useCachedValue, &name);

  if (useCachedValue) {
    aFunction = mFunname;
    return;
  }

  if (name) {
    nsAutoJSString str;
    if (!str.init(aCx, name)) {
      JS_ClearPendingException(aCx);
      aFunction.Truncate();
      return;
    }
    aFunction = str;
  } else {
    aFunction.SetIsVoid(true);
  }

  if (canCache) {
    mFunname = aFunction;
    mFunnameInitialized = true;
  }
}

int32_t JSStackFrame::GetSourceId(JSContext* aCx) {
  if (!mStack) {
    return 0;
  }

  uint32_t id;
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameSourceId,
                      mSourceIdInitialized, &canCache, &useCachedValue, &id);

  if (useCachedValue) {
    return mSourceId;
  }

  if (canCache) {
    mSourceId = id;
    mSourceIdInitialized = true;
  }

  return id;
}

NS_IMETHODIMP
JSStackFrame::GetSourceIdXPCOM(JSContext* aCx, int32_t* aSourceId) {
  *aSourceId = GetSourceId(aCx);
  return NS_OK;
}

int32_t JSStackFrame::GetLineNumber(JSContext* aCx) {
  if (!mStack) {
    return 0;
  }

  uint32_t line;
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameLine, mLinenoInitialized,
                      &canCache, &useCachedValue, &line);

  if (useCachedValue) {
    return mLineno;
  }

  if (canCache) {
    mLineno = line;
    mLinenoInitialized = true;
  }

  return line;
}

NS_IMETHODIMP
JSStackFrame::GetLineNumberXPCOM(JSContext* aCx, int32_t* aLineNumber) {
  *aLineNumber = GetLineNumber(aCx);
  return NS_OK;
}

int32_t JSStackFrame::GetColumnNumber(JSContext* aCx) {
  if (!mStack) {
    return 0;
  }

  uint32_t col;
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameColumn, mColNoInitialized,
                      &canCache, &useCachedValue, &col);

  if (useCachedValue) {
    return mColNo;
  }

  if (canCache) {
    mColNo = col;
    mColNoInitialized = true;
  }

  return col;
}

NS_IMETHODIMP
JSStackFrame::GetColumnNumberXPCOM(JSContext* aCx, int32_t* aColumnNumber) {
  *aColumnNumber = GetColumnNumber(aCx);
  return NS_OK;
}

NS_IMETHODIMP JSStackFrame::GetSourceLine(nsACString& aSourceLine) {
  aSourceLine.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
JSStackFrame::GetAsyncCauseXPCOM(JSContext* aCx, nsAString& aAsyncCause) {
  GetAsyncCause(aCx, aAsyncCause);
  return NS_OK;
}

void JSStackFrame::GetAsyncCause(JSContext* aCx, nsAString& aAsyncCause) {
  if (!mStack) {
    aAsyncCause.Truncate();
    return;
  }

  JS::Rooted<JSString*> asyncCause(aCx);
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameAsyncCause,
                      mAsyncCauseInitialized, &canCache, &useCachedValue,
                      &asyncCause);

  if (useCachedValue) {
    aAsyncCause = mAsyncCause;
    return;
  }

  if (asyncCause) {
    nsAutoJSString str;
    if (!str.init(aCx, asyncCause)) {
      JS_ClearPendingException(aCx);
      aAsyncCause.Truncate();
      return;
    }
    aAsyncCause = str;
  } else {
    aAsyncCause.SetIsVoid(true);
  }

  if (canCache) {
    mAsyncCause = aAsyncCause;
    mAsyncCauseInitialized = true;
  }
}

NS_IMETHODIMP
JSStackFrame::GetAsyncCallerXPCOM(JSContext* aCx,
                                  nsIStackFrame** aAsyncCaller) {
  *aAsyncCaller = GetAsyncCaller(aCx).take();
  return NS_OK;
}

already_AddRefed<nsIStackFrame> JSStackFrame::GetAsyncCaller(JSContext* aCx) {
  if (!mStack) {
    return nullptr;
  }

  JS::Rooted<JSObject*> asyncCallerObj(aCx);
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameAsyncParent,
                      mAsyncCallerInitialized, &canCache, &useCachedValue,
                      &asyncCallerObj);

  if (useCachedValue) {
    nsCOMPtr<nsIStackFrame> asyncCaller = mAsyncCaller;
    return asyncCaller.forget();
  }

  nsCOMPtr<nsIStackFrame> asyncCaller =
      asyncCallerObj ? new JSStackFrame(asyncCallerObj) : nullptr;

  if (canCache) {
    mAsyncCaller = asyncCaller;
    mAsyncCallerInitialized = true;
  }

  return asyncCaller.forget();
}

NS_IMETHODIMP
JSStackFrame::GetCallerXPCOM(JSContext* aCx, nsIStackFrame** aCaller) {
  *aCaller = GetCaller(aCx).take();
  return NS_OK;
}

already_AddRefed<nsIStackFrame> JSStackFrame::GetCaller(JSContext* aCx) {
  if (!mStack) {
    return nullptr;
  }

  JS::Rooted<JSObject*> callerObj(aCx);
  bool canCache = false, useCachedValue = false;
  GetValueIfNotCached(aCx, mStack, JS::GetSavedFrameParent, mCallerInitialized,
                      &canCache, &useCachedValue, &callerObj);

  if (useCachedValue) {
    nsCOMPtr<nsIStackFrame> caller = mCaller;
    return caller.forget();
  }

  nsCOMPtr<nsIStackFrame> caller =
      callerObj ? new JSStackFrame(callerObj) : nullptr;

  if (canCache) {
    mCaller = caller;
    mCallerInitialized = true;
  }

  return caller.forget();
}

NS_IMETHODIMP
JSStackFrame::GetFormattedStackXPCOM(JSContext* aCx, nsAString& aStack) {
  GetFormattedStack(aCx, aStack);
  return NS_OK;
}

void JSStackFrame::GetFormattedStack(JSContext* aCx, nsAString& aStack) {
  if (!mStack) {
    aStack.Truncate();
    return;
  }

  // Sadly we can't use GetValueIfNotCached here, because our getter
  // returns bool, not JS::SavedFrameResult.  Maybe it's possible to
  // make the templates more complicated to deal, but in the meantime
  // let's just inline GetValueIfNotCached here.

  JS::Rooted<JSObject*> stack(aCx, mStack);

  bool canCache;
  JSPrincipals* principals = GetPrincipalsForStackGetter(aCx, stack, &canCache);
  if (canCache && mFormattedStackInitialized) {
    aStack = mFormattedStack;
    return;
  }

  JS::Rooted<JSString*> formattedStack(aCx);
  if (!JS::BuildStackString(aCx, principals, stack, &formattedStack)) {
    JS_ClearPendingException(aCx);
    aStack.Truncate();
    return;
  }

  nsAutoJSString str;
  if (!str.init(aCx, formattedStack)) {
    JS_ClearPendingException(aCx);
    aStack.Truncate();
    return;
  }

  aStack = str;

  if (canCache) {
    mFormattedStack = str;
    mFormattedStackInitialized = true;
  }
}

NS_IMETHODIMP JSStackFrame::GetNativeSavedFrame(
    JS::MutableHandle<JS::Value> aSavedFrame) {
  aSavedFrame.setObjectOrNull(mStack);
  return NS_OK;
}

NS_IMETHODIMP
JSStackFrame::ToStringXPCOM(JSContext* aCx, nsACString& _retval) {
  ToString(aCx, _retval);
  return NS_OK;
}

void JSStackFrame::ToString(JSContext* aCx, nsACString& _retval) {
  _retval.Truncate();

  nsString filename;
  GetFilename(aCx, filename);

  if (filename.IsEmpty()) {
    filename.AssignLiteral("<unknown filename>");
  }

  nsString funname;
  GetName(aCx, funname);

  if (funname.IsEmpty()) {
    funname.AssignLiteral("<TOP_LEVEL>");
  }

  int32_t lineno = GetLineNumber(aCx);

  static const char format[] = "JS frame :: %s :: %s :: line %d";
  _retval.AppendPrintf(format, NS_ConvertUTF16toUTF8(filename).get(),
                       NS_ConvertUTF16toUTF8(funname).get(), lineno);
}

already_AddRefed<nsIStackFrame> CreateStack(JSContext* aCx,
                                            JS::StackCapture&& aCaptureMode) {
  JS::Rooted<JSObject*> stack(aCx);
  if (!JS::CaptureCurrentStack(aCx, &stack, std::move(aCaptureMode))) {
    return nullptr;
  }

  return CreateStack(aCx, stack);
}

already_AddRefed<nsIStackFrame> CreateStack(JSContext* aCx,
                                            JS::Handle<JSObject*> aStack) {
  if (aStack) {
    return MakeAndAddRef<JSStackFrame>(aStack);
  }
  return nullptr;
}

}  // namespace exceptions
}  // namespace mozilla::dom