1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:48:11 +00:00

LibJS: Add String.fromCharCode()

This commit is contained in:
Linus Groh 2020-05-31 00:47:28 +01:00 committed by Andreas Kling
parent 8c05e78b6c
commit e33820b557
3 changed files with 42 additions and 1 deletions

View file

@ -25,6 +25,7 @@
*/
#include <AK/StringBuilder.h>
#include <AK/Utf32View.h>
#include <LibJS/Interpreter.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Error.h>
@ -40,7 +41,9 @@ StringConstructor::StringConstructor()
define_property("prototype", interpreter().global_object().string_prototype(), 0);
define_property("length", Value(1), Attribute::Configurable);
define_native_function("raw", raw, 0, Attribute::Writable | Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function("raw", raw, 0, attr);
define_native_function("fromCharCode", from_char_code, 1, attr);
}
StringConstructor::~StringConstructor()
@ -106,4 +109,18 @@ Value StringConstructor::raw(Interpreter& interpreter)
return js_string(interpreter, builder.build());
}
Value StringConstructor::from_char_code(Interpreter& interpreter)
{
StringBuilder builder;
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
auto char_code = interpreter.argument(i).to_i32(interpreter);
if (interpreter.exception())
return {};
auto truncated = char_code & 0xffff;
// FIXME: We need an Utf16View :^)
builder.append(Utf32View((u32*)&truncated, 1));
}
return js_string(interpreter, builder.build());
}
}