Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

[WIP] Test out GopherJS struct val fix #82

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .vendor.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
./_vendor/src/golang.org/x/crypto 3cb07270c9455e8ad27956a70891c962d121a228 https://go.googlesource.com/crypto
./_vendor/src/golang.org/x/tools 620ecdb8d7943e20dc030b61bfe898d1b000bdea https://go.googlesource.com/tools
./_vendor/src/github.com/jteeuwen/go-bindata a0ff2567cfb70903282db057e799fd826784d41d git@github.com:jteeuwen/go-bindata
./_vendor/src/github.com/gopherjs/gopherjs e14987c0ef06db387b90fec85e8d06dc05598e24 git@github.com:gopherjs/gopherjs
./_vendor/src/github.com/gopherjs/gopherjs 91be9d3fa503472d6ccde719f7ec6923dfb18699 git@github.com:gopherjs/gopherjs
./_vendor/src/github.com/gopherjs/jsbuiltin 67703bfb044e3192fbcab025c3aeaeedafad1f2f git@github.com:gopherjs/jsbuiltin
./_vendor/src/github.com/kisielk/gotool 0de1eaf82fa3f583ce21fde859f1e7e0c5e9b220 git@github.com:kisielk/gotool
./_vendor/src/github.com/fsnotify/fsnotify 7d7316ed6e1ed2de075aab8dfc76de5d158d66e1 git@github.com:fsnotify/fsnotify
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,11 @@ func (c *funcContext) makeReceiver(e *ast.SelectorExpr) *expression {

_, isPointer := recvType.Underlying().(*types.Pointer)
methodsRecvType := sel.Obj().Type().(*types.Signature).Recv().Type()

if _, isInterface := methodsRecvType.Underlying().(*types.Interface); isInterface {
return c.formatExpr("$copyInterfaceVal(%e)", x)
}

_, pointerExpected := methodsRecvType.(*types.Pointer)
if !isPointer && pointerExpected {
recvType = types.NewPointer(recvType)
Expand All @@ -825,6 +830,7 @@ func (c *funcContext) makeReceiver(e *ast.SelectorExpr) *expression {
if isWrapped(recvType) {
recv = c.formatExpr("new %s(%s)", c.typeName(methodsRecvType), recv)
}

return recv
}

Expand Down Expand Up @@ -1130,12 +1136,17 @@ func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType typ
// wrap JS object into js.Object struct when converting to interface
return c.formatExpr("new $jsObjectPtr(%e)", expr)
}

switch exprType.Underlying().(type) {
case *types.Array:
return c.formatExpr("new %1s($clone(%e, %1s))", c.typeName(exprType), expr)
case *types.Struct:
return c.formatExpr("new %1e.constructor.elem($clone(%1e, %s))", expr, c.typeName(exprType))
}

if isWrapped(exprType) {
return c.formatExpr("new %s(%e)", c.typeName(exprType), expr)
}
if _, isStruct := exprType.Underlying().(*types.Struct); isStruct {
return c.formatExpr("new %1e.constructor.elem(%1e)", expr)
}
}

return c.translateExpr(expr)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ var $externalizeFunction = function(v, t, passThis) {
case 0:
return;
case 1:
return $externalize(result, t.results[0]);
return $externalize($copyInterfaceVal(result), t.results[0]);
default:
for (var i = 0; i < t.results.length; i++) {
result[i] = $externalize(result[i], t.results[i]);
result[i] = $externalize($copyInterfaceVal(result[i]), t.results[i]);
}
return result;
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package tests

import (
"fmt"
"testing"
)

type Struct struct {
Name string
}

func (s Struct) SetName(n string) {
s.Name = n
}

type SetName interface {
SetName(n string)
}

func TestAssignStructValInterface(t *testing.T) {
s := Struct{
Name: "Rob",
}

var i1 interface{} = s
var i2 interface{} = i1

s.Name = "Pike"

ss := fmt.Sprintf("%#v", s)
i1s := fmt.Sprintf("%#v", i1)
i2s := fmt.Sprintf("%#v", i2)

if exp := "tests.Struct{Name:\"Pike\"}"; ss != exp {
t.Fatalf("ss should have been %q; got %q", exp, ss)
}

iexp := "tests.Struct{Name:\"Rob\"}"

if i1s != iexp {
t.Fatalf("is should have been %q; got %q", iexp, i1s)
}

if i2s != iexp {
t.Fatalf("is should have been %q; got %q", iexp, i2s)
}
}

func TestStructValInterfaceMethodCall(t *testing.T) {
var i SetName = Struct{
Name: "Rob",
}

i.SetName("Pike")

is := fmt.Sprintf("%#v", i)

if exp := "tests.Struct{Name:\"Rob\"}"; is != exp {
t.Fatalf("is should have been %q; got %q", exp, is)
}
}

func TestAssignArrayInterface(t *testing.T) {
a := [2]int{1, 2}

var i1 interface{} = a
var i2 interface{} = i1

a[0] = 0

as := fmt.Sprintf("%#v", a)
i1s := fmt.Sprintf("%#v", i1)
i2s := fmt.Sprintf("%#v", i2)

if exp := "[2]int{0, 2}"; as != exp {
t.Fatalf("ss should have been %q; got %q", exp, as)
}

iexp := "[2]int{1, 2}"

if i1s != iexp {
t.Fatalf("is should have been %q; got %q", iexp, i1s)
}

if i2s != iexp {
t.Fatalf("is should have been %q; got %q", iexp, i2s)
}
}