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