mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:37:35 +00:00
LibJS: Report string length as the code point length, not byte length
For example, U+180E is 3 bytes, but should have a string length of 1.
This commit is contained in:
parent
87848cdf7d
commit
a2e734d202
2 changed files with 9 additions and 1 deletions
|
@ -5,6 +5,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/Utf8View.h>
|
||||||
#include <LibJS/Runtime/AbstractOperations.h>
|
#include <LibJS/Runtime/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/PrimitiveString.h>
|
#include <LibJS/Runtime/PrimitiveString.h>
|
||||||
|
@ -33,7 +34,7 @@ void StringObject::initialize(GlobalObject& global_object)
|
||||||
{
|
{
|
||||||
auto& vm = this->vm();
|
auto& vm = this->vm();
|
||||||
Object::initialize(global_object);
|
Object::initialize(global_object);
|
||||||
define_direct_property(vm.names.length, Value(m_string.string().length()), 0);
|
define_direct_property(vm.names.length, Value(Utf8View(m_string.string()).length()), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void StringObject::visit_edges(Cell::Visitor& visitor)
|
void StringObject::visit_edges(Cell::Visitor& visitor)
|
||||||
|
|
|
@ -7,3 +7,10 @@ test("typeof", () => {
|
||||||
expect(typeof String()).toBe("string");
|
expect(typeof String()).toBe("string");
|
||||||
expect(typeof new String()).toBe("object");
|
expect(typeof new String()).toBe("object");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("length", () => {
|
||||||
|
expect(new String().length).toBe(0);
|
||||||
|
expect(new String("a").length).toBe(1);
|
||||||
|
expect(new String("\u180E").length).toBe(1);
|
||||||
|
expect(new String("\uDBFF\uDFFF").length).toBe(2);
|
||||||
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue