ModuleScript.cpp 6.8 KB

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