ModuleScript.cpp 7.2 KB

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