goscript 0.0.33 → 0.0.35

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.
Files changed (347) hide show
  1. package/compiler/analysis.go +30 -22
  2. package/compiler/analysis_test.go +14 -0
  3. package/compiler/assignment.go +45 -7
  4. package/compiler/builtin_test.go +2 -0
  5. package/compiler/compiler.go +15 -89
  6. package/compiler/compiler_test.go +0 -53
  7. package/compiler/composite-lit.go +60 -17
  8. package/compiler/decl.go +1 -1
  9. package/compiler/expr-call.go +347 -30
  10. package/compiler/expr-selector.go +28 -2
  11. package/compiler/expr.go +79 -38
  12. package/compiler/lit.go +112 -3
  13. package/compiler/primitive.go +6 -6
  14. package/compiler/protobuf.go +0 -5
  15. package/compiler/sanitize.go +101 -0
  16. package/compiler/spec-value.go +25 -18
  17. package/compiler/stmt-assign.go +128 -91
  18. package/compiler/stmt-for.go +78 -1
  19. package/compiler/stmt-range.go +333 -461
  20. package/compiler/stmt.go +46 -9
  21. package/compiler/type.go +14 -11
  22. package/dist/gs/builtin/builtin.d.ts +8 -0
  23. package/dist/gs/builtin/builtin.js +31 -0
  24. package/dist/gs/builtin/builtin.js.map +1 -1
  25. package/dist/gs/builtin/map.d.ts +4 -4
  26. package/dist/gs/builtin/map.js +12 -6
  27. package/dist/gs/builtin/map.js.map +1 -1
  28. package/dist/gs/builtin/slice.d.ts +14 -8
  29. package/dist/gs/builtin/slice.js +131 -31
  30. package/dist/gs/builtin/slice.js.map +1 -1
  31. package/dist/gs/github.com/pkg/errors/errors.d.ts +13 -0
  32. package/dist/gs/github.com/pkg/errors/errors.js +232 -0
  33. package/dist/gs/github.com/pkg/errors/errors.js.map +1 -0
  34. package/dist/gs/github.com/pkg/errors/go113.d.ts +4 -0
  35. package/dist/gs/github.com/pkg/errors/go113.js +34 -0
  36. package/dist/gs/github.com/pkg/errors/go113.js.map +1 -0
  37. package/dist/gs/github.com/pkg/errors/index.d.ts +3 -0
  38. package/dist/gs/github.com/pkg/errors/index.js +4 -0
  39. package/dist/gs/github.com/pkg/errors/index.js.map +1 -0
  40. package/dist/gs/github.com/pkg/errors/stack.d.ts +32 -0
  41. package/dist/gs/github.com/pkg/errors/stack.js +111 -0
  42. package/dist/gs/github.com/pkg/errors/stack.js.map +1 -0
  43. package/dist/gs/maps/index.d.ts +2 -0
  44. package/dist/gs/maps/index.js +3 -0
  45. package/dist/gs/maps/index.js.map +1 -0
  46. package/dist/gs/maps/iter.d.ts +7 -0
  47. package/dist/gs/maps/iter.gs.d.ts +7 -0
  48. package/dist/gs/maps/iter.gs.js +65 -0
  49. package/dist/gs/maps/iter.gs.js.map +1 -0
  50. package/dist/gs/maps/iter.js +57 -0
  51. package/dist/gs/maps/iter.js.map +1 -0
  52. package/dist/gs/maps/maps.d.ts +7 -0
  53. package/dist/gs/maps/maps.gs.d.ts +7 -0
  54. package/dist/gs/maps/maps.gs.js +79 -0
  55. package/dist/gs/maps/maps.gs.js.map +1 -0
  56. package/dist/gs/maps/maps.js +67 -0
  57. package/dist/gs/maps/maps.js.map +1 -0
  58. package/dist/gs/math/abs.gs.d.ts +1 -0
  59. package/dist/gs/math/abs.gs.js +10 -0
  60. package/dist/gs/math/abs.gs.js.map +1 -0
  61. package/dist/gs/math/acosh.gs.d.ts +2 -0
  62. package/dist/gs/math/acosh.gs.js +14 -0
  63. package/dist/gs/math/acosh.gs.js.map +1 -0
  64. package/dist/gs/math/asin.gs.d.ts +4 -0
  65. package/dist/gs/math/asin.gs.js +24 -0
  66. package/dist/gs/math/asin.gs.js.map +1 -0
  67. package/dist/gs/math/asinh.gs.d.ts +2 -0
  68. package/dist/gs/math/asinh.gs.js +14 -0
  69. package/dist/gs/math/asinh.gs.js.map +1 -0
  70. package/dist/gs/math/atan.gs.d.ts +4 -0
  71. package/dist/gs/math/atan.gs.js +22 -0
  72. package/dist/gs/math/atan.gs.js.map +1 -0
  73. package/dist/gs/math/atan2.gs.d.ts +2 -0
  74. package/dist/gs/math/atan2.gs.js +30 -0
  75. package/dist/gs/math/atan2.gs.js.map +1 -0
  76. package/dist/gs/math/atanh.gs.d.ts +2 -0
  77. package/dist/gs/math/atanh.gs.js +16 -0
  78. package/dist/gs/math/atanh.gs.js.map +1 -0
  79. package/dist/gs/math/bits.gs.d.ts +5 -0
  80. package/dist/gs/math/bits.gs.js +46 -0
  81. package/dist/gs/math/bits.gs.js.map +1 -0
  82. package/dist/gs/math/cbrt.gs.d.ts +2 -0
  83. package/dist/gs/math/cbrt.gs.js +14 -0
  84. package/dist/gs/math/cbrt.gs.js.map +1 -0
  85. package/dist/gs/math/const.gs.d.ts +30 -0
  86. package/dist/gs/math/const.gs.js +61 -0
  87. package/dist/gs/math/const.gs.js.map +1 -0
  88. package/dist/gs/math/copysign.gs.d.ts +1 -0
  89. package/dist/gs/math/copysign.gs.js +20 -0
  90. package/dist/gs/math/copysign.gs.js.map +1 -0
  91. package/dist/gs/math/dim.gs.d.ts +5 -0
  92. package/dist/gs/math/dim.gs.js +69 -0
  93. package/dist/gs/math/dim.gs.js.map +1 -0
  94. package/dist/gs/math/erf.gs.d.ts +4 -0
  95. package/dist/gs/math/erf.gs.js +336 -0
  96. package/dist/gs/math/erf.gs.js.map +1 -0
  97. package/dist/gs/math/erfinv.gs.d.ts +2 -0
  98. package/dist/gs/math/erfinv.gs.js +118 -0
  99. package/dist/gs/math/erfinv.gs.js.map +1 -0
  100. package/dist/gs/math/exp.gs.d.ts +5 -0
  101. package/dist/gs/math/exp.gs.js +30 -0
  102. package/dist/gs/math/exp.gs.js.map +1 -0
  103. package/dist/gs/math/expm1.gs.d.ts +2 -0
  104. package/dist/gs/math/expm1.gs.js +17 -0
  105. package/dist/gs/math/expm1.gs.js.map +1 -0
  106. package/dist/gs/math/floor.gs.d.ts +8 -0
  107. package/dist/gs/math/floor.gs.js +75 -0
  108. package/dist/gs/math/floor.gs.js.map +1 -0
  109. package/dist/gs/math/fma.gs.d.ts +1 -0
  110. package/dist/gs/math/fma.gs.js +8 -0
  111. package/dist/gs/math/fma.gs.js.map +1 -0
  112. package/dist/gs/math/frexp.gs.d.ts +2 -0
  113. package/dist/gs/math/frexp.gs.js +28 -0
  114. package/dist/gs/math/frexp.gs.js.map +1 -0
  115. package/dist/gs/math/gamma.gs.d.ts +3 -0
  116. package/dist/gs/math/gamma.gs.js +149 -0
  117. package/dist/gs/math/gamma.gs.js.map +1 -0
  118. package/dist/gs/math/hypot.gs.d.ts +2 -0
  119. package/dist/gs/math/hypot.gs.js +16 -0
  120. package/dist/gs/math/hypot.gs.js.map +1 -0
  121. package/dist/gs/math/index.d.ts +44 -0
  122. package/dist/gs/math/index.js +45 -0
  123. package/dist/gs/math/index.js.map +1 -0
  124. package/dist/gs/math/j0.gs.d.ts +4 -0
  125. package/dist/gs/math/j0.gs.js +228 -0
  126. package/dist/gs/math/j0.gs.js.map +1 -0
  127. package/dist/gs/math/j1.gs.d.ts +4 -0
  128. package/dist/gs/math/j1.gs.js +211 -0
  129. package/dist/gs/math/j1.gs.js.map +1 -0
  130. package/dist/gs/math/jn.gs.d.ts +2 -0
  131. package/dist/gs/math/jn.gs.js +412 -0
  132. package/dist/gs/math/jn.gs.js.map +1 -0
  133. package/dist/gs/math/ldexp.gs.d.ts +2 -0
  134. package/dist/gs/math/ldexp.gs.js +20 -0
  135. package/dist/gs/math/ldexp.gs.js.map +1 -0
  136. package/dist/gs/math/lgamma.gs.d.ts +2 -0
  137. package/dist/gs/math/lgamma.gs.js +243 -0
  138. package/dist/gs/math/lgamma.gs.js.map +1 -0
  139. package/dist/gs/math/log.gs.d.ts +2 -0
  140. package/dist/gs/math/log.gs.js +16 -0
  141. package/dist/gs/math/log.gs.js.map +1 -0
  142. package/dist/gs/math/log10.gs.d.ts +4 -0
  143. package/dist/gs/math/log10.gs.js +17 -0
  144. package/dist/gs/math/log10.gs.js.map +1 -0
  145. package/dist/gs/math/log1p.gs.d.ts +2 -0
  146. package/dist/gs/math/log1p.gs.js +17 -0
  147. package/dist/gs/math/log1p.gs.js.map +1 -0
  148. package/dist/gs/math/logb.gs.d.ts +3 -0
  149. package/dist/gs/math/logb.gs.js +43 -0
  150. package/dist/gs/math/logb.gs.js.map +1 -0
  151. package/dist/gs/math/mod.gs.d.ts +2 -0
  152. package/dist/gs/math/mod.gs.js +26 -0
  153. package/dist/gs/math/mod.gs.js.map +1 -0
  154. package/dist/gs/math/modf.gs.d.ts +2 -0
  155. package/dist/gs/math/modf.gs.js +24 -0
  156. package/dist/gs/math/modf.gs.js.map +1 -0
  157. package/dist/gs/math/nextafter.gs.d.ts +2 -0
  158. package/dist/gs/math/nextafter.gs.js +66 -0
  159. package/dist/gs/math/nextafter.gs.js.map +1 -0
  160. package/dist/gs/math/pow.gs.d.ts +3 -0
  161. package/dist/gs/math/pow.gs.js +40 -0
  162. package/dist/gs/math/pow.gs.js.map +1 -0
  163. package/dist/gs/math/pow10.gs.d.ts +1 -0
  164. package/dist/gs/math/pow10.gs.js +14 -0
  165. package/dist/gs/math/pow10.gs.js.map +1 -0
  166. package/dist/gs/math/remainder.gs.d.ts +2 -0
  167. package/dist/gs/math/remainder.gs.js +25 -0
  168. package/dist/gs/math/remainder.gs.js.map +1 -0
  169. package/dist/gs/math/signbit.gs.d.ts +1 -0
  170. package/dist/gs/math/signbit.gs.js +5 -0
  171. package/dist/gs/math/signbit.gs.js.map +1 -0
  172. package/dist/gs/math/sin.gs.d.ts +4 -0
  173. package/dist/gs/math/sin.gs.js +29 -0
  174. package/dist/gs/math/sin.gs.js.map +1 -0
  175. package/dist/gs/math/sincos.gs.d.ts +1 -0
  176. package/dist/gs/math/sincos.gs.js +11 -0
  177. package/dist/gs/math/sincos.gs.js.map +1 -0
  178. package/dist/gs/math/sinh.gs.d.ts +4 -0
  179. package/dist/gs/math/sinh.gs.js +27 -0
  180. package/dist/gs/math/sinh.gs.js.map +1 -0
  181. package/dist/gs/math/sqrt.gs.d.ts +2 -0
  182. package/dist/gs/math/sqrt.gs.js +15 -0
  183. package/dist/gs/math/sqrt.gs.js.map +1 -0
  184. package/dist/gs/math/tan.gs.d.ts +2 -0
  185. package/dist/gs/math/tan.gs.js +17 -0
  186. package/dist/gs/math/tan.gs.js.map +1 -0
  187. package/dist/gs/math/tanh.gs.d.ts +2 -0
  188. package/dist/gs/math/tanh.gs.js +17 -0
  189. package/dist/gs/math/tanh.gs.js.map +1 -0
  190. package/dist/gs/math/trig_reduce.gs.d.ts +1 -0
  191. package/dist/gs/math/trig_reduce.gs.js +62 -0
  192. package/dist/gs/math/trig_reduce.gs.js.map +1 -0
  193. package/dist/gs/math/unsafe.gs.d.ts +4 -0
  194. package/dist/gs/math/unsafe.gs.js +47 -0
  195. package/dist/gs/math/unsafe.gs.js.map +1 -0
  196. package/dist/gs/slices/slices.d.ts +6 -0
  197. package/dist/gs/slices/slices.js +8 -0
  198. package/dist/gs/slices/slices.js.map +1 -1
  199. package/dist/gs/strconv/atob.gs.d.ts +4 -0
  200. package/dist/gs/strconv/atob.gs.js +42 -0
  201. package/dist/gs/strconv/atob.gs.js.map +1 -0
  202. package/dist/gs/strconv/atof.gs.d.ts +2 -0
  203. package/dist/gs/strconv/atof.gs.js +51 -0
  204. package/dist/gs/strconv/atof.gs.js.map +1 -0
  205. package/dist/gs/strconv/atoi.gs.d.ts +33 -0
  206. package/dist/gs/strconv/atoi.gs.js +200 -0
  207. package/dist/gs/strconv/atoi.gs.js.map +1 -0
  208. package/dist/gs/strconv/doc.gs.d.ts +1 -0
  209. package/dist/gs/strconv/doc.gs.js +2 -0
  210. package/dist/gs/strconv/doc.gs.js.map +1 -0
  211. package/dist/gs/strconv/ftoa.gs.d.ts +3 -0
  212. package/dist/gs/strconv/ftoa.gs.js +58 -0
  213. package/dist/gs/strconv/ftoa.gs.js.map +1 -0
  214. package/dist/gs/strconv/index.d.ts +6 -0
  215. package/dist/gs/strconv/index.js +7 -0
  216. package/dist/gs/strconv/index.js.map +1 -0
  217. package/dist/gs/strconv/itoa.gs.d.ts +6 -0
  218. package/dist/gs/strconv/itoa.gs.js +37 -0
  219. package/dist/gs/strconv/itoa.gs.js.map +1 -0
  220. package/dist/gs/strconv/quote.gs.d.ts +19 -0
  221. package/dist/gs/strconv/quote.gs.js +217 -0
  222. package/dist/gs/strconv/quote.gs.js.map +1 -0
  223. package/dist/gs/strings/index.d.ts +3 -0
  224. package/dist/gs/strings/index.js +4 -0
  225. package/dist/gs/strings/index.js.map +1 -1
  226. package/dist/gs/strings/replace.d.ts +0 -74
  227. package/dist/gs/strings/replace.js +6 -204
  228. package/dist/gs/strings/replace.js.map +1 -1
  229. package/dist/gs/strings/search.d.ts +0 -1
  230. package/dist/gs/strings/search.js +0 -21
  231. package/dist/gs/strings/search.js.map +1 -1
  232. package/gs/builtin/builtin.ts +40 -0
  233. package/gs/builtin/map.ts +12 -7
  234. package/gs/builtin/slice.ts +174 -34
  235. package/gs/github.com/pkg/errors/errors.ts +307 -0
  236. package/gs/github.com/pkg/errors/go113.ts +39 -0
  237. package/gs/github.com/pkg/errors/index.ts +3 -0
  238. package/gs/github.com/pkg/errors/stack.ts +127 -0
  239. package/gs/maps/index.ts +2 -0
  240. package/gs/maps/iter.ts +67 -0
  241. package/gs/maps/maps.ts +89 -0
  242. package/gs/math/TODO.md +156 -0
  243. package/gs/math/abs.gs.test.ts +29 -0
  244. package/gs/math/abs.gs.ts +13 -0
  245. package/gs/math/acosh.gs.test.ts +39 -0
  246. package/gs/math/acosh.gs.ts +21 -0
  247. package/gs/math/asin.gs.test.ts +66 -0
  248. package/gs/math/asin.gs.ts +27 -0
  249. package/gs/math/asinh.gs.test.ts +37 -0
  250. package/gs/math/asinh.gs.ts +21 -0
  251. package/gs/math/atan.gs.test.ts +49 -0
  252. package/gs/math/atan.gs.ts +27 -0
  253. package/gs/math/atan2.gs.test.ts +55 -0
  254. package/gs/math/atan2.gs.ts +37 -0
  255. package/gs/math/atanh.gs.test.ts +47 -0
  256. package/gs/math/atanh.gs.ts +21 -0
  257. package/gs/math/bits.gs.test.ts +88 -0
  258. package/gs/math/bits.gs.ts +61 -0
  259. package/gs/math/cbrt.gs.test.ts +57 -0
  260. package/gs/math/cbrt.gs.ts +20 -0
  261. package/gs/math/const.gs.test.ts +54 -0
  262. package/gs/math/const.gs.ts +93 -0
  263. package/gs/math/copysign.gs.test.ts +44 -0
  264. package/gs/math/copysign.gs.ts +27 -0
  265. package/gs/math/dim.gs.test.ts +102 -0
  266. package/gs/math/dim.gs.ts +84 -0
  267. package/gs/math/erf.gs.test.ts +92 -0
  268. package/gs/math/erf.gs.ts +409 -0
  269. package/gs/math/erfinv.gs.test.ts +104 -0
  270. package/gs/math/erfinv.gs.ts +169 -0
  271. package/gs/math/exp.gs.test.ts +82 -0
  272. package/gs/math/exp.gs.ts +39 -0
  273. package/gs/math/expm1.gs.test.ts +48 -0
  274. package/gs/math/expm1.gs.ts +23 -0
  275. package/gs/math/floor.gs.test.ts +146 -0
  276. package/gs/math/floor.gs.ts +88 -0
  277. package/gs/math/fma.gs.test.ts +83 -0
  278. package/gs/math/fma.gs.ts +7 -0
  279. package/gs/math/frexp.gs.test.ts +146 -0
  280. package/gs/math/frexp.gs.ts +37 -0
  281. package/gs/math/gamma.gs.test.ts +66 -0
  282. package/gs/math/gamma.gs.ts +158 -0
  283. package/gs/math/hypot.gs.test.ts +73 -0
  284. package/gs/math/hypot.gs.ts +23 -0
  285. package/gs/math/index.ts +44 -0
  286. package/gs/math/j0.gs.test.ts +74 -0
  287. package/gs/math/j0.gs.ts +257 -0
  288. package/gs/math/j1.gs.test.ts +81 -0
  289. package/gs/math/j1.gs.ts +231 -0
  290. package/gs/math/jn.gs.test.ts +133 -0
  291. package/gs/math/jn.gs.ts +447 -0
  292. package/gs/math/ldexp.gs.test.ts +128 -0
  293. package/gs/math/ldexp.gs.ts +28 -0
  294. package/gs/math/lgamma.gs.test.ts +102 -0
  295. package/gs/math/lgamma.gs.ts +251 -0
  296. package/gs/math/log.gs.test.ts +40 -0
  297. package/gs/math/log.gs.ts +21 -0
  298. package/gs/math/log10.gs.test.ts +80 -0
  299. package/gs/math/log10.gs.ts +25 -0
  300. package/gs/math/log1p.gs.test.ts +55 -0
  301. package/gs/math/log1p.gs.ts +24 -0
  302. package/gs/math/logb.gs.test.ts +87 -0
  303. package/gs/math/logb.gs.ts +54 -0
  304. package/gs/math/mod.gs.test.ts +64 -0
  305. package/gs/math/mod.gs.ts +36 -0
  306. package/gs/math/modf.gs.test.ts +80 -0
  307. package/gs/math/modf.gs.ts +32 -0
  308. package/gs/math/nextafter.gs.test.ts +107 -0
  309. package/gs/math/nextafter.gs.ts +71 -0
  310. package/gs/math/pow.gs.test.ts +103 -0
  311. package/gs/math/pow.gs.ts +55 -0
  312. package/gs/math/pow10.gs.test.ts +58 -0
  313. package/gs/math/pow10.gs.ts +19 -0
  314. package/gs/math/remainder.gs.test.ts +70 -0
  315. package/gs/math/remainder.gs.ts +33 -0
  316. package/gs/math/signbit.gs.test.ts +33 -0
  317. package/gs/math/signbit.gs.ts +8 -0
  318. package/gs/math/sin.gs.test.ts +83 -0
  319. package/gs/math/sin.gs.ts +38 -0
  320. package/gs/math/sincos.gs.test.ts +91 -0
  321. package/gs/math/sincos.gs.ts +15 -0
  322. package/gs/math/sinh.gs.test.ts +66 -0
  323. package/gs/math/sinh.gs.ts +34 -0
  324. package/gs/math/sqrt.gs.test.ts +49 -0
  325. package/gs/math/sqrt.gs.ts +20 -0
  326. package/gs/math/tan.gs.test.ts +50 -0
  327. package/gs/math/tan.gs.ts +23 -0
  328. package/gs/math/tanh.gs.test.ts +52 -0
  329. package/gs/math/tanh.gs.ts +23 -0
  330. package/gs/math/trig_reduce.gs.ts +66 -0
  331. package/gs/math/unsafe.gs.ts +52 -0
  332. package/gs/slices/slices.ts +9 -0
  333. package/gs/strconv/atob.gs.ts +45 -0
  334. package/gs/strconv/atof.gs.ts +60 -0
  335. package/gs/strconv/atoi.gs.ts +243 -0
  336. package/gs/strconv/doc.gs.ts +2 -0
  337. package/gs/strconv/ftoa.gs.ts +66 -0
  338. package/gs/strconv/index.ts +6 -0
  339. package/gs/strconv/itoa.gs.ts +41 -0
  340. package/gs/strconv/quote.gs.ts +245 -0
  341. package/gs/strings/index.ts +4 -0
  342. package/gs/strings/replace.ts +9 -237
  343. package/gs/strings/search.ts +0 -28
  344. package/package.json +1 -1
  345. package/gs/stringslite/godoc.txt +0 -17
  346. package/gs/stringslite/index.ts +0 -1
  347. package/gs/stringslite/strings.ts +0 -82
package/compiler/stmt.go CHANGED
@@ -4,6 +4,7 @@ import (
4
4
  "fmt"
5
5
  "go/ast"
6
6
  "go/token"
7
+ "go/types"
7
8
  "strings"
8
9
 
9
10
  "github.com/pkg/errors"
@@ -519,13 +520,17 @@ func (c *GoToTSCompiler) WriteStmtReturn(exp *ast.ReturnStmt) error {
519
520
  }
520
521
  }
521
522
 
522
- if len(namedReturns) > 0 {
523
+ if len(namedReturns) == 1 {
524
+ // Single named return - don't wrap in array
525
+ c.tsw.WriteLiterally(c.sanitizeIdentifier(namedReturns[0]))
526
+ } else if len(namedReturns) > 1 {
527
+ // Multiple named returns - wrap in array
523
528
  c.tsw.WriteLiterally("[")
524
529
  for i, name := range namedReturns {
525
530
  if i != 0 {
526
531
  c.tsw.WriteLiterally(", ")
527
532
  }
528
- c.tsw.WriteLiterally(name)
533
+ c.tsw.WriteLiterally(c.sanitizeIdentifier(name))
529
534
  }
530
535
  c.tsw.WriteLiterally("]")
531
536
  }
@@ -538,6 +543,25 @@ func (c *GoToTSCompiler) WriteStmtReturn(exp *ast.ReturnStmt) error {
538
543
  if i != 0 {
539
544
  c.tsw.WriteLiterally(", ")
540
545
  }
546
+ // Special handling for nil in generic function returns
547
+ if ident, isIdent := res.(*ast.Ident); isIdent && ident.Name == "nil" {
548
+ // Check if we're in a generic function and get the return type
549
+ if nodeInfo := c.analysis.NodeData[exp]; nodeInfo != nil && nodeInfo.EnclosingFuncDecl != nil {
550
+ funcDecl := nodeInfo.EnclosingFuncDecl
551
+ if funcDecl.Type.Results != nil && i < len(funcDecl.Type.Results.List) {
552
+ // Get the return type for this result position
553
+ resultField := funcDecl.Type.Results.List[i]
554
+ if resultType := c.pkg.TypesInfo.TypeOf(resultField.Type); resultType != nil {
555
+ if _, isTypeParam := resultType.(*types.TypeParam); isTypeParam {
556
+ // This is a generic type parameter, use type assertion with unknown intermediate
557
+ c.tsw.WriteLiterally("null as unknown as ")
558
+ c.WriteGoType(resultType, GoTypeContextFunctionReturn)
559
+ continue
560
+ }
561
+ }
562
+ }
563
+ }
564
+ }
541
565
  if err := c.WriteValueExpr(res); err != nil { // Return results are values
542
566
  return err
543
567
  }
@@ -829,15 +853,28 @@ func (c *GoToTSCompiler) WriteStmtDefer(exp *ast.DeferStmt) error {
829
853
  }
830
854
 
831
855
  // WriteStmtLabeled handles labeled statements (ast.LabeledStmt), such as "label: statement".
832
- // In TypeScript, this translates to "label: statement" directly.
856
+ // In TypeScript, labels cannot be used with variable declarations, so we need to handle this case specially.
833
857
  func (c *GoToTSCompiler) WriteStmtLabeled(stmt *ast.LabeledStmt) error {
834
- // Write the label name followed by a colon
835
- c.tsw.WriteLiterally(stmt.Label.Name)
836
- c.tsw.WriteLiterally(": ")
858
+ // Check if the labeled statement is a declaration statement
859
+ if declStmt, ok := stmt.Stmt.(*ast.DeclStmt); ok {
860
+ // For declaration statements, we need to put the label on a separate line
861
+ // because TypeScript doesn't allow labels with declarations
862
+ c.tsw.WriteLiterally(stmt.Label.Name)
863
+ c.tsw.WriteLine(": {}")
864
+
865
+ // Write the declaration statement without the label
866
+ if err := c.WriteStmt(declStmt); err != nil {
867
+ return fmt.Errorf("failed to write labeled declaration statement: %w", err)
868
+ }
869
+ } else {
870
+ // For non-declaration statements, write the label normally
871
+ c.tsw.WriteLiterally(stmt.Label.Name)
872
+ c.tsw.WriteLiterally(": ")
837
873
 
838
- // Write the labeled statement
839
- if err := c.WriteStmt(stmt.Stmt); err != nil {
840
- return fmt.Errorf("failed to write labeled statement: %w", err)
874
+ // Write the labeled statement
875
+ if err := c.WriteStmt(stmt.Stmt); err != nil {
876
+ return fmt.Errorf("failed to write labeled statement: %w", err)
877
+ }
841
878
  }
842
879
 
843
880
  return nil
package/compiler/type.go CHANGED
@@ -3,6 +3,7 @@ package compiler
3
3
  import (
4
4
  "fmt"
5
5
  "go/ast"
6
+ "go/token"
6
7
  "go/types"
7
8
  "strings"
8
9
  )
@@ -67,7 +68,8 @@ func (c *GoToTSCompiler) WriteGoType(typ types.Type, context GoTypeContext) {
67
68
  c.WriteGoType(t.Underlying(), context)
68
69
  case *types.TypeParam:
69
70
  // For type parameters, write the type parameter name (e.g., "T", "K", etc.)
70
- c.tsw.WriteLiterally(t.Obj().Name())
71
+ // Use sanitizeIdentifier to handle conflicts with TypeScript built-in types
72
+ c.tsw.WriteLiterally(c.sanitizeIdentifier(t.Obj().Name()))
71
73
  case *types.Union:
72
74
  // Handle union types (e.g., string | []byte)
73
75
  for i := 0; i < t.Len(); i++ {
@@ -171,9 +173,9 @@ func (c *GoToTSCompiler) WriteZeroValueForType(typ any) {
171
173
  // For anonymous struct types, initialize with {}
172
174
  c.tsw.WriteLiterally("{}")
173
175
  case *types.TypeParam:
174
- // For type parameters, use null! (non-null assertion) to avoid TypeScript
175
- // casting errors with union types like string | Uint8Array
176
- c.tsw.WriteLiterally("null!")
176
+ // For type parameters, use null with type assertion to work around TypeScript's strict checking
177
+ // This allows null to be assigned to generic type parameters even when the constraint doesn't explicitly include null
178
+ c.tsw.WriteLiterally("null as any")
177
179
  default:
178
180
  c.tsw.WriteLiterally("null")
179
181
  }
@@ -311,7 +313,7 @@ func (c *GoToTSCompiler) WritePointerType(t *types.Pointer, context GoTypeContex
311
313
  func (c *GoToTSCompiler) WriteSliceType(t *types.Slice) {
312
314
  // Check if it's a []byte slice
313
315
  if elem, ok := t.Elem().(*types.Basic); ok && elem.Kind() == types.Uint8 {
314
- c.tsw.WriteLiterally("Uint8Array")
316
+ c.tsw.WriteLiterally("$.Bytes")
315
317
  return
316
318
  }
317
319
  c.tsw.WriteLiterally("$.Slice<")
@@ -367,12 +369,12 @@ func (c *GoToTSCompiler) WriteFuncType(exp *ast.FuncType, isAsync bool) {
367
369
  if isAsync {
368
370
  c.tsw.WriteLiterally("Promise<")
369
371
  }
370
- if len(exp.Results.List) == 1 && len(exp.Results.List[0].Names) == 0 {
371
- // Single unnamed return type
372
+ if len(exp.Results.List) == 1 {
373
+ // Single return type (named or unnamed)
372
374
  typ := c.pkg.TypesInfo.TypeOf(exp.Results.List[0].Type)
373
375
  c.WriteGoType(typ, GoTypeContextFunctionReturn)
374
376
  } else {
375
- // Multiple or named return types -> tuple
377
+ // Multiple return types -> tuple
376
378
  c.tsw.WriteLiterally("[")
377
379
  for i, field := range exp.Results.List {
378
380
  if i > 0 {
@@ -629,7 +631,8 @@ func (c *GoToTSCompiler) WriteTypeParameters(typeParams *ast.FieldList) {
629
631
  if j > 0 {
630
632
  c.tsw.WriteLiterally(", ")
631
633
  }
632
- c.tsw.WriteLiterally(name.Name)
634
+ // Use sanitizeIdentifier to handle conflicts with TypeScript built-in types
635
+ c.tsw.WriteLiterally(c.sanitizeIdentifier(name.Name))
633
636
 
634
637
  // Write constraint if present
635
638
  if field.Type != nil {
@@ -661,7 +664,7 @@ func (c *GoToTSCompiler) WriteTypeConstraint(constraint ast.Expr) {
661
664
  }
662
665
  case *ast.BinaryExpr:
663
666
  // Handle union types like []byte | string
664
- if t.Op.String() == "|" {
667
+ if t.Op == token.OR {
665
668
  c.WriteTypeConstraint(t.X)
666
669
  c.tsw.WriteLiterally(" | ")
667
670
  c.WriteTypeConstraint(t.Y)
@@ -675,7 +678,7 @@ func (c *GoToTSCompiler) WriteTypeConstraint(constraint ast.Expr) {
675
678
  case *ast.ArrayType:
676
679
  // Handle []byte specifically
677
680
  if ident, ok := t.Elt.(*ast.Ident); ok && ident.Name == "byte" {
678
- c.tsw.WriteLiterally("Uint8Array")
681
+ c.tsw.WriteLiterally("$.Bytes")
679
682
  } else {
680
683
  c.WriteTypeExpr(constraint)
681
684
  }
@@ -5,6 +5,8 @@ export * from './io.js';
5
5
  export * from './map.js';
6
6
  export * from './slice.js';
7
7
  export * from './type.js';
8
+ import type { Slice } from './slice.js';
9
+ export type Bytes = Uint8Array | Slice<number>;
8
10
  export declare function int(value: number): number;
9
11
  export declare function copy<T>(dst: T[] | Uint8Array, src: T[] | Uint8Array | string): number;
10
12
  export declare function multiplyDuration(duration: any, multiplier: number): any;
@@ -26,3 +28,9 @@ export declare function normalizeBytes(bytes: Uint8Array | number[] | null | und
26
28
  } | {
27
29
  valueOf(): number[];
28
30
  }): Uint8Array;
31
+ /**
32
+ * sortSlice sorts a slice in ascending order.
33
+ * Handles all slice types including null, arrays, Uint8Array, and SliceProxy.
34
+ * @param s The slice to sort in place
35
+ */
36
+ export declare function sortSlice<T extends string | number>(s: Slice<T>): void;
@@ -5,6 +5,7 @@ export * from './io.js';
5
5
  export * from './map.js';
6
6
  export * from './slice.js';
7
7
  export * from './type.js';
8
+ import { isSliceProxy } from './slice.js';
8
9
  // int converts a value to a Go int type, handling proper signed integer conversion
9
10
  // This ensures that values like 2147483648 (2^31) are properly handled according to Go semantics
10
11
  export function int(value) {
@@ -134,4 +135,34 @@ export function normalizeBytes(bytes) {
134
135
  }
135
136
  throw new Error(`Cannot normalize bytes of type ${typeof bytes}: ${bytes}`);
136
137
  }
138
+ /**
139
+ * sortSlice sorts a slice in ascending order.
140
+ * Handles all slice types including null, arrays, Uint8Array, and SliceProxy.
141
+ * @param s The slice to sort in place
142
+ */
143
+ export function sortSlice(s) {
144
+ if (s === null || s === undefined) {
145
+ return; // Nothing to sort for nil slice
146
+ }
147
+ if (Array.isArray(s)) {
148
+ s.sort();
149
+ return;
150
+ }
151
+ if (s instanceof Uint8Array) {
152
+ s.sort();
153
+ return;
154
+ }
155
+ // Handle SliceProxy case - sort the backing array in-place within the slice bounds
156
+ if (isSliceProxy(s)) {
157
+ const proxy = s;
158
+ const meta = proxy.__meta__;
159
+ const section = meta.backing.slice(meta.offset, meta.offset + meta.length);
160
+ section.sort();
161
+ // Copy sorted section back to the backing array
162
+ for (let i = 0; i < section.length; i++) {
163
+ meta.backing[meta.offset + i] = section[i];
164
+ }
165
+ return;
166
+ }
167
+ }
137
168
  //# sourceMappingURL=builtin.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"builtin.js","sourceRoot":"","sources":["../../../gs/builtin/builtin.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,cAAc,CAAA;AAC5B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,UAAU,CAAA;AACxB,cAAc,YAAY,CAAA;AAC1B,cAAc,WAAW,CAAA;AAEzB,mFAAmF;AACnF,iGAAiG;AACjG,MAAM,UAAU,GAAG,CAAC,KAAa;IAC/B,0FAA0F;IAC1F,wFAAwF;IACxF,6FAA6F;IAC7F,kDAAkD;IAElD,yEAAyE;IACzE,uFAAuF;IACvF,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAA;AAC1B,CAAC;AAED,oFAAoF;AACpF,4CAA4C;AAC5C,MAAM,UAAU,IAAI,CAClB,GAAqB,EACrB,GAA8B;IAE9B,kDAAkD;IAClD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,GAAG,YAAY,UAAU,EAAE,CAAC;QACzD,MAAM,OAAO,GAAG,IAAI,WAAW,EAAE,CAAA;QACjC,MAAM,QAAQ,GAAG,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QACpC,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAA;QAC/C,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;YAC3B,GAAG,CAAC,CAAC,CAAC,GAAG,QAAQ,CAAC,CAAC,CAAC,CAAA;QACtB,CAAC;QACD,OAAO,CAAC,CAAA;IACV,CAAC;IAED,uCAAuC;IACvC,IAAI,GAA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,CAA4B,CAAW;IAC9D,IAAI,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,SAAS,EAAE,CAAC;QAClC,OAAM,CAAC,gCAAgC;IACzC,CAAC;IAED,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,CAAC;QACrB,CAAC,CAAC,IAAI,EAAE,CAAA;QACR,OAAM;IACR,CAAC;IAED,IAAI,CAAC,YAAY,UAAU,EAAE,CAAC;QAC5B,CAAC,CAAC,IAAI,EAAE,CAAA;QACR,OAAM;IACR,CAAC;IAED,mFAAmF;IACnF,IAAI,YAAY,CAAC,CAAC,CAAC,EAAE,CAAC;QACpB,MAAM,KAAK,GAAG,CAAkB,CAAA;QAChC,MAAM,IAAI,GAAG,KAAK,CAAC,QAAQ,CAAA;QAC3B,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,MAAM,CAAC,CAAA;QAC1E,OAAO,CAAC,IAAI,EAAE,CAAA;QACd,gDAAgD;QAChD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACxC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAA;QAC5C,CAAC;QACD,OAAM;IACR,CAAC;AACH,CAAC"}
@@ -4,13 +4,13 @@
4
4
  */
5
5
  export declare const makeMap: <K, V>() => Map<K, V>;
6
6
  /**
7
- * Gets a value from a map, with a default value if the key doesn't exist.
7
+ * Gets a value from a map, returning a tuple [value, exists].
8
8
  * @param map The map to get from.
9
9
  * @param key The key to get.
10
- * @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
11
- * @returns The value for the key, or the default value if the key doesn't exist.
10
+ * @param defaultValue The default value to return if the key doesn't exist.
11
+ * @returns A tuple [value, exists] where value is the map value or defaultValue, and exists is whether the key was found.
12
12
  */
13
- export declare const mapGet: <K, V>(map: Map<K, V>, key: K, defaultValue?: V | null) => V | null;
13
+ export declare function mapGet<K, V, D>(map: Map<K, V>, key: K, defaultValue: D): [V, true] | [D, false];
14
14
  /**
15
15
  * Sets a value in a map.
16
16
  * @param map The map to set in.
@@ -6,15 +6,21 @@ export const makeMap = () => {
6
6
  return new Map();
7
7
  };
8
8
  /**
9
- * Gets a value from a map, with a default value if the key doesn't exist.
9
+ * Gets a value from a map, returning a tuple [value, exists].
10
10
  * @param map The map to get from.
11
11
  * @param key The key to get.
12
- * @param defaultValue The default value to return if the key doesn't exist (defaults to 0).
13
- * @returns The value for the key, or the default value if the key doesn't exist.
12
+ * @param defaultValue The default value to return if the key doesn't exist.
13
+ * @returns A tuple [value, exists] where value is the map value or defaultValue, and exists is whether the key was found.
14
14
  */
15
- export const mapGet = (map, key, defaultValue = null) => {
16
- return map.has(key) ? map.get(key) : defaultValue;
17
- };
15
+ export function mapGet(map, key, defaultValue) {
16
+ const exists = map.has(key);
17
+ if (exists) {
18
+ return [map.get(key), true];
19
+ }
20
+ else {
21
+ return [defaultValue, false];
22
+ }
23
+ }
18
24
  /**
19
25
  * Sets a value in a map.
20
26
  * @param map The map to set in.
@@ -1 +1 @@
1
- {"version":3,"file":"map.js","sourceRoot":"","sources":["../../../gs/builtin/map.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,GAAoB,EAAE;IAC3C,OAAO,IAAI,GAAG,EAAQ,CAAA;AACxB,CAAC,CAAA;AACD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CACpB,GAAc,EACd,GAAM,EACN,eAAyB,IAAI,EACnB,EAAE;IACZ,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAC,CAAC,YAAY,CAAA;AAC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AIG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAO,GAAc,EAAE,GAAM,EAAQ,EAAE;IACnE,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;AACjB,CAAC,CAAA;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,MAAM,GAAG,CAAO,GAAc,EAAE,GAAM,EAAW,EAAE;IAC9D,OAAO,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;AACrB,CAAC,CAAA"}
@@ -21,6 +21,11 @@ export type SliceProxy<T> = T[] & {
21
21
  */
22
22
  export type Slice<T> = T[] | SliceProxy<T> | null | (T extends number ? Uint8Array : never);
23
23
  export declare function asArray<T>(slice: Slice<T>): T[];
24
+ /**
25
+ * isSliceProxy checks if a slice is a SliceProxy (has __meta__ property)
26
+ * This is an alias for isComplexSlice for better type hinting
27
+ */
28
+ export declare function isSliceProxy<T>(slice: Slice<T>): slice is SliceProxy<T>;
24
29
  /**
25
30
  * Creates a new slice with the specified length and capacity.
26
31
  * @param length The length of the slice.
@@ -67,7 +72,8 @@ export declare const cap: <T>(obj: Slice<T> | Uint8Array) => number;
67
72
  * @param elements The elements to append.
68
73
  * @returns The modified or new slice.
69
74
  */
70
- export declare const append: <T>(slice: Slice<T> | Uint8Array, ...elements: any[]) => Slice<T>;
75
+ export declare function append(slice: Uint8Array, ...elements: any[]): Uint8Array;
76
+ export declare function append<T>(slice: Slice<T>, ...elements: any[]): Slice<T>;
71
77
  /**
72
78
  * Copies elements from src to dst.
73
79
  * @param dst The destination slice.
@@ -152,22 +158,22 @@ export declare function stringToBytes(s: string): Uint8Array;
152
158
  */
153
159
  export declare function genericBytesOrStringToString(value: string | Uint8Array): string;
154
160
  /**
155
- * Indexes into a value that could be either a string or Uint8Array.
161
+ * Indexes into a value that could be either a string or bytes.
156
162
  * Used for generic type parameters with constraint string | []byte.
157
163
  * Both cases return a byte value (number).
158
- * @param value Value that is either a string or Uint8Array
164
+ * @param value Value that is either a string or bytes (Uint8Array or Slice<number>)
159
165
  * @param index The index to access
160
166
  * @returns The byte value at the specified index
161
167
  */
162
- export declare function indexStringOrBytes(value: string | Uint8Array, index: number): number;
168
+ export declare function indexStringOrBytes(value: string | import('./builtin.js').Bytes, index: number): number;
163
169
  /**
164
- * Slices a value that could be either a string or Uint8Array.
170
+ * Slices a value that could be either a string or bytes.
165
171
  * Used for generic type parameters with constraint string | []byte.
166
- * @param value Value that is either a string or Uint8Array
172
+ * @param value Value that is either a string or bytes (Uint8Array or Slice<number>)
167
173
  * @param low Starting index (inclusive). Defaults to 0.
168
174
  * @param high Ending index (exclusive). Defaults to length.
169
- * @param max Capacity limit (only used for Uint8Array, ignored for strings)
175
+ * @param max Capacity limit (only used for bytes, ignored for strings)
170
176
  * @returns The sliced value of the same type as input
171
177
  */
172
- export declare function sliceStringOrBytes<T extends string | Uint8Array>(value: T, low?: number, high?: number, max?: number): T;
178
+ export declare function sliceStringOrBytes<T extends string | import('./builtin.js').Bytes>(value: T, low?: number, high?: number, max?: number): T;
173
179
  export {};
@@ -28,6 +28,13 @@ function isComplexSlice(slice) {
28
28
  '__meta__' in slice &&
29
29
  slice.__meta__ !== undefined);
30
30
  }
31
+ /**
32
+ * isSliceProxy checks if a slice is a SliceProxy (has __meta__ property)
33
+ * This is an alias for isComplexSlice for better type hinting
34
+ */
35
+ export function isSliceProxy(slice) {
36
+ return isComplexSlice(slice);
37
+ }
31
38
  /**
32
39
  * Creates a new slice with the specified length and capacity.
33
40
  * @param length The length of the slice.
@@ -36,9 +43,63 @@ function isComplexSlice(slice) {
36
43
  */
37
44
  export const makeSlice = (length, capacity, typeHint) => {
38
45
  if (typeHint === 'byte') {
39
- // Uint8Array is initialized to zeros by default.
40
- // Capacity for Uint8Array is its length.
41
- return new Uint8Array(length);
46
+ const actualCapacity = capacity === undefined ? length : capacity;
47
+ if (length < 0 || actualCapacity < 0 || length > actualCapacity) {
48
+ throw new Error(`Invalid slice length (${length}) or capacity (${actualCapacity})`);
49
+ }
50
+ // If capacity equals length, use Uint8Array directly for efficiency
51
+ if (actualCapacity === length) {
52
+ return new Uint8Array(length);
53
+ }
54
+ // If capacity > length, create a SliceProxy backed by a Uint8Array
55
+ const backingUint8 = new Uint8Array(actualCapacity);
56
+ const backingNumbers = Array.from(backingUint8); // Convert to number[] for backing
57
+ const proxyTargetArray = new Array(length);
58
+ for (let i = 0; i < length; i++) {
59
+ proxyTargetArray[i] = 0; // Initialize with zeros
60
+ }
61
+ const proxy = proxyTargetArray;
62
+ proxy.__meta__ = {
63
+ backing: backingNumbers,
64
+ offset: 0,
65
+ length: length,
66
+ capacity: actualCapacity,
67
+ };
68
+ // Create a proper Proxy with the handler for SliceProxy behavior
69
+ const handler = {
70
+ get(target, prop) {
71
+ if (typeof prop === 'string' && /^\d+$/.test(prop)) {
72
+ const index = Number(prop);
73
+ if (index >= 0 && index < target.__meta__.length) {
74
+ return target.__meta__.backing[target.__meta__.offset + index];
75
+ }
76
+ throw new Error(`Slice index out of range: ${index} >= ${target.__meta__.length}`);
77
+ }
78
+ if (prop === 'length') {
79
+ return target.__meta__.length;
80
+ }
81
+ if (prop === '__meta__') {
82
+ return target.__meta__;
83
+ }
84
+ return Reflect.get(target, prop);
85
+ },
86
+ set(target, prop, value) {
87
+ if (typeof prop === 'string' && /^\d+$/.test(prop)) {
88
+ const index = Number(prop);
89
+ if (index >= 0 && index < target.__meta__.length) {
90
+ target.__meta__.backing[target.__meta__.offset + index] = value;
91
+ target[index] = value; // Also update the proxy target for consistency
92
+ return true;
93
+ }
94
+ throw new Error(`Slice index out of range: ${index} >= ${target.__meta__.length}`);
95
+ }
96
+ if (prop === 'length' || prop === '__meta__') {
97
+ return false;
98
+ }
99
+ return Reflect.set(target, prop, value);
100
+ },
101
+ };
102
+ return new Proxy(proxy, handler);
42
103
  }
43
104
  const actualCapacity = capacity === undefined ? length : capacity;
44
105
  if (length < 0 || actualCapacity < 0 || length > actualCapacity) {
@@ -78,7 +139,41 @@ export const makeSlice = (length, capacity, typeHint) => {
78
139
  length: length,
79
140
  capacity: actualCapacity,
80
141
  };
81
- return proxy;
142
+ // Create a proper Proxy with the handler for SliceProxy behavior
143
+ const handler = {
144
+ get(target, prop) {
145
+ if (typeof prop === 'string' && /^\d+$/.test(prop)) {
146
+ const index = Number(prop);
147
+ if (index >= 0 && index < target.__meta__.length) {
148
+ return target.__meta__.backing[target.__meta__.offset + index];
149
+ }
150
+ throw new Error(`Slice index out of range: ${index} >= ${target.__meta__.length}`);
151
+ }
152
+ if (prop === 'length') {
153
+ return target.__meta__.length;
154
+ }
155
+ if (prop === '__meta__') {
156
+ return target.__meta__;
157
+ }
158
+ return Reflect.get(target, prop);
159
+ },
160
+ set(target, prop, value) {
161
+ if (typeof prop === 'string' && /^\d+$/.test(prop)) {
162
+ const index = Number(prop);
163
+ if (index >= 0 && index < target.__meta__.length) {
164
+ target.__meta__.backing[target.__meta__.offset + index] = value;
165
+ target[index] = value; // Also update the proxy target for consistency
166
+ return true;
167
+ }
168
+ throw new Error(`Slice index out of range: ${index} >= ${target.__meta__.length}`);
169
+ }
170
+ if (prop === 'length' || prop === '__meta__') {
171
+ return false;
172
+ }
173
+ return Reflect.set(target, prop, value);
174
+ },
175
+ };
176
+ return new Proxy(proxy, handler);
82
177
  };
83
178
  /**
84
179
  * goSlice creates a slice from s[low:high:max]
@@ -234,16 +329,19 @@ s, low, high, max) => {
234
329
  }
235
330
  const newLength = high - low;
236
331
  const newOffset = oldOffset + low;
237
- const target = {
238
- __meta__: {
239
- backing: backing,
240
- offset: newOffset,
241
- length: newLength,
242
- capacity: newCap,
243
- },
332
+ // Create an array-like target with the correct length
333
+ const proxyTargetArray = new Array(newLength);
334
+ // Note: We don't need to initialize the values here since the proxy handler
335
+ // will fetch them from the backing array when accessed
336
+ const proxy = proxyTargetArray;
337
+ proxy.__meta__ = {
338
+ backing: backing,
339
+ offset: newOffset,
340
+ length: newLength,
341
+ capacity: newCap,
244
342
  };
245
343
  // const handler = { ... } // Handler is now defined at the top
246
- return new Proxy(target, handler);
344
+ return new Proxy(proxy, handler);
247
345
  };
248
346
  /**
249
347
  * Converts a JavaScript array to a Go slice.
@@ -369,15 +467,7 @@ export const cap = (obj) => {
369
467
  }
370
468
  return 0;
371
469
  };
372
- /**
373
- * Appends elements to a slice.
374
- * Note: In Go, append can return a new slice if the underlying array is reallocated.
375
- * This helper emulates that by returning the modified or new slice.
376
- * @param slice The slice to append to.
377
- * @param elements The elements to append.
378
- * @returns The modified or new slice.
379
- */
380
- export const append = (slice, ...elements) => {
470
+ export function append(slice, ...elements) {
381
471
  // 1. Flatten all elements from the varargs `...elements` into `varargsElements`.
382
472
  // Determine if the result should be a Uint8Array.
383
473
  const inputIsUint8Array = slice instanceof Uint8Array;
@@ -508,7 +598,7 @@ export const append = (slice, ...elements) => {
508
598
  capacity: newCapacity,
509
599
  };
510
600
  return resultProxy;
511
- };
601
+ }
512
602
  /**
513
603
  * Copies elements from src to dst.
514
604
  * @param dst The destination slice.
@@ -730,8 +820,7 @@ export const sliceString = (str, low, high) => {
730
820
  const result = new TextDecoder('utf-8', { fatal: true }).decode(slicedBytes);
731
821
  return result;
732
822
  }
733
- catch (_e) {
734
- //eslint-disable-line @typescript-eslint/no-unused-vars
823
+ catch (_e) { //eslint-disable-line @typescript-eslint/no-unused-vars
735
824
  // If we get here, the slice would create invalid UTF-8
736
825
  // This is a fundamental limitation of JavaScript string handling
737
826
  throw new Error(`Cannot slice string at byte indices [${actualLow}:${actualHigh}] because it would create invalid UTF-8. ` +
@@ -785,10 +874,10 @@ export function genericBytesOrStringToString(value) {
785
874
  return bytesToString(value);
786
875
  }
787
876
  /**
788
- * Indexes into a value that could be either a string or Uint8Array.
877
+ * Indexes into a value that could be either a string or bytes.
789
878
  * Used for generic type parameters with constraint string | []byte.
790
879
  * Both cases return a byte value (number).
791
- * @param value Value that is either a string or Uint8Array
880
+ * @param value Value that is either a string or bytes (Uint8Array or Slice<number>)
792
881
  * @param index The index to access
793
882
  * @returns The byte value at the specified index
794
883
  */
@@ -796,21 +885,32 @@ export function indexStringOrBytes(value, index) {
796
885
  if (typeof value === 'string') {
797
886
  return indexString(value, index);
798
887
  }
799
- else {
888
+ else if (value instanceof Uint8Array) {
800
889
  // For Uint8Array, direct access returns the byte value
801
890
  if (index < 0 || index >= value.length) {
802
891
  throw new Error(`runtime error: index out of range [${index}] with length ${value.length}`);
803
892
  }
804
893
  return value[index];
805
894
  }
895
+ else if (value === null) {
896
+ throw new Error(`runtime error: index out of range [${index}] with length 0`);
897
+ }
898
+ else {
899
+ // For Slice<number> (including SliceProxy)
900
+ const length = len(value);
901
+ if (index < 0 || index >= length) {
902
+ throw new Error(`runtime error: index out of range [${index}] with length ${length}`);
903
+ }
904
+ return value[index];
905
+ }
806
906
  }
807
907
  /**
808
- * Slices a value that could be either a string or Uint8Array.
908
+ * Slices a value that could be either a string or bytes.
809
909
  * Used for generic type parameters with constraint string | []byte.
810
- * @param value Value that is either a string or Uint8Array
910
+ * @param value Value that is either a string or bytes (Uint8Array or Slice<number>)
811
911
  * @param low Starting index (inclusive). Defaults to 0.
812
912
  * @param high Ending index (exclusive). Defaults to length.
813
- * @param max Capacity limit (only used for Uint8Array, ignored for strings)
913
+ * @param max Capacity limit (only used for bytes, ignored for strings)
814
914
  * @returns The sliced value of the same type as input
815
915
  */
816
916
  export function sliceStringOrBytes(value, low, high, max) {
@@ -819,7 +919,7 @@ export function sliceStringOrBytes(value, low, high, max) {
819
919
  return sliceString(value, low, high);
820
920
  }
821
921
  else {
822
- // For Uint8Array, use goSlice
922
+ // For bytes (Uint8Array or Slice<number>), use goSlice
823
923
  return goSlice(value, low, high, max);
824
924
  }
825
925
  }