mirror of
https://github.com/RGBCube/serenity
synced 2025-07-03 05:32:13 +00:00
LibJS: Implement basic support for the "new" keyword
NewExpression mostly piggybacks on the existing CallExpression. The big difference is that "new" creates a new Object and passes it as |this| to the callee.
This commit is contained in:
parent
fecbef4ffe
commit
0593ce406b
5 changed files with 67 additions and 9 deletions
|
@ -80,18 +80,30 @@ Value CallExpression::execute(Interpreter& interpreter) const
|
|||
return {};
|
||||
}
|
||||
|
||||
if (m_callee->is_member_expression()) {
|
||||
auto object_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
auto this_value = object_value.to_object(interpreter.heap());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
call_frame.this_value = this_value;
|
||||
Object* new_object = nullptr;
|
||||
if (is_new_expression()) {
|
||||
new_object = interpreter.heap().allocate<Object>();
|
||||
call_frame.this_value = new_object;
|
||||
} else {
|
||||
if (m_callee->is_member_expression()) {
|
||||
auto object_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter);
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
auto this_value = object_value.to_object(interpreter.heap());
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
call_frame.this_value = this_value;
|
||||
}
|
||||
}
|
||||
|
||||
auto result = function->call(interpreter, call_frame.arguments);
|
||||
interpreter.pop_call_frame();
|
||||
|
||||
if (is_new_expression()) {
|
||||
if (result.is_object())
|
||||
return result;
|
||||
return new_object;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue