1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:17:45 +00:00

LibJS: Bring function environment records closer to the spec

This patch adds FunctionEnvironmentRecord as a subclass of the existing
DeclarativeEnvironmentRecord. Things that are specific to function
environment records move into there, simplifying the base.

Most of the abstract operations related to function environment records
are rewritten to match the spec exactly. I also had to implement
GetThisEnvironment() and GetSuperConstructor() to keep tests working
after the changes, so that's nice as well. :^)
This commit is contained in:
Andreas Kling 2021-06-22 13:30:48 +02:00
parent 6ed6434bab
commit aabd82d508
28 changed files with 228 additions and 159 deletions

View file

@ -19,6 +19,7 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/BigInt.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionEnvironmentRecord.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibJS/Runtime/MarkedValueList.h>
@ -132,7 +133,7 @@ CallExpression::ThisAndCallee CallExpression::compute_this_and_callee(Interprete
Object* this_value = nullptr;
if (is<SuperExpression>(member_expression.object())) {
auto super_base = interpreter.current_declarative_environment_record()->get_super_base();
auto super_base = interpreter.current_function_environment_record()->get_super_base();
if (super_base.is_nullish()) {
vm.throw_exception<TypeError>(global_object, ErrorType::ObjectPrototypeNullOrUndefinedOnSuperPropertyAccess, super_base.to_string_without_side_effects());
return {};
@ -227,14 +228,7 @@ Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_obj
if (result.is_object())
new_object = &result.as_object();
} else if (is<SuperExpression>(*m_callee)) {
// FIXME: This is merely a band-aid to make super() inside catch {} work (which constructs
// a new DeclarativeEnvironmentRecord without current function). Implement GetSuperConstructor()
// and subsequently GetThisEnvironment() instead.
auto* function_environment = interpreter.current_declarative_environment_record();
if (!function_environment->current_function())
function_environment = static_cast<DeclarativeEnvironmentRecord*>(function_environment->outer_environment());
auto* super_constructor = function_environment->current_function()->prototype();
auto* super_constructor = get_super_constructor(interpreter.vm());
// FIXME: Functions should track their constructor kind.
if (!super_constructor || !super_constructor->is_function()) {
vm.throw_exception<TypeError>(global_object, ErrorType::NotAConstructor, "Super constructor");
@ -244,7 +238,9 @@ Value CallExpression::execute(Interpreter& interpreter, GlobalObject& global_obj
if (vm.exception())
return {};
function_environment->bind_this_value(global_object, result);
auto& this_er = get_this_environment(interpreter.vm());
VERIFY(is<FunctionEnvironmentRecord>(this_er));
static_cast<FunctionEnvironmentRecord&>(this_er).bind_this_value(global_object, result);
} else {
result = vm.call(function, this_value, move(arguments));
}
@ -1355,7 +1351,7 @@ Value SpreadExpression::execute(Interpreter& interpreter, GlobalObject& global_o
Value ThisExpression::execute(Interpreter& interpreter, GlobalObject& global_object) const
{
InterpreterNodeScope node_scope { interpreter, *this };
return interpreter.vm().resolve_this_binding(global_object);
return get_this_environment(interpreter.vm()).get_this_binding(global_object);
}
void ThisExpression::dump(int indent) const