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
@@ -1,4 +1,10 @@
1
- import { ReflectValue, StructField, StructTag, ValueError } from './types.js'
1
+ import {
2
+ Method,
3
+ ReflectValue,
4
+ StructField,
5
+ StructTag,
6
+ ValueError,
7
+ } from './types.js'
2
8
  export { StructField }
3
9
  import { MapIter } from './map.js'
4
10
  import {
@@ -176,6 +182,7 @@ export const UnsafePointer: Kind = 26
176
182
  const pointerAddressStride = 0x100000000
177
183
  const pointerAddresses = new WeakMap<object, number>()
178
184
  let nextPointerAddress = 1
185
+ const canonicalTypes = new globalThis.Map<string, Type>()
179
186
 
180
187
  function pointerAddress(value: object): number {
181
188
  let address = pointerAddresses.get(value)
@@ -187,6 +194,16 @@ function pointerAddress(value: object): number {
187
194
  return address
188
195
  }
189
196
 
197
+ function internType(t: Type): Type {
198
+ const key = `${t.Kind()}:${t.PkgPath()}:${t.Name()}:${t.String()}`
199
+ const existing = canonicalTypes.get(key)
200
+ if (existing) {
201
+ return existing
202
+ }
203
+ canonicalTypes.set(key, t)
204
+ return t
205
+ }
206
+
190
207
  // Type is the representation of a Go type.
191
208
  export interface Type {
192
209
  // String returns a string representation of the type.
@@ -206,12 +223,18 @@ export interface Type {
206
223
  NumField(): number
207
224
 
208
225
  // PkgPath returns the package path for named types, empty for unnamed types.
209
- PkgPath?(): string
226
+ PkgPath(): string
210
227
 
211
228
  // Field returns a struct type's i'th field.
212
229
  // Panics if the type's Kind is not Struct or i is out of range.
213
230
  Field(i: number): StructField
214
231
 
232
+ // FieldByName returns the struct field with the given name.
233
+ FieldByName(name: string): [StructField, boolean]
234
+
235
+ // FieldByNameFunc returns the first struct field whose name satisfies match.
236
+ FieldByNameFunc(match: (name: string) => boolean): [StructField, boolean]
237
+
215
238
  // Key returns a map type's key type.
216
239
  // Panics if the type's Kind is not Map.
217
240
  Key(): Type
@@ -222,6 +245,9 @@ export interface Type {
222
245
  // Implements reports whether the type implements the interface type u.
223
246
  Implements(u: Type | null): boolean
224
247
 
248
+ // AssignableTo reports whether a value of this type is assignable to u.
249
+ AssignableTo(u: Type | null): boolean
250
+
225
251
  // common returns the common type implementation.
226
252
  common?(): rtype
227
253
 
@@ -240,6 +266,9 @@ export interface Type {
240
266
  // NumMethod returns the number of methods in the type's method set
241
267
  NumMethod(): number
242
268
 
269
+ // MethodByName returns the method with that name in the type's method set.
270
+ MethodByName(name: string): [Method, boolean]
271
+
243
272
  // Len returns an array type's length.
244
273
  // Panics if the type's Kind is not Array.
245
274
  Len(): number
@@ -247,6 +276,9 @@ export interface Type {
247
276
  // Bits returns the size of the type in bits
248
277
  // Panics if the type's Kind is not a sized type.
249
278
  Bits(): number
279
+
280
+ // Comparable reports whether values of this type are comparable.
281
+ Comparable(): boolean
250
282
  }
251
283
 
252
284
  // InvalidTypeInstance is a singleton type for invalid/zero reflect.Value
@@ -254,12 +286,18 @@ class InvalidTypeClass implements Type {
254
286
  Kind(): Kind {
255
287
  return Invalid
256
288
  }
289
+ Comparable(): boolean {
290
+ return false
291
+ }
257
292
  String(): string {
258
293
  return '<invalid reflect.Value>'
259
294
  }
260
295
  Name(): string {
261
296
  return ''
262
297
  }
298
+ PkgPath(): string {
299
+ return ''
300
+ }
263
301
  Size(): number {
264
302
  return 0
265
303
  }
@@ -275,9 +313,18 @@ class InvalidTypeClass implements Type {
275
313
  Field(_i: number): StructField {
276
314
  throw new Error('reflect: Field of invalid type')
277
315
  }
316
+ FieldByName(_name: string): [StructField, boolean] {
317
+ return [new StructField(), false]
318
+ }
319
+ FieldByNameFunc(_match: (name: string) => boolean): [StructField, boolean] {
320
+ return [new StructField(), false]
321
+ }
278
322
  Implements(_u: Type | null): boolean {
279
323
  return false
280
324
  }
325
+ AssignableTo(_u: Type | null): boolean {
326
+ return false
327
+ }
281
328
  OverflowInt(_x: number): boolean {
282
329
  throw new Error('reflect: OverflowInt of invalid type')
283
330
  }
@@ -290,6 +337,9 @@ class InvalidTypeClass implements Type {
290
337
  NumMethod(): number {
291
338
  return 0
292
339
  }
340
+ MethodByName(_name: string): [Method, boolean] {
341
+ return [zeroMethod(), false]
342
+ }
293
343
  Len(): number {
294
344
  throw new Error('reflect: Len of invalid type')
295
345
  }
@@ -479,6 +529,35 @@ export class Value {
479
529
  )
480
530
  }
481
531
 
532
+ public Slice(i: number, j: number): Value {
533
+ const length = this.Len()
534
+ if (i < 0 || j < i || j > length) {
535
+ throw new Error(
536
+ `reflect.Value.Slice: slice index out of bounds [${i}:${j}] with length ${length}`,
537
+ )
538
+ }
539
+ if (
540
+ this._value &&
541
+ typeof this._value === 'object' &&
542
+ '__meta__' in this._value
543
+ ) {
544
+ return new Value(
545
+ $.goSlice(this._value as $.Slice<unknown>, i, j),
546
+ this._type,
547
+ )
548
+ }
549
+ if (globalThis.Array.isArray(this._value)) {
550
+ return new Value(this._value.slice(i, j), this._type)
551
+ }
552
+ if (this._value instanceof Uint8Array) {
553
+ return new Value(this._value.slice(i, j), this._type)
554
+ }
555
+ if (typeof this._value === 'string') {
556
+ return new Value(this._value.slice(i, j), this._type)
557
+ }
558
+ throw new ValueError({ Kind: this.Kind(), Method: 'Slice' })
559
+ }
560
+
482
561
  public Bytes(): Uint8Array {
483
562
  if (this._value instanceof Uint8Array) {
484
563
  return this._value
@@ -524,6 +603,22 @@ export class Value {
524
603
  return new Value(fieldVal, field.Type, undefined, parentObj, field.Name)
525
604
  }
526
605
 
606
+ public FieldByIndex(index: $.Slice<number>): Value {
607
+ let current: Value = this
608
+ for (const fieldIndex of $.asArray(index)) {
609
+ current = current.Field(fieldIndex)
610
+ }
611
+ return current
612
+ }
613
+
614
+ public FieldByName(name: string): Value {
615
+ const [field, ok] = this.Type().FieldByName(name)
616
+ if (!ok) {
617
+ return new Value(null, invalidTypeInstance)
618
+ }
619
+ return this.FieldByIndex(field.Index)
620
+ }
621
+
527
622
  // Additional methods needed by various parts of the codebase
528
623
  public UnsafePointer(): unknown {
529
624
  return this._value
@@ -568,6 +663,16 @@ export class Value {
568
663
  return 0
569
664
  }
570
665
 
666
+ public UnsafeAddr(): number | { value: Value } {
667
+ if (!this.CanAddr()) {
668
+ throw new ValueError({ Kind: this.Kind(), Method: 'UnsafeAddr' })
669
+ }
670
+ if (this._parentStruct && this._fieldName) {
671
+ return { value: this }
672
+ }
673
+ return this.Pointer()
674
+ }
675
+
571
676
  public pointer(): unknown {
572
677
  return this._value
573
678
  }
@@ -596,7 +701,10 @@ export class Value {
596
701
  throw new Error('reflect: call of reflect.Value.Addr on invalid Value')
597
702
  }
598
703
  const ptrType = PointerTo(this.Type())
599
- return new Value(this, ptrType) // Simplified
704
+ if (this._parentStruct && this._fieldName) {
705
+ return new Value($.fieldRef(this._parentStruct, this._fieldName), ptrType)
706
+ }
707
+ return new Value($.varRef(this._value), ptrType)
600
708
  }
601
709
 
602
710
  public CanSet(): boolean {
@@ -644,6 +752,40 @@ export class Value {
644
752
  return this._value
645
753
  }
646
754
 
755
+ public MethodByName(name: string): Value {
756
+ if (!this.IsValid()) {
757
+ return new Value()
758
+ }
759
+ const receiver = $.isVarRef(this._value) ? this._value.value : this._value
760
+ if (
761
+ receiver === null ||
762
+ receiver === undefined ||
763
+ (typeof receiver !== 'object' && typeof receiver !== 'function')
764
+ ) {
765
+ return new Value()
766
+ }
767
+ const method = (receiver as Record<string, unknown>)[name]
768
+ if (typeof method !== 'function') {
769
+ return new Value()
770
+ }
771
+ return new Value(method.bind(receiver), new FunctionType('func'))
772
+ }
773
+
774
+ public Call(inArgs: $.Slice<Value>): $.Slice<Value> {
775
+ if (this.Kind() !== Func || typeof this._value !== 'function') {
776
+ throw new ValueError({ Kind: this.Kind(), Method: 'Call' })
777
+ }
778
+ const args = $.asArray(inArgs).map((arg) => arg.Interface())
779
+ const result = this._value(...args) as ReflectValue | ReflectValue[]
780
+ if (globalThis.Array.isArray(result)) {
781
+ return $.arrayToSlice(result.map((value) => ValueOf(value)))
782
+ }
783
+ if (result === undefined) {
784
+ return $.makeSlice<Value>(0)
785
+ }
786
+ return $.arrayToSlice([ValueOf(result)])
787
+ }
788
+
647
789
  public IsZero(): boolean {
648
790
  const zeroVal = Zero(this.Type()).value
649
791
  return DeepEqual(this._value, zeroVal)
@@ -658,13 +800,33 @@ export class Value {
658
800
  }
659
801
 
660
802
  public MapRange(): MapIter<unknown, unknown> | null {
661
- // Placeholder for map iteration
662
- return null
803
+ if (this.Kind() !== Map) {
804
+ throw new ValueError({ Kind: this.Kind(), Method: 'MapRange' })
805
+ }
806
+ if (this._value === null || this._value === undefined) {
807
+ return new MapIter(new globalThis.Map())
808
+ }
809
+ if (!(this._value instanceof globalThis.Map)) {
810
+ throw new ValueError({ Kind: this.Kind(), Method: 'MapRange' })
811
+ }
812
+ return new MapIter(this._value)
663
813
  }
664
814
 
665
- public MapIndex(_key: Value): Value {
666
- // Placeholder for map access
667
- return new Value(null, new BasicType(Invalid, 'invalid'))
815
+ public MapIndex(key: Value): Value {
816
+ if (this.Kind() !== Map) {
817
+ throw new ValueError({ Kind: this.Kind(), Method: 'MapIndex' })
818
+ }
819
+ if (!(this._value instanceof globalThis.Map)) {
820
+ return new Value(null, new BasicType(Invalid, 'invalid'))
821
+ }
822
+ const rawKey = key.Interface()
823
+ if (!this._value.has(rawKey)) {
824
+ return new Value(null, new BasicType(Invalid, 'invalid'))
825
+ }
826
+ return new Value(
827
+ this._value.get(rawKey) as ReflectValue,
828
+ this.Type().Elem(),
829
+ )
668
830
  }
669
831
 
670
832
  public MapKeys(): $.Slice<Value> {
@@ -1048,16 +1210,21 @@ export class BasicType implements Type {
1048
1210
  private _kind: Kind,
1049
1211
  private _name: string,
1050
1212
  private _size: number = 8,
1213
+ private _typeName: string = '',
1051
1214
  ) {}
1052
1215
 
1053
1216
  public String(): string {
1054
- return this._name
1217
+ return this._typeName || this._name
1055
1218
  }
1056
1219
 
1057
1220
  public Kind(): Kind {
1058
1221
  return this._kind
1059
1222
  }
1060
1223
 
1224
+ public Comparable(): boolean {
1225
+ return this._kind !== Func && this._kind !== Map && this._kind !== Slice
1226
+ }
1227
+
1061
1228
  public Size(): number {
1062
1229
  return this._size
1063
1230
  }
@@ -1070,11 +1237,24 @@ export class BasicType implements Type {
1070
1237
  return 0
1071
1238
  }
1072
1239
 
1073
- public PkgPath?(): string {
1240
+ public PkgPath(): string {
1241
+ if (this._typeName) {
1242
+ const dotIndex = this._typeName.lastIndexOf('.')
1243
+ if (dotIndex > 0) {
1244
+ return this._typeName.substring(0, dotIndex)
1245
+ }
1246
+ }
1074
1247
  return ''
1075
1248
  }
1076
1249
 
1077
1250
  public Name(): string {
1251
+ if (this._typeName) {
1252
+ const dotIndex = this._typeName.lastIndexOf('.')
1253
+ if (dotIndex >= 0) {
1254
+ return this._typeName.substring(dotIndex + 1)
1255
+ }
1256
+ return this._typeName
1257
+ }
1078
1258
  // Basic types have names like 'int', 'string', etc.
1079
1259
  return this._name
1080
1260
  }
@@ -1083,6 +1263,16 @@ export class BasicType implements Type {
1083
1263
  throw new Error(`reflect: Field of non-struct type ${this._name}`)
1084
1264
  }
1085
1265
 
1266
+ public FieldByName(name: string): [StructField, boolean] {
1267
+ return typeFieldByName(this, name)
1268
+ }
1269
+
1270
+ public FieldByNameFunc(
1271
+ match: (name: string) => boolean,
1272
+ ): [StructField, boolean] {
1273
+ return typeFieldByNameFunc(this, match)
1274
+ }
1275
+
1086
1276
  public Key(): Type {
1087
1277
  throw new Error(`reflect: Key of non-map type ${this._name}`)
1088
1278
  }
@@ -1097,6 +1287,10 @@ export class BasicType implements Type {
1097
1287
  return false
1098
1288
  }
1099
1289
 
1290
+ public AssignableTo(u: Type | null): boolean {
1291
+ return typeAssignableTo(this, u)
1292
+ }
1293
+
1100
1294
  public common?(): rtype {
1101
1295
  return new rtype(this._kind)
1102
1296
  }
@@ -1163,6 +1357,9 @@ export class BasicType implements Type {
1163
1357
  public NumMethod(): number {
1164
1358
  return 0
1165
1359
  }
1360
+ public MethodByName(_name: string): [Method, boolean] {
1361
+ return [zeroMethod(), false]
1362
+ }
1166
1363
 
1167
1364
  public Len(): number {
1168
1365
  throw new Error(
@@ -1219,6 +1416,10 @@ class SliceType implements Type {
1219
1416
  return Slice
1220
1417
  }
1221
1418
 
1419
+ public Comparable(): boolean {
1420
+ return false
1421
+ }
1422
+
1222
1423
  public Size(): number {
1223
1424
  return 24 // slice header size
1224
1425
  }
@@ -1231,7 +1432,7 @@ class SliceType implements Type {
1231
1432
  return 0
1232
1433
  }
1233
1434
 
1234
- public PkgPath?(): string {
1435
+ public PkgPath(): string {
1235
1436
  return ''
1236
1437
  }
1237
1438
 
@@ -1244,6 +1445,16 @@ class SliceType implements Type {
1244
1445
  throw new Error('reflect: Field of non-struct type')
1245
1446
  }
1246
1447
 
1448
+ public FieldByName(name: string): [StructField, boolean] {
1449
+ return typeFieldByName(this, name)
1450
+ }
1451
+
1452
+ public FieldByNameFunc(
1453
+ match: (name: string) => boolean,
1454
+ ): [StructField, boolean] {
1455
+ return typeFieldByNameFunc(this, match)
1456
+ }
1457
+
1247
1458
  public Key(): Type {
1248
1459
  throw new Error('reflect: Key of non-map type')
1249
1460
  }
@@ -1258,6 +1469,10 @@ class SliceType implements Type {
1258
1469
  return false
1259
1470
  }
1260
1471
 
1472
+ public AssignableTo(u: Type | null): boolean {
1473
+ return typeAssignableTo(this, u)
1474
+ }
1475
+
1261
1476
  public OverflowInt(_x: number): boolean {
1262
1477
  throw new Error('reflect: call of reflect.Type.OverflowInt on slice Type')
1263
1478
  }
@@ -1273,6 +1488,9 @@ class SliceType implements Type {
1273
1488
  public NumMethod(): number {
1274
1489
  return 0
1275
1490
  }
1491
+ public MethodByName(_name: string): [Method, boolean] {
1492
+ return [zeroMethod(), false]
1493
+ }
1276
1494
 
1277
1495
  public Len(): number {
1278
1496
  throw new Error('reflect: call of reflect.Type.Len on slice Type')
@@ -1298,6 +1516,10 @@ class ArrayType implements Type {
1298
1516
  return Array
1299
1517
  }
1300
1518
 
1519
+ public Comparable(): boolean {
1520
+ return this._elemType.Comparable()
1521
+ }
1522
+
1301
1523
  public Size(): number {
1302
1524
  return this._elemType.Size() * this._len
1303
1525
  }
@@ -1314,7 +1536,7 @@ class ArrayType implements Type {
1314
1536
  return this._len
1315
1537
  }
1316
1538
 
1317
- public PkgPath?(): string {
1539
+ public PkgPath(): string {
1318
1540
  return ''
1319
1541
  }
1320
1542
 
@@ -1327,6 +1549,16 @@ class ArrayType implements Type {
1327
1549
  throw new Error('reflect: Field of non-struct type')
1328
1550
  }
1329
1551
 
1552
+ public FieldByName(name: string): [StructField, boolean] {
1553
+ return typeFieldByName(this, name)
1554
+ }
1555
+
1556
+ public FieldByNameFunc(
1557
+ match: (name: string) => boolean,
1558
+ ): [StructField, boolean] {
1559
+ return typeFieldByNameFunc(this, match)
1560
+ }
1561
+
1330
1562
  public Key(): Type {
1331
1563
  throw new Error('reflect: Key of non-map type')
1332
1564
  }
@@ -1341,6 +1573,10 @@ class ArrayType implements Type {
1341
1573
  return false
1342
1574
  }
1343
1575
 
1576
+ public AssignableTo(u: Type | null): boolean {
1577
+ return typeAssignableTo(this, u)
1578
+ }
1579
+
1344
1580
  public common?(): rtype {
1345
1581
  return new rtype(this.Kind())
1346
1582
  }
@@ -1360,6 +1596,9 @@ class ArrayType implements Type {
1360
1596
  public NumMethod(): number {
1361
1597
  return 0
1362
1598
  }
1599
+ public MethodByName(_name: string): [Method, boolean] {
1600
+ return [zeroMethod(), false]
1601
+ }
1363
1602
 
1364
1603
  public Bits(): number {
1365
1604
  throw new Error('reflect: call of reflect.Type.Bits on array Type')
@@ -1378,6 +1617,10 @@ class PointerType implements Type {
1378
1617
  return Ptr
1379
1618
  }
1380
1619
 
1620
+ public Comparable(): boolean {
1621
+ return true
1622
+ }
1623
+
1381
1624
  public Size(): number {
1382
1625
  return 8 // pointer size
1383
1626
  }
@@ -1390,7 +1633,7 @@ class PointerType implements Type {
1390
1633
  return 0
1391
1634
  }
1392
1635
 
1393
- public PkgPath?(): string {
1636
+ public PkgPath(): string {
1394
1637
  return ''
1395
1638
  }
1396
1639
 
@@ -1403,6 +1646,16 @@ class PointerType implements Type {
1403
1646
  throw new Error('reflect: Field of non-struct type')
1404
1647
  }
1405
1648
 
1649
+ public FieldByName(name: string): [StructField, boolean] {
1650
+ return typeFieldByName(this, name)
1651
+ }
1652
+
1653
+ public FieldByNameFunc(
1654
+ match: (name: string) => boolean,
1655
+ ): [StructField, boolean] {
1656
+ return typeFieldByNameFunc(this, match)
1657
+ }
1658
+
1406
1659
  public Key(): Type {
1407
1660
  throw new Error('reflect: Key of non-map type')
1408
1661
  }
@@ -1419,6 +1672,10 @@ class PointerType implements Type {
1419
1672
  return typeImplementsInterface(elemTypeName, u)
1420
1673
  }
1421
1674
 
1675
+ public AssignableTo(u: Type | null): boolean {
1676
+ return typeAssignableTo(this, u)
1677
+ }
1678
+
1422
1679
  public common?(): rtype {
1423
1680
  return new rtype(this.Kind())
1424
1681
  }
@@ -1442,6 +1699,9 @@ class PointerType implements Type {
1442
1699
  public NumMethod(): number {
1443
1700
  return 0
1444
1701
  }
1702
+ public MethodByName(name: string): [Method, boolean] {
1703
+ return typeMethodByName(this._elemType, name)
1704
+ }
1445
1705
 
1446
1706
  public Len(): number {
1447
1707
  throw new Error('reflect: call of reflect.Type.Len on pointer Type')
@@ -1464,6 +1724,10 @@ class FunctionType implements Type {
1464
1724
  return Func
1465
1725
  }
1466
1726
 
1727
+ public Comparable(): boolean {
1728
+ return false
1729
+ }
1730
+
1467
1731
  public Size(): number {
1468
1732
  return 8 // function pointer size
1469
1733
  }
@@ -1476,7 +1740,7 @@ class FunctionType implements Type {
1476
1740
  return 0
1477
1741
  }
1478
1742
 
1479
- public PkgPath?(): string {
1743
+ public PkgPath(): string {
1480
1744
  return ''
1481
1745
  }
1482
1746
 
@@ -1489,6 +1753,16 @@ class FunctionType implements Type {
1489
1753
  throw new Error('reflect: Field of non-struct type')
1490
1754
  }
1491
1755
 
1756
+ public FieldByName(name: string): [StructField, boolean] {
1757
+ return typeFieldByName(this, name)
1758
+ }
1759
+
1760
+ public FieldByNameFunc(
1761
+ match: (name: string) => boolean,
1762
+ ): [StructField, boolean] {
1763
+ return typeFieldByNameFunc(this, match)
1764
+ }
1765
+
1492
1766
  public Key(): Type {
1493
1767
  throw new Error('reflect: Key of non-map type')
1494
1768
  }
@@ -1503,6 +1777,10 @@ class FunctionType implements Type {
1503
1777
  return false
1504
1778
  }
1505
1779
 
1780
+ public AssignableTo(u: Type | null): boolean {
1781
+ return typeAssignableTo(this, u)
1782
+ }
1783
+
1506
1784
  public common?(): rtype {
1507
1785
  return new rtype(this.Kind())
1508
1786
  }
@@ -1522,6 +1800,9 @@ class FunctionType implements Type {
1522
1800
  public NumMethod(): number {
1523
1801
  return 0
1524
1802
  }
1803
+ public MethodByName(_name: string): [Method, boolean] {
1804
+ return [zeroMethod(), false]
1805
+ }
1525
1806
 
1526
1807
  public Len(): number {
1527
1808
  throw new Error('reflect: call of reflect.Type.Len on func Type')
@@ -1547,6 +1828,10 @@ class MapType implements Type {
1547
1828
  return Map
1548
1829
  }
1549
1830
 
1831
+ public Comparable(): boolean {
1832
+ return false
1833
+ }
1834
+
1550
1835
  public Size(): number {
1551
1836
  return 8 // map header size
1552
1837
  }
@@ -1563,7 +1848,7 @@ class MapType implements Type {
1563
1848
  return this._keyType
1564
1849
  }
1565
1850
 
1566
- public PkgPath?(): string {
1851
+ public PkgPath(): string {
1567
1852
  return ''
1568
1853
  }
1569
1854
 
@@ -1576,6 +1861,16 @@ class MapType implements Type {
1576
1861
  throw new Error('reflect: Field of non-struct type')
1577
1862
  }
1578
1863
 
1864
+ public FieldByName(name: string): [StructField, boolean] {
1865
+ return typeFieldByName(this, name)
1866
+ }
1867
+
1868
+ public FieldByNameFunc(
1869
+ match: (name: string) => boolean,
1870
+ ): [StructField, boolean] {
1871
+ return typeFieldByNameFunc(this, match)
1872
+ }
1873
+
1579
1874
  public Implements(u: Type | null): boolean {
1580
1875
  if (!u) {
1581
1876
  return false
@@ -1586,6 +1881,10 @@ class MapType implements Type {
1586
1881
  return false
1587
1882
  }
1588
1883
 
1884
+ public AssignableTo(u: Type | null): boolean {
1885
+ return typeAssignableTo(this, u)
1886
+ }
1887
+
1589
1888
  public common?(): rtype {
1590
1889
  return new rtype(this.Kind())
1591
1890
  }
@@ -1605,6 +1904,9 @@ class MapType implements Type {
1605
1904
  public NumMethod(): number {
1606
1905
  return 0
1607
1906
  }
1907
+ public MethodByName(_name: string): [Method, boolean] {
1908
+ return [zeroMethod(), false]
1909
+ }
1608
1910
 
1609
1911
  public Len(): number {
1610
1912
  throw new Error('reflect: call of reflect.Type.Len on map Type')
@@ -1673,6 +1975,74 @@ function typeImplementsInterface(
1673
1975
  return true
1674
1976
  }
1675
1977
 
1978
+ function typeFieldByName(t: Type, name: string): [StructField, boolean] {
1979
+ return typeFieldByNameFunc(t, (fieldName) => fieldName === name)
1980
+ }
1981
+
1982
+ function typeFieldByNameFunc(
1983
+ t: Type,
1984
+ match: (name: string) => boolean,
1985
+ ): [StructField, boolean] {
1986
+ if (t.Kind() !== Struct) {
1987
+ throw new Error('reflect: FieldByName of non-struct type')
1988
+ }
1989
+ for (let i = 0; i < t.NumField(); i++) {
1990
+ const field = t.Field(i)
1991
+ if (match(field.Name)) {
1992
+ return [field, true]
1993
+ }
1994
+ }
1995
+ return [new StructField(), false]
1996
+ }
1997
+
1998
+ function zeroMethod(): Method {
1999
+ return {
2000
+ Name: '',
2001
+ Type: new BasicType(Invalid, '<invalid>'),
2002
+ Func: () => undefined,
2003
+ Index: 0,
2004
+ }
2005
+ }
2006
+
2007
+ function methodFromSignature(
2008
+ signature: $.MethodSignature,
2009
+ index: number,
2010
+ ): Method {
2011
+ return {
2012
+ Name: signature.name,
2013
+ Type: new FunctionType('func'),
2014
+ Func: () => undefined,
2015
+ Index: index,
2016
+ }
2017
+ }
2018
+
2019
+ function typeMethods(t: Type): $.MethodSignature[] {
2020
+ const typeInfo = builtinGetTypeByName(t.String())
2021
+ if (!typeInfo) {
2022
+ return []
2023
+ }
2024
+ if (isStructTypeInfo(typeInfo) || isInterfaceTypeInfo(typeInfo)) {
2025
+ return typeInfo.methods || []
2026
+ }
2027
+ return []
2028
+ }
2029
+
2030
+ function typeMethodByName(t: Type, name: string): [Method, boolean] {
2031
+ const methods = typeMethods(t)
2032
+ const index = methods.findIndex((method) => method.name === name)
2033
+ if (index === -1) {
2034
+ return [zeroMethod(), false]
2035
+ }
2036
+ return [methodFromSignature(methods[index], index), true]
2037
+ }
2038
+
2039
+ function typeAssignableTo(t: Type, u: Type | null): boolean {
2040
+ if (u === null) {
2041
+ return false
2042
+ }
2043
+ return t.String() === u.String() || t.Implements(u)
2044
+ }
2045
+
1676
2046
  class StructType implements Type {
1677
2047
  constructor(
1678
2048
  private _name: string,
@@ -1687,6 +2057,10 @@ class StructType implements Type {
1687
2057
  return Struct
1688
2058
  }
1689
2059
 
2060
+ public Comparable(): boolean {
2061
+ return this._fields.every((field) => field.type.Comparable())
2062
+ }
2063
+
1690
2064
  public Size(): number {
1691
2065
  // Struct size is implementation-defined, we'll use a reasonable default
1692
2066
  return this._fields.reduce((sum, field) => sum + field.type.Size(), 0)
@@ -1700,7 +2074,7 @@ class StructType implements Type {
1700
2074
  return this._fields.length
1701
2075
  }
1702
2076
 
1703
- public PkgPath?(): string {
2077
+ public PkgPath(): string {
1704
2078
  // Extract package path from full type name (e.g., "main.Person" -> "main")
1705
2079
  const dotIndex = this._name.lastIndexOf('.')
1706
2080
  if (dotIndex > 0) {
@@ -1727,11 +2101,23 @@ class StructType implements Type {
1727
2101
  const f = this._fields[i]
1728
2102
  return new StructField({
1729
2103
  Name: f.name,
2104
+ PkgPath: '',
1730
2105
  Type: f.type,
1731
2106
  Tag: f.tag ? new StructTag(f.tag) : undefined,
2107
+ Index: [i],
1732
2108
  })
1733
2109
  }
1734
2110
 
2111
+ public FieldByName(name: string): [StructField, boolean] {
2112
+ return typeFieldByName(this, name)
2113
+ }
2114
+
2115
+ public FieldByNameFunc(
2116
+ match: (name: string) => boolean,
2117
+ ): [StructField, boolean] {
2118
+ return typeFieldByNameFunc(this, match)
2119
+ }
2120
+
1735
2121
  public Key(): Type {
1736
2122
  throw new Error('reflect: Key of non-map type')
1737
2123
  }
@@ -1746,6 +2132,10 @@ class StructType implements Type {
1746
2132
  return typeImplementsInterface(this._name, u)
1747
2133
  }
1748
2134
 
2135
+ public AssignableTo(u: Type | null): boolean {
2136
+ return typeAssignableTo(this, u)
2137
+ }
2138
+
1749
2139
  public common?(): rtype {
1750
2140
  return new rtype(this.Kind())
1751
2141
  }
@@ -1765,7 +2155,11 @@ class StructType implements Type {
1765
2155
  }
1766
2156
 
1767
2157
  public NumMethod(): number {
1768
- return 0
2158
+ return typeMethods(this).length
2159
+ }
2160
+
2161
+ public MethodByName(name: string): [Method, boolean] {
2162
+ return typeMethodByName(this, name)
1769
2163
  }
1770
2164
 
1771
2165
  public Len(): number {
@@ -1805,28 +2199,34 @@ class StructType implements Type {
1805
2199
  } else if (ti && ti.kind) {
1806
2200
  // Handle TypeInfo objects from the builtin type system
1807
2201
  const name = ti.name || 'unknown'
2202
+ const typeName = ti.typeName || ''
1808
2203
  switch (ti.kind) {
1809
2204
  case 'basic':
1810
2205
  // Map TypeScript type names to Go type names
1811
2206
  switch (name) {
1812
2207
  case 'string':
1813
- return new BasicType(String, 'string', 16)
2208
+ return new BasicType(String, 'string', 16, typeName)
1814
2209
  case 'number':
1815
2210
  case 'int':
1816
2211
  case 'int32':
1817
2212
  case 'int64':
1818
- return new BasicType(Int, name === 'number' ? 'int' : name, 8)
2213
+ return new BasicType(
2214
+ Int,
2215
+ name === 'number' ? 'int' : name,
2216
+ 8,
2217
+ typeName,
2218
+ )
1819
2219
  case 'boolean':
1820
2220
  case 'bool':
1821
- return new BasicType(Bool, 'bool', 1)
2221
+ return new BasicType(Bool, 'bool', 1, typeName)
1822
2222
  case 'float64':
1823
- return new BasicType(Float64, 'float64', 8)
2223
+ return new BasicType(Float64, 'float64', 8, typeName)
1824
2224
  case 'complex64':
1825
- return new BasicType(Complex64, 'complex64', 8)
2225
+ return new BasicType(Complex64, 'complex64', 8, typeName)
1826
2226
  case 'complex128':
1827
- return new BasicType(Complex128, 'complex128', 16)
2227
+ return new BasicType(Complex128, 'complex128', 16, typeName)
1828
2228
  default:
1829
- return new BasicType(Invalid, name, 8)
2229
+ return new BasicType(Invalid, name, 8, typeName)
1830
2230
  }
1831
2231
  case 'slice':
1832
2232
  if (ti.elemType) {
@@ -1845,7 +2245,7 @@ class StructType implements Type {
1845
2245
  case 'interface':
1846
2246
  return new InterfaceType(name)
1847
2247
  case 'struct':
1848
- return new StructType(name, [])
2248
+ return new StructType(name, structFieldsFromTypeInfo(ti))
1849
2249
  default:
1850
2250
  return new BasicType(Invalid, name, 8)
1851
2251
  }
@@ -1854,6 +2254,24 @@ class StructType implements Type {
1854
2254
  }
1855
2255
  }
1856
2256
 
2257
+ function structFieldsFromTypeInfo(
2258
+ ti: $.StructTypeInfo,
2259
+ ): Array<{ name: string; type: Type; tag?: string }> {
2260
+ return Object.entries(ti.fields || {}).map(([name, fieldInfo]) => {
2261
+ if (isStructFieldInfo(fieldInfo)) {
2262
+ return {
2263
+ name: fieldInfo.name ?? name,
2264
+ type: typeFromTypeInfo(fieldInfo.type),
2265
+ tag: fieldInfo.tag,
2266
+ }
2267
+ }
2268
+ return {
2269
+ name,
2270
+ type: typeFromTypeInfo(fieldInfo),
2271
+ }
2272
+ })
2273
+ }
2274
+
1857
2275
  class ChannelType implements Type {
1858
2276
  constructor(
1859
2277
  private _elemType: Type,
@@ -1878,6 +2296,10 @@ class ChannelType implements Type {
1878
2296
  return Chan
1879
2297
  }
1880
2298
 
2299
+ public Comparable(): boolean {
2300
+ return true
2301
+ }
2302
+
1881
2303
  public Size(): number {
1882
2304
  // Channels are represented as pointers, so pointer size
1883
2305
  return 8
@@ -1891,7 +2313,7 @@ class ChannelType implements Type {
1891
2313
  return 0
1892
2314
  }
1893
2315
 
1894
- public PkgPath?(): string {
2316
+ public PkgPath(): string {
1895
2317
  return ''
1896
2318
  }
1897
2319
 
@@ -1904,6 +2326,16 @@ class ChannelType implements Type {
1904
2326
  throw new Error('reflect: Field of non-struct type')
1905
2327
  }
1906
2328
 
2329
+ public FieldByName(name: string): [StructField, boolean] {
2330
+ return typeFieldByName(this, name)
2331
+ }
2332
+
2333
+ public FieldByNameFunc(
2334
+ match: (name: string) => boolean,
2335
+ ): [StructField, boolean] {
2336
+ return typeFieldByNameFunc(this, match)
2337
+ }
2338
+
1907
2339
  public Key(): Type {
1908
2340
  throw new Error('reflect: Key of non-map type')
1909
2341
  }
@@ -1918,6 +2350,10 @@ class ChannelType implements Type {
1918
2350
  return false
1919
2351
  }
1920
2352
 
2353
+ public AssignableTo(u: Type | null): boolean {
2354
+ return typeAssignableTo(this, u)
2355
+ }
2356
+
1921
2357
  public common?(): rtype {
1922
2358
  return new rtype(this.Kind())
1923
2359
  }
@@ -1941,6 +2377,9 @@ class ChannelType implements Type {
1941
2377
  public NumMethod(): number {
1942
2378
  return 0
1943
2379
  }
2380
+ public MethodByName(_name: string): [Method, boolean] {
2381
+ return [zeroMethod(), false]
2382
+ }
1944
2383
 
1945
2384
  public Len(): number {
1946
2385
  throw new Error('reflect: call of reflect.Type.Len on chan Type')
@@ -1966,6 +2405,10 @@ class InterfaceType implements Type {
1966
2405
  return Interface
1967
2406
  }
1968
2407
 
2408
+ public Comparable(): boolean {
2409
+ return true
2410
+ }
2411
+
1969
2412
  public Size(): number {
1970
2413
  return 16
1971
2414
  }
@@ -1978,7 +2421,7 @@ class InterfaceType implements Type {
1978
2421
  return 0
1979
2422
  }
1980
2423
 
1981
- public PkgPath?(): string {
2424
+ public PkgPath(): string {
1982
2425
  if (this._name === 'interface{}' || this._name.startsWith('interface {')) {
1983
2426
  return ''
1984
2427
  }
@@ -2004,6 +2447,16 @@ class InterfaceType implements Type {
2004
2447
  throw new Error('reflect: Field of non-struct type')
2005
2448
  }
2006
2449
 
2450
+ public FieldByName(name: string): [StructField, boolean] {
2451
+ return typeFieldByName(this, name)
2452
+ }
2453
+
2454
+ public FieldByNameFunc(
2455
+ match: (name: string) => boolean,
2456
+ ): [StructField, boolean] {
2457
+ return typeFieldByNameFunc(this, match)
2458
+ }
2459
+
2007
2460
  public Key(): Type {
2008
2461
  throw new Error('reflect: Key of non-map type')
2009
2462
  }
@@ -2012,6 +2465,10 @@ class InterfaceType implements Type {
2012
2465
  return false
2013
2466
  }
2014
2467
 
2468
+ public AssignableTo(u: Type | null): boolean {
2469
+ return typeAssignableTo(this, u)
2470
+ }
2471
+
2015
2472
  public common?(): rtype {
2016
2473
  return new rtype(this.Kind())
2017
2474
  }
@@ -2035,7 +2492,11 @@ class InterfaceType implements Type {
2035
2492
  }
2036
2493
 
2037
2494
  public NumMethod(): number {
2038
- return 0
2495
+ return typeMethods(this).length
2496
+ }
2497
+
2498
+ public MethodByName(name: string): [Method, boolean] {
2499
+ return typeMethodByName(this, name)
2039
2500
  }
2040
2501
 
2041
2502
  public Len(): number {
@@ -2293,7 +2754,7 @@ function getTypeOf(value: ReflectValue): Type {
2293
2754
 
2294
2755
  // Exported functions as required by godoc.txt
2295
2756
  export function TypeOf(i: ReflectValue): Type {
2296
- return getTypeOf(i)
2757
+ return internType(getTypeOf(i))
2297
2758
  }
2298
2759
 
2299
2760
  export function ValueOf(i: ReflectValue): Value {
@@ -2312,16 +2773,16 @@ export function TypeAssert(
2312
2773
  }
2313
2774
 
2314
2775
  export function ArrayOf(length: number, elem: Type): Type {
2315
- return new ArrayType(elem, length)
2776
+ return internType(new ArrayType(elem, length))
2316
2777
  }
2317
2778
 
2318
2779
  export function SliceOf(t: Type): Type {
2319
- return new SliceType(t)
2780
+ return internType(new SliceType(t))
2320
2781
  }
2321
2782
 
2322
2783
  export function PointerTo(t: Type | null): Type | null {
2323
2784
  if (t === null) return null
2324
- return new PointerType(t)
2785
+ return internType(new PointerType(t))
2325
2786
  }
2326
2787
 
2327
2788
  export function PtrTo(t: Type | null): Type | null {
@@ -2329,76 +2790,98 @@ export function PtrTo(t: Type | null): Type | null {
2329
2790
  }
2330
2791
 
2331
2792
  export function MapOf(key: Type, elem: Type): Type {
2332
- return new MapType(key, elem)
2793
+ return internType(new MapType(key, elem))
2333
2794
  }
2334
2795
 
2335
2796
  export function ChanOf(dir: ChanDir, t: Type): Type {
2336
- return new ChannelType(t, dir)
2797
+ return internType(new ChannelType(t, dir))
2337
2798
  }
2338
2799
 
2339
2800
  export function TypeFor(typeArgs?: $.GenericTypeArgs): Type {
2340
2801
  const descriptor = typeArgs?.T
2341
2802
  if (descriptor?.type) {
2342
- return typeFromTypeInfo(descriptor.type)
2803
+ return internType(typeFromTypeInfo(descriptor.type))
2343
2804
  }
2344
2805
  if (descriptor?.methods) {
2345
2806
  const methods = Object.keys(descriptor.methods)
2346
2807
  if (methods.length !== 0) {
2347
- return new InterfaceType(
2348
- `interface { ${methods.map((method) => method + '()').join('; ')} }`,
2808
+ return internType(
2809
+ new InterfaceType(
2810
+ `interface { ${methods.map((method) => method + '()').join('; ')} }`,
2811
+ ),
2349
2812
  )
2350
2813
  }
2351
2814
  }
2352
2815
  if (descriptor?.zero) {
2353
- return getTypeOf(descriptor.zero())
2816
+ return internType(getTypeOf(descriptor.zero()))
2354
2817
  }
2355
- return new InterfaceType('interface{}')
2818
+ return internType(new InterfaceType('interface{}'))
2356
2819
  }
2357
2820
 
2358
2821
  function typeFromTypeInfo(info: $.TypeInfo | string): Type {
2359
2822
  if (typeof info === 'string') {
2360
2823
  const registered = builtinGetTypeByName(info)
2361
2824
  if (registered) {
2362
- return typeFromTypeInfo(registered)
2825
+ return internType(typeFromTypeInfo(registered))
2363
2826
  }
2364
- return StructType.createTypeFromFieldInfo(info)
2827
+ return internType(StructType.createTypeFromFieldInfo(info))
2365
2828
  }
2366
2829
  switch (info.kind) {
2367
2830
  case $.TypeKind.Array:
2368
- return new ArrayType(
2369
- typeFromTypeInfo(
2370
- info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2831
+ return internType(
2832
+ new ArrayType(
2833
+ typeFromTypeInfo(
2834
+ info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2835
+ ),
2836
+ info.length,
2371
2837
  ),
2372
- info.length,
2373
2838
  )
2839
+ case $.TypeKind.Basic:
2840
+ return internType(StructType.createTypeFromFieldInfo(info))
2374
2841
  case $.TypeKind.Channel:
2375
- return new ChannelType(
2376
- typeFromTypeInfo(
2377
- info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2842
+ return internType(
2843
+ new ChannelType(
2844
+ typeFromTypeInfo(
2845
+ info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2846
+ ),
2847
+ chanDirFromTypeInfo(info.direction),
2378
2848
  ),
2379
- chanDirFromTypeInfo(info.direction),
2380
2849
  )
2381
2850
  case $.TypeKind.Function:
2382
- return functionTypeFromInfo(info)
2851
+ return internType(functionTypeFromInfo(info))
2383
2852
  case $.TypeKind.Interface:
2384
- return interfaceTypeFromInfo(info)
2853
+ return internType(interfaceTypeFromInfo(info))
2385
2854
  case $.TypeKind.Map:
2386
- return new MapType(
2387
- typeFromTypeInfo(
2388
- info.keyType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2855
+ return internType(
2856
+ new MapType(
2857
+ typeFromTypeInfo(
2858
+ info.keyType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2859
+ ),
2860
+ typeFromTypeInfo(
2861
+ info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2862
+ ),
2389
2863
  ),
2390
- typeFromTypeInfo(
2391
- info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2864
+ )
2865
+ case $.TypeKind.Slice:
2866
+ return internType(
2867
+ new SliceType(
2868
+ typeFromTypeInfo(
2869
+ info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2870
+ ),
2392
2871
  ),
2393
2872
  )
2873
+ case $.TypeKind.Struct:
2874
+ return internType(StructType.createTypeFromFieldInfo(info))
2394
2875
  case $.TypeKind.Pointer:
2395
- return new PointerType(
2396
- typeFromTypeInfo(
2397
- info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2876
+ return internType(
2877
+ new PointerType(
2878
+ typeFromTypeInfo(
2879
+ info.elemType ?? { kind: $.TypeKind.Basic, name: 'unknown' },
2880
+ ),
2398
2881
  ),
2399
2882
  )
2400
2883
  default:
2401
- return StructType.createTypeFromFieldInfo(info)
2884
+ return internType(StructType.createTypeFromFieldInfo(info))
2402
2885
  }
2403
2886
  }
2404
2887