ClassicScript.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/HTML/Scripting/ClassicScript.h>
  10. #include <LibWeb/HTML/Scripting/ExceptionReporter.h>
  11. namespace Web::HTML {
  12. // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
  13. NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, JS::Realm& realm, AK::URL base_url, MutedErrors muted_errors)
  14. {
  15. // 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.)
  16. // 2. If muted errors is true, then set baseURL to about:blank.
  17. if (muted_errors == MutedErrors::Yes)
  18. base_url = "about:blank";
  19. // FIXME: 3. If scripting is disabled for settings, then set source to the empty string.
  20. // 4. Let script be a new classic script that this algorithm will subsequently initialize.
  21. auto script = adopt_ref(*new ClassicScript(move(base_url), move(filename)));
  22. // FIXME: 5. Set script's settings object to settings.
  23. // 6. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
  24. // FIXME: 7. Set script's fetch options to options.
  25. // 8. Set script's muted errors to muted errors.
  26. script->m_muted_errors = muted_errors;
  27. // FIXME: 9. Set script's parse error and error to rethrow to null.
  28. // 10. Let result be ParseScript(source, settings's Realm, script).
  29. auto parse_timer = Core::ElapsedTimer::start_new();
  30. auto result = JS::Script::parse(source, realm, script->filename());
  31. dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Parsed {} in {}ms", script->filename(), parse_timer.elapsed());
  32. // 11. If result is a list of errors, then:
  33. if (result.is_error()) {
  34. // FIXME: 1. Set script's parse error and its error to rethrow to result[0].
  35. // 2. Return script.
  36. return script;
  37. }
  38. // 12. Set script's record to result.
  39. script->m_script_record = result.release_value();
  40. // 13. Return script.
  41. return script;
  42. }
  43. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
  44. JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
  45. {
  46. if (!m_script_record) {
  47. // FIXME: Throw a SyntaxError per the spec.
  48. dbgln("ClassicScript: Unable to run script {}", filename());
  49. return {};
  50. }
  51. dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Running script {}", filename());
  52. (void)rethrow_errors;
  53. auto timer = Core::ElapsedTimer::start_new();
  54. // 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
  55. auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm());
  56. auto evaluation_status = interpreter->run(*m_script_record);
  57. // FIXME: If ScriptEvaluation does not complete because the user agent has aborted the running script, leave evaluationStatus as null.
  58. dbgln_if(HTML_SCRIPT_DEBUG, "ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
  59. if (evaluation_status.is_error()) {
  60. // FIXME: Propagate error according to the spec.
  61. report_exception(evaluation_status);
  62. return {};
  63. }
  64. return evaluation_status.value();
  65. }
  66. ClassicScript::ClassicScript(AK::URL base_url, String filename)
  67. : Script(move(base_url), move(filename))
  68. {
  69. }
  70. ClassicScript::~ClassicScript()
  71. {
  72. }
  73. }