Skip to content

Commit

Permalink
LibJS: Refactor ScriptFunction::call() a bit
Browse files Browse the repository at this point in the history
- Get VM reference once
- Less nesting
- Better variable names
  • Loading branch information
linusg authored and awesomekling committed Nov 12, 2020
1 parent 549786e commit b07c7f5
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions Libraries/LibJS/Runtime/ScriptFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,10 @@ LexicalEnvironment* ScriptFunction::create_environment()

Value ScriptFunction::call()
{
auto& vm = this->vm();

OwnPtr<Interpreter> local_interpreter;
Interpreter* interpreter = vm().interpreter_if_exists();
Interpreter* interpreter = vm.interpreter_if_exists();

if (!interpreter) {
local_interpreter = Interpreter::create_with_existing_global_object(global_object());
Expand All @@ -122,30 +124,30 @@ Value ScriptFunction::call()

VM::InterpreterExecutionScope scope(*interpreter);

auto& argument_values = vm().call_frame().arguments;
auto& call_frame_args = vm.call_frame().arguments;
ArgumentVector arguments;
for (size_t i = 0; i < m_parameters.size(); ++i) {
auto parameter = parameters()[i];
auto value = js_undefined();
auto parameter = m_parameters[i];
Value argument_value;
if (parameter.is_rest) {
auto* array = Array::create(global_object());
for (size_t rest_index = i; rest_index < argument_values.size(); ++rest_index)
array->indexed_properties().append(argument_values[rest_index]);
value = Value(array);
for (size_t rest_index = i; rest_index < call_frame_args.size(); ++rest_index)
array->indexed_properties().append(call_frame_args[rest_index]);
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 {
if (i < argument_values.size() && !argument_values[i].is_undefined()) {
value = argument_values[i];
} else if (parameter.default_value) {
value = parameter.default_value->execute(*interpreter, global_object());
if (vm().exception())
return {};
}
argument_value = js_undefined();
}
arguments.append({ parameter.name, value });
vm().current_environment()->set(global_object(), parameter.name, { value, DeclarationKind::Var });
arguments.append({ parameter.name, argument_value });
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&)
Expand Down

0 comments on commit b07c7f5

Please sign in to comment.