Array.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/Assertions.h>
  9. #include <AK/Concepts.h>
  10. #include <AK/Function.h>
  11. #include <AK/Span.h>
  12. #include <AK/Vector.h>
  13. #include <LibJS/Runtime/Completion.h>
  14. #include <LibJS/Runtime/GlobalObject.h>
  15. #include <LibJS/Runtime/Object.h>
  16. #include <LibJS/Runtime/VM.h>
  17. namespace JS {
  18. class Array : public Object {
  19. JS_OBJECT(Array, Object);
  20. public:
  21. static ThrowCompletionOr<NonnullGCPtr<Array>> create(Realm&, u64 length, Object* prototype = nullptr);
  22. static NonnullGCPtr<Array> create_from(Realm&, Vector<Value> const&);
  23. // Non-standard but equivalent to CreateArrayFromList.
  24. template<typename T>
  25. static NonnullGCPtr<Array> create_from(Realm& realm, ReadonlySpan<T> elements, Function<Value(T const&)> map_fn)
  26. {
  27. auto values = MarkedVector<Value> { realm.heap() };
  28. values.ensure_capacity(elements.size());
  29. for (auto const& element : elements)
  30. values.append(map_fn(element));
  31. return Array::create_from(realm, values);
  32. }
  33. // Non-standard but equivalent to CreateArrayFromList.
  34. template<typename T, FallibleFunction<T const&> Callback>
  35. static ThrowCompletionOr<NonnullGCPtr<Array>> try_create_from(VM& vm, Realm& realm, ReadonlySpan<T> elements, Callback map_fn)
  36. {
  37. auto values = MarkedVector<Value> { realm.heap() };
  38. TRY_OR_THROW_OOM(vm, values.try_ensure_capacity(elements.size()));
  39. for (auto const& element : elements)
  40. TRY_OR_THROW_OOM(vm, values.try_append(TRY(map_fn(element))));
  41. return Array::create_from(realm, values);
  42. }
  43. virtual ~Array() override = default;
  44. virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override;
  45. virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&) override;
  46. virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
  47. virtual ThrowCompletionOr<MarkedVector<Value>> internal_own_property_keys() const override;
  48. [[nodiscard]] bool length_is_writable() const { return m_length_writable; };
  49. protected:
  50. explicit Array(Object& prototype);
  51. private:
  52. ThrowCompletionOr<bool> set_length(PropertyDescriptor const&);
  53. bool m_length_writable { true };
  54. };
  55. enum class Holes {
  56. SkipHoles,
  57. ReadThroughHoles,
  58. };
  59. ThrowCompletionOr<MarkedVector<Value>> sort_indexed_properties(VM&, Object const&, size_t length, Function<ThrowCompletionOr<double>(Value, Value)> const& sort_compare, Holes holes);
  60. ThrowCompletionOr<double> compare_array_elements(VM&, Value x, Value y, FunctionObject* comparefn);
  61. }