goscript 0.1.1 → 0.1.2

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 (356) hide show
  1. package/cmd/goscript/cmd-test.go +104 -11
  2. package/cmd/goscript/cmd-test_test.go +1 -1
  3. package/cmd/goscript/cmd_compile.go +9 -0
  4. package/compiler/compile-request.go +31 -0
  5. package/compiler/compiler.go +1 -1
  6. package/compiler/compliance_test.go +0 -2
  7. package/compiler/config.go +2 -0
  8. package/compiler/gotest/package-result.go +2 -0
  9. package/compiler/gotest/request.go +85 -20
  10. package/compiler/gotest/runner.go +733 -96
  11. package/compiler/gotest/runner_test.go +647 -3
  12. package/compiler/lowered-program.go +9 -2
  13. package/compiler/lowering.go +2001 -345
  14. package/compiler/override-facts.go +77 -27
  15. package/compiler/override-registry.go +5 -4
  16. package/compiler/override-registry_test.go +135 -0
  17. package/compiler/package-graph_test.go +62 -7
  18. package/compiler/package-test-graph-variant.go +40 -16
  19. package/compiler/package-test-graph.go +0 -5
  20. package/compiler/package-test-graph_test.go +61 -3
  21. package/compiler/runtime-contract.go +40 -0
  22. package/compiler/semantic-model-types.go +16 -0
  23. package/compiler/semantic-model.go +336 -91
  24. package/compiler/semantic-model_test.go +50 -1
  25. package/compiler/service.go +9 -3
  26. package/compiler/skeleton_test.go +1921 -298
  27. package/compiler/tsworkspace/owner-process-unix_test.go +72 -0
  28. package/compiler/tsworkspace/owner.go +8 -0
  29. package/compiler/tsworkspace/tool-process-other.go +14 -0
  30. package/compiler/tsworkspace/tool-process-unix.go +19 -0
  31. package/compiler/typescript-emitter.go +122 -9
  32. package/dist/gs/builtin/builtin.d.ts +20 -1
  33. package/dist/gs/builtin/builtin.js +246 -26
  34. package/dist/gs/builtin/builtin.js.map +1 -1
  35. package/dist/gs/builtin/channel.d.ts +24 -10
  36. package/dist/gs/builtin/channel.js +107 -25
  37. package/dist/gs/builtin/channel.js.map +1 -1
  38. package/dist/gs/builtin/defer.d.ts +1 -0
  39. package/dist/gs/builtin/defer.js +12 -2
  40. package/dist/gs/builtin/defer.js.map +1 -1
  41. package/dist/gs/builtin/hostio.d.ts +9 -0
  42. package/dist/gs/builtin/hostio.js +25 -0
  43. package/dist/gs/builtin/hostio.js.map +1 -1
  44. package/dist/gs/builtin/map.js +40 -6
  45. package/dist/gs/builtin/map.js.map +1 -1
  46. package/dist/gs/builtin/print.js.map +1 -1
  47. package/dist/gs/builtin/slice.d.ts +43 -9
  48. package/dist/gs/builtin/slice.js +437 -234
  49. package/dist/gs/builtin/slice.js.map +1 -1
  50. package/dist/gs/builtin/type.d.ts +2 -0
  51. package/dist/gs/builtin/type.js +47 -7
  52. package/dist/gs/builtin/type.js.map +1 -1
  53. package/dist/gs/builtin/varRef.d.ts +2 -0
  54. package/dist/gs/builtin/varRef.js.map +1 -1
  55. package/dist/gs/bytes/buffer.gs.js +28 -28
  56. package/dist/gs/bytes/buffer.gs.js.map +1 -1
  57. package/dist/gs/bytes/iter.gs.js +13 -13
  58. package/dist/gs/bytes/iter.gs.js.map +1 -1
  59. package/dist/gs/compress/zlib/index.d.ts +26 -0
  60. package/dist/gs/compress/zlib/index.js +168 -0
  61. package/dist/gs/compress/zlib/index.js.map +1 -0
  62. package/dist/gs/context/context.d.ts +1 -1
  63. package/dist/gs/context/context.js +8 -3
  64. package/dist/gs/context/context.js.map +1 -1
  65. package/dist/gs/crypto/ecdh/index.d.ts +52 -0
  66. package/dist/gs/crypto/ecdh/index.js +226 -0
  67. package/dist/gs/crypto/ecdh/index.js.map +1 -0
  68. package/dist/gs/crypto/ed25519/index.d.ts +34 -0
  69. package/dist/gs/crypto/ed25519/index.js +160 -0
  70. package/dist/gs/crypto/ed25519/index.js.map +1 -0
  71. package/dist/gs/crypto/internal/constanttime/index.d.ts +4 -0
  72. package/dist/gs/crypto/internal/constanttime/index.js +18 -0
  73. package/dist/gs/crypto/internal/constanttime/index.js.map +1 -0
  74. package/dist/gs/crypto/rand/index.d.ts +2 -0
  75. package/dist/gs/crypto/rand/index.js +85 -0
  76. package/dist/gs/crypto/rand/index.js.map +1 -1
  77. package/dist/gs/crypto/sha256/index.d.ts +8 -0
  78. package/dist/gs/crypto/sha256/index.js +118 -0
  79. package/dist/gs/crypto/sha256/index.js.map +1 -0
  80. package/dist/gs/crypto/sha512/index.d.ts +14 -0
  81. package/dist/gs/crypto/sha512/index.js +129 -0
  82. package/dist/gs/crypto/sha512/index.js.map +1 -0
  83. package/dist/gs/encoding/json/index.d.ts +3 -0
  84. package/dist/gs/encoding/json/index.js +15 -0
  85. package/dist/gs/encoding/json/index.js.map +1 -1
  86. package/dist/gs/errors/errors.js +29 -6
  87. package/dist/gs/errors/errors.js.map +1 -1
  88. package/dist/gs/fmt/fmt.js.map +1 -1
  89. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +7 -7
  90. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +52 -18
  91. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
  92. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +56 -20
  93. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -1
  94. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +57 -3
  95. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +366 -1
  96. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -1
  97. package/dist/gs/github.com/aperturerobotics/util/conc/index.d.ts +20 -0
  98. package/dist/gs/github.com/aperturerobotics/util/conc/index.js +134 -0
  99. package/dist/gs/github.com/aperturerobotics/util/conc/index.js.map +1 -0
  100. package/dist/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.js.map +1 -1
  101. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.d.ts +3 -0
  102. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js +50 -0
  103. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js.map +1 -0
  104. package/dist/gs/github.com/klauspost/compress/internal/le/index.js +3 -2
  105. package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -1
  106. package/dist/gs/github.com/mr-tron/base58/base58/index.d.ts +27 -0
  107. package/dist/gs/github.com/mr-tron/base58/base58/index.js +172 -0
  108. package/dist/gs/github.com/mr-tron/base58/base58/index.js.map +1 -0
  109. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.d.ts +21 -0
  110. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js +22 -0
  111. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js.map +1 -0
  112. package/dist/gs/go/token/index.js +11 -4
  113. package/dist/gs/go/token/index.js.map +1 -1
  114. package/dist/gs/hash/fnv/index.d.ts +57 -0
  115. package/dist/gs/hash/fnv/index.js +299 -0
  116. package/dist/gs/hash/fnv/index.js.map +1 -0
  117. package/dist/gs/hash/index.d.ts +17 -0
  118. package/dist/gs/hash/index.js +94 -0
  119. package/dist/gs/hash/index.js.map +1 -0
  120. package/dist/gs/io/fs/readlink.js +2 -6
  121. package/dist/gs/io/fs/readlink.js.map +1 -1
  122. package/dist/gs/io/fs/walk.js.map +1 -1
  123. package/dist/gs/io/io.js.map +1 -1
  124. package/dist/gs/iter/iter.d.ts +3 -2
  125. package/dist/gs/iter/iter.js.map +1 -1
  126. package/dist/gs/maps/iter.d.ts +5 -5
  127. package/dist/gs/maps/iter.js +48 -21
  128. package/dist/gs/maps/iter.js.map +1 -1
  129. package/dist/gs/maps/maps.d.ts +6 -6
  130. package/dist/gs/math/bits/index.js +14 -24
  131. package/dist/gs/math/bits/index.js.map +1 -1
  132. package/dist/gs/mime/index.js +3 -1
  133. package/dist/gs/mime/index.js.map +1 -1
  134. package/dist/gs/net/http/httptest/index.d.ts +20 -1
  135. package/dist/gs/net/http/httptest/index.js +83 -3
  136. package/dist/gs/net/http/httptest/index.js.map +1 -1
  137. package/dist/gs/net/http/index.d.ts +110 -6
  138. package/dist/gs/net/http/index.js +262 -16
  139. package/dist/gs/net/http/index.js.map +1 -1
  140. package/dist/gs/net/http/pprof/index.d.ts +8 -0
  141. package/dist/gs/net/http/pprof/index.js +59 -0
  142. package/dist/gs/net/http/pprof/index.js.map +1 -0
  143. package/dist/gs/os/error.gs.js +9 -7
  144. package/dist/gs/os/error.gs.js.map +1 -1
  145. package/dist/gs/os/types_js.gs.js +95 -15
  146. package/dist/gs/os/types_js.gs.js.map +1 -1
  147. package/dist/gs/path/filepath/match.js.map +1 -1
  148. package/dist/gs/path/filepath/path.d.ts +5 -3
  149. package/dist/gs/path/filepath/path.js +65 -10
  150. package/dist/gs/path/filepath/path.js.map +1 -1
  151. package/dist/gs/reflect/index.d.ts +3 -2
  152. package/dist/gs/reflect/index.js +2 -1
  153. package/dist/gs/reflect/index.js.map +1 -1
  154. package/dist/gs/reflect/iter.js +2 -2
  155. package/dist/gs/reflect/iter.js.map +1 -1
  156. package/dist/gs/reflect/map.js +26 -0
  157. package/dist/gs/reflect/map.js.map +1 -1
  158. package/dist/gs/reflect/type.d.ts +24 -5
  159. package/dist/gs/reflect/type.js +390 -38
  160. package/dist/gs/reflect/type.js.map +1 -1
  161. package/dist/gs/reflect/types.d.ts +1 -0
  162. package/dist/gs/reflect/types.js +3 -1
  163. package/dist/gs/reflect/types.js.map +1 -1
  164. package/dist/gs/reflect/value.d.ts +4 -1
  165. package/dist/gs/reflect/value.js +39 -1
  166. package/dist/gs/reflect/value.js.map +1 -1
  167. package/dist/gs/reflect/visiblefields.js +1 -1
  168. package/dist/gs/reflect/visiblefields.js.map +1 -1
  169. package/dist/gs/runtime/debug/index.d.ts +39 -0
  170. package/dist/gs/runtime/debug/index.js +58 -0
  171. package/dist/gs/runtime/debug/index.js.map +1 -1
  172. package/dist/gs/runtime/pprof/index.d.ts +20 -0
  173. package/dist/gs/runtime/pprof/index.js +85 -0
  174. package/dist/gs/runtime/pprof/index.js.map +1 -0
  175. package/dist/gs/runtime/trace/index.d.ts +19 -0
  176. package/dist/gs/runtime/trace/index.js +64 -0
  177. package/dist/gs/runtime/trace/index.js.map +1 -0
  178. package/dist/gs/slices/slices.d.ts +24 -9
  179. package/dist/gs/slices/slices.js +229 -24
  180. package/dist/gs/slices/slices.js.map +1 -1
  181. package/dist/gs/sort/slice.gs.d.ts +5 -3
  182. package/dist/gs/sort/slice.gs.js +55 -17
  183. package/dist/gs/sort/slice.gs.js.map +1 -1
  184. package/dist/gs/strings/builder.js +26 -17
  185. package/dist/gs/strings/builder.js.map +1 -1
  186. package/dist/gs/strings/iter.js +140 -75
  187. package/dist/gs/strings/iter.js.map +1 -1
  188. package/dist/gs/strings/replace.js +2 -2
  189. package/dist/gs/strings/replace.js.map +1 -1
  190. package/dist/gs/strings/strings.js +52 -6
  191. package/dist/gs/strings/strings.js.map +1 -1
  192. package/dist/gs/sync/sync.d.ts +6 -3
  193. package/dist/gs/sync/sync.js +39 -11
  194. package/dist/gs/sync/sync.js.map +1 -1
  195. package/dist/gs/syscall/errors.d.ts +116 -112
  196. package/dist/gs/syscall/errors.js +38 -1
  197. package/dist/gs/syscall/errors.js.map +1 -1
  198. package/dist/gs/syscall/fs.d.ts +2 -8
  199. package/dist/gs/syscall/fs.js.map +1 -1
  200. package/dist/gs/syscall/js/index.js +20 -12
  201. package/dist/gs/syscall/js/index.js.map +1 -1
  202. package/dist/gs/syscall/types.d.ts +4 -1
  203. package/dist/gs/syscall/types.js.map +1 -1
  204. package/dist/gs/testing/testing.d.ts +4 -3
  205. package/dist/gs/testing/testing.js +21 -4
  206. package/dist/gs/testing/testing.js.map +1 -1
  207. package/dist/gs/time/time.js +22 -0
  208. package/dist/gs/time/time.js.map +1 -1
  209. package/dist/gs/unicode/unicode.js.map +1 -1
  210. package/dist/gs/unique/index.js +7 -2
  211. package/dist/gs/unique/index.js.map +1 -1
  212. package/go.mod +8 -8
  213. package/go.sum +14 -23
  214. package/gs/builtin/builtin.ts +364 -37
  215. package/gs/builtin/channel.ts +161 -29
  216. package/gs/builtin/defer.ts +13 -2
  217. package/gs/builtin/hostio.test.ts +1 -0
  218. package/gs/builtin/hostio.ts +38 -0
  219. package/gs/builtin/map.ts +46 -6
  220. package/gs/builtin/print.ts +12 -3
  221. package/gs/builtin/runtime-contract.test.ts +257 -10
  222. package/gs/builtin/slice.test.ts +70 -0
  223. package/gs/builtin/slice.ts +566 -255
  224. package/gs/builtin/type.ts +53 -9
  225. package/gs/builtin/varRef.ts +2 -0
  226. package/gs/bytes/buffer.gs.ts +28 -28
  227. package/gs/bytes/iter.gs.ts +13 -14
  228. package/gs/compress/zlib/index.test.ts +28 -0
  229. package/gs/compress/zlib/index.ts +200 -0
  230. package/gs/compress/zlib/meta.json +3 -0
  231. package/gs/context/context.test.ts +31 -1
  232. package/gs/context/context.ts +9 -4
  233. package/gs/crypto/ecdh/index.test.ts +43 -0
  234. package/gs/crypto/ecdh/index.ts +274 -0
  235. package/gs/crypto/ed25519/index.test.ts +41 -0
  236. package/gs/crypto/ed25519/index.ts +238 -0
  237. package/gs/crypto/ed25519/meta.json +13 -0
  238. package/gs/crypto/internal/constanttime/index.test.ts +25 -0
  239. package/gs/crypto/internal/constanttime/index.ts +22 -0
  240. package/gs/crypto/rand/index.test.ts +89 -1
  241. package/gs/crypto/rand/index.ts +103 -1
  242. package/gs/crypto/rand/meta.json +4 -1
  243. package/gs/crypto/sha256/index.test.ts +78 -0
  244. package/gs/crypto/sha256/index.ts +150 -0
  245. package/gs/crypto/sha256/meta.json +9 -0
  246. package/gs/crypto/sha512/index.test.ts +31 -0
  247. package/gs/crypto/sha512/index.ts +161 -0
  248. package/gs/crypto/sha512/meta.json +11 -0
  249. package/gs/encoding/json/index.test.ts +25 -3
  250. package/gs/encoding/json/index.ts +21 -3
  251. package/gs/errors/errors.test.ts +4 -1
  252. package/gs/errors/errors.ts +32 -8
  253. package/gs/fmt/fmt.test.ts +3 -1
  254. package/gs/fmt/fmt.ts +1 -5
  255. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +62 -7
  256. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +78 -36
  257. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +32 -11
  258. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +122 -43
  259. package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +31 -0
  260. package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +518 -4
  261. package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +6 -0
  262. package/gs/github.com/aperturerobotics/util/conc/index.test.ts +30 -0
  263. package/gs/github.com/aperturerobotics/util/conc/index.ts +172 -0
  264. package/gs/github.com/aperturerobotics/util/conc/meta.json +9 -0
  265. package/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.ts +1 -4
  266. package/gs/github.com/hack-pad/safejs/internal/catch/index.test.ts +35 -0
  267. package/gs/github.com/hack-pad/safejs/internal/catch/index.ts +65 -0
  268. package/gs/github.com/hack-pad/safejs/internal/catch/meta.json +9 -0
  269. package/gs/github.com/klauspost/compress/internal/le/index.test.ts +2 -1
  270. package/gs/github.com/klauspost/compress/internal/le/index.ts +6 -5
  271. package/gs/github.com/mr-tron/base58/base58/index.test.ts +70 -0
  272. package/gs/github.com/mr-tron/base58/base58/index.ts +231 -0
  273. package/gs/github.com/mr-tron/base58/base58/meta.json +3 -0
  274. package/gs/github.com/zeebo/blake3/internal/consts/index.test.ts +46 -0
  275. package/gs/github.com/zeebo/blake3/internal/consts/index.ts +26 -0
  276. package/gs/go/token/index.ts +17 -4
  277. package/gs/hash/fnv/index.test.ts +67 -0
  278. package/gs/hash/fnv/index.ts +351 -0
  279. package/gs/hash/fnv/meta.json +3 -0
  280. package/gs/hash/index.test.ts +37 -0
  281. package/gs/hash/index.ts +118 -0
  282. package/gs/hash/meta.json +5 -0
  283. package/gs/internal/byteorder/index.test.ts +6 -6
  284. package/gs/io/fs/readlink.ts +40 -48
  285. package/gs/io/fs/walk.ts +10 -2
  286. package/gs/io/io.ts +4 -1
  287. package/gs/iter/iter.ts +8 -2
  288. package/gs/maps/iter.ts +69 -26
  289. package/gs/maps/maps.test.ts +23 -0
  290. package/gs/maps/maps.ts +6 -6
  291. package/gs/math/bits/index.test.ts +20 -0
  292. package/gs/math/bits/index.ts +15 -28
  293. package/gs/mime/index.ts +8 -2
  294. package/gs/net/http/httptest/index.test.ts +53 -0
  295. package/gs/net/http/httptest/index.ts +98 -3
  296. package/gs/net/http/index.test.ts +129 -1
  297. package/gs/net/http/index.ts +370 -19
  298. package/gs/net/http/meta.json +6 -0
  299. package/gs/net/http/pprof/index.test.ts +47 -0
  300. package/gs/net/http/pprof/index.ts +65 -0
  301. package/gs/os/error.gs.ts +9 -10
  302. package/gs/os/error.test.ts +41 -0
  303. package/gs/os/file_unix_js.test.ts +55 -0
  304. package/gs/os/tempfile.gs.test.ts +37 -10
  305. package/gs/os/types_js.gs.ts +94 -15
  306. package/gs/path/filepath/match.ts +4 -1
  307. package/gs/path/filepath/meta.json +6 -0
  308. package/gs/path/filepath/path.test.ts +57 -2
  309. package/gs/path/filepath/path.ts +91 -12
  310. package/gs/reflect/field.test.ts +63 -0
  311. package/gs/reflect/index.ts +4 -1
  312. package/gs/reflect/iter.ts +2 -2
  313. package/gs/reflect/map.test.ts +24 -2
  314. package/gs/reflect/map.ts +35 -0
  315. package/gs/reflect/type.ts +543 -60
  316. package/gs/reflect/typefor.test.ts +100 -0
  317. package/gs/reflect/types.ts +3 -1
  318. package/gs/reflect/value.ts +50 -1
  319. package/gs/reflect/visiblefields.ts +1 -1
  320. package/gs/runtime/debug/index.test.ts +22 -1
  321. package/gs/runtime/debug/index.ts +88 -0
  322. package/gs/runtime/pprof/index.test.ts +36 -0
  323. package/gs/runtime/pprof/index.ts +104 -0
  324. package/gs/runtime/pprof/meta.json +6 -0
  325. package/gs/runtime/trace/index.test.ts +45 -0
  326. package/gs/runtime/trace/index.ts +97 -0
  327. package/gs/runtime/trace/meta.json +7 -0
  328. package/gs/slices/meta.json +2 -1
  329. package/gs/slices/slices.test.ts +86 -0
  330. package/gs/slices/slices.ts +284 -37
  331. package/gs/sort/slice.gs.ts +73 -23
  332. package/gs/sort/slice.test.ts +40 -0
  333. package/gs/strings/builder.test.ts +8 -0
  334. package/gs/strings/builder.ts +29 -17
  335. package/gs/strings/iter.test.ts +5 -7
  336. package/gs/strings/iter.ts +146 -71
  337. package/gs/strings/replace.test.ts +1 -4
  338. package/gs/strings/replace.ts +6 -6
  339. package/gs/strings/strings.test.ts +4 -0
  340. package/gs/strings/strings.ts +54 -6
  341. package/gs/sync/sync.test.ts +57 -1
  342. package/gs/sync/sync.ts +45 -13
  343. package/gs/syscall/errors.ts +158 -115
  344. package/gs/syscall/fs.ts +8 -8
  345. package/gs/syscall/js/index.ts +49 -22
  346. package/gs/syscall/net.test.ts +26 -0
  347. package/gs/syscall/types.ts +7 -2
  348. package/gs/testing/testing.test.ts +56 -0
  349. package/gs/testing/testing.ts +27 -10
  350. package/gs/time/meta.json +2 -2
  351. package/gs/time/time.test.ts +4 -0
  352. package/gs/time/time.ts +33 -2
  353. package/gs/unicode/unicode.test.ts +14 -3
  354. package/gs/unicode/unicode.ts +1 -5
  355. package/gs/unique/index.ts +9 -2
  356. package/package.json +3 -3
@@ -18,6 +18,7 @@ export enum TypeKind {
18
18
  */
19
19
  export interface BaseTypeInfo {
20
20
  name?: string
21
+ typeName?: string
21
22
  kind: TypeKind
22
23
  zeroValue?: any
23
24
  }
@@ -204,6 +205,19 @@ export interface Comparable {
204
205
 
205
206
  // Registry to store runtime type information
206
207
  const typeRegistry = new Map<string, TypeInfo>()
208
+ const duplicateTypeRegistry = new Map<string, TypeInfo[]>()
209
+
210
+ function registerTypeInfo(name: string, typeInfo: TypeInfo): void {
211
+ const existing = typeRegistry.get(name)
212
+ if (existing && existing !== typeInfo) {
213
+ const candidates = duplicateTypeRegistry.get(name) ?? [existing]
214
+ if (!candidates.includes(typeInfo)) {
215
+ candidates.push(typeInfo)
216
+ }
217
+ duplicateTypeRegistry.set(name, candidates)
218
+ }
219
+ typeRegistry.set(name, typeInfo)
220
+ }
207
221
 
208
222
  /**
209
223
  * Registers a struct type with the runtime type system.
@@ -230,7 +244,7 @@ export const registerStructType = (
230
244
  ctor,
231
245
  fields,
232
246
  }
233
- typeRegistry.set(name, typeInfo)
247
+ registerTypeInfo(name, typeInfo)
234
248
  return typeInfo
235
249
  }
236
250
 
@@ -260,7 +274,7 @@ export const registerInterfaceType = (
260
274
  zeroValue,
261
275
  methods,
262
276
  }
263
- typeRegistry.set(name, typeInfo)
277
+ registerTypeInfo(name, typeInfo)
264
278
  return typeInfo
265
279
  }
266
280
 
@@ -301,6 +315,14 @@ function normalizeTypeInfo(info: string | TypeInfo): TypeInfo {
301
315
  return info
302
316
  }
303
317
 
318
+ function typeInfoRuntimeName(info: TypeInfo): string | undefined {
319
+ return info.typeName || info.name
320
+ }
321
+
322
+ function goTypeMatchesTypeInfo(goType: string, info: TypeInfo): boolean {
323
+ return goType === typeInfoRuntimeName(info)
324
+ }
325
+
304
326
  function compareOptionalTypeInfo(
305
327
  type1?: string | TypeInfo,
306
328
  type2?: string | TypeInfo,
@@ -530,11 +552,13 @@ function matchesStructType(value: any, info: TypeInfo): boolean {
530
552
  */
531
553
  function matchesInterfaceType(value: any, info: TypeInfo): boolean {
532
554
  // Check basic conditions first
533
- if (
534
- !isInterfaceTypeInfo(info) ||
535
- typeof value !== 'object' ||
536
- value === null
537
- ) {
555
+ if (!isInterfaceTypeInfo(info) || value === null || value === undefined) {
556
+ return false
557
+ }
558
+ if (info.methods.length === 0) {
559
+ return true
560
+ }
561
+ if (typeof value !== 'object') {
538
562
  return false
539
563
  }
540
564
 
@@ -740,6 +764,18 @@ export function cloneStructValue<T>(value: T): T {
740
764
  throw new Error('runtime error: value is not cloneable')
741
765
  }
742
766
 
767
+ export function cloneArrayValue<T>(value: T): T {
768
+ if (value instanceof Uint8Array) {
769
+ const out = new Uint8Array(value.length)
770
+ out.set(value)
771
+ return out as T
772
+ }
773
+ if (Array.isArray(value)) {
774
+ return value.map((item) => cloneArrayValue(item)) as T
775
+ }
776
+ return value
777
+ }
778
+
743
779
  // Check if a struct instance is marked as a value
744
780
  function isMarkedAsStructValue(value: any): boolean {
745
781
  return (
@@ -900,7 +936,7 @@ function matchesType(value: any, info: TypeInfo): boolean {
900
936
  if (
901
937
  typeof value === 'object' &&
902
938
  typeof value.__goType === 'string' &&
903
- value.__goType === info.name
939
+ goTypeMatchesTypeInfo(value.__goType, info)
904
940
  ) {
905
941
  return true
906
942
  }
@@ -1086,7 +1122,7 @@ export function typeAssert<T>(
1086
1122
  typeof value === 'object' &&
1087
1123
  value !== null &&
1088
1124
  typeof value.__goType === 'string' &&
1089
- value.__goType === normalizedType.name
1125
+ goTypeMatchesTypeInfo(value.__goType, normalizedType)
1090
1126
  ) {
1091
1127
  if ('__goValue' in value) {
1092
1128
  return { value: value.__goValue as T, ok: true }
@@ -1147,6 +1183,14 @@ export function typeAssert<T>(
1147
1183
  return { value: value as T, ok: true }
1148
1184
  }
1149
1185
 
1186
+ if (typeof typeInfo === 'string') {
1187
+ for (const candidate of duplicateTypeRegistry.get(typeInfo) ?? []) {
1188
+ if (candidate !== normalizedType && matchesType(value, candidate)) {
1189
+ return { value: value as T, ok: true }
1190
+ }
1191
+ }
1192
+ }
1193
+
1150
1194
  // If we get here, the assertion failed
1151
1195
  // For registered types, use the zero value from the registry
1152
1196
  if (typeof typeInfo === 'string') {
@@ -10,6 +10,8 @@ export type VarRef<T> = {
10
10
  __isVarRef?: true
11
11
  __goType?: string
12
12
  __goAddress?: () => number
13
+ __goCollection?: unknown
14
+ __goIndex?: number
13
15
  }
14
16
 
15
17
  /** Wrap a non-null T in a variable reference. */
@@ -66,7 +66,7 @@ export class Buffer {
66
66
  // The slice aliases the buffer content at least until the next buffer modification,
67
67
  // so immediate changes to the slice will affect the result of future reads.
68
68
  public Bytes(): $.Bytes {
69
- const b = this
69
+ const b = $.pointerValue<Buffer>(this)
70
70
  return $.goSlice(b.buf, b.off, undefined)
71
71
  }
72
72
 
@@ -75,7 +75,7 @@ export class Buffer {
75
75
  // passed to an immediately succeeding [Buffer.Write] call.
76
76
  // The buffer is only valid until the next write operation on b.
77
77
  public AvailableBuffer(): $.Bytes {
78
- const b = this
78
+ const b = $.pointerValue<Buffer>(this)
79
79
  return $.goSlice(b.buf, $.len(b.buf), undefined)
80
80
  }
81
81
 
@@ -84,7 +84,7 @@ export class Buffer {
84
84
  //
85
85
  // To build strings more efficiently, see the [strings.Builder] type.
86
86
  public String(): string {
87
- const b = this
87
+ const b = $.pointerValueOrNil<Buffer>(this)
88
88
  if (b == null) {
89
89
  // Special case, useful in debugging.
90
90
  return "<nil>"
@@ -97,27 +97,27 @@ export class Buffer {
97
97
 
98
98
  // empty reports whether the unread portion of the buffer is empty.
99
99
  public empty(): boolean {
100
- const b = this
100
+ const b = $.pointerValue<Buffer>(this)
101
101
  return $.len(b.buf) <= b.off
102
102
  }
103
103
 
104
104
  // Len returns the number of bytes of the unread portion of the buffer;
105
105
  // b.Len() == len(b.Bytes()).
106
106
  public Len(): number {
107
- const b = this
107
+ const b = $.pointerValue<Buffer>(this)
108
108
  return $.len(b.buf) - b.off
109
109
  }
110
110
 
111
111
  // Cap returns the capacity of the buffer's underlying byte slice, that is, the
112
112
  // total space allocated for the buffer's data.
113
113
  public Cap(): number {
114
- const b = this
114
+ const b = $.pointerValue<Buffer>(this)
115
115
  return $.cap(b.buf)
116
116
  }
117
117
 
118
118
  // Available returns how many bytes are unused in the buffer.
119
119
  public Available(): number {
120
- const b = this
120
+ const b = $.pointerValue<Buffer>(this)
121
121
  return $.cap(b.buf) - $.len(b.buf)
122
122
  }
123
123
 
@@ -125,7 +125,7 @@ export class Buffer {
125
125
  // but continues to use the same allocated storage.
126
126
  // It panics if n is negative or greater than the length of the buffer.
127
127
  public Truncate(n: number): void {
128
- const b = this
128
+ const b = $.pointerValue<Buffer>(this)
129
129
  if (n == 0) {
130
130
  b.Reset()
131
131
  return
@@ -141,7 +141,7 @@ export class Buffer {
141
141
  // but it retains the underlying storage for use by future writes.
142
142
  // Reset is the same as [Buffer.Truncate](0).
143
143
  public Reset(): void {
144
- const b = this
144
+ const b = $.pointerValue<Buffer>(this)
145
145
  b.buf = $.goSlice(b.buf, undefined, 0)
146
146
  b.off = 0
147
147
  b.lastRead = 0
@@ -151,7 +151,7 @@ export class Buffer {
151
151
  // internal buffer only needs to be resliced.
152
152
  // It returns the index where bytes should be written and whether it succeeded.
153
153
  public tryGrowByReslice(n: number): [number, boolean] {
154
- const b = this
154
+ const b = $.pointerValue<Buffer>(this)
155
155
  if (b.buf === null) {
156
156
  return [0, false]
157
157
  }
@@ -169,7 +169,7 @@ export class Buffer {
169
169
  // It returns the index where bytes should be written.
170
170
  // If the buffer can't grow it will panic with ErrTooLarge.
171
171
  public grow(n: number): number {
172
- const b = this
172
+ const b = $.pointerValue<Buffer>(this)
173
173
  let m = b.Len()
174
174
  if (m == 0 && b.off != 0) {
175
175
  b.Reset()
@@ -210,7 +210,7 @@ export class Buffer {
210
210
  // If n is negative, Grow will panic.
211
211
  // If the buffer can't grow it will panic with [ErrTooLarge].
212
212
  public Grow(n: number): void {
213
- const b = this
213
+ const b = $.pointerValue<Buffer>(this)
214
214
  if (n < 0) {
215
215
  $.panic("bytes.Buffer.Grow: negative count")
216
216
  }
@@ -222,7 +222,7 @@ export class Buffer {
222
222
  // needed. The return value n is the length of p; err is always nil. If the
223
223
  // buffer becomes too large, Write will panic with [ErrTooLarge].
224
224
  public Write(p: $.Bytes): [number, $.GoError] {
225
- const b = this
225
+ const b = $.pointerValue<Buffer>(this)
226
226
  b.lastRead = 0
227
227
  let [m, ok] = b.tryGrowByReslice($.len(p))
228
228
  if (!ok) {
@@ -248,7 +248,7 @@ export class Buffer {
248
248
  // needed. The return value n is the length of s; err is always nil. If the
249
249
  // buffer becomes too large, WriteString will panic with [ErrTooLarge].
250
250
  public WriteString(s: string): [number, $.GoError] {
251
- const b = this
251
+ const b = $.pointerValue<Buffer>(this)
252
252
  b.lastRead = 0
253
253
  let [m, ok] = b.tryGrowByReslice($.len(s))
254
254
  if (!ok) {
@@ -270,7 +270,7 @@ export class Buffer {
270
270
  // buffer becomes too large, ReadFrom will panic with [ErrTooLarge].
271
271
  public ReadFrom(r: io.Reader): [number, $.GoError] {
272
272
  return (async (): Promise<[number, $.GoError]> => {
273
- const b = this
273
+ const b = $.pointerValue<Buffer>(this)
274
274
  b.lastRead = 0
275
275
  let n = 0
276
276
  for (; ; ) {
@@ -301,7 +301,7 @@ export class Buffer {
301
301
  // encountered during the write is also returned.
302
302
  public WriteTo(w: io.Writer): [number, $.GoError] {
303
303
  return (async (): Promise<[number, $.GoError]> => {
304
- const b = this
304
+ const b = $.pointerValue<Buffer>(this)
305
305
  b.lastRead = 0
306
306
  let n = 0
307
307
  {
@@ -333,7 +333,7 @@ export class Buffer {
333
333
  // WriteByte. If the buffer becomes too large, WriteByte will panic with
334
334
  // [ErrTooLarge].
335
335
  public WriteByte(c: number): $.GoError {
336
- const b = this
336
+ const b = $.pointerValue<Buffer>(this)
337
337
  b.lastRead = 0
338
338
  let [m, ok] = b.tryGrowByReslice(1)
339
339
  if (!ok) {
@@ -348,7 +348,7 @@ export class Buffer {
348
348
  // included to match [bufio.Writer]'s WriteRune. The buffer is grown as needed;
349
349
  // if it becomes too large, WriteRune will panic with [ErrTooLarge].
350
350
  public WriteRune(r: number): [number, $.GoError] {
351
- const b = this
351
+ const b = $.pointerValue<Buffer>(this)
352
352
  if ((r as number) < utf8.RuneSelf) {
353
353
  b.WriteByte($.byte(r))
354
354
  return [1, null]
@@ -367,7 +367,7 @@ export class Buffer {
367
367
  // buffer has no data to return, err is [io.EOF] (unless len(p) is zero);
368
368
  // otherwise it is nil.
369
369
  public Read(p: $.Bytes): [number, $.GoError] {
370
- const b = this
370
+ const b = $.pointerValue<Buffer>(this)
371
371
  b.lastRead = 0
372
372
  if (b.empty()) {
373
373
  // Buffer is empty, reset to recover space.
@@ -390,7 +390,7 @@ export class Buffer {
390
390
  // If there are fewer than n bytes in the buffer, Next returns the entire buffer.
391
391
  // The slice is only valid until the next call to a read or write method.
392
392
  public Next(n: number): $.Bytes {
393
- const b = this
393
+ const b = $.pointerValue<Buffer>(this)
394
394
  b.lastRead = 0
395
395
  let m = b.Len()
396
396
  if (n > m) {
@@ -407,7 +407,7 @@ export class Buffer {
407
407
  // Peek returns the next n bytes without advancing the buffer.
408
408
  // If fewer than n bytes are available, it returns the unread bytes and io.EOF.
409
409
  public Peek(n: number): [$.Bytes, $.GoError] {
410
- const b = this
410
+ const b = $.pointerValue<Buffer>(this)
411
411
  if (n < 0) {
412
412
  return [null, errors.New("bytes.Buffer.Peek: negative count")]
413
413
  }
@@ -428,7 +428,7 @@ export class Buffer {
428
428
  // ReadByte reads and returns the next byte from the buffer.
429
429
  // If no byte is available, it returns error [io.EOF].
430
430
  public ReadByte(): [number, $.GoError] {
431
- const b = this
431
+ const b = $.pointerValue<Buffer>(this)
432
432
  if (b.empty()) {
433
433
  // Buffer is empty, reset to recover space.
434
434
  b.Reset()
@@ -446,7 +446,7 @@ export class Buffer {
446
446
  // If the bytes are an erroneous UTF-8 encoding, it
447
447
  // consumes one byte and returns U+FFFD, 1.
448
448
  public ReadRune(): [number, number, $.GoError] {
449
- const b = this
449
+ const b = $.pointerValue<Buffer>(this)
450
450
  if (b.empty()) {
451
451
  // Buffer is empty, reset to recover space.
452
452
  b.Reset()
@@ -472,7 +472,7 @@ export class Buffer {
472
472
  // it is stricter than [Buffer.UnreadByte], which will unread the last byte
473
473
  // from any read operation.)
474
474
  public UnreadRune(): $.GoError {
475
- const b = this
475
+ const b = $.pointerValue<Buffer>(this)
476
476
  if (b.lastRead <= 0) {
477
477
  return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
478
478
  }
@@ -488,7 +488,7 @@ export class Buffer {
488
488
  // the last read, if the last read returned an error, or if the read read zero
489
489
  // bytes, UnreadByte returns an error.
490
490
  public UnreadByte(): $.GoError {
491
- const b = this
491
+ const b = $.pointerValue<Buffer>(this)
492
492
  if (b.lastRead == 0) {
493
493
  return errUnreadByte
494
494
  }
@@ -506,7 +506,7 @@ export class Buffer {
506
506
  // ReadBytes returns err != nil if and only if the returned data does not end in
507
507
  // delim.
508
508
  public ReadBytes(delim: number): [$.Bytes, $.GoError] {
509
- const b = this
509
+ const b = $.pointerValue<Buffer>(this)
510
510
  let [slice, err] = b.readSlice(delim)
511
511
  let line = $.append<number>(null, slice)
512
512
  return [line, err]
@@ -514,7 +514,7 @@ export class Buffer {
514
514
 
515
515
  // readSlice is like ReadBytes but returns a reference to internal buffer data.
516
516
  public readSlice(delim: number): [$.Bytes, $.GoError] {
517
- const b = this
517
+ const b = $.pointerValue<Buffer>(this)
518
518
  let i = IndexByte($.goSlice(b.buf, b.off, undefined), delim)
519
519
  let end = b.off + i + 1
520
520
  let err: $.GoError = null
@@ -536,7 +536,7 @@ export class Buffer {
536
536
  // ReadString returns err != nil if and only if the returned data does not end
537
537
  // in delim.
538
538
  public ReadString(delim: number): [string, $.GoError] {
539
- const b = this
539
+ const b = $.pointerValue<Buffer>(this)
540
540
  let slice: $.Bytes
541
541
  let err: $.GoError
542
542
  [slice, err] = b.readSlice(delim)
@@ -13,7 +13,7 @@ import * as utf8 from "@goscript/unicode/utf8/index.js"
13
13
  // If s does not end in a newline, the final yielded line will not end in a newline.
14
14
  // It returns a single-use iterator.
15
15
  export function Lines(s: $.Bytes): iter.Seq<$.Bytes> {
16
- return (_yield: ((p0: $.Bytes) => boolean) | null): void => {
16
+ return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
17
17
  for (; $.len(s) > 0; ) {
18
18
  let line: $.Bytes = new Uint8Array(0)
19
19
  {
@@ -24,7 +24,7 @@ export function Lines(s: $.Bytes): iter.Seq<$.Bytes> {
24
24
  [line, s] = [s, null]
25
25
  }
26
26
  }
27
- if (!_yield!($.goSlice(line, undefined, $.len(line), $.len(line)))) {
27
+ if (!await _yield!($.goSlice(line, undefined, $.len(line), $.len(line)))) {
28
28
  return
29
29
  }
30
30
  }
@@ -34,10 +34,10 @@ export function Lines(s: $.Bytes): iter.Seq<$.Bytes> {
34
34
 
35
35
  // explodeSeq returns an iterator over the runes in s.
36
36
  export function explodeSeq(s: $.Bytes): iter.Seq<$.Bytes> {
37
- return (_yield: ((p0: $.Bytes) => boolean) | null): void => {
37
+ return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
38
38
  for (; $.len(s) > 0; ) {
39
39
  let [, size] = utf8.DecodeRune(s)
40
- if (!_yield!($.goSlice(s, undefined, size, size))) {
40
+ if (!await _yield!($.goSlice(s, undefined, size, size))) {
41
41
  return
42
42
  }
43
43
  s = $.goSlice(s, size, undefined)
@@ -51,19 +51,19 @@ export function splitSeq(s: $.Bytes, sep: $.Bytes, sepSave: number): iter.Seq<$.
51
51
  if ($.len(sep) == 0) {
52
52
  return explodeSeq(s)
53
53
  }
54
- return (_yield: ((p0: $.Bytes) => boolean) | null): void => {
54
+ return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
55
55
  for (; ; ) {
56
56
  let i = Index(s, sep)
57
57
  if (i < 0) {
58
58
  break
59
59
  }
60
60
  let frag = $.goSlice(s, undefined, i + sepSave)
61
- if (!_yield!($.goSlice(frag, undefined, $.len(frag), $.len(frag)))) {
61
+ if (!await _yield!($.goSlice(frag, undefined, $.len(frag), $.len(frag)))) {
62
62
  return
63
63
  }
64
64
  s = $.goSlice(s, i + $.len(sep), undefined)
65
65
  }
66
- _yield!($.goSlice(s, undefined, $.len(s), $.len(s)))
66
+ await _yield!($.goSlice(s, undefined, $.len(s), $.len(s)))
67
67
  }
68
68
  }
69
69
 
@@ -88,7 +88,7 @@ export function SplitAfterSeq(s: $.Bytes, sep: $.Bytes): iter.Seq<$.Bytes> {
88
88
  // The iterator yields the same subslices that would be returned by [Fields](s),
89
89
  // but without constructing a new slice containing the subslices.
90
90
  export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
91
- return (_yield: ((p0: $.Bytes) => boolean) | null): void => {
91
+ return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
92
92
  let start = -1
93
93
  for (let i = 0; i < $.len(s); ) {
94
94
  let size = 1
@@ -100,7 +100,7 @@ export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
100
100
  }
101
101
  if (isSpace) {
102
102
  if (start >= 0) {
103
- if (!_yield!($.goSlice(s, start, i, i))) {
103
+ if (!await _yield!($.goSlice(s, start, i, i))) {
104
104
  return
105
105
  }
106
106
  start = -1
@@ -111,7 +111,7 @@ export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
111
111
  i += size
112
112
  }
113
113
  if (start >= 0) {
114
- _yield!($.goSlice(s, start, $.len(s), $.len(s)))
114
+ await _yield!($.goSlice(s, start, $.len(s), $.len(s)))
115
115
  }
116
116
  }
117
117
  }
@@ -121,7 +121,7 @@ export function FieldsSeq(s: $.Bytes): iter.Seq<$.Bytes> {
121
121
  // The iterator yields the same subslices that would be returned by [FieldsFunc](s),
122
122
  // but without constructing a new slice containing the subslices.
123
123
  export function FieldsFuncSeq(s: $.Bytes, f: ((p0: number) => boolean) | null): iter.Seq<$.Bytes> {
124
- return (_yield: ((p0: $.Bytes) => boolean) | null): void => {
124
+ return async (_yield: ((p0: $.Bytes) => iter.YieldResult) | null): Promise<void> => {
125
125
  let start = -1
126
126
  for (let i = 0; i < $.len(s); ) {
127
127
  let size = 1
@@ -131,7 +131,7 @@ export function FieldsFuncSeq(s: $.Bytes, f: ((p0: number) => boolean) | null):
131
131
  }
132
132
  if (f!(r)) {
133
133
  if (start >= 0) {
134
- if (!_yield!($.goSlice(s, start, i, i))) {
134
+ if (!await _yield!($.goSlice(s, start, i, i))) {
135
135
  return
136
136
  }
137
137
  start = -1
@@ -142,8 +142,7 @@ export function FieldsFuncSeq(s: $.Bytes, f: ((p0: number) => boolean) | null):
142
142
  i += size
143
143
  }
144
144
  if (start >= 0) {
145
- _yield!($.goSlice(s, start, $.len(s), $.len(s)))
145
+ await _yield!($.goSlice(s, start, $.len(s), $.len(s)))
146
146
  }
147
147
  }
148
148
  }
149
-
@@ -0,0 +1,28 @@
1
+ import { describe, expect, test } from 'vitest'
2
+
3
+ import * as $ from '@goscript/builtin/index.js'
4
+ import * as bytes from '@goscript/bytes/index.js'
5
+ import * as io from '@goscript/io/index.js'
6
+
7
+ import { NewReader, NewWriter } from './index.js'
8
+
9
+ describe('compress/zlib override', () => {
10
+ test('round trips bytes through writer and reader', async () => {
11
+ const input = $.stringToBytes('hello compressed world')
12
+ const buf = $.markAsStructValue(new bytes.Buffer())
13
+ const writer = NewWriter(buf)
14
+
15
+ const [written, writeErr] = writer.Write(input)
16
+ expect(writeErr).toBeNull()
17
+ expect(written).toBe(input.length)
18
+ expect(await writer.Close()).toBeNull()
19
+
20
+ const [reader, readerErr] = await NewReader(bytes.NewReader(buf.Bytes()))
21
+ expect(readerErr).toBeNull()
22
+ expect(reader).not.toBeNull()
23
+
24
+ const [out, readErr] = await io.ReadAll(reader!)
25
+ expect(readErr).toBeNull()
26
+ expect($.bytesToString(out)).toBe('hello compressed world')
27
+ })
28
+ })