AbstractOperations.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * Copyright (c) 2021-2023, Luke Wilde <lukew@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2023, Shannon Booth <shannon@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Forward.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Runtime/AbstractOperations.h>
  12. #include <LibJS/Runtime/FunctionObject.h>
  13. #include <LibWeb/Bindings/HostDefined.h>
  14. #include <LibWeb/HTML/Scripting/Environments.h>
  15. #include <LibWeb/WebIDL/CallbackType.h>
  16. namespace Web::WebIDL {
  17. ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source);
  18. JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String const& operation_name, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args);
  19. // https://webidl.spec.whatwg.org/#call-a-user-objects-operation
  20. template<typename... Args>
  21. JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String const& operation_name, Optional<JS::Value> this_argument, Args&&... args)
  22. {
  23. auto& function_object = callback.callback;
  24. JS::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  25. (arguments_list.append(forward<Args>(args)), ...);
  26. return call_user_object_operation(callback, operation_name, move(this_argument), move(arguments_list));
  27. }
  28. JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, JS::MarkedVector<JS::Value> args);
  29. // https://webidl.spec.whatwg.org/#invoke-a-callback-function
  30. template<typename... Args>
  31. JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, Args&&... args)
  32. {
  33. auto& function_object = callback.callback;
  34. JS::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  35. (arguments_list.append(forward<Args>(args)), ...);
  36. return invoke_callback(callback, move(this_argument), move(arguments_list));
  37. }
  38. JS::Completion construct(WebIDL::CallbackType& callback, JS::MarkedVector<JS::Value> args);
  39. // https://webidl.spec.whatwg.org/#construct-a-callback-function
  40. template<typename... Args>
  41. JS::Completion construct(WebIDL::CallbackType& callback, Args&&... args)
  42. {
  43. auto& function_object = callback.callback;
  44. JS::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  45. (arguments_list.append(forward<Args>(args)), ...);
  46. return construct(callback, move(arguments_list));
  47. }
  48. // https://webidl.spec.whatwg.org/#abstract-opdef-integerpart
  49. double integer_part(double n);
  50. enum class EnforceRange {
  51. Yes,
  52. No,
  53. };
  54. enum class Clamp {
  55. Yes,
  56. No,
  57. };
  58. // https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  59. template<Integral T>
  60. JS::ThrowCompletionOr<T> convert_to_int(JS::VM& vm, JS::Value, EnforceRange enforce_range = EnforceRange::No, Clamp clamp = Clamp::No);
  61. }