LibWeb: Move ad hoc CallbackType helper method to CallbackType header

Abstract operations of a stream does not seem like the correct home for
this function.
This commit is contained in:
Shannon Booth 2024-12-08 17:43:24 +13:00 committed by Jelle Raaijmakers
commit 99073c0561
Notes: github-actions[bot] 2024-12-11 14:12:23 +00:00
7 changed files with 29 additions and 28 deletions

View file

@ -5,6 +5,7 @@
*/
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/WebIDL/CallbackType.h>
@ -26,4 +27,19 @@ void CallbackType::visit_edges(Cell::Visitor& visitor)
visitor.visit(callback_context);
}
// Non-standard function to aid in converting a user-provided function into a WebIDL::Callback. This is essentially
// what the Bindings generator would do at compile time, but at runtime instead.
JS::ThrowCompletionOr<GC::Root<CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key, OperationReturnsPromise operation_returns_promise)
{
auto property = TRY(value.get(vm, property_key));
if (property.is_undefined())
return GC::Root<CallbackType> {};
if (!property.is_function())
return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, property.to_string_without_side_effects());
return vm.heap().allocate<CallbackType>(property.as_object(), HTML::incumbent_realm(), operation_returns_promise);
}
}

View file

@ -39,4 +39,6 @@ private:
virtual void visit_edges(Cell::Visitor&) override;
};
JS::ThrowCompletionOr<GC::Root<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key, WebIDL::OperationReturnsPromise);
}