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

LibJS: Add %TypedArray%.prototype.toLocaleString

This commit is contained in:
Idan Horowitz 2021-07-09 23:11:14 +03:00 committed by Linus Groh
parent 56d8098d13
commit a44de7a55f
3 changed files with 71 additions and 0 deletions

View file

@ -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());
}
}

View file

@ -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);
};
}