1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:07:45 +00:00

LibJS: Add the String.fromCodePoint() method

This commit is contained in:
Idan Horowitz 2021-06-16 11:36:23 +03:00 committed by Linus Groh
parent 89855d0b8c
commit 2299be474b
5 changed files with 63 additions and 0 deletions

View file

@ -32,6 +32,7 @@ void StringConstructor::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.raw, raw, 1, attr);
define_native_function(vm.names.fromCharCode, from_char_code, 1, attr);
define_native_function(vm.names.fromCodePoint, from_code_point, 1, attr);
}
StringConstructor::~StringConstructor()
@ -120,4 +121,26 @@ JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_char_code)
return js_string(vm, builder.build());
}
// 22.1.2.2 String.fromCodePoint ( ...codePoints ), https://tc39.es/ecma262/#sec-string.fromcodepoint
JS_DEFINE_NATIVE_FUNCTION(StringConstructor::from_code_point)
{
StringBuilder builder;
for (size_t i = 0; i < vm.argument_count(); ++i) {
auto next_code_point = vm.argument(i).to_number(global_object);
if (vm.exception())
return {};
if (!next_code_point.is_integer()) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
return {};
}
auto code_point = next_code_point.to_i32(global_object);
if (code_point < 0 || code_point > 0x10FFFF) {
vm.throw_exception<RangeError>(global_object, ErrorType::InvalidCodePoint, next_code_point.to_string_without_side_effects());
return {};
}
builder.append_code_point(code_point);
}
return js_string(vm, builder.build());
}
}