From 1e8a4f44fd42158a90bcd26d942e62a570ad7128 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Tue, 21 May 2019 00:57:38 +0000 Subject: [PATCH] Bug 1553001 - Move module APIs out of jsapi.{cpp,h} into js/public/Modules.h and js/src/vm/Modules.cpp for better isolation of module-related APIs. r=arai Differential Revision: https://phabricator.services.mozilla.com/D31917 --HG-- rename : js/src/jsapi.h => js/public/Modules.h rename : js/src/jsapi.cpp => js/src/vm/Modules.cpp extra : moz-landing-system : lando --- dom/base/nsJSUtils.cpp | 1 + dom/script/LoadedScript.cpp | 1 + dom/script/ScriptLoader.cpp | 1 + js/public/Modules.h | 162 ++++++++++++++++++++++++++++++++ js/src/builtin/ModuleObject.cpp | 1 + js/src/jsapi.cpp | 120 ----------------------- js/src/jsapi.h | 126 ------------------------- js/src/moz.build | 2 + js/src/shell/js.cpp | 1 + js/src/vm/Modules.cpp | 160 +++++++++++++++++++++++++++++++ js/src/vm/Runtime.h | 1 + js/src/vm/SelfHosting.cpp | 1 + 12 files changed, 331 insertions(+), 246 deletions(-) create mode 100644 js/public/Modules.h create mode 100644 js/src/vm/Modules.cpp diff --git a/dom/base/nsJSUtils.cpp b/dom/base/nsJSUtils.cpp index 97c9eb8199be..53c06fa03425 100644 --- a/dom/base/nsJSUtils.cpp +++ b/dom/base/nsJSUtils.cpp @@ -15,6 +15,7 @@ #include "jsapi.h" #include "jsfriendapi.h" #include "js/CompilationAndEvaluation.h" +#include "js/Modules.h" // JS::CompileModule, JS::GetModuleScript, JS::Module{Instantiate,Evaluate} #include "js/OffThreadScriptCompilation.h" #include "js/SourceText.h" #include "nsIScriptContext.h" diff --git a/dom/script/LoadedScript.cpp b/dom/script/LoadedScript.cpp index 8b9b686182b5..1cfb96abd053 100644 --- a/dom/script/LoadedScript.cpp +++ b/dom/script/LoadedScript.cpp @@ -9,6 +9,7 @@ #include "mozilla/HoldDropJSObjects.h" #include "jsfriendapi.h" +#include "js/Modules.h" // JS::{Get,Set}ModulePrivate #include "ScriptLoader.h" namespace mozilla { diff --git a/dom/script/ScriptLoader.cpp b/dom/script/ScriptLoader.cpp index 1ce18cb0b2f0..a4a7ea9a27e9 100644 --- a/dom/script/ScriptLoader.cpp +++ b/dom/script/ScriptLoader.cpp @@ -16,6 +16,7 @@ #include "jsfriendapi.h" #include "js/CompilationAndEvaluation.h" #include "js/MemoryFunctions.h" +#include "js/Modules.h" // JS::CompileModule, JS::FinishDynamicModuleImport, JS::{G,S}etModuleResolveHook, JS::Get{ModulePrivate,ModuleScript,RequestedModule{s,Specifier,SourcePos}}, JS::SetModule{DynamicImport,Metadata}Hook #include "js/OffThreadScriptCompilation.h" #include "js/Realm.h" #include "js/SourceText.h" diff --git a/js/public/Modules.h b/js/public/Modules.h new file mode 100644 index 000000000000..b96b21b4fb3d --- /dev/null +++ b/js/public/Modules.h @@ -0,0 +1,162 @@ +/* -*- 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/. */ + +/* JavaScript module (as in, the syntactic construct) operations. */ + +#ifndef js_Modules_h +#define js_Modules_h + +#include // uint32_t + +#include "jstypes.h" // JS_PUBLIC_API + +#include "js/CompileOptions.h" // JS::ReadOnlyCompileOptions +#include "js/RootingAPI.h" // JS::{Mutable,}Handle +#include "js/Value.h" // JS::Value + +struct JSContext; +class JSObject; +struct JSRuntime; +class JSString; + +namespace JS { +template +class SourceText; +} // namespace JS + +namespace JS { + +using ModuleResolveHook = JSObject* (*)(JSContext*, Handle, + Handle); + +/** + * Get the HostResolveImportedModule hook for the runtime. + */ +extern JS_PUBLIC_API ModuleResolveHook GetModuleResolveHook(JSRuntime* rt); + +/** + * Set the HostResolveImportedModule hook for the runtime to the given function. + */ +extern JS_PUBLIC_API void SetModuleResolveHook(JSRuntime* rt, + ModuleResolveHook func); + +using ModuleMetadataHook = bool (*)(JSContext*, Handle, + Handle); + +/** + * Get the hook for populating the import.meta metadata object. + */ +extern JS_PUBLIC_API ModuleMetadataHook GetModuleMetadataHook(JSRuntime* rt); + +/** + * Set the hook for populating the import.meta metadata object to the given + * function. + */ +extern JS_PUBLIC_API void SetModuleMetadataHook(JSRuntime* rt, + ModuleMetadataHook func); + +using ModuleDynamicImportHook = bool (*)(JSContext* cx, + Handle referencingPrivate, + Handle specifier, + Handle promise); + +/** + * Get the HostImportModuleDynamically hook for the runtime. + */ +extern JS_PUBLIC_API ModuleDynamicImportHook +GetModuleDynamicImportHook(JSRuntime* rt); + +/** + * Set the HostImportModuleDynamically hook for the runtime to the given + * function. + * + * If this hook is not set (or set to nullptr) then the JS engine will throw an + * exception if dynamic module import is attempted. + */ +extern JS_PUBLIC_API void SetModuleDynamicImportHook( + JSRuntime* rt, ModuleDynamicImportHook func); + +extern JS_PUBLIC_API bool FinishDynamicModuleImport( + JSContext* cx, Handle referencingPrivate, + Handle specifier, Handle promise); + +/** + * Parse the given source buffer as a module in the scope of the current global + * of cx and return a source text module record. + */ +extern JS_PUBLIC_API bool CompileModule(JSContext* cx, + const ReadOnlyCompileOptions& options, + SourceText& srcBuf, + MutableHandle moduleRecord); + +/** + * Set a private value associated with a source text module record. + */ +extern JS_PUBLIC_API void SetModulePrivate(JSObject* module, + const Value& value); + +/** + * Get the private value associated with a source text module record. + */ +extern JS_PUBLIC_API Value GetModulePrivate(JSObject* module); + +/* + * Perform the ModuleInstantiate operation on the given source text module + * record. + * + * This transitively resolves all module dependencies (calling the + * HostResolveImportedModule hook) and initializes the environment record for + * the module. + */ +extern JS_PUBLIC_API bool ModuleInstantiate(JSContext* cx, + Handle moduleRecord); + +/* + * Perform the ModuleEvaluate operation on the given source text module record. + * + * This does nothing if this module has already been evaluated. Otherwise, it + * transitively evaluates all dependences of this module and then evaluates this + * module. + * + * ModuleInstantiate must have completed prior to calling this. + */ +extern JS_PUBLIC_API bool ModuleEvaluate(JSContext* cx, + Handle moduleRecord); + +/* + * Get a list of the module specifiers used by a source text module + * record to request importation of modules. + * + * The result is a JavaScript array of object values. To extract the individual + * values use only JS_GetArrayLength and JS_GetElement with indices 0 to length + * - 1. + * + * The element values are objects with the following properties: + * - moduleSpecifier: the module specifier string + * - lineNumber: the line number of the import in the source text + * - columnNumber: the column number of the import in the source text + * + * These property values can be extracted with GetRequestedModuleSpecifier() and + * GetRequestedModuleSourcePos() + */ +extern JS_PUBLIC_API JSObject* GetRequestedModules( + JSContext* cx, Handle moduleRecord); + +extern JS_PUBLIC_API JSString* GetRequestedModuleSpecifier( + JSContext* cx, Handle requestedModuleObject); + +extern JS_PUBLIC_API void GetRequestedModuleSourcePos( + JSContext* cx, Handle requestedModuleObject, uint32_t* lineNumber, + uint32_t* columnNumber); + +/* + * Get the top-level script for a module which has not yet been executed. + */ +extern JS_PUBLIC_API JSScript* GetModuleScript(Handle moduleRecord); + +} // namespace JS + +#endif // js_Modules_h diff --git a/js/src/builtin/ModuleObject.cpp b/js/src/builtin/ModuleObject.cpp index 6241284754fb..e0cf814d4e22 100644 --- a/js/src/builtin/ModuleObject.cpp +++ b/js/src/builtin/ModuleObject.cpp @@ -16,6 +16,7 @@ #include "gc/FreeOp.h" #include "gc/Policy.h" #include "gc/Tracer.h" +#include "js/Modules.h" // JS::GetModulePrivate, JS::ModuleDynamicImportHook #include "js/PropertySpec.h" #include "vm/AsyncFunction.h" #include "vm/AsyncIteration.h" diff --git a/js/src/jsapi.cpp b/js/src/jsapi.cpp index 66f15ec5985b..ba07226f1af5 100644 --- a/js/src/jsapi.cpp +++ b/js/src/jsapi.cpp @@ -3689,74 +3689,6 @@ JS_PUBLIC_API JSString* JS_DecompileFunction(JSContext* cx, return FunctionToString(cx, fun, /* isToSource = */ false); } -JS_PUBLIC_API JS::ModuleResolveHook JS::GetModuleResolveHook(JSRuntime* rt) { - AssertHeapIsIdle(); - return rt->moduleResolveHook; -} - -JS_PUBLIC_API void JS::SetModuleResolveHook(JSRuntime* rt, - JS::ModuleResolveHook func) { - AssertHeapIsIdle(); - rt->moduleResolveHook = func; -} - -JS_PUBLIC_API JS::ModuleMetadataHook JS::GetModuleMetadataHook(JSRuntime* rt) { - AssertHeapIsIdle(); - return rt->moduleMetadataHook; -} - -JS_PUBLIC_API void JS::SetModuleMetadataHook(JSRuntime* rt, - JS::ModuleMetadataHook func) { - AssertHeapIsIdle(); - rt->moduleMetadataHook = func; -} - -JS_PUBLIC_API JS::ModuleDynamicImportHook JS::GetModuleDynamicImportHook( - JSRuntime* rt) { - AssertHeapIsIdle(); - return rt->moduleDynamicImportHook; -} - -JS_PUBLIC_API void JS::SetModuleDynamicImportHook( - JSRuntime* rt, JS::ModuleDynamicImportHook func) { - AssertHeapIsIdle(); - rt->moduleDynamicImportHook = func; -} - -JS_PUBLIC_API bool JS::FinishDynamicModuleImport(JSContext* cx, - HandleValue referencingPrivate, - HandleString specifier, - HandleObject promise) { - AssertHeapIsIdle(); - CHECK_THREAD(cx); - cx->check(referencingPrivate, promise); - - return js::FinishDynamicModuleImport(cx, referencingPrivate, specifier, - promise); -} - -JS_PUBLIC_API bool JS::CompileModule(JSContext* cx, - const ReadOnlyCompileOptions& options, - SourceText& srcBuf, - JS::MutableHandleObject module) { - MOZ_ASSERT(!cx->zone()->isAtomsZone()); - AssertHeapIsIdle(); - CHECK_THREAD(cx); - - module.set(frontend::CompileModule(cx, options, srcBuf)); - return !!module; -} - -JS_PUBLIC_API void JS::SetModulePrivate(JSObject* module, - const JS::Value& value) { - JSRuntime* rt = module->zone()->runtimeFromMainThread(); - module->as().scriptSourceObject()->setPrivate(rt, value); -} - -JS_PUBLIC_API JS::Value JS::GetModulePrivate(JSObject* module) { - return module->as().scriptSourceObject()->canonicalPrivate(); -} - JS_PUBLIC_API void JS::SetScriptPrivate(JSScript* script, const JS::Value& value) { JSRuntime* rt = script->zone()->runtimeFromMainThread(); @@ -3787,58 +3719,6 @@ JS_PUBLIC_API void JS::SetScriptPrivateReferenceHooks( rt->scriptPrivateReleaseHook = releaseHook; } -JS_PUBLIC_API bool JS::ModuleInstantiate(JSContext* cx, - JS::HandleObject moduleArg) { - AssertHeapIsIdle(); - CHECK_THREAD(cx); - cx->releaseCheck(moduleArg); - return ModuleObject::Instantiate(cx, moduleArg.as()); -} - -JS_PUBLIC_API bool JS::ModuleEvaluate(JSContext* cx, - JS::HandleObject moduleArg) { - AssertHeapIsIdle(); - CHECK_THREAD(cx); - cx->releaseCheck(moduleArg); - return ModuleObject::Evaluate(cx, moduleArg.as()); -} - -JS_PUBLIC_API JSObject* JS::GetRequestedModules(JSContext* cx, - JS::HandleObject moduleArg) { - AssertHeapIsIdle(); - CHECK_THREAD(cx); - cx->check(moduleArg); - return &moduleArg->as().requestedModules(); -} - -JS_PUBLIC_API JSString* JS::GetRequestedModuleSpecifier(JSContext* cx, - JS::HandleValue value) { - AssertHeapIsIdle(); - CHECK_THREAD(cx); - cx->check(value); - JSObject* obj = &value.toObject(); - return obj->as().moduleSpecifier(); -} - -JS_PUBLIC_API void JS::GetRequestedModuleSourcePos(JSContext* cx, - JS::HandleValue value, - uint32_t* lineNumber, - uint32_t* columnNumber) { - AssertHeapIsIdle(); - CHECK_THREAD(cx); - cx->check(value); - MOZ_ASSERT(lineNumber); - MOZ_ASSERT(columnNumber); - auto& requested = value.toObject().as(); - *lineNumber = requested.lineNumber(); - *columnNumber = requested.columnNumber(); -} - -JS_PUBLIC_API JSScript* JS::GetModuleScript(JS::HandleObject moduleRecord) { - AssertHeapIsIdle(); - return moduleRecord->as().script(); -} - JS_PUBLIC_API JSObject* JS_New(JSContext* cx, HandleObject ctor, const JS::HandleValueArray& inputArgs) { AssertHeapIsIdle(); diff --git a/js/src/jsapi.h b/js/src/jsapi.h index 21908759fa73..6148d4b41e28 100644 --- a/js/src/jsapi.h +++ b/js/src/jsapi.h @@ -1905,78 +1905,6 @@ extern JS_PUBLIC_API JSString* JS_DecompileFunction( namespace JS { -using ModuleResolveHook = JSObject* (*)(JSContext*, HandleValue, HandleString); - -/** - * Get the HostResolveImportedModule hook for the runtime. - */ -extern JS_PUBLIC_API ModuleResolveHook GetModuleResolveHook(JSRuntime* rt); - -/** - * Set the HostResolveImportedModule hook for the runtime to the given function. - */ -extern JS_PUBLIC_API void SetModuleResolveHook(JSRuntime* rt, - ModuleResolveHook func); - -using ModuleMetadataHook = bool (*)(JSContext*, HandleValue, HandleObject); - -/** - * Get the hook for populating the import.meta metadata object. - */ -extern JS_PUBLIC_API ModuleMetadataHook GetModuleMetadataHook(JSRuntime* rt); - -/** - * Set the hook for populating the import.meta metadata object to the given - * function. - */ -extern JS_PUBLIC_API void SetModuleMetadataHook(JSRuntime* rt, - ModuleMetadataHook func); - -using ModuleDynamicImportHook = bool (*)(JSContext* cx, - HandleValue referencingPrivate, - HandleString specifier, - HandleObject promise); - -/** - * Get the HostImportModuleDynamically hook for the runtime. - */ -extern JS_PUBLIC_API ModuleDynamicImportHook -GetModuleDynamicImportHook(JSRuntime* rt); - -/** - * Set the HostImportModuleDynamically hook for the runtime to the given - * function. - * - * If this hook is not set (or set to nullptr) then the JS engine will throw an - * exception if dynamic module import is attempted. - */ -extern JS_PUBLIC_API void SetModuleDynamicImportHook( - JSRuntime* rt, ModuleDynamicImportHook func); - -extern JS_PUBLIC_API bool FinishDynamicModuleImport( - JSContext* cx, HandleValue referencingPrivate, HandleString specifier, - HandleObject promise); - -/** - * Parse the given source buffer as a module in the scope of the current global - * of cx and return a source text module record. - */ -extern JS_PUBLIC_API bool CompileModule(JSContext* cx, - const ReadOnlyCompileOptions& options, - SourceText& srcBuf, - JS::MutableHandleObject moduleRecord); - -/** - * Set a private value associated with a source text module record. - */ -extern JS_PUBLIC_API void SetModulePrivate(JSObject* module, - const JS::Value& value); - -/** - * Get the private value associated with a source text module record. - */ -extern JS_PUBLIC_API JS::Value GetModulePrivate(JSObject* module); - /** * Set a private value associated with a script. Note that this value is shared * by all nested scripts compiled from a single source file. @@ -2010,60 +1938,6 @@ extern JS_PUBLIC_API void SetScriptPrivateReferenceHooks( JSRuntime* rt, ScriptPrivateReferenceHook addRefHook, ScriptPrivateReferenceHook releaseHook); -/* - * Perform the ModuleInstantiate operation on the given source text module - * record. - * - * This transitively resolves all module dependencies (calling the - * HostResolveImportedModule hook) and initializes the environment record for - * the module. - */ -extern JS_PUBLIC_API bool ModuleInstantiate(JSContext* cx, - JS::HandleObject moduleRecord); - -/* - * Perform the ModuleEvaluate operation on the given source text module record. - * - * This does nothing if this module has already been evaluated. Otherwise, it - * transitively evaluates all dependences of this module and then evaluates this - * module. - * - * ModuleInstantiate must have completed prior to calling this. - */ -extern JS_PUBLIC_API bool ModuleEvaluate(JSContext* cx, - JS::HandleObject moduleRecord); - -/* - * Get a list of the module specifiers used by a source text module - * record to request importation of modules. - * - * The result is a JavaScript array of object values. To extract the individual - * values use only JS_GetArrayLength and JS_GetElement with indices 0 to length - * - 1. - * - * The element values are objects with the following properties: - * - moduleSpecifier: the module specifier string - * - lineNumber: the line number of the import in the source text - * - columnNumber: the column number of the import in the source text - * - * These property values can be extracted with GetRequestedModuleSpecifier() and - * GetRequestedModuleSourcePos() - */ -extern JS_PUBLIC_API JSObject* GetRequestedModules( - JSContext* cx, JS::HandleObject moduleRecord); - -extern JS_PUBLIC_API JSString* GetRequestedModuleSpecifier( - JSContext* cx, JS::HandleValue requestedModuleObject); - -extern JS_PUBLIC_API void GetRequestedModuleSourcePos( - JSContext* cx, JS::HandleValue requestedModuleObject, uint32_t* lineNumber, - uint32_t* columnNumber); - -/* - * Get the top-level script for a module which has not yet been executed. - */ -extern JS_PUBLIC_API JSScript* GetModuleScript(JS::HandleObject moduleRecord); - } /* namespace JS */ #if defined(JS_BUILD_BINAST) diff --git a/js/src/moz.build b/js/src/moz.build index b898fe3c3a53..0c05e108ee78 100755 --- a/js/src/moz.build +++ b/js/src/moz.build @@ -146,6 +146,7 @@ EXPORTS.js += [ '../public/LocaleSensitive.h', '../public/MemoryFunctions.h', '../public/MemoryMetrics.h', + '../public/Modules.h', '../public/OffThreadScriptCompilation.h', '../public/Principals.h', '../public/Printf.h', @@ -304,6 +305,7 @@ UNIFIED_SOURCES += [ 'vm/JSScript.cpp', 'vm/List.cpp', 'vm/MemoryMetrics.cpp', + 'vm/Modules.cpp', 'vm/NativeObject.cpp', 'vm/ObjectGroup.cpp', 'vm/OffThreadScriptCompilation.cpp', diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 944e918f2aa4..bbdee264bf07 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -98,6 +98,7 @@ #include "js/Initialization.h" #include "js/JSON.h" #include "js/MemoryFunctions.h" +#include "js/Modules.h" // JS::GetModulePrivate, JS::SetModule{DynamicImport,Metadata,Resolve}Hook, JS::SetModulePrivate #include "js/Printf.h" #include "js/PropertySpec.h" #include "js/Realm.h" diff --git a/js/src/vm/Modules.cpp b/js/src/vm/Modules.cpp new file mode 100644 index 000000000000..059c12e38fb4 --- /dev/null +++ b/js/src/vm/Modules.cpp @@ -0,0 +1,160 @@ +/* -*- 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/. */ + +/* JavaScript modules (as in, the syntactic construct) implementation. */ + +#include "js/Modules.h" + +#include "mozilla/Assertions.h" // MOZ_ASSERT + +#include // uint32_t + +#include "jsapi.h" // js::AssertHeapIsIdle +#include "jstypes.h" // JS_PUBLIC_API + +#include "builtin/ModuleObject.h" // js::FinishDynamicModuleImport, js::{,Requested}ModuleObject +#include "frontend/BytecodeCompiler.h" // js::frontend::CompileModule +#include "js/RootingAPI.h" // JS::MutableHandle +#include "js/Value.h" // JS::Value +#include "vm/JSContext.h" // CHECK_THREAD, JSContext +#include "vm/JSObject.h" // JSObject +#include "vm/Runtime.h" // JSRuntime + +#include "vm/JSContext-inl.h" // JSContext::{c,releaseC}heck + +using js::AssertHeapIsIdle; +using js::ModuleObject; +using js::RequestedModuleObject; + +JS_PUBLIC_API JS::ModuleResolveHook JS::GetModuleResolveHook(JSRuntime* rt) { + AssertHeapIsIdle(); + + return rt->moduleResolveHook; +} + +JS_PUBLIC_API void JS::SetModuleResolveHook(JSRuntime* rt, + ModuleResolveHook func) { + AssertHeapIsIdle(); + + rt->moduleResolveHook = func; +} + +JS_PUBLIC_API JS::ModuleMetadataHook JS::GetModuleMetadataHook(JSRuntime* rt) { + AssertHeapIsIdle(); + + return rt->moduleMetadataHook; +} + +JS_PUBLIC_API void JS::SetModuleMetadataHook(JSRuntime* rt, + ModuleMetadataHook func) { + AssertHeapIsIdle(); + + rt->moduleMetadataHook = func; +} + +JS_PUBLIC_API JS::ModuleDynamicImportHook JS::GetModuleDynamicImportHook( + JSRuntime* rt) { + AssertHeapIsIdle(); + + return rt->moduleDynamicImportHook; +} + +JS_PUBLIC_API void JS::SetModuleDynamicImportHook( + JSRuntime* rt, ModuleDynamicImportHook func) { + AssertHeapIsIdle(); + + rt->moduleDynamicImportHook = func; +} + +JS_PUBLIC_API bool JS::FinishDynamicModuleImport( + JSContext* cx, Handle referencingPrivate, + Handle specifier, Handle promise) { + AssertHeapIsIdle(); + CHECK_THREAD(cx); + cx->check(referencingPrivate, promise); + + return js::FinishDynamicModuleImport(cx, referencingPrivate, specifier, + promise); +} + +JS_PUBLIC_API bool JS::CompileModule(JSContext* cx, + const ReadOnlyCompileOptions& options, + SourceText& srcBuf, + MutableHandle module) { + MOZ_ASSERT(!cx->zone()->isAtomsZone()); + AssertHeapIsIdle(); + CHECK_THREAD(cx); + + module.set(js::frontend::CompileModule(cx, options, srcBuf)); + return !!module; +} + +JS_PUBLIC_API void JS::SetModulePrivate(JSObject* module, const Value& value) { + JSRuntime* rt = module->zone()->runtimeFromMainThread(); + module->as().scriptSourceObject()->setPrivate(rt, value); +} + +JS_PUBLIC_API JS::Value JS::GetModulePrivate(JSObject* module) { + return module->as().scriptSourceObject()->canonicalPrivate(); +} + +JS_PUBLIC_API bool JS::ModuleInstantiate(JSContext* cx, + Handle moduleArg) { + AssertHeapIsIdle(); + CHECK_THREAD(cx); + cx->releaseCheck(moduleArg); + + return ModuleObject::Instantiate(cx, moduleArg.as()); +} + +JS_PUBLIC_API bool JS::ModuleEvaluate(JSContext* cx, + Handle moduleArg) { + AssertHeapIsIdle(); + CHECK_THREAD(cx); + cx->releaseCheck(moduleArg); + + return ModuleObject::Evaluate(cx, moduleArg.as()); +} + +JS_PUBLIC_API JSObject* JS::GetRequestedModules(JSContext* cx, + Handle moduleArg) { + AssertHeapIsIdle(); + CHECK_THREAD(cx); + cx->check(moduleArg); + + return &moduleArg->as().requestedModules(); +} + +JS_PUBLIC_API JSString* JS::GetRequestedModuleSpecifier(JSContext* cx, + Handle value) { + AssertHeapIsIdle(); + CHECK_THREAD(cx); + cx->check(value); + + JSObject* obj = &value.toObject(); + return obj->as().moduleSpecifier(); +} + +JS_PUBLIC_API void JS::GetRequestedModuleSourcePos(JSContext* cx, + JS::HandleValue value, + uint32_t* lineNumber, + uint32_t* columnNumber) { + AssertHeapIsIdle(); + CHECK_THREAD(cx); + cx->check(value); + MOZ_ASSERT(lineNumber); + MOZ_ASSERT(columnNumber); + + auto& requested = value.toObject().as(); + *lineNumber = requested.lineNumber(); + *columnNumber = requested.columnNumber(); +} + +JS_PUBLIC_API JSScript* JS::GetModuleScript(JS::HandleObject moduleRecord) { + AssertHeapIsIdle(); + + return moduleRecord->as().script(); +} diff --git a/js/src/vm/Runtime.h b/js/src/vm/Runtime.h index 0ed459ffff2f..541b8f52b7d3 100644 --- a/js/src/vm/Runtime.h +++ b/js/src/vm/Runtime.h @@ -34,6 +34,7 @@ #include "js/experimental/SourceHook.h" // js::SourceHook #include "js/GCVector.h" #include "js/HashTable.h" +#include "js/Modules.h" // JS::Module{DynamicImport,Metadata,Resolve}Hook #ifdef DEBUG # include "js/Proxy.h" // For AutoEnterPolicy #endif diff --git a/js/src/vm/SelfHosting.cpp b/js/src/vm/SelfHosting.cpp index 442bbdfa16c1..055c98aa25c3 100644 --- a/js/src/vm/SelfHosting.cpp +++ b/js/src/vm/SelfHosting.cpp @@ -44,6 +44,7 @@ #include "js/CharacterEncoding.h" #include "js/CompilationAndEvaluation.h" #include "js/Date.h" +#include "js/Modules.h" // JS::GetModulePrivate #include "js/PropertySpec.h" #include "js/SourceText.h" // JS::SourceText #include "js/StableStringChars.h"