Repository navigation
Honor toJSON in JSON.stringify's fast path for non-enumerable own properties and swapped array prototypes #267
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
Closed
Closed
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| function shouldBe(actual, expected) | ||
| { | ||
| if (actual !== expected) | ||
| throw new Error('bad value: ' + actual); | ||
| } | ||
|
|
||
| // SerializeJSONProperty looks up toJSON with GetV, so an own toJSON is called | ||
| // regardless of its enumerability. The fast stringifier skips DontEnum | ||
| // properties, so it has to check for toJSON before doing that. | ||
| function withNonEnumerableToJSON(object, toJSON) | ||
| { | ||
| Object.defineProperty(object, 'toJSON', { value: toJSON, enumerable: false, writable: false, configurable: false }); | ||
| return object; | ||
| } | ||
|
|
||
| { | ||
| let object = withNonEnumerableToJSON({ uri: 'u', cid: 'c', text: 't' }, function () { return { uri: this.uri, cid: this.cid }; }); | ||
| Object.freeze(object); | ||
| shouldBe(JSON.stringify(object), '{"uri":"u","cid":"c"}'); | ||
| shouldBe(JSON.stringify({ record: object }), '{"record":{"uri":"u","cid":"c"}}'); | ||
| shouldBe(JSON.stringify([object]), '[{"uri":"u","cid":"c"}]'); | ||
| shouldBe(JSON.stringify(object, null, 2), '{\n "uri": "u",\n "cid": "c"\n}'); | ||
| } | ||
|
|
||
| { | ||
| shouldBe(JSON.stringify(withNonEnumerableToJSON({ a: 1 }, function () { return 'replaced'; })), '"replaced"'); | ||
| shouldBe(JSON.stringify(withNonEnumerableToJSON({ a: 1 }, function () { return undefined; })), undefined); | ||
| shouldBe(JSON.stringify(withNonEnumerableToJSON({ a: 1 }, function (key) { return key; })), '""'); | ||
| shouldBe(JSON.stringify({ key: withNonEnumerableToJSON({ a: 1 }, function (key) { return key; }) }), '{"key":"key"}'); | ||
| } | ||
|
|
||
| // The fast stringifier bails out on the DynamicBuffer path too. | ||
| { | ||
| let array = []; | ||
| for (let i = 0; i < 2000; ++i) | ||
| array.push(withNonEnumerableToJSON({ index: i, name: 'name' + i }, function () { return this.index; })); | ||
| let expected = '[' + array.map((_, i) => i).join(',') + ']'; | ||
| shouldBe(JSON.stringify(array), expected); | ||
| } | ||
|
|
||
| // An own toJSON that is not callable is serialized as a normal property. | ||
| { | ||
| shouldBe(JSON.stringify(withNonEnumerableToJSON({ a: 1 }, 42)), '{"a":1}'); | ||
| shouldBe(JSON.stringify({ a: 1, toJSON: 42 }), '{"a":1,"toJSON":42}'); | ||
| } | ||
|
|
||
| // An enumerable own toJSON keeps working. | ||
| { | ||
| shouldBe(JSON.stringify({ a: 1, toJSON() { return 'enumerable'; } }), '"enumerable"'); | ||
| shouldBe(JSON.stringify(Object.freeze({ a: 1, toJSON() { return 'frozen'; } })), '"frozen"'); | ||
| } | ||
|
|
||
| // The fast path must agree with the general stringifier, which an identity | ||
| // callable replacer forces us onto. | ||
| { | ||
| let values = [ | ||
| withNonEnumerableToJSON({ a: 1 }, function () { return { b: 2 }; }), | ||
| withNonEnumerableToJSON({ a: 1 }, function () { return 'string'; }), | ||
| withNonEnumerableToJSON({ a: 1 }, 'not callable'), | ||
| { nested: withNonEnumerableToJSON({ a: 1 }, function () { return [1, 2]; }) }, | ||
| [withNonEnumerableToJSON({ a: 1 }, function () { return null; })], | ||
| withNonEnumerableToJSON({ 'キー': '値' }, function () { return this['キー']; }), | ||
| ]; | ||
| for (let value of values) { | ||
| for (let space of [undefined, 2, '\t']) { | ||
| shouldBe(JSON.stringify(value, null, space), JSON.stringify(value, (key, x) => x, space)); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.