SyntheticModule.cpp 7.3 KB

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