Script.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/AST.h>
  8. #include <LibJS/Lexer.h>
  9. #include <LibJS/Parser.h>
  10. #include <LibJS/Script.h>
  11. namespace JS {
  12. // 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
  13. Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView source_text, Realm& realm, StringView filename)
  14. {
  15. auto timer = Core::ElapsedTimer::start_new();
  16. ScopeGuard timer_guard([&] {
  17. dbgln("JS::Script: Parsed {} in {}ms", filename, timer.elapsed());
  18. });
  19. // 1. Let body be ParseText(sourceText, Script).
  20. auto parser = Parser(Lexer(source_text, filename));
  21. auto body = parser.parse_program();
  22. // 2. If body is a List of errors, return body.
  23. if (parser.has_errors())
  24. return parser.errors();
  25. // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
  26. return adopt_ref(*new Script(realm, move(body)));
  27. }
  28. Script::Script(Realm& realm, NonnullRefPtr<Program> parse_node)
  29. : m_vm(realm.vm())
  30. , m_realm(make_handle(&realm))
  31. , m_parse_node(move(parse_node))
  32. {
  33. }
  34. Script::~Script()
  35. {
  36. }
  37. }