Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for 0 length new String #404

Merged
merged 4 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions boa/src/builtins/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
30 changes: 30 additions & 0 deletions boa/src/builtins/string/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ 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
"#;

eprintln!("{}", forward(&mut engine, init));

let empty = String::from("4");
assert_eq!(forward(&mut engine, "a.length"), empty);
Razican marked this conversation as resolved.
Show resolved Hide resolved
}

#[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
"#;

eprintln!("{}", forward(&mut engine, init));

let empty = String::from("2");
assert_eq!(forward(&mut engine, "a.length"), empty);
}

#[test]
fn concat() {
let realm = Realm::create();
Expand Down