ClassicScript.cpp 7.0 KB

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