diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2023-04-02 15:14:21 -0700 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-04-06 22:54:58 +0200 |
commit | 78feba401d05966e4339169d0afa48d5f44d9829 (patch) | |
tree | c5002aab2f4ec55b908afae6c000b7007bf39671 /Userland/Libraries/LibWeb/Streams | |
parent | 2b6c44852c6cb8127d870ec72b5d4c82df7825e0 (diff) | |
download | serenity-78feba401d05966e4339169d0afa48d5f44d9829.zip |
LibWeb: Move property_to_callback to Streams/AbstractOperations
This will be necessary for UnderlyingSink, which is WritableStream's
equivalent of UnderlyingSource, and functions in much the same way.
Diffstat (limited to 'Userland/Libraries/LibWeb/Streams')
3 files changed, 18 insertions, 16 deletions
diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp index 76eb467175..0ed20ddebb 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.cpp @@ -734,4 +734,19 @@ WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underly return set_up_readable_stream_default_controller(stream, controller, move(start_algorithm), move(pull_algorithm), move(cancel_algorithm), high_water_mark, move(size_algorithm)); } +// 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<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key) +{ + auto property = TRY(value.get(vm, property_key)); + + if (property.is_undefined()) + return JS::Handle<WebIDL::CallbackType> {}; + + if (!property.is_function()) + return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, property.to_string_without_side_effects())); + + return vm.heap().allocate_without_realm<WebIDL::CallbackType>(property.as_object(), HTML::incumbent_settings_object()); +} + } diff --git a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h index 593188fca6..15a47f6ad4 100644 --- a/Userland/Libraries/LibWeb/Streams/AbstractOperations.h +++ b/Userland/Libraries/LibWeb/Streams/AbstractOperations.h @@ -50,4 +50,6 @@ bool readable_stream_default_controller_can_close_or_enqueue(ReadableStreamDefau WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller(ReadableStream&, ReadableStreamDefaultController&, StartAlgorithm&&, PullAlgorithm&&, CancelAlgorithm&&, double high_water_mark, SizeAlgorithm&&); WebIDL::ExceptionOr<void> set_up_readable_stream_default_controller_from_underlying_source(ReadableStream&, JS::Value underlying_source_value, UnderlyingSource, double high_water_mark, SizeAlgorithm&&); +JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key); + } diff --git a/Userland/Libraries/LibWeb/Streams/UnderlyingSource.cpp b/Userland/Libraries/LibWeb/Streams/UnderlyingSource.cpp index 7ea43d3e44..42016b98f3 100644 --- a/Userland/Libraries/LibWeb/Streams/UnderlyingSource.cpp +++ b/Userland/Libraries/LibWeb/Streams/UnderlyingSource.cpp @@ -5,27 +5,12 @@ */ #include <LibJS/Runtime/VM.h> -#include <LibWeb/HTML/Scripting/Environments.h> +#include <LibWeb/Streams/AbstractOperations.h> #include <LibWeb/Streams/UnderlyingSource.h> #include <LibWeb/WebIDL/CallbackType.h> namespace Web::Streams { -// 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. -static JS::ThrowCompletionOr<JS::Handle<WebIDL::CallbackType>> property_to_callback(JS::VM& vm, JS::Value value, JS::PropertyKey const& property_key) -{ - auto property = TRY(value.get(vm, property_key)); - - if (property.is_undefined()) - return JS::Handle<WebIDL::CallbackType> {}; - - if (!property.is_function()) - return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAFunction, TRY_OR_THROW_OOM(vm, property.to_string_without_side_effects())); - - return vm.heap().allocate_without_realm<WebIDL::CallbackType>(property.as_object(), HTML::incumbent_settings_object()); -} - JS::ThrowCompletionOr<UnderlyingSource> UnderlyingSource::from_value(JS::VM& vm, JS::Value value) { if (!value.is_object()) |