ModuleScript.cpp 6.6 KB

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