ClassicScript.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 result = JS::Script::parse(source, realm, script->filename());
  28. // 11. If result is a list of errors, then:
  29. if (result.is_error()) {
  30. // FIXME: 1. Set script's parse error and its error to rethrow to result[0].
  31. // 2. Return script.
  32. return script;
  33. }
  34. // 12. Set script's record to result.
  35. script->m_script_record = result.release_value();
  36. // 13. Return script.
  37. return script;
  38. }
  39. // https://html.spec.whatwg.org/multipage/webappapis.html#run-a-classic-script
  40. JS::Value ClassicScript::run(RethrowErrors rethrow_errors)
  41. {
  42. dbgln("ClassicScript: Running script {}", filename());
  43. (void)rethrow_errors;
  44. auto timer = Core::ElapsedTimer::start_new();
  45. auto interpreter = JS::Interpreter::create_with_existing_realm(m_script_record->realm());
  46. interpreter->run(interpreter->global_object(), m_script_record->parse_node());
  47. auto& vm = interpreter->vm();
  48. if (vm.exception())
  49. vm.clear_exception();
  50. dbgln("ClassicScript: Finished running script {}, Duration: {}ms", filename(), timer.elapsed());
  51. return vm.last_value();
  52. }
  53. ClassicScript::ClassicScript(AK::URL base_url, String filename)
  54. : Script(move(base_url), move(filename))
  55. {
  56. }
  57. ClassicScript::~ClassicScript()
  58. {
  59. }
  60. }