2020-03-20 19:29:57 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2020, Andreas Kling <andreas@ladybird.org>
|
2022-06-13 06:53:58 +00:00
|
|
|
* Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org>
|
2020-03-20 19:29:57 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-20 19:29:57 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <AK/Assertions.h>
|
2023-01-22 03:34:42 +00:00
|
|
|
#include <AK/Concepts.h>
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <AK/Function.h>
|
2022-01-30 21:52:40 +00:00
|
|
|
#include <AK/Span.h>
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <AK/Vector.h>
|
2021-09-29 16:53:57 +00:00
|
|
|
#include <LibJS/Runtime/Completion.h>
|
2021-09-04 16:07:46 +00:00
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
2020-03-20 19:29:57 +00:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2023-01-22 03:34:42 +00:00
|
|
|
#include <LibJS/Runtime/VM.h>
|
2020-03-20 19:29:57 +00:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2021-02-24 08:48:29 +00:00
|
|
|
class Array : public Object {
|
2020-06-21 13:14:02 +00:00
|
|
|
JS_OBJECT(Array, Object);
|
2024-11-14 15:01:23 +00:00
|
|
|
GC_DECLARE_ALLOCATOR(Array);
|
2020-06-21 13:14:02 +00:00
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
public:
|
2024-11-14 15:01:23 +00:00
|
|
|
static ThrowCompletionOr<GC::Ref<Array>> create(Realm&, u64 length, Object* prototype = nullptr);
|
|
|
|
static GC::Ref<Array> create_from(Realm&, ReadonlySpan<Value>);
|
2023-01-22 03:34:42 +00:00
|
|
|
|
2024-10-31 16:29:31 +00:00
|
|
|
template<size_t N>
|
2024-11-14 15:01:23 +00:00
|
|
|
static GC::Ref<Array> create_from(Realm& realm, Value const (&values)[N])
|
2024-10-31 16:29:31 +00:00
|
|
|
{
|
|
|
|
return create_from(realm, ReadonlySpan<Value> { values, N });
|
|
|
|
}
|
|
|
|
|
2021-09-04 16:07:46 +00:00
|
|
|
// Non-standard but equivalent to CreateArrayFromList.
|
|
|
|
template<typename T>
|
2024-11-14 15:01:23 +00:00
|
|
|
static GC::Ref<Array> create_from(Realm& realm, ReadonlySpan<T> elements, Function<Value(T const&)> map_fn)
|
2021-09-04 16:07:46 +00:00
|
|
|
{
|
2024-11-14 15:01:23 +00:00
|
|
|
auto values = GC::MarkedVector<Value> { realm.heap() };
|
2021-09-04 16:07:46 +00:00
|
|
|
values.ensure_capacity(elements.size());
|
2022-02-07 14:12:41 +00:00
|
|
|
for (auto const& element : elements)
|
2021-09-04 16:07:46 +00:00
|
|
|
values.append(map_fn(element));
|
2022-02-07 14:12:41 +00:00
|
|
|
|
2022-08-15 23:20:49 +00:00
|
|
|
return Array::create_from(realm, values);
|
2023-01-22 03:34:42 +00:00
|
|
|
}
|
|
|
|
|
2022-03-14 16:25:06 +00:00
|
|
|
virtual ~Array() override = default;
|
2020-03-20 19:29:57 +00:00
|
|
|
|
2023-08-13 10:53:46 +00:00
|
|
|
virtual ThrowCompletionOr<Optional<PropertyDescriptor>> internal_get_own_property(PropertyKey const&) const override final;
|
2024-11-01 20:03:18 +00:00
|
|
|
virtual ThrowCompletionOr<bool> internal_define_own_property(PropertyKey const&, PropertyDescriptor const&, Optional<PropertyDescriptor>* precomputed_get_own_property = nullptr) override final;
|
2024-03-07 22:29:25 +00:00
|
|
|
virtual ThrowCompletionOr<bool> internal_delete(PropertyKey const&) override;
|
2024-11-14 15:01:23 +00:00
|
|
|
virtual ThrowCompletionOr<GC::MarkedVector<Value>> internal_own_property_keys() const override final;
|
2021-07-07 00:50:19 +00:00
|
|
|
|
2023-07-08 02:48:11 +00:00
|
|
|
[[nodiscard]] bool length_is_writable() const { return m_length_writable; }
|
2020-06-20 14:25:12 +00:00
|
|
|
|
2022-08-28 21:51:28 +00:00
|
|
|
protected:
|
|
|
|
explicit Array(Object& prototype);
|
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
private:
|
2021-10-21 22:34:06 +00:00
|
|
|
ThrowCompletionOr<bool> set_length(PropertyDescriptor const&);
|
2021-07-07 00:50:19 +00:00
|
|
|
|
|
|
|
bool m_length_writable { true };
|
2020-03-20 19:29:57 +00:00
|
|
|
};
|
|
|
|
|
2023-03-20 16:24:48 +00:00
|
|
|
enum class Holes {
|
|
|
|
SkipHoles,
|
|
|
|
ReadThroughHoles,
|
|
|
|
};
|
|
|
|
|
2024-11-14 15:01:23 +00:00
|
|
|
ThrowCompletionOr<GC::MarkedVector<Value>> sort_indexed_properties(VM&, Object const&, size_t length, Function<ThrowCompletionOr<double>(Value, Value)> const& sort_compare, Holes holes);
|
2022-08-21 16:54:08 +00:00
|
|
|
ThrowCompletionOr<double> compare_array_elements(VM&, Value x, Value y, FunctionObject* comparefn);
|
2022-06-13 06:53:58 +00:00
|
|
|
|
2020-03-20 19:29:57 +00:00
|
|
|
}
|