/* * Copyright (c) 2022, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include namespace JS { class CanonicalIndex { public: enum class Type { Index, Numeric, Undefined, }; CanonicalIndex(Type type, u32 index) : m_type(type) , m_index(index) { } template CanonicalIndex(Type type, T index) = delete; template static ThrowCompletionOr from_double(VM& vm, Type type, T index) { if (index < static_cast(NumericLimits::min())) return vm.throw_completion(ErrorType::TypedArrayInvalidIntegerIndex, index); if (index > static_cast(NumericLimits::max())) return vm.throw_completion(ErrorType::TypedArrayInvalidIntegerIndex, index); return CanonicalIndex { type, static_cast(index) }; } u32 as_index() const { VERIFY(is_index()); return m_index; } bool is_index() const { return m_type == Type::Index; } bool is_undefined() const { return m_type == Type::Undefined; } private: Type m_type; u32 m_index; }; }