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

Add well formed JSON.stringify, String.prototype.isWellFormed, and String.prototype.toWellFormed #1628

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

camnwalter
Copy link
Contributor

Closes #982

@@ -5253,7 +5254,7 @@ language/expressions/new 41/59 (69.49%)

~language/expressions/new.target

language/expressions/object 865/1169 (73.99%)
language/expressions/object 863/1169 (73.82%)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Any idea what's going on here? The stats for updated, but I don't see any tests being unbroken in this section

Copy link
Contributor Author

@camnwalter camnwalter Sep 13, 2024

Choose a reason for hiding this comment

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

Not sure, could a previous PR have fixed something but forgot to update the properties file?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Well, the last thing that got merged was another one of your PRs 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok no idea then 😅

Copy link
Collaborator

Choose a reason for hiding this comment

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

I have the impression i have seen something similar for other pr's also, but i might be wrong

Copy link
Contributor

Choose a reason for hiding this comment

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

You probably need to rebase this branch. language/expressions/object 863/1169 (73.82%) is the value currently in the master branch. It's not being added with this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will do, thanks

@@ -772,6 +780,56 @@ else if (Normalizer.Form.NFC.name().equals(formStr))

return str.substring(k, k + 1);
}
case Id_isWellFormed:
{
String str = ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f));
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is minor but it will likely help with performance in a bunch of cases -- how about using "toCharSequence" here instead of "toString"? That would mean that concatenated strings here don't need to be re-concatenated, and would work fine since it looks like you're just iterating character-by-character anyway.

@gbrail
Copy link
Collaborator

gbrail commented Sep 17, 2024

See above, I'm curious if you could use CharSequence here instead for performance, otherwise this looks good and thanks!

@camnwalter
Copy link
Contributor Author

I will try that out in a little bit!

}
product.append('"');
return product.toString();
}

static boolean isLeadingSurrogate(char c) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are these checks specific to JSON or more general? If they are not specific to JSON i vote for haing a more central place for these methods. Not sure if we already have one (sorry i only have my cell phone at hand at the moment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No they aren't. Not sure where they should go though...

Copy link
Collaborator

Choose a reason for hiding this comment

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

@gbrail @p-bakker @tonygermano any suggestion?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess it could go into ScriptRuntime? Not sure of a better spot.

}
case Id_toWellFormed:
{
String str = ScriptRuntime.toString(requireObjectCoercible(cx, thisObj, f));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this kind of string processing can be optimized even more. Have a first loop that simply traverses the string until you reach a char that has to be replaced. Then create the string builder an use the already processed chars as initial input. Now you can process like you did.
This ends up with more code but avoidas the string buider and the reconstruction of the string in all standard cases. I assume that most of the json to be normalized does not contain surrogates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a great point, thanks! I can try this out and see how it goes.


if (NativeJSON.isLeadingSurrogate(prev)
&& NativeJSON.isTrailingSurrogate(c)) {
surrogates.put(Integer.valueOf(i - 1), Boolean.TRUE);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used the Boxed types for the map because I saw a commit from a few years ago that reduced the amount of auto boxing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support ES2019 well formed JSON.stringify
5 participants