Skip to content

Commit

Permalink
WIP: to_json returns undefined to null
Browse files Browse the repository at this point in the history
  • Loading branch information
n14little committed May 15, 2020
1 parent b88d759 commit dd229aa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
27 changes: 20 additions & 7 deletions boa/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use crate::builtins::{
object::ObjectKind,
property::Property,
value::{ResultValue, Value, ValueData},
};
use crate::exec::Interpreter;
Expand Down Expand Up @@ -66,7 +67,7 @@ pub fn parse(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue
///
/// [spec]: https://tc39.es/ecma262/#sec-json.stringify
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
pub fn stringify(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultValue {
pub fn stringify(_: &mut Value, args: &[Value], interpreter: &mut Interpreter) -> ResultValue {
let object = args.get(0).expect("cannot get argument for JSON.stringify");
if args.len() == 1 {
let json = object.to_json().to_string();
Expand All @@ -77,9 +78,9 @@ pub fn stringify(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVa
if let Some(arg) = args.get(1) {
if let ValueData::Object(ref obj) = arg.data() {
let derefed_obj = (*obj).deref();
let borrowed_derefed_obj = derefed_obj.borrow();
if borrowed_derefed_obj.kind == ObjectKind::Array {
for (key, value) in borrowed_derefed_obj.properties.iter() {
let borrowed_derefed_replacer = derefed_obj.borrow();
if borrowed_derefed_replacer.kind == ObjectKind::Array {
for (key, value) in borrowed_derefed_replacer.properties.iter() {
if let Some(Value(x)) = &value.value {
if key != "length" {
object_to_return.set_property(
Expand All @@ -89,10 +90,22 @@ pub fn stringify(_: &mut Value, args: &[Value], _: &mut Interpreter) -> ResultVa
}
}
}
return Ok(Value::from(object_to_return.to_json().to_string()));
} else {
unimplemented!("replacer only supports arrays at this time");
} else if borrowed_derefed_replacer.kind == ObjectKind::Function {
if let ValueData::Object(ref obj) = object.data() {
let derefed_obj = (*obj).deref();
let borrowed_deref_obj = derefed_obj.borrow();
for (key, val) in borrowed_deref_obj.properties.iter() {
if let Some(value) = &val.value {
let mut this_arg = object.clone();
object_to_return.set_property(
String::from(key),
Property::default().value(interpreter.call(arg, &mut this_arg, &[Value::string(key), Value::from(value)]).unwrap()),
);
}
}
}
}
return Ok(Value::from(object_to_return.to_json().to_string()));
}
}
panic!("cannot get replacer for JSON.stringify");
Expand Down
18 changes: 18 additions & 0 deletions boa/src/builtins/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,21 @@ fn json_stringify_replacer_array_numbers() {
let expected = forward(&mut engine, r#"'{"1":"bbb","2":"ccc"}'"#);
assert_eq!(actual, expected);
}

#[test]
fn json_stringify_replacer_function() {
let realm = Realm::create();
let mut engine = Executor::new(realm);
let actual = forward(
&mut engine,
r#"JSON.stringify({ aaa: 1, bbb: 2}, (key, value) => {
if (key === 'aaa') {
return undefined;
}
return value;
})"#,
);
let expected = forward(&mut engine, r#"'{"bbb":2}'"#);
assert_eq!(actual, expected);
}

0 comments on commit dd229aa

Please sign in to comment.