mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 23:17:44 +00:00
LibJS: Refactor ScriptFunction::call() a bit
- Get VM reference once - Less nesting - Better variable names
This commit is contained in:
parent
549786e89a
commit
b07c7f589f
1 changed files with 19 additions and 17 deletions
|
@ -112,8 +112,10 @@ LexicalEnvironment* ScriptFunction::create_environment()
|
||||||
|
|
||||||
Value ScriptFunction::call()
|
Value ScriptFunction::call()
|
||||||
{
|
{
|
||||||
|
auto& vm = this->vm();
|
||||||
|
|
||||||
OwnPtr<Interpreter> local_interpreter;
|
OwnPtr<Interpreter> local_interpreter;
|
||||||
Interpreter* interpreter = vm().interpreter_if_exists();
|
Interpreter* interpreter = vm.interpreter_if_exists();
|
||||||
|
|
||||||
if (!interpreter) {
|
if (!interpreter) {
|
||||||
local_interpreter = Interpreter::create_with_existing_global_object(global_object());
|
local_interpreter = Interpreter::create_with_existing_global_object(global_object());
|
||||||
|
@ -122,30 +124,30 @@ Value ScriptFunction::call()
|
||||||
|
|
||||||
VM::InterpreterExecutionScope scope(*interpreter);
|
VM::InterpreterExecutionScope scope(*interpreter);
|
||||||
|
|
||||||
auto& argument_values = vm().call_frame().arguments;
|
auto& call_frame_args = vm.call_frame().arguments;
|
||||||
ArgumentVector arguments;
|
ArgumentVector arguments;
|
||||||
for (size_t i = 0; i < m_parameters.size(); ++i) {
|
for (size_t i = 0; i < m_parameters.size(); ++i) {
|
||||||
auto parameter = parameters()[i];
|
auto parameter = m_parameters[i];
|
||||||
auto value = js_undefined();
|
Value argument_value;
|
||||||
if (parameter.is_rest) {
|
if (parameter.is_rest) {
|
||||||
auto* array = Array::create(global_object());
|
auto* array = Array::create(global_object());
|
||||||
for (size_t rest_index = i; rest_index < argument_values.size(); ++rest_index)
|
for (size_t rest_index = i; rest_index < call_frame_args.size(); ++rest_index)
|
||||||
array->indexed_properties().append(argument_values[rest_index]);
|
array->indexed_properties().append(call_frame_args[rest_index]);
|
||||||
value = Value(array);
|
argument_value = move(array);
|
||||||
|
} else if (i < call_frame_args.size() && !call_frame_args[i].is_undefined()) {
|
||||||
|
argument_value = call_frame_args[i];
|
||||||
|
} else if (parameter.default_value) {
|
||||||
|
argument_value = parameter.default_value->execute(*interpreter, global_object());
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
} else {
|
} else {
|
||||||
if (i < argument_values.size() && !argument_values[i].is_undefined()) {
|
argument_value = js_undefined();
|
||||||
value = argument_values[i];
|
|
||||||
} else if (parameter.default_value) {
|
|
||||||
value = parameter.default_value->execute(*interpreter, global_object());
|
|
||||||
if (vm().exception())
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
arguments.append({ parameter.name, value });
|
arguments.append({ parameter.name, argument_value });
|
||||||
vm().current_environment()->set(global_object(), parameter.name, { value, DeclarationKind::Var });
|
vm.current_environment()->set(global_object(), parameter.name, { argument_value, DeclarationKind::Var });
|
||||||
}
|
}
|
||||||
|
|
||||||
return interpreter->execute_statement(global_object(), m_body, arguments, ScopeType::Function);
|
return interpreter->execute_statement(global_object(), m_body, move(arguments), ScopeType::Function);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value ScriptFunction::construct(Function&)
|
Value ScriptFunction::construct(Function&)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue