AbstractOperations.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  4. * Copyright (c) 2024, Tim Flynn <trflynn89@ladybird.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/Variant.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibJS/Runtime/Completion.h>
  12. #include <LibJS/Runtime/VM.h>
  13. #include <LibJS/Runtime/ValueInlines.h>
  14. namespace JS::Temporal {
  15. // 13.39 ToIntegerIfIntegral ( argument ), https://tc39.es/proposal-temporal/#sec-tointegerifintegral
  16. template<typename... Args>
  17. ThrowCompletionOr<double> to_integer_if_integral(VM& vm, Value argument, ErrorType error_type, Args&&... args)
  18. {
  19. // 1. Let number be ? ToNumber(argument).
  20. auto number = TRY(argument.to_number(vm));
  21. // 2. If number is not an integral Number, throw a RangeError exception.
  22. if (!number.is_integral_number())
  23. return vm.throw_completion<RangeError>(error_type, forward<Args>(args)...);
  24. // 3. Return ℝ(number).
  25. return number.as_double();
  26. }
  27. enum class OptionType {
  28. Boolean,
  29. String,
  30. };
  31. struct DefaultRequired { };
  32. using OptionDefault = Variant<DefaultRequired, Empty, bool, StringView, double>;
  33. ThrowCompletionOr<Object*> get_options_object(VM&, Value options);
  34. ThrowCompletionOr<Value> get_option(VM&, Object const& options, PropertyKey const& property, OptionType type, ReadonlySpan<StringView> values, OptionDefault const&);
  35. template<size_t Size>
  36. ThrowCompletionOr<Value> get_option(VM& vm, Object const& options, PropertyKey const& property, OptionType type, StringView const (&values)[Size], OptionDefault const& default_)
  37. {
  38. return get_option(vm, options, property, type, ReadonlySpan<StringView> { values }, default_);
  39. }
  40. }