mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:07:45 +00:00
LibJS: Add spec comments to String.prototype.at
This commit is contained in:
parent
8701832095
commit
650d2fdc2d
1 changed files with 11 additions and 1 deletions
|
@ -774,23 +774,33 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::last_index_of)
|
||||||
// 22.1.3.1 String.prototype.at ( index ), https://tc39.es/ecma262/#sec-string.prototype.at
|
// 22.1.3.1 String.prototype.at ( index ), https://tc39.es/ecma262/#sec-string.prototype.at
|
||||||
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::at)
|
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::at)
|
||||||
{
|
{
|
||||||
|
// 1. Let O be ? ToObject(this value).
|
||||||
auto string = TRY(utf16_string_from(vm));
|
auto string = TRY(utf16_string_from(vm));
|
||||||
|
// 2. Let len be ? LengthOfArrayLike(O).
|
||||||
auto length = string.length_in_code_units();
|
auto length = string.length_in_code_units();
|
||||||
|
|
||||||
|
// 3. Let relativeIndex be ? ToIntegerOrInfinity(index).
|
||||||
auto relative_index = TRY(vm.argument(0).to_integer_or_infinity(vm));
|
auto relative_index = TRY(vm.argument(0).to_integer_or_infinity(vm));
|
||||||
if (Value(relative_index).is_infinity())
|
if (Value(relative_index).is_infinity())
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
|
|
||||||
Checked<size_t> index { 0 };
|
Checked<size_t> index { 0 };
|
||||||
|
// 4. If relativeIndex ≥ 0, then
|
||||||
if (relative_index >= 0) {
|
if (relative_index >= 0) {
|
||||||
|
// a. Let k be relativeIndex.
|
||||||
index += relative_index;
|
index += relative_index;
|
||||||
} else {
|
}
|
||||||
|
// 5. Else,
|
||||||
|
else {
|
||||||
|
// a. Let k be len + relativeIndex.
|
||||||
index += length;
|
index += length;
|
||||||
index -= -relative_index;
|
index -= -relative_index;
|
||||||
}
|
}
|
||||||
|
// 6. If k < 0 or k ≥ len, return undefined.
|
||||||
if (index.has_overflow() || index.value() >= length)
|
if (index.has_overflow() || index.value() >= length)
|
||||||
return js_undefined();
|
return js_undefined();
|
||||||
|
|
||||||
|
// 7. Return ? Get(O, ! ToString(𝔽(k))).
|
||||||
return js_string(vm, string.substring_view(index.value(), 1));
|
return js_string(vm, string.substring_view(index.value(), 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue