1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 05:48:12 +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

@ -14,6 +14,7 @@
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
#include <LibJS/Runtime/FunctionEnvironmentRecord.h>
#include <LibJS/Runtime/GeneratorObject.h>
#include <LibJS/Runtime/GeneratorObjectPrototype.h>
#include <LibJS/Runtime/GlobalObject.h>
@ -93,7 +94,7 @@ void ScriptFunction::visit_edges(Visitor& visitor)
visitor.visit(m_parent_scope);
}
DeclarativeEnvironmentRecord* ScriptFunction::create_environment_record()
FunctionEnvironmentRecord* ScriptFunction::create_environment_record()
{
HashMap<FlyString, Variable> variables;
for (auto& parameter : m_parameters) {
@ -122,11 +123,11 @@ DeclarativeEnvironmentRecord* ScriptFunction::create_environment_record()
}
}
auto* environment = heap().allocate<DeclarativeEnvironmentRecord>(global_object(), move(variables), m_parent_scope, DeclarativeEnvironmentRecord::EnvironmentRecordType::Function);
environment->set_current_function(*this);
auto* environment = heap().allocate<FunctionEnvironmentRecord>(global_object(), m_parent_scope, variables);
environment->set_function_object(*this);
if (m_is_arrow_function) {
if (is<DeclarativeEnvironmentRecord>(m_parent_scope))
environment->set_new_target(static_cast<DeclarativeEnvironmentRecord*>(m_parent_scope)->new_target());
if (is<FunctionEnvironmentRecord>(m_parent_scope))
environment->set_new_target(static_cast<FunctionEnvironmentRecord*>(m_parent_scope)->new_target());
}
return environment;
}