From 169b0cbe211caeb8201626d28ff8356a6df0c60e Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Thu, 16 Apr 2020 14:31:36 -0500 Subject: [PATCH 1/7] Move JSON functions to the object, not the prototype --- boa/src/builtins/json.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index 0a7e9865a29..686534fb0bf 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -1,5 +1,4 @@ use crate::builtins::function::NativeFunctionData; -use crate::builtins::object::{Object, ObjectKind, PROTOTYPE}; /// The JSON Object /// use crate::builtins::value::{to_value, ResultValue, Value, ValueData}; @@ -29,14 +28,10 @@ pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue /// Create a new `JSON` object pub fn create_constructor(global: &Value) -> Value { - let mut json = Object::default(); - json.kind = ObjectKind::Ordinary; + let json = ValueData::new_obj(Some(global)); - let prototype = ValueData::new_obj(Some(global)); - make_builtin_fn!(parse, named "parse", with length 2, of prototype); - make_builtin_fn!(stringify, named "stringify", with length 3, of prototype); + make_builtin_fn!(parse, named "parse", with length 2, of json); + make_builtin_fn!(stringify, named "stringify", with length 3, of json); - let json_value = to_value(json); - json_value.set_field_slice(PROTOTYPE, prototype); - json_value + to_value(json) } From 854c96f0c98fe9a0df163b058d044d1af08e4a3e Mon Sep 17 00:00:00 2001 From: Noah <33094578+coolreader18@users.noreply.github.com> Date: Sun, 19 Apr 2020 00:37:56 -0500 Subject: [PATCH 2/7] Fix Value.to_json and add JSON tests --- boa/src/builtins/json.rs | 29 ++++++++++++++++++++++++++--- boa/src/builtins/value/mod.rs | 12 ++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index 686534fb0bf..d1ab37e1cfe 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -3,7 +3,7 @@ use crate::builtins::function::NativeFunctionData; /// use crate::builtins::value::{to_value, ResultValue, Value, ValueData}; use crate::exec::Interpreter; -use serde_json::{self, to_string_pretty, Value as JSONValue}; +use serde_json::{self, Value as JSONValue}; /// Parse a JSON string into a Javascript object /// @@ -22,8 +22,8 @@ pub fn parse(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { /// Process a Javascript object into a JSON string pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { let obj = args.get(0).expect("cannot get argument for JSON.stringify"); - let json = obj.to_json(); - Ok(to_value(to_string_pretty(&json).expect(""))) + let json: String = obj.to_json().to_string(); + Ok(to_value(json)) } /// Create a new `JSON` object @@ -35,3 +35,26 @@ pub fn create_constructor(global: &Value) -> Value { to_value(json) } + +#[cfg(test)] +mod tests { + use crate::exec::Executor; + use crate::forward; + use crate::realm::Realm; + #[test] + fn json_sanity() { + let realm = Realm::create(); + let mut engine = Executor::new(realm); + assert_eq!( + forward(&mut engine, r#"JSON.parse('{"aaa":"bbb"}').aaa == 'bbb'"#), + "true" + ); + assert_eq!( + forward( + &mut engine, + r#"JSON.stringify({aaa: 'bbb'}) == '{"aaa":"bbb"}'"# + ), + "true" + ); + } +} diff --git a/boa/src/builtins/value/mod.rs b/boa/src/builtins/value/mod.rs index 03d676c6d9e..ceb0e0a782b 100644 --- a/boa/src/builtins/value/mod.rs +++ b/boa/src/builtins/value/mod.rs @@ -586,12 +586,12 @@ impl ValueData { | ValueData::Function(_) => JSONValue::Null, ValueData::Boolean(b) => JSONValue::Bool(b), ValueData::Object(ref obj) => { - let mut new_obj = Map::new(); - for (k, v) in obj.borrow().internal_slots.iter() { - if k != INSTANCE_PROTOTYPE { - new_obj.insert(k.clone(), v.to_json()); - } - } + let new_obj = obj + .borrow() + .properties + .iter() + .map(|(k, _)| (k.clone(), self.get_field_slice(k).to_json())) + .collect::>(); JSONValue::Object(new_obj) } ValueData::String(ref str) => JSONValue::String(str.clone()), From 7fad67581c30109897b94ee21903d679c20e55d2 Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sat, 25 Apr 2020 13:34:05 +0200 Subject: [PATCH 3/7] Update boa/src/builtins/json.rs --- boa/src/builtins/json.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index d1ab37e1cfe..d9bdbe11efc 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -22,7 +22,7 @@ pub fn parse(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { /// Process a Javascript object into a JSON string pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { let obj = args.get(0).expect("cannot get argument for JSON.stringify"); - let json: String = obj.to_json().to_string(); + let json = obj.to_json().to_string(); Ok(to_value(json)) } From bccdcf9f68fc5f8690d831f12150ef9c29cae45a Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sat, 25 Apr 2020 13:34:22 +0200 Subject: [PATCH 4/7] Update boa/src/builtins/json.rs --- boa/src/builtins/json.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index d9bdbe11efc..81205c5d2d1 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -38,9 +38,12 @@ pub fn create_constructor(global: &Value) -> Value { #[cfg(test)] mod tests { - use crate::exec::Executor; - use crate::forward; - use crate::realm::Realm; + use crate::{ + exec::Executor, + forward, + realm::Realm, + }; + #[test] fn json_sanity() { let realm = Realm::create(); From 50095ae8584735d411e4594ea925eacf1e3c11ad Mon Sep 17 00:00:00 2001 From: Iban Eguia Date: Sat, 25 Apr 2020 13:34:58 +0200 Subject: [PATCH 5/7] Update boa/src/builtins/json.rs --- boa/src/builtins/json.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index 81205c5d2d1..2e97352c067 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -1,6 +1,8 @@ use crate::builtins::function::NativeFunctionData; -/// The JSON Object -/// +//! The JSON Object +//! +//! + use crate::builtins::value::{to_value, ResultValue, Value, ValueData}; use crate::exec::Interpreter; use serde_json::{self, Value as JSONValue}; From e9937914c402c0e6c9ae09dc7bcdc01dde810274 Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Sat, 25 Apr 2020 15:53:53 +0200 Subject: [PATCH 6/7] Update json.rs --- boa/src/builtins/json.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index 2e97352c067..991822ce3dd 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -1,8 +1,19 @@ -use crate::builtins::function::NativeFunctionData; -//! The JSON Object +//! This module implements the global `JSON` object. +//! +//! The `JSON` object contains methods for parsing [JavaScript Object Notation (JSON)][json] +//! and converting values to JSON. It can't be called or constructed, and aside from its +//! two method properties, it has no interesting functionality of its own. +//! +//! More information: +//! - [ECMAScript reference][spec] +//! - [MDN documentation][mdn] +//! - [JSON specification][json] //! -//! +//! [spec]: https://tc39.es/ecma262/#sec-json +//! [json]: https://www.json.org/json-en.html +//! [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON +use crate::builtins::function::NativeFunctionData; use crate::builtins::value::{to_value, ResultValue, Value, ValueData}; use crate::exec::Interpreter; use serde_json::{self, Value as JSONValue}; @@ -21,6 +32,7 @@ pub fn parse(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { Err(err) => Err(to_value(err.to_string())), } } + /// Process a Javascript object into a JSON string pub fn stringify(_: &Value, args: &[Value], _: &mut Interpreter) -> ResultValue { let obj = args.get(0).expect("cannot get argument for JSON.stringify"); From c7a5fca0736bb88e15f8303519cdf475af13587e Mon Sep 17 00:00:00 2001 From: HalidOdat Date: Sat, 25 Apr 2020 16:42:37 +0200 Subject: [PATCH 7/7] Fix fmt issues. --- boa/src/builtins/json.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/boa/src/builtins/json.rs b/boa/src/builtins/json.rs index 991822ce3dd..bffd28428c0 100644 --- a/boa/src/builtins/json.rs +++ b/boa/src/builtins/json.rs @@ -52,11 +52,7 @@ pub fn create_constructor(global: &Value) -> Value { #[cfg(test)] mod tests { - use crate::{ - exec::Executor, - forward, - realm::Realm, - }; + use crate::{exec::Executor, forward, realm::Realm}; #[test] fn json_sanity() {