2020-03-13 10:08:52 +01:00
|
|
|
/*
|
2021-05-29 12:38:28 +02:00
|
|
|
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2020-03-13 10:08:52 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-13 10:08:52 +01:00
|
|
|
*/
|
|
|
|
|
2021-06-07 15:17:37 +02:00
|
|
|
#include <AK/Debug.h>
|
2020-04-04 14:34:27 +01:00
|
|
|
#include <AK/Function.h>
|
2020-03-18 11:23:53 +01:00
|
|
|
#include <LibJS/AST.h>
|
2021-06-09 06:49:58 +04:30
|
|
|
#include <LibJS/Bytecode/BasicBlock.h>
|
2021-06-05 15:53:36 +02:00
|
|
|
#include <LibJS/Bytecode/Generator.h>
|
|
|
|
#include <LibJS/Bytecode/Interpreter.h>
|
2020-03-13 10:08:52 +01:00
|
|
|
#include <LibJS/Interpreter.h>
|
2020-05-04 16:05:13 +01:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2021-09-24 22:40:38 +02:00
|
|
|
#include <LibJS/Runtime/ECMAScriptFunctionObject.h>
|
2020-04-04 14:34:27 +01:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2021-07-01 12:24:46 +02:00
|
|
|
#include <LibJS/Runtime/FunctionEnvironment.h>
|
2021-06-11 01:38:30 +04:30
|
|
|
#include <LibJS/Runtime/GeneratorObject.h>
|
2021-06-15 00:04:08 -07:00
|
|
|
#include <LibJS/Runtime/GeneratorObjectPrototype.h>
|
2020-04-17 19:59:32 +02:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2021-06-11 01:38:30 +04:30
|
|
|
#include <LibJS/Runtime/NativeFunction.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/Value.h>
|
2020-03-13 10:08:52 +01:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-09-24 23:10:21 +02:00
|
|
|
ECMAScriptFunctionObject* ECMAScriptFunctionObject::create(GlobalObject& global_object, FlyString name, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, Environment* parent_scope, FunctionKind kind, bool is_strict, bool is_arrow_function)
|
2020-04-17 19:59:32 +02:00
|
|
|
{
|
2021-06-15 00:04:08 -07:00
|
|
|
Object* prototype = nullptr;
|
|
|
|
switch (kind) {
|
|
|
|
case FunctionKind::Regular:
|
|
|
|
prototype = global_object.function_prototype();
|
|
|
|
break;
|
|
|
|
case FunctionKind::Generator:
|
|
|
|
prototype = global_object.generator_function_prototype();
|
|
|
|
break;
|
|
|
|
}
|
2021-09-24 23:10:21 +02:00
|
|
|
return global_object.heap().allocate<ECMAScriptFunctionObject>(global_object, global_object, move(name), ecmascript_code, move(parameters), m_function_length, parent_scope, *prototype, kind, is_strict, is_arrow_function);
|
2020-04-17 19:59:32 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:10:21 +02:00
|
|
|
ECMAScriptFunctionObject::ECMAScriptFunctionObject(GlobalObject& global_object, FlyString name, Statement const& ecmascript_code, Vector<FunctionNode::Parameter> formal_parameters, i32 function_length, Environment* parent_scope, Object& prototype, FunctionKind kind, bool strict, bool is_arrow_function)
|
2021-06-27 21:48:34 +02:00
|
|
|
: FunctionObject(is_arrow_function ? vm().this_value(global_object) : Value(), {}, prototype)
|
2021-06-25 21:22:37 +02:00
|
|
|
, m_environment(parent_scope)
|
2021-09-24 23:10:21 +02:00
|
|
|
, m_formal_parameters(move(formal_parameters))
|
|
|
|
, m_ecmascript_code(ecmascript_code)
|
2021-09-12 23:10:16 +01:00
|
|
|
, m_realm(vm().interpreter_if_exists() ? &vm().interpreter().realm() : nullptr)
|
2021-09-24 23:10:21 +02:00
|
|
|
, m_strict(strict)
|
|
|
|
, m_name(move(name))
|
2021-06-24 13:29:25 +02:00
|
|
|
, m_function_length(function_length)
|
2021-06-11 03:38:05 +04:30
|
|
|
, m_kind(kind)
|
2020-05-30 00:10:42 -05:00
|
|
|
, m_is_arrow_function(is_arrow_function)
|
2020-03-13 10:08:52 +01:00
|
|
|
{
|
2021-06-25 20:53:17 +02:00
|
|
|
// NOTE: This logic is from OrdinaryFunctionCreate, https://tc39.es/ecma262/#sec-ordinaryfunctioncreate
|
|
|
|
if (m_is_arrow_function)
|
2021-09-24 23:19:36 +02:00
|
|
|
m_this_mode = ThisMode::Lexical;
|
2021-09-24 23:10:21 +02:00
|
|
|
else if (m_strict)
|
2021-09-24 23:19:36 +02:00
|
|
|
m_this_mode = ThisMode::Strict;
|
2021-06-25 20:53:17 +02:00
|
|
|
else
|
2021-09-24 23:19:36 +02:00
|
|
|
m_this_mode = ThisMode::Global;
|
2021-06-28 11:18:32 +02:00
|
|
|
|
|
|
|
// 15.1.3 Static Semantics: IsSimpleParameterList, https://tc39.es/ecma262/#sec-static-semantics-issimpleparameterlist
|
2021-09-24 23:10:21 +02:00
|
|
|
set_has_simple_parameter_list(all_of(m_formal_parameters, [&](auto& parameter) {
|
2021-06-28 11:18:32 +02:00
|
|
|
if (parameter.is_rest)
|
|
|
|
return false;
|
|
|
|
if (parameter.default_value)
|
|
|
|
return false;
|
|
|
|
if (!parameter.binding.template has<FlyString>())
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}));
|
2020-06-20 17:11:11 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
void ECMAScriptFunctionObject::initialize(GlobalObject& global_object)
|
2020-06-20 17:11:11 +02:00
|
|
|
{
|
2020-10-13 23:49:19 +02:00
|
|
|
auto& vm = this->vm();
|
2021-06-27 21:48:34 +02:00
|
|
|
Base::initialize(global_object);
|
2020-06-08 13:31:21 -05:00
|
|
|
if (!m_is_arrow_function) {
|
2021-06-27 22:15:58 +02:00
|
|
|
auto* prototype = vm.heap().allocate<Object>(global_object, *global_object.new_ordinary_function_prototype_object_shape());
|
2021-06-15 00:04:08 -07:00
|
|
|
switch (m_kind) {
|
|
|
|
case FunctionKind::Regular:
|
2021-07-06 02:15:08 +03:00
|
|
|
prototype->define_property_or_throw(vm.names.constructor, { .value = this, .writable = true, .enumerable = false, .configurable = true });
|
2021-06-15 00:04:08 -07:00
|
|
|
break;
|
|
|
|
case FunctionKind::Generator:
|
|
|
|
// prototype is "g1.prototype" in figure-2 (https://tc39.es/ecma262/img/figure-2.png)
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
prototype->internal_set_prototype_of(global_object.generator_object_prototype());
|
2021-06-15 00:04:08 -07:00
|
|
|
break;
|
|
|
|
}
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.prototype, prototype, Attribute::Writable);
|
2020-06-08 13:24:15 -05:00
|
|
|
}
|
2021-07-05 18:02:27 +03:00
|
|
|
define_property_or_throw(vm.names.length, { .value = Value(m_function_length), .writable = false, .enumerable = false, .configurable = true });
|
|
|
|
define_property_or_throw(vm.names.name, { .value = js_string(vm, m_name.is_null() ? "" : m_name), .writable = false, .enumerable = false, .configurable = true });
|
2020-03-13 10:08:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
ECMAScriptFunctionObject::~ECMAScriptFunctionObject()
|
2020-03-13 10:08:52 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
void ECMAScriptFunctionObject::visit_edges(Visitor& visitor)
|
2020-04-15 21:58:22 +02:00
|
|
|
{
|
2021-06-27 21:48:34 +02:00
|
|
|
Base::visit_edges(visitor);
|
2021-06-25 21:22:37 +02:00
|
|
|
visitor.visit(m_environment);
|
2021-09-11 19:19:08 +03:00
|
|
|
visitor.visit(m_realm);
|
2021-09-24 23:49:24 +02:00
|
|
|
visitor.visit(m_home_object);
|
2020-04-15 21:58:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
FunctionEnvironment* ECMAScriptFunctionObject::create_environment(FunctionObject& function_being_invoked)
|
2020-04-15 21:58:22 +02:00
|
|
|
{
|
|
|
|
HashMap<FlyString, Variable> variables;
|
2021-09-24 23:10:21 +02:00
|
|
|
for (auto& parameter : m_formal_parameters) {
|
2021-05-29 16:03:19 +04:30
|
|
|
parameter.binding.visit(
|
|
|
|
[&](const FlyString& name) { variables.set(name, { js_undefined(), DeclarationKind::Var }); },
|
|
|
|
[&](const NonnullRefPtr<BindingPattern>& binding) {
|
2021-06-12 18:04:28 -07:00
|
|
|
binding->for_each_bound_name([&](const auto& name) {
|
2021-05-29 16:03:19 +04:30
|
|
|
variables.set(name, { js_undefined(), DeclarationKind::Var });
|
|
|
|
});
|
|
|
|
});
|
2020-04-15 21:58:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 23:10:21 +02:00
|
|
|
if (is<ScopeNode>(ecmascript_code())) {
|
|
|
|
for (auto& declaration : static_cast<const ScopeNode&>(ecmascript_code()).variables()) {
|
2020-04-15 21:58:22 +02:00
|
|
|
for (auto& declarator : declaration.declarations()) {
|
2021-05-29 16:03:19 +04:30
|
|
|
declarator.target().visit(
|
|
|
|
[&](const NonnullRefPtr<Identifier>& id) {
|
|
|
|
variables.set(id->string(), { js_undefined(), declaration.declaration_kind() });
|
|
|
|
},
|
|
|
|
[&](const NonnullRefPtr<BindingPattern>& binding) {
|
2021-06-12 18:04:28 -07:00
|
|
|
binding->for_each_bound_name([&](const auto& name) {
|
2021-05-29 16:03:19 +04:30
|
|
|
variables.set(name, { js_undefined(), declaration.declaration_kind() });
|
|
|
|
});
|
|
|
|
});
|
2020-04-15 21:58:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-08 13:31:21 -05:00
|
|
|
|
2021-07-01 12:24:46 +02:00
|
|
|
auto* environment = heap().allocate<FunctionEnvironment>(global_object(), m_environment, variables);
|
2021-09-24 23:49:24 +02:00
|
|
|
environment->set_function_object(static_cast<ECMAScriptFunctionObject&>(function_being_invoked));
|
2020-11-28 16:02:27 +01:00
|
|
|
if (m_is_arrow_function) {
|
2021-08-09 15:21:15 +02:00
|
|
|
environment->set_this_binding_status(FunctionEnvironment::ThisBindingStatus::Lexical);
|
2021-07-01 12:24:46 +02:00
|
|
|
if (is<FunctionEnvironment>(m_environment))
|
|
|
|
environment->set_new_target(static_cast<FunctionEnvironment*>(m_environment)->new_target());
|
2020-11-28 16:02:27 +01:00
|
|
|
}
|
2020-06-08 13:31:21 -05:00
|
|
|
return environment;
|
2020-04-15 21:58:22 +02:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
Value ECMAScriptFunctionObject::execute_function_body()
|
2020-03-13 10:08:52 +01:00
|
|
|
{
|
2020-11-11 21:37:23 +00:00
|
|
|
auto& vm = this->vm();
|
|
|
|
|
2021-06-05 15:53:36 +02:00
|
|
|
Interpreter* ast_interpreter = nullptr;
|
|
|
|
auto* bytecode_interpreter = Bytecode::Interpreter::current();
|
|
|
|
|
|
|
|
auto prepare_arguments = [&] {
|
2021-06-24 19:17:45 +02:00
|
|
|
auto& execution_context_arguments = vm.running_execution_context().arguments;
|
2021-09-24 23:10:21 +02:00
|
|
|
for (size_t i = 0; i < m_formal_parameters.size(); ++i) {
|
|
|
|
auto& parameter = m_formal_parameters[i];
|
2021-06-05 15:53:36 +02:00
|
|
|
parameter.binding.visit(
|
|
|
|
[&](const auto& param) {
|
|
|
|
Value argument_value;
|
|
|
|
if (parameter.is_rest) {
|
2021-07-04 01:36:44 +03:00
|
|
|
auto* array = Array::create(global_object(), 0);
|
2021-06-24 19:17:45 +02:00
|
|
|
for (size_t rest_index = i; rest_index < execution_context_arguments.size(); ++rest_index)
|
|
|
|
array->indexed_properties().append(execution_context_arguments[rest_index]);
|
2021-06-05 15:53:36 +02:00
|
|
|
argument_value = move(array);
|
2021-06-24 19:17:45 +02:00
|
|
|
} else if (i < execution_context_arguments.size() && !execution_context_arguments[i].is_undefined()) {
|
|
|
|
argument_value = execution_context_arguments[i];
|
2021-06-05 15:53:36 +02:00
|
|
|
} else if (parameter.default_value) {
|
|
|
|
// FIXME: Support default arguments in the bytecode world!
|
|
|
|
if (!bytecode_interpreter)
|
|
|
|
argument_value = parameter.default_value->execute(*ast_interpreter, global_object());
|
|
|
|
if (vm.exception())
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
argument_value = js_undefined();
|
|
|
|
}
|
|
|
|
|
2021-06-22 15:42:44 +02:00
|
|
|
vm.assign(param, argument_value, global_object(), true, vm.lexical_environment());
|
2021-06-05 15:53:36 +02:00
|
|
|
});
|
2020-09-27 17:24:14 +02:00
|
|
|
|
2021-06-05 15:53:36 +02:00
|
|
|
if (vm.exception())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (bytecode_interpreter) {
|
|
|
|
prepare_arguments();
|
2021-06-09 09:11:20 +02:00
|
|
|
if (!m_bytecode_executable.has_value()) {
|
2021-09-24 23:10:21 +02:00
|
|
|
m_bytecode_executable = Bytecode::Generator::generate(m_ecmascript_code, m_kind == FunctionKind::Generator);
|
2021-06-13 20:40:20 +04:30
|
|
|
auto& passes = JS::Bytecode::Interpreter::optimization_pipeline();
|
|
|
|
passes.perform(*m_bytecode_executable);
|
2021-06-07 15:19:48 +02:00
|
|
|
if constexpr (JS_BYTECODE_DEBUG) {
|
2021-06-13 20:40:20 +04:30
|
|
|
dbgln("Optimisation passes took {}us", passes.elapsed());
|
2021-06-07 15:19:48 +02:00
|
|
|
dbgln("Compiled Bytecode::Block for function '{}':", m_name);
|
2021-06-09 09:11:20 +02:00
|
|
|
for (auto& block : m_bytecode_executable->basic_blocks)
|
2021-06-09 10:02:01 +02:00
|
|
|
block.dump(*m_bytecode_executable);
|
2021-06-07 15:19:48 +02:00
|
|
|
}
|
2021-06-07 15:17:37 +02:00
|
|
|
}
|
2021-06-11 01:38:30 +04:30
|
|
|
auto result = bytecode_interpreter->run(*m_bytecode_executable);
|
2021-06-11 03:38:05 +04:30
|
|
|
if (m_kind != FunctionKind::Generator)
|
2021-06-11 01:38:30 +04:30
|
|
|
return result;
|
|
|
|
|
2021-06-24 19:17:45 +02:00
|
|
|
return GeneratorObject::create(global_object(), result, this, vm.running_execution_context().lexical_environment, bytecode_interpreter->snapshot_frame());
|
2021-06-05 15:53:36 +02:00
|
|
|
} else {
|
2021-06-11 03:38:05 +04:30
|
|
|
VERIFY(m_kind != FunctionKind::Generator);
|
2021-06-05 15:53:36 +02:00
|
|
|
OwnPtr<Interpreter> local_interpreter;
|
|
|
|
ast_interpreter = vm.interpreter_if_exists();
|
|
|
|
|
|
|
|
if (!ast_interpreter) {
|
2021-09-12 12:33:54 +01:00
|
|
|
local_interpreter = Interpreter::create_with_existing_realm(*realm());
|
2021-06-05 15:53:36 +02:00
|
|
|
ast_interpreter = local_interpreter.ptr();
|
|
|
|
}
|
2020-09-27 17:24:14 +02:00
|
|
|
|
2021-06-05 15:53:36 +02:00
|
|
|
VM::InterpreterExecutionScope scope(*ast_interpreter);
|
2021-05-29 16:03:19 +04:30
|
|
|
|
2021-06-05 15:53:36 +02:00
|
|
|
prepare_arguments();
|
2021-05-29 16:03:19 +04:30
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
2020-10-04 13:54:44 +02:00
|
|
|
|
2021-09-24 23:10:21 +02:00
|
|
|
return ast_interpreter->execute_statement(global_object(), m_ecmascript_code, ScopeType::Function);
|
2021-06-05 15:53:36 +02:00
|
|
|
}
|
2020-03-13 10:08:52 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
Value ECMAScriptFunctionObject::call()
|
2020-11-11 21:37:40 +00:00
|
|
|
{
|
|
|
|
if (m_is_class_constructor) {
|
|
|
|
vm().throw_exception<TypeError>(global_object(), ErrorType::ClassConstructorWithoutNew, m_name);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
return execute_function_body();
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
Value ECMAScriptFunctionObject::construct(FunctionObject&)
|
2020-04-01 18:31:24 +01:00
|
|
|
{
|
2021-06-15 00:04:08 -07:00
|
|
|
if (m_is_arrow_function || m_kind == FunctionKind::Generator) {
|
2020-10-04 13:55:20 +01:00
|
|
|
vm().throw_exception<TypeError>(global_object(), ErrorType::NotAConstructor, m_name);
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
}
|
2020-11-11 21:37:40 +00:00
|
|
|
return execute_function_body();
|
2020-04-01 18:31:24 +01:00
|
|
|
}
|
|
|
|
|
2021-09-24 22:40:38 +02:00
|
|
|
void ECMAScriptFunctionObject::set_name(const FlyString& name)
|
2020-04-04 14:34:27 +01:00
|
|
|
{
|
2021-07-05 18:02:27 +03:00
|
|
|
VERIFY(!name.is_null());
|
|
|
|
auto& vm = this->vm();
|
|
|
|
m_name = name;
|
|
|
|
auto success = define_property_or_throw(vm.names.name, { .value = js_string(vm, m_name), .writable = false, .enumerable = false, .configurable = true });
|
|
|
|
VERIFY(success);
|
2020-04-04 14:34:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-13 10:08:52 +01:00
|
|
|
}
|