ClassicScript.cpp 2.4 KB

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