mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:57:45 +00:00
LibJS: Add spec comments to TypedArray.prototype.includes
This commit is contained in:
parent
43456ad708
commit
34f27f76ec
1 changed files with 23 additions and 1 deletions
|
@ -604,39 +604,61 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::for_each)
|
||||||
// 23.2.3.16 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes
|
// 23.2.3.16 %TypedArray%.prototype.includes ( searchElement [ , fromIndex ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.includes
|
||||||
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::includes)
|
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::includes)
|
||||||
{
|
{
|
||||||
|
// 1. Let O be the this value.
|
||||||
|
// 2. Perform ? ValidateTypedArray(O).
|
||||||
auto typed_array = TRY(validate_typed_array_from_this(vm));
|
auto typed_array = TRY(validate_typed_array_from_this(vm));
|
||||||
|
|
||||||
|
// 3. Let len be O.[[ArrayLength]].
|
||||||
auto length = typed_array->array_length();
|
auto length = typed_array->array_length();
|
||||||
|
|
||||||
|
// 4. If len is 0, return false.
|
||||||
if (length == 0)
|
if (length == 0)
|
||||||
return Value(false);
|
return Value(false);
|
||||||
|
|
||||||
|
// 5. Let n be ? ToIntegerOrInfinity(fromIndex).
|
||||||
auto n = TRY(vm.argument(1).to_integer_or_infinity(vm));
|
auto n = TRY(vm.argument(1).to_integer_or_infinity(vm));
|
||||||
|
|
||||||
|
// FIXME: 6. Assert: If fromIndex is undefined, then n is 0.
|
||||||
|
|
||||||
auto value_n = Value(n);
|
auto value_n = Value(n);
|
||||||
|
// 7. If n is +∞, return false.
|
||||||
if (value_n.is_positive_infinity())
|
if (value_n.is_positive_infinity())
|
||||||
return Value(false);
|
return Value(false);
|
||||||
|
// 8. Else if n is -∞, set n to 0.
|
||||||
else if (value_n.is_negative_infinity())
|
else if (value_n.is_negative_infinity())
|
||||||
n = 0;
|
n = 0;
|
||||||
|
|
||||||
u32 k;
|
u32 k;
|
||||||
|
// 9. If n ≥ 0, then
|
||||||
if (n >= 0) {
|
if (n >= 0) {
|
||||||
|
// a. Let k be n.
|
||||||
k = n;
|
k = n;
|
||||||
} else {
|
}
|
||||||
|
// 10. Else,
|
||||||
|
else {
|
||||||
|
// a. Let k be len + n.
|
||||||
auto relative_k = length + n;
|
auto relative_k = length + n;
|
||||||
|
|
||||||
|
// b. If k < 0, set k to 0.
|
||||||
if (relative_k < 0)
|
if (relative_k < 0)
|
||||||
relative_k = 0;
|
relative_k = 0;
|
||||||
k = relative_k;
|
k = relative_k;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto search_element = vm.argument(0);
|
auto search_element = vm.argument(0);
|
||||||
|
// 11. Repeat, while k < len,
|
||||||
for (; k < length; ++k) {
|
for (; k < length; ++k) {
|
||||||
|
// a. Let elementK be ! Get(O, ! ToString(𝔽(k))).
|
||||||
auto element_k = MUST(typed_array->get(k));
|
auto element_k = MUST(typed_array->get(k));
|
||||||
|
|
||||||
|
// b. If SameValueZero(searchElement, elementK) is true, return true.
|
||||||
if (same_value_zero(search_element, element_k))
|
if (same_value_zero(search_element, element_k))
|
||||||
return Value(true);
|
return Value(true);
|
||||||
|
|
||||||
|
// c. Set k to k + 1.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 12. Return false.
|
||||||
return Value(false);
|
return Value(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue