Browse Source

Fix for 0 length new String (#404)

* Fix for 0 length field when constructing a new String.

* String.length uses character count not byte count. Also, corresponding test

* Made tests more succinct per suggestion.
pull/407/head
Tyler Morten 5 years ago committed by GitHub
parent
commit
d84d9cbe2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      boa/src/builtins/string/mod.rs
  2. 26
      boa/src/builtins/string/tests.rs

15
boa/src/builtins/string/mod.rs

@ -35,13 +35,16 @@ use std::{
pub fn make_string(this: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue { 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 // 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) // to its Javascript Identifier (global constructor method name)
this.set_kind(ObjectKind::String); let s = args
this.set_internal_slot( .get(0)
"StringData",
args.get(0)
.expect("failed to get StringData for make_string()") .expect("failed to get StringData for make_string()")
.clone(), .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", s);
let arg = match args.get(0) { let arg = match args.get(0) {
Some(v) => v.clone(), Some(v) => v.clone(),

26
boa/src/builtins/string/tests.rs

@ -36,6 +36,32 @@ fn check_string_constructor_is_function() {
// assert_eq!(d, String::from("4")); // 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] #[test]
fn concat() { fn concat() {
let realm = Realm::create(); let realm = Realm::create();

Loading…
Cancel
Save