| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2021-04-22 22:51:19 +02:00
										 |  |  |  * Copyright (c) 2020, Linus Groh <linusg@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibJS/AST.h>
 | 
					
						
							|  |  |  | #include <LibJS/Interpreter.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  | #include <LibJS/Lexer.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | #include <LibJS/Parser.h>
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | #include <LibJS/Runtime/ECMAScriptFunctionObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | #include <LibJS/Runtime/Error.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/FunctionConstructor.h>
 | 
					
						
							| 
									
										
										
										
											2021-06-27 21:48:34 +02:00
										 |  |  | #include <LibJS/Runtime/FunctionObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-18 13:18:06 +02:00
										 |  |  | #include <LibJS/Runtime/GlobalObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace JS { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | FunctionConstructor::FunctionConstructor(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2021-06-28 03:45:49 +03:00
										 |  |  |     : NativeFunction(vm().names.Function.as_string(), *global_object.function_prototype()) | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  | void FunctionConstructor::initialize(GlobalObject& global_object) | 
					
						
							| 
									
										
										
										
											2020-06-20 15:40:48 +02:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-10-13 23:49:19 +02:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2020-07-22 17:50:18 +02:00
										 |  |  |     NativeFunction::initialize(global_object); | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 20.2.2.2 Function.prototype, https://tc39.es/ecma262/#sec-function.prototype
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.prototype, global_object.function_prototype(), 0); | 
					
						
							| 
									
										
										
										
											2021-06-19 00:38:41 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-06 02:15:08 +03:00
										 |  |  |     define_direct_property(vm.names.length, Value(1), Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | FunctionConstructor::~FunctionConstructor() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  | // 20.2.1.1.1 CreateDynamicFunction ( constructor, newTarget, kind, args ), https://tc39.es/ecma262/#sec-createdynamicfunction
 | 
					
						
							| 
									
										
										
										
											2021-10-29 00:36:21 +03:00
										 |  |  | ThrowCompletionOr<RefPtr<FunctionExpression>> FunctionConstructor::create_dynamic_function_node(GlobalObject& global_object, FunctionObject&, FunctionKind kind) | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  |     auto& vm = global_object.vm(); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |     String parameters_source = ""; | 
					
						
							|  |  |  |     String body_source = ""; | 
					
						
							| 
									
										
										
										
											2021-10-12 17:49:01 +01:00
										 |  |  |     if (vm.argument_count() == 1) | 
					
						
							| 
									
										
										
										
											2021-10-29 00:36:21 +03:00
										 |  |  |         body_source = TRY(vm.argument(0).to_string(global_object)); | 
					
						
							| 
									
										
										
										
											2020-09-27 18:36:49 +02:00
										 |  |  |     if (vm.argument_count() > 1) { | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |         Vector<String> parameters; | 
					
						
							| 
									
										
										
										
											2021-10-12 17:49:01 +01:00
										 |  |  |         for (size_t i = 0; i < vm.argument_count() - 1; ++i) | 
					
						
							| 
									
										
										
										
											2021-10-29 00:36:21 +03:00
										 |  |  |             parameters.append(TRY(vm.argument(i).to_string(global_object))); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |         StringBuilder parameters_builder; | 
					
						
							|  |  |  |         parameters_builder.join(',', parameters); | 
					
						
							|  |  |  |         parameters_source = parameters_builder.build(); | 
					
						
							| 
									
										
										
										
											2021-10-29 00:36:21 +03:00
										 |  |  |         body_source = TRY(vm.argument(vm.argument_count() - 1).to_string(global_object)); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-11-15 01:53:24 +01:00
										 |  |  |     auto is_generator = kind == FunctionKind::Generator || kind == FunctionKind::AsyncGenerator; | 
					
						
							|  |  |  |     auto is_async = kind == FunctionKind::Async || kind == FunctionKind::AsyncGenerator; | 
					
						
							| 
									
										
										
										
											2021-11-09 20:39:22 +02:00
										 |  |  |     auto source = String::formatted("{}function{} anonymous({}\n) {{\n{}\n}}", is_async ? "async " : "", is_generator ? "*" : "", parameters_source, body_source); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |     auto parser = Parser(Lexer(source)); | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  |     auto function = parser.parse_function_node<FunctionExpression>(); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |     if (parser.has_errors()) { | 
					
						
							| 
									
										
										
										
											2020-05-14 16:26:01 +01:00
										 |  |  |         auto error = parser.errors()[0]; | 
					
						
							| 
									
										
										
										
											2021-10-29 00:36:21 +03:00
										 |  |  |         return vm.throw_completion<SyntaxError>(global_object, error.to_string()); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-09-27 18:45:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  |     return function; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Value> FunctionConstructor::call() | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     return TRY(construct(*this)); | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
 | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  | ThrowCompletionOr<Object*> FunctionConstructor::construct(FunctionObject& new_target) | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto& vm = this->vm(); | 
					
						
							| 
									
										
										
										
											2021-10-29 00:36:21 +03:00
										 |  |  |     auto function = TRY(create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular)); | 
					
						
							| 
									
										
										
										
											2021-06-16 10:52:48 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-27 18:45:21 +02:00
										 |  |  |     OwnPtr<Interpreter> local_interpreter; | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     Interpreter* interpreter = vm.interpreter_if_exists(); | 
					
						
							| 
									
										
										
										
											2020-09-27 18:45:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!interpreter) { | 
					
						
							| 
									
										
										
										
											2021-09-12 12:33:54 +01:00
										 |  |  |         local_interpreter = Interpreter::create_with_existing_realm(*realm()); | 
					
						
							| 
									
										
										
										
											2020-09-27 18:45:21 +02:00
										 |  |  |         interpreter = local_interpreter.ptr(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     VM::InterpreterExecutionScope scope(*interpreter); | 
					
						
							| 
									
										
										
										
											2021-10-20 21:16:30 +01:00
										 |  |  |     auto result = function->execute(*interpreter, global_object()); | 
					
						
							|  |  |  |     if (auto* exception = vm.exception()) | 
					
						
							|  |  |  |         return throw_completion(exception->value()); | 
					
						
							|  |  |  |     VERIFY(result.is_object() && is<ECMAScriptFunctionObject>(result.as_object())); | 
					
						
							|  |  |  |     return &result.as_object(); | 
					
						
							| 
									
										
										
										
											2020-04-04 14:34:31 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |