AbstractOperations.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Forward.h>
  9. #include <LibJS/Forward.h>
  10. #include <LibJS/Runtime/AbstractOperations.h>
  11. #include <LibJS/Runtime/FunctionObject.h>
  12. #include <LibWeb/Bindings/HostDefined.h>
  13. #include <LibWeb/HTML/Scripting/Environments.h>
  14. #include <LibWeb/WebIDL/CallbackType.h>
  15. namespace Web::WebIDL {
  16. ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source);
  17. JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, DeprecatedString const& operation_name, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args);
  18. // https://webidl.spec.whatwg.org/#call-a-user-objects-operation
  19. template<typename... Args>
  20. JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, DeprecatedString const& operation_name, Optional<JS::Value> this_argument, Args&&... args)
  21. {
  22. auto& function_object = callback.callback;
  23. JS::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  24. (arguments_list.append(forward<Args>(args)), ...);
  25. return call_user_object_operation(callback, operation_name, move(this_argument), move(arguments_list));
  26. }
  27. JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args);
  28. // https://webidl.spec.whatwg.org/#invoke-a-callback-function
  29. template<typename... Args>
  30. JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, Args&&... args)
  31. {
  32. auto& function_object = callback.callback;
  33. JS::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  34. (arguments_list.append(forward<Args>(args)), ...);
  35. return invoke_callback(callback, move(this_argument), move(arguments_list));
  36. }
  37. JS::Completion construct(WebIDL::CallbackType& callback, JS::MarkedVector<JS::Value> args);
  38. // https://webidl.spec.whatwg.org/#construct-a-callback-function
  39. template<typename... Args>
  40. JS::Completion construct(WebIDL::CallbackType& callback, Args&&... args)
  41. {
  42. auto& function_object = callback.callback;
  43. JS::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  44. (arguments_list.append(forward<Args>(args)), ...);
  45. return construct(callback, move(arguments_list));
  46. }
  47. }