espruino / Espruino

The Espruino JavaScript interpreter - Official Repo
http://www.espruino.com/
Other
2.75k stars 739 forks source link

Object.entries and Object.values behave unexpectedly for objects with numeric keys #2375

Closed philipandresen closed 1 year ago

philipandresen commented 1 year ago

I'm not sure about the theoretical minimal reproduction case but this is the best I have:

Espruino Web IDE - Left hand side: Enter: Object.entries({"1": "Hello"}) Expected result: [ [ "1", "Hello" ] ] Actual result: [ [ "1", undefined ] ]

Enter: Object.values({"1": "Hello"}) Expected result: [ "Hello" ] Actual result: [ undefined ]

By contrast if you do these tests with an object containing "1a" as a key, it works as expected. That would be a reasonable workaround in some cases. Another workaround would be using Object.keys (which works fine) and then programmatically accessing the object within a loop, which is what I'll probably do for now.

Use case: Using an object as a map where the key is a numeric ID.

Suspected root cause: https://github.com/espruino/Espruino/commit/ae664896d01716aef6b035d16eac4ccbee59a254

I would normally try to submit a fix, but I am super shaky on C and am pretty new to Espruino and javascript interpreters in general. If I can work it out I would be happy to open a PR but it's a little over my head at the moment.

gfwilliams commented 1 year ago

Thanks! That's super helpful.

A change went in recently to ensure that Object.keys/etc always returned array indexes as strings to be spec compliant, and I bet that broke this. Hopefully quite an easy fix though...