ClassicScript.cpp 3.3 KB

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