diff --git a/boa/src/builtins/string/mod.rs b/boa/src/builtins/string/mod.rs index 67109cc48a..d332f852f6 100644 --- a/boa/src/builtins/string/mod.rs +++ b/boa/src/builtins/string/mod.rs @@ -35,13 +35,16 @@ use std::{ pub fn make_string(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { // This value is used by console.log and other routines to match Obexpecty"failed to parse argument for String method"pe // to its Javascript Identifier (global constructor method name) + let s = args + .get(0) + .expect("failed to get StringData for make_string()") + .clone(); + let length_str = s.to_string().chars().count(); + + this.set_field_slice("length", Value::from(length_str as i32)); + this.set_kind(ObjectKind::String); - this.set_internal_slot( - "StringData", - args.get(0) - .expect("failed to get StringData for make_string()") - .clone(), - ); + this.set_internal_slot("StringData", s); let arg = match args.get(0) { Some(v) => v.clone(), diff --git a/boa/src/builtins/string/tests.rs b/boa/src/builtins/string/tests.rs index e5f8d21fbe..52bdba3efa 100644 --- a/boa/src/builtins/string/tests.rs +++ b/boa/src/builtins/string/tests.rs @@ -36,6 +36,32 @@ fn check_string_constructor_is_function() { // assert_eq!(d, String::from("4")); // } +#[test] +fn new_string_has_length() { + let realm = Realm::create(); + let mut engine = Executor::new(realm); + let init = r#" + let a = new String("1234"); + a + "#; + + forward(&mut engine, init); + assert_eq!(forward(&mut engine, "a.length"), "4"); +} + +#[test] +fn new_utf8_string_has_length() { + let realm = Realm::create(); + let mut engine = Executor::new(realm); + let init = r#" + let a = new String("中文"); + a + "#; + + forward(&mut engine, init); + assert_eq!(forward(&mut engine, "a.length"), "2"); +} + #[test] fn concat() { let realm = Realm::create();