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

@ -68,7 +68,7 @@
namespace JS {
GlobalObject::GlobalObject()
: Object(GlobalObjectTag::Tag)
: ScopeObject(GlobalObjectTag::Tag)
, m_console(make<Console>(*this))
{
}
@ -149,7 +149,7 @@ GlobalObject::~GlobalObject()
void GlobalObject::visit_edges(Visitor& visitor)
{
Object::visit_edges(visitor);
Base::visit_edges(visitor);
visitor.visit(m_empty_object_shape);
visitor.visit(m_new_object_shape);
@ -205,4 +205,27 @@ JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_float)
return js_nan();
}
Optional<Variable> GlobalObject::get_from_scope(const FlyString& name) const
{
auto value = get(name);
if (value.is_empty())
return {};
return Variable { value, DeclarationKind::Var };
}
void GlobalObject::put_to_scope(const FlyString& name, Variable variable)
{
put(name, variable.value);
}
bool GlobalObject::has_this_binding() const
{
return true;
}
Value GlobalObject::get_this_binding(GlobalObject&) const
{
return Value(this);
}
}