Skip to content

Commit

Permalink
Change default return type from null to undefined (#643)
Browse files Browse the repository at this point in the history
* Change default return type from null to undefined

* Change default return value of block

* Add link to spec

* Add tests
  • Loading branch information
54k1 authored Aug 19, 2020
1 parent c9afbf4 commit 2d1dabb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
4 changes: 3 additions & 1 deletion boa/src/exec/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ impl Executable for Block {
)));
}

let mut obj = Value::null();
// https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
// The return value is uninitialized, which means it defaults to Value::Undefined
let mut obj = Value::default();
for statement in self.statements() {
obj = statement.run(interpreter)?;

Expand Down
5 changes: 4 additions & 1 deletion boa/src/exec/statement_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use crate::{builtins::value::Value, syntax::ast::node::StatementList, BoaProfile
impl Executable for StatementList {
fn run(&self, interpreter: &mut Interpreter) -> Result<Value> {
let _timer = BoaProfiler::global().start_event("StatementList", "exec");
let mut obj = Value::null();

// https://tc39.es/ecma262/#sec-block-runtime-semantics-evaluation
// The return value is uninitialized, which means it defaults to Value::Undefined
let mut obj = Value::default();
interpreter.set_current_state(InterpreterState::Executing);
for (i, item) in self.statements().iter().enumerate() {
let val = item.run(interpreter)?;
Expand Down
12 changes: 12 additions & 0 deletions boa/src/exec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ fn function_declaration_returns_undefined() {
assert_eq!(&exec(scenario), "undefined");
}

#[test]
fn empty_function_returns_undefined() {
let scenario = "(function () {}) ()";
assert_eq!(&exec(scenario), "undefined");
}

#[test]
fn property_accessor_member_expression_dot_notation_on_string_literal() {
let scenario = r#"
Expand Down Expand Up @@ -1205,3 +1211,9 @@ fn comma_operator() {
"#;
assert_eq!(&exec(scenario), "2");
}

#[test]
fn test_result_of_empty_block() {
let scenario = "{}";
assert_eq!(&exec(scenario), "undefined");
}

0 comments on commit 2d1dabb

Please sign in to comment.