mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 23:47:45 +00:00
LibJS: Implement constructor/non-constructor function calls
This adds Function::construct() for constructor function calls via `new` keyword. NativeFunction doesn't have constructor behaviour by default, ScriptFunction simply calls call() in construct()
This commit is contained in:
parent
a27884e4be
commit
849e2c77e4
11 changed files with 66 additions and 1 deletions
|
@ -46,6 +46,14 @@ DateConstructor::~DateConstructor()
|
|||
}
|
||||
|
||||
Value DateConstructor::call(Interpreter& interpreter)
|
||||
{
|
||||
auto* date = static_cast<Date*>(construct(interpreter).as_object());
|
||||
if (!date)
|
||||
return {};
|
||||
return js_string(interpreter.heap(), date->string());
|
||||
}
|
||||
|
||||
Value DateConstructor::construct(Interpreter& interpreter)
|
||||
{
|
||||
// TODO: Support args
|
||||
struct timeval tv;
|
||||
|
|
|
@ -36,8 +36,10 @@ public:
|
|||
virtual ~DateConstructor() override;
|
||||
|
||||
virtual Value call(Interpreter&) override;
|
||||
virtual Value construct(Interpreter&) override;
|
||||
|
||||
private:
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
virtual const char* class_name() const override { return "DateConstructor"; }
|
||||
|
||||
static Value now(Interpreter&);
|
||||
|
|
|
@ -36,6 +36,7 @@ public:
|
|||
virtual ~Function();
|
||||
|
||||
virtual Value call(Interpreter&) = 0;
|
||||
virtual Value construct(Interpreter&) = 0;
|
||||
|
||||
protected:
|
||||
Function();
|
||||
|
|
|
@ -44,4 +44,9 @@ Value NativeFunction::call(Interpreter& interpreter)
|
|||
return m_native_function(interpreter);
|
||||
}
|
||||
|
||||
Value NativeFunction::construct(Interpreter&)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -37,6 +37,9 @@ public:
|
|||
virtual ~NativeFunction() override;
|
||||
|
||||
virtual Value call(Interpreter&) override;
|
||||
virtual Value construct(Interpreter&) override;
|
||||
|
||||
virtual bool has_constructor() const { return false; }
|
||||
|
||||
protected:
|
||||
NativeFunction() {}
|
||||
|
|
|
@ -50,6 +50,11 @@ Value ObjectConstructor::call(Interpreter& interpreter)
|
|||
return interpreter.heap().allocate<Object>();
|
||||
}
|
||||
|
||||
Value ObjectConstructor::construct(Interpreter& interpreter)
|
||||
{
|
||||
return call(interpreter);
|
||||
}
|
||||
|
||||
Value ObjectConstructor::get_own_property_names(Interpreter& interpreter)
|
||||
{
|
||||
if (interpreter.call_frame().arguments.size() < 1)
|
||||
|
|
|
@ -36,8 +36,10 @@ public:
|
|||
virtual ~ObjectConstructor() override;
|
||||
|
||||
virtual Value call(Interpreter&) override;
|
||||
virtual Value construct(Interpreter&) override;
|
||||
|
||||
private:
|
||||
virtual bool has_constructor() const override { return true; }
|
||||
virtual const char* class_name() const override { return "ObjectConstructor"; }
|
||||
|
||||
static Value get_own_property_names(Interpreter&);
|
||||
|
|
|
@ -55,4 +55,9 @@ Value ScriptFunction::call(Interpreter& interpreter)
|
|||
return interpreter.run(m_body, arguments, ScopeType::Function);
|
||||
}
|
||||
|
||||
Value ScriptFunction::construct(Interpreter& interpreter)
|
||||
{
|
||||
return call(interpreter);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -39,6 +39,7 @@ public:
|
|||
const Vector<FlyString>& parameters() const { return m_parameters; };
|
||||
|
||||
virtual Value call(Interpreter&) override;
|
||||
virtual Value construct(Interpreter&) override;
|
||||
|
||||
private:
|
||||
virtual bool is_script_function() const final { return true; }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue