SyntheticModule.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. auto& global_object = realm().global_object();
  45. // 3. Let env be NewModuleEnvironment(realm.[[GlobalEnv]]).
  46. auto* environment = vm.heap().allocate_without_global_object<ModuleEnvironment>(&realm().global_environment());
  47. // 4. Set module.[[Environment]] to env.
  48. set_environment(environment);
  49. // 5. For each exportName in module.[[ExportNames]],
  50. for (auto& export_name : m_export_names) {
  51. // a. Perform ! envRec.CreateMutableBinding(exportName, false).
  52. environment->create_mutable_binding(global_object, export_name, false);
  53. // b. Perform ! envRec.InitializeBinding(exportName, undefined).
  54. environment->initialize_binding(global_object, export_name, js_undefined());
  55. }
  56. // 6. Return unused.
  57. return {};
  58. }
  59. // 1.2.3.4 Evaluate ( ), https://tc39.es/proposal-json-modules/#sec-smr-Evaluate
  60. ThrowCompletionOr<Promise*> SyntheticModule::evaluate(VM& vm)
  61. {
  62. // Note: Has some changes from PR: https://github.com/tc39/proposal-json-modules/pull/13.
  63. // 1. Suspend the currently running execution context.
  64. // FIXME: We don't have suspend yet.
  65. // 2. Let moduleContext be a new ECMAScript code execution context.
  66. ExecutionContext module_context { vm.heap() };
  67. // 3. Set the Function of moduleContext to null.
  68. // Note: This is the default value.
  69. // 4. Set the Realm of moduleContext to module.[[Realm]].
  70. module_context.realm = &realm();
  71. // 5. Set the ScriptOrModule of moduleContext to module.
  72. module_context.script_or_module = this->make_weak_ptr();
  73. // 6. Set the VariableEnvironment of moduleContext to module.[[Environment]].
  74. module_context.variable_environment = environment();
  75. // 7. Set the LexicalEnvironment of moduleContext to module.[[Environment]].
  76. module_context.lexical_environment = environment();
  77. // 8. Push moduleContext on to the execution context stack; moduleContext is now the running execution context.
  78. TRY(vm.push_execution_context(module_context, realm().global_object()));
  79. // 9. Let result be the result of performing module.[[EvaluationSteps]](module).
  80. auto result = m_evaluation_steps(*this);
  81. // 10. Suspend moduleContext and remove it from the execution context stack.
  82. vm.pop_execution_context();
  83. // 11. Resume the context that is now on the top of the execution context stack as the running execution context.
  84. // 12. Return Completion(result).
  85. // Note: Because we expect it to return a promise we convert this here.
  86. auto* promise = Promise::create(realm().global_object());
  87. if (result.is_error()) {
  88. VERIFY(result.throw_completion().value().has_value());
  89. promise->reject(*result.throw_completion().value());
  90. } else {
  91. // Note: This value probably isn't visible to JS code? But undefined is fine anyway.
  92. promise->fulfill(js_undefined());
  93. }
  94. return promise;
  95. }
  96. // 1.2.2 SetSyntheticModuleExport ( module, exportName, exportValue ), https://tc39.es/proposal-json-modules/#sec-setsyntheticmoduleexport
  97. ThrowCompletionOr<void> SyntheticModule::set_synthetic_module_export(FlyString const& export_name, Value export_value)
  98. {
  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(realm().global_object(), export_name, export_value, true);
  102. }
  103. // 1.3 CreateDefaultExportSyntheticModule ( defaultExport ), https://tc39.es/proposal-json-modules/#sec-create-default-export-synthetic-module
  104. NonnullRefPtr<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 adopt_ref(*new SyntheticModule({ "default" }, move(closure), realm, filename));
  114. }
  115. // 1.4 ParseJSONModule ( source ), https://tc39.es/proposal-json-modules/#sec-parse-json-module
  116. ThrowCompletionOr<NonnullRefPtr<Module>> parse_json_module(StringView source_text, Realm& realm, StringView filename)
  117. {
  118. // 1. Let jsonParse be realm's intrinsic object named "%JSON.parse%".
  119. auto* json_parse = realm.global_object().json_parse_function();
  120. // 2. Let json be ? Call(jsonParse, undefined, « sourceText »).
  121. auto json = TRY(call(realm.global_object(), *json_parse, js_undefined(), js_string(realm.vm(), source_text)));
  122. // 3. Return CreateDefaultExportSyntheticModule(json, realm, hostDefined).
  123. return SyntheticModule::create_default_export_synthetic_module(json, realm, filename);
  124. }
  125. }