| 
									
										
										
										
											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-24 22:03:50 +01:00
										 |  |  | #include <AK/FlyString.h>
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2020-03-24 22:03:50 +01:00
										 |  |  | #include <LibJS/Runtime/Exception.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-16 14:20:30 +01:00
										 |  |  | #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 { | 
					
						
							| 
									
										
										
										
											2020-03-23 19:19:03 +01:00
										 |  |  |     None, | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  |     Function, | 
					
						
							|  |  |  |     Block, | 
					
						
							| 
									
										
										
										
											2020-03-24 14:37:39 +01:00
										 |  |  |     Try, | 
					
						
							| 
									
										
										
										
											2020-03-29 14:34:25 +02:00
										 |  |  |     Breakable, | 
					
						
							| 
									
										
										
										
											2020-03-11 21:09:20 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-22 11:07:55 +01:00
										 |  |  |     HashMap<FlyString, 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 { | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     FlyString name; | 
					
						
							| 
									
										
										
										
											2020-03-12 19:53:31 +01:00
										 |  |  |     Value value; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | class Interpreter { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |     template<typename GlobalObjectType, typename... Args> | 
					
						
							|  |  |  |     static NonnullOwnPtr<Interpreter> create(Args&&... args) | 
					
						
							| 
									
										
										
										
											2020-04-01 18:53:28 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |         auto interpreter = adopt_own(*new Interpreter); | 
					
						
							|  |  |  |         interpreter->m_global_object = interpreter->heap().allocate<GlobalObjectType>(forward<Args>(args)...); | 
					
						
							|  |  |  |         return interpreter; | 
					
						
							| 
									
										
										
										
											2020-04-01 18:53:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |     ~Interpreter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-23 16:46:41 +01:00
										 |  |  |     Value run(const Statement&, 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-23 19:19:03 +01:00
										 |  |  |     void unwind(ScopeType type) { m_unwind_until = type; } | 
					
						
							| 
									
										
										
										
											2020-03-29 14:34:25 +02:00
										 |  |  |     bool should_unwind() const { return m_unwind_until != ScopeType::None; } | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-27 12:54:18 +01:00
										 |  |  |     Optional<Value> get_variable(const FlyString& name); | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     void set_variable(const FlyString& name, Value, bool first_assignment = false); | 
					
						
							|  |  |  |     void declare_variable(const FlyString& 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-29 00:45:53 +01:00
										 |  |  |     Value call(Function*, Value this_value = {}, const Vector<Value>& arguments = {}); | 
					
						
							| 
									
										
										
										
											2020-03-18 15:20:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-22 11:07:55 +01:00
										 |  |  |     CallFrame& push_call_frame() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         m_call_stack.append({ js_undefined(), {} }); | 
					
						
							|  |  |  |         return m_call_stack.last(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-17 11:00:09 +01:00
										 |  |  |     void pop_call_frame() { m_call_stack.take_last(); } | 
					
						
							| 
									
										
										
										
											2020-03-28 22:48:35 +01:00
										 |  |  |     const CallFrame& call_frame() { return m_call_stack.last(); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 22:38:59 +02:00
										 |  |  |     size_t argument_count() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_call_stack.is_empty()) | 
					
						
							|  |  |  |             return 0; | 
					
						
							|  |  |  |         return m_call_stack.last().arguments.size(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Value argument(size_t index) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (m_call_stack.is_empty()) | 
					
						
							|  |  |  |             return {}; | 
					
						
							|  |  |  |         auto& arguments = m_call_stack.last().arguments; | 
					
						
							|  |  |  |         return index < arguments.size() ? arguments[index] : js_undefined(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-04-02 19:32:21 +02:00
										 |  |  |     Shape* empty_object_shape() { return m_empty_object_shape; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-20 21:01:36 +01:00
										 |  |  |     Object* array_prototype() { return m_array_prototype; } | 
					
						
							| 
									
										
										
										
											2020-03-24 14:37:39 +01:00
										 |  |  |     Object* error_prototype() { return m_error_prototype; } | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  |     Object* date_prototype() { return m_date_prototype; } | 
					
						
							| 
									
										
										
										
											2020-03-24 14:37:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 22:03:50 +01:00
										 |  |  |     Exception* exception() { return m_exception; } | 
					
						
							| 
									
										
										
										
											2020-03-24 14:37:39 +01:00
										 |  |  |     void clear_exception() { m_exception = nullptr; } | 
					
						
							| 
									
										
										
										
											2020-03-24 22:03:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     template<typename T, typename... Args> | 
					
						
							|  |  |  |     Value throw_exception(Args&&... args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return throw_exception(heap().allocate<T>(forward<Args>(args)...)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Value throw_exception(Exception*); | 
					
						
							|  |  |  |     Value throw_exception(Value value) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return throw_exception(heap().allocate<Exception>(value)); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-03-15 15:11:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:56:49 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |     Interpreter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-02 19:32:21 +02:00
										 |  |  |     Shape* m_empty_object_shape { nullptr }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-20 21:01:36 +01:00
										 |  |  |     Object* m_array_prototype { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-24 14:37:39 +01:00
										 |  |  |     Object* m_error_prototype { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-30 00:21:56 +01:00
										 |  |  |     Object* m_date_prototype { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-24 14:37:39 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-24 22:03:50 +01:00
										 |  |  |     Exception* m_exception { nullptr }; | 
					
						
							| 
									
										
										
										
											2020-03-23 19:19:03 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ScopeType m_unwind_until { ScopeType::None }; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |