mirror of
https://github.com/RGBCube/serenity
synced 2025-07-23 19:07:34 +00:00
LibJS: Allow Function objects to be constructed with a bound |this|
value and bound arguments This allows Function objects produced by Function.prototype.bind, as well as arrow functions to track their |this| values and bound arguments.
This commit is contained in:
parent
6a66207efa
commit
5750edd859
2 changed files with 36 additions and 1 deletions
|
@ -30,10 +30,30 @@
|
||||||
namespace JS {
|
namespace JS {
|
||||||
|
|
||||||
Function::Function(Object& prototype)
|
Function::Function(Object& prototype)
|
||||||
: Object(&prototype)
|
: Function(prototype, {}, {})
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function::Function(Object& prototype, Optional<Value> bound_this, Vector<Value> bound_arguments)
|
||||||
|
: Object(&prototype)
|
||||||
|
, m_bound_this(bound_this)
|
||||||
|
, m_bound_arguments(move(bound_arguments))
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void Function::visit_children(Visitor& visitor)
|
||||||
|
{
|
||||||
|
Object::visit_children(visitor);
|
||||||
|
|
||||||
|
if (m_bound_this.has_value()) {
|
||||||
|
visitor.visit(m_bound_this.value());
|
||||||
|
}
|
||||||
|
|
||||||
|
for (auto argument : m_bound_arguments) {
|
||||||
|
visitor.visit(argument);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Function::~Function()
|
Function::~Function()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,12 +40,27 @@ public:
|
||||||
virtual const FlyString& name() const = 0;
|
virtual const FlyString& name() const = 0;
|
||||||
virtual LexicalEnvironment* create_environment() = 0;
|
virtual LexicalEnvironment* create_environment() = 0;
|
||||||
|
|
||||||
|
virtual void visit_children(Visitor&) override;
|
||||||
|
|
||||||
|
Optional<Value> bound_this() const
|
||||||
|
{
|
||||||
|
return m_bound_this;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Vector<Value>& bound_arguments() const
|
||||||
|
{
|
||||||
|
return m_bound_arguments;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit Function(Object& prototype);
|
explicit Function(Object& prototype);
|
||||||
|
explicit Function(Object& prototype, Optional<Value> bound_this, Vector<Value> bound_arguments);
|
||||||
virtual const char* class_name() const override { return "Function"; }
|
virtual const char* class_name() const override { return "Function"; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool is_function() const final { return true; }
|
virtual bool is_function() const final { return true; }
|
||||||
|
Optional<Value> m_bound_this;
|
||||||
|
Vector<Value> m_bound_arguments;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue