/src/serenity/Userland/Libraries/LibJS/Script.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibJS/AST.h> |
8 | | #include <LibJS/Lexer.h> |
9 | | #include <LibJS/Parser.h> |
10 | | #include <LibJS/Runtime/VM.h> |
11 | | #include <LibJS/Script.h> |
12 | | |
13 | | namespace JS { |
14 | | |
15 | | JS_DEFINE_ALLOCATOR(Script); |
16 | | |
17 | | // 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script |
18 | | Result<NonnullGCPtr<Script>, Vector<ParserError>> Script::parse(StringView source_text, Realm& realm, StringView filename, HostDefined* host_defined, size_t line_number_offset) |
19 | 0 | { |
20 | | // 1. Let script be ParseText(sourceText, Script). |
21 | 0 | auto parser = Parser(Lexer(source_text, filename, line_number_offset)); |
22 | 0 | auto script = parser.parse_program(); |
23 | | |
24 | | // 2. If script is a List of errors, return body. |
25 | 0 | if (parser.has_errors()) |
26 | 0 | return parser.errors(); |
27 | | |
28 | | // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[HostDefined]]: hostDefined }. |
29 | 0 | return realm.heap().allocate_without_realm<Script>(realm, filename, move(script), host_defined); |
30 | 0 | } |
31 | | |
32 | | Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined) |
33 | 0 | : m_realm(realm) |
34 | 0 | , m_parse_node(move(parse_node)) |
35 | 0 | , m_filename(filename) |
36 | 0 | , m_host_defined(host_defined) |
37 | 0 | { |
38 | 0 | } |
39 | | |
40 | | Script::~Script() |
41 | 0 | { |
42 | 0 | } |
43 | | |
44 | | void Script::visit_edges(Cell::Visitor& visitor) |
45 | 0 | { |
46 | 0 | Base::visit_edges(visitor); |
47 | 0 | visitor.visit(m_realm); |
48 | 0 | if (m_host_defined) |
49 | 0 | m_host_defined->visit_host_defined_self(visitor); |
50 | 0 | for (auto const& loaded_module : m_loaded_modules) |
51 | 0 | visitor.visit(loaded_module.module); |
52 | 0 | } |
53 | | |
54 | | } |