ModuleScript.cpp 7.6 KB

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