Bug 1719459 - Part 1: Move module testing function to JS shell. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D119584
This commit is contained in:
Tooru Fujisawa 2021-07-12 16:02:20 +00:00
Родитель abe3033d39
Коммит e819448fb3
2 изменённых файлов: 102 добавлений и 103 удалений

Просмотреть файл

@ -42,7 +42,6 @@
# include "builtin/intl/CommonFunctions.h"
# include "builtin/intl/SharedIntlData.h"
#endif
#include "builtin/ModuleObject.h"
#include "builtin/Promise.h"
#include "builtin/SelfHostingDefines.h"
#include "builtin/TestingUtility.h" // js::ParseCompileOptions
@ -6496,100 +6495,6 @@ static bool SetRNGState(JSContext* cx, unsigned argc, Value* vp) {
}
#endif
static ModuleEnvironmentObject* GetModuleEnvironment(
JSContext* cx, HandleModuleObject module) {
// Use the initial environment so that tests can check bindings exists
// before they have been instantiated.
RootedModuleEnvironmentObject env(cx, &module->initialEnvironment());
MOZ_ASSERT(env);
return env;
}
static bool GetModuleEnvironmentNames(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() != 1) {
JS_ReportErrorASCII(cx, "Wrong number of arguments");
return false;
}
if (!args[0].isObject() || !args[0].toObject().is<ModuleObject>()) {
JS_ReportErrorASCII(cx, "First argument should be a ModuleObject");
return false;
}
RootedModuleObject module(cx, &args[0].toObject().as<ModuleObject>());
if (module->hadEvaluationError()) {
JS_ReportErrorASCII(cx, "Module environment unavailable");
return false;
}
RootedModuleEnvironmentObject env(cx, GetModuleEnvironment(cx, module));
Rooted<IdVector> ids(cx, IdVector(cx));
if (!JS_Enumerate(cx, env, &ids)) {
return false;
}
// The "*namespace*" binding is a detail of current implementation so hide
// it to give stable results in tests.
ids.eraseIfEqual(NameToId(cx->names().starNamespaceStar));
uint32_t length = ids.length();
RootedArrayObject array(cx, NewDenseFullyAllocatedArray(cx, length));
if (!array) {
return false;
}
array->setDenseInitializedLength(length);
for (uint32_t i = 0; i < length; i++) {
array->initDenseElement(i, StringValue(JSID_TO_STRING(ids[i])));
}
args.rval().setObject(*array);
return true;
}
static bool GetModuleEnvironmentValue(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() != 2) {
JS_ReportErrorASCII(cx, "Wrong number of arguments");
return false;
}
if (!args[0].isObject() || !args[0].toObject().is<ModuleObject>()) {
JS_ReportErrorASCII(cx, "First argument should be a ModuleObject");
return false;
}
if (!args[1].isString()) {
JS_ReportErrorASCII(cx, "Second argument should be a string");
return false;
}
RootedModuleObject module(cx, &args[0].toObject().as<ModuleObject>());
if (module->hadEvaluationError()) {
JS_ReportErrorASCII(cx, "Module environment unavailable");
return false;
}
RootedModuleEnvironmentObject env(cx, GetModuleEnvironment(cx, module));
RootedString name(cx, args[1].toString());
RootedId id(cx);
if (!JS_StringToId(cx, name, &id)) {
return false;
}
if (!GetProperty(cx, env, env, id, args.rval())) {
return false;
}
if (args.rval().isMagic(JS_UNINITIALIZED_LEXICAL)) {
ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
return false;
}
return true;
}
static bool GetTimeZone(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
RootedObject callee(cx, &args.callee());
@ -8333,14 +8238,6 @@ JS_FOR_WASM_FEATURES(WASM_FEATURE, WASM_FEATURE)
" Set this compartment's RNG state.\n"),
#endif
JS_FN_HELP("getModuleEnvironmentNames", GetModuleEnvironmentNames, 1, 0,
"getModuleEnvironmentNames(module)",
" Get the list of a module environment's bound names for a specified module.\n"),
JS_FN_HELP("getModuleEnvironmentValue", GetModuleEnvironmentValue, 2, 0,
"getModuleEnvironmentValue(module, name)",
" Get the value of a bound name in a module environment.\n"),
#if defined(FUZZING) && defined(__AFL_COMPILER)
JS_FN_HELP("aflloop", AflLoop, 1, 0,
"aflloop(max_cnt)",

Просмотреть файл

@ -5703,6 +5703,100 @@ static bool RegisterModule(JSContext* cx, unsigned argc, Value* vp) {
return true;
}
static ModuleEnvironmentObject* GetModuleEnvironment(
JSContext* cx, HandleModuleObject module) {
// Use the initial environment so that tests can check bindings exists
// before they have been instantiated.
RootedModuleEnvironmentObject env(cx, &module->initialEnvironment());
MOZ_ASSERT(env);
return env;
}
static bool GetModuleEnvironmentNames(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() != 1) {
JS_ReportErrorASCII(cx, "Wrong number of arguments");
return false;
}
if (!args[0].isObject() || !args[0].toObject().is<ModuleObject>()) {
JS_ReportErrorASCII(cx, "First argument should be a ModuleObject");
return false;
}
RootedModuleObject module(cx, &args[0].toObject().as<ModuleObject>());
if (module->hadEvaluationError()) {
JS_ReportErrorASCII(cx, "Module environment unavailable");
return false;
}
RootedModuleEnvironmentObject env(cx, GetModuleEnvironment(cx, module));
Rooted<IdVector> ids(cx, IdVector(cx));
if (!JS_Enumerate(cx, env, &ids)) {
return false;
}
// The "*namespace*" binding is a detail of current implementation so hide
// it to give stable results in tests.
ids.eraseIfEqual(NameToId(cx->names().starNamespaceStar));
uint32_t length = ids.length();
RootedArrayObject array(cx, NewDenseFullyAllocatedArray(cx, length));
if (!array) {
return false;
}
array->setDenseInitializedLength(length);
for (uint32_t i = 0; i < length; i++) {
array->initDenseElement(i, StringValue(JSID_TO_STRING(ids[i])));
}
args.rval().setObject(*array);
return true;
}
static bool GetModuleEnvironmentValue(JSContext* cx, unsigned argc, Value* vp) {
CallArgs args = CallArgsFromVp(argc, vp);
if (args.length() != 2) {
JS_ReportErrorASCII(cx, "Wrong number of arguments");
return false;
}
if (!args[0].isObject() || !args[0].toObject().is<ModuleObject>()) {
JS_ReportErrorASCII(cx, "First argument should be a ModuleObject");
return false;
}
if (!args[1].isString()) {
JS_ReportErrorASCII(cx, "Second argument should be a string");
return false;
}
RootedModuleObject module(cx, &args[0].toObject().as<ModuleObject>());
if (module->hadEvaluationError()) {
JS_ReportErrorASCII(cx, "Module environment unavailable");
return false;
}
RootedModuleEnvironmentObject env(cx, GetModuleEnvironment(cx, module));
RootedString name(cx, args[1].toString());
RootedId id(cx);
if (!JS_StringToId(cx, name, &id)) {
return false;
}
if (!GetProperty(cx, env, env, id, args.rval())) {
return false;
}
if (args.rval().isMagic(JS_UNINITIALIZED_LEXICAL)) {
ReportRuntimeLexicalError(cx, JSMSG_UNINITIALIZED_LEXICAL, id);
return false;
}
return true;
}
enum class DumpType {
ParseNode,
Stencil,
@ -9473,6 +9567,14 @@ static const JSFunctionSpecWithHelp shell_functions[] = {
" Register a module with the module loader, so that subsequent import from\n"
" |specifier| will resolve to |module|. Returns |module|."),
JS_FN_HELP("getModuleEnvironmentNames", GetModuleEnvironmentNames, 1, 0,
"getModuleEnvironmentNames(module)",
" Get the list of a module environment's bound names for a specified module.\n"),
JS_FN_HELP("getModuleEnvironmentValue", GetModuleEnvironmentValue, 2, 0,
"getModuleEnvironmentValue(module, name)",
" Get the value of a bound name in a module environment.\n"),
JS_FN_HELP("dumpStencil", DumpStencil, 1, 0,
"dumpStencil(code, [options])",
" Parses a string and returns string that represents stencil.\n"