mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +00:00
LibJS: Add %TypedArray%.prototype.toLocaleString
This commit is contained in:
parent
56d8098d13
commit
a44de7a55f
Notes:
sideshowbarker
2024-07-18 09:28:14 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/a44de7a55f0 Pull-request: https://github.com/SerenityOS/serenity/pull/8592 Reviewed-by: https://github.com/linusg ✅
3 changed files with 71 additions and 0 deletions
|
@ -53,6 +53,7 @@ void TypedArrayPrototype::initialize(GlobalObject& object)
|
|||
define_native_function(vm.names.copyWithin, copy_within, 2, attr);
|
||||
define_native_function(vm.names.filter, filter, 1, attr);
|
||||
define_native_function(vm.names.map, map, 1, attr);
|
||||
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
|
||||
|
||||
define_native_accessor(*vm.well_known_symbol_to_string_tag(), to_string_tag_getter, nullptr, Attribute::Configurable);
|
||||
|
||||
|
@ -1452,4 +1453,36 @@ JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::map)
|
|||
return return_array;
|
||||
}
|
||||
|
||||
// 23.2.3.28 %TypedArray%.prototype.toLocaleString ( [ reserved1 [ , reserved2 ] ] ), https://tc39.es/ecma262/#sec-%typedarray%.prototype.tolocalestring
|
||||
JS_DEFINE_NATIVE_FUNCTION(TypedArrayPrototype::to_locale_string)
|
||||
{
|
||||
auto* typed_array = validate_typed_array(global_object);
|
||||
if (!typed_array)
|
||||
return {};
|
||||
|
||||
auto length = typed_array->array_length();
|
||||
|
||||
StringBuilder builder;
|
||||
for (u32 k = 0; k < length; ++k) {
|
||||
if (k > 0)
|
||||
builder.append(','); // NOTE: Until we implement ECMA-402 (Intl) this is implementation specific.
|
||||
auto value = typed_array->get(k);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
if (value.is_nullish())
|
||||
continue;
|
||||
auto* value_object = value.to_object(global_object);
|
||||
if (!value_object)
|
||||
return {};
|
||||
auto locale_string_result = value_object->invoke(vm.names.toLocaleString);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
auto string = locale_string_result.to_string(global_object);
|
||||
if (vm.exception())
|
||||
return {};
|
||||
builder.append(string);
|
||||
}
|
||||
return js_string(vm, builder.to_string());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -49,6 +49,7 @@ private:
|
|||
JS_DECLARE_NATIVE_FUNCTION(copy_within);
|
||||
JS_DECLARE_NATIVE_FUNCTION(filter);
|
||||
JS_DECLARE_NATIVE_FUNCTION(map);
|
||||
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
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.toLocaleString).toHaveLength(0);
|
||||
|
||||
const typedArray = new T(3);
|
||||
typedArray[0] = 1;
|
||||
typedArray[1] = 2;
|
||||
typedArray[2] = 3;
|
||||
|
||||
expect(typedArray.toLocaleString()).toBe("1,2,3");
|
||||
});
|
||||
|
||||
BIGINT_TYPED_ARRAYS.forEach(T => {
|
||||
expect(T.prototype.toLocaleString).toLocaleString(0);
|
||||
|
||||
const typedArray = new T(3);
|
||||
typedArray[0] = 1n;
|
||||
typedArray[1] = 2n;
|
||||
typedArray[2] = 3n;
|
||||
|
||||
expect(typedArray.toLocaleString()).toBe("1,2,3");
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue