Skip to content

Commit

Permalink
fix calling go functions with goStructObject arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
deoxxa committed May 30, 2016
1 parent ef5bb30 commit 9597787
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
140 changes: 140 additions & 0 deletions call_test.go
Expand Up @@ -1364,3 +1364,143 @@ func TestNativeCallWithFunctionStringString(t *testing.T) {
t.Fail()
}
}

type testNativeCallWithStruct struct {
Prefix string
}

type testNativeCallWithStructArg struct {
Text string
}

func (t testNativeCallWithStruct) MakeStruct(s string) testNativeCallWithStructArg {
return testNativeCallWithStructArg{Text: s}
}

func (t testNativeCallWithStruct) MakeStructPointer(s string) *testNativeCallWithStructArg {
return &testNativeCallWithStructArg{Text: s}
}

func (t testNativeCallWithStruct) CallWithStruct(a testNativeCallWithStructArg) string {
return t.Prefix + a.Text
}

func (t *testNativeCallWithStruct) CallPointerWithStruct(a testNativeCallWithStructArg) string {
return t.Prefix + a.Text
}

func (t testNativeCallWithStruct) CallWithStructPointer(a *testNativeCallWithStructArg) string {
return t.Prefix + a.Text
}

func (t *testNativeCallWithStruct) CallPointerWithStructPointer(a *testNativeCallWithStructArg) string {
return t.Prefix + a.Text
}

func TestNativeCallMethodWithStruct(t *testing.T) {
vm := New()

called := false

vm.Set("x", testNativeCallWithStruct{Prefix: "a"})

vm.Set("t", func(s string) {
if s != "ab" {
t.Fail()
}

called = true
})

s, _ := vm.Compile("test.js", `t(x.CallWithStruct(x.MakeStruct("b")))`)

if _, err := vm.Run(s); err != nil {
t.Logf("err should have been nil; was %s\n", err.Error())
t.Fail()
}

if !called {
t.Fail()
}
}

func TestNativeCallPointerMethodWithStruct(t *testing.T) {
vm := New()

called := false

vm.Set("x", &testNativeCallWithStruct{Prefix: "a"})

vm.Set("t", func(s string) {
if s != "ab" {
t.Fail()
}

called = true
})

s, _ := vm.Compile("test.js", `t(x.CallPointerWithStruct(x.MakeStruct("b")))`)

if _, err := vm.Run(s); err != nil {
t.Logf("err should have been nil; was %s\n", err.Error())
t.Fail()
}

if !called {
t.Fail()
}
}

func TestNativeCallMethodWithStructPointer(t *testing.T) {
vm := New()

called := false

vm.Set("x", testNativeCallWithStruct{Prefix: "a"})

vm.Set("t", func(s string) {
if s != "ab" {
t.Fail()
}

called = true
})

s, _ := vm.Compile("test.js", `t(x.CallWithStructPointer(x.MakeStructPointer("b")))`)

if _, err := vm.Run(s); err != nil {
t.Logf("err should have been nil; was %s\n", err.Error())
t.Fail()
}

if !called {
t.Fail()
}
}

func TestNativeCallPointerMethodWithStructPointer(t *testing.T) {
vm := New()

called := false

vm.Set("x", &testNativeCallWithStruct{Prefix: "a"})

vm.Set("t", func(s string) {
if s != "ab" {
t.Fail()
}

called = true
})

s, _ := vm.Compile("test.js", `t(x.CallPointerWithStructPointer(x.MakeStructPointer("b")))`)

if _, err := vm.Run(s); err != nil {
t.Logf("err should have been nil; was %s\n", err.Error())
t.Fail()
}

if !called {
t.Fail()
}
}
8 changes: 8 additions & 0 deletions runtime.go
Expand Up @@ -293,6 +293,14 @@ func (self *_runtime) convertCallParameter(v Value, t reflect.Type) reflect.Valu
return reflect.ValueOf(v)
}

if v.kind == valueObject {
if gso, ok := v._object().value.(*_goStructObject); ok {
if gso.value.Type().AssignableTo(t) {
return gso.value
}
}
}

if t.Kind() == reflect.Interface {
iv := reflect.ValueOf(v.export())
if iv.Type().AssignableTo(t) {
Expand Down

0 comments on commit 9597787

Please sign in to comment.