Repository navigation
Conversation
A schema property named constructor, toString, valueOf, hasOwnProperty, ...
was read with obj[key] and checked against undefined, so a plain object that
does not own the property contributed the inherited function: {} came out as
{"constructor":true} or {"toString":"function toString() { [native code] }"},
and an integer property named valueOf made stringify({}) throw. Read such
properties only when the input owns them, in the generated serializer and in
the native projection. Other properties keep going through the prototype
chain, so getters on a class prototype still serialize.
The native projection also lost an own __proto__ property: the scratch literal
{"__proto__": undefined} creates no slot, so the later assignment set the
prototype instead. Use a computed key for that slot.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Checklist
npm run testandnpm run benchmark— the test suite, lint and type tests pass; the change only adds anObject.hasOwncheck for the handful of property names that exist onObject.prototype, so other properties compile to the same code as beforeProblem
A property whose name is also a member of
Object.prototypeis serialized from the prototype when the input does not own it:With
valueOfleft out of the schema the empty object serializes as{"constructor":true,"toString":"function toString() { [native code] }"};JSON.stringify({})is{}. The same happens withobjectMechanism: 'native'.buildInnerObjectemitsconst value = obj["constructor"]; if (value !== undefined) …, and the inherited function passes that check. I found this with a generator that builds random schemas and conforming values and compares the output withJSON.stringify; property names such asconstructorturned up in the key pool.While adding the
nativecase I noticed that an own__proto__property is dropped by the projection: the scratch literal{ "__proto__": undefined }does not create a slot (a non-computed__proto__key in a literal sets the prototype, andundefinedis ignored), soscratch["__proto__"] = valuelater sets the prototype instead of a property andJSON.stringifynever sees it.Fix
For the property names that
Object.hasOwn(Object.prototype, name)reports at build time, the generated code and the native projection readObject.hasOwn(obj, name) ? obj[name] : undefined. Every other property is read exactly as before, so getters on a class prototype keep working (covered by a test). The scratch literal uses a computed key for__proto__.Tests
test/inherited-properties.test.jscovers both mechanisms and standalone mode:{},{ x: 1 }and a null-prototype object serialize without the inherited members; own values for all those names (including__proto__) round-trip;requiredstill sees own values and still throws when they are absent; a prototype getter is still read. The new assertions fail onmain(thevalueOfcase throws) and pass with the change.