Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Json stringify replacer #402

Merged
merged 31 commits into from
May 30, 2020
Merged

Conversation

n14little
Copy link
Contributor

@n14little n14little commented May 14, 2020

Closes #345
Fixes #403

Additionally, wrote and ignored some tests that should be addressed once #405 is fixed.

@Razican Razican added this to the v0.9.0 milestone May 14, 2020
@Razican Razican added the enhancement New feature or request label May 14, 2020
Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking pretty good for me. I like the idea of creating a new object if needed, but I would add a fast path in the case that args 1 and 2 are not passed, just returning the JSON of the provided object, it should improve performance in most cases.

boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
@n14little n14little force-pushed the json-stringify-replacer branch 2 times, most recently from dd229aa to 3f1d992 Compare May 18, 2020 02:45
@n14little
Copy link
Contributor Author

n14little commented May 18, 2020

@Razican --- The error on the test in commit b22e5b9 feels like a bug, would you agree?

@n14little
Copy link
Contributor Author

Once I get the symbol thing figured out, I should be able to wrap this up pretty quickly.

@Razican
Copy link
Member

Razican commented May 18, 2020

@Razican --- The error on the test in commit b22e5b9 feels like a bug, would you agree?

Hmm this depends. Does the test panic if you run it against master? if so, we should open an issue about it.

@n14little
Copy link
Contributor Author

@Razican -- Yes, it happens on master. Just opened an issue #405

@Razican
Copy link
Member

Razican commented May 18, 2020

@Razican -- Yes, it happens on master. Just opened an issue #405

Good, so comment that out and we can progress with this issue :)

@n14little n14little marked this pull request as ready for review May 19, 2020 02:24
@Razican
Copy link
Member

Razican commented May 19, 2020

I checked the spec and thought of an arguably simpler design:

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 {
        return Ok(Value::from(object.to_json().to_string()));
    }

    let replacer = args.get(1).expect("first element disappeared");
    if !replacer.is_object() {
        return Ok(Value::from(object.to_json().to_string()));
    }

    let replacer = replacer.as_object().expect("replacer was an object");
    if replacer.is_callable() {
        unimplemented!("replacer function");
    } else if replacer.kind == ObjectKind::Array {
        let mut obj_to_return = serde_json::Map::with_capacity(replacer.properties.len() - 1);
        for field in replacer.properties.iter().filter_map(|(key, prop)| {
            if key == "length" {
                None
            } else {
                // TODO: this should be the abstract operation `Get`
                prop.value.as_ref().map(Value::to_string)
            }
        }) {
            if let Some(value) = object
                .get_property(&field)
                .map(|prop| prop.value.as_ref().map(|v| v.to_json()))
                .flatten()
            {
                obj_to_return.insert(field, value);
            }
        }
        Ok(Value::from(JSONValue::Object(obj_to_return).to_string()))
    } else {
        Ok(Value::from(object.to_json().to_string()))
    }
}

Note that I didn't implement the function replacer and that I repeated Ok(Value::from(object.to_json().to_string())) too many times, so this should probably be replaced by a function or something.

The idea behind this is to avoid all that nesting. Then, instead of directly looping through all properties, I filtered the property iterator and mapped it to just return the string values.

Then, I just start creating a JSONValue, since there is no need to create an object to then convert it to JSON. I add only properties that exist and have a value, not sure if this this is 100% spec compliant, I just skimmed through it.

I did a bit of a heavy use of the Option API there, I recommend you to check it. Let me know if there is something unclear :)

@n14little n14little force-pushed the json-stringify-replacer branch 2 times, most recently from 7fc41a2 to 6fcacf2 Compare May 20, 2020 04:03
@n14little
Copy link
Contributor Author

@Razican -- thanks for taking the time to write something up. I've implemented the array replacer functionality per your suggestion. Still working on the function replacer.

The idea behind this is to avoid all that nesting.

That is what I was hoping to get rid of.

@n14little
Copy link
Contributor Author

Made an attempt at using Options in the function replacer branch.

@n14little
Copy link
Contributor Author

@Razican thoughts on how this is looking?

@Razican
Copy link
Member

Razican commented May 22, 2020

@Razican thoughts on how this is looking?

I will check this today :)

Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some comments, give them a look :)

boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/value/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/value/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
@n14little n14little force-pushed the json-stringify-replacer branch 2 times, most recently from f32e4ad to 17b54dc Compare May 25, 2020 21:01
@n14little
Copy link
Contributor Author

@Razican --- addressed all of your comments.

Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good! almost ready, I just added a couple of comments, one to improve a bit how we call the function and the other one to take advantage of a change that was just merged to master :)

boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good to go for me :) we should at some point review how we write tests, they are a bit too wordy, and I think we can improve them, but it's not directly related to this PR.

Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! check my comments to see on how we can improve it :)

boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
@n14little
Copy link
Contributor Author

@HalidOdat -- addressed your comments. Added a test for the case where nothing is passed to stringify.

Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good, check my comments to improve readability a bit :)

boa/src/builtins/json/mod.rs Show resolved Hide resolved
boa/src/builtins/json/mod.rs Outdated Show resolved Hide resolved
boa/src/builtins/value/mod.rs Outdated Show resolved Hide resolved
@n14little
Copy link
Contributor Author

@Razican @HalidOdat -- I think that I have all comments addressed.

Copy link
Member

@Razican Razican left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me! :)

@Razican Razican merged commit 82908df into boa-dev:master May 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants