From 8cb43aa27b0a6c00adebb002e1457cf33b5a3983 Mon Sep 17 00:00:00 2001 From: Alexander Kryvomaz Date: Wed, 6 May 2020 21:19:31 +0300 Subject: [PATCH 1/4] implement for loop execution --- boa/src/exec/mod.rs | 18 ++++++++++++++++++ boa/src/exec/tests.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/boa/src/exec/mod.rs b/boa/src/exec/mod.rs index f3ed0040fb4..ef11ee3613f 100644 --- a/boa/src/exec/mod.rs +++ b/boa/src/exec/mod.rs @@ -173,6 +173,24 @@ impl Executor for Interpreter { } Ok(result) } + Node::ForLoop(ref init, ref cond, ref step, ref body) => { + if let Some(init) = init { + self.run(init)?; + } + + while match cond { + Some(cond) => self.run(cond)?.borrow().is_true(), + None => true, + } { + self.run(body)?; + + if let Some(step) = step { + self.run(step)?; + } + } + + Ok(Gc::new(ValueData::Undefined)) + } Node::If(ref cond, ref expr, None) => Ok(if self.run(cond)?.borrow().is_true() { self.run(expr)? } else { diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index 720cf8c563b..fc61b27de45 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -304,6 +304,44 @@ fn test_do_while_post_inc() { assert_eq!(exec(with_post_incrementors), String::from("11")); } +#[test] +fn test_for_loop() { + let simple = r#" + let a = 0; + for (let i = 0; i < 10; i = i + 1) { + a = a + i; + } + + a + "#; + assert_eq!(exec(simple), String::from("45")); + + let without_init_and_inc_step = r#" + let a = 0; + let i = 0; + for (;i < 10;) { + a = a + i; + i = i + 1; + } + + a + "#; + assert_eq!(exec(without_init_and_inc_step), String::from("45")); + + let body_should_not_execute_on_false_condition = r#" + let a = 0 + for (;false;) { + a = a + 1; + } + + a + "#; + assert_eq!( + exec(body_should_not_execute_on_false_condition), + String::from("0") + ); +} + #[test] #[ignore] fn test_unary_pre() { From 10ce2ddbfd9d2bf65dc03a22f2f9f5fd9d9d5612 Mon Sep 17 00:00:00 2001 From: Alexander Kryvomaz Date: Wed, 6 May 2020 21:37:29 +0300 Subject: [PATCH 2/4] for loop benchmark --- boa/benches/exec.rs | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/boa/benches/exec.rs b/boa/benches/exec.rs index ff564aac0f9..60de7dbe7b2 100644 --- a/boa/benches/exec.rs +++ b/boa/benches/exec.rs @@ -26,30 +26,25 @@ fn symbol_creation(c: &mut Criterion) { }); } -// TODO: implement for loops. -// static FOR_LOOP: &str = r#" -// let a = 10; -// let b = "hello"; -// for (;;) { -// a += 5; +static FOR_LOOP: &str = r#" +let a = 10; +let b = "hello"; +for (;a > 100;) { + a += 5; -// if (a < 50) { -// b += "world"; -// } + if (a < 50) { + b += "world"; + } +} -// if (a > 100) { -// break; -// } -// } -// let c = a; -// let d = b; -// "#; +b +"#; -// fn for_loop_execution(c: &mut Criterion) { -// c.bench_function("For loop (Execution)", move |b| { -// b.iter(|| exec(black_box(FOR_LOOP))) -// }); -// } +fn for_loop_execution(c: &mut Criterion) { + c.bench_function("For loop (Execution)", move |b| { + b.iter(|| exec(black_box(FOR_LOOP))) + }); +} static FIBONACCI: &str = r#" let num = 12; @@ -74,7 +69,7 @@ criterion_group!( execution, create_realm, symbol_creation, - // for_loop_execution, + for_loop_execution, fibonacci ); criterion_main!(execution); From b636d6c3aec52cca77ba54ee6aa55da3bcb79b6a Mon Sep 17 00:00:00 2001 From: Alexander Kryvomaz Date: Thu, 7 May 2020 12:15:01 +0300 Subject: [PATCH 3/4] add more for loop tests --- boa/src/exec/tests.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index fc61b27de45..060c10f1d92 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -307,14 +307,15 @@ fn test_do_while_post_inc() { #[test] fn test_for_loop() { let simple = r#" - let a = 0; - for (let i = 0; i < 10; i = i + 1) { - a = a + i; + const a = ['h', 'e', 'l', 'l', 'o']; + let b = ''; + for (let i = 0; i < a.length; i = i + 1) { + b = b + a[i]; } - - a + + b "#; - assert_eq!(exec(simple), String::from("45")); + assert_eq!(exec(simple), String::from("hello")); let without_init_and_inc_step = r#" let a = 0; @@ -340,6 +341,13 @@ fn test_for_loop() { exec(body_should_not_execute_on_false_condition), String::from("0") ); + + let inner_scope = r#" + for (let i = 0;false;) {} + + i + "#; + assert_eq!(exec(inner_scope), String::from("undefined")); } #[test] From 963c6271dba49704649db0f8361baea64a106abd Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Thu, 7 May 2020 12:02:33 +0200 Subject: [PATCH 4/4] Update boa/src/exec/tests.rs --- boa/src/exec/tests.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/boa/src/exec/tests.rs b/boa/src/exec/tests.rs index 060c10f1d92..8c5f57901d3 100644 --- a/boa/src/exec/tests.rs +++ b/boa/src/exec/tests.rs @@ -312,7 +312,6 @@ fn test_for_loop() { for (let i = 0; i < a.length; i = i + 1) { b = b + a[i]; } - b "#; assert_eq!(exec(simple), String::from("hello"));