AbstractOperations.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/HTML/Scripting/Environments.h>
  14. #include <LibWeb/WebIDL/CallbackType.h>
  15. namespace Web::WebIDL {
  16. bool is_buffer_source_type(JS::Value);
  17. GC::Ptr<JS::ArrayBuffer> underlying_buffer_source(JS::Object& buffer_source);
  18. ErrorOr<ByteBuffer> get_buffer_source_copy(JS::Object const& buffer_source);
  19. JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String const& operation_name, Optional<JS::Value> this_argument, GC::MarkedVector<JS::Value> args);
  20. JS::ThrowCompletionOr<String> to_string(JS::VM&, JS::Value);
  21. JS::ThrowCompletionOr<String> to_usv_string(JS::VM&, JS::Value);
  22. JS::ThrowCompletionOr<String> to_byte_string(JS::VM&, JS::Value);
  23. // https://webidl.spec.whatwg.org/#call-a-user-objects-operation
  24. template<typename... Args>
  25. JS::Completion call_user_object_operation(WebIDL::CallbackType& callback, String const& operation_name, Optional<JS::Value> this_argument, Args&&... args)
  26. {
  27. auto& function_object = callback.callback;
  28. GC::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  29. (arguments_list.append(forward<Args>(args)), ...);
  30. return call_user_object_operation(callback, operation_name, move(this_argument), move(arguments_list));
  31. }
  32. JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, GC::MarkedVector<JS::Value> args);
  33. // https://webidl.spec.whatwg.org/#invoke-a-callback-function
  34. template<typename... Args>
  35. JS::Completion invoke_callback(WebIDL::CallbackType& callback, Optional<JS::Value> this_argument, Args&&... args)
  36. {
  37. auto& function_object = callback.callback;
  38. GC::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  39. (arguments_list.append(forward<Args>(args)), ...);
  40. return invoke_callback(callback, move(this_argument), move(arguments_list));
  41. }
  42. JS::Completion construct(WebIDL::CallbackType& callback, GC::MarkedVector<JS::Value> args);
  43. // https://webidl.spec.whatwg.org/#construct-a-callback-function
  44. template<typename... Args>
  45. JS::Completion construct(WebIDL::CallbackType& callback, Args&&... args)
  46. {
  47. auto& function_object = callback.callback;
  48. GC::MarkedVector<JS::Value> arguments_list { function_object->heap() };
  49. (arguments_list.append(forward<Args>(args)), ...);
  50. return construct(callback, move(arguments_list));
  51. }
  52. // https://webidl.spec.whatwg.org/#abstract-opdef-integerpart
  53. double integer_part(double n);
  54. enum class EnforceRange {
  55. Yes,
  56. No,
  57. };
  58. enum class Clamp {
  59. Yes,
  60. No,
  61. };
  62. // https://webidl.spec.whatwg.org/#abstract-opdef-converttoint
  63. template<Integral T>
  64. JS::ThrowCompletionOr<T> convert_to_int(JS::VM& vm, JS::Value, EnforceRange enforce_range = EnforceRange::No, Clamp clamp = Clamp::No);
  65. }