2022-01-27 13:51:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, David Tuin <davidot@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2022-02-07 17:51:58 +00:00
|
|
|
#include <LibJS/Runtime/GlobalEnvironment.h>
|
2022-01-27 13:51:21 +00:00
|
|
|
#include <LibJS/Runtime/ModuleEnvironment.h>
|
2023-12-03 10:25:12 +00:00
|
|
|
#include <LibJS/Runtime/PromiseCapability.h>
|
|
|
|
#include <LibJS/Runtime/PromiseConstructor.h>
|
2022-01-27 13:51:21 +00:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
|
|
|
#include <LibJS/SyntheticModule.h>
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2023-11-19 08:45:05 +00:00
|
|
|
JS_DEFINE_ALLOCATOR(SyntheticModule);
|
|
|
|
|
2022-04-11 18:44:33 +00:00
|
|
|
// 1.2.1 CreateSyntheticModule ( exportNames, evaluationSteps, realm, hostDefined ), https://tc39.es/proposal-json-modules/#sec-createsyntheticmodule
|
2023-01-09 00:23:00 +00:00
|
|
|
SyntheticModule::SyntheticModule(Vector<DeprecatedFlyString> export_names, SyntheticModule::EvaluationFunction evaluation_steps, Realm& realm, StringView filename)
|
2022-01-27 13:51:21 +00:00
|
|
|
: 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
|
2023-01-09 00:23:00 +00:00
|
|
|
ThrowCompletionOr<Vector<DeprecatedFlyString>> SyntheticModule::get_exported_names(VM&, Vector<Module*>)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
|
|
|
// 1. Return module.[[ExportNames]].
|
|
|
|
return m_export_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1.2.3.2 ResolveExport( exportName, resolveSet ), https://tc39.es/proposal-json-modules/#sec-smr-resolveexport
|
2023-01-09 00:23:00 +00:00
|
|
|
ThrowCompletionOr<ResolvedBinding> SyntheticModule::resolve_export(VM&, DeprecatedFlyString const& export_name, Vector<ResolvedBinding>)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
|
|
|
// 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
|
2022-04-03 14:19:33 +00:00
|
|
|
ThrowCompletionOr<void> SyntheticModule::link(VM& vm)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]).
|
2022-12-14 17:40:33 +00:00
|
|
|
auto environment = vm.heap().allocate_without_realm<ModuleEnvironment>(&realm().global_environment());
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 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).
|
2022-08-21 14:12:43 +00:00
|
|
|
MUST(environment->create_mutable_binding(vm, export_name, false));
|
2022-01-27 13:51:21 +00:00
|
|
|
|
2022-12-14 12:26:10 +00:00
|
|
|
// b. Perform ! envRec.InitializeBinding(exportName, undefined, normal).
|
|
|
|
MUST(environment->initialize_binding(vm, export_name, js_undefined(), Environment::InitializeBindingHint::Normal));
|
2022-01-27 13:51:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-02 18:54:39 +00:00
|
|
|
// 6. Return unused.
|
2022-01-27 13:51:21 +00:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1.2.3.4 Evaluate ( ), https://tc39.es/proposal-json-modules/#sec-smr-Evaluate
|
2022-04-03 14:19:33 +00:00
|
|
|
ThrowCompletionOr<Promise*> SyntheticModule::evaluate(VM& vm)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
|
|
|
// 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.
|
2024-05-31 13:12:33 +00:00
|
|
|
auto module_context = ExecutionContext::create();
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 3. Set the Function of moduleContext to null.
|
|
|
|
// Note: This is the default value.
|
|
|
|
|
|
|
|
// 4. Set the Realm of moduleContext to module.[[Realm]].
|
2023-11-27 15:45:45 +00:00
|
|
|
module_context->realm = &realm();
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 5. Set the ScriptOrModule of moduleContext to module.
|
2023-11-27 15:45:45 +00:00
|
|
|
module_context->script_or_module = NonnullGCPtr<Module>(*this);
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 6. Set the VariableEnvironment of moduleContext to module.[[Environment]].
|
2023-11-27 15:45:45 +00:00
|
|
|
module_context->variable_environment = environment();
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 7. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
|
2023-11-27 15:45:45 +00:00
|
|
|
module_context->lexical_environment = environment();
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 8. Push moduleContext on to the execution context stack; moduleContext is now the running execution context.
|
2023-11-27 15:45:45 +00:00
|
|
|
TRY(vm.push_execution_context(*module_context, {}));
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 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.
|
2022-12-13 20:49:50 +00:00
|
|
|
auto promise = Promise::create(realm());
|
2022-01-27 13:51:21 +00:00
|
|
|
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());
|
|
|
|
}
|
2022-12-13 20:49:50 +00:00
|
|
|
return promise.ptr();
|
2022-01-27 13:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1.2.2 SetSyntheticModuleExport ( module, exportName, exportValue ), https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport
|
2023-01-09 00:23:00 +00:00
|
|
|
ThrowCompletionOr<void> SyntheticModule::set_synthetic_module_export(DeprecatedFlyString const& export_name, Value export_value)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
2022-08-21 14:12:43 +00:00
|
|
|
auto& vm = this->realm().vm();
|
|
|
|
|
2022-01-27 13:51:21 +00:00
|
|
|
// Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
|
|
|
|
// 1. Return ? module.[[Environment]].SetMutableBinding(name, value, true).
|
2022-08-21 14:12:43 +00:00
|
|
|
return environment()->set_mutable_binding(vm, export_name, export_value, true);
|
2022-01-27 13:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1.3 CreateDefaultExportSyntheticModule ( defaultExport ), https://tc39.es/proposal-json-modules/#sec-create-default-export-synthetic-module
|
2022-09-05 12:31:25 +00:00
|
|
|
NonnullGCPtr<SyntheticModule> SyntheticModule::create_default_export_synthetic_module(Value default_export, Realm& realm, StringView filename)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
|
|
|
// 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)
|
2023-01-09 00:23:00 +00:00
|
|
|
return realm.heap().allocate_without_realm<SyntheticModule>(Vector<DeprecatedFlyString> { "default" }, move(closure), realm, filename);
|
2022-01-27 13:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 1.4 ParseJSONModule ( source ), https://tc39.es/proposal-json-modules/#sec-parse-json-module
|
2022-09-05 12:31:25 +00:00
|
|
|
ThrowCompletionOr<NonnullGCPtr<Module>> parse_json_module(StringView source_text, Realm& realm, StringView filename)
|
2022-01-27 13:51:21 +00:00
|
|
|
{
|
2022-08-21 18:24:32 +00:00
|
|
|
auto& vm = realm.vm();
|
|
|
|
|
2022-01-27 13:51:21 +00:00
|
|
|
// 1. Let jsonParse be realm's intrinsic object named "%JSON.parse%".
|
2023-04-12 22:47:15 +00:00
|
|
|
auto json_parse = realm.intrinsics().json_parse_function();
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 2. Let json be ? Call(jsonParse, undefined, « sourceText »).
|
2023-08-08 16:25:57 +00:00
|
|
|
auto json = TRY(call(vm, *json_parse, js_undefined(), PrimitiveString::create(realm.vm(), source_text)));
|
2022-01-27 13:51:21 +00:00
|
|
|
|
|
|
|
// 3. Return CreateDefaultExportSyntheticModule(json, realm, hostDefined).
|
|
|
|
return SyntheticModule::create_default_export_synthetic_module(json, realm, filename);
|
|
|
|
}
|
|
|
|
|
2023-12-03 10:25:12 +00:00
|
|
|
// 1.2.3.1 LoadRequestedModules ( ), https://tc39.es/proposal-json-modules/#sec-smr-LoadRequestedModules
|
|
|
|
PromiseCapability& SyntheticModule::load_requested_modules(GCPtr<GraphLoadingState::HostDefined>)
|
|
|
|
{
|
|
|
|
// 1. Return ! PromiseResolve(%Promise%, undefined).
|
|
|
|
auto& constructor = *vm().current_realm()->intrinsics().promise_constructor();
|
|
|
|
auto promise_capability = MUST(new_promise_capability(vm(), &constructor));
|
|
|
|
MUST(call(vm(), *promise_capability->resolve(), js_undefined(), js_undefined()));
|
|
|
|
return promise_capability;
|
|
|
|
}
|
|
|
|
|
2022-01-27 13:51:21 +00:00
|
|
|
}
|