1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 02:27:35 +00:00

LibJS: Add a scope object abstraction

Both GlobalObject and LexicalEnvironment now inherit from ScopeObject,
and the VM's call frames point to a ScopeObject chain rather than just
a LexicalEnvironment chain.

This gives us much more flexibility to implement things like "with",
and also unifies some of the code paths that previously required
special handling of the global object.

There's a bunch of more cleanup that can be done in the wake of this
change, and there might be some oversights in the handling of the
"super" keyword, but this generally seems like a good architectural
improvement. :^)
This commit is contained in:
Andreas Kling 2020-11-28 16:02:27 +01:00
parent e1bbc7c075
commit c3fe9b4df8
16 changed files with 241 additions and 92 deletions

View file

@ -34,23 +34,25 @@
namespace JS {
LexicalEnvironment::LexicalEnvironment()
: ScopeObject(nullptr)
{
}
LexicalEnvironment::LexicalEnvironment(EnvironmentRecordType environment_record_type)
: m_environment_record_type(environment_record_type)
: ScopeObject(nullptr)
, m_environment_record_type(environment_record_type)
{
}
LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent)
: m_parent(parent)
LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, ScopeObject* parent_scope)
: ScopeObject(parent_scope)
, m_variables(move(variables))
{
}
LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, LexicalEnvironment* parent, EnvironmentRecordType environment_record_type)
: m_environment_record_type(environment_record_type)
, m_parent(parent)
LexicalEnvironment::LexicalEnvironment(HashMap<FlyString, Variable> variables, ScopeObject* parent_scope, EnvironmentRecordType environment_record_type)
: ScopeObject(parent_scope)
, m_environment_record_type(environment_record_type)
, m_variables(move(variables))
{
}
@ -62,7 +64,6 @@ LexicalEnvironment::~LexicalEnvironment()
void LexicalEnvironment::visit_edges(Visitor& visitor)
{
Cell::visit_edges(visitor);
visitor.visit(m_parent);
visitor.visit(m_this_value);
visitor.visit(m_home_object);
visitor.visit(m_new_target);
@ -71,18 +72,14 @@ void LexicalEnvironment::visit_edges(Visitor& visitor)
visitor.visit(it.value.value);
}
Optional<Variable> LexicalEnvironment::get(const FlyString& name) const
Optional<Variable> LexicalEnvironment::get_from_scope(const FlyString& name) const
{
ASSERT(type() != EnvironmentRecordType::Global);
return m_variables.get(name);
}
void LexicalEnvironment::set(GlobalObject& global_object, const FlyString& name, Variable variable)
void LexicalEnvironment::put_to_scope(const FlyString& name, Variable variable)
{
if (type() == EnvironmentRecordType::Global)
global_object.put(name, variable.value);
else
m_variables.set(name, variable);
m_variables.set(name, variable);
}
bool LexicalEnvironment::has_super_binding() const
@ -108,7 +105,6 @@ bool LexicalEnvironment::has_this_binding() const
case EnvironmentRecordType::Function:
return this_binding_status() != ThisBindingStatus::Lexical;
case EnvironmentRecordType::Module:
case EnvironmentRecordType::Global:
return true;
}
ASSERT_NOT_REACHED();