Script.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/AST.h>
  7. #include <LibJS/Lexer.h>
  8. #include <LibJS/Parser.h>
  9. #include <LibJS/Script.h>
  10. namespace JS {
  11. // 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
  12. Result<NonnullRefPtr<Script>, Vector<Parser::Error>> Script::parse(StringView source_text, Realm& realm, StringView filename)
  13. {
  14. // 1. Let body be ParseText(sourceText, Script).
  15. auto parser = Parser(Lexer(source_text, filename));
  16. auto body = parser.parse_program();
  17. // 2. If body is a List of errors, return body.
  18. if (parser.has_errors())
  19. return parser.errors();
  20. // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
  21. return adopt_ref(*new Script(realm, move(body)));
  22. }
  23. Script::Script(Realm& realm, NonnullRefPtr<Program> parse_node)
  24. : m_vm(realm.vm())
  25. , m_realm(make_handle(&realm))
  26. , m_parse_node(move(parse_node))
  27. {
  28. }
  29. Script::~Script()
  30. {
  31. }
  32. }