LibJS: Add support for JSON modules

We now have one supported assertion: 'type' if that is 'json' we attempt
to parse the module as JSON.
This commit is contained in:
davidot 2022-01-27 14:51:21 +01:00 committed by Linus Groh
parent 202de6ed25
commit 6b5c882af3
8 changed files with 277 additions and 24 deletions

View file

@ -227,6 +227,7 @@ set(SOURCES
Script.cpp
SourceTextModule.cpp
SyntaxHighlighter.cpp
SyntheticModule.cpp
Token.cpp
)

View file

@ -24,11 +24,7 @@ Module::~Module()
// 16.2.1.5.1.1 InnerModuleLinking ( module, stack, index ), https://tc39.es/ecma262/#sec-InnerModuleLinking
ThrowCompletionOr<u32> Module::inner_module_linking(VM& vm, Vector<Module*>&, u32 index)
{
// Note: Until we have something extending module which is not SourceTextModule we crash.
VERIFY_NOT_REACHED();
// 1. If module is not a Cyclic Module Record, then
// a. Perform ? module.Link().
TRY(link(vm));
// b. Return index.
@ -38,9 +34,6 @@ ThrowCompletionOr<u32> Module::inner_module_linking(VM& vm, Vector<Module*>&, u3
// 16.2.1.5.2.1 InnerModuleEvaluation ( module, stack, index ), https://tc39.es/ecma262/#sec-innermoduleevaluation
ThrowCompletionOr<u32> Module::inner_module_evaluation(VM& vm, Vector<Module*>&, u32 index)
{
// Note: Until we have something extending module which is not SourceTextModule we crash.
VERIFY_NOT_REACHED();
// 1. If module is not a Cyclic Module Record, then
// a. Let promise be ! module.Evaluate().
auto* promise = TRY(evaluate(vm));

View file

@ -29,6 +29,7 @@
#include <LibJS/Runtime/TemporaryClearException.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/SourceTextModule.h>
#include <LibJS/SyntheticModule.h>
namespace JS {
@ -89,7 +90,7 @@ VM::VM(OwnPtr<CustomData> custom_data)
};
host_get_supported_import_assertions = [&] {
return Vector<String> {};
return Vector<String> { "type" };
};
#define __JS_ENUMERATE(SymbolName, snake_name) \
@ -728,12 +729,18 @@ ScriptOrModule VM::get_active_script_or_module() const
return m_execution_context_stack[0]->script_or_module;
}
VM::StoredModule* VM::get_stored_module(ScriptOrModule const&, String const& filepath)
VM::StoredModule* VM::get_stored_module(ScriptOrModule const&, String const& filepath, String const&)
{
// Note the spec says:
// Each time this operation is called with a specific referencingScriptOrModule, specifier pair as arguments
// it must return the same Module Record instance if it completes normally.
// Currently, we ignore the referencing script or module but this might not be correct in all cases.
// Editor's Note from https://tc39.es/proposal-json-modules/#sec-hostresolveimportedmodule
// The above text implies that is recommended but not required that hosts do not use moduleRequest.[[Assertions]]
// as part of the module cache key. In either case, an exception thrown from an import with a given assertion list
// does not rule out success of another import with the same specifier but a different assertion list.
auto end_or_module = m_loaded_modules.find_if([&](StoredModule const& stored_module) {
return stored_module.filepath == filepath;
});
@ -747,7 +754,7 @@ ThrowCompletionOr<void> VM::link_and_eval_module(Badge<Interpreter>, SourceTextM
return link_and_eval_module(module);
}
ThrowCompletionOr<void> VM::link_and_eval_module(SourceTextModule& module)
ThrowCompletionOr<void> VM::link_and_eval_module(Module& module)
{
auto filepath = module.filename();
@ -766,6 +773,7 @@ ThrowCompletionOr<void> VM::link_and_eval_module(SourceTextModule& module)
m_loaded_modules.empend(
&module,
module.filename(),
String {}, // Null type
module,
true);
stored_module = &m_loaded_modules.last();
@ -852,7 +860,13 @@ ThrowCompletionOr<NonnullRefPtr<Module>> VM::resolve_imported_module(ScriptOrMod
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] resolved {} + {} -> {}", base_path, module_request.module_specifier, filepath);
#endif
auto* loaded_module_or_end = get_stored_module(referencing_script_or_module, filepath);
// We only allow "type" as a supported assertion so it is the only valid key that should ever arrive here.
VERIFY(module_request.assertions.is_empty() || (module_request.assertions.size() == 1 && module_request.assertions.first().key == "type"));
auto module_type = module_request.assertions.is_empty() ? String {} : module_request.assertions.first().value;
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] module at {} has type {} [is_null={}]", module_request.module_specifier, module_type, module_type.is_null());
auto* loaded_module_or_end = get_stored_module(referencing_script_or_module, filepath, module_type);
if (loaded_module_or_end != nullptr) {
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] resolve_imported_module({}) already loaded at {}", filepath, loaded_module_or_end->module.ptr());
return loaded_module_or_end->module;
@ -872,21 +886,32 @@ ThrowCompletionOr<NonnullRefPtr<Module>> VM::resolve_imported_module(ScriptOrMod
auto file_content = file_or_error.value()->read_all();
StringView content_view { file_content.data(), file_content.size() };
// Note: We treat all files as module, so if a script does not have exports it just runs it.
auto module_or_errors = SourceTextModule::parse(content_view, *current_realm(), filepath);
auto module = TRY([&]() -> ThrowCompletionOr<NonnullRefPtr<Module>> {
// If assertions has an entry entry such that entry.[[Key]] is "type", let type be entry.[[Value]]. The following requirements apply:
// If type is "json", then this algorithm must either invoke ParseJSONModule and return the resulting Completion Record, or throw an exception.
if (module_type == "json"sv) {
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] reading and parsing JSON module {}", filepath);
return parse_json_module(content_view, *current_realm(), filepath);
}
if (module_or_errors.is_error()) {
VERIFY(module_or_errors.error().size() > 0);
return throw_completion<SyntaxError>(global_object, module_or_errors.error().first().to_string());
}
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] reading and parsing as SourceTextModule module {}", filepath);
// Note: We treat all files as module, so if a script does not have exports it just runs it.
auto module_or_errors = SourceTextModule::parse(content_view, *current_realm(), filepath);
if (module_or_errors.is_error()) {
VERIFY(module_or_errors.error().size() > 0);
return throw_completion<SyntaxError>(global_object, module_or_errors.error().first().to_string());
}
return module_or_errors.release_value();
}());
auto module = module_or_errors.release_value();
dbgln_if(JS_MODULE_DEBUG, "[JS MODULE] resolve_imported_module(...) parsed {} to {}", filepath, module.ptr());
// We have to set it here already in case it references itself.
m_loaded_modules.empend(
referencing_script_or_module,
filepath,
module_type,
module,
false);
@ -939,11 +964,8 @@ void VM::import_module_dynamically(ScriptOrModule referencing_script_or_module,
clear_exception();
promise->reject(*module_or_error.throw_completion().value());
} else {
// Note: If you are here because this VERIFY is failing overwrite host_import_module_dynamically
// because this is LibJS internal logic which won't always work
auto module = module_or_error.release_value();
VERIFY(is<SourceTextModule>(*module));
auto& source_text_module = static_cast<SourceTextModule&>(*module);
auto& source_text_module = static_cast<Module&>(*module);
auto evaluated_or_error = link_and_eval_module(source_text_module);

View file

@ -250,7 +250,7 @@ private:
ThrowCompletionOr<void> iterator_binding_initialization(BindingPattern const& binding, Iterator& iterator_record, Environment* environment, GlobalObject& global_object);
ThrowCompletionOr<NonnullRefPtr<Module>> resolve_imported_module(ScriptOrModule referencing_script_or_module, ModuleRequest const& module_request);
ThrowCompletionOr<void> link_and_eval_module(SourceTextModule& module);
ThrowCompletionOr<void> link_and_eval_module(Module& module);
void import_module_dynamically(ScriptOrModule referencing_script_or_module, ModuleRequest module_request, PromiseCapability promise_capability);
void finish_dynamic_import(ScriptOrModule referencing_script_or_module, ModuleRequest module_request, PromiseCapability promise_capability, Promise* inner_promise);
@ -280,11 +280,12 @@ private:
struct StoredModule {
ScriptOrModule referencing_script_or_module;
String filepath;
String type;
NonnullRefPtr<Module> module;
bool has_once_started_linking { false };
};
StoredModule* get_stored_module(ScriptOrModule const& script_or_module, String const& filepath);
StoredModule* get_stored_module(ScriptOrModule const& script_or_module, String const& filepath, String const& type);
Vector<StoredModule> m_loaded_modules;

View file

@ -0,0 +1,159 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/JSONObject.h>
#include <LibJS/Runtime/ModuleEnvironment.h>
#include <LibJS/Runtime/VM.h>
#include <LibJS/SyntheticModule.h>
namespace JS {
// 1.2.1 CreateSyntheticModule ( exportNames, evaluationSteps, realm, hostDefined ), https://tc39.es/proposal-json-modules/#sec-synthetic-module-records
SyntheticModule::SyntheticModule(Vector<FlyString> export_names, SyntheticModule::EvaluationFunction evaluation_steps, Realm& realm, StringView filename)
: Module(realm, filename)
, m_export_names(move(export_names))
, m_evaluation_steps(move(evaluation_steps))
{
// 1. Return Synthetic Module Record { [[Realm]]: realm, [[Environment]]: undefined, [[Namespace]]: undefined, [[HostDefined]]: hostDefined, [[ExportNames]]: exportNames, [[EvaluationSteps]]: evaluationSteps }.
}
// 1.2.3.1 GetExportedNames( exportStarSet ), https://tc39.es/proposal-json-modules/#sec-smr-getexportednames
ThrowCompletionOr<Vector<FlyString>> JS::SyntheticModule::get_exported_names(VM&, Vector<Module*>)
{
// 1. Return module.[[ExportNames]].
return m_export_names;
}
// 1.2.3.2 ResolveExport( exportName, resolveSet ), https://tc39.es/proposal-json-modules/#sec-smr-resolveexport
ThrowCompletionOr<ResolvedBinding> JS::SyntheticModule::resolve_export(VM&, FlyString const& export_name, Vector<ResolvedBinding>)
{
// 1. If module.[[ExportNames]] does not contain exportName, return null.
if (!m_export_names.contains_slow(export_name))
return ResolvedBinding::null();
// 2. Return ResolvedBinding Record { [[Module]]: module, [[BindingName]]: exportName }.
return ResolvedBinding { ResolvedBinding::BindingName, this, export_name };
}
// 1.2.3.3 Link ( ), https://tc39.es/proposal-json-modules/#sec-smr-instantiate
ThrowCompletionOr<void> JS::SyntheticModule::link(VM& vm)
{
// Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
// Which includes renaming it from Instantiate ( ) to Link ( ).
// 1. Let realm be module.[[Realm]].
// 2. Assert: realm is not undefined.
// Note: This must be true because we use a reference.
auto& global_object = realm().global_object();
// 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]).
auto* environment = vm.heap().allocate<ModuleEnvironment>(global_object, &realm().global_environment());
// 4. Set module.[[Environment]] to env.
set_environment(environment);
// 5. For each exportName in module.[[ExportNames]],
for (auto& export_name : m_export_names) {
// a. Perform ! envRec.CreateMutableBinding(exportName, false).
environment->create_mutable_binding(global_object, export_name, false);
// b. Perform ! envRec.InitializeBinding(exportName, undefined).
environment->initialize_binding(global_object, export_name, js_undefined());
}
// 6. Return undefined.
// Note: This return value is never visible to the outside so we use void.
return {};
}
// 1.2.3.4 Evaluate ( ), https://tc39.es/proposal-json-modules/#sec-smr-Evaluate
ThrowCompletionOr<Promise*> JS::SyntheticModule::evaluate(VM& vm)
{
// Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
// 1. Suspend the currently running execution context.
// FIXME: We don't have suspend yet.
// 2. Let moduleContext be a new ECMAScript code execution context.
ExecutionContext module_context { vm.heap() };
// 3. Set the Function of moduleContext to null.
// Note: This is the default value.
// 4. Set the Realm of moduleContext to module.[[Realm]].
module_context.realm = &realm();
// 5. Set the ScriptOrModule of moduleContext to module.
module_context.script_or_module = this;
// 6. Set the VariableEnvironment of moduleContext to module.[[Environment]].
module_context.variable_environment = environment();
// 7. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
module_context.lexical_environment = environment();
// 8. Push moduleContext on to the execution context stack; moduleContext is now the running execution context.
vm.push_execution_context(module_context, realm().global_object());
// 9. Let result be the result of performing module.[[EvaluationSteps]](module).
auto result = m_evaluation_steps(*this);
// 10. Suspend moduleContext and remove it from the execution context stack.
vm.pop_execution_context();
// 11. Resume the context that is now on the top of the execution context stack as the running execution context.
// 12. Return Completion(result).
// Note: Because we expect it to return a promise we convert this here.
auto* promise = Promise::create(realm().global_object());
if (result.is_error()) {
VERIFY(result.throw_completion().value().has_value());
promise->reject(*result.throw_completion().value());
} else {
// Note: This value probably isn't visible to JS code? But undefined is fine anyway.
promise->fulfill(js_undefined());
}
return promise;
}
// 1.2.2 SetSyntheticModuleExport ( module, exportName, exportValue ), https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport
ThrowCompletionOr<void> SyntheticModule::set_synthetic_module_export(FlyString const& export_name, Value export_value)
{
// Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
// 1. Return ? module.[[Environment]].SetMutableBinding(name, value, true).
return environment()->set_mutable_binding(realm().global_object(), export_name, export_value, true);
}
// 1.3 CreateDefaultExportSyntheticModule ( defaultExport ), https://tc39.es/proposal-json-modules/#sec-create-default-export-synthetic-module
NonnullRefPtr<SyntheticModule> SyntheticModule::create_default_export_synthetic_module(Value default_export, Realm& realm, StringView filename)
{
// Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
// 1. Let closure be the a Abstract Closure with parameters (module) that captures defaultExport and performs the following steps when called:
auto closure = [default_export = make_handle(default_export)](SyntheticModule& module) -> ThrowCompletionOr<void> {
// a. Return ? module.SetSyntheticExport("default", defaultExport).
return module.set_synthetic_module_export("default", default_export.value());
};
// 2. Return CreateSyntheticModule("default", closure, realm)
return adopt_ref(*new SyntheticModule({ "default" }, move(closure), realm, filename));
}
// 1.4 ParseJSONModule ( source ), https://tc39.es/proposal-json-modules/#sec-parse-json-module
ThrowCompletionOr<NonnullRefPtr<Module>> parse_json_module(StringView source_text, Realm& realm, StringView filename)
{
// 1. Let jsonParse be realm's intrinsic object named "%JSON.parse%".
auto* json_parse = realm.global_object().json_parse_function();
// 2. Let json be ? Call(jsonParse, undefined, « sourceText »).
auto json = TRY(call(realm.global_object(), *json_parse, js_undefined(), js_string(realm.vm(), source_text)));
// 3. Return CreateDefaultExportSyntheticModule(json, realm, hostDefined).
return SyntheticModule::create_default_export_synthetic_module(json, realm, filename);
}
}

View file

@ -0,0 +1,36 @@
/*
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibJS/Module.h>
namespace JS {
// 1.2 Synthetic Module Records, https://tc39.es/proposal-json-modules/#sec-synthetic-module-records
class SyntheticModule final : public Module {
public:
using EvaluationFunction = Function<ThrowCompletionOr<void>(SyntheticModule&)>;
SyntheticModule(Vector<FlyString> export_names, EvaluationFunction evaluation_steps, Realm& realm, StringView filename);
static NonnullRefPtr<SyntheticModule> create_default_export_synthetic_module(Value default_export, Realm& realm, StringView filename);
ThrowCompletionOr<void> set_synthetic_module_export(FlyString const& export_name, Value export_value);
virtual ThrowCompletionOr<void> link(VM& vm) override;
virtual ThrowCompletionOr<Promise*> evaluate(VM& vm) override;
virtual ThrowCompletionOr<Vector<FlyString>> get_exported_names(VM& vm, Vector<Module*> export_star_set) override;
virtual ThrowCompletionOr<ResolvedBinding> resolve_export(VM& vm, FlyString const& export_name, Vector<ResolvedBinding> resolve_set) override;
private:
Vector<FlyString> m_export_names; // [[ExportNames]]
EvaluationFunction m_evaluation_steps; // [[EvaluationSteps]]
};
ThrowCompletionOr<NonnullRefPtr<Module>> parse_json_module(StringView source_text, Realm& realm, StringView filename);
}

View file

@ -0,0 +1,7 @@
{
"value": "value",
"array": [1, 2, 3],
"map": {
"innerValue": "innerValue"
}
}

View file

@ -0,0 +1,34 @@
describe("basic behavior", () => {
test("can import json modules", () => {
let passed = false;
let error = null;
let result = null;
import("./json-module.json", { assert: { type: "json" } })
.then(jsonObj => {
passed = true;
result = jsonObj;
})
.catch(err => {
error = err;
});
runQueuedPromiseJobs();
if (error) throw error;
console.log(JSON.stringify(result));
expect(passed).toBeTrue();
expect(result).not.toBeNull();
expect(result).not.toBeUndefined();
const jsonResult = result.default;
expect(jsonResult).not.toBeNull();
expect(jsonResult).not.toBeUndefined();
expect(jsonResult).toHaveProperty("value", "value");
expect(jsonResult).toHaveProperty("array", [1, 2, 3]);
expect(jsonResult).toHaveProperty("map", { innerValue: "innerValue" });
});
});