mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:48:11 +00:00
LibJS: Support basic function calls in the bytecode world :^)
This patch adds the Call bytecode instruction which is emitted for the CallExpression AST node. It's pretty barebones and doesn't handle 'this' values properly, etc. But it can perform basic function calls! :^) Note that the called function will *not* execute as bytecode, but will simply fall back into the old codepath and use the AST interpreter.
This commit is contained in:
parent
1eafaf67fe
commit
dc63958478
4 changed files with 85 additions and 0 deletions
|
@ -91,6 +91,31 @@ void JumpIfTrue::execute(Bytecode::Interpreter& interpreter) const
|
|||
interpreter.jump(m_target.value());
|
||||
}
|
||||
|
||||
void Call::execute(Bytecode::Interpreter& interpreter) const
|
||||
{
|
||||
auto callee = interpreter.reg(m_callee);
|
||||
if (!callee.is_function()) {
|
||||
TODO();
|
||||
}
|
||||
auto& function = callee.as_function();
|
||||
|
||||
auto this_value = interpreter.reg(m_this_value);
|
||||
|
||||
Value return_value;
|
||||
|
||||
if (m_arguments.is_empty()) {
|
||||
return_value = interpreter.vm().call(function, this_value);
|
||||
} else {
|
||||
MarkedValueList argument_values { interpreter.vm().heap() };
|
||||
for (auto& arg : m_arguments) {
|
||||
argument_values.append(interpreter.reg(arg));
|
||||
}
|
||||
return_value = interpreter.vm().call(function, this_value, move(argument_values));
|
||||
}
|
||||
|
||||
interpreter.reg(m_dst) = return_value;
|
||||
}
|
||||
|
||||
void EnterScope::execute(Bytecode::Interpreter& interpreter) const
|
||||
{
|
||||
auto& vm = interpreter.vm();
|
||||
|
@ -182,6 +207,22 @@ String JumpIfTrue::to_string() const
|
|||
return String::formatted("JumpIfTrue result:{}, target:<empty>", m_result);
|
||||
}
|
||||
|
||||
String Call::to_string() const
|
||||
{
|
||||
StringBuilder builder;
|
||||
builder.appendff("Call dst:{}, callee:{}, this:{}", m_dst, m_callee, m_this_value);
|
||||
if (!m_arguments.is_empty()) {
|
||||
builder.append(", arguments:[");
|
||||
for (size_t i = 0; i < m_arguments.size(); ++i) {
|
||||
builder.appendff("{}", m_arguments[i]);
|
||||
if (i != m_arguments.size() - 1)
|
||||
builder.append(',');
|
||||
}
|
||||
builder.append(']');
|
||||
}
|
||||
return builder.to_string();
|
||||
}
|
||||
|
||||
String EnterScope::to_string() const
|
||||
{
|
||||
return "EnterScope";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue