mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-20 17:58:18 +03:00
LibJS: Pass JS::Function around by reference more
This commit is contained in:
parent
97382677bd
commit
aaf35112a4
Notes:
sideshowbarker
2024-07-19 07:11:39 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/aaf35112a43
@ -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();
|
||||
call_frame.function_name = function->name();
|
||||
call_frame.function_name = function.name();
|
||||
call_frame.this_value = this_value;
|
||||
if (arguments.has_value())
|
||||
call_frame.arguments = arguments.value().values();
|
||||
call_frame.environment = function->create_environment();
|
||||
auto result = function->call(*this);
|
||||
call_frame.environment = function.create_environment();
|
||||
auto result = function.call(*this);
|
||||
pop_call_frame();
|
||||
return result;
|
||||
}
|
||||
|
@ -103,7 +103,7 @@ public:
|
||||
void enter_scope(const ScopeNode&, ArgumentVector, ScopeType);
|
||||
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()
|
||||
{
|
||||
|
@ -105,7 +105,7 @@ Value ArrayPrototype::filter(Interpreter& interpreter)
|
||||
arguments.append(value);
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
auto result = interpreter.call(callback, this_value, move(arguments));
|
||||
auto result = interpreter.call(*callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
if (result.to_boolean())
|
||||
@ -134,7 +134,7 @@ Value ArrayPrototype::for_each(Interpreter& interpreter)
|
||||
arguments.append(value);
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
interpreter.call(callback, this_value, move(arguments));
|
||||
interpreter.call(*callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
}
|
||||
@ -171,7 +171,7 @@ Value ArrayPrototype::map(Interpreter& interpreter)
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
|
||||
auto result = interpreter.call(callback, this_value, move(arguments));
|
||||
auto result = interpreter.call(*callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
@ -465,7 +465,7 @@ Value ArrayPrototype::find(Interpreter& interpreter)
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
|
||||
auto result = interpreter.call(callback, this_value, move(arguments));
|
||||
auto result = interpreter.call(*callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
@ -502,7 +502,7 @@ Value ArrayPrototype::find_index(Interpreter& interpreter)
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
|
||||
auto result = interpreter.call(callback, this_value, move(arguments));
|
||||
auto result = interpreter.call(*callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
@ -539,7 +539,7 @@ Value ArrayPrototype::some(Interpreter& interpreter)
|
||||
arguments.append(Value((i32)i));
|
||||
arguments.append(array);
|
||||
|
||||
auto result = interpreter.call(callback, this_value, move(arguments));
|
||||
auto result = interpreter.call(*callback, this_value, move(arguments));
|
||||
if (interpreter.exception())
|
||||
return {};
|
||||
|
||||
|
@ -64,7 +64,7 @@ Value FunctionPrototype::apply(Interpreter& interpreter)
|
||||
return {};
|
||||
if (!this_object->is_function())
|
||||
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 arg_array = interpreter.argument(1);
|
||||
if (arg_array.is_null() || arg_array.is_undefined())
|
||||
@ -112,7 +112,7 @@ Value FunctionPrototype::call(Interpreter& interpreter)
|
||||
return {};
|
||||
if (!this_object->is_function())
|
||||
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);
|
||||
MarkedValueList arguments(interpreter.heap());
|
||||
if (interpreter.argument_count() > 1) {
|
||||
|
@ -359,7 +359,7 @@ Value Object::to_string() const
|
||||
&& to_string_property.is_object()
|
||||
&& to_string_property.as_object().is_function()) {
|
||||
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()));
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ void Node::dispatch_event(NonnullRefPtr<Event> event)
|
||||
auto* event_wrapper = wrap(heap, *event);
|
||||
JS::MarkedValueList arguments(heap);
|
||||
arguments.append(event_wrapper);
|
||||
document().interpreter().call(&function, this_value, move(arguments));
|
||||
document().interpreter().call(function, this_value, move(arguments));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,8 +63,8 @@ void Window::set_interval(JS::Function& callback, i32 interval)
|
||||
{
|
||||
// FIXME: This leaks the interval timer and makes it unstoppable.
|
||||
(void)Core::Timer::construct(interval, [handle = make_handle(&callback)] {
|
||||
auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(handle.cell()));
|
||||
auto& interpreter = function->interpreter();
|
||||
auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
|
||||
auto& interpreter = function.interpreter();
|
||||
interpreter.call(function);
|
||||
}).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.
|
||||
auto& timer = Core::Timer::construct(interval, [handle = make_handle(&callback)] {
|
||||
auto* function = const_cast<JS::Function*>(static_cast<const JS::Function*>(handle.cell()));
|
||||
auto& interpreter = function->interpreter();
|
||||
auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
|
||||
auto& interpreter = function.interpreter();
|
||||
interpreter.call(function);
|
||||
}).leak_ref();
|
||||
timer.set_single_shot(true);
|
||||
@ -86,8 +86,8 @@ i32 Window::request_animation_frame(JS::Function& callback)
|
||||
static double fake_timestamp = 0;
|
||||
|
||||
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& interpreter = function->interpreter();
|
||||
auto& function = const_cast<JS::Function&>(static_cast<const JS::Function&>(*handle.cell()));
|
||||
auto& interpreter = function.interpreter();
|
||||
JS::MarkedValueList arguments(interpreter.heap());
|
||||
arguments.append(JS::Value(fake_timestamp));
|
||||
fake_timestamp += 10;
|
||||
|
@ -97,7 +97,7 @@ void XMLHttpRequest::dispatch_event(NonnullRefPtr<Event> event)
|
||||
auto* this_value = wrap(heap, *this);
|
||||
JS::MarkedValueList arguments(heap);
|
||||
arguments.append(wrap(heap, *event));
|
||||
function.interpreter().call(&function, this_value, move(arguments));
|
||||
function.interpreter().call(function, this_value, move(arguments));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user