| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2020-05-23 12:53:09 -04:00
										 |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-27 22:22:08 -07:00
										 |  |  | #include <LibJS/AST.h>
 | 
					
						
							| 
									
										
										
										
											2020-05-01 14:40:43 +02:00
										 |  |  | #include <LibJS/Console.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | #include <LibJS/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  | #include <LibJS/Heap/DeferGC.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-16 14:20:30 +01:00
										 |  |  | #include <LibJS/Heap/Heap.h>
 | 
					
						
							| 
									
										
										
										
											2020-06-09 22:48:01 -07:00
										 |  |  | #include <LibJS/Runtime/ErrorTypes.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-24 22:03:50 +01:00
										 |  |  | #include <LibJS/Runtime/Exception.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-15 21:58:22 +02:00
										 |  |  | #include <LibJS/Runtime/LexicalEnvironment.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-19 17:24:56 +02:00
										 |  |  | #include <LibJS/Runtime/MarkedValueList.h>
 | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  | #include <LibJS/Runtime/VM.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-05-23 12:53:09 -04:00
										 |  |  | class Interpreter : public Weakable<Interpreter> { | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |     template<typename GlobalObjectType, typename... Args> | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |     static NonnullOwnPtr<Interpreter> create(VM& vm, Args&&... args) | 
					
						
							| 
									
										
										
										
											2020-04-01 18:53:28 +02:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |         DeferGC defer_gc(vm.heap()); | 
					
						
							|  |  |  |         auto interpreter = adopt_own(*new Interpreter(vm)); | 
					
						
							| 
									
										
										
										
											2020-09-21 13:36:32 +02:00
										 |  |  |         VM::InterpreterExecutionScope scope(*interpreter); | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |         interpreter->m_global_object = make_handle(static_cast<Object*>(interpreter->heap().allocate_without_global_object<GlobalObjectType>(forward<Args>(args)...))); | 
					
						
							|  |  |  |         static_cast<GlobalObjectType*>(interpreter->m_global_object.cell())->initialize(); | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |         return interpreter; | 
					
						
							| 
									
										
										
										
											2020-04-01 18:53:28 +02:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 22:18:32 +04:30
										 |  |  |     template<typename... Args> | 
					
						
							|  |  |  |     [[nodiscard]] ALWAYS_INLINE Value call(Function& function, Value this_value, Args... args) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Are there any values in this argpack?
 | 
					
						
							|  |  |  |         // args = [] -> if constexpr (false)
 | 
					
						
							|  |  |  |         // args = [x, y, z] -> if constexpr ((void)x, true || ...)
 | 
					
						
							|  |  |  |         if constexpr ((((void)args, true) || ...)) { | 
					
						
							|  |  |  |             MarkedValueList arglist { heap() }; | 
					
						
							|  |  |  |             (..., arglist.append(move(args))); | 
					
						
							|  |  |  |             return call(function, this_value, move(arglist)); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return call(function, this_value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  |     ~Interpreter(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-11 22:47:43 +01:00
										 |  |  |     Value run(GlobalObject&, const Program&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-08 11:05:38 +02:00
										 |  |  |     GlobalObject& global_object(); | 
					
						
							|  |  |  |     const GlobalObject& global_object() const; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |     VM& vm() { return *m_vm; } | 
					
						
							| 
									
										
										
										
											2020-09-27 15:18:55 +02:00
										 |  |  |     const VM& vm() const { return *m_vm; } | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |     Heap& heap() { return vm().heap(); } | 
					
						
							| 
									
										
										
										
											2020-09-21 15:28:09 +02:00
										 |  |  |     Exception* exception() { return vm().exception(); } | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-01 14:40:43 +02:00
										 |  |  |     Console& console() { return m_console; } | 
					
						
							| 
									
										
										
										
											2020-05-04 14:18:15 +02:00
										 |  |  |     const Console& console() const { return m_console; } | 
					
						
							| 
									
										
										
										
											2020-05-01 07:14:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 16:56:58 +02:00
										 |  |  |     bool in_strict_mode() const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // FIXME: This implementation is bogus; strict mode is per-file or per-function, not per-block!
 | 
					
						
							|  |  |  |         if (m_scope_stack.is_empty()) | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         return m_scope_stack.last().scope_node->in_strict_mode(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 15:18:55 +02:00
										 |  |  |     size_t argument_count() const { return vm().argument_count(); } | 
					
						
							|  |  |  |     Value argument(size_t index) const { return vm().argument(index); } | 
					
						
							|  |  |  |     Value this_value(Object& global_object) const { return vm().this_value(global_object); } | 
					
						
							|  |  |  |     LexicalEnvironment* current_environment() { return vm().current_environment(); } | 
					
						
							|  |  |  |     const CallFrame& call_frame() { return vm().call_frame(); } | 
					
						
							| 
									
										
										
										
											2020-06-08 13:31:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 16:56:58 +02:00
										 |  |  |     void enter_scope(const ScopeNode&, ArgumentVector, ScopeType, GlobalObject&); | 
					
						
							|  |  |  |     void exit_scope(const ScopeNode&); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Value execute_statement(GlobalObject&, const Statement&, ArgumentVector = {}, ScopeType = ScopeType::Block); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-14 13:56:49 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |     explicit Interpreter(VM&); | 
					
						
							| 
									
										
										
										
											2020-04-01 21:04:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 22:18:32 +04:30
										 |  |  |     [[nodiscard]] Value call_internal(Function&, Value this_value, Optional<MarkedValueList>); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |     NonnullRefPtr<VM> m_vm; | 
					
						
							| 
									
										
										
										
											2020-03-08 19:23:58 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-20 19:24:44 +02:00
										 |  |  |     Handle<Object> m_global_object; | 
					
						
							| 
									
										
										
										
											2020-04-10 12:42:33 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-01 14:40:43 +02:00
										 |  |  |     Console m_console; | 
					
						
							| 
									
										
										
										
											2020-09-27 16:56:58 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Vector<ScopeFrame> m_scope_stack; | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-25 22:18:32 +04:30
										 |  |  | template<> | 
					
						
							|  |  |  | [[nodiscard]] ALWAYS_INLINE Value Interpreter::call(Function& function, Value this_value, MarkedValueList arguments) { return call_internal(function, this_value, move(arguments)); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | [[nodiscard]] ALWAYS_INLINE Value Interpreter::call(Function& function, Value this_value, Optional<MarkedValueList> arguments) { return call_internal(function, this_value, move(arguments)); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<> | 
					
						
							|  |  |  | [[nodiscard]] ALWAYS_INLINE Value Interpreter::call(Function& function, Value this_value) { return call(function, this_value, Optional<MarkedValueList> {}); } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 19:42:11 +01:00
										 |  |  | } |