ModuleScript.cpp 6.7 KB

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