Script.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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<NonnullGCPtr<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 script be ParseText(sourceText, Script).
  16. auto parser = Parser(Lexer(source_text, filename, line_number_offset));
  17. auto script = parser.parse_program();
  18. // 2. If script is a List of errors, return body.
  19. if (parser.has_errors())
  20. return parser.errors();
  21. // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[HostDefined]]: hostDefined }.
  22. return NonnullGCPtr(*realm.heap().allocate_without_realm<Script>(realm, filename, move(script), host_defined));
  23. }
  24. Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined)
  25. : m_realm(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. void Script::visit_edges(Cell::Visitor& visitor)
  35. {
  36. Base::visit_edges(visitor);
  37. visitor.visit(m_realm);
  38. }
  39. }