ModuleScript.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. GC_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<GC::Ptr<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. Set script's record to result.
  51. script->m_record = result.value();
  52. // 10. Return script.
  53. return script;
  54. }
  55. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-module-script
  56. // https://whatpr.org/html/9893/webappapis.html#run-a-module-script
  57. JS::Promise* JavaScriptModuleScript::run(PreventErrorReporting)
  58. {
  59. // 1. Let realm be the realm of script.
  60. auto& realm = this->realm();
  61. // 2. Check if we can run script with realm. If this returns "do not run", then return a promise resolved with undefined.
  62. if (can_run_script(realm) == RunScriptDecision::DoNotRun) {
  63. auto promise = JS::Promise::create(realm);
  64. promise->fulfill(JS::js_undefined());
  65. return promise;
  66. }
  67. // 3. Prepare to run script given realm.
  68. prepare_to_run_script(realm);
  69. // 4. Let evaluationPromise be null.
  70. JS::Promise* evaluation_promise = nullptr;
  71. // 5. If script's error to rethrow is not null, then set evaluationPromise to a promise rejected with script's error to rethrow.
  72. if (!error_to_rethrow().is_null()) {
  73. evaluation_promise = JS::Promise::create(realm);
  74. evaluation_promise->reject(error_to_rethrow());
  75. }
  76. // 6. Otherwise:
  77. else {
  78. // 1. Let record be script's record.
  79. auto record = m_record;
  80. VERIFY(record);
  81. // NON-STANDARD: To ensure that LibJS can find the module on the stack, we push a new execution context.
  82. auto module_execution_context = JS::ExecutionContext::create();
  83. module_execution_context->realm = &realm;
  84. module_execution_context->script_or_module = GC::Ref<JS::Module> { *record };
  85. vm().push_execution_context(*module_execution_context);
  86. // 2. Set evaluationPromise to record.Evaluate().
  87. auto elevation_promise_or_error = record->evaluate(vm());
  88. // NOTE: This step will recursively evaluate all of the module's dependencies.
  89. // If Evaluate fails to complete as a result of the user agent aborting the running script,
  90. // then set evaluationPromise to a promise rejected with a new "QuotaExceededError" DOMException.
  91. if (elevation_promise_or_error.is_error()) {
  92. auto promise = JS::Promise::create(realm);
  93. promise->reject(WebIDL::QuotaExceededError::create(realm, "Failed to evaluate module script"_string).ptr());
  94. evaluation_promise = promise;
  95. } else {
  96. evaluation_promise = elevation_promise_or_error.value();
  97. }
  98. // NON-STANDARD: Pop the execution context mentioned above.
  99. vm().pop_execution_context();
  100. }
  101. // FIXME: 7. If preventErrorReporting is false, then upon rejection of evaluationPromise with reason, report the exception given by reason for script.
  102. // 8. Clean up after running script with realm.
  103. clean_up_after_running_script(realm);
  104. // 9. Return evaluationPromise.
  105. return evaluation_promise;
  106. }
  107. void JavaScriptModuleScript::visit_edges(Cell::Visitor& visitor)
  108. {
  109. Base::visit_edges(visitor);
  110. visitor.visit(m_record);
  111. }
  112. }