ClassicScript.cpp 2.6 KB

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