From 9deeef06b5fcd6bb843f4f898b859e1a14ede7f2 Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Mon, 28 Sep 2020 21:28:43 +0100 Subject: [PATCH 1/3] - Set a length on new String objects - Add a test --- boa/src/exec/tests.rs | 9 +++++++++ boa/src/value/mod.rs | 8 ++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index 246fbe6c59a..770e357ac90 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -33,6 +33,15 @@ fn property_accessor_member_expression_bracket_notation_on_string_literal() { assert_eq!(&exec(scenario), "\"function\""); } +#[test] +fn length_correct_value_on_string_literal() { + let scenario = r#" + 'hello'.length; + "#; + + assert_eq!(&exec(scenario), "5"); +} + #[test] fn property_accessor_member_expression_dot_notation_on_function() { let scenario = r#" diff --git a/boa/src/value/mod.rs b/boa/src/value/mod.rs index 93826e5322d..da540258b18 100644 --- a/boa/src/value/mod.rs +++ b/boa/src/value/mod.rs @@ -713,10 +713,10 @@ impl Value { .expect("String was not initialized") .get_field(PROTOTYPE); - Ok(GcObject::new(Object::with_prototype( - proto, - ObjectData::String(string.clone()), - ))) + let mut obj = Object::with_prototype(proto, ObjectData::String(string.clone())); + // Make sure the correct length is set on our new string object + obj.set(PropertyKey::from("length"), string.len().into()); + Ok(GcObject::new(obj)) } Value::Symbol(ref symbol) => { let proto = ctx From 3c61caecc6bed63d897a0cf252a87c4823725e5b Mon Sep 17 00:00:00 2001 From: jasonwilliams Date: Mon, 28 Sep 2020 21:39:25 +0100 Subject: [PATCH 2/3] change to chars().count() --- boa/src/value/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/value/mod.rs b/boa/src/value/mod.rs index da540258b18..66e5fb79277 100644 --- a/boa/src/value/mod.rs +++ b/boa/src/value/mod.rs @@ -715,7 +715,7 @@ impl Value { let mut obj = Object::with_prototype(proto, ObjectData::String(string.clone())); // Make sure the correct length is set on our new string object - obj.set(PropertyKey::from("length"), string.len().into()); + obj.set(PropertyKey::from("length"), string.chars().count().into()); Ok(GcObject::new(obj)) } Value::Symbol(ref symbol) => { From 5523e484ef8d6e95bcac3702a65469e2b4e9f528 Mon Sep 17 00:00:00 2001 From: Jason Williams <936006+jasonwilliams@users.noreply.github.com> Date: Mon, 28 Sep 2020 22:14:04 +0100 Subject: [PATCH 3/3] Update boa/src/value/mod.rs Co-authored-by: Halid Odat --- boa/src/value/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/value/mod.rs b/boa/src/value/mod.rs index 66e5fb79277..cb3d53e3628 100644 --- a/boa/src/value/mod.rs +++ b/boa/src/value/mod.rs @@ -715,7 +715,7 @@ impl Value { let mut obj = Object::with_prototype(proto, ObjectData::String(string.clone())); // Make sure the correct length is set on our new string object - obj.set(PropertyKey::from("length"), string.chars().count().into()); + obj.set("length".into(), string.chars().count().into()); Ok(GcObject::new(obj)) } Value::Symbol(ref symbol) => {