ClassicScript.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2021-2023, Andreas Kling <andreas@ladybird.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibCore/ElapsedTimer.h>
  8. #include <LibJS/Bytecode/Interpreter.h>
  9. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  10. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  11. #include <LibWeb/HTML/Scripting/Environments.h>
  12. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  13. #include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
  14. #include <LibWeb/WebIDL/DOMException.h>
  15. namespace Web::HTML {
  16. JS_DEFINE_ALLOCATOR(ClassicScript);
  17. // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
  18. // https://whatpr.org/html/9893/webappapis.html#creating-a-classic-script
  19. JS::NonnullGCPtr<ClassicScript> ClassicScript::create(ByteString filename, StringView source, EnvironmentSettingsObject& environment_settings_object, URL::URL base_url, size_t source_line_number, MutedErrors muted_errors)
  20. {
  21. auto& realm = environment_settings_object.realm();
  22. auto& vm = realm.vm();
  23. // 1. If muted errors is true, then set baseURL to about:blank.
  24. if (muted_errors == MutedErrors::Yes)
  25. base_url = "about:blank"sv;
  26. // 2. If scripting is disabled for realm, then set source to the empty string.
  27. if (is_scripting_disabled(realm))
  28. source = ""sv;
  29. // 3. Let script be a new classic script that this algorithm will subsequently initialize.
  30. auto script = vm.heap().allocate_without_realm<ClassicScript>(move(base_url), move(filename), environment_settings_object);
  31. // FIXME: 4. Set script's realm to realm. (NOTE: This was already done when constructing.)
  32. // 5. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
  33. // FIXME: 6. Set script's fetch options to options.
  34. // 7. Set script's muted errors to muted errors.
  35. script->m_muted_errors = muted_errors;
  36. // 8. 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. // FIXME: 9. Record classic script creation time given script and sourceURLForWindowScripts .
  40. // 10. Let result be ParseScript(source, realm, script).
  41. auto parse_timer = Core::ElapsedTimer::start_new();
  42. auto result = JS::Script::parse(source, realm, script->filename(), script, source_line_number);
  43. dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Parsed {} in {}ms", script->filename(), parse_timer.elapsed());
  44. // 11. If result is a list of errors, then:
  45. if (result.is_error()) {
  46. auto& parse_error = result.error().first();
  47. dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Failed to parse: {}", parse_error.to_string());
  48. // 1. Set script's parse error and its error to rethrow to result[0].
  49. script->set_parse_error(JS::SyntaxError::create(realm, parse_error.to_string()));
  50. script->set_error_to_rethrow(script->parse_error());
  51. // 2. Return script.
  52. return script;
  53. }
  54. // 12. Set script's record to result.
  55. script->m_script_record = *result.release_value();
  56. // 13. Return script.
  57. return script;
  58. }
  59. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
  60. // https://whatpr.org/html/9893/webappapis.html#run-a-classic-script
  61. JS::Completion ClassicScript::run(RethrowErrors rethrow_errors)
  62. {
  63. // 1. Let settings be the settings object of script.
  64. auto& settings = settings_object();
  65. auto& realm = settings.realm();
  66. // 2. Check if we can run script with settings. If this returns "do not run" then return NormalCompletion(empty).
  67. if (can_run_script(realm) == RunScriptDecision::DoNotRun)
  68. return JS::normal_completion({});
  69. // 3. Prepare to run script given realm.
  70. prepare_to_run_script(realm);
  71. // 4. Let evaluationStatus be null.
  72. JS::Completion evaluation_status;
  73. // 5. If script's error to rethrow is not null, then set evaluationStatus to Completion { [[Type]]: throw, [[Value]]: script's error to rethrow, [[Target]]: empty }.
  74. if (!error_to_rethrow().is_null()) {
  75. evaluation_status = JS::Completion { JS::Completion::Type::Throw, error_to_rethrow() };
  76. } else {
  77. auto timer = Core::ElapsedTimer::start_new();
  78. // 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
  79. evaluation_status = vm().bytecode_interpreter().run(*m_script_record);
  80. // FIXME: If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.
  81. dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
  82. }
  83. // 7. If evaluationStatus is an abrupt completion, then:
  84. if (evaluation_status.is_abrupt()) {
  85. // 1. If rethrow errors is true and script's muted errors is false, then:
  86. if (rethrow_errors == RethrowErrors::Yes && m_muted_errors == MutedErrors::No) {
  87. // 1. Clean up after running script with realm.
  88. clean_up_after_running_script(realm);
  89. // 2. Rethrow evaluationStatus.[[Value]].
  90. return JS::throw_completion(*evaluation_status.value());
  91. }
  92. // 2. If rethrow errors is true and script's muted errors is true, then:
  93. if (rethrow_errors == RethrowErrors::Yes && m_muted_errors == MutedErrors::Yes) {
  94. // 1. Clean up after running script with realm.
  95. clean_up_after_running_script(realm);
  96. // 2. Throw a "NetworkError" DOMException.
  97. return throw_completion(WebIDL::NetworkError::create(realm, "Script error."_string));
  98. }
  99. // 3. Otherwise, rethrow errors is false. Perform the following steps:
  100. VERIFY(rethrow_errors == RethrowErrors::No);
  101. // 1. Report an exception given by evaluationStatus.[[Value]] for script's settings object's global object.
  102. auto* window_or_worker = dynamic_cast<WindowOrWorkerGlobalScopeMixin*>(&settings.global_object());
  103. VERIFY(window_or_worker);
  104. window_or_worker->report_an_exception(*evaluation_status.value());
  105. // 2. Clean up after running script with realm.
  106. clean_up_after_running_script(realm);
  107. // 3. Return evaluationStatus.
  108. return evaluation_status;
  109. }
  110. // 8. Clean up after running script with realm.
  111. clean_up_after_running_script(realm);
  112. // 9. If evaluationStatus is a normal completion, then return evaluationStatus.
  113. VERIFY(!evaluation_status.is_abrupt());
  114. return evaluation_status;
  115. // FIXME: 10. If we've reached this point, evaluationStatus was left as null because the script was aborted prematurely during evaluation.
  116. // Return Completion { [[Type]]: throw, [[Value]]: a new "QuotaExceededError" DOMException, [[Target]]: empty }.
  117. }
  118. ClassicScript::ClassicScript(URL::URL base_url, ByteString filename, EnvironmentSettingsObject& environment_settings_object)
  119. : Script(move(base_url), move(filename), environment_settings_object)
  120. {
  121. }
  122. ClassicScript::~ClassicScript() = default;
  123. void ClassicScript::visit_edges(Cell::Visitor& visitor)
  124. {
  125. Base::visit_edges(visitor);
  126. visitor.visit(m_script_record);
  127. }
  128. }