Script.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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/Runtime/VM.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, HostDefined* host_defined, size_t line_number_offset)
  14. {
  15. // 1. Let body be ParseText(sourceText, Script).
  16. auto parser = Parser(Lexer(source_text, filename, line_number_offset));
  17. auto body = parser.parse_program();
  18. // 2. If body is a List of errors, return body.
  19. if (parser.has_errors())
  20. return parser.errors();
  21. // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: body, [[HostDefined]]: hostDefined }.
  22. return adopt_ref(*new Script(realm, filename, move(body), host_defined));
  23. }
  24. Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined)
  25. : m_vm(realm.vm())
  26. , m_realm(make_handle(&realm))
  27. , m_parse_node(move(parse_node))
  28. , m_filename(filename)
  29. , m_host_defined(host_defined)
  30. {
  31. }
  32. }