1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 06:48:12 +00:00

LibJS: Protect function call "this" and arguments from GC

This patch adds a CallFrame stack to Interpreter, which keeps track of
the "this" value and all argument values passed in function calls.

Interpreter::gather_roots() scans the call stack, making sure that all
argument values get marked. :^)
This commit is contained in:
Andreas Kling 2020-03-17 11:00:09 +01:00
parent 666f84b933
commit bf9912cc59
3 changed files with 22 additions and 19 deletions

View file

@ -59,21 +59,15 @@ Value CallExpression::execute(Interpreter& interpreter) const
ASSERT(callee.as_object()->is_function());
auto* function = static_cast<Function*>(callee.as_object());
Vector<Value> argument_values;
auto& call_frame = interpreter.push_call_frame();
for (size_t i = 0; i < m_arguments.size(); ++i)
argument_values.append(m_arguments[i].execute(interpreter));
call_frame.arguments.append(m_arguments[i].execute(interpreter));
Value this_value = js_undefined();
if (m_callee->is_member_expression())
this_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter).to_object(interpreter.heap());
call_frame.this_value = static_cast<const MemberExpression&>(*m_callee).object().execute(interpreter).to_object(interpreter.heap());
if (!this_value.is_undefined())
interpreter.push_this_value(this_value);
auto result = function->call(interpreter, move(argument_values));
if (!this_value.is_undefined())
interpreter.pop_this_value();
auto result = function->call(interpreter, call_frame.arguments);
interpreter.pop_call_frame();
return result;
}