From d798566377d835e46f362e558beaceccda736df1 Mon Sep 17 00:00:00 2001 From: "Elijah Caine M. Voigt" Date: Thu, 3 Oct 2019 12:05:16 -0700 Subject: [PATCH] unassigned var is undefined (#125) * Unassigned variables are set to `undefined` not `null` Fixes #113 * Rust tests for `var x` and `let x` default to undefined * CHANGELOG for issue #113 fix + add tests/js/test.js to gitignore. --- .gitignore | 9 ++++++++- CHANGELOG.md | 5 +++++ src/lib/exec.rs | 33 +++++++++++++++++++++++++++++++-- tests/js/test.js | 4 ++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index b7bb3b00d4..6241b9fa41 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,10 @@ .idea/ *.iml +# Vim +.*.swp +.*.swo + # Build target dist @@ -9,4 +13,7 @@ dist node_modules .DS_Store yarn-error.log -.vscode/settings.json \ No newline at end of file +.vscode/settings.json + +# tests/js/test.js is used for testing changes locally +test/js/test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 7056115e1e..a26f0a5996 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,11 @@ Feature enhancements: - [FEATURE #119](https://github.com/jasonwilliams/boa/issues/119): Introduce realm struct to hold realm context and global object +Bug fixes: + +- [BUG #113](https://github.com/jasonwilliams/boa/issues/113): + Unassigned variables have default of undefined (@pop) + # 0.4.0 (2019-09-25) v0.4.0 brings quite a big release. The biggest feature to land is the support of regular expressions. diff --git a/src/lib/exec.rs b/src/lib/exec.rs index e391bd4897..ceb7d9e07e 100644 --- a/src/lib/exec.rs +++ b/src/lib/exec.rs @@ -389,7 +389,7 @@ impl Executor for Interpreter { let (name, value) = var.clone(); let val = match value { Some(v) => self.run(&v)?, - None => Gc::new(ValueData::Null), + None => Gc::new(ValueData::Undefined), }; self.realm .environment @@ -403,7 +403,7 @@ impl Executor for Interpreter { let (name, value) = var.clone(); let val = match value { Some(v) => self.run(&v)?, - None => Gc::new(ValueData::Null), + None => Gc::new(ValueData::Undefined), }; self.realm .environment @@ -630,3 +630,32 @@ impl Interpreter { } } } + +#[cfg(test)] +mod tests { + use crate::exec; + + #[test] + fn empty_let_decl_undefined() { + let scenario = r#" + let a; + a == undefined; + "#; + + let pass = String::from("true"); + + assert_eq!(exec(scenario), pass); + } + + #[test] + fn empty_var_decl_undefined() { + let scenario = r#" + let b; + b == undefined; + "#; + + let pass = String::from("true"); + + assert_eq!(exec(scenario), pass); + } +} diff --git a/tests/js/test.js b/tests/js/test.js index c990b090bb..ae107b0f1e 100644 --- a/tests/js/test.js +++ b/tests/js/test.js @@ -1,2 +1,6 @@ +/// +// Use this file to test your changes to boa. +/// + let a = Boolean(0); typeof a;