ModuleScript.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. WebIDL::ExceptionOr<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 = MUST_OR_THROW_OOM(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. script->set_parse_error(JS::js_null());
  40. script->set_error_to_rethrow(JS::js_null());
  41. // 7. Let result be ParseModule(source, settings's Realm, script).
  42. auto result = JS::SourceTextModule::parse(source, settings_object.realm(), filename.view(), script);
  43. // 8. If result is a list of errors, then:
  44. if (result.is_error()) {
  45. auto& parse_error = result.error().first();
  46. dbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_deprecated_string());
  47. // 1. Set script's parse error to result[0].
  48. script->set_parse_error(JS::SyntaxError::create(settings_object.realm(), parse_error.to_string().release_value_but_fixme_should_propagate_errors()));
  49. // 2. Return script.
  50. return script;
  51. }
  52. // 10. For each ModuleRequest record requested of result.[[RequestedModules]]:
  53. for (auto const& requested : result.value()->requested_modules()) {
  54. // FIXME: Clarify if this should be checked for all requested before running the steps below.
  55. // 9. Assert: requested.[[Assertions]] does not contain any Record entry such that entry.[[Key]] is not "type",
  56. // because we only asked for "type" assertions in HostGetSupportedImportAssertions.
  57. VERIFY(all_of(requested.assertions, [](auto const& assertion) { return assertion.key == "type"sv; }));
  58. // 1. Let url be the result of resolving a module specifier given script and requested.[[Specifier]], catching any exceptions.
  59. auto url = resolve_module_specifier(*script, requested.module_specifier);
  60. // 2. If the previous step threw an exception, then:
  61. if (url.is_exception()) {
  62. // FIXME: 1. Set script's parse error to that exception.
  63. // 2. Return script.
  64. return script;
  65. }
  66. // 3. Let moduleType be the result of running the module type from module request steps given requested.
  67. auto module_type = module_type_from_module_request(requested);
  68. // 4. If the result of running the module type allowed steps given moduleType and settings is false, then:
  69. if (!settings_object.module_type_allowed(module_type)) {
  70. // FIXME: 1. Let error be a new TypeError exception.
  71. // FIXME: 2. Set script's parse error to error.
  72. // 3. Return script.
  73. return script;
  74. }
  75. }
  76. // 11. Set script's record to result.
  77. script->m_record = result.value();
  78. // 12. Return script.
  79. return script;
  80. }
  81. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script
  82. JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting)
  83. {
  84. // 1. Let settings be the settings object of script.
  85. auto& settings = settings_object();
  86. // 2. Check if we can run script with settings. If this returns "do not run", then return a promise resolved with undefined.
  87. if (settings.can_run_script() == RunScriptDecision::DoNotRun) {
  88. auto promise = JS::Promise::create(settings.realm());
  89. promise->fulfill(JS::js_undefined());
  90. return promise;
  91. }
  92. // 3. Prepare to run script given settings.
  93. settings.prepare_to_run_script();
  94. // 4. Let evaluationPromise be null.
  95. JS::Promise* evaluation_promise = nullptr;
  96. // 5. If script's error to rethrow is not null, then set evaluationPromise to a promise rejected with script's error to rethrow.
  97. if (!error_to_rethrow().is_null()) {
  98. evaluation_promise = JS::Promise::create(settings.realm());
  99. evaluation_promise->reject(error_to_rethrow());
  100. }
  101. // 6. Otherwise:
  102. else {
  103. // 1. Let record be script's record.
  104. auto record = m_record;
  105. VERIFY(record);
  106. auto interpreter = JS::Interpreter::create_with_existing_realm(settings.realm());
  107. JS::VM::InterpreterExecutionScope scope(*interpreter);
  108. // 2. Set evaluationPromise to record.Evaluate().
  109. auto elevation_promise_or_error = record->evaluate(vm());
  110. // NOTE: This step will recursively evaluate all of the module's dependencies.
  111. // If Evaluate fails to complete as a result of the user agent aborting the running script,
  112. // then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException.
  113. if (elevation_promise_or_error.is_error()) {
  114. auto promise = JS::Promise::create(settings_object().realm());
  115. promise->reject(WebIDL::QuotaExceededError::create(settings_object().realm(), "Failed to evaluate module script").ptr());
  116. evaluation_promise = promise;
  117. } else {
  118. evaluation_promise = elevation_promise_or_error.value();
  119. }
  120. }
  121. // FIXME: 7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.
  122. // 8. Clean up after running script with settings.
  123. settings.clean_up_after_running_script();
  124. // 9. Return evaluationPromise.
  125. return evaluation_promise;
  126. }
  127. void JavaScriptModuleScript::visit_edges(Cell::Visitor& visitor)
  128. {
  129. Base::visit_edges(visitor);
  130. visitor.visit(m_record);
  131. }
  132. }