ClassicScript.cpp 6.8 KB

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