diff options
author | Moonchild <moonchild@palemoon.org> | 2020-11-27 15:47:49 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-11-27 15:47:49 +0000 |
commit | 15914ec5780e7867ab508a48a83311c56950f8a9 (patch) | |
tree | 80562c8ff9e25d119fde8725fa89d171ef20bb42 /js/src/shell/js.cpp | |
parent | b863bd2edc94c16498edc3274531f57fbfc30d3f (diff) | |
download | uxp-15914ec5780e7867ab508a48a83311c56950f8a9.tar.gz |
Issue #1691 - Part 1: Provide a way of associating a private value with a script
or module.
This is a prerequisite for dynamic import
Diffstat (limited to 'js/src/shell/js.cpp')
-rw-r--r-- | js/src/shell/js.cpp | 59 |
1 files changed, 57 insertions, 2 deletions
diff --git a/js/src/shell/js.cpp b/js/src/shell/js.cpp index 6e155d3ff6..acd2ec207f 100644 --- a/js/src/shell/js.cpp +++ b/js/src/shell/js.cpp @@ -4039,12 +4039,12 @@ SetModuleResolveHook(JSContext* cx, unsigned argc, Value* vp) } static JSObject* -CallModuleResolveHook(JSContext* cx, HandleObject module, HandleString specifier) +CallModuleResolveHook(JSContext* cx, HandleValue referencingPrivate, HandleString specifier) { ShellContext* sc = GetShellContext(cx); JS::AutoValueArray<2> args(cx); - args[0].setObject(*module); + args[0].set(referencingPrivate); args[1].setString(specifier); RootedValue result(cx); @@ -4060,6 +4060,53 @@ CallModuleResolveHook(JSContext* cx, HandleObject module, HandleString specifier } static bool +ReportArgumentTypeError(JSContext* cx, HandleValue value, const char* expected) +{ + const char* typeName = InformalValueTypeName(value); + JS_ReportErrorASCII(cx, "Expected %s, got %s", expected, typeName); + return false; +} + +static bool +ShellSetModulePrivate(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + + if (args.length() != 2) { + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_MORE_ARGS_NEEDED, + "setModulePrivate", "0", "s"); + return false; + } + + if (!args[0].isObject() || !args[0].toObject().is<ModuleObject>()) { + return ReportArgumentTypeError(cx, args[0], "module object"); + } + + JS::SetModulePrivate(&args[0].toObject(), args[1]); + args.rval().setUndefined(); + return true; +} + +static bool +ShellGetModulePrivate(JSContext* cx, unsigned argc, Value* vp) +{ + CallArgs args = CallArgsFromVp(argc, vp); + + if (args.length() != 1) { + JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr, JSMSG_MORE_ARGS_NEEDED, + "getModulePrivate", "0", "s"); + return false; + } + + if (!args[0].isObject() || !args[0].toObject().is<ModuleObject>()) { + return ReportArgumentTypeError(cx, args[0], "module object"); + } + + args.rval().set(JS::GetModulePrivate(&args[0].toObject())); + return true; +} + +static bool GetModuleLoadPath(JSContext* cx, unsigned argc, Value* vp) { CallArgs args = CallArgsFromVp(argc, vp); @@ -5927,6 +5974,14 @@ static const JSFunctionSpecWithHelp shell_functions[] = { " This hook is used to look up a previously loaded module object. It should\n" " be implemented by the module loader."), + JS_FN_HELP("setModulePrivate", ShellSetModulePrivate, 2, 0, +"setModulePrivate(scriptObject, privateValue)", +" Associate a private value with a module object.\n"), + + JS_FN_HELP("getModulePrivate", ShellGetModulePrivate, 2, 0, +"getModulePrivate(scriptObject)", +" Get the private value associated with a module object.\n"), + JS_FN_HELP("getModuleLoadPath", GetModuleLoadPath, 0, 0, "getModuleLoadPath()", " Return any --module-load-path argument passed to the shell. Used by the\n" |