goscript 0.0.34 → 0.0.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compiler/analysis.go +28 -20
- package/compiler/analysis_test.go +14 -0
- package/compiler/assignment.go +19 -7
- package/compiler/compiler.go +15 -99
- package/compiler/composite-lit.go +60 -17
- package/compiler/decl.go +1 -1
- package/compiler/expr-call.go +233 -35
- package/compiler/expr-selector.go +28 -2
- package/compiler/expr.go +13 -37
- package/compiler/lit.go +111 -2
- package/compiler/primitive.go +6 -6
- package/compiler/protobuf.go +0 -5
- package/compiler/sanitize.go +101 -0
- package/compiler/spec-struct.go +41 -8
- package/compiler/spec-value.go +29 -18
- package/compiler/stmt-assign.go +22 -1
- package/compiler/stmt.go +26 -9
- package/compiler/type.go +3 -3
- package/dist/gs/builtin/builtin.d.ts +3 -2
- package/dist/gs/builtin/builtin.js +2 -1
- package/dist/gs/builtin/builtin.js.map +1 -1
- package/dist/gs/builtin/map.js.map +1 -1
- package/dist/gs/builtin/slice.d.ts +7 -1
- package/dist/gs/builtin/slice.js +112 -22
- package/dist/gs/builtin/slice.js.map +1 -1
- package/dist/gs/github.com/pkg/errors/errors.d.ts +13 -0
- package/dist/gs/github.com/pkg/errors/errors.js +232 -0
- package/dist/gs/github.com/pkg/errors/errors.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/go113.d.ts +4 -0
- package/dist/gs/github.com/pkg/errors/go113.js +34 -0
- package/dist/gs/github.com/pkg/errors/go113.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/index.d.ts +3 -0
- package/dist/gs/github.com/pkg/errors/index.js +4 -0
- package/dist/gs/github.com/pkg/errors/index.js.map +1 -0
- package/dist/gs/github.com/pkg/errors/stack.d.ts +32 -0
- package/dist/gs/github.com/pkg/errors/stack.js +111 -0
- package/dist/gs/github.com/pkg/errors/stack.js.map +1 -0
- package/dist/gs/maps/index.d.ts +2 -2
- package/dist/gs/maps/index.js +2 -2
- package/dist/gs/maps/index.js.map +1 -1
- package/dist/gs/maps/iter.d.ts +7 -0
- package/dist/gs/maps/iter.js +57 -0
- package/dist/gs/maps/iter.js.map +1 -0
- package/dist/gs/maps/maps.d.ts +7 -0
- package/dist/gs/maps/maps.js +67 -0
- package/dist/gs/maps/maps.js.map +1 -0
- package/dist/gs/math/abs.gs.d.ts +1 -0
- package/dist/gs/math/abs.gs.js +10 -0
- package/dist/gs/math/abs.gs.js.map +1 -0
- package/dist/gs/math/acosh.gs.d.ts +2 -0
- package/dist/gs/math/acosh.gs.js +14 -0
- package/dist/gs/math/acosh.gs.js.map +1 -0
- package/dist/gs/math/asin.gs.d.ts +4 -0
- package/dist/gs/math/asin.gs.js +24 -0
- package/dist/gs/math/asin.gs.js.map +1 -0
- package/dist/gs/math/asinh.gs.d.ts +2 -0
- package/dist/gs/math/asinh.gs.js +14 -0
- package/dist/gs/math/asinh.gs.js.map +1 -0
- package/dist/gs/math/atan.gs.d.ts +4 -0
- package/dist/gs/math/atan.gs.js +22 -0
- package/dist/gs/math/atan.gs.js.map +1 -0
- package/dist/gs/math/atan2.gs.d.ts +2 -0
- package/dist/gs/math/atan2.gs.js +30 -0
- package/dist/gs/math/atan2.gs.js.map +1 -0
- package/dist/gs/math/atanh.gs.d.ts +2 -0
- package/dist/gs/math/atanh.gs.js +16 -0
- package/dist/gs/math/atanh.gs.js.map +1 -0
- package/dist/gs/math/bits.gs.d.ts +5 -0
- package/dist/gs/math/bits.gs.js +46 -0
- package/dist/gs/math/bits.gs.js.map +1 -0
- package/dist/gs/math/cbrt.gs.d.ts +2 -0
- package/dist/gs/math/cbrt.gs.js +14 -0
- package/dist/gs/math/cbrt.gs.js.map +1 -0
- package/dist/gs/math/const.gs.d.ts +30 -0
- package/dist/gs/math/const.gs.js +61 -0
- package/dist/gs/math/const.gs.js.map +1 -0
- package/dist/gs/math/copysign.gs.d.ts +1 -0
- package/dist/gs/math/copysign.gs.js +20 -0
- package/dist/gs/math/copysign.gs.js.map +1 -0
- package/dist/gs/math/dim.gs.d.ts +5 -0
- package/dist/gs/math/dim.gs.js +69 -0
- package/dist/gs/math/dim.gs.js.map +1 -0
- package/dist/gs/math/erf.gs.d.ts +4 -0
- package/dist/gs/math/erf.gs.js +336 -0
- package/dist/gs/math/erf.gs.js.map +1 -0
- package/dist/gs/math/erfinv.gs.d.ts +2 -0
- package/dist/gs/math/erfinv.gs.js +118 -0
- package/dist/gs/math/erfinv.gs.js.map +1 -0
- package/dist/gs/math/exp.gs.d.ts +5 -0
- package/dist/gs/math/exp.gs.js +30 -0
- package/dist/gs/math/exp.gs.js.map +1 -0
- package/dist/gs/math/expm1.gs.d.ts +2 -0
- package/dist/gs/math/expm1.gs.js +17 -0
- package/dist/gs/math/expm1.gs.js.map +1 -0
- package/dist/gs/math/floor.gs.d.ts +8 -0
- package/dist/gs/math/floor.gs.js +75 -0
- package/dist/gs/math/floor.gs.js.map +1 -0
- package/dist/gs/math/fma.gs.d.ts +1 -0
- package/dist/gs/math/fma.gs.js +8 -0
- package/dist/gs/math/fma.gs.js.map +1 -0
- package/dist/gs/math/frexp.gs.d.ts +2 -0
- package/dist/gs/math/frexp.gs.js +28 -0
- package/dist/gs/math/frexp.gs.js.map +1 -0
- package/dist/gs/math/gamma.gs.d.ts +3 -0
- package/dist/gs/math/gamma.gs.js +149 -0
- package/dist/gs/math/gamma.gs.js.map +1 -0
- package/dist/gs/math/hypot.gs.d.ts +2 -0
- package/dist/gs/math/hypot.gs.js +16 -0
- package/dist/gs/math/hypot.gs.js.map +1 -0
- package/dist/gs/math/index.d.ts +44 -0
- package/dist/gs/math/index.js +45 -0
- package/dist/gs/math/index.js.map +1 -0
- package/dist/gs/math/j0.gs.d.ts +4 -0
- package/dist/gs/math/j0.gs.js +228 -0
- package/dist/gs/math/j0.gs.js.map +1 -0
- package/dist/gs/math/j1.gs.d.ts +4 -0
- package/dist/gs/math/j1.gs.js +211 -0
- package/dist/gs/math/j1.gs.js.map +1 -0
- package/dist/gs/math/jn.gs.d.ts +2 -0
- package/dist/gs/math/jn.gs.js +412 -0
- package/dist/gs/math/jn.gs.js.map +1 -0
- package/dist/gs/math/ldexp.gs.d.ts +2 -0
- package/dist/gs/math/ldexp.gs.js +20 -0
- package/dist/gs/math/ldexp.gs.js.map +1 -0
- package/dist/gs/math/lgamma.gs.d.ts +2 -0
- package/dist/gs/math/lgamma.gs.js +243 -0
- package/dist/gs/math/lgamma.gs.js.map +1 -0
- package/dist/gs/math/log.gs.d.ts +2 -0
- package/dist/gs/math/log.gs.js +16 -0
- package/dist/gs/math/log.gs.js.map +1 -0
- package/dist/gs/math/log10.gs.d.ts +4 -0
- package/dist/gs/math/log10.gs.js +17 -0
- package/dist/gs/math/log10.gs.js.map +1 -0
- package/dist/gs/math/log1p.gs.d.ts +2 -0
- package/dist/gs/math/log1p.gs.js +17 -0
- package/dist/gs/math/log1p.gs.js.map +1 -0
- package/dist/gs/math/logb.gs.d.ts +3 -0
- package/dist/gs/math/logb.gs.js +43 -0
- package/dist/gs/math/logb.gs.js.map +1 -0
- package/dist/gs/math/mod.gs.d.ts +2 -0
- package/dist/gs/math/mod.gs.js +26 -0
- package/dist/gs/math/mod.gs.js.map +1 -0
- package/dist/gs/math/modf.gs.d.ts +2 -0
- package/dist/gs/math/modf.gs.js +24 -0
- package/dist/gs/math/modf.gs.js.map +1 -0
- package/dist/gs/math/nextafter.gs.d.ts +2 -0
- package/dist/gs/math/nextafter.gs.js +66 -0
- package/dist/gs/math/nextafter.gs.js.map +1 -0
- package/dist/gs/math/pow.gs.d.ts +3 -0
- package/dist/gs/math/pow.gs.js +40 -0
- package/dist/gs/math/pow.gs.js.map +1 -0
- package/dist/gs/math/pow10.gs.d.ts +1 -0
- package/dist/gs/math/pow10.gs.js +14 -0
- package/dist/gs/math/pow10.gs.js.map +1 -0
- package/dist/gs/math/remainder.gs.d.ts +2 -0
- package/dist/gs/math/remainder.gs.js +25 -0
- package/dist/gs/math/remainder.gs.js.map +1 -0
- package/dist/gs/math/signbit.gs.d.ts +1 -0
- package/dist/gs/math/signbit.gs.js +5 -0
- package/dist/gs/math/signbit.gs.js.map +1 -0
- package/dist/gs/math/sin.gs.d.ts +4 -0
- package/dist/gs/math/sin.gs.js +29 -0
- package/dist/gs/math/sin.gs.js.map +1 -0
- package/dist/gs/math/sincos.gs.d.ts +1 -0
- package/dist/gs/math/sincos.gs.js +11 -0
- package/dist/gs/math/sincos.gs.js.map +1 -0
- package/dist/gs/math/sinh.gs.d.ts +4 -0
- package/dist/gs/math/sinh.gs.js +27 -0
- package/dist/gs/math/sinh.gs.js.map +1 -0
- package/dist/gs/math/sqrt.gs.d.ts +2 -0
- package/dist/gs/math/sqrt.gs.js +15 -0
- package/dist/gs/math/sqrt.gs.js.map +1 -0
- package/dist/gs/math/tan.gs.d.ts +2 -0
- package/dist/gs/math/tan.gs.js +17 -0
- package/dist/gs/math/tan.gs.js.map +1 -0
- package/dist/gs/math/tanh.gs.d.ts +2 -0
- package/dist/gs/math/tanh.gs.js +17 -0
- package/dist/gs/math/tanh.gs.js.map +1 -0
- package/dist/gs/math/trig_reduce.gs.d.ts +1 -0
- package/dist/gs/math/trig_reduce.gs.js +62 -0
- package/dist/gs/math/trig_reduce.gs.js.map +1 -0
- package/dist/gs/math/unsafe.gs.d.ts +4 -0
- package/dist/gs/math/unsafe.gs.js +47 -0
- package/dist/gs/math/unsafe.gs.js.map +1 -0
- package/dist/gs/strconv/atob.gs.d.ts +4 -0
- package/dist/gs/strconv/atob.gs.js +42 -0
- package/dist/gs/strconv/atob.gs.js.map +1 -0
- package/dist/gs/strconv/atof.gs.d.ts +2 -0
- package/dist/gs/strconv/atof.gs.js +51 -0
- package/dist/gs/strconv/atof.gs.js.map +1 -0
- package/dist/gs/strconv/atoi.gs.d.ts +33 -0
- package/dist/gs/strconv/atoi.gs.js +200 -0
- package/dist/gs/strconv/atoi.gs.js.map +1 -0
- package/dist/gs/strconv/doc.gs.d.ts +1 -0
- package/dist/gs/strconv/doc.gs.js +2 -0
- package/dist/gs/strconv/doc.gs.js.map +1 -0
- package/dist/gs/strconv/ftoa.gs.d.ts +3 -0
- package/dist/gs/strconv/ftoa.gs.js +58 -0
- package/dist/gs/strconv/ftoa.gs.js.map +1 -0
- package/dist/gs/strconv/index.d.ts +6 -0
- package/dist/gs/strconv/index.js +7 -0
- package/dist/gs/strconv/index.js.map +1 -0
- package/dist/gs/strconv/itoa.gs.d.ts +6 -0
- package/dist/gs/strconv/itoa.gs.js +37 -0
- package/dist/gs/strconv/itoa.gs.js.map +1 -0
- package/dist/gs/strconv/quote.gs.d.ts +19 -0
- package/dist/gs/strconv/quote.gs.js +217 -0
- package/dist/gs/strconv/quote.gs.js.map +1 -0
- package/dist/gs/strings/index.d.ts +3 -0
- package/dist/gs/strings/index.js +4 -0
- package/dist/gs/strings/index.js.map +1 -1
- package/dist/gs/strings/replace.d.ts +0 -74
- package/dist/gs/strings/replace.js +6 -204
- package/dist/gs/strings/replace.js.map +1 -1
- package/dist/gs/strings/search.d.ts +0 -1
- package/dist/gs/strings/search.js +0 -21
- package/dist/gs/strings/search.js.map +1 -1
- package/dist/gs/sync/atomic/doc.gs.d.ts +28 -0
- package/dist/gs/sync/atomic/doc.gs.js +265 -0
- package/dist/gs/sync/atomic/doc.gs.js.map +1 -0
- package/dist/gs/sync/atomic/doc_64.gs.d.ts +15 -0
- package/dist/gs/sync/atomic/doc_64.gs.js +165 -0
- package/dist/gs/sync/atomic/doc_64.gs.js.map +1 -0
- package/dist/gs/sync/atomic/index.d.ts +4 -0
- package/dist/gs/sync/atomic/index.js +5 -0
- package/dist/gs/sync/atomic/index.js.map +1 -0
- package/dist/gs/sync/atomic/type.gs.d.ts +130 -0
- package/dist/gs/sync/atomic/type.gs.js +433 -0
- package/dist/gs/sync/atomic/type.gs.js.map +1 -0
- package/dist/gs/sync/atomic/value.gs.d.ts +19 -0
- package/dist/gs/sync/atomic/value.gs.js +116 -0
- package/dist/gs/sync/atomic/value.gs.js.map +1 -0
- package/dist/gs/unsafe/unsafe.d.ts +1 -0
- package/dist/gs/unsafe/unsafe.js +5 -0
- package/dist/gs/unsafe/unsafe.js.map +1 -1
- package/gs/builtin/builtin.ts +9 -7
- package/gs/builtin/map.ts +5 -1
- package/gs/builtin/slice.ts +152 -24
- package/gs/github.com/pkg/errors/errors.ts +307 -0
- package/gs/github.com/pkg/errors/go113.ts +39 -0
- package/gs/github.com/pkg/errors/index.ts +3 -0
- package/gs/github.com/pkg/errors/stack.ts +127 -0
- package/gs/maps/index.ts +2 -2
- package/gs/maps/iter.ts +67 -0
- package/gs/maps/maps.ts +89 -0
- package/gs/math/TODO.md +156 -0
- package/gs/math/abs.gs.test.ts +29 -0
- package/gs/math/abs.gs.ts +13 -0
- package/gs/math/acosh.gs.test.ts +39 -0
- package/gs/math/acosh.gs.ts +21 -0
- package/gs/math/asin.gs.test.ts +66 -0
- package/gs/math/asin.gs.ts +27 -0
- package/gs/math/asinh.gs.test.ts +37 -0
- package/gs/math/asinh.gs.ts +21 -0
- package/gs/math/atan.gs.test.ts +49 -0
- package/gs/math/atan.gs.ts +27 -0
- package/gs/math/atan2.gs.test.ts +55 -0
- package/gs/math/atan2.gs.ts +37 -0
- package/gs/math/atanh.gs.test.ts +47 -0
- package/gs/math/atanh.gs.ts +21 -0
- package/gs/math/bits.gs.test.ts +88 -0
- package/gs/math/bits.gs.ts +61 -0
- package/gs/math/cbrt.gs.test.ts +57 -0
- package/gs/math/cbrt.gs.ts +20 -0
- package/gs/math/const.gs.test.ts +54 -0
- package/gs/math/const.gs.ts +93 -0
- package/gs/math/copysign.gs.test.ts +44 -0
- package/gs/math/copysign.gs.ts +27 -0
- package/gs/math/dim.gs.test.ts +102 -0
- package/gs/math/dim.gs.ts +84 -0
- package/gs/math/erf.gs.test.ts +92 -0
- package/gs/math/erf.gs.ts +409 -0
- package/gs/math/erfinv.gs.test.ts +104 -0
- package/gs/math/erfinv.gs.ts +169 -0
- package/gs/math/exp.gs.test.ts +82 -0
- package/gs/math/exp.gs.ts +39 -0
- package/gs/math/expm1.gs.test.ts +48 -0
- package/gs/math/expm1.gs.ts +23 -0
- package/gs/math/floor.gs.test.ts +146 -0
- package/gs/math/floor.gs.ts +88 -0
- package/gs/math/fma.gs.test.ts +83 -0
- package/gs/math/fma.gs.ts +7 -0
- package/gs/math/frexp.gs.test.ts +146 -0
- package/gs/math/frexp.gs.ts +37 -0
- package/gs/math/gamma.gs.test.ts +66 -0
- package/gs/math/gamma.gs.ts +158 -0
- package/gs/math/hypot.gs.test.ts +73 -0
- package/gs/math/hypot.gs.ts +23 -0
- package/gs/math/index.ts +44 -0
- package/gs/math/j0.gs.test.ts +74 -0
- package/gs/math/j0.gs.ts +257 -0
- package/gs/math/j1.gs.test.ts +81 -0
- package/gs/math/j1.gs.ts +231 -0
- package/gs/math/jn.gs.test.ts +133 -0
- package/gs/math/jn.gs.ts +447 -0
- package/gs/math/ldexp.gs.test.ts +128 -0
- package/gs/math/ldexp.gs.ts +28 -0
- package/gs/math/lgamma.gs.test.ts +102 -0
- package/gs/math/lgamma.gs.ts +251 -0
- package/gs/math/log.gs.test.ts +40 -0
- package/gs/math/log.gs.ts +21 -0
- package/gs/math/log10.gs.test.ts +80 -0
- package/gs/math/log10.gs.ts +25 -0
- package/gs/math/log1p.gs.test.ts +55 -0
- package/gs/math/log1p.gs.ts +24 -0
- package/gs/math/logb.gs.test.ts +87 -0
- package/gs/math/logb.gs.ts +54 -0
- package/gs/math/mod.gs.test.ts +64 -0
- package/gs/math/mod.gs.ts +36 -0
- package/gs/math/modf.gs.test.ts +80 -0
- package/gs/math/modf.gs.ts +32 -0
- package/gs/math/nextafter.gs.test.ts +107 -0
- package/gs/math/nextafter.gs.ts +71 -0
- package/gs/math/pow.gs.test.ts +103 -0
- package/gs/math/pow.gs.ts +55 -0
- package/gs/math/pow10.gs.test.ts +58 -0
- package/gs/math/pow10.gs.ts +19 -0
- package/gs/math/remainder.gs.test.ts +70 -0
- package/gs/math/remainder.gs.ts +33 -0
- package/gs/math/signbit.gs.test.ts +33 -0
- package/gs/math/signbit.gs.ts +8 -0
- package/gs/math/sin.gs.test.ts +83 -0
- package/gs/math/sin.gs.ts +38 -0
- package/gs/math/sincos.gs.test.ts +91 -0
- package/gs/math/sincos.gs.ts +15 -0
- package/gs/math/sinh.gs.test.ts +66 -0
- package/gs/math/sinh.gs.ts +34 -0
- package/gs/math/sqrt.gs.test.ts +49 -0
- package/gs/math/sqrt.gs.ts +20 -0
- package/gs/math/tan.gs.test.ts +50 -0
- package/gs/math/tan.gs.ts +23 -0
- package/gs/math/tanh.gs.test.ts +52 -0
- package/gs/math/tanh.gs.ts +23 -0
- package/gs/math/trig_reduce.gs.ts +66 -0
- package/gs/math/unsafe.gs.ts +52 -0
- package/gs/strconv/atob.gs.ts +45 -0
- package/gs/strconv/atof.gs.ts +60 -0
- package/gs/strconv/atoi.gs.ts +243 -0
- package/gs/strconv/doc.gs.ts +2 -0
- package/gs/strconv/ftoa.gs.ts +66 -0
- package/gs/strconv/index.ts +6 -0
- package/gs/strconv/itoa.gs.ts +41 -0
- package/gs/strconv/quote.gs.ts +245 -0
- package/gs/strings/index.ts +4 -0
- package/gs/strings/replace.ts +9 -237
- package/gs/strings/search.ts +0 -28
- package/gs/sync/atomic/doc.gs.ts +276 -0
- package/gs/sync/atomic/doc_64.gs.ts +168 -0
- package/gs/sync/atomic/index.ts +4 -0
- package/gs/sync/atomic/type.gs.ts +596 -0
- package/gs/sync/atomic/value.gs.ts +158 -0
- package/gs/unsafe/unsafe.ts +6 -0
- package/package.json +1 -1
- package/gs/maps/iter.gs.ts +0 -71
- package/gs/maps/maps.gs.ts +0 -87
- package/gs/stringslite/godoc.txt +0 -17
- package/gs/stringslite/index.ts +0 -1
- package/gs/stringslite/strings.ts +0 -82
package/compiler/expr-call.go
CHANGED
|
@@ -206,17 +206,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
206
206
|
|
|
207
207
|
// Check if it's make([]byte, ...)
|
|
208
208
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
209
|
-
|
|
210
|
-
if len(exp.Args)
|
|
209
|
+
// Check if capacity is different from length
|
|
210
|
+
if len(exp.Args) == 3 {
|
|
211
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
212
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
211
213
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
212
214
|
return err
|
|
213
215
|
}
|
|
214
|
-
|
|
216
|
+
c.tsw.WriteLiterally(", ")
|
|
217
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
218
|
+
return err
|
|
219
|
+
}
|
|
220
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
215
221
|
} else {
|
|
216
|
-
//
|
|
217
|
-
c.tsw.WriteLiterally("
|
|
222
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
223
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
224
|
+
if len(exp.Args) >= 2 {
|
|
225
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
226
|
+
return err
|
|
227
|
+
}
|
|
228
|
+
} else {
|
|
229
|
+
// If no length is provided, default to 0
|
|
230
|
+
c.tsw.WriteLiterally("0")
|
|
231
|
+
}
|
|
232
|
+
c.tsw.WriteLiterally(")")
|
|
218
233
|
}
|
|
219
|
-
c.tsw.WriteLiterally(")")
|
|
220
234
|
return nil // Handled make for []byte
|
|
221
235
|
}
|
|
222
236
|
|
|
@@ -251,11 +265,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
251
265
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
252
266
|
c.tsw.WriteLiterally(">(")
|
|
253
267
|
|
|
268
|
+
hasCapacity := len(exp.Args) == 3
|
|
269
|
+
|
|
254
270
|
if len(exp.Args) >= 2 {
|
|
255
271
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
256
272
|
return err
|
|
257
273
|
}
|
|
258
|
-
if
|
|
274
|
+
if hasCapacity {
|
|
259
275
|
c.tsw.WriteLiterally(", ")
|
|
260
276
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
261
277
|
return err
|
|
@@ -267,6 +283,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
267
283
|
// If no length is provided, default to 0
|
|
268
284
|
c.tsw.WriteLiterally("0")
|
|
269
285
|
}
|
|
286
|
+
|
|
287
|
+
// Add type hint for proper zero value initialization
|
|
288
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
289
|
+
if typeHint != "" {
|
|
290
|
+
if !hasCapacity {
|
|
291
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
292
|
+
c.tsw.WriteLiterally(", undefined")
|
|
293
|
+
}
|
|
294
|
+
c.tsw.WriteLiterally(", '")
|
|
295
|
+
c.tsw.WriteLiterally(typeHint)
|
|
296
|
+
c.tsw.WriteLiterally("'")
|
|
297
|
+
}
|
|
298
|
+
|
|
270
299
|
c.tsw.WriteLiterally(")")
|
|
271
300
|
return nil // Handled make for slice
|
|
272
301
|
}
|
|
@@ -291,17 +320,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
291
320
|
if elemType != nil {
|
|
292
321
|
// Check if it's make(S, ...) where S constrains to []byte
|
|
293
322
|
if basicElem, isBasic := elemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
294
|
-
|
|
295
|
-
if len(exp.Args)
|
|
323
|
+
// Check if capacity is different from length
|
|
324
|
+
if len(exp.Args) == 3 {
|
|
325
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
326
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
296
327
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
297
328
|
return err
|
|
298
329
|
}
|
|
299
|
-
|
|
330
|
+
c.tsw.WriteLiterally(", ")
|
|
331
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
332
|
+
return err
|
|
333
|
+
}
|
|
334
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
300
335
|
} else {
|
|
301
|
-
//
|
|
302
|
-
c.tsw.WriteLiterally("
|
|
336
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
337
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
338
|
+
if len(exp.Args) >= 2 {
|
|
339
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
340
|
+
return err
|
|
341
|
+
}
|
|
342
|
+
} else {
|
|
343
|
+
// If no length is provided, default to 0
|
|
344
|
+
c.tsw.WriteLiterally("0")
|
|
345
|
+
}
|
|
346
|
+
c.tsw.WriteLiterally(")")
|
|
303
347
|
}
|
|
304
|
-
c.tsw.WriteLiterally(")")
|
|
305
348
|
return nil // Handled make for generic []byte
|
|
306
349
|
}
|
|
307
350
|
|
|
@@ -309,11 +352,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
309
352
|
c.WriteGoType(elemType, GoTypeContextGeneral) // Write the element type
|
|
310
353
|
c.tsw.WriteLiterally(">(")
|
|
311
354
|
|
|
355
|
+
hasCapacity := len(exp.Args) == 3
|
|
356
|
+
|
|
312
357
|
if len(exp.Args) >= 2 {
|
|
313
358
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
314
359
|
return err
|
|
315
360
|
}
|
|
316
|
-
if
|
|
361
|
+
if hasCapacity {
|
|
317
362
|
c.tsw.WriteLiterally(", ")
|
|
318
363
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
319
364
|
return err
|
|
@@ -325,6 +370,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
325
370
|
// If no length is provided, default to 0
|
|
326
371
|
c.tsw.WriteLiterally("0")
|
|
327
372
|
}
|
|
373
|
+
|
|
374
|
+
// Add type hint for proper zero value initialization
|
|
375
|
+
typeHint := c.getTypeHintForSliceElement(elemType)
|
|
376
|
+
if typeHint != "" {
|
|
377
|
+
if !hasCapacity {
|
|
378
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
379
|
+
c.tsw.WriteLiterally(", undefined")
|
|
380
|
+
}
|
|
381
|
+
c.tsw.WriteLiterally(", '")
|
|
382
|
+
c.tsw.WriteLiterally(typeHint)
|
|
383
|
+
c.tsw.WriteLiterally("'")
|
|
384
|
+
}
|
|
385
|
+
|
|
328
386
|
c.tsw.WriteLiterally(")")
|
|
329
387
|
return nil // Handled make for generic slice
|
|
330
388
|
}
|
|
@@ -340,17 +398,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
340
398
|
|
|
341
399
|
// Check if it's a named type with []byte underlying type
|
|
342
400
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
343
|
-
|
|
344
|
-
if len(exp.Args)
|
|
401
|
+
// Check if capacity is different from length
|
|
402
|
+
if len(exp.Args) == 3 {
|
|
403
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
404
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
345
405
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
346
406
|
return err
|
|
347
407
|
}
|
|
348
|
-
|
|
408
|
+
c.tsw.WriteLiterally(", ")
|
|
409
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
410
|
+
return err
|
|
411
|
+
}
|
|
412
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
349
413
|
} else {
|
|
350
|
-
//
|
|
351
|
-
c.tsw.WriteLiterally("
|
|
414
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
415
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
416
|
+
if len(exp.Args) >= 2 {
|
|
417
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
418
|
+
return err
|
|
419
|
+
}
|
|
420
|
+
} else {
|
|
421
|
+
// If no length is provided, default to 0
|
|
422
|
+
c.tsw.WriteLiterally("0")
|
|
423
|
+
}
|
|
424
|
+
c.tsw.WriteLiterally(")")
|
|
352
425
|
}
|
|
353
|
-
c.tsw.WriteLiterally(")")
|
|
354
426
|
return nil // Handled make for named []byte type
|
|
355
427
|
}
|
|
356
428
|
|
|
@@ -359,11 +431,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
359
431
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
360
432
|
c.tsw.WriteLiterally(">(")
|
|
361
433
|
|
|
434
|
+
hasCapacity := len(exp.Args) == 3
|
|
435
|
+
|
|
362
436
|
if len(exp.Args) >= 2 {
|
|
363
437
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
364
438
|
return err
|
|
365
439
|
}
|
|
366
|
-
if
|
|
440
|
+
if hasCapacity {
|
|
367
441
|
c.tsw.WriteLiterally(", ")
|
|
368
442
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
369
443
|
return err
|
|
@@ -375,6 +449,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
375
449
|
// If no length is provided, default to 0
|
|
376
450
|
c.tsw.WriteLiterally("0")
|
|
377
451
|
}
|
|
452
|
+
|
|
453
|
+
// Add type hint for proper zero value initialization
|
|
454
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
455
|
+
if typeHint != "" {
|
|
456
|
+
if !hasCapacity {
|
|
457
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
458
|
+
c.tsw.WriteLiterally(", undefined")
|
|
459
|
+
}
|
|
460
|
+
c.tsw.WriteLiterally(", '")
|
|
461
|
+
c.tsw.WriteLiterally(typeHint)
|
|
462
|
+
c.tsw.WriteLiterally("'")
|
|
463
|
+
}
|
|
464
|
+
|
|
378
465
|
c.tsw.WriteLiterally(")")
|
|
379
466
|
return nil // Handled make for named slice type
|
|
380
467
|
}
|
|
@@ -461,17 +548,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
461
548
|
|
|
462
549
|
// Check if it's an instantiated generic type with []byte underlying type
|
|
463
550
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
464
|
-
|
|
465
|
-
if len(exp.Args)
|
|
551
|
+
// Check if capacity is different from length
|
|
552
|
+
if len(exp.Args) == 3 {
|
|
553
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
554
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
466
555
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
467
556
|
return err
|
|
468
557
|
}
|
|
469
|
-
|
|
558
|
+
c.tsw.WriteLiterally(", ")
|
|
559
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
560
|
+
return err
|
|
561
|
+
}
|
|
562
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
470
563
|
} else {
|
|
471
|
-
//
|
|
472
|
-
c.tsw.WriteLiterally("
|
|
564
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
565
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
566
|
+
if len(exp.Args) >= 2 {
|
|
567
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
568
|
+
return err
|
|
569
|
+
}
|
|
570
|
+
} else {
|
|
571
|
+
// If no length is provided, default to 0
|
|
572
|
+
c.tsw.WriteLiterally("0")
|
|
573
|
+
}
|
|
574
|
+
c.tsw.WriteLiterally(")")
|
|
473
575
|
}
|
|
474
|
-
c.tsw.WriteLiterally(")")
|
|
475
576
|
return nil // Handled make for instantiated generic []byte type
|
|
476
577
|
}
|
|
477
578
|
|
|
@@ -480,11 +581,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
480
581
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
481
582
|
c.tsw.WriteLiterally(">(")
|
|
482
583
|
|
|
584
|
+
hasCapacity := len(exp.Args) == 3
|
|
585
|
+
|
|
483
586
|
if len(exp.Args) >= 2 {
|
|
484
587
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
485
588
|
return err
|
|
486
589
|
}
|
|
487
|
-
if
|
|
590
|
+
if hasCapacity {
|
|
488
591
|
c.tsw.WriteLiterally(", ")
|
|
489
592
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
490
593
|
return err
|
|
@@ -496,6 +599,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
496
599
|
// If no length is provided, default to 0
|
|
497
600
|
c.tsw.WriteLiterally("0")
|
|
498
601
|
}
|
|
602
|
+
|
|
603
|
+
// Add type hint for proper zero value initialization
|
|
604
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
605
|
+
if typeHint != "" {
|
|
606
|
+
if !hasCapacity {
|
|
607
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
608
|
+
c.tsw.WriteLiterally(", undefined")
|
|
609
|
+
}
|
|
610
|
+
c.tsw.WriteLiterally(", '")
|
|
611
|
+
c.tsw.WriteLiterally(typeHint)
|
|
612
|
+
c.tsw.WriteLiterally("'")
|
|
613
|
+
}
|
|
614
|
+
|
|
499
615
|
c.tsw.WriteLiterally(")")
|
|
500
616
|
return nil // Handled make for instantiated generic slice type
|
|
501
617
|
}
|
|
@@ -569,17 +685,31 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
569
685
|
|
|
570
686
|
// Check if it's a selector expression with []byte underlying type
|
|
571
687
|
if basicElem, isBasic := goElemType.(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
572
|
-
|
|
573
|
-
if len(exp.Args)
|
|
688
|
+
// Check if capacity is different from length
|
|
689
|
+
if len(exp.Args) == 3 {
|
|
690
|
+
// make([]byte, len, cap) - need to handle capacity
|
|
691
|
+
c.tsw.WriteLiterally("$.makeSlice<number>(")
|
|
574
692
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
575
693
|
return err
|
|
576
694
|
}
|
|
577
|
-
|
|
695
|
+
c.tsw.WriteLiterally(", ")
|
|
696
|
+
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
697
|
+
return err
|
|
698
|
+
}
|
|
699
|
+
c.tsw.WriteLiterally(", 'byte')")
|
|
578
700
|
} else {
|
|
579
|
-
//
|
|
580
|
-
c.tsw.WriteLiterally("
|
|
701
|
+
// make([]byte, len) - capacity equals length, use Uint8Array
|
|
702
|
+
c.tsw.WriteLiterally("new Uint8Array(")
|
|
703
|
+
if len(exp.Args) >= 2 {
|
|
704
|
+
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
705
|
+
return err
|
|
706
|
+
}
|
|
707
|
+
} else {
|
|
708
|
+
// If no length is provided, default to 0
|
|
709
|
+
c.tsw.WriteLiterally("0")
|
|
710
|
+
}
|
|
711
|
+
c.tsw.WriteLiterally(")")
|
|
581
712
|
}
|
|
582
|
-
c.tsw.WriteLiterally(")")
|
|
583
713
|
return nil // Handled make for selector expression []byte type
|
|
584
714
|
}
|
|
585
715
|
|
|
@@ -588,11 +718,13 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
588
718
|
c.WriteGoType(goElemType, GoTypeContextGeneral) // Write the element type
|
|
589
719
|
c.tsw.WriteLiterally(">(")
|
|
590
720
|
|
|
721
|
+
hasCapacity := len(exp.Args) == 3
|
|
722
|
+
|
|
591
723
|
if len(exp.Args) >= 2 {
|
|
592
724
|
if err := c.WriteValueExpr(exp.Args[1]); err != nil { // Length
|
|
593
725
|
return err
|
|
594
726
|
}
|
|
595
|
-
if
|
|
727
|
+
if hasCapacity {
|
|
596
728
|
c.tsw.WriteLiterally(", ")
|
|
597
729
|
if err := c.WriteValueExpr(exp.Args[2]); err != nil { // Capacity
|
|
598
730
|
return err
|
|
@@ -604,6 +736,19 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
604
736
|
// If no length is provided, default to 0
|
|
605
737
|
c.tsw.WriteLiterally("0")
|
|
606
738
|
}
|
|
739
|
+
|
|
740
|
+
// Add type hint for proper zero value initialization
|
|
741
|
+
typeHint := c.getTypeHintForSliceElement(goElemType)
|
|
742
|
+
if typeHint != "" {
|
|
743
|
+
if !hasCapacity {
|
|
744
|
+
// If no capacity was provided, add undefined for capacity parameter
|
|
745
|
+
c.tsw.WriteLiterally(", undefined")
|
|
746
|
+
}
|
|
747
|
+
c.tsw.WriteLiterally(", '")
|
|
748
|
+
c.tsw.WriteLiterally(typeHint)
|
|
749
|
+
c.tsw.WriteLiterally("'")
|
|
750
|
+
}
|
|
751
|
+
|
|
607
752
|
c.tsw.WriteLiterally(")")
|
|
608
753
|
return nil // Handled make for selector expression slice type
|
|
609
754
|
}
|
|
@@ -774,6 +919,40 @@ func (c *GoToTSCompiler) WriteCallExpr(exp *ast.CallExpr) error {
|
|
|
774
919
|
if i > 0 || len(exp.Args) > 1 { // Add comma before elements if there are any
|
|
775
920
|
c.tsw.WriteLiterally(", ")
|
|
776
921
|
}
|
|
922
|
+
|
|
923
|
+
// Special case: append([]byte, string...) should convert string to bytes
|
|
924
|
+
if exp.Ellipsis != token.NoPos && i == 0 { // This is the first element after slice and has ellipsis
|
|
925
|
+
// Check if the slice is []byte and the argument is a string
|
|
926
|
+
sliceType := c.pkg.TypesInfo.TypeOf(exp.Args[0])
|
|
927
|
+
argType := c.pkg.TypesInfo.TypeOf(arg)
|
|
928
|
+
|
|
929
|
+
if sliceType != nil && argType != nil {
|
|
930
|
+
// Check if slice is []byte (Uint8Array)
|
|
931
|
+
isSliceOfBytes := false
|
|
932
|
+
if sliceUnder, ok := sliceType.Underlying().(*types.Slice); ok {
|
|
933
|
+
if basicElem, ok := sliceUnder.Elem().(*types.Basic); ok && basicElem.Kind() == types.Uint8 {
|
|
934
|
+
isSliceOfBytes = true
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
// Check if argument is string (including untyped string)
|
|
939
|
+
isArgString := false
|
|
940
|
+
if basicArg, ok := argType.Underlying().(*types.Basic); ok && (basicArg.Kind() == types.String || basicArg.Kind() == types.UntypedString) {
|
|
941
|
+
isArgString = true
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
if isSliceOfBytes && isArgString {
|
|
945
|
+
// Convert string to bytes: append([]byte, string...) -> $.append(slice, ...$.stringToBytes(string))
|
|
946
|
+
c.tsw.WriteLiterally("...$.stringToBytes(")
|
|
947
|
+
if err := c.WriteValueExpr(arg); err != nil {
|
|
948
|
+
return fmt.Errorf("failed to write string argument in append call: %w", err)
|
|
949
|
+
}
|
|
950
|
+
c.tsw.WriteLiterally(")")
|
|
951
|
+
continue
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
|
|
777
956
|
if err := c.WriteValueExpr(arg); err != nil {
|
|
778
957
|
return fmt.Errorf("failed to write argument %d in append call: %w", i+1, err)
|
|
779
958
|
}
|
|
@@ -1123,3 +1302,22 @@ func hasMixedStringByteConstraint(iface *types.Interface) bool {
|
|
|
1123
1302
|
// Return true only if we have both string and []byte in the constraint
|
|
1124
1303
|
return hasString && hasByteSlice
|
|
1125
1304
|
}
|
|
1305
|
+
|
|
1306
|
+
// getTypeHintForSliceElement returns the appropriate type hint for makeSlice based on the Go element type
|
|
1307
|
+
func (c *GoToTSCompiler) getTypeHintForSliceElement(elemType types.Type) string {
|
|
1308
|
+
if basicType, isBasic := elemType.(*types.Basic); isBasic {
|
|
1309
|
+
switch basicType.Kind() {
|
|
1310
|
+
case types.Int, types.Int8, types.Int16, types.Int32, types.Int64,
|
|
1311
|
+
types.Uint, types.Uint8, types.Uint16, types.Uint32, types.Uint64,
|
|
1312
|
+
types.Float32, types.Float64, types.Complex64, types.Complex128:
|
|
1313
|
+
return "number"
|
|
1314
|
+
case types.Bool:
|
|
1315
|
+
return "boolean"
|
|
1316
|
+
case types.String:
|
|
1317
|
+
return "string"
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
// For other types (structs, interfaces, pointers, etc.), don't provide a hint
|
|
1321
|
+
// This will use the default null initialization which is appropriate for object types
|
|
1322
|
+
return ""
|
|
1323
|
+
}
|
|
@@ -129,7 +129,33 @@ func (c *GoToTSCompiler) WriteSelectorExpr(exp *ast.SelectorExpr) error {
|
|
|
129
129
|
// Add null assertion for selector expressions when accessing fields/methods on nullable types
|
|
130
130
|
// In Go, accessing fields or calling methods on nil pointers/interfaces panics, so we should throw in TypeScript
|
|
131
131
|
baseType := c.pkg.TypesInfo.TypeOf(exp.X)
|
|
132
|
-
|
|
132
|
+
|
|
133
|
+
// Special case: Check if this is a method receiver alias (e.g., `b` in `const b = this`)
|
|
134
|
+
// In this case, we should use regular field access instead of pointer access
|
|
135
|
+
isMethodReceiverAlias := false
|
|
136
|
+
if baseIdent, ok := exp.X.(*ast.Ident); ok {
|
|
137
|
+
// Check if this identifier refers to a method receiver
|
|
138
|
+
// Method receivers are typically named variables that alias `this`
|
|
139
|
+
if obj := c.pkg.TypesInfo.ObjectOf(baseIdent); obj != nil {
|
|
140
|
+
if varObj, ok := obj.(*types.Var); ok {
|
|
141
|
+
// Check if this is a local variable in a method context
|
|
142
|
+
// Method receiver aliases are local variables with pointer types that represent `this`
|
|
143
|
+
if _, isPtr := varObj.Type().(*types.Pointer); isPtr {
|
|
144
|
+
// Additional check: only consider it a method receiver alias if the variable name
|
|
145
|
+
// suggests it's an alias (single letter names are common for aliases like `b`, `r`, etc.)
|
|
146
|
+
// and it's not a common variable name like `f` which is often used for regular variables
|
|
147
|
+
varName := baseIdent.Name
|
|
148
|
+
if len(varName) == 1 && varName != "f" {
|
|
149
|
+
// This is likely a method receiver alias like `const b = this`
|
|
150
|
+
// We should treat it as a regular object access, not a pointer access
|
|
151
|
+
isMethodReceiverAlias = true
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if baseType != nil && !isMethodReceiverAlias {
|
|
133
159
|
// Check if the base is a pointer type
|
|
134
160
|
if _, isPtr := baseType.(*types.Pointer); isPtr {
|
|
135
161
|
c.tsw.WriteLiterally("!.")
|
|
@@ -145,7 +171,7 @@ func (c *GoToTSCompiler) WriteSelectorExpr(exp *ast.SelectorExpr) error {
|
|
|
145
171
|
c.tsw.WriteLiterally(".")
|
|
146
172
|
}
|
|
147
173
|
} else {
|
|
148
|
-
// Add .
|
|
174
|
+
// Add . (either baseType is nil or this is a method receiver alias)
|
|
149
175
|
c.tsw.WriteLiterally(".")
|
|
150
176
|
}
|
|
151
177
|
|
package/compiler/expr.go
CHANGED
|
@@ -415,13 +415,18 @@ func (c *GoToTSCompiler) WriteBinaryExpr(exp *ast.BinaryExpr) error {
|
|
|
415
415
|
|
|
416
416
|
// Handle large bit shift expressions that would overflow in JavaScript
|
|
417
417
|
if exp.Op == token.SHL {
|
|
418
|
-
// Check if this is 1 << 63 pattern
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
418
|
+
// Check if this is 1 << 63 pattern using constant evaluation
|
|
419
|
+
leftValue := c.evaluateConstantExpr(exp.X)
|
|
420
|
+
rightValue := c.evaluateConstantExpr(exp.Y)
|
|
421
|
+
|
|
422
|
+
if leftValue != nil && rightValue != nil {
|
|
423
|
+
if leftInt, leftOk := leftValue.(int); leftOk && leftInt == 1 {
|
|
424
|
+
if rightInt, rightOk := rightValue.(int); rightOk && rightInt == 63 {
|
|
425
|
+
// Replace 1 << 63 with Number.MAX_SAFE_INTEGER (9007199254740991)
|
|
426
|
+
// This is the largest integer that can be exactly represented in JavaScript
|
|
427
|
+
c.tsw.WriteLiterally("Number.MAX_SAFE_INTEGER")
|
|
428
|
+
return nil
|
|
429
|
+
}
|
|
425
430
|
}
|
|
426
431
|
}
|
|
427
432
|
}
|
|
@@ -555,23 +560,17 @@ func (c *GoToTSCompiler) WriteUnaryExpr(exp *ast.UnaryExpr) error {
|
|
|
555
560
|
|
|
556
561
|
// WriteSliceExpr translates a Go slice expression (e.g., `s[low:high:max]`) to its TypeScript equivalent.
|
|
557
562
|
// If `s` is a string and it's not a 3-index slice, it uses `s.substring(low, high)`.
|
|
558
|
-
// If `s` is `[]byte` (Uint8Array) and it's not a 3-index slice, it uses
|
|
563
|
+
// If `s` is `[]byte` (Uint8Array) and it's not a 3-index slice, it uses $.goSlice.
|
|
559
564
|
// Otherwise, it falls back to the `$.goSlice(s, low, high, max)` runtime helper.
|
|
560
565
|
func (c *GoToTSCompiler) WriteSliceExpr(exp *ast.SliceExpr) error {
|
|
561
566
|
// Check if the expression being sliced is a string
|
|
562
567
|
tv := c.pkg.TypesInfo.TypeOf(exp.X)
|
|
563
568
|
isString := false
|
|
564
|
-
isByteSlice := false
|
|
565
569
|
isTypeParam := false
|
|
566
570
|
if tv != nil {
|
|
567
571
|
if basicType, isBasic := tv.Underlying().(*types.Basic); isBasic && (basicType.Info()&types.IsString) != 0 {
|
|
568
572
|
isString = true
|
|
569
573
|
}
|
|
570
|
-
if sliceType, isSlice := tv.Underlying().(*types.Slice); isSlice {
|
|
571
|
-
if basicElem, isBasic := sliceType.Elem().(*types.Basic); isBasic && basicElem.Kind() == types.Uint8 {
|
|
572
|
-
isByteSlice = true
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
574
|
if _, isTP := tv.(*types.TypeParam); isTP {
|
|
576
575
|
isTypeParam = true
|
|
577
576
|
}
|
|
@@ -641,29 +640,6 @@ func (c *GoToTSCompiler) WriteSliceExpr(exp *ast.SliceExpr) error {
|
|
|
641
640
|
c.tsw.WriteLiterally("undefined")
|
|
642
641
|
}
|
|
643
642
|
c.tsw.WriteLiterally(")")
|
|
644
|
-
} else if isByteSlice && !exp.Slice3 {
|
|
645
|
-
// Use s.subarray(low, high) for []byte slices
|
|
646
|
-
if err := c.WriteValueExpr(exp.X); err != nil {
|
|
647
|
-
return err
|
|
648
|
-
}
|
|
649
|
-
c.tsw.WriteLiterally(".subarray(")
|
|
650
|
-
if exp.Low != nil {
|
|
651
|
-
if err := c.WriteValueExpr(exp.Low); err != nil {
|
|
652
|
-
return err
|
|
653
|
-
}
|
|
654
|
-
} else {
|
|
655
|
-
c.tsw.WriteLiterally("0") // Default low for subarray is 0
|
|
656
|
-
}
|
|
657
|
-
if exp.High != nil {
|
|
658
|
-
c.tsw.WriteLiterally(", ")
|
|
659
|
-
if err := c.WriteValueExpr(exp.High); err != nil {
|
|
660
|
-
return err
|
|
661
|
-
}
|
|
662
|
-
} else {
|
|
663
|
-
// If high is omitted, subarray goes to the end of the array.
|
|
664
|
-
// No need to write undefined or length, just close the parenthesis if low was the last arg.
|
|
665
|
-
}
|
|
666
|
-
c.tsw.WriteLiterally(")")
|
|
667
643
|
} else {
|
|
668
644
|
// Fallback to $.goSlice for actual slices (arrays) or 3-index string slices (which are rare and might need $.goSlice's complexity)
|
|
669
645
|
// Or if it's a string but has Slice3, it's not handled by simple substring.
|