ClassicScript.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm());
  53. interpreter->run(interpreter->global_object(), m_script_record->parse_node());
  54. auto& vm = interpreter->vm();
  55. if (vm.exception())
  56. vm.clear_exception();
  57. dbgln("ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
  58. return vm.last_value();
  59. }
  60. ClassicScript::ClassicScript(AK::URL base_url, String filename)
  61. : Script(move(base_url), move(filename))
  62. {
  63. }
  64. ClassicScript::~ClassicScript()
  65. {
  66. }
  67. }