Skip to content

Commit

Permalink
JSON.stringify(undefined) should return undefined (#512)
Browse files Browse the repository at this point in the history
  • Loading branch information
n14little authored Jun 19, 2020
1 parent a7cffe3 commit 299a431
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
4 changes: 3 additions & 1 deletion boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ impl Json {
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
pub(crate) fn stringify(_: &mut Value, args: &[Value], ctx: &mut Interpreter) -> ResultValue {
let object = match args.get(0) {
Some(obj) if obj.is_symbol() || obj.is_function() => return Ok(Value::undefined()),
Some(obj) if obj.is_symbol() || obj.is_function() || obj.is_undefined() => {
return Ok(Value::undefined())
}
None => return Ok(Value::undefined()),
Some(obj) => obj,
};
Expand Down
36 changes: 32 additions & 4 deletions boa/src/builtins/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,48 @@ fn json_stringify_function_replacer_propogate_error() {
}

#[test]
fn json_stringify_return_undefined() {
fn json_stringify_function() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let actual_no_args = forward(&mut engine, r#"JSON.stringify()"#);
let actual_function = forward(&mut engine, r#"JSON.stringify(() => {})"#);
let actual_symbol = forward(&mut engine, r#"JSON.stringify(Symbol())"#);
let expected = forward(&mut engine, r#"undefined"#);

assert_eq!(actual_no_args, expected);
assert_eq!(actual_function, expected);
}

#[test]
fn json_stringify_undefined() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);
let actual_undefined = forward(&mut engine, r#"JSON.stringify(undefined)"#);
let expected = forward(&mut engine, r#"undefined"#);

assert_eq!(actual_undefined, expected);
}

#[test]
fn json_stringify_symbol() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let actual_symbol = forward(&mut engine, r#"JSON.stringify(Symbol())"#);
let expected = forward(&mut engine, r#"undefined"#);

assert_eq!(actual_symbol, expected);
}

#[test]
fn json_stringify_no_args() {
let realm = Realm::create();
let mut engine = Interpreter::new(realm);

let actual_no_args = forward(&mut engine, r#"JSON.stringify()"#);
let expected = forward(&mut engine, r#"undefined"#);

assert_eq!(actual_no_args, expected);
}

#[test]
fn json_parse_array_with_reviver() {
let realm = Realm::create();
Expand Down

0 comments on commit 299a431

Please sign in to comment.