mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:57:35 +00:00
LibJS: Add %TypedArray%.prototype.lastIndexOf
This commit is contained in:
parent
6343bfa9d7
commit
f0abcde00c
3 changed files with 94 additions and 0 deletions
|
@ -36,6 +36,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
|
||||||
define_native_function(vm.names.forEach, for_each, 1, attr);
|
define_native_function(vm.names.forEach, for_each, 1, attr);
|
||||||
define_native_function(vm.names.includes, includes, 1, attr);
|
define_native_function(vm.names.includes, includes, 1, attr);
|
||||||
define_native_function(vm.names.indexOf, index_of, 1, attr);
|
define_native_function(vm.names.indexOf, index_of, 1, attr);
|
||||||
|
define_native_function(vm.names.lastIndexOf, last_index_of, 1, attr);
|
||||||
define_native_function(vm.names.some, some, 1, attr);
|
define_native_function(vm.names.some, some, 1, attr);
|
||||||
define_native_function(vm.names.join, join, 1, attr);
|
define_native_function(vm.names.join, join, 1, attr);
|
||||||
define_native_function(vm.names.keys, keys, 0, attr);
|
define_native_function(vm.names.keys, keys, 0, attr);
|
||||||
|
@ -355,6 +356,54 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::index_of)
|
||||||
return Value(-1);
|
return Value(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 23.2.3.17 %TypedArray%.prototype.lastIndexOf ( searchElement [ , fromIndex ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.lastindexof
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::last_index_of)
|
||||||
|
{
|
||||||
|
auto typed_array = typed_array_from(vm, global_object);
|
||||||
|
if (!typed_array)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto length = typed_array->array_length();
|
||||||
|
|
||||||
|
if (length == 0)
|
||||||
|
return Value(-1);
|
||||||
|
|
||||||
|
double n;
|
||||||
|
if (vm.argument_count() > 1) {
|
||||||
|
n = vm.argument(1).to_integer_or_infinity(global_object);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
} else {
|
||||||
|
n = length - 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Value(n).is_negative_infinity())
|
||||||
|
return Value(-1);
|
||||||
|
|
||||||
|
i32 k;
|
||||||
|
if (n >= 0) {
|
||||||
|
k = min(n, (i32)length - 1);
|
||||||
|
} else {
|
||||||
|
auto relative_k = length + n;
|
||||||
|
if (relative_k < 0) // ensures we dont underflow `k`
|
||||||
|
relative_k = -1;
|
||||||
|
k = relative_k;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto search_element = vm.argument(0);
|
||||||
|
for (; k >= 0; --k) {
|
||||||
|
auto k_present = typed_array->has_property(k);
|
||||||
|
if (k_present) {
|
||||||
|
auto element_k = typed_array->get(k);
|
||||||
|
|
||||||
|
if (strict_eq(search_element, element_k))
|
||||||
|
return Value(k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Value(-1);
|
||||||
|
}
|
||||||
|
|
||||||
// 23.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
|
// 23.2.3.25 %TypedArray%.prototype.some ( callbackfn [ , thisArg ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.some
|
||||||
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::some)
|
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::some)
|
||||||
{
|
{
|
||||||
|
|
|
@ -33,6 +33,7 @@ private:
|
||||||
JS_DECLARE_NATIVE_FUNCTION(for_each);
|
JS_DECLARE_NATIVE_FUNCTION(for_each);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(includes);
|
JS_DECLARE_NATIVE_FUNCTION(includes);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(index_of);
|
JS_DECLARE_NATIVE_FUNCTION(index_of);
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(last_index_of);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(some);
|
JS_DECLARE_NATIVE_FUNCTION(some);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(join);
|
JS_DECLARE_NATIVE_FUNCTION(join);
|
||||||
JS_DECLARE_NATIVE_FUNCTION(keys);
|
JS_DECLARE_NATIVE_FUNCTION(keys);
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
const TYPED_ARRAYS = [
|
||||||
|
Uint8Array,
|
||||||
|
Uint8ClampedArray,
|
||||||
|
Uint16Array,
|
||||||
|
Uint32Array,
|
||||||
|
Int8Array,
|
||||||
|
Int16Array,
|
||||||
|
Int32Array,
|
||||||
|
Float32Array,
|
||||||
|
Float64Array,
|
||||||
|
];
|
||||||
|
|
||||||
|
const BIGINT_TYPED_ARRAYS = [BigUint64Array, BigInt64Array];
|
||||||
|
|
||||||
|
test("basic functionality", () => {
|
||||||
|
TYPED_ARRAYS.forEach(T => {
|
||||||
|
expect(T.prototype.lastIndexOf).toHaveLength(1);
|
||||||
|
|
||||||
|
const typedArray = new T(3);
|
||||||
|
typedArray[0] = 1;
|
||||||
|
typedArray[1] = 2;
|
||||||
|
typedArray[2] = 2;
|
||||||
|
|
||||||
|
expect(typedArray.lastIndexOf(2)).toBe(2);
|
||||||
|
expect(typedArray.lastIndexOf(-1)).toBe(-1);
|
||||||
|
expect(typedArray.lastIndexOf(Infinity)).toBe(-1);
|
||||||
|
expect(typedArray.lastIndexOf(2, 2)).toBe(2);
|
||||||
|
expect(typedArray.lastIndexOf(2, -2)).toBe(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
BIGINT_TYPED_ARRAYS.forEach(T => {
|
||||||
|
expect(T.prototype.lastIndexOf).toHaveLength(1);
|
||||||
|
|
||||||
|
const typedArray = new T(3);
|
||||||
|
typedArray[0] = 1n;
|
||||||
|
typedArray[1] = 2n;
|
||||||
|
typedArray[2] = 2n;
|
||||||
|
|
||||||
|
expect(typedArray.lastIndexOf(2n)).toBe(2);
|
||||||
|
expect(typedArray.lastIndexOf(-1n)).toBe(-1);
|
||||||
|
expect(typedArray.lastIndexOf(2n, 2)).toBe(2);
|
||||||
|
expect(typedArray.lastIndexOf(2n, -2)).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue