1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:47:35 +00:00

LibJS: Add spec comments to TypedArray.prototype.join

This commit is contained in:
Jamie Mansfield 2022-11-19 12:43:26 +00:00 committed by Linus Groh
parent 70a4e4bd67
commit 5341af6225

View file

@ -737,23 +737,44 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::index_of)
// 23.2.3.18 %TypedArray%.prototype.join ( separator ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.join // 23.2.3.18 %TypedArray%.prototype.join ( separator ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.join
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::join) JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::join)
{ {
// 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 separator is undefined, let sep be ",".
// 5. Else, let sep be ? ToString(separator).
String separator = ","; String separator = ",";
if (!vm.argument(0).is_undefined()) if (!vm.argument(0).is_undefined())
separator = TRY(vm.argument(0).to_string(vm)); separator = TRY(vm.argument(0).to_string(vm));
// 6. Let R be the empty String.
StringBuilder builder; StringBuilder builder;
// 7. Let k be 0.
// 8. Repeat, while k < len,
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
// a. If k > 0, set R to the string-concatenation of R and sep.
if (i > 0) if (i > 0)
builder.append(separator); builder.append(separator);
auto value = TRY(typed_array->get(i));
if (value.is_nullish()) // b. Let element be ! Get(O, ! ToString(𝔽(k))).
auto element = TRY(typed_array->get(i));
// c. If element is undefined, let next be the empty String; otherwise, let next be ! ToString(element).
if (element.is_nullish())
continue; continue;
auto string = TRY(value.to_string(vm)); auto next = TRY(element.to_string(vm));
builder.append(string);
// d. Set R to the string-concatenation of R and next.
builder.append(next);
// e. Set k to k + 1.
} }
// 9. Return R.
return js_string(vm, builder.to_string()); return js_string(vm, builder.to_string());
} }