From 2a17e24d5b1c1c97f8dea6bc8ee2a21610a29dad Mon Sep 17 00:00:00 2001 From: Jason Williams <936006+jasonwilliams@users.noreply.github.com> Date: Mon, 28 Sep 2020 22:28:10 +0100 Subject: [PATCH] Fix hanging test262 bug by setting the length on String objects (#728) Co-authored-by: Halid Odat --- 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 2e3f3211dc..4927b74ef2 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 93826e5322..cb3d53e362 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("length".into(), string.chars().count().into()); + Ok(GcObject::new(obj)) } Value::Symbol(ref symbol) => { let proto = ctx