mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 13:07:46 +00:00
LibJS: Add the String.fromCodePoint() method
This commit is contained in:
parent
89855d0b8c
commit
2299be474b
5 changed files with 63 additions and 0 deletions
|
@ -128,6 +128,7 @@ namespace JS {
|
|||
P(freeze) \
|
||||
P(from) \
|
||||
P(fromCharCode) \
|
||||
P(fromCodePoint) \
|
||||
P(fromEntries) \
|
||||
P(fround) \
|
||||
P(gc) \
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
M(InOperatorWithObject, "'in' operator must be used on an object") \
|
||||
M(InstanceOfOperatorBadPrototype, "'prototype' property of {} is not an object") \
|
||||
M(InvalidAssignToConst, "Invalid assignment to const variable") \
|
||||
M(InvalidCodePoint, "Invalid code point {}, must be an integer no less than 0 and no greater than 0x10FFFF") \
|
||||
M(InvalidHint, "Invalid hint: \"{}\"") \
|
||||
M(InvalidIndex, "Index must be a positive integer") \
|
||||
M(InvalidLeftHandAssignment, "Invalid left-hand side in assignment") \
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,6 +26,7 @@ private:
|
|||
|
||||
JS_DECLARE_NATIVE_FUNCTION(raw);
|
||||
JS_DECLARE_NATIVE_FUNCTION(from_char_code);
|
||||
JS_DECLARE_NATIVE_FUNCTION(from_code_point);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue