| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-12 19:53:31 +01:00
										 |  |  | #include <AK/String.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							|  |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-16 14:20:30 +01:00
										 |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/Value.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  | enum class ScopeType { | 
					
						
							|  |  |  |     Function, | 
					
						
							|  |  |  |     Block, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 14:24:34 +02:00
										 |  |  | struct Variable { | 
					
						
							|  |  |  |     Value value; | 
					
						
							|  |  |  |     DeclarationType declaration_type; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | struct ScopeFrame { | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  |     ScopeType type; | 
					
						
							| 
									
										
										
										
											2020-03-18 11:23:53 +01:00
										 |  |  |     NonnullRefPtr<ScopeNode> scope_node; | 
					
						
							| 
									
										
										
										
											2020-03-12 14:24:34 +02:00
										 |  |  |     HashMap<String, Variable> variables; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 11:00:09 +01:00
										 |  |  | struct CallFrame { | 
					
						
							|  |  |  |     Value this_value; | 
					
						
							|  |  |  |     Vector<Value> arguments; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 19:53:31 +01:00
										 |  |  | struct Argument { | 
					
						
							|  |  |  |     String name; | 
					
						
							|  |  |  |     Value value; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | class Interpreter { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     Interpreter(); | 
					
						
							|  |  |  |     ~Interpreter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 19:53:31 +01:00
										 |  |  |     Value run(const ScopeNode&, Vector<Argument> = {}, ScopeType = ScopeType::Block); | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Object& global_object() { return *m_global_object; } | 
					
						
							|  |  |  |     const Object& global_object() const { return *m_global_object; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |     Heap& heap() { return m_heap; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     void do_return(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  |     Value get_variable(const String& name); | 
					
						
							| 
									
										
										
										
											2020-03-15 23:57:54 +02:00
										 |  |  |     void set_variable(String name, Value, bool first_assignment = false); | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  |     void declare_variable(String name, DeclarationType); | 
					
						
							| 
									
										
										
										
											2020-03-09 21:13:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:12:34 +01:00
										 |  |  |     void gather_roots(Badge<Heap>, HashTable<Cell*>&); | 
					
						
							| 
									
										
										
										
											2020-03-09 21:29:22 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-12 19:53:31 +01:00
										 |  |  |     void enter_scope(const ScopeNode&, Vector<Argument>, ScopeType); | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     void exit_scope(const ScopeNode&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-18 15:20:49 +01:00
										 |  |  |     Value call(Function*, Value this_value, const Vector<Value>& arguments); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-17 11:00:09 +01:00
										 |  |  |     CallFrame& push_call_frame() { m_call_stack.append({ js_undefined(), {} }); return m_call_stack.last(); } | 
					
						
							|  |  |  |     void pop_call_frame() { m_call_stack.take_last(); } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:01:10 +01:00
										 |  |  |     Value this_value() const | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-03-17 11:00:09 +01:00
										 |  |  |         if (m_call_stack.is_empty()) | 
					
						
							| 
									
										
										
										
											2020-03-15 15:01:10 +01:00
										 |  |  |             return m_global_object; | 
					
						
							| 
									
										
										
										
											2020-03-17 11:00:09 +01:00
										 |  |  |         return m_call_stack.last().this_value; | 
					
						
							| 
									
										
										
										
											2020-03-15 15:01:10 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-15 15:11:13 +01:00
										 |  |  |     Object* string_prototype() { return m_string_prototype; } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:25:43 +01:00
										 |  |  |     Object* object_prototype() { return m_object_prototype; } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:11:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:56:49 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  |     Heap m_heap; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  |     Vector<ScopeFrame> m_scope_stack; | 
					
						
							| 
									
										
										
										
											2020-03-17 11:00:09 +01:00
										 |  |  |     Vector<CallFrame> m_call_stack; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Object* m_global_object { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-15 15:11:13 +01:00
										 |  |  |     Object* m_string_prototype { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-15 15:25:43 +01:00
										 |  |  |     Object* m_object_prototype { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |