@@ -108,7 +108,7 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
108
108
collectIndexedElements := func (elementType types.Type ) []string {
109
109
var elements []string
110
110
i := 0
111
- zero := c .zeroValue (elementType )
111
+ zero := c .translateExpr ( c . zeroValue (elementType )). String ( )
112
112
for _ , element := range e .Elts {
113
113
if kve , isKve := element .(* ast.KeyValueExpr ); isKve {
114
114
key , ok := exact .Int64Val (c .p .Types [kve .Key ].Value )
@@ -131,9 +131,9 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
131
131
case * types.Array :
132
132
elements := collectIndexedElements (t .Elem ())
133
133
if len (elements ) == 0 {
134
- return c .formatExpr ("%s" , c .zeroValue (t ))
134
+ return c .formatExpr ("%s.zero() " , c .typeName (t ))
135
135
}
136
- zero := c .zeroValue (t .Elem ())
136
+ zero := c .translateExpr ( c . zeroValue (t .Elem ())). String ( )
137
137
for len (elements ) < int (t .Len ()) {
138
138
elements = append (elements , zero )
139
139
}
@@ -162,7 +162,7 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
162
162
}
163
163
if isKeyValue {
164
164
for i := range elements {
165
- elements [i ] = c .zeroValue (t .Field (i ).Type ())
165
+ elements [i ] = c .translateExpr ( c . zeroValue (t .Field (i ).Type ())). String ( )
166
166
}
167
167
for _ , element := range e .Elts {
168
168
kve := element .(* ast.KeyValueExpr )
@@ -462,9 +462,9 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
462
462
}
463
463
key := fmt .Sprintf ("%s.keyFor(%s)" , c .typeName (t .Key ()), c .translateImplicitConversion (e .Index , t .Key ()))
464
464
if _ , isTuple := exprType .(* types.Tuple ); isTuple {
465
- return c .formatExpr (`(%1s = %2e[%3s], %1s !== undefined ? [%1s.v, true] : [%4s , false])` , c .newVariable ("_entry" ), e .X , key , c .zeroValue (t .Elem ()))
465
+ return c .formatExpr (`(%1s = %2e[%3s], %1s !== undefined ? [%1s.v, true] : [%4e , false])` , c .newVariable ("_entry" ), e .X , key , c .zeroValue (t .Elem ()))
466
466
}
467
- return c .formatExpr (`(%1s = %2e[%3s], %1s !== undefined ? %1s.v : %4s )` , c .newVariable ("_entry" ), e .X , key , c .zeroValue (t .Elem ()))
467
+ return c .formatExpr (`(%1s = %2e[%3s], %1s !== undefined ? %1s.v : %4e )` , c .newVariable ("_entry" ), e .X , key , c .zeroValue (t .Elem ()))
468
468
case * types.Basic :
469
469
return c .formatExpr ("%e.charCodeAt(%f)" , e .X , e .Index )
470
470
default :
@@ -728,7 +728,26 @@ func (c *funcContext) translateExpr(expr ast.Expr) *expression {
728
728
case * types.TypeName :
729
729
return c .formatExpr ("%s" , c .typeName (o .Type ()))
730
730
case * types.Nil :
731
- return c .formatExpr ("%s" , c .zeroValue (c .p .TypeOf (e )))
731
+ if typesutil .IsJsObject (exprType ) {
732
+ return c .formatExpr ("null" )
733
+ }
734
+ switch t := exprType .Underlying ().(type ) {
735
+ case * types.Basic :
736
+ if t .Kind () != types .UnsafePointer {
737
+ panic ("unexpected basic type" )
738
+ }
739
+ return c .formatExpr ("0" )
740
+ case * types.Slice , * types.Pointer , * types.Chan :
741
+ return c .formatExpr ("%s.nil" , c .typeName (exprType ))
742
+ case * types.Map :
743
+ return c .formatExpr ("false" )
744
+ case * types.Interface :
745
+ return c .formatExpr ("$ifaceNil" )
746
+ case * types.Signature :
747
+ return c .formatExpr ("$throwNilPointerError" )
748
+ default :
749
+ panic (fmt .Sprintf ("unexpected type: %T" , t ))
750
+ }
732
751
default :
733
752
panic (fmt .Sprintf ("Unhandled object: %T\n " , o ))
734
753
}
@@ -808,9 +827,9 @@ func (c *funcContext) translateBuiltin(name string, sig *types.Signature, args [
808
827
}
809
828
switch t .Elem ().Underlying ().(type ) {
810
829
case * types.Struct , * types.Array :
811
- return c .formatExpr ("%s " , c .zeroValue (t .Elem ()))
830
+ return c .formatExpr ("%e " , c .zeroValue (t .Elem ()))
812
831
default :
813
- return c .formatExpr ("$newDataPointer(%s , %s)" , c .zeroValue (t .Elem ()), c .typeName (t ))
832
+ return c .formatExpr ("$newDataPointer(%e , %s)" , c .zeroValue (t .Elem ()), c .typeName (t ))
814
833
}
815
834
case "make" :
816
835
switch argType := c .p .TypeOf (args [0 ]).Underlying ().(type ) {
@@ -1035,7 +1054,7 @@ func (c *funcContext) translateConversion(expr ast.Expr, desiredType types.Type)
1035
1054
if c .p .Pkg .Path () == "syscall" && types .Identical (exprType , types .Typ [types .UnsafePointer ]) {
1036
1055
array := c .newVariable ("_array" )
1037
1056
target := c .newVariable ("_struct" )
1038
- return c .formatExpr ("(%s = %e, %s = %s , %s, %s)" , array , expr , target , c .zeroValue (t .Elem ()), c .loadStruct (array , target , s ), target )
1057
+ return c .formatExpr ("(%s = %e, %s = %e , %s, %s)" , array , expr , target , c .zeroValue (t .Elem ()), c .loadStruct (array , target , s ), target )
1039
1058
}
1040
1059
return c .formatExpr ("$pointerOfStructConversion(%e, %s)" , expr , c .typeName (t ))
1041
1060
}
@@ -1076,7 +1095,7 @@ func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType typ
1076
1095
return c .translateExpr (expr )
1077
1096
}
1078
1097
if expr == nil {
1079
- return c .formatExpr ("%s " , c .zeroValue (desiredType ))
1098
+ return c .formatExpr ("%e " , c .zeroValue (desiredType ))
1080
1099
}
1081
1100
1082
1101
exprType := c .p .TypeOf (expr )
@@ -1086,7 +1105,7 @@ func (c *funcContext) translateImplicitConversion(expr ast.Expr, desiredType typ
1086
1105
1087
1106
basicExprType , isBasicExpr := exprType .Underlying ().(* types.Basic )
1088
1107
if isBasicExpr && basicExprType .Kind () == types .UntypedNil {
1089
- return c .formatExpr ("%s " , c .zeroValue (desiredType ))
1108
+ return c .formatExpr ("%e " , c .zeroValue (desiredType ))
1090
1109
}
1091
1110
1092
1111
switch desiredType .Underlying ().(type ) {
0 commit comments