rogchap / v8go

Execute JavaScript from Go
https://rogchap.com/v8go
BSD 3-Clause "New" or "Revised" License
3.21k stars 221 forks source link

Using MarshallJSON() on objects yields strange results #404

Open mustafaakin opened 8 months ago

mustafaakin commented 8 months ago
ctx := v8.NewContext()
val, _ := ctx.RunScript("result = { name: 'mustafa'}; result", "main.js")
b, _ := val.MarshalJSON()
fmt.Println(val, string(b))

The outputs are:

[object Object] {"name":"mustafa"}

However when I run the following output does not care about my object and when I define another field its just an error

{ name: 'mustafa'} => mustafa "mustafa"
{name: 'mustafa', age: 32} => SyntaxError: Unexpected token ':'

Not sure what's going on here but it works on arrays without a problem

[1, false, 3.14, 'mustafa', {name: 'mustafa', age : 30}] 
=>
1,false,3.14,mustafa,[object Object] [1,false,3.14,"mustafa",{"name":"mustafa","age":30}]

Any pointers would be great. When I type the object definition to Chrome console or Node REPL it does not complain, so what is different here? I'm building an template execution engine and current parsing code is a quick hack and not ideal. https://github.com/mustafaakin/templ8go