Script.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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, HostDefined* host_defined)
  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, filename, move(body), host_defined));
  22. }
  23. Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined)
  24. : m_vm(realm.vm())
  25. , m_realm(make_handle(&realm))
  26. , m_parse_node(move(parse_node))
  27. , m_filename(filename)
  28. , m_host_defined(host_defined)
  29. {
  30. }
  31. Script::~Script()
  32. {
  33. }
  34. }