Преглед изворни кода

LibJS: Pass JS::Function around by reference more

Andreas Kling пре 5 година
родитељ
комит
aaf35112a4

+ 4 - 4
Libraries/LibJS/Interpreter.cpp

@@ -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;
 }

+ 1 - 1
Libraries/LibJS/Interpreter.h

@@ -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()
     {

+ 6 - 6
Libraries/LibJS/Runtime/ArrayPrototype.cpp

@@ -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 {};
 

+ 2 - 2
Libraries/LibJS/Runtime/FunctionPrototype.cpp

@@ -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) {

+ 1 - 1
Libraries/LibJS/Runtime/Object.cpp

@@ -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()));
 }

+ 1 - 1
Libraries/LibWeb/DOM/Node.cpp

@@ -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));
         }
     }
 

+ 6 - 6
Libraries/LibWeb/DOM/Window.cpp

@@ -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;

+ 1 - 1
Libraries/LibWeb/DOM/XMLHttpRequest.cpp

@@ -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));
         }
     }
 }