mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 20:47:43 +00:00
LibJS: Add String.fromCharCode()
This commit is contained in:
parent
8c05e78b6c
commit
e33820b557
3 changed files with 42 additions and 1 deletions
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,6 +43,7 @@ private:
|
|||
virtual const char* class_name() const override { return "StringConstructor"; }
|
||||
|
||||
static Value raw(Interpreter&);
|
||||
static Value from_char_code(Interpreter&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
23
Libraries/LibJS/Tests/String.fromCharCode.js
Normal file
23
Libraries/LibJS/Tests/String.fromCharCode.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
load("test-common.js");
|
||||
|
||||
try {
|
||||
assert(String.fromCharCode.length === 1);
|
||||
|
||||
assert(String.fromCharCode() === "");
|
||||
assert(String.fromCharCode(0) === "\u0000");
|
||||
assert(String.fromCharCode(false) === "\u0000");
|
||||
assert(String.fromCharCode(null) === "\u0000");
|
||||
assert(String.fromCharCode(undefined) === "\u0000");
|
||||
assert(String.fromCharCode(1) === "\u0001");
|
||||
assert(String.fromCharCode(true) === "\u0001");
|
||||
assert(String.fromCharCode(-1) === "\uffff");
|
||||
assert(String.fromCharCode(0xffff) === "\uffff");
|
||||
assert(String.fromCharCode(0x123ffff) === "\uffff");
|
||||
assert(String.fromCharCode(65) === "A");
|
||||
assert(String.fromCharCode(65, 66, 67) === "ABC");
|
||||
assert(String.fromCharCode(228, 246, 252) === "äöü");
|
||||
|
||||
console.log("PASS");
|
||||
} catch (e) {
|
||||
console.log("FAIL: " + e);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue