From 086c044dc34dfc0f74fbe41f4ecb402b2cd34884 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:13:33 +0200 Subject: Merging upstream version 125.0.1. Signed-off-by: Daniel Baumann --- js/xpconnect/loader/ChromeScriptLoader.cpp | 9 ++- js/xpconnect/loader/mozJSModuleLoader.cpp | 50 +++++++------ js/xpconnect/loader/mozJSModuleLoader.h | 9 ++- js/xpconnect/loader/nsImportModule.cpp | 35 +-------- js/xpconnect/loader/nsImportModule.h | 112 ----------------------------- 5 files changed, 40 insertions(+), 175 deletions(-) (limited to 'js/xpconnect/loader') diff --git a/js/xpconnect/loader/ChromeScriptLoader.cpp b/js/xpconnect/loader/ChromeScriptLoader.cpp index 5c7115c997..d67c41d014 100644 --- a/js/xpconnect/loader/ChromeScriptLoader.cpp +++ b/js/xpconnect/loader/ChromeScriptLoader.cpp @@ -17,8 +17,8 @@ #include "js/CompileOptions.h" // JS::CompileOptions, JS::OwningCompileOptions #include "js/CompilationAndEvaluation.h" #include "js/experimental/CompileScript.h" // JS::CompileGlobalScriptToStencil, JS::NewFrontendContext, JS::DestroyFrontendContext, JS::SetNativeStackQuota, JS::ThreadStackQuotaForSize, JS::HadFrontendErrors, JS::ConvertFrontendErrorsToRuntimeErrors -#include "js/experimental/JSStencil.h" // JS::Stencil, JS::CompileGlobalScriptToStencil, JS::InstantiateGlobalStencil, JS::CompilationStorage -#include "js/SourceText.h" // JS::SourceText +#include "js/experimental/JSStencil.h" // JS::Stencil, JS::CompileGlobalScriptToStencil, JS::InstantiateGlobalStencil +#include "js/SourceText.h" // JS::SourceText #include "js/Utility.h" #include "mozilla/AlreadyAddRefed.h" // already_AddRefed @@ -119,9 +119,8 @@ class AsyncScriptCompileTask final : public Task { JS::SetNativeStackQuota(mFrontendContext, JS::ThreadStackQuotaForSize(stackSize)); - JS::CompilationStorage compileStorage; - mStencil = JS::CompileGlobalScriptToStencil(mFrontendContext, mOptions, - mSrcBuf, compileStorage); + mStencil = + JS::CompileGlobalScriptToStencil(mFrontendContext, mOptions, mSrcBuf); } // Cancel the task. diff --git a/js/xpconnect/loader/mozJSModuleLoader.cpp b/js/xpconnect/loader/mozJSModuleLoader.cpp index f20306f0a0..017ac32b3b 100644 --- a/js/xpconnect/loader/mozJSModuleLoader.cpp +++ b/js/xpconnect/loader/mozJSModuleLoader.cpp @@ -454,6 +454,12 @@ void mozJSModuleLoader::InitStatics() { MOZ_ASSERT(!sSelf); sSelf = new mozJSModuleLoader(); RegisterWeakMemoryReporter(sSelf); + + dom::AutoJSAPI jsapi; + jsapi.Init(); + JSContext* cx = jsapi.cx(); + sSelf->InitSharedGlobal(cx); + NonSharedGlobalSyncModuleLoaderScope::InitStatics(); } @@ -486,12 +492,16 @@ void mozJSModuleLoader::ShutdownLoaders() { } } -mozJSModuleLoader* mozJSModuleLoader::GetOrCreateDevToolsLoader() { +mozJSModuleLoader* mozJSModuleLoader::GetOrCreateDevToolsLoader( + JSContext* aCx) { if (sDevToolsLoader) { return sDevToolsLoader; } sDevToolsLoader = new mozJSModuleLoader(); RegisterWeakMemoryReporter(sDevToolsLoader); + + sDevToolsLoader->InitSharedGlobal(aCx); + return sDevToolsLoader; } @@ -682,26 +692,22 @@ void mozJSModuleLoader::CreateLoaderGlobal(JSContext* aCx, aGlobal.set(global); } -JSObject* mozJSModuleLoader::GetSharedGlobal(JSContext* aCx) { - if (!mLoaderGlobal) { - JS::RootedObject globalObj(aCx); +void mozJSModuleLoader::InitSharedGlobal(JSContext* aCx) { + JS::RootedObject globalObj(aCx); - CreateLoaderGlobal( - aCx, IsDevToolsLoader() ? "DevTools global"_ns : "shared JSM global"_ns, - &globalObj); + CreateLoaderGlobal( + aCx, IsDevToolsLoader() ? "DevTools global"_ns : "shared JSM global"_ns, + &globalObj); - // If we fail to create a module global this early, we're not going to - // get very far, so just bail out now. - MOZ_RELEASE_ASSERT(globalObj); - mLoaderGlobal = globalObj; - - // AutoEntryScript required to invoke debugger hook, which is a - // Gecko-specific concept at present. - dom::AutoEntryScript aes(globalObj, "module loader report global"); - JS_FireOnNewGlobalObject(aes.cx(), globalObj); - } + // If we fail to create a module global this early, we're not going to + // get very far, so just bail out now. + MOZ_RELEASE_ASSERT(globalObj); + mLoaderGlobal = globalObj; - return mLoaderGlobal; + // AutoEntryScript required to invoke debugger hook, which is a + // Gecko-specific concept at present. + dom::AutoEntryScript aes(globalObj, "module loader report global"); + JS_FireOnNewGlobalObject(aes.cx(), globalObj); } // Read script file on the main thread and pass it back to worker. @@ -970,8 +976,8 @@ JSObject* mozJSModuleLoader::PrepareObjectForLocation(JSContext* aCx, nsIFile* aModuleFile, nsIURI* aURI, bool aRealFile) { - RootedObject globalObj(aCx, GetSharedGlobal(aCx)); - NS_ENSURE_TRUE(globalObj, nullptr); + RootedObject globalObj(aCx, GetSharedGlobal()); + MOZ_ASSERT(globalObj); JSAutoRealm ar(aCx, globalObj); // |thisObj| is the object we set properties on for a particular .jsm. @@ -2018,8 +2024,8 @@ nsresult mozJSModuleLoader::ImportESModule( MarkerInnerWindowIdFromJSContext(aCx)), Substring(aLocation, 0, std::min(size_t(128), aLocation.Length()))); - RootedObject globalObj(aCx, GetSharedGlobal(aCx)); - NS_ENSURE_TRUE(globalObj, NS_ERROR_FAILURE); + RootedObject globalObj(aCx, GetSharedGlobal()); + MOZ_ASSERT(globalObj); MOZ_ASSERT_IF(NS_IsMainThread(), xpc::Scriptability::Get(globalObj).Allowed()); diff --git a/js/xpconnect/loader/mozJSModuleLoader.h b/js/xpconnect/loader/mozJSModuleLoader.h index 0b8e5f85d9..ac118c507d 100644 --- a/js/xpconnect/loader/mozJSModuleLoader.h +++ b/js/xpconnect/loader/mozJSModuleLoader.h @@ -76,9 +76,14 @@ class mozJSModuleLoader final : public nsIMemoryReporter { return sSelf; } - JSObject* GetSharedGlobal(JSContext* aCx); + JSObject* GetSharedGlobal() { + MOZ_ASSERT(mLoaderGlobal); + return mLoaderGlobal; + } private: + void InitSharedGlobal(JSContext* aCx); + void InitSyncModuleLoaderForGlobal(nsIGlobalObject* aGlobal); void DisconnectSyncModuleLoaderFromGlobal(); @@ -86,7 +91,7 @@ class mozJSModuleLoader final : public nsIMemoryReporter { public: static mozJSModuleLoader* GetDevToolsLoader() { return sDevToolsLoader; } - static mozJSModuleLoader* GetOrCreateDevToolsLoader(); + static mozJSModuleLoader* GetOrCreateDevToolsLoader(JSContext* aCx); nsresult ImportInto(const nsACString& aResourceURI, JS::HandleValue aTargetObj, JSContext* aCx, uint8_t aArgc, diff --git a/js/xpconnect/loader/nsImportModule.cpp b/js/xpconnect/loader/nsImportModule.cpp index a313c44388..3720a444a6 100644 --- a/js/xpconnect/loader/nsImportModule.cpp +++ b/js/xpconnect/loader/nsImportModule.cpp @@ -39,44 +39,11 @@ static void AnnotateCrashReportWithJSException(JSContext* aCx, aURI, file.get(), line, column, NS_ConvertUTF16toUTF8(msg).get()); - CrashReporter::AnnotateCrashReport( + CrashReporter::RecordAnnotationNSCString( CrashReporter::Annotation::JSModuleLoadError, errorString); } } -nsresult ImportModule(const char* aURI, const char* aExportName, - const nsIID& aIID, void** aResult, bool aInfallible) { - AutoJSAPI jsapi; - MOZ_ALWAYS_TRUE(jsapi.Init(xpc::PrivilegedJunkScope())); - JSContext* cx = jsapi.cx(); - - JS::RootedObject global(cx); - JS::RootedObject exports(cx); - nsresult rv = mozJSModuleLoader::Get()->Import(cx, nsDependentCString(aURI), - &global, &exports); - if (NS_WARN_IF(NS_FAILED(rv))) { - if (aInfallible) { - AnnotateCrashReportWithJSException(cx, aURI); - - MOZ_CRASH_UNSAFE_PRINTF("Failed to load critical module \"%s\"", aURI); - } - return rv; - } - - if (aExportName) { - JS::RootedValue namedExport(cx); - if (!JS_GetProperty(cx, exports, aExportName, &namedExport)) { - return NS_ERROR_FAILURE; - } - if (!namedExport.isObject()) { - return NS_ERROR_XPC_BAD_CONVERT_JS; - } - exports.set(&namedExport.toObject()); - } - - return nsXPConnect::XPConnect()->WrapJS(cx, exports, aIID, aResult); -} - nsresult ImportESModule(const char* aURI, const char* aExportName, const nsIID& aIID, void** aResult, bool aInfallible) { AutoJSAPI jsapi; diff --git a/js/xpconnect/loader/nsImportModule.h b/js/xpconnect/loader/nsImportModule.h index 31f6f8c7c1..16a5c40a88 100644 --- a/js/xpconnect/loader/nsImportModule.h +++ b/js/xpconnect/loader/nsImportModule.h @@ -16,124 +16,12 @@ namespace mozilla { namespace loader { -nsresult ImportModule(const char* aURI, const char* aExportName, - const nsIID& aIID, void** aResult, bool aInfallible); - nsresult ImportESModule(const char* aURI, const char* aExportName, const nsIID& aIID, void** aResult, bool aInfallible); } // namespace loader } // namespace mozilla -class MOZ_STACK_CLASS nsImportModule final : public nsCOMPtr_helper { - public: - nsImportModule(const char* aURI, const char* aExportName, nsresult* aErrorPtr, - bool aInfallible) - : mURI(aURI), - mExportName(aExportName), - mErrorPtr(aErrorPtr), - mInfallible(aInfallible) { - MOZ_ASSERT_IF(mErrorPtr, !mInfallible); - } - - virtual nsresult NS_FASTCALL operator()(const nsIID& aIID, - void** aResult) const override { - nsresult rv = ::mozilla::loader::ImportModule(mURI, mExportName, aIID, - aResult, mInfallible); - if (mErrorPtr) { - *mErrorPtr = rv; - } - return rv; - } - - private: - const char* mURI; - const char* mExportName; - nsresult* mErrorPtr; - bool mInfallible; -}; - -/** - * These helpers make it considerably easier for C++ code to import a JS module - * and wrap it in an appropriately-defined XPIDL interface for its exports. - * Typical usage is something like: - * - * Foo.jsm: - * - * var EXPORTED_SYMBOLS = ["foo"]; - * - * function foo(bar) { - * return bar.toString(); - * } - * - * mozIFoo.idl: - * - * interface mozIFoo : nsISupports { - * AString foo(double meh); - * } - * - * Thing.cpp: - * - * nsCOMPtr foo = do_ImportModule( - * "resource://meh/Foo.jsm"); - * - * MOZ_TRY(foo->Foo(42)); - * - * For JS modules which export all fields within a single named object, a second - * argument can be passed naming that object. - * - * Foo.jsm: - * - * var EXPORTED_SYMBOLS = ["Foo"]; - * - * var Foo = { - * function foo(bar) { - * return bar.toString(); - * } - * }; - * - * Thing.cpp: - * - * nsCOMPtr foo = do_ImportModule( - * "resource:://meh/Foo.jsm", "Foo"); - */ - -template -inline nsImportModule do_ImportModule(const char (&aURI)[N]) { - return {aURI, nullptr, nullptr, /* infallible */ true}; -} - -template -inline nsImportModule do_ImportModule(const char (&aURI)[N], - const mozilla::fallible_t&) { - return {aURI, nullptr, nullptr, /* infallible */ false}; -} - -template -inline nsImportModule do_ImportModule(const char (&aURI)[N], nsresult* aRv) { - return {aURI, nullptr, aRv, /* infallible */ false}; -} - -template -inline nsImportModule do_ImportModule(const char (&aURI)[N], - const char (&aExportName)[N2]) { - return {aURI, aExportName, nullptr, /* infallible */ true}; -} - -template -inline nsImportModule do_ImportModule(const char (&aURI)[N], - const char (&aExportName)[N2], - const mozilla::fallible_t&) { - return {aURI, aExportName, nullptr, /* infallible */ false}; -} - -template -inline nsImportModule do_ImportModule(const char (&aURI)[N], - const char (&aExportName)[N2], - nsresult* aRv) { - return {aURI, aExportName, aRv, /* infallible */ false}; -} - class MOZ_STACK_CLASS nsImportESModule final : public nsCOMPtr_helper { public: nsImportESModule(const char* aURI, const char* aExportName, -- cgit v1.2.3