| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 19:02:40 +02:00
										 |  |  | #include <LibJS/AST.h>
 | 
					
						
							|  |  |  | #include <LibJS/Lexer.h>
 | 
					
						
							|  |  |  | #include <LibJS/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:51:58 +01:00
										 |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  | #include <LibJS/Script.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 19:02:40 +02:00
										 |  |  | // 16.1.5 ParseScript ( sourceText, realm, hostDefined ), https://tc39.es/ecma262/#sec-parse-script
 | 
					
						
							| 
									
										
										
										
											2022-11-23 12:39:23 +01:00
										 |  |  | Result<NonnullGCPtr<Script>, Vector<ParserError>> Script::parse(StringView source_text, Realm& realm, StringView filename, HostDefined* host_defined, size_t line_number_offset) | 
					
						
							| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-05-01 01:13:33 +02:00
										 |  |  |     // 1. Let script be ParseText(sourceText, Script).
 | 
					
						
							| 
									
										
										
										
											2022-03-13 23:17:35 +02:00
										 |  |  |     auto parser = Parser(Lexer(source_text, filename, line_number_offset)); | 
					
						
							| 
									
										
										
										
											2022-05-01 01:13:33 +02:00
										 |  |  |     auto script = parser.parse_program(); | 
					
						
							| 
									
										
										
										
											2021-09-09 19:02:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-01 01:13:33 +02:00
										 |  |  |     // 2. If script is a List of errors, return body.
 | 
					
						
							| 
									
										
										
										
											2021-09-14 20:56:57 +02:00
										 |  |  |     if (parser.has_errors()) | 
					
						
							|  |  |  |         return parser.errors(); | 
					
						
							| 
									
										
										
										
											2021-09-09 19:02:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-01 01:13:33 +02:00
										 |  |  |     // 3. Return Script Record { [[Realm]]: realm, [[ECMAScriptCode]]: script, [[HostDefined]]: hostDefined }.
 | 
					
						
							| 
									
										
										
										
											2022-12-14 17:40:33 +00:00
										 |  |  |     return realm.heap().allocate_without_realm<Script>(realm, filename, move(script), host_defined); | 
					
						
							| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-07 18:25:39 +01:00
										 |  |  | Script::Script(Realm& realm, StringView filename, NonnullRefPtr<Program> parse_node, HostDefined* host_defined) | 
					
						
							| 
									
										
										
										
											2022-09-05 14:31:25 +02:00
										 |  |  |     : m_realm(realm) | 
					
						
							| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  |     , m_parse_node(move(parse_node)) | 
					
						
							| 
									
										
										
										
											2022-01-18 19:21:42 +01:00
										 |  |  |     , m_filename(filename) | 
					
						
							| 
									
										
										
										
											2022-02-07 18:25:39 +01:00
										 |  |  |     , m_host_defined(host_defined) | 
					
						
							| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-04-17 22:59:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-05 14:31:25 +02:00
										 |  |  | Script::~Script() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void Script::visit_edges(Cell::Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_realm); | 
					
						
							| 
									
										
										
										
											2022-09-06 01:19:58 +02:00
										 |  |  |     if (m_host_defined) | 
					
						
							|  |  |  |         m_host_defined->visit_host_defined_self(visitor); | 
					
						
							| 
									
										
										
										
											2023-10-28 23:56:15 +02:00
										 |  |  |     for (auto const& loaded_module : m_loaded_modules) | 
					
						
							|  |  |  |         visitor.visit(loaded_module.module); | 
					
						
							| 
									
										
										
										
											2022-09-05 14:31:25 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-09 18:02:31 +02:00
										 |  |  | } |