summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-07-19 11:15:18 -0400
committerAndreas Kling <kling@serenityos.org>2021-07-22 09:10:44 +0200
commit48a28a9a7373b7d17bffdadddc9923897a1dce3d (patch)
treef339f789afd15ef4c90a5bf1750eaf95988f8495 /Userland/Libraries
parent5d11614bc7213ad2af173f7c3c39cb5fb8a526ea (diff)
downloadserenity-48a28a9a7373b7d17bffdadddc9923897a1dce3d.zip
LibJS: Implement String.prototype.charCodeAt with UTF-16 code units
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/StringPrototype.cpp11
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js8
2 files changed, 15 insertions, 4 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
index 3d837e167c..6bc35c61df 100644
--- a/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/StringPrototype.cpp
@@ -145,15 +145,18 @@ JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_at)
// 22.1.3.2 String.prototype.charCodeAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.charcodeat
JS_DEFINE_NATIVE_FUNCTION(StringPrototype::char_code_at)
{
- auto string = ak_string_from(vm, global_object);
- if (!string.has_value())
+ auto string = utf16_string_from(vm, global_object);
+ if (vm.exception())
return {};
auto position = vm.argument(0).to_integer_or_infinity(global_object);
if (vm.exception())
return {};
- if (position < 0 || position >= string->length())
+
+ Utf16View utf16_string_view { string };
+ if (position < 0 || position >= utf16_string_view.length_in_code_units())
return js_nan();
- return Value((*string)[position]);
+
+ return Value(utf16_string_view.code_unit_at(position));
}
// 22.1.3.3 String.prototype.codePointAt ( pos ), https://tc39.es/ecma262/#sec-string.prototype.codepointat
diff --git a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js
index e7ebd9d0d3..385d203039 100644
--- a/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js
+++ b/Userland/Libraries/LibJS/Tests/builtins/String/String.prototype.charCodeAt.js
@@ -19,3 +19,11 @@ test("basic functionality", () => {
expect(s.charCodeAt("foo")).toBe(70);
expect(s.charCodeAt(undefined)).toBe(70);
});
+
+test("UTF-16", () => {
+ var s = "😀";
+ expect(s).toHaveLength(2);
+ expect(s.charCodeAt(0)).toBe(0xd83d);
+ expect(s.charCodeAt(1)).toBe(0xde00);
+ expect(s.charCodeAt(2)).toBe(NaN);
+});