ClassicScript.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCore/ElapsedTimer.h>
  7. #include <LibJS/Interpreter.h>
  8. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  9. namespace Web::HTML {
  10. // https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-classic-script
  11. NonnullRefPtr<ClassicScript> ClassicScript::create(String filename, StringView source, JS::Realm& realm, AK::URL base_url, MutedErrors muted_errors)
  12. {
  13. // 1. If muted errors was not provided, let it be false. (NOTE: This is taken care of by the default argument.)
  14. // 2. If muted errors is true, then set baseURL to about:blank.
  15. if (muted_errors == MutedErrors::Yes)
  16. base_url = "about:blank";
  17. // FIXME: 3. If scripting is disabled for settings, then set source to the empty string.
  18. // 4. Let script be a new classic script that this algorithm will subsequently initialize.
  19. auto script = adopt_ref(*new ClassicScript(move(base_url), move(filename)));
  20. // FIXME: 5. Set script's settings object to settings.
  21. // 6. Set script's base URL to baseURL. (NOTE: This was already done when constructing.)
  22. // FIXME: 7. Set script's fetch options to options.
  23. // 8. Set script's muted errors to muted errors.
  24. script->m_muted_errors = muted_errors;
  25. // FIXME: 9. Set script's parse error and error to rethrow to null.
  26. // 10. Let result be ParseScript(source, settings's Realm, script).
  27. auto parse_timer = Core::ElapsedTimer::start_new();
  28. auto result = JS::Script::parse(source, realm, script->filename());
  29. dbgln("ClassicScript: Parsed {} in {}ms", script->filename(), parse_timer.elapsed());
  30. // 11. If result is a list of errors, then:
  31. if (result.is_error()) {
  32. // FIXME: 1. Set script's parse error and its error to rethrow to result[0].
  33. // 2. Return script.
  34. return script;
  35. }
  36. // 12. Set script's record to result.
  37. script->m_script_record = result.release_value();
  38. // 13. Return script.
  39. return script;
  40. }
  41. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
  42. JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
  43. {
  44. if (!m_script_record) {
  45. // FIXME: Throw a SyntaxError per the spec.
  46. dbgln("ClassicScript: Unable to run script {}", filename());
  47. return {};
  48. }
  49. dbgln("ClassicScript: Running script {}", filename());
  50. (void)rethrow_errors;
  51. auto timer = Core::ElapsedTimer::start_new();
  52. // 6. Otherwise, set evaluationStatus to ScriptEvaluation(script's record).
  53. // FIXME: Interpreter::run doesn't currently return a JS::Completion.
  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("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. }