summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSimon Rask <simon.ras.and@gmail.com>2022-10-13 20:57:25 +0200
committerLinus Groh <mail@linusgroh.de>2022-10-13 21:14:32 +0200
commit650d2fdc2d193013ea91de2b9f0f2c528b33f8e7 (patch)
treebe05cf3ba7d9ff09b8c8f124dfa958d7634599f3 /Userland
parent8701832095edc1eca17ca776954efe35b5e0ce7c (diff)
downloadserenity-650d2fdc2d193013ea91de2b9f0f2c528b33f8e7.zip
LibJS: Add spec comments to String.prototype.at
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/StringPrototype.cpp12
1 files changed, 11 insertions, 1 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
index d43e41429f..371237d949 100644
--- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
@@ -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
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::at)
{
+ // 1. Let O be ? ToObject(this value).
auto string = TRY(utf16_string_from(vm));
+ // 2. Let len be ? LengthOfArrayLike(O).
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));
if (Value(relative_index).is_infinity())
return js_undefined();
Checked<size_t> index { 0 };
+ // 4. If relativeIndex ≥ 0, then
if (relative_index >= 0) {
+ // a. Let k be relativeIndex.
index += relative_index;
- } else {
+ }
+ // 5. Else,
+ else {
+ // a. Let k be len + relativeIndex.
index += length;
index -= -relative_index;
}
+ // 6. If k < 0 or k ≥ len, return undefined.
if (index.has_overflow() || index.value() >= length)
return js_undefined();
+ // 7. Return ? Get(O, ! ToString(𝔽(k))).
return js_string(vm, string.substring_view(index.value(), 1));
}