ModuleScript.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2022, networkException <networkexception@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Interpreter.h>
  7. #include <LibWeb/HTML/Scripting/Environments.h>
  8. #include <LibWeb/HTML/Scripting/Fetching.h>
  9. #include <LibWeb/HTML/Scripting/ModuleScript.h>
  10. #include <LibWeb/WebIDL/DOMException.h>
  11. namespace Web::HTML {
  12. ModuleScript::~ModuleScript() = default;
  13. ModuleScript::ModuleScript(AK::URL base_url, String filename, EnvironmentSettingsObject& environment_settings_object)
  14. : Script(move(base_url), move(filename), environment_settings_object)
  15. {
  16. }
  17. JavaScriptModuleScript::~JavaScriptModuleScript() = default;
  18. JavaScriptModuleScript::JavaScriptModuleScript(AK::URL base_url, String filename, EnvironmentSettingsObject& environment_settings_object)
  19. : ModuleScript(move(base_url), move(filename), environment_settings_object)
  20. {
  21. }
  22. // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-javascript-module-script
  23. JS::GCPtr<JavaScriptModuleScript> JavaScriptModuleScript::create(String const& filename, StringView source, EnvironmentSettingsObject& settings_object, AK::URL base_url)
  24. {
  25. // 1. If scripting is disabled for settings, then set source to the empty string.
  26. if (settings_object.is_scripting_disabled())
  27. source = ""sv;
  28. auto& realm = settings_object.realm();
  29. // 2. Let script be a new module script that this algorithm will subsequently initialize.
  30. auto* script = realm.heap().allocate<JavaScriptModuleScript>(realm, move(base_url), filename, settings_object);
  31. // 3. Set script's settings object to settings. (NOTE: This was already done when constructing.)
  32. // 4. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
  33. // FIXME: 5. Set script's fetch options to options.
  34. // 6. Set script's parse error and error to rethrow to null.
  35. // NOTE: Parse error and error to rethrow were set to null in the construction of Script.
  36. // 7. Let result be ParseModule(source, settings's Realm, script).
  37. auto result = JS::SourceTextModule::parse(source, settings_object.realm(), filename.view(), script);
  38. // 8. If result is a list of errors, then:
  39. if (result.is_error()) {
  40. auto& parse_error = result.error().first();
  41. dbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_string());
  42. // FIXME: 1. Set script's parse error to result[0].
  43. // 2. Return script.
  44. return script;
  45. }
  46. // 10. For each ModuleRequest record requested of result.[[RequestedModules]]:
  47. for (auto const& requested : result.value()->requested_modules()) {
  48. // FIXME: Clarify if this should be checked for all requested before running the steps below.
  49. // 9. Assert: requested.[[Assertions]] does not contain any Record entry such that entry.[[Key]] is not "type",
  50. // because we only asked for "type" assertions in HostGetSupportedImportAssertions.
  51. VERIFY(all_of(requested.assertions, [](auto const& assertion) { return assertion.key == "type"sv; }));
  52. // 1. Let url be the result of resolving a module specifier given script's base URL and requested.[[Specifier]].
  53. auto url = resolve_module_specifier(requested, script->base_url());
  54. // 2. Let moduleType be the result of running the module type from module request steps given requested.
  55. auto module_type = module_type_from_module_request(requested);
  56. // 3. If url is failure, or if the result of running the module type allowed steps given moduleType and settings is false, then:
  57. if (!url.is_valid() || !settings_object.module_type_allowed(module_type)) {
  58. // FIXME: 1. Let error be a new TypeError exception.
  59. // FIXME: 2. Set script's parse error to error.
  60. // FIXME: 3. Return script.
  61. TODO();
  62. }
  63. }
  64. // 11. Set script's record to result.
  65. script->m_record = result.value();
  66. // 12. Return script.
  67. return script;
  68. }
  69. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script
  70. JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting)
  71. {
  72. // 1. Let settings be the settings object of script.
  73. auto& settings = settings_object();
  74. // 2. Check if we can run script with settings. If this returns "do not run", then return a promise resolved with undefined.
  75. if (settings.can_run_script() == RunScriptDecision::DoNotRun) {
  76. auto* promise = JS::Promise::create(settings.realm());
  77. promise->fulfill(JS::js_undefined());
  78. return promise;
  79. }
  80. // 3. Prepare to run script given settings.
  81. settings.prepare_to_run_script();
  82. // 4. Let evaluationPromise be null.
  83. JS::Promise* evaluation_promise = nullptr;
  84. // FIXME: 5. If script's error to rethrow is not null, then set evaluationPromise to a promise rejected with script's error to rethrow.
  85. // 6. Otherwise:
  86. if (m_record) {
  87. // 1. Let record be script's record.
  88. auto record = m_record;
  89. auto interpreter = JS::Interpreter::create_with_existing_realm(settings.realm());
  90. JS::VM::InterpreterExecutionScope scope(*interpreter);
  91. // 2. Set evaluationPromise to record.Evaluate().
  92. auto elevation_promise_or_error = record->evaluate(vm());
  93. // NOTE: This step will recursively evaluate all of the module's dependencies.
  94. // If Evaluate fails to complete as a result of the user agent aborting the running script,
  95. // then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException.
  96. if (elevation_promise_or_error.is_error()) {
  97. auto* promise = JS::Promise::create(settings_object().realm());
  98. promise->reject(WebIDL::QuotaExceededError::create(settings_object().realm(), "Failed to evaluate module script").ptr());
  99. evaluation_promise = promise;
  100. } else {
  101. evaluation_promise = elevation_promise_or_error.value();
  102. }
  103. } else {
  104. TODO();
  105. }
  106. // FIXME: 7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.
  107. // 8. Clean up after running script with settings.
  108. settings.clean_up_after_running_script();
  109. // 9. Return evaluationPromise.
  110. return evaluation_promise;
  111. }
  112. void JavaScriptModuleScript::visit_edges(Cell::Visitor& visitor)
  113. {
  114. Base::visit_edges(visitor);
  115. visitor.visit(m_record);
  116. }
  117. }