ModuleScript.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. JS_DEFINE_ALLOCATOR(JavaScriptModuleScript);
  14. ModuleScript::~ModuleScript() = default;
  15. ModuleScript::ModuleScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& environment_settings_object)
  16. : Script(move(base_url), move(filename), environment_settings_object)
  17. {
  18. }
  19. JavaScriptModuleScript::~JavaScriptModuleScript() = default;
  20. JavaScriptModuleScript::JavaScriptModuleScript(URL::URL base_url, ByteString 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. // https://whatpr.org/html/9893/webappapis.html#creating-a-javascript-module-script
  26. WebIDL::ExceptionOr<JS::GCPtr<JavaScriptModuleScript>> JavaScriptModuleScript::create(ByteString const& filename, StringView source, EnvironmentSettingsObject& settings_object, URL::URL base_url)
  27. {
  28. auto& realm = settings_object.realm();
  29. // 1. If scripting is disabled for settings, then set source to the empty string.
  30. if (HTML::is_scripting_disabled(realm))
  31. source = ""sv;
  32. // 2. Let script be a new module script that this algorithm will subsequently initialize.
  33. auto script = realm.heap().allocate<JavaScriptModuleScript>(realm, move(base_url), filename, settings_object);
  34. // FIXME: 3. Set script's settings object to settings.
  35. // NOTE: This was already done when constructing.
  36. // 4. Set script's base URL to baseURL.
  37. // NOTE: This was already done when constructing.
  38. // FIXME: 5. Set script's fetch options to options.
  39. // 6. Set script's parse error and error to rethrow to null.
  40. script->set_parse_error(JS::js_null());
  41. script->set_error_to_rethrow(JS::js_null());
  42. // 7. Let result be ParseModule(source, realm, script).
  43. auto result = JS::SourceTextModule::parse(source, realm, filename.view(), script);
  44. // 8. If result is a list of errors, then:
  45. if (result.is_error()) {
  46. auto& parse_error = result.error().first();
  47. dbgln("JavaScriptModuleScript: Failed to parse: {}", parse_error.to_string());
  48. // 1. Set script's parse error to result[0].
  49. script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_string()));
  50. // 2. Return script.
  51. return script;
  52. }
  53. // 9. For each ModuleRequest record requested of result.[[RequestedModules]]:
  54. for (auto const& requested : result.value()->requested_modules()) {
  55. // FIXME: Clarify if this should be checked for all requested before running the steps below.
  56. // 1. If requested.[[Attributes]] contains a Record entry such that entry.[[Key]] is not "type", then:
  57. for (auto const& attribute : requested.attributes) {
  58. if (attribute.key != "type"sv) {
  59. // 1. Let error be a new SyntaxError exception.
  60. auto error = JS::SyntaxError::create(realm, "Module request attributes must only contain a type attribute"_string);
  61. // 2. Set script's parse error to error.
  62. script->set_parse_error(error);
  63. // 3. Return script.
  64. return script;
  65. }
  66. }
  67. // 2. Let url be the result of resolving a module specifier given script and requested.[[Specifier]], catching any exceptions.
  68. auto url = resolve_module_specifier(*script, requested.module_specifier);
  69. // 3. If the previous step threw an exception, then:
  70. if (url.is_exception()) {
  71. // FIXME: 1. Set script's parse error to that exception.
  72. // 2. Return script.
  73. return script;
  74. }
  75. // 4. Let moduleType be the result of running the module type from module request steps given requested.
  76. auto module_type = module_type_from_module_request(requested);
  77. // 5. If the result of running the module type allowed steps given moduleType and settings is false, then:
  78. if (!settings_object.module_type_allowed(module_type)) {
  79. // FIXME: 1. Let error be a new TypeError exception.
  80. // FIXME: 2. Set script's parse error to error.
  81. // 3. Return script.
  82. return script;
  83. }
  84. }
  85. // 10. Set script's record to result.
  86. script->m_record = result.value();
  87. // 11. Return script.
  88. return script;
  89. }
  90. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script
  91. // https://whatpr.org/html/9893/webappapis.html#run-a-module-script
  92. JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting)
  93. {
  94. // 1. Let settings be the settings object of script.
  95. auto& settings = settings_object();
  96. auto& realm = settings.realm();
  97. // 2. Check if we can run script with realm. If this returns "do not run", then return a promise resolved with undefined.
  98. if (can_run_script(realm) == RunScriptDecision::DoNotRun) {
  99. auto promise = JS::Promise::create(settings.realm());
  100. promise->fulfill(JS::js_undefined());
  101. return promise;
  102. }
  103. // 3. Prepare to run script given realm.
  104. prepare_to_run_script(realm);
  105. // 4. Let evaluationPromise be null.
  106. JS::Promise* evaluation_promise = nullptr;
  107. // 5. If script's error to rethrow is not null, then set evaluationPromise to a promise rejected with script's error to rethrow.
  108. if (!error_to_rethrow().is_null()) {
  109. evaluation_promise = JS::Promise::create(realm);
  110. evaluation_promise->reject(error_to_rethrow());
  111. }
  112. // 6. Otherwise:
  113. else {
  114. // 1. Let record be script's record.
  115. auto record = m_record;
  116. VERIFY(record);
  117. // NON-STANDARD: To ensure that LibJS can find the module on the stack, we push a new execution context.
  118. auto module_execution_context = JS::ExecutionContext::create();
  119. module_execution_context->realm = &realm;
  120. module_execution_context->script_or_module = JS::NonnullGCPtr<JS::Module> { *record };
  121. vm().push_execution_context(*module_execution_context);
  122. // 2. Set evaluationPromise to record.Evaluate().
  123. auto elevation_promise_or_error = record->evaluate(vm());
  124. // NOTE: This step will recursively evaluate all of the module's dependencies.
  125. // If Evaluate fails to complete as a result of the user agent aborting the running script,
  126. // then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException.
  127. if (elevation_promise_or_error.is_error()) {
  128. auto promise = JS::Promise::create(realm);
  129. promise->reject(WebIDL::QuotaExceededError::create(realm, "Failed to evaluate module script"_string).ptr());
  130. evaluation_promise = promise;
  131. } else {
  132. evaluation_promise = elevation_promise_or_error.value();
  133. }
  134. // NON-STANDARD: Pop the execution context mentioned above.
  135. vm().pop_execution_context();
  136. }
  137. // FIXME: 7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.
  138. // 8. Clean up after running script with realm.
  139. clean_up_after_running_script(realm);
  140. // 9. Return evaluationPromise.
  141. return evaluation_promise;
  142. }
  143. void JavaScriptModuleScript::visit_edges(Cell::Visitor& visitor)
  144. {
  145. Base::visit_edges(visitor);
  146. visitor.visit(m_record);
  147. }
  148. }