Skip to content

Fixed own key order of functions before .prototype is accessed - #866

Closed
bardrop wants to merge 1 commit into
dop251:masterfrom
bardrop-oss:fix-function-own-keys-order
Closed

bardrop wants to merge 1 commit into
dop251:masterfrom
bardrop-oss:fix-function-own-keys-order

Conversation

@bardrop

@bardrop bardrop commented Nov 12, 2026

Copy link
Copy Markdown
Contributor

Fixes #854.

The prototype property of an ordinary function is created lazily. Until it was created, funcObject.stringKeys() put "prototype" in front of the other keys, so Reflect.ownKeys(f) and Object.getOwnPropertyNames(f) gave prototype,length,name, and length,name,prototype after f.prototype was read. Adding a property first (f.foo = 1, then reading f.prototype) also gave length,name,foo,prototype.

Per spec (OrdinaryFunctionCreate → MakeConstructor), prototype is created right after length and name, so it should always come before any property added later.

The change:

  • setOwnStr, defineOwnPropertyStr and deleteStr now 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 just length and name (plus integer keys, which sort first anyway).
  • Because of that, 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 TestFuncOwnKeysOrder in func_test.go. It covers the case from the issue, adding a property with assignment and defineProperty, deleting and redefining name, an integer key, and add-then-delete. It fails on master and passes with the change. Each expected value matches node (V8 also adds the legacy arguments/caller keys, which goja doesn't have). I also checked Reflect.set, Object.assign, Object.defineProperties, Object.freeze, a setter via Object.create(f), a symbol key and f.prototype = 5 against node: same order. go test ./... (with test262 checked out), go vet, gofmt and staticcheck pass.

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.
@bardrop

bardrop commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@dop251 Friendly ping when you have time. Is there anything you'd like changed here?

@dop251

dop251 commented Nov 19, 2026

Copy link
Copy Markdown
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.

@bardrop

bardrop commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

Makes sense, thanks for looking. I'll close this so it doesn't get in the way of the tinyobject branch.

@bardrop bardrop closed this Nov 19, 2026
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.

Reflect.ownKeys returns prototype before length and name on functions before .prototype is accessed

2 participants