Skip to content

compiler: fix incorrect emitted Javascript for type switch #683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 6, 2017
Merged
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
4 changes: 3 additions & 1 deletion compiler/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func (c *funcContext) translateStmt(stmt ast.Stmt, label *types.Label) {
var bodyPrefix []ast.Stmt
if implicit := c.p.Implicits[clause]; implicit != nil {
value := refVar
if _, isInterface := implicit.Type().Underlying().(*types.Interface); !isInterface {
if typesutil.IsJsObject(implicit.Type().Underlying()) {
value += ".$val.object"
} else if _, ok := implicit.Type().Underlying().(*types.Interface); !ok {
value += ".$val"
}
bodyPrefix = []ast.Stmt{&ast.AssignStmt{
Expand Down
26 changes: 26 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,29 @@ func TestUint8Array(t *testing.T) {
t.Errorf("Non-empty byte array is not externalized as a Uint8Array")
}
}

func TestTypeSwitchJSObject(t *testing.T) {
obj := js.Global.Get("Object").New()
obj.Set("foo", "bar")

want := "bar"

if got := obj.Get("foo").String(); got != want {
t.Errorf("Direct access to *js.Object field gave %q, want %q", got, want)
}

var x interface{} = obj

switch x := x.(type) {
case *js.Object:
if got := x.Get("foo").String(); got != want {
t.Errorf("Value passed through interface and type switch gave %q, want %q", got, want)
}
}

if y, ok := x.(*js.Object); ok {
if got := y.Get("foo").String(); got != want {
t.Errorf("Value passed through interface and type assert gave %q, want %q", got, want)
}
}
}