Created
October 23, 2014 16:14
-
-
Save fiorix/a527556896522b1b1d8c to your computer and use it in GitHub Desktop.
v8.go binding structs in structs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package v8 | |
import "testing" | |
type User struct { | |
Name string | |
Info UserInfo | |
} | |
type UserInfo struct { | |
A, B int | |
} | |
func Test_Bind_Embedded_Struct(t *testing.T) { | |
template := engine.NewObjectTemplate() | |
template.Bind("User", User{}) | |
//template.Bind("UserInfo", UserInfo{}) // <- causes panic | |
template.Bind("Test", func() User { | |
return User{"foobar", UserInfo{1, 2}} | |
}) | |
engine.NewContext(template).Scope(func(cs ContextScope) { | |
err := cs.TryCatch(func() { | |
retVal := cs.Eval(`Test()`) | |
object := retVal.ToObject() | |
if name := object.GetProperty("Name").ToString(); name != "foobar" { | |
t.Errorf("Unexpected User.Name: %q", name) | |
} | |
info := object.GetProperty("Info") | |
if info.IsUndefined() { | |
t.Fatal("Info is undefined") | |
} | |
t.Logf("Info: %+v", info) | |
}) | |
if err != nil { | |
t.Fatal(err) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment