SyntheticModule.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2022, David Tuin <davidot@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/Completion.h>
  8. #include <LibJS/Runtime/GlobalEnvironment.h>
  9. #include <LibJS/Runtime/ModuleEnvironment.h>
  10. #include <LibJS/Runtime/PromiseCapability.h>
  11. #include <LibJS/Runtime/PromiseConstructor.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibJS/SyntheticModule.h>
  14. namespace JS {
  15. JS_DEFINE_ALLOCATOR(SyntheticModule);
  16. // 1.2.1 CreateSyntheticModule ( exportNames, evaluationSteps, realm, hostDefined ), https://tc39.es/proposal-json-modules/#sec-createsyntheticmodule
  17. SyntheticModule::SyntheticModule(Vector<DeprecatedFlyString> export_names, SyntheticModule::EvaluationFunction evaluation_steps, Realm& realm, StringView filename)
  18. : Module(realm, filename)
  19. , m_export_names(move(export_names))
  20. , m_evaluation_steps(move(evaluation_steps))
  21. {
  22. // 1. Return Synthetic Module Record { [[Realm]]: realm, [[Environment]]: undefined, [[Namespace]]: undefined, [[HostDefined]]: hostDefined, [[ExportNames]]: exportNames, [[EvaluationSteps]]: evaluationSteps }.
  23. }
  24. // 1.2.3.1 GetExportedNames( exportStarSet ), https://tc39.es/proposal-json-modules/#sec-smr-getexportednames
  25. ThrowCompletionOr<Vector<DeprecatedFlyString>> SyntheticModule::get_exported_names(VM&, Vector<Module*>)
  26. {
  27. // 1. Return module.[[ExportNames]].
  28. return m_export_names;
  29. }
  30. // 1.2.3.2 ResolveExport( exportName, resolveSet ), https://tc39.es/proposal-json-modules/#sec-smr-resolveexport
  31. ThrowCompletionOr<ResolvedBinding> SyntheticModule::resolve_export(VM&, DeprecatedFlyString const& export_name, Vector<ResolvedBinding>)
  32. {
  33. // 1. If module.[[ExportNames]] does not contain exportName, return null.
  34. if (!m_export_names.contains_slow(export_name))
  35. return ResolvedBinding::null();
  36. // 2. Return ResolvedBinding Record { [[Module]]: module, [[BindingName]]: exportName }.
  37. return ResolvedBinding { ResolvedBinding::BindingName, this, export_name };
  38. }
  39. // 1.2.3.3 Link ( ), https://tc39.es/proposal-json-modules/#sec-smr-instantiate
  40. ThrowCompletionOr<void> SyntheticModule::link(VM& vm)
  41. {
  42. // Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
  43. // Which includes renaming it from Instantiate ( ) to Link ( ).
  44. // 1. Let realm be module.[[Realm]].
  45. // 2. Assert: realm is not undefined.
  46. // Note: This must be true because we use a reference.
  47. // 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]).
  48. auto environment = vm.heap().allocate<ModuleEnvironment>(&realm().global_environment());
  49. // 4. Set module.[[Environment]] to env.
  50. set_environment(environment);
  51. // 5. For each exportName in module.[[ExportNames]],
  52. for (auto& export_name : m_export_names) {
  53. // a. Perform ! envRec.CreateMutableBinding(exportName, false).
  54. MUST(environment->create_mutable_binding(vm, export_name, false));
  55. // b. Perform ! envRec.InitializeBinding(exportName, undefined, normal).
  56. MUST(environment->initialize_binding(vm, export_name, js_undefined(), Environment::InitializeBindingHint::Normal));
  57. }
  58. // 6. Return unused.
  59. return {};
  60. }
  61. // 1.2.3.4 Evaluate ( ), https://tc39.es/proposal-json-modules/#sec-smr-Evaluate
  62. ThrowCompletionOr<Promise*> SyntheticModule::evaluate(VM& vm)
  63. {
  64. // Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
  65. // 1. Suspend the currently running execution context.
  66. // FIXME: We don't have suspend yet.
  67. // 2. Let moduleContext be a new ECMAScript code execution context.
  68. auto module_context = ExecutionContext::create();
  69. // 3. Set the Function of moduleContext to null.
  70. // Note: This is the default value.
  71. // 4. Set the Realm of moduleContext to module.[[Realm]].
  72. module_context->realm = &realm();
  73. // 5. Set the ScriptOrModule of moduleContext to module.
  74. module_context->script_or_module = NonnullGCPtr<Module>(*this);
  75. // 6. Set the VariableEnvironment of moduleContext to module.[[Environment]].
  76. module_context->variable_environment = environment();
  77. // 7. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
  78. module_context->lexical_environment = environment();
  79. // 8. Push moduleContext on to the execution context stack; moduleContext is now the running execution context.
  80. TRY(vm.push_execution_context(*module_context, {}));
  81. // 9. Let result be the result of performing module.[[EvaluationSteps]](module).
  82. auto result = m_evaluation_steps(*this);
  83. // 10. Suspend moduleContext and remove it from the execution context stack.
  84. vm.pop_execution_context();
  85. // 11. Resume the context that is now on the top of the execution context stack as the running execution context.
  86. // 12. Return Completion(result).
  87. // Note: Because we expect it to return a promise we convert this here.
  88. auto promise = Promise::create(realm());
  89. if (result.is_error()) {
  90. VERIFY(result.throw_completion().value().has_value());
  91. promise->reject(*result.throw_completion().value());
  92. } else {
  93. // Note: This value probably isn't visible to JS code? But undefined is fine anyway.
  94. promise->fulfill(js_undefined());
  95. }
  96. return promise.ptr();
  97. }
  98. // 1.2.2 SetSyntheticModuleExport ( module, exportName, exportValue ), https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport
  99. ThrowCompletionOr<void> SyntheticModule::set_synthetic_module_export(DeprecatedFlyString const& export_name, Value export_value)
  100. {
  101. auto& vm = this->realm().vm();
  102. // Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
  103. // 1. Return ? module.[[Environment]].SetMutableBinding(name, value, true).
  104. return environment()->set_mutable_binding(vm, export_name, export_value, true);
  105. }
  106. // 1.3 CreateDefaultExportSyntheticModule ( defaultExport ), https://tc39.es/proposal-json-modules/#sec-create-default-export-synthetic-module
  107. NonnullGCPtr<SyntheticModule> SyntheticModule::create_default_export_synthetic_module(Value default_export, Realm& realm, StringView filename)
  108. {
  109. // Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
  110. // 1. Let closure be the a Abstract Closure with parameters (module) that captures defaultExport and performs the following steps when called:
  111. auto closure = [default_export = make_handle(default_export)](SyntheticModule& module) -> ThrowCompletionOr<void> {
  112. // a. Return ? module.SetSyntheticExport("default", defaultExport).
  113. return module.set_synthetic_module_export("default", default_export.value());
  114. };
  115. // 2. Return CreateSyntheticModule("default", closure, realm)
  116. return realm.heap().allocate<SyntheticModule>(Vector<DeprecatedFlyString> { "default" }, move(closure), realm, filename);
  117. }
  118. // 1.4 ParseJSONModule ( source ), https://tc39.es/proposal-json-modules/#sec-parse-json-module
  119. ThrowCompletionOr<NonnullGCPtr<Module>> parse_json_module(StringView source_text, Realm& realm, StringView filename)
  120. {
  121. auto& vm = realm.vm();
  122. // 1. Let jsonParse be realm's intrinsic object named "%JSON.parse%".
  123. auto json_parse = realm.intrinsics().json_parse_function();
  124. // 2. Let json be ? Call(jsonParse, undefined, « sourceText »).
  125. auto json = TRY(call(vm, *json_parse, js_undefined(), PrimitiveString::create(realm.vm(), source_text)));
  126. // 3. Return CreateDefaultExportSyntheticModule(json, realm, hostDefined).
  127. return SyntheticModule::create_default_export_synthetic_module(json, realm, filename);
  128. }
  129. // 1.2.3.1 LoadRequestedModules ( ), https://tc39.es/proposal-json-modules/#sec-smr-LoadRequestedModules
  130. PromiseCapability& SyntheticModule::load_requested_modules(GCPtr<GraphLoadingState::HostDefined>)
  131. {
  132. // 1. Return ! PromiseResolve(%Promise%, undefined).
  133. auto& constructor = *vm().current_realm()->intrinsics().promise_constructor();
  134. auto promise_capability = MUST(new_promise_capability(vm(), &constructor));
  135. MUST(call(vm(), *promise_capability->resolve(), js_undefined(), js_undefined()));
  136. return promise_capability;
  137. }
  138. }