Closed amreo closed 1 year ago
This appears to work as expected now using:
func Test_issue382(t *testing.T) {
var err error
type Foobar struct {
Foo []string
Bar []string
}
var value Foobar
var temp map[string]interface{}
// Decode value to temp.
tempRaw, err := json.Marshal(value)
require.NoError(t, err)
err = json.Unmarshal(tempRaw, &temp)
require.NoError(t, err)
vm := New()
err = vm.Set("foobar", temp) // Using &value instead of temp work in this example but not in my exact use case.
require.NoError(t, err)
err = vm.Set("bar", []string{"foo", "bar"})
require.NoError(t, err)
_, err = vm.Run(`
console.log([ 'foo', 'bar' ]);
console.log(typeof [ 'foo', 'bar' ]);
console.log(bar);
console.log(typeof bar);
foobar.Foo = [ 'foo', 'bar' ]; //don't work
foobar.Bar = bar; //work
`)
require.NoError(t, err)
// Encode temp to value.
tempRaw, err = json.Marshal(temp)
require.NoError(t, err)
fmt.Println(string(tempRaw))
err = json.Unmarshal(tempRaw, &value)
require.NoError(t, err)
fmt.Println(value)
}
Otto version: c382bd3c16ff2fef9b5fe0dd8bf4c4ec6bfe62c1
Actual: Otto marshal a javascript array of string to a JSON object Expected: I expected that Otto marshal a javascript array to a JSON array
Output:
Example code: