goscript 0.1.0 → 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 (495) hide show
  1. package/README.md +267 -255
  2. package/cmd/goscript/cmd-test.go +286 -0
  3. package/cmd/goscript/cmd-test_test.go +76 -0
  4. package/cmd/goscript/cmd_compile.go +9 -0
  5. package/cmd/goscript/main.go +1 -0
  6. package/compiler/build-flags.go +38 -0
  7. package/compiler/compile-request.go +33 -0
  8. package/compiler/compiler.go +1 -1
  9. package/compiler/compliance_test.go +0 -10
  10. package/compiler/config.go +2 -0
  11. package/compiler/gotest/owner.go +24 -0
  12. package/compiler/gotest/package-result.go +69 -0
  13. package/compiler/gotest/request.go +210 -0
  14. package/compiler/gotest/result.go +28 -0
  15. package/compiler/gotest/runner.go +1225 -0
  16. package/compiler/gotest/runner_test.go +1271 -0
  17. package/compiler/gotest/test.go +9 -0
  18. package/compiler/index.test.ts +1 -1
  19. package/compiler/lowered-program.go +80 -21
  20. package/compiler/lowering.go +6754 -602
  21. package/compiler/override-facts.go +357 -0
  22. package/compiler/override-registry.go +52 -190
  23. package/compiler/override-registry_test.go +182 -0
  24. package/compiler/package-graph.go +50 -27
  25. package/compiler/package-graph_test.go +99 -9
  26. package/compiler/package-test-function.go +9 -0
  27. package/compiler/package-test-graph-package.go +40 -0
  28. package/compiler/package-test-graph-variant.go +129 -0
  29. package/compiler/package-test-graph.go +112 -0
  30. package/compiler/package-test-graph_test.go +202 -0
  31. package/compiler/runtime-contract.go +229 -29
  32. package/compiler/runtime-contract_test.go +44 -30
  33. package/compiler/semantic-model-types.go +25 -6
  34. package/compiler/semantic-model.go +819 -74
  35. package/compiler/semantic-model_test.go +104 -0
  36. package/compiler/service.go +10 -4
  37. package/compiler/skeleton_test.go +2777 -524
  38. package/compiler/tsworkspace/owner-process-unix_test.go +72 -0
  39. package/compiler/tsworkspace/owner.go +342 -0
  40. package/compiler/tsworkspace/owner_test.go +93 -0
  41. package/compiler/tsworkspace/result.go +17 -0
  42. package/compiler/tsworkspace/tool-process-other.go +14 -0
  43. package/compiler/tsworkspace/tool-process-unix.go +19 -0
  44. package/compiler/typescript-emitter.go +576 -86
  45. package/compiler/wasm/compile.go +1 -1
  46. package/compiler/wasm/compile_test.go +61 -11
  47. package/compiler/wasm_api.go +172 -7
  48. package/dist/gs/builtin/builtin.d.ts +40 -3
  49. package/dist/gs/builtin/builtin.js +430 -22
  50. package/dist/gs/builtin/builtin.js.map +1 -1
  51. package/dist/gs/builtin/channel.d.ts +32 -10
  52. package/dist/gs/builtin/channel.js +119 -25
  53. package/dist/gs/builtin/channel.js.map +1 -1
  54. package/dist/gs/builtin/defer.d.ts +1 -0
  55. package/dist/gs/builtin/defer.js +12 -2
  56. package/dist/gs/builtin/defer.js.map +1 -1
  57. package/dist/gs/builtin/hostio.d.ts +9 -0
  58. package/dist/gs/builtin/hostio.js +25 -0
  59. package/dist/gs/builtin/hostio.js.map +1 -1
  60. package/dist/gs/builtin/map.js +40 -6
  61. package/dist/gs/builtin/map.js.map +1 -1
  62. package/dist/gs/builtin/print.js.map +1 -1
  63. package/dist/gs/builtin/slice.d.ts +64 -10
  64. package/dist/gs/builtin/slice.js +619 -244
  65. package/dist/gs/builtin/slice.js.map +1 -1
  66. package/dist/gs/builtin/type.d.ts +7 -2
  67. package/dist/gs/builtin/type.js +128 -29
  68. package/dist/gs/builtin/type.js.map +1 -1
  69. package/dist/gs/builtin/varRef.d.ts +7 -0
  70. package/dist/gs/builtin/varRef.js +23 -0
  71. package/dist/gs/builtin/varRef.js.map +1 -1
  72. package/dist/gs/bytes/buffer.gs.js +74 -70
  73. package/dist/gs/bytes/buffer.gs.js.map +1 -1
  74. package/dist/gs/bytes/iter.gs.js +13 -13
  75. package/dist/gs/bytes/iter.gs.js.map +1 -1
  76. package/dist/gs/bytes/reader.gs.js +20 -18
  77. package/dist/gs/bytes/reader.gs.js.map +1 -1
  78. package/dist/gs/compress/zlib/index.d.ts +26 -0
  79. package/dist/gs/compress/zlib/index.js +168 -0
  80. package/dist/gs/compress/zlib/index.js.map +1 -0
  81. package/dist/gs/context/context.d.ts +6 -5
  82. package/dist/gs/context/context.js +17 -12
  83. package/dist/gs/context/context.js.map +1 -1
  84. package/dist/gs/crypto/ecdh/index.d.ts +52 -0
  85. package/dist/gs/crypto/ecdh/index.js +226 -0
  86. package/dist/gs/crypto/ecdh/index.js.map +1 -0
  87. package/dist/gs/crypto/ed25519/index.d.ts +34 -0
  88. package/dist/gs/crypto/ed25519/index.js +160 -0
  89. package/dist/gs/crypto/ed25519/index.js.map +1 -0
  90. package/dist/gs/crypto/internal/constanttime/index.d.ts +4 -0
  91. package/dist/gs/crypto/internal/constanttime/index.js +18 -0
  92. package/dist/gs/crypto/internal/constanttime/index.js.map +1 -0
  93. package/dist/gs/crypto/internal/fips140deps/byteorder/index.d.ts +1 -0
  94. package/dist/gs/crypto/internal/fips140deps/byteorder/index.js +2 -0
  95. package/dist/gs/crypto/internal/fips140deps/byteorder/index.js.map +1 -0
  96. package/dist/gs/crypto/internal/fips140deps/godebug/index.d.ts +1 -0
  97. package/dist/gs/crypto/internal/fips140deps/godebug/index.js +2 -0
  98. package/dist/gs/crypto/internal/fips140deps/godebug/index.js.map +1 -0
  99. package/dist/gs/crypto/rand/index.d.ts +2 -0
  100. package/dist/gs/crypto/rand/index.js +85 -0
  101. package/dist/gs/crypto/rand/index.js.map +1 -1
  102. package/dist/gs/crypto/sha256/index.d.ts +8 -0
  103. package/dist/gs/crypto/sha256/index.js +118 -0
  104. package/dist/gs/crypto/sha256/index.js.map +1 -0
  105. package/dist/gs/crypto/sha512/index.d.ts +14 -0
  106. package/dist/gs/crypto/sha512/index.js +129 -0
  107. package/dist/gs/crypto/sha512/index.js.map +1 -0
  108. package/dist/gs/embed/index.d.ts +7 -0
  109. package/dist/gs/embed/index.js +16 -0
  110. package/dist/gs/embed/index.js.map +1 -0
  111. package/dist/gs/encoding/json/index.d.ts +4 -0
  112. package/dist/gs/encoding/json/index.js +33 -0
  113. package/dist/gs/encoding/json/index.js.map +1 -1
  114. package/dist/gs/errors/errors.d.ts +4 -0
  115. package/dist/gs/errors/errors.js +108 -4
  116. package/dist/gs/errors/errors.js.map +1 -1
  117. package/dist/gs/fmt/fmt.d.ts +4 -4
  118. package/dist/gs/fmt/fmt.js +42 -11
  119. package/dist/gs/fmt/fmt.js.map +1 -1
  120. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +37 -2
  121. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +245 -1
  122. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
  123. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.d.ts +189 -0
  124. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +861 -0
  125. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -0
  126. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +217 -0
  127. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +814 -0
  128. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -0
  129. package/dist/gs/github.com/aperturerobotics/util/conc/index.d.ts +20 -0
  130. package/dist/gs/github.com/aperturerobotics/util/conc/index.js +134 -0
  131. package/dist/gs/github.com/aperturerobotics/util/conc/index.js.map +1 -0
  132. package/dist/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.js.map +1 -1
  133. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.d.ts +3 -0
  134. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js +50 -0
  135. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js.map +1 -0
  136. package/dist/gs/github.com/klauspost/compress/internal/le/index.d.ts +9 -0
  137. package/dist/gs/github.com/klauspost/compress/internal/le/index.js +72 -0
  138. package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -0
  139. package/dist/gs/github.com/mr-tron/base58/base58/index.d.ts +27 -0
  140. package/dist/gs/github.com/mr-tron/base58/base58/index.js +172 -0
  141. package/dist/gs/github.com/mr-tron/base58/base58/index.js.map +1 -0
  142. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.d.ts +21 -0
  143. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js +22 -0
  144. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js.map +1 -0
  145. package/dist/gs/go/internal/scannerhooks/index.d.ts +3 -0
  146. package/dist/gs/go/internal/scannerhooks/index.js +5 -0
  147. package/dist/gs/go/internal/scannerhooks/index.js.map +1 -0
  148. package/dist/gs/go/scanner/index.d.ts +13 -0
  149. package/dist/gs/go/scanner/index.js +35 -0
  150. package/dist/gs/go/scanner/index.js.map +1 -1
  151. package/dist/gs/go/token/index.d.ts +156 -0
  152. package/dist/gs/go/token/index.js +507 -4
  153. package/dist/gs/go/token/index.js.map +1 -1
  154. package/dist/gs/hash/fnv/index.d.ts +57 -0
  155. package/dist/gs/hash/fnv/index.js +299 -0
  156. package/dist/gs/hash/fnv/index.js.map +1 -0
  157. package/dist/gs/hash/index.d.ts +17 -0
  158. package/dist/gs/hash/index.js +94 -0
  159. package/dist/gs/hash/index.js.map +1 -0
  160. package/dist/gs/internal/abi/index.d.ts +4 -0
  161. package/dist/gs/internal/abi/index.js +10 -0
  162. package/dist/gs/internal/abi/index.js.map +1 -1
  163. package/dist/gs/internal/bytealg/index.d.ts +2 -0
  164. package/dist/gs/internal/bytealg/index.js +14 -0
  165. package/dist/gs/internal/bytealg/index.js.map +1 -1
  166. package/dist/gs/internal/byteorder/index.d.ts +8 -2
  167. package/dist/gs/internal/byteorder/index.js +56 -25
  168. package/dist/gs/internal/byteorder/index.js.map +1 -1
  169. package/dist/gs/internal/godebug/index.d.ts +12 -0
  170. package/dist/gs/internal/godebug/index.js +30 -0
  171. package/dist/gs/internal/godebug/index.js.map +1 -0
  172. package/dist/gs/io/fs/index.d.ts +1 -0
  173. package/dist/gs/io/fs/index.js +1 -0
  174. package/dist/gs/io/fs/index.js.map +1 -1
  175. package/dist/gs/io/fs/readlink.d.ts +8 -0
  176. package/dist/gs/io/fs/readlink.js +60 -0
  177. package/dist/gs/io/fs/readlink.js.map +1 -0
  178. package/dist/gs/io/fs/walk.d.ts +3 -3
  179. package/dist/gs/io/fs/walk.js +7 -7
  180. package/dist/gs/io/fs/walk.js.map +1 -1
  181. package/dist/gs/io/io.d.ts +40 -6
  182. package/dist/gs/io/io.js +151 -26
  183. package/dist/gs/io/io.js.map +1 -1
  184. package/dist/gs/iter/iter.d.ts +3 -2
  185. package/dist/gs/iter/iter.js.map +1 -1
  186. package/dist/gs/maps/iter.d.ts +5 -5
  187. package/dist/gs/maps/iter.js +48 -21
  188. package/dist/gs/maps/iter.js.map +1 -1
  189. package/dist/gs/maps/maps.d.ts +6 -6
  190. package/dist/gs/maps/maps.js +1 -1
  191. package/dist/gs/maps/maps.js.map +1 -1
  192. package/dist/gs/math/bits/index.d.ts +13 -4
  193. package/dist/gs/math/bits/index.js +70 -48
  194. package/dist/gs/math/bits/index.js.map +1 -1
  195. package/dist/gs/math/const.gs.d.ts +5 -5
  196. package/dist/gs/math/const.gs.js +4 -4
  197. package/dist/gs/math/const.gs.js.map +1 -1
  198. package/dist/gs/mime/index.d.ts +1 -0
  199. package/dist/gs/mime/index.js +52 -0
  200. package/dist/gs/mime/index.js.map +1 -0
  201. package/dist/gs/net/http/httptest/index.d.ts +30 -0
  202. package/dist/gs/net/http/httptest/index.js +101 -0
  203. package/dist/gs/net/http/httptest/index.js.map +1 -0
  204. package/dist/gs/net/http/index.d.ts +131 -0
  205. package/dist/gs/net/http/index.js +307 -0
  206. package/dist/gs/net/http/index.js.map +1 -0
  207. package/dist/gs/net/http/pprof/index.d.ts +8 -0
  208. package/dist/gs/net/http/pprof/index.js +59 -0
  209. package/dist/gs/net/http/pprof/index.js.map +1 -0
  210. package/dist/gs/os/dir_unix.gs.js +2 -2
  211. package/dist/gs/os/dir_unix.gs.js.map +1 -1
  212. package/dist/gs/os/error.gs.js +9 -7
  213. package/dist/gs/os/error.gs.js.map +1 -1
  214. package/dist/gs/os/types_js.gs.js +95 -15
  215. package/dist/gs/os/types_js.gs.js.map +1 -1
  216. package/dist/gs/path/filepath/match.js +165 -3
  217. package/dist/gs/path/filepath/match.js.map +1 -1
  218. package/dist/gs/path/filepath/path.d.ts +8 -4
  219. package/dist/gs/path/filepath/path.js +192 -8
  220. package/dist/gs/path/filepath/path.js.map +1 -1
  221. package/dist/gs/path/path.d.ts +4 -1
  222. package/dist/gs/path/path.js +16 -4
  223. package/dist/gs/path/path.js.map +1 -1
  224. package/dist/gs/reflect/index.d.ts +4 -3
  225. package/dist/gs/reflect/index.js +3 -2
  226. package/dist/gs/reflect/index.js.map +1 -1
  227. package/dist/gs/reflect/iter.js +2 -2
  228. package/dist/gs/reflect/iter.js.map +1 -1
  229. package/dist/gs/reflect/map.js +29 -0
  230. package/dist/gs/reflect/map.js.map +1 -1
  231. package/dist/gs/reflect/type.d.ts +31 -9
  232. package/dist/gs/reflect/type.js +536 -43
  233. package/dist/gs/reflect/type.js.map +1 -1
  234. package/dist/gs/reflect/types.d.ts +1 -0
  235. package/dist/gs/reflect/types.js +3 -1
  236. package/dist/gs/reflect/types.js.map +1 -1
  237. package/dist/gs/reflect/value.d.ts +4 -1
  238. package/dist/gs/reflect/value.js +39 -1
  239. package/dist/gs/reflect/value.js.map +1 -1
  240. package/dist/gs/reflect/visiblefields.js +1 -1
  241. package/dist/gs/reflect/visiblefields.js.map +1 -1
  242. package/dist/gs/runtime/debug/index.d.ts +41 -0
  243. package/dist/gs/runtime/debug/index.js +66 -0
  244. package/dist/gs/runtime/debug/index.js.map +1 -0
  245. package/dist/gs/runtime/pprof/index.d.ts +20 -0
  246. package/dist/gs/runtime/pprof/index.js +85 -0
  247. package/dist/gs/runtime/pprof/index.js.map +1 -0
  248. package/dist/gs/runtime/runtime.d.ts +35 -3
  249. package/dist/gs/runtime/runtime.js +72 -0
  250. package/dist/gs/runtime/runtime.js.map +1 -1
  251. package/dist/gs/runtime/trace/index.d.ts +19 -0
  252. package/dist/gs/runtime/trace/index.js +64 -0
  253. package/dist/gs/runtime/trace/index.js.map +1 -0
  254. package/dist/gs/slices/slices.d.ts +42 -8
  255. package/dist/gs/slices/slices.js +425 -11
  256. package/dist/gs/slices/slices.js.map +1 -1
  257. package/dist/gs/sort/slice.gs.d.ts +5 -3
  258. package/dist/gs/sort/slice.gs.js +60 -22
  259. package/dist/gs/sort/slice.gs.js.map +1 -1
  260. package/dist/gs/sort/sort.gs.d.ts +4 -4
  261. package/dist/gs/sort/sort.gs.js +11 -8
  262. package/dist/gs/sort/sort.gs.js.map +1 -1
  263. package/dist/gs/strings/builder.d.ts +1 -1
  264. package/dist/gs/strings/builder.js +29 -19
  265. package/dist/gs/strings/builder.js.map +1 -1
  266. package/dist/gs/strings/iter.js +140 -75
  267. package/dist/gs/strings/iter.js.map +1 -1
  268. package/dist/gs/strings/replace.js +2 -2
  269. package/dist/gs/strings/replace.js.map +1 -1
  270. package/dist/gs/strings/strings.js +52 -6
  271. package/dist/gs/strings/strings.js.map +1 -1
  272. package/dist/gs/sync/atomic/type.gs.d.ts +9 -8
  273. package/dist/gs/sync/atomic/type.gs.js +0 -2
  274. package/dist/gs/sync/atomic/type.gs.js.map +1 -1
  275. package/dist/gs/sync/sync.d.ts +8 -3
  276. package/dist/gs/sync/sync.js +66 -11
  277. package/dist/gs/sync/sync.js.map +1 -1
  278. package/dist/gs/syscall/constants.d.ts +36 -24
  279. package/dist/gs/syscall/constants.js +12 -0
  280. package/dist/gs/syscall/constants.js.map +1 -1
  281. package/dist/gs/syscall/errors.d.ts +117 -111
  282. package/dist/gs/syscall/errors.js +45 -0
  283. package/dist/gs/syscall/errors.js.map +1 -1
  284. package/dist/gs/syscall/fs.d.ts +37 -0
  285. package/dist/gs/syscall/fs.js +102 -0
  286. package/dist/gs/syscall/fs.js.map +1 -1
  287. package/dist/gs/syscall/js/index.d.ts +90 -0
  288. package/dist/gs/syscall/js/index.js +383 -0
  289. package/dist/gs/syscall/js/index.js.map +1 -0
  290. package/dist/gs/syscall/types.d.ts +26 -1
  291. package/dist/gs/syscall/types.js +45 -1
  292. package/dist/gs/syscall/types.js.map +1 -1
  293. package/dist/gs/testing/index.d.ts +1 -0
  294. package/dist/gs/testing/index.js +2 -0
  295. package/dist/gs/testing/index.js.map +1 -0
  296. package/dist/gs/testing/testing.d.ts +78 -0
  297. package/dist/gs/testing/testing.js +318 -0
  298. package/dist/gs/testing/testing.js.map +1 -0
  299. package/dist/gs/time/time.d.ts +41 -4
  300. package/dist/gs/time/time.js +227 -36
  301. package/dist/gs/time/time.js.map +1 -1
  302. package/dist/gs/unicode/unicode.d.ts +23 -1
  303. package/dist/gs/unicode/unicode.js +79 -10
  304. package/dist/gs/unicode/unicode.js.map +1 -1
  305. package/dist/gs/unicode/utf8/utf8.d.ts +4 -4
  306. package/dist/gs/unicode/utf8/utf8.js +24 -11
  307. package/dist/gs/unicode/utf8/utf8.js.map +1 -1
  308. package/dist/gs/unique/index.d.ts +11 -0
  309. package/dist/gs/unique/index.js +76 -0
  310. package/dist/gs/unique/index.js.map +1 -0
  311. package/go.mod +8 -8
  312. package/go.sum +14 -14
  313. package/gs/builtin/builtin.ts +585 -27
  314. package/gs/builtin/channel.ts +183 -29
  315. package/gs/builtin/defer.ts +13 -2
  316. package/gs/builtin/hostio.test.ts +1 -0
  317. package/gs/builtin/hostio.ts +38 -0
  318. package/gs/builtin/map.ts +46 -6
  319. package/gs/builtin/print.ts +12 -3
  320. package/gs/builtin/runtime-contract.test.ts +383 -10
  321. package/gs/builtin/slice.test.ts +70 -0
  322. package/gs/builtin/slice.ts +785 -265
  323. package/gs/builtin/type.ts +160 -41
  324. package/gs/builtin/varRef.ts +40 -1
  325. package/gs/bytes/buffer.gs.ts +74 -70
  326. package/gs/bytes/iter.gs.ts +13 -14
  327. package/gs/bytes/meta.json +8 -3
  328. package/gs/bytes/reader.gs.ts +20 -19
  329. package/gs/compress/zlib/index.test.ts +28 -0
  330. package/gs/compress/zlib/index.ts +200 -0
  331. package/gs/compress/zlib/meta.json +3 -0
  332. package/gs/context/context.test.ts +71 -0
  333. package/gs/context/context.ts +30 -29
  334. package/gs/crypto/ecdh/index.test.ts +43 -0
  335. package/gs/crypto/ecdh/index.ts +274 -0
  336. package/gs/crypto/ed25519/index.test.ts +41 -0
  337. package/gs/crypto/ed25519/index.ts +238 -0
  338. package/gs/crypto/ed25519/meta.json +13 -0
  339. package/gs/crypto/internal/constanttime/index.test.ts +25 -0
  340. package/gs/crypto/internal/constanttime/index.ts +22 -0
  341. package/gs/crypto/internal/fips140deps/byteorder/index.ts +1 -0
  342. package/gs/crypto/internal/fips140deps/godebug/index.ts +1 -0
  343. package/gs/crypto/rand/index.test.ts +89 -1
  344. package/gs/crypto/rand/index.ts +103 -1
  345. package/gs/crypto/rand/meta.json +4 -1
  346. package/gs/crypto/sha256/index.test.ts +78 -0
  347. package/gs/crypto/sha256/index.ts +150 -0
  348. package/gs/crypto/sha256/meta.json +9 -0
  349. package/gs/crypto/sha512/index.test.ts +31 -0
  350. package/gs/crypto/sha512/index.ts +161 -0
  351. package/gs/crypto/sha512/meta.json +11 -0
  352. package/gs/embed/index.ts +20 -0
  353. package/gs/embed/meta.json +5 -0
  354. package/gs/encoding/json/index.test.ts +39 -3
  355. package/gs/encoding/json/index.ts +45 -3
  356. package/gs/errors/errors.test.ts +85 -0
  357. package/gs/errors/errors.ts +132 -4
  358. package/gs/fmt/fmt.test.ts +3 -1
  359. package/gs/fmt/fmt.ts +55 -19
  360. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +128 -1
  361. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +342 -4
  362. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +180 -0
  363. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +1084 -0
  364. package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +31 -0
  365. package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +1233 -0
  366. package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +46 -0
  367. package/gs/github.com/aperturerobotics/util/conc/index.test.ts +30 -0
  368. package/gs/github.com/aperturerobotics/util/conc/index.ts +172 -0
  369. package/gs/github.com/aperturerobotics/util/conc/meta.json +9 -0
  370. package/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.ts +1 -4
  371. package/gs/github.com/hack-pad/safejs/internal/catch/index.test.ts +35 -0
  372. package/gs/github.com/hack-pad/safejs/internal/catch/index.ts +65 -0
  373. package/gs/github.com/hack-pad/safejs/internal/catch/meta.json +9 -0
  374. package/gs/github.com/klauspost/compress/internal/le/index.test.ts +37 -0
  375. package/gs/github.com/klauspost/compress/internal/le/index.ts +115 -0
  376. package/gs/github.com/mr-tron/base58/base58/index.test.ts +70 -0
  377. package/gs/github.com/mr-tron/base58/base58/index.ts +231 -0
  378. package/gs/github.com/mr-tron/base58/base58/meta.json +3 -0
  379. package/gs/github.com/zeebo/blake3/internal/consts/index.test.ts +46 -0
  380. package/gs/github.com/zeebo/blake3/internal/consts/index.ts +26 -0
  381. package/gs/go/internal/scannerhooks/index.test.ts +14 -0
  382. package/gs/go/internal/scannerhooks/index.ts +9 -0
  383. package/gs/go/scanner/index.test.ts +22 -0
  384. package/gs/go/scanner/index.ts +47 -0
  385. package/gs/go/token/index.test.ts +47 -1
  386. package/gs/go/token/index.ts +583 -4
  387. package/gs/hash/fnv/index.test.ts +67 -0
  388. package/gs/hash/fnv/index.ts +351 -0
  389. package/gs/hash/fnv/meta.json +3 -0
  390. package/gs/hash/index.test.ts +37 -0
  391. package/gs/hash/index.ts +118 -0
  392. package/gs/hash/meta.json +5 -0
  393. package/gs/internal/abi/index.test.ts +18 -0
  394. package/gs/internal/abi/index.ts +14 -0
  395. package/gs/internal/bytealg/index.test.ts +18 -0
  396. package/gs/internal/bytealg/index.ts +16 -0
  397. package/gs/internal/byteorder/index.test.ts +39 -0
  398. package/gs/internal/byteorder/index.ts +100 -27
  399. package/gs/internal/godebug/index.test.ts +16 -0
  400. package/gs/internal/godebug/index.ts +35 -0
  401. package/gs/io/fs/index.ts +1 -0
  402. package/gs/io/fs/meta.json +5 -0
  403. package/gs/io/fs/readlink.test.ts +43 -0
  404. package/gs/io/fs/readlink.ts +69 -0
  405. package/gs/io/fs/walk.test.ts +61 -0
  406. package/gs/io/fs/walk.ts +17 -9
  407. package/gs/io/io.ts +177 -31
  408. package/gs/io/meta.json +10 -2
  409. package/gs/iter/iter.ts +8 -2
  410. package/gs/maps/iter.ts +75 -26
  411. package/gs/maps/maps.test.ts +23 -0
  412. package/gs/maps/maps.ts +13 -11
  413. package/gs/math/bits/index.test.ts +20 -0
  414. package/gs/math/bits/index.ts +107 -64
  415. package/gs/math/const.gs.test.ts +11 -5
  416. package/gs/math/const.gs.ts +5 -6
  417. package/gs/mime/index.ts +60 -0
  418. package/gs/net/http/httptest/index.test.ts +53 -0
  419. package/gs/net/http/httptest/index.ts +120 -0
  420. package/gs/net/http/index.test.ts +148 -0
  421. package/gs/net/http/index.ts +432 -0
  422. package/gs/net/http/meta.json +6 -0
  423. package/gs/net/http/pprof/index.test.ts +47 -0
  424. package/gs/net/http/pprof/index.ts +65 -0
  425. package/gs/os/dir_unix.gs.ts +2 -3
  426. package/gs/os/error.gs.ts +9 -10
  427. package/gs/os/error.test.ts +41 -0
  428. package/gs/os/file_unix_js.test.ts +55 -0
  429. package/gs/os/tempfile.gs.test.ts +37 -10
  430. package/gs/os/types_js.gs.ts +96 -17
  431. package/gs/path/filepath/match.test.ts +31 -12
  432. package/gs/path/filepath/match.ts +181 -3
  433. package/gs/path/filepath/meta.json +6 -0
  434. package/gs/path/filepath/path.test.ts +80 -0
  435. package/gs/path/filepath/path.ts +244 -11
  436. package/gs/path/path.ts +20 -5
  437. package/gs/reflect/field.test.ts +63 -0
  438. package/gs/reflect/index.ts +5 -1
  439. package/gs/reflect/iter.ts +2 -2
  440. package/gs/reflect/map.test.ts +42 -1
  441. package/gs/reflect/map.ts +39 -0
  442. package/gs/reflect/type.ts +728 -65
  443. package/gs/reflect/typefor.test.ts +100 -0
  444. package/gs/reflect/types.ts +3 -1
  445. package/gs/reflect/value.ts +50 -1
  446. package/gs/reflect/visiblefields.ts +1 -1
  447. package/gs/runtime/debug/index.test.ts +45 -0
  448. package/gs/runtime/debug/index.ts +96 -0
  449. package/gs/runtime/pprof/index.test.ts +36 -0
  450. package/gs/runtime/pprof/index.ts +104 -0
  451. package/gs/runtime/pprof/meta.json +6 -0
  452. package/gs/runtime/runtime.test.ts +19 -0
  453. package/gs/runtime/runtime.ts +98 -3
  454. package/gs/runtime/trace/index.test.ts +45 -0
  455. package/gs/runtime/trace/index.ts +97 -0
  456. package/gs/runtime/trace/meta.json +7 -0
  457. package/gs/slices/meta.json +2 -1
  458. package/gs/slices/slices.test.ts +180 -0
  459. package/gs/slices/slices.ts +502 -15
  460. package/gs/sort/meta.json +7 -0
  461. package/gs/sort/slice.gs.ts +85 -26
  462. package/gs/sort/slice.test.ts +40 -0
  463. package/gs/sort/sort.gs.ts +16 -13
  464. package/gs/strings/builder.test.ts +8 -0
  465. package/gs/strings/builder.ts +33 -20
  466. package/gs/strings/iter.test.ts +5 -7
  467. package/gs/strings/iter.ts +146 -71
  468. package/gs/strings/replace.test.ts +1 -4
  469. package/gs/strings/replace.ts +6 -6
  470. package/gs/strings/strings.test.ts +4 -0
  471. package/gs/strings/strings.ts +54 -6
  472. package/gs/sync/atomic/type.gs.ts +13 -14
  473. package/gs/sync/meta.json +3 -1
  474. package/gs/sync/sync.test.ts +69 -1
  475. package/gs/sync/sync.ts +72 -13
  476. package/gs/syscall/constants.ts +39 -24
  477. package/gs/syscall/errors.ts +165 -112
  478. package/gs/syscall/fs.ts +195 -0
  479. package/gs/syscall/js/index.ts +485 -0
  480. package/gs/syscall/js/meta.json +4 -0
  481. package/gs/syscall/net.test.ts +111 -0
  482. package/gs/syscall/types.ts +63 -2
  483. package/gs/testing/index.ts +1 -0
  484. package/gs/testing/meta.json +5 -0
  485. package/gs/testing/testing.test.ts +146 -0
  486. package/gs/testing/testing.ts +399 -0
  487. package/gs/time/meta.json +2 -2
  488. package/gs/time/time.test.ts +110 -0
  489. package/gs/time/time.ts +309 -57
  490. package/gs/unicode/unicode.test.ts +36 -0
  491. package/gs/unicode/unicode.ts +115 -9
  492. package/gs/unicode/utf8/utf8.test.ts +13 -0
  493. package/gs/unicode/utf8/utf8.ts +28 -16
  494. package/gs/unique/index.ts +98 -0
  495. package/package.json +3 -2
@@ -0,0 +1,1084 @@
1
+ import * as $ from '../../../../builtin/index.js'
2
+ import * as time from '../../../../time/index.js'
3
+
4
+ export interface FieldMask {
5
+ GetPaths(): $.Slice<string>
6
+ }
7
+
8
+ export interface Marshaler {
9
+ MarshalProtoJSON(s: MarshalState | null): void
10
+ }
11
+
12
+ export interface Unmarshaler {
13
+ UnmarshalProtoJSON(s: UnmarshalState | null): void
14
+ }
15
+
16
+ type JSONRecord = Record<string, unknown>
17
+
18
+ export class JsonStream {
19
+ private chunks: string[] = []
20
+ private err: $.GoError = null
21
+
22
+ constructor(_writer?: unknown) {}
23
+
24
+ public Error(): $.GoError {
25
+ return this.err
26
+ }
27
+
28
+ public Write(p: $.Slice<number>): [number, $.GoError] {
29
+ this.chunks.push(bytesToString(p))
30
+ return [$.len(p), null]
31
+ }
32
+
33
+ public WriteArrayEnd(): void {
34
+ this.chunks.push(']')
35
+ }
36
+
37
+ public WriteArrayStart(): void {
38
+ this.chunks.push('[')
39
+ }
40
+
41
+ public WriteBool(b: boolean): void {
42
+ this.chunks.push(JSON.stringify(b))
43
+ }
44
+
45
+ public WriteFloat32(f: number): void {
46
+ this.WriteFloat64(f)
47
+ }
48
+
49
+ public WriteFloat64(f: number): void {
50
+ if (Number.isNaN(f)) {
51
+ this.WriteString('NaN')
52
+ return
53
+ }
54
+ if (f === Infinity) {
55
+ this.WriteString('Infinity')
56
+ return
57
+ }
58
+ if (f === -Infinity) {
59
+ this.WriteString('-Infinity')
60
+ return
61
+ }
62
+ this.chunks.push(JSON.stringify(f))
63
+ }
64
+
65
+ public WriteInt32(i: number): void {
66
+ this.chunks.push(JSON.stringify(i))
67
+ }
68
+
69
+ public WriteMore(): void {
70
+ this.chunks.push(',')
71
+ }
72
+
73
+ public WriteNil(): void {
74
+ this.chunks.push('null')
75
+ }
76
+
77
+ public WriteObjectEnd(): void {
78
+ this.chunks.push('}')
79
+ }
80
+
81
+ public WriteObjectField(field: string): void {
82
+ this.WriteString(field)
83
+ this.chunks.push(':')
84
+ }
85
+
86
+ public WriteObjectStart(): void {
87
+ this.chunks.push('{')
88
+ }
89
+
90
+ public WriteString(str: string): void {
91
+ this.chunks.push(JSON.stringify(str))
92
+ }
93
+
94
+ public WriteUint32(u: number): void {
95
+ this.chunks.push(JSON.stringify(u))
96
+ }
97
+
98
+ public String(): string {
99
+ return this.chunks.join('')
100
+ }
101
+ }
102
+
103
+ export class MarshalerConfig {
104
+ public EnumsAsInts = true
105
+ public AnyTypeResolver: unknown = null
106
+
107
+ constructor(init?: Partial<MarshalerConfig>) {
108
+ Object.assign(this, init)
109
+ }
110
+
111
+ public clone(): MarshalerConfig {
112
+ return new MarshalerConfig({
113
+ EnumsAsInts: this.EnumsAsInts,
114
+ AnyTypeResolver: this.AnyTypeResolver,
115
+ })
116
+ }
117
+
118
+ public Marshal(m: Marshaler | null): [$.Slice<number>, $.GoError] {
119
+ const stream = new JsonStream()
120
+ const state = new MarshalState({ config: this, stream })
121
+ if (m == null) {
122
+ state.WriteNil()
123
+ } else {
124
+ m.MarshalProtoJSON(state)
125
+ }
126
+ if (state.Err() != null) {
127
+ return [null, state.Err()]
128
+ }
129
+ return [stringToBytes(stream.String()), null]
130
+ }
131
+ }
132
+
133
+ export class UnmarshalerConfig {
134
+ public AnyTypeResolver: unknown = null
135
+
136
+ constructor(init?: Partial<UnmarshalerConfig>) {
137
+ Object.assign(this, init)
138
+ }
139
+
140
+ public clone(): UnmarshalerConfig {
141
+ return new UnmarshalerConfig({ AnyTypeResolver: this.AnyTypeResolver })
142
+ }
143
+
144
+ public Unmarshal(data: $.Slice<number>, m: Unmarshaler | null): $.GoError {
145
+ const state = NewUnmarshalState(data, this)
146
+ if (state == null) {
147
+ return $.newError('json: failed to allocate unmarshal state')
148
+ }
149
+ m?.UnmarshalProtoJSON(state)
150
+ return state.Err()
151
+ }
152
+ }
153
+
154
+ export const DefaultMarshalerConfig = new MarshalerConfig()
155
+ export const DefaultUnmarshalerConfig = new UnmarshalerConfig()
156
+
157
+ export class MarshalState {
158
+ private configValue: MarshalerConfig
159
+ private stream: JsonStream
160
+ private err: $.GoError = null
161
+ private fields = new Set<string>()
162
+
163
+ constructor(init?: Partial<{ config: MarshalerConfig; stream: JsonStream }>) {
164
+ this.configValue = init?.config?.clone() ?? DefaultMarshalerConfig.clone()
165
+ this.stream = init?.stream ?? new JsonStream()
166
+ }
167
+
168
+ public AnyTypeResolver(): unknown {
169
+ return this.configValue.AnyTypeResolver
170
+ }
171
+
172
+ public Config(): MarshalerConfig {
173
+ return this.configValue.clone()
174
+ }
175
+
176
+ public Err(): $.GoError {
177
+ return this.err ?? this.stream.Error()
178
+ }
179
+
180
+ public HasField(field: string): boolean {
181
+ return this.fields.has(field)
182
+ }
183
+
184
+ public SetError(err: $.GoError): void {
185
+ this.err = err
186
+ }
187
+
188
+ public SetErrorf(format: string, ...args: unknown[]): void {
189
+ this.err = $.newError(formatError(format, args))
190
+ }
191
+
192
+ public Sub(js: JsonStream | null): MarshalState | null {
193
+ return new MarshalState({
194
+ config: this.configValue,
195
+ stream: js ?? new JsonStream(),
196
+ })
197
+ }
198
+
199
+ public WithField(field: string): MarshalState | null {
200
+ const next = new MarshalState({
201
+ config: this.configValue,
202
+ stream: this.stream,
203
+ })
204
+ next.fields = new Set(this.fields)
205
+ next.fields.add(field)
206
+ return next
207
+ }
208
+
209
+ public WithFieldMask(...paths: string[]): MarshalState | null {
210
+ const next = new MarshalState({
211
+ config: this.configValue,
212
+ stream: this.stream,
213
+ })
214
+ next.fields = new Set([...this.fields, ...paths])
215
+ return next
216
+ }
217
+
218
+ public Write(v: $.Slice<number>): [number, $.GoError] {
219
+ return this.stream.Write(v)
220
+ }
221
+
222
+ public WriteArrayEnd(): void {
223
+ this.stream.WriteArrayEnd()
224
+ }
225
+
226
+ public WriteArrayStart(): void {
227
+ this.stream.WriteArrayStart()
228
+ }
229
+
230
+ public WriteBool(v: boolean): void {
231
+ this.stream.WriteBool(v)
232
+ }
233
+
234
+ public WriteBoolArray(vs: $.Slice<boolean>): void {
235
+ this.writeArray(vs, (v) => this.WriteBool(v))
236
+ }
237
+
238
+ public WriteBytes(v: $.Slice<number>): void {
239
+ this.WriteString(base64Encode(v))
240
+ }
241
+
242
+ public WriteBytesArray(vs: $.Slice<$.Slice<number>>): void {
243
+ this.writeArray(vs, (v) => this.WriteBytes(v))
244
+ }
245
+
246
+ public WriteDuration(x: number): void {
247
+ this.WriteString(`${x}s`)
248
+ }
249
+
250
+ public WriteEnum(
251
+ x: number,
252
+ ...valueMaps: Array<Map<number, string> | Record<number, string> | null>
253
+ ): void {
254
+ if (this.configValue.EnumsAsInts) {
255
+ this.WriteEnumNumber(x)
256
+ return
257
+ }
258
+ this.WriteEnumString(x, ...valueMaps)
259
+ }
260
+
261
+ public WriteEnumNumber(x: number): void {
262
+ this.WriteInt32(x)
263
+ }
264
+
265
+ public WriteEnumString(
266
+ x: number,
267
+ ...valueMaps: Array<Map<number, string> | Record<number, string> | null>
268
+ ): void {
269
+ const value = enumStringValue(x, valueMaps)
270
+ if (value == null) {
271
+ this.WriteEnumNumber(x)
272
+ return
273
+ }
274
+ this.WriteString(value)
275
+ }
276
+
277
+ public WriteFieldMask(x: FieldMask | null): void {
278
+ this.WriteString(sliceValues(x?.GetPaths()).join(','))
279
+ }
280
+
281
+ public WriteFloat32(v: number): void {
282
+ this.stream.WriteFloat32(v)
283
+ }
284
+
285
+ public WriteFloat32Array(vs: $.Slice<number>): void {
286
+ this.writeArray(vs, (v) => this.WriteFloat32(v))
287
+ }
288
+
289
+ public WriteFloat64(v: number): void {
290
+ this.stream.WriteFloat64(v)
291
+ }
292
+
293
+ public WriteFloat64Array(vs: $.Slice<number>): void {
294
+ this.writeArray(vs, (v) => this.WriteFloat64(v))
295
+ }
296
+
297
+ public WriteInt32(v: number): void {
298
+ this.stream.WriteInt32(v)
299
+ }
300
+
301
+ public WriteInt32Array(vs: $.Slice<number>): void {
302
+ this.writeArray(vs, (v) => this.WriteInt32(v))
303
+ }
304
+
305
+ public WriteInt64(v: number): void {
306
+ this.WriteString(String(Math.trunc(v)))
307
+ }
308
+
309
+ public WriteInt64Array(vs: $.Slice<number>): void {
310
+ this.writeArray(vs, (v) => this.WriteInt64(v))
311
+ }
312
+
313
+ public WriteLegacyFieldMask(x: FieldMask | null): void {
314
+ this.WriteFieldMask(x)
315
+ }
316
+
317
+ public WriteMore(): void {
318
+ this.stream.WriteMore()
319
+ }
320
+
321
+ public WriteMoreIf(b: $.VarRef<boolean> | boolean): void {
322
+ const value = typeof b === 'boolean' ? b : b.value
323
+ if (value) {
324
+ this.WriteMore()
325
+ return
326
+ }
327
+ if (typeof b !== 'boolean') {
328
+ b.value = true
329
+ }
330
+ }
331
+
332
+ public WriteNil(): void {
333
+ this.stream.WriteNil()
334
+ }
335
+
336
+ public WriteObjectBoolField(field: boolean): void {
337
+ this.WriteObjectField(String(field))
338
+ }
339
+
340
+ public WriteObjectEnd(): void {
341
+ this.stream.WriteObjectEnd()
342
+ }
343
+
344
+ public WriteObjectField(field: string): void {
345
+ this.stream.WriteObjectField(field)
346
+ }
347
+
348
+ public WriteObjectInt32Field(field: number): void {
349
+ this.WriteObjectField(String(Math.trunc(field)))
350
+ }
351
+
352
+ public WriteObjectInt64Field(field: number): void {
353
+ this.WriteObjectField(String(Math.trunc(field)))
354
+ }
355
+
356
+ public WriteObjectStart(): void {
357
+ this.stream.WriteObjectStart()
358
+ }
359
+
360
+ public WriteObjectStringField(field: string): void {
361
+ this.WriteObjectField(field)
362
+ }
363
+
364
+ public WriteObjectUint32Field(field: number): void {
365
+ this.WriteObjectField(String(Math.trunc(field)))
366
+ }
367
+
368
+ public WriteObjectUint64Field(field: number): void {
369
+ this.WriteObjectField(String(Math.trunc(field)))
370
+ }
371
+
372
+ public WriteString(v: string): void {
373
+ this.stream.WriteString(v)
374
+ }
375
+
376
+ public WriteStringArray(vs: $.Slice<string>): void {
377
+ this.writeArray(vs, (v) => this.WriteString(v))
378
+ }
379
+
380
+ public WriteTime(x: time.Time | $.VarRef<time.Time> | null): void {
381
+ this.WriteString($.pointerValue<time.Time>(x).Format(time.RFC3339Nano))
382
+ }
383
+
384
+ public WriteUint32(v: number): void {
385
+ this.stream.WriteUint32(v)
386
+ }
387
+
388
+ public WriteUint32Array(vs: $.Slice<number>): void {
389
+ this.writeArray(vs, (v) => this.WriteUint32(v))
390
+ }
391
+
392
+ public WriteUint64(v: number): void {
393
+ this.WriteString(String(Math.trunc(v)))
394
+ }
395
+
396
+ public WriteUint64Array(vs: $.Slice<number>): void {
397
+ this.writeArray(vs, (v) => this.WriteUint64(v))
398
+ }
399
+
400
+ private writeArray<T>(
401
+ values: $.Slice<T>,
402
+ writeValue: (value: T) => void,
403
+ ): void {
404
+ this.WriteArrayStart()
405
+ let wrote = false
406
+ for (const value of sliceValues(values)) {
407
+ if (wrote) {
408
+ this.WriteMore()
409
+ }
410
+ wrote = true
411
+ writeValue(value)
412
+ }
413
+ this.WriteArrayEnd()
414
+ }
415
+ }
416
+
417
+ export class UnmarshalState {
418
+ private configValue: UnmarshalerConfig
419
+ private value: unknown
420
+ private root: UnmarshalState
421
+ private err: $.GoError = null
422
+ private fieldMaskPaths: Set<string>
423
+ private path: string[]
424
+ private objectEntries: Array<[string, unknown]> | null = null
425
+ private objectIndex = 0
426
+
427
+ constructor(
428
+ init?: Partial<{
429
+ config: UnmarshalerConfig
430
+ value: unknown
431
+ root: UnmarshalState
432
+ fieldMaskPaths: Set<string>
433
+ path: string[]
434
+ }>,
435
+ ) {
436
+ this.configValue = init?.config?.clone() ?? DefaultUnmarshalerConfig.clone()
437
+ this.value = init?.value ?? null
438
+ this.root = init?.root ?? this
439
+ this.fieldMaskPaths = init?.fieldMaskPaths ?? new Set<string>()
440
+ this.path = init?.path ?? []
441
+ }
442
+
443
+ public AddField(field: string): void {
444
+ this.fieldMaskPaths.add([...this.path, field].join('.'))
445
+ }
446
+
447
+ public AnyTypeResolver(): unknown {
448
+ return this.configValue.AnyTypeResolver
449
+ }
450
+
451
+ public Config(): UnmarshalerConfig {
452
+ return this.configValue.clone()
453
+ }
454
+
455
+ public Err(): $.GoError {
456
+ return this.root.err
457
+ }
458
+
459
+ public FieldMask(): FieldMask | null {
460
+ const paths = Array.from(this.fieldMaskPaths)
461
+ return { GetPaths: () => $.arrayToSlice(paths) }
462
+ }
463
+
464
+ public ReadArray(cb: (() => void) | null): void {
465
+ if (!Array.isArray(this.value)) {
466
+ this.SetErrorf('expected JSON array')
467
+ return
468
+ }
469
+ const original = this.value
470
+ for (const item of this.value) {
471
+ this.value = item
472
+ cb?.()
473
+ if (this.Err() != null) {
474
+ break
475
+ }
476
+ }
477
+ this.value = original
478
+ }
479
+
480
+ public ReadBool(): boolean {
481
+ return this.value === true
482
+ }
483
+
484
+ public ReadBoolArray(): $.Slice<boolean> {
485
+ return this.readArrayValues(() => this.ReadBool())
486
+ }
487
+
488
+ public ReadBoolMap(cb: ((key: boolean) => void) | null): void {
489
+ this.readMapKeys((key) => {
490
+ const parsed = parseBoolMapKey(key)
491
+ if (parsed == null) {
492
+ this.SetErrorf('invalid map key %q for bool map', key)
493
+ return
494
+ }
495
+ cb?.(parsed)
496
+ })
497
+ }
498
+
499
+ public ReadBytes(): $.Slice<number> {
500
+ return base64Decode(this.ReadString())
501
+ }
502
+
503
+ public ReadBytesArray(): $.Slice<$.Slice<number>> {
504
+ return this.readArrayValues(() => this.ReadBytes())
505
+ }
506
+
507
+ public ReadDuration(): $.VarRef<number> | null {
508
+ return $.varRef(numberFromJSON(this.value))
509
+ }
510
+
511
+ public ReadEnum(
512
+ ...valueMaps: Array<Map<string, number> | Record<string, number> | null>
513
+ ): number {
514
+ if (typeof this.value === 'number') {
515
+ return Math.trunc(this.value)
516
+ }
517
+ if (typeof this.value === 'string') {
518
+ for (const valueMap of valueMaps) {
519
+ const mapped = lookupStringMap(valueMap, this.value)
520
+ if (mapped !== undefined) {
521
+ return mapped
522
+ }
523
+ }
524
+ return Math.trunc(Number(this.value) || 0)
525
+ }
526
+ return 0
527
+ }
528
+
529
+ public ReadFieldMask(): FieldMask | null {
530
+ const paths =
531
+ typeof this.value === 'string' ?
532
+ this.value.split(',').filter((part) => part !== '')
533
+ : Array.isArray(recordValue(this.value)?.paths) ?
534
+ sliceValues(recordValue(this.value)?.paths as $.Slice<string>)
535
+ : []
536
+ return { GetPaths: () => $.arrayToSlice(paths) }
537
+ }
538
+
539
+ public ReadFloat32(): number {
540
+ return numberFromJSON(this.value)
541
+ }
542
+
543
+ public ReadFloat32Array(): $.Slice<number> {
544
+ return this.readArrayValues(() => this.ReadFloat32())
545
+ }
546
+
547
+ public ReadFloat64(): number {
548
+ return numberFromJSON(this.value)
549
+ }
550
+
551
+ public ReadFloat64Array(): $.Slice<number> {
552
+ return this.readArrayValues(() => this.ReadFloat64())
553
+ }
554
+
555
+ public ReadInt32(): number {
556
+ return Math.trunc(numberFromJSON(this.value))
557
+ }
558
+
559
+ public ReadInt32Array(): $.Slice<number> {
560
+ return this.readArrayValues(() => this.ReadInt32())
561
+ }
562
+
563
+ public ReadInt32Map(cb: ((key: number) => void) | null): void {
564
+ this.readMapKeys((key) => {
565
+ const parsed = parseSignedMapKey(key, 32)
566
+ if (parsed == null) {
567
+ this.SetErrorf('invalid map key %q for int32 map', key)
568
+ return
569
+ }
570
+ cb?.(parsed)
571
+ })
572
+ }
573
+
574
+ public ReadInt64(): number {
575
+ return Math.trunc(numberFromJSON(this.value))
576
+ }
577
+
578
+ public ReadInt64Array(): $.Slice<number> {
579
+ return this.readArrayValues(() => this.ReadInt64())
580
+ }
581
+
582
+ public ReadInt64Map(cb: ((key: number) => void) | null): void {
583
+ this.readMapKeys((key) => {
584
+ const parsed = parseSignedMapKey(key, 64)
585
+ if (parsed == null) {
586
+ this.SetErrorf('invalid map key %q for int64 map', key)
587
+ return
588
+ }
589
+ cb?.(parsed)
590
+ })
591
+ }
592
+
593
+ public ReadNil(): boolean {
594
+ return this.value === null
595
+ }
596
+
597
+ public ReadObject(cb: ((key: string) => void) | null): void {
598
+ const record = recordValue(this.value)
599
+ if (record == null) {
600
+ this.SetErrorf('expected JSON object')
601
+ return
602
+ }
603
+ const original = this.value
604
+ for (const key of Object.keys(record)) {
605
+ this.value = record[key]
606
+ cb?.(key)
607
+ if (this.Err() != null) {
608
+ break
609
+ }
610
+ }
611
+ this.value = original
612
+ }
613
+
614
+ public ReadObjectField(): string {
615
+ const record = recordValue(this.value)
616
+ if (record == null) {
617
+ this.SetErrorf('expected JSON object')
618
+ return ''
619
+ }
620
+ if (this.objectEntries == null) {
621
+ this.objectEntries = Object.entries(record)
622
+ this.objectIndex = 0
623
+ }
624
+ if (this.objectIndex >= this.objectEntries.length) {
625
+ return ''
626
+ }
627
+ const [key, value] = this.objectEntries[this.objectIndex]
628
+ this.objectIndex++
629
+ this.value = value
630
+ return key
631
+ }
632
+
633
+ public ReadString(): string {
634
+ if (typeof this.value === 'string') {
635
+ return this.value
636
+ }
637
+ if (this.value == null) {
638
+ return ''
639
+ }
640
+ return String(this.value)
641
+ }
642
+
643
+ public ReadStringArray(): $.Slice<string> {
644
+ return this.readArrayValues(() => this.ReadString())
645
+ }
646
+
647
+ public ReadStringMap(cb: ((key: string) => void) | null): void {
648
+ this.readMapKeys((key) => cb?.(key))
649
+ }
650
+
651
+ public ReadTime(): $.VarRef<time.Time> | null {
652
+ const [parsed, err] = time.Parse(time.RFC3339, this.ReadString())
653
+ if (err != null) {
654
+ this.SetError(err)
655
+ }
656
+ return $.varRef($.markAsStructValue($.cloneStructValue(parsed)))
657
+ }
658
+
659
+ public ReadUint32(): number {
660
+ return Math.trunc(Math.max(0, numberFromJSON(this.value)))
661
+ }
662
+
663
+ public ReadUint32Array(): $.Slice<number> {
664
+ return this.readArrayValues(() => this.ReadUint32())
665
+ }
666
+
667
+ public ReadUint32Map(cb: ((key: number) => void) | null): void {
668
+ this.readMapKeys((key) => {
669
+ const parsed = parseUnsignedMapKey(key, 32)
670
+ if (parsed == null) {
671
+ this.SetErrorf('invalid map key %q for uint32 map', key)
672
+ return
673
+ }
674
+ cb?.(parsed)
675
+ })
676
+ }
677
+
678
+ public ReadUint64(): number {
679
+ return this.ReadUint32()
680
+ }
681
+
682
+ public ReadUint64Array(): $.Slice<number> {
683
+ return this.readArrayValues(() => this.ReadUint64())
684
+ }
685
+
686
+ public ReadUint64Map(cb: ((key: number) => void) | null): void {
687
+ this.readMapKeys((key) => {
688
+ const parsed = parseUnsignedMapKey(key, 64)
689
+ if (parsed == null) {
690
+ this.SetErrorf('invalid map key %q for uint64 map', key)
691
+ return
692
+ }
693
+ cb?.(parsed)
694
+ })
695
+ }
696
+
697
+ public ReadWrappedBool(): boolean {
698
+ return this.readWrapped(() => this.ReadBool())
699
+ }
700
+ public ReadWrappedBytes(): $.Slice<number> {
701
+ return this.readWrapped(() => this.ReadBytes())
702
+ }
703
+ public ReadWrappedFloat32(): number {
704
+ return this.readWrapped(() => this.ReadFloat32())
705
+ }
706
+ public ReadWrappedFloat64(): number {
707
+ return this.readWrapped(() => this.ReadFloat64())
708
+ }
709
+ public ReadWrappedInt32(): number {
710
+ return this.readWrapped(() => this.ReadInt32())
711
+ }
712
+ public ReadWrappedInt64(): number {
713
+ return this.readWrapped(() => this.ReadInt64())
714
+ }
715
+ public ReadWrappedString(): string {
716
+ return this.readWrapped(() => this.ReadString())
717
+ }
718
+ public ReadWrappedUint32(): number {
719
+ return this.readWrapped(() => this.ReadUint32())
720
+ }
721
+ public ReadWrappedUint64(): number {
722
+ return this.readWrapped(() => this.ReadUint64())
723
+ }
724
+
725
+ public SetError(err: $.GoError): void {
726
+ this.root.err = err
727
+ }
728
+
729
+ public SetErrorf(format: string, ...args: unknown[]): void {
730
+ this.SetError($.newError(formatError(format, args)))
731
+ }
732
+
733
+ public Skip(): void {}
734
+
735
+ public SkipAndAppendBytes(p: $.Slice<number>): $.Slice<number> {
736
+ const encoded = new TextEncoder().encode(JSON.stringify(this.value))
737
+ return $.append(p, encoded)
738
+ }
739
+
740
+ public SkipAndReturnBytes(): $.Slice<number> {
741
+ return stringToBytes(JSON.stringify(this.value))
742
+ }
743
+
744
+ public Sub(data: $.Slice<number>): UnmarshalState | null {
745
+ return NewUnmarshalState(data, this.configValue)
746
+ }
747
+
748
+ public WhatIsNext(): number {
749
+ if (this.value === null) {
750
+ return 0
751
+ }
752
+ if (Array.isArray(this.value)) {
753
+ return 1
754
+ }
755
+ if (typeof this.value === 'object') {
756
+ return 2
757
+ }
758
+ if (typeof this.value === 'string') {
759
+ return 3
760
+ }
761
+ if (typeof this.value === 'number') {
762
+ return 4
763
+ }
764
+ if (typeof this.value === 'boolean') {
765
+ return 5
766
+ }
767
+ return 0
768
+ }
769
+
770
+ public WithField(
771
+ _field: string,
772
+ _mask: boolean = true,
773
+ ): UnmarshalState | null {
774
+ return new UnmarshalState({
775
+ config: this.configValue,
776
+ value: this.value,
777
+ root: this.root,
778
+ fieldMaskPaths: _mask ? this.fieldMaskPaths : new Set<string>(),
779
+ path: [...this.path, _field],
780
+ })
781
+ }
782
+
783
+ private readArrayValues<T>(read: () => T): $.Slice<T> {
784
+ const values: T[] = []
785
+ this.ReadArray(() => {
786
+ values.push(read())
787
+ })
788
+ return $.arrayToSlice(values)
789
+ }
790
+
791
+ private readMapKeys(cb: (key: string) => void): void {
792
+ const record = recordValue(this.value)
793
+ if (record == null) {
794
+ this.SetErrorf('expected JSON object')
795
+ return
796
+ }
797
+ const original = this.value
798
+ for (const key of Object.keys(record)) {
799
+ this.value = record[key]
800
+ cb(key)
801
+ if (this.Err() != null) {
802
+ break
803
+ }
804
+ }
805
+ this.value = original
806
+ }
807
+
808
+ private readWrapped<T>(read: () => T): T {
809
+ const record = recordValue(this.value)
810
+ if (record == null) {
811
+ return read()
812
+ }
813
+ if (!Object.hasOwn(record, 'value')) {
814
+ this.SetErrorf('first field in wrapped value is not value')
815
+ return read()
816
+ }
817
+ const keys = Object.keys(record)
818
+ if (keys.length !== 1) {
819
+ this.SetErrorf(
820
+ 'unexpected %q field in wrapped value',
821
+ keys.find((key) => key !== 'value') ?? '',
822
+ )
823
+ return read()
824
+ }
825
+ const original = this.value
826
+ this.value = record.value
827
+ const value = read()
828
+ this.value = original
829
+ return value
830
+ }
831
+ }
832
+
833
+ export function NewJsonStream(wr: unknown): JsonStream | null {
834
+ return new JsonStream(wr)
835
+ }
836
+
837
+ export function NewMarshalState(
838
+ config: MarshalerConfig,
839
+ stream: JsonStream | null,
840
+ ): MarshalState | null {
841
+ return new MarshalState({ config, stream: stream ?? new JsonStream() })
842
+ }
843
+
844
+ export function NewUnmarshalState(
845
+ data: $.Slice<number>,
846
+ config: UnmarshalerConfig,
847
+ ): UnmarshalState | null {
848
+ try {
849
+ return new UnmarshalState({
850
+ config,
851
+ value: JSON.parse(bytesToString(data)),
852
+ })
853
+ } catch (err) {
854
+ const state = new UnmarshalState({ config })
855
+ state.SetError($.newError(err instanceof Error ? err.message : String(err)))
856
+ return state
857
+ }
858
+ }
859
+
860
+ export function Marshal(
861
+ c: MarshalerConfig,
862
+ m: Marshaler | null,
863
+ ): [$.Slice<number>, $.GoError] {
864
+ return c.Marshal(m)
865
+ }
866
+
867
+ export function MarshalMap<
868
+ M extends Map<string, Marshaler> | Record<string, Marshaler>,
869
+ >(
870
+ _typeArgs: $.GenericTypeArgs | undefined,
871
+ c: MarshalerConfig,
872
+ mm: M,
873
+ ): [$.Slice<number>, $.GoError] {
874
+ const stream = new JsonStream()
875
+ const state = new MarshalState({ config: c, stream })
876
+ state.WriteObjectStart()
877
+ const keys = mm instanceof Map ? Array.from(mm.keys()) : Object.keys(mm)
878
+ keys.sort()
879
+ keys.forEach((key, idx) => {
880
+ if (idx !== 0) {
881
+ state.WriteMore()
882
+ }
883
+ state.WriteObjectField(key)
884
+ const value = mm instanceof Map ? mm.get(key) : mm[key]
885
+ value?.MarshalProtoJSON(state)
886
+ })
887
+ state.WriteObjectEnd()
888
+ return state.Err() == null ?
889
+ [stringToBytes(stream.String()), null]
890
+ : [null, state.Err()]
891
+ }
892
+
893
+ export function MarshalSlice<S extends $.Slice<Marshaler>>(
894
+ _typeArgs: $.GenericTypeArgs | undefined,
895
+ c: MarshalerConfig,
896
+ ms: S,
897
+ ): [$.Slice<number>, $.GoError] {
898
+ const stream = new JsonStream()
899
+ const state = new MarshalState({ config: c, stream })
900
+ state.WriteArrayStart()
901
+ sliceValues(ms).forEach((value, idx) => {
902
+ if (idx !== 0) {
903
+ state.WriteMore()
904
+ }
905
+ value?.MarshalProtoJSON(state)
906
+ })
907
+ state.WriteArrayEnd()
908
+ return state.Err() == null ?
909
+ [stringToBytes(stream.String()), null]
910
+ : [null, state.Err()]
911
+ }
912
+
913
+ export function GetEnumString(
914
+ x: number,
915
+ ...valueMaps: Array<Map<number, string> | Record<number, string> | null>
916
+ ): string {
917
+ return enumStringValue(x, valueMaps) ?? String(x)
918
+ }
919
+
920
+ export function ParseEnumString(
921
+ v: string,
922
+ ...valueMaps: Array<Map<string, number> | Record<string, number> | null>
923
+ ): [number, $.GoError] {
924
+ for (const valueMap of valueMaps) {
925
+ const value = lookupStringMap(valueMap, v)
926
+ if (value !== undefined) {
927
+ return [value, null]
928
+ }
929
+ }
930
+ const parsed = Number(v)
931
+ return Number.isFinite(parsed) ?
932
+ [parsed, null]
933
+ : [0, $.newError(`invalid enum ${v}`)]
934
+ }
935
+
936
+ function sliceValues<T>(
937
+ values: $.Slice<T> | readonly T[] | null | undefined,
938
+ ): T[] {
939
+ return Array.from((values ?? []) as Iterable<T>)
940
+ }
941
+
942
+ function bytesToString(bytes: $.Slice<number>): string {
943
+ return new TextDecoder().decode(new Uint8Array(sliceValues(bytes)))
944
+ }
945
+
946
+ function stringToBytes(value: string): $.Slice<number> {
947
+ return $.arrayToSlice(Array.from(new TextEncoder().encode(value)))
948
+ }
949
+
950
+ function recordValue(value: unknown): JSONRecord | null {
951
+ if (value == null || Array.isArray(value) || typeof value !== 'object') {
952
+ return null
953
+ }
954
+ return value as JSONRecord
955
+ }
956
+
957
+ function numberFromJSON(value: unknown): number {
958
+ if (typeof value === 'number') {
959
+ return value
960
+ }
961
+ if (typeof value === 'string') {
962
+ switch (value) {
963
+ case 'NaN':
964
+ return NaN
965
+ case 'Infinity':
966
+ return Infinity
967
+ case '-Infinity':
968
+ return -Infinity
969
+ default:
970
+ return Number(value) || 0
971
+ }
972
+ }
973
+ return 0
974
+ }
975
+
976
+ function parseBoolMapKey(key: string): boolean | null {
977
+ switch (key) {
978
+ case 'true':
979
+ return true
980
+ case 'false':
981
+ return false
982
+ default:
983
+ return null
984
+ }
985
+ }
986
+
987
+ function parseSignedMapKey(key: string, bits: 32 | 64): number | null {
988
+ if (!/^-?(0|[1-9]\d*)$/.test(key)) {
989
+ return null
990
+ }
991
+ const parsed = Number(key)
992
+ if (!Number.isSafeInteger(parsed)) {
993
+ return null
994
+ }
995
+ if (bits === 32 && (parsed < -2147483648 || parsed > 2147483647)) {
996
+ return null
997
+ }
998
+ return parsed
999
+ }
1000
+
1001
+ function parseUnsignedMapKey(key: string, bits: 32 | 64): number | null {
1002
+ if (!/^(0|[1-9]\d*)$/.test(key)) {
1003
+ return null
1004
+ }
1005
+ const parsed = Number(key)
1006
+ if (!Number.isSafeInteger(parsed)) {
1007
+ return null
1008
+ }
1009
+ if (bits === 32 && parsed > 4294967295) {
1010
+ return null
1011
+ }
1012
+ return parsed
1013
+ }
1014
+
1015
+ function enumStringValue(
1016
+ x: number,
1017
+ valueMaps: Array<Map<number, string> | Record<number, string> | null>,
1018
+ ): string | null {
1019
+ for (const valueMap of valueMaps) {
1020
+ if (valueMap == null) {
1021
+ continue
1022
+ }
1023
+ const value = valueMap instanceof Map ? valueMap.get(x) : valueMap[x]
1024
+ if (value !== undefined) {
1025
+ return value
1026
+ }
1027
+ }
1028
+ return null
1029
+ }
1030
+
1031
+ function lookupStringMap(
1032
+ valueMap: Map<string, number> | Record<string, number> | null,
1033
+ key: string,
1034
+ ): number | undefined {
1035
+ if (valueMap == null) {
1036
+ return undefined
1037
+ }
1038
+ return valueMap instanceof Map ? valueMap.get(key) : valueMap[key]
1039
+ }
1040
+
1041
+ function formatError(format: string, args: unknown[]): string {
1042
+ return format.replace(/%[vqsd]/g, () => String(args.shift()))
1043
+ }
1044
+
1045
+ const base64Alphabet =
1046
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
1047
+
1048
+ function base64Encode(bytes: $.Slice<number>): string {
1049
+ const data = sliceValues(bytes)
1050
+ let out = ''
1051
+ for (let idx = 0; idx < data.length; idx += 3) {
1052
+ const a = data[idx] ?? 0
1053
+ const b = data[idx + 1] ?? 0
1054
+ const c = data[idx + 2] ?? 0
1055
+ const triple = (a << 16) | (b << 8) | c
1056
+ out += base64Alphabet[(triple >> 18) & 0x3f]
1057
+ out += base64Alphabet[(triple >> 12) & 0x3f]
1058
+ out += idx + 1 < data.length ? base64Alphabet[(triple >> 6) & 0x3f] : '='
1059
+ out += idx + 2 < data.length ? base64Alphabet[triple & 0x3f] : '='
1060
+ }
1061
+ return out
1062
+ }
1063
+
1064
+ function base64Decode(value: string): $.Slice<number> {
1065
+ const normalized = value.replace(/-/g, '+').replace(/_/g, '/')
1066
+ const padded = normalized + '='.repeat((4 - (normalized.length % 4)) % 4)
1067
+ const bytes: number[] = []
1068
+ for (let idx = 0; idx < padded.length; idx += 4) {
1069
+ const chars = padded.slice(idx, idx + 4)
1070
+ const a = base64Alphabet.indexOf(chars[0])
1071
+ const b = base64Alphabet.indexOf(chars[1])
1072
+ const c = chars[2] === '=' ? 0 : base64Alphabet.indexOf(chars[2])
1073
+ const d = chars[3] === '=' ? 0 : base64Alphabet.indexOf(chars[3])
1074
+ const triple = (a << 18) | (b << 12) | (c << 6) | d
1075
+ bytes.push((triple >> 16) & 0xff)
1076
+ if (chars[2] !== '=') {
1077
+ bytes.push((triple >> 8) & 0xff)
1078
+ }
1079
+ if (chars[3] !== '=') {
1080
+ bytes.push(triple & 0xff)
1081
+ }
1082
+ }
1083
+ return $.arrayToSlice(bytes)
1084
+ }