Closed TristanSpeakEasy closed 1 week ago
I just wanted to add the runtime we run this in contains a lot more code and data then this snippet and I am going to continue to try and create a reproducible for it. I may have to just try and recreate the logic 1:1
"Native" objects (i.e. created with NewObject() or returned by built-in methods) should maintain the order as per ECMAScript specification, however wrapped Go maps (i.e. created with ToValue(map[string]any{.... }) or passed to an exposed JS functions) do not, because they just wrap a Go value. Most likely this is what you're seeing. A solution here is to manually copy the Go map into a JS object, or wrap it using a DynamicObject implementation that maintains consistent property order.
Thanks for the response, I suspected originally it may be because the type was backed by a Go map but we are not passing a go type to this code except for the string that represents the raw JSON to be parsed as shown in the example above.
I then went hunting for where maybe some level of JS object manipulation might have fallen back on Go maps in the Goja code base but haven't made much progress there.
Will see if some of the workarounds you suggested help us or try and get the reproducible together
Thanks for the pointers, got to the bottom of it and it was a call site that was moving the object out to a go function and then back again that caused the order to change. Just an innocuous little call that got looked over.
Anyway thanks again and closing this now
I have been trying to track down an issue in our usage of Goja that is causing the order of keys in an object to change between executions of a script.
I have been trying to create a reproducible but have failed to isolate the issue in a small enough script and go example to provide something that could highlight the issue.
I am opening the issue in hopes there may be some idea of what might be causing the issue we are seeing?
Below I will provide a little snippet of code that shows a very simplified example of the code we are using that is exhibiting the issue, but just to be clear this code snippet doesn't reproduce it.
The below snippet is trying to replicate the following logic: we are parsing some JSON we load from an external file, we then are trying to create an object that contains the "remaining" fields that weren't known about ahead of time and print the keys, but in our more complex version of this the order of the keys printed from the line
console.log(JSON.stringify(Object.keys(remaining)));
changes most executions, just not in this simplified example.