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

compiler: fix incorrect emitted Javascript for type switch #683

Merged
merged 3 commits into from Sep 6, 2017
Jump to file or symbol
Failed to load files and symbols.
+29 −1
Diff settings

Always

Just for now

View
@@ -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{
View
@@ -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)
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.