Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / dom / script / AutoEntryScript.cpp
blobed58e3a1fd2b4a3017bf10739a587b9e4146e4c5
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/. */
7 #include "mozilla/dom/AutoEntryScript.h"
9 #include <stdint.h>
10 #include <utility>
11 #include "js/ProfilingCategory.h"
12 #include "js/ProfilingStack.h"
13 #include "jsapi.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/Span.h"
17 #include "nsCOMPtr.h"
18 #include "nsContentUtils.h"
19 #include "nsGlobalWindowInner.h"
20 #include "nsIDocShell.h"
21 #include "nsJSUtils.h"
22 #include "nsPIDOMWindow.h"
23 #include "nsPIDOMWindowInlines.h"
24 #include "nsString.h"
25 #include "xpcpublic.h"
27 namespace mozilla::dom {
29 namespace {
30 // Assert if it's not safe to run script. The helper class
31 // AutoAllowLegacyScriptExecution allows to allow-list
32 // legacy cases where it's actually not safe to run script.
33 #ifdef DEBUG
34 void AssertIfNotSafeToRunScript() {
35 // if it's safe to run script, then there is nothing to do here.
36 if (nsContentUtils::IsSafeToRunScript()) {
37 return;
40 // auto allowing legacy script execution is fine for now.
41 if (AutoAllowLegacyScriptExecution::IsAllowed()) {
42 return;
45 MOZ_ASSERT(false, "is it safe to run script?");
47 #endif
49 } // namespace
51 AutoEntryScript::AutoEntryScript(nsIGlobalObject* aGlobalObject,
52 const char* aReason, bool aIsMainThread)
53 : AutoJSAPI(aGlobalObject, aIsMainThread, eEntryScript),
54 mWebIDLCallerPrincipal(nullptr)
55 // This relies on us having a cx() because the AutoJSAPI constructor
56 // already ran.
58 mCallerOverride(cx()),
59 mAutoProfilerLabel(
60 "", aReason, JS::ProfilingCategoryPair::JS,
61 uint32_t(js::ProfilingStackFrame::Flags::RELEVANT_FOR_JS)),
62 mJSThreadExecution(aGlobalObject, aIsMainThread) {
63 MOZ_ASSERT(aGlobalObject);
65 if (aIsMainThread) {
66 #ifdef DEBUG
67 AssertIfNotSafeToRunScript();
68 #endif
69 mScriptActivity.emplace(true);
73 AutoEntryScript::AutoEntryScript(JSObject* aObject, const char* aReason,
74 bool aIsMainThread)
75 : AutoEntryScript(xpc::NativeGlobal(aObject), aReason, aIsMainThread) {
76 // xpc::NativeGlobal uses JS::GetNonCCWObjectGlobal, which asserts that
77 // aObject is not a CCW.
80 AutoEntryScript::~AutoEntryScript() = default;
82 } // namespace mozilla::dom