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>
|
|
|
|
#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)
|
2020-10-13 23:49:19 +02:00
|
|
|
: NativeFunction(vm().names.Function, *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
|
2020-10-13 23:49:19 +02:00
|
|
|
define_property(vm.names.prototype, global_object.function_prototype(), 0);
|
2021-06-19 00:38:41 +01:00
|
|
|
|
2020-10-13 23:49:19 +02:00
|
|
|
define_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-06-27 21:48:34 +02:00
|
|
|
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 = "";
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.argument_count() == 1) {
|
2021-06-16 10:52:48 -07:00
|
|
|
body_source = vm.argument(0).to_string(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.argument_count() > 1) {
|
2020-04-04 14:34:31 +01:00
|
|
|
Vector<String> parameters;
|
2020-09-27 18:36:49 +02:00
|
|
|
for (size_t i = 0; i < vm.argument_count() - 1; ++i) {
|
2021-06-16 10:52:48 -07:00
|
|
|
parameters.append(vm.argument(i).to_string(global_object));
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
|
|
|
}
|
2020-04-04 14:34:31 +01:00
|
|
|
StringBuilder parameters_builder;
|
|
|
|
parameters_builder.join(',', parameters);
|
|
|
|
parameters_source = parameters_builder.build();
|
2021-06-16 10:52:48 -07:00
|
|
|
body_source = vm.argument(vm.argument_count() - 1).to_string(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
2020-04-04 14:34:31 +01:00
|
|
|
}
|
2021-06-16 10:52:48 -07:00
|
|
|
auto is_generator = kind == FunctionKind::Generator;
|
|
|
|
auto source = String::formatted("function{} anonymous({}\n) {{\n{}\n}}", 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-06-16 10:52:48 -07:00
|
|
|
vm.throw_exception<SyntaxError>(global_object, error.to_string());
|
2020-05-13 01:09:49 +01:00
|
|
|
return {};
|
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
|
|
|
|
Value FunctionConstructor::call()
|
|
|
|
{
|
|
|
|
return construct(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 20.2.1.1 Function ( p1, p2, … , pn, body ), https://tc39.es/ecma262/#sec-function-p1-p2-pn-body
|
2021-06-27 21:48:34 +02:00
|
|
|
Value FunctionConstructor::construct(FunctionObject& new_target)
|
2021-06-16 10:52:48 -07:00
|
|
|
{
|
|
|
|
auto function = create_dynamic_function_node(global_object(), new_target, FunctionKind::Regular);
|
|
|
|
if (!function)
|
|
|
|
return {};
|
|
|
|
|
2020-09-27 18:45:21 +02:00
|
|
|
OwnPtr<Interpreter> local_interpreter;
|
2021-06-16 10:52:48 -07:00
|
|
|
Interpreter* interpreter = vm().interpreter_if_exists();
|
2020-09-27 18:45:21 +02:00
|
|
|
|
|
|
|
if (!interpreter) {
|
|
|
|
local_interpreter = Interpreter::create_with_existing_global_object(global_object());
|
|
|
|
interpreter = local_interpreter.ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
VM::InterpreterExecutionScope scope(*interpreter);
|
2021-06-16 10:52:48 -07:00
|
|
|
return function->execute(*interpreter, global_object());
|
2020-04-04 14:34:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|