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

Implement for loop #374

Merged
merged 4 commits into from
May 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
39 changes: 17 additions & 22 deletions boa/benches/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -74,7 +69,7 @@ criterion_group!(
execution,
create_realm,
symbol_creation,
// for_loop_execution,
for_loop_execution,
fibonacci
);
criterion_main!(execution);
18 changes: 18 additions & 0 deletions boa/src/exec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
38 changes: 38 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down