1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:38:10 +00:00

LibJS: Function.length respects default and rest parameters

"[Function.length is] the number of formal parameters. This number
excludes the rest parameter and only includes parameters before
the first one with a default value." - MDN
This commit is contained in:
Matthew Olsson 2020-05-05 20:02:14 -07:00 committed by Andreas Kling
parent 2c14714ee0
commit 838390171c
6 changed files with 43 additions and 15 deletions

View file

@ -47,17 +47,18 @@ static ScriptFunction* script_function_from(Interpreter& interpreter)
return static_cast<ScriptFunction*>(this_object);
}
ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, LexicalEnvironment* parent_environment)
ScriptFunction* ScriptFunction::create(GlobalObject& global_object, const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment)
{
return global_object.heap().allocate<ScriptFunction>(name, body, move(parameters), parent_environment, *global_object.function_prototype());
return global_object.heap().allocate<ScriptFunction>(name, body, move(parameters), m_function_length, parent_environment, *global_object.function_prototype());
}
ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, LexicalEnvironment* parent_environment, Object& prototype)
ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FunctionNode::Parameter> parameters, i32 m_function_length, LexicalEnvironment* parent_environment, Object& prototype)
: Function(prototype)
, m_name(name)
, m_body(body)
, m_parameters(move(parameters))
, m_parent_environment(parent_environment)
, m_function_length(m_function_length)
{
put("prototype", Object::create_empty(interpreter(), interpreter().global_object()), 0);
put_native_property("length", length_getter, nullptr, Attribute::Configurable);
@ -130,7 +131,7 @@ Value ScriptFunction::length_getter(Interpreter& interpreter)
auto* function = script_function_from(interpreter);
if (!function)
return {};
return Value(static_cast<i32>(function->parameters().size()));
return Value(static_cast<i32>(function->m_function_length));
}
Value ScriptFunction::name_getter(Interpreter& interpreter)