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

LibJS: Pass JS::Function around by reference more

This commit is contained in:
Andreas Kling 2020-04-29 13:43:57 +02:00
parent 97382677bd
commit aaf35112a4
8 changed files with 22 additions and 22 deletions

View file

@ -195,15 +195,15 @@ void Interpreter::gather_roots(Badge<Heap>, HashTable<Cell*>& roots)
} }
} }
Value Interpreter::call(Function* function, Value this_value, Optional<MarkedValueList> arguments) Value Interpreter::call(Function& function, Value this_value, Optional<MarkedValueList> arguments)
{ {
auto& call_frame = push_call_frame(); auto& call_frame = push_call_frame();
call_frame.function_name = function->name(); call_frame.function_name = function.name();
call_frame.this_value = this_value; call_frame.this_value = this_value;
if (arguments.has_value()) if (arguments.has_value())
call_frame.arguments = arguments.value().values(); call_frame.arguments = arguments.value().values();
call_frame.environment = function->create_environment(); call_frame.environment = function.create_environment();
auto result = function->call(*this); auto result = function.call(*this);
pop_call_frame(); pop_call_frame();
return result; return result;
} }

View file

@ -103,7 +103,7 @@ public:
void enter_scope(const ScopeNode&, ArgumentVector, ScopeType); void enter_scope(const ScopeNode&, ArgumentVector, ScopeType);
void exit_scope(const ScopeNode&); void exit_scope(const ScopeNode&);
Value call(Function*, Value this_value = {}, Optional<MarkedValueList> arguments = {}); Value call(Function&, Value this_value = {}, Optional<MarkedValueList> arguments = {});
CallFrame& push_call_frame() CallFrame& push_call_frame()
{ {

View file

@ -105,7 +105,7 @@ Value ArrayPrototype::filter(Interpreter& interpreter)
arguments.append(value); arguments.append(value);
arguments.append(Value((i32)i)); arguments.append(Value((i32)i));
arguments.append(array); arguments.append(array);
auto result = interpreter.call(callback, this_value, move(arguments)); auto result = interpreter.call(*callback, this_value, move(arguments));
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
if (result.to_boolean()) if (result.to_boolean())
@ -134,7 +134,7 @@ Value ArrayPrototype::for_each(Interpreter& interpreter)
arguments.append(value); arguments.append(value);
arguments.append(Value((i32)i)); arguments.append(Value((i32)i));
arguments.append(array); arguments.append(array);
interpreter.call(callback, this_value, move(arguments)); interpreter.call(*callback, this_value, move(arguments));
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
} }
@ -171,7 +171,7 @@ Value ArrayPrototype::map(Interpreter& interpreter)
arguments.append(Value((i32)i)); arguments.append(Value((i32)i));
arguments.append(array); arguments.append(array);
auto result = interpreter.call(callback, this_value, move(arguments)); auto result = interpreter.call(*callback, this_value, move(arguments));
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
@ -465,7 +465,7 @@ Value ArrayPrototype::find(Interpreter& interpreter)
arguments.append(Value((i32)i)); arguments.append(Value((i32)i));
arguments.append(array); arguments.append(array);
auto result = interpreter.call(callback, this_value, move(arguments)); auto result = interpreter.call(*callback, this_value, move(arguments));
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
@ -502,7 +502,7 @@ Value ArrayPrototype::find_index(Interpreter& interpreter)
arguments.append(Value((i32)i)); arguments.append(Value((i32)i));
arguments.append(array); arguments.append(array);
auto result = interpreter.call(callback, this_value, move(arguments)); auto result = interpreter.call(*callback, this_value, move(arguments));
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};
@ -539,7 +539,7 @@ Value ArrayPrototype::some(Interpreter& interpreter)
arguments.append(Value((i32)i)); arguments.append(Value((i32)i));
arguments.append(array); arguments.append(array);
auto result = interpreter.call(callback, this_value, move(arguments)); auto result = interpreter.call(*callback, this_value, move(arguments));
if (interpreter.exception()) if (interpreter.exception())
return {}; return {};

View file

@ -64,7 +64,7 @@ Value FunctionPrototype::apply(Interpreter& interpreter)
return {}; return {};
if (!this_object->is_function()) if (!this_object->is_function())
return interpreter.throw_exception<TypeError>("Not a Function object"); return interpreter.throw_exception<TypeError>("Not a Function object");
auto function = static_cast<Function*>(this_object); auto& function = static_cast<Function&>(*this_object);
auto this_arg = interpreter.argument(0); auto this_arg = interpreter.argument(0);
auto arg_array = interpreter.argument(1); auto arg_array = interpreter.argument(1);
if (arg_array.is_null() || arg_array.is_undefined()) if (arg_array.is_null() || arg_array.is_undefined())
@ -112,7 +112,7 @@ Value FunctionPrototype::call(Interpreter& interpreter)
return {}; return {};
if (!this_object->is_function()) if (!this_object->is_function())
return interpreter.throw_exception<TypeError>("Not a Function object"); return interpreter.throw_exception<TypeError>("Not a Function object");
auto function = static_cast<Function*>(this_object); auto& function = static_cast<Function&>(*this_object);
auto this_arg = interpreter.argument(0); auto this_arg = interpreter.argument(0);
MarkedValueList arguments(interpreter.heap()); MarkedValueList arguments(interpreter.heap());
if (interpreter.argument_count() > 1) { if (interpreter.argument_count() > 1) {

View file

@ -359,7 +359,7 @@ Value Object::to_string() const
&& to_string_property.is_object() && to_string_property.is_object()
&& to_string_property.as_object().is_function()) { && to_string_property.as_object().is_function()) {
auto& to_string_function = static_cast<Function&>(to_string_property.as_object()); auto& to_string_function = static_cast<Function&>(to_string_property.as_object());
return const_cast<Object*>(this)->interpreter().call(&to_string_function, const_cast<Object*>(this)); return const_cast<Object*>(this)->interpreter().call(to_string_function, const_cast<Object*>(this));
} }
return js_string(heap(), String::format("[object %s]", class_name())); return js_string(heap(), String::format("[object %s]", class_name()));
} }

View file

@ -146,7 +146,7 @@ void Node::dispatch_event(NonnullRefPtr<Event> event)
auto* event_wrapper = wrap(heap, *event); auto* event_wrapper = wrap(heap, *event);
JS::MarkedValueList arguments(heap); JS::MarkedValueList arguments(heap);
arguments.append(event_wrapper); arguments.append(event_wrapper);
document().interpreter().call(&function, this_value, move(arguments)); document().interpreter().call(function, this_value, move(arguments));
} }
} }

View file

@ -63,8 +63,8 @@ void Window::set_interval(JS::Function& callback, i32 interval)
{ {
// FIXME: This leaks the interval timer and makes it unstoppable. // FIXME: This leaks the interval timer and makes it unstoppable.
(void)Core::Timer::construct(interval, [handle = make_handle(&callback)] { (void)Core::Timer::construct(interval, [handle = make_handle(&callback)] {
auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(handle.cell())); auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
auto& interpreter = function->interpreter(); auto& interpreter = function.interpreter();
interpreter.call(function); interpreter.call(function);
}).leak_ref(); }).leak_ref();
} }
@ -73,8 +73,8 @@ void Window::set_timeout(JS::Function& callback, i32 interval)
{ {
// FIXME: This leaks the interval timer and makes it unstoppable. // FIXME: This leaks the interval timer and makes it unstoppable.
auto& timer = Core::Timer::construct(interval, [handle = make_handle(&callback)] { auto& timer = Core::Timer::construct(interval, [handle = make_handle(&callback)] {
auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(handle.cell())); auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
auto& interpreter = function->interpreter(); auto& interpreter = function.interpreter();
interpreter.call(function); interpreter.call(function);
}).leak_ref(); }).leak_ref();
timer.set_single_shot(true); timer.set_single_shot(true);
@ -86,8 +86,8 @@ i32 Window::request_animation_frame(JS::Function& callback)
static double fake_timestamp = 0; static double fake_timestamp = 0;
i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) { i32 link_id = GUI::DisplayLink::register_callback([handle = make_handle(&callback)](i32 link_id) {
auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(handle.cell())); auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
auto& interpreter = function->interpreter(); auto& interpreter = function.interpreter();
JS::MarkedValueList arguments(interpreter.heap()); JS::MarkedValueList arguments(interpreter.heap());
arguments.append(JS::Value(fake_timestamp)); arguments.append(JS::Value(fake_timestamp));
fake_timestamp += 10; fake_timestamp += 10;

View file

@ -97,7 +97,7 @@ void XMLHttpRequest::dispatch_event(NonnullRefPtr<Event> event)
auto* this_value = wrap(heap, *this); auto* this_value = wrap(heap, *this);
JS::MarkedValueList arguments(heap); JS::MarkedValueList arguments(heap);
arguments.append(wrap(heap, *event)); arguments.append(wrap(heap, *event));
function.interpreter().call(&function, this_value, move(arguments)); function.interpreter().call(function, this_value, move(arguments));
} }
} }
} }