Repository navigation
Conversation
The "prototype" property of an ordinary function is created lazily, and stringKeys() put it first, so Reflect.ownKeys() and Object.getOwnPropertyNames() returned "prototype,length,name" until it was accessed. Adding a property first (f.foo = 1) also put "foo" before "prototype" once it was created. Per spec "prototype" is created right after "length" and "name". It is now created before any other own property is added or deleted, so until then it always goes last in stringKeys(). Fixes #854.
Contributor
Author
|
@dop251 Friendly ping when you have time. Is there anything you'd like changed here? |
Owner
|
I have made it work in the tinyobject branch which I'm hoping to merge soon. Merging this PR would mean extra conflicts which I'd like to avoid. I don't think this is a high-proprity issue (existing engines are all over the place), so I highly doubt anyone relies on the correct behaviour anyway. |
Contributor
Author
|
Makes sense, thanks for looking. I'll close this so it doesn't get in the way of the tinyobject branch. |
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.
Fixes #854.
The
prototypeproperty of an ordinary function is created lazily. Until it was created,funcObject.stringKeys()put"prototype"in front of the other keys, soReflect.ownKeys(f)andObject.getOwnPropertyNames(f)gaveprototype,length,name, andlength,name,prototypeafterf.prototypewas read. Adding a property first (f.foo = 1, then readingf.prototype) also gavelength,name,foo,prototype.Per spec (
OrdinaryFunctionCreate→MakeConstructor),prototypeis created right afterlengthandname, so it should always come before any property added later.The change:
setOwnStr,defineOwnPropertyStranddeleteStrnow create the prototype before any own property is added or deleted (before, only when the name was"prototype"). So the lazy prototype is only kept while the function still has justlengthandname(plus integer keys, which sort first anyway).stringKeys()can add"prototype"after the other keys instead of before them.A plain function with no added properties still gets its prototype lazily, so the common case does not allocate more than before.
Testing: added
TestFuncOwnKeysOrderinfunc_test.go. It covers the case from the issue, adding a property with assignment anddefineProperty, deleting and redefiningname, an integer key, and add-then-delete. It fails onmasterand passes with the change. Each expected value matches node (V8 also adds the legacyarguments/callerkeys, which goja doesn't have). I also checkedReflect.set,Object.assign,Object.defineProperties,Object.freeze, a setter viaObject.create(f), a symbol key andf.prototype = 5against node: same order.go test ./...(with test262 checked out),go vet,gofmtand staticcheck pass.