Skip to content

Commit

Permalink
Propsed fix for struct and array vals in interface-typed variables
Browse files Browse the repository at this point in the history
  • Loading branch information
myitcv committed Aug 1, 2017
1 parent 2b1d432 commit 926d94d
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
12 changes: 10 additions & 2 deletions compiler/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,12 @@ func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType typ

exprType := c.p.TypeOf(expr)
if types.Identical(exprType, desiredType) {
return c.translateExpr(expr)
switch exprType.Underlying().(type) {
case *types.Interface:
return c.formatExpr("$copyIntf(%e)", expr)
default:
return c.translateExpr(expr)
}
}

basicExprType, isBasicExpr := exprType.Underlying().(*types.Basic)
Expand All @@ -1130,11 +1135,14 @@ 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)
}
if _, isArray := exprType.Underlying().(*types.Array); isArray {
return c.formatExpr("new %1s($clone(%e, %1s))", c.typeName(exprType), expr)
}
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.formatExpr("new %1e.constructor.elem($clone(%1e, %s))", expr, c.typeName(exprType))
}
}

Expand Down
8 changes: 8 additions & 0 deletions compiler/prelude/prelude.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,14 @@ var $clone = function(src, type) {
return clone;
};
var $copyIntf = function(src) {
if (src.constructor.copy) {
return new src.constructor($clone(src.$val, src.constructor));
}
return src;
};
var $pointerOfStructConversion = function(obj, type) {
if(obj.$proxies === undefined) {
obj.$proxies = {};
Expand Down
88 changes: 88 additions & 0 deletions tests/interface_test.go
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 TestStructValIntfMethodCall(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)
}
}

0 comments on commit 926d94d

Please sign in to comment.