/* * Copyright (c) 2021-2023, Luke Wilde * Copyright (c) 2021-2022, Linus Groh * Copyright (c) 2023, Shannon Booth * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include #include #include #include namespace Web::WebIDL { bool is_buffer_source_type(JS::Value); GC::Ptr underlying_buffer_source(JS::Object& buffer_source); WEB_API ErrorOr get_buffer_source_copy(JS::Object const& buffer_source); JS::Completion call_user_object_operation(CallbackType& callback, Utf16FlyString const& operation_name, Optional this_argument, ReadonlySpan args); WEB_API JS::ThrowCompletionOr to_string(JS::VM&, JS::Value); WEB_API JS::ThrowCompletionOr to_utf16_string(JS::VM&, JS::Value); WEB_API JS::ThrowCompletionOr to_usv_string(JS::VM&, JS::Value); JS::ThrowCompletionOr to_utf16_usv_string(JS::VM&, JS::Value); JS::ThrowCompletionOr to_byte_string(JS::VM&, JS::Value); enum class ExceptionBehavior { NotSpecified, Report, Rethrow, }; WEB_API JS::Completion invoke_callback(CallbackType& callback, Optional this_argument, ExceptionBehavior exception_behavior, ReadonlySpan args); WEB_API JS::Completion invoke_callback(CallbackType& callback, Optional this_argument, ReadonlySpan args); WEB_API GC::Ref invoke_promise_callback(CallbackType& callback, Optional this_argument, ReadonlySpan args); JS::Completion construct(CallbackType& callable, ReadonlySpan args); // https://webidl.spec.whatwg.org/#abstract-opdef-integerpart double integer_part(double n); enum class EnforceRange { Yes, No, }; enum class Clamp { Yes, No, }; // https://webidl.spec.whatwg.org/#abstract-opdef-converttoint template JS::ThrowCompletionOr convert_to_int(JS::VM& vm, JS::Value, EnforceRange enforce_range = EnforceRange::No, Clamp clamp = Clamp::No); bool lists_contain_same_elements(GC::Ptr array, Optional>> const& elements); // https://webidl.spec.whatwg.org/#internally-create-a-new-object-implementing-the-interface // Steps from "internally create a new object implementing the interface" template JS::ThrowCompletionOr set_prototype_from_new_target(JS::VM& vm, JS::FunctionObject& new_target, FlyString const& interface_name, JS::Object& object) { // 3.2. Let prototype be ? Get(newTarget, "prototype"). auto prototype = TRY(new_target.get(vm.names.prototype)); // 3.3. If Type(prototype) is not Object, then: if (!prototype.is_object()) { // 1. Let targetRealm be ? GetFunctionRealm(newTarget). auto* target_realm = TRY(JS::get_function_realm(vm, new_target)); // 2. Set prototype to the interface prototype object for interface in targetRealm. VERIFY(target_realm); prototype = &Bindings::ensure_web_prototype(*target_realm, interface_name); } // 9. Set instance.[[Prototype]] to prototype. VERIFY(prototype.is_object()); TRY(object.internal_set_prototype_of(&prototype.as_object())); return {}; } }