goscript 0.1.1 → 0.1.3

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 (376) 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 +10 -2
  13. package/compiler/lowering.go +2676 -349
  14. package/compiler/override-facts.go +77 -27
  15. package/compiler/override-registry.go +5 -4
  16. package/compiler/override-registry_test.go +178 -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 +2371 -296
  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 +149 -10
  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 +143 -34
  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 +55 -10
  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/bytes.gs.d.ts +7 -5
  58. package/dist/gs/bytes/bytes.gs.js +10 -4
  59. package/dist/gs/bytes/bytes.gs.js.map +1 -1
  60. package/dist/gs/bytes/iter.gs.js +13 -13
  61. package/dist/gs/bytes/iter.gs.js.map +1 -1
  62. package/dist/gs/compress/zlib/index.d.ts +26 -0
  63. package/dist/gs/compress/zlib/index.js +168 -0
  64. package/dist/gs/compress/zlib/index.js.map +1 -0
  65. package/dist/gs/context/context.d.ts +1 -1
  66. package/dist/gs/context/context.js +8 -3
  67. package/dist/gs/context/context.js.map +1 -1
  68. package/dist/gs/crypto/ecdh/index.d.ts +52 -0
  69. package/dist/gs/crypto/ecdh/index.js +226 -0
  70. package/dist/gs/crypto/ecdh/index.js.map +1 -0
  71. package/dist/gs/crypto/ed25519/index.d.ts +34 -0
  72. package/dist/gs/crypto/ed25519/index.js +160 -0
  73. package/dist/gs/crypto/ed25519/index.js.map +1 -0
  74. package/dist/gs/crypto/internal/constanttime/index.d.ts +4 -0
  75. package/dist/gs/crypto/internal/constanttime/index.js +18 -0
  76. package/dist/gs/crypto/internal/constanttime/index.js.map +1 -0
  77. package/dist/gs/crypto/rand/index.d.ts +2 -0
  78. package/dist/gs/crypto/rand/index.js +85 -0
  79. package/dist/gs/crypto/rand/index.js.map +1 -1
  80. package/dist/gs/crypto/sha1/index.d.ts +5 -0
  81. package/dist/gs/crypto/sha1/index.js +106 -0
  82. package/dist/gs/crypto/sha1/index.js.map +1 -0
  83. package/dist/gs/crypto/sha256/index.d.ts +8 -0
  84. package/dist/gs/crypto/sha256/index.js +118 -0
  85. package/dist/gs/crypto/sha256/index.js.map +1 -0
  86. package/dist/gs/crypto/sha512/index.d.ts +14 -0
  87. package/dist/gs/crypto/sha512/index.js +129 -0
  88. package/dist/gs/crypto/sha512/index.js.map +1 -0
  89. package/dist/gs/encoding/json/index.d.ts +3 -0
  90. package/dist/gs/encoding/json/index.js +15 -0
  91. package/dist/gs/encoding/json/index.js.map +1 -1
  92. package/dist/gs/errors/errors.js +29 -6
  93. package/dist/gs/errors/errors.js.map +1 -1
  94. package/dist/gs/fmt/fmt.d.ts +1 -1
  95. package/dist/gs/fmt/fmt.js +64 -3
  96. package/dist/gs/fmt/fmt.js.map +1 -1
  97. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.d.ts +7 -7
  98. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js +52 -18
  99. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/index.js.map +1 -1
  100. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js +56 -20
  101. package/dist/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.js.map +1 -1
  102. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.d.ts +57 -3
  103. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js +366 -1
  104. package/dist/gs/github.com/aperturerobotics/starpc/srpc/index.js.map +1 -1
  105. package/dist/gs/github.com/aperturerobotics/util/conc/index.d.ts +20 -0
  106. package/dist/gs/github.com/aperturerobotics/util/conc/index.js +134 -0
  107. package/dist/gs/github.com/aperturerobotics/util/conc/index.js.map +1 -0
  108. package/dist/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.js.map +1 -1
  109. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.d.ts +3 -0
  110. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js +50 -0
  111. package/dist/gs/github.com/hack-pad/safejs/internal/catch/index.js.map +1 -0
  112. package/dist/gs/github.com/klauspost/compress/internal/le/index.js +3 -2
  113. package/dist/gs/github.com/klauspost/compress/internal/le/index.js.map +1 -1
  114. package/dist/gs/github.com/mr-tron/base58/base58/index.d.ts +27 -0
  115. package/dist/gs/github.com/mr-tron/base58/base58/index.js +172 -0
  116. package/dist/gs/github.com/mr-tron/base58/base58/index.js.map +1 -0
  117. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.d.ts +21 -0
  118. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js +22 -0
  119. package/dist/gs/github.com/zeebo/blake3/internal/consts/index.js.map +1 -0
  120. package/dist/gs/go/token/index.js +11 -4
  121. package/dist/gs/go/token/index.js.map +1 -1
  122. package/dist/gs/hash/fnv/index.d.ts +57 -0
  123. package/dist/gs/hash/fnv/index.js +299 -0
  124. package/dist/gs/hash/fnv/index.js.map +1 -0
  125. package/dist/gs/hash/index.d.ts +17 -0
  126. package/dist/gs/hash/index.js +94 -0
  127. package/dist/gs/hash/index.js.map +1 -0
  128. package/dist/gs/io/fs/readlink.js +2 -6
  129. package/dist/gs/io/fs/readlink.js.map +1 -1
  130. package/dist/gs/io/fs/walk.js.map +1 -1
  131. package/dist/gs/io/io.d.ts +8 -5
  132. package/dist/gs/io/io.js +20 -2
  133. package/dist/gs/io/io.js.map +1 -1
  134. package/dist/gs/iter/iter.d.ts +3 -2
  135. package/dist/gs/iter/iter.js.map +1 -1
  136. package/dist/gs/maps/iter.d.ts +5 -5
  137. package/dist/gs/maps/iter.js +48 -21
  138. package/dist/gs/maps/iter.js.map +1 -1
  139. package/dist/gs/maps/maps.d.ts +6 -6
  140. package/dist/gs/math/bits/index.js +14 -24
  141. package/dist/gs/math/bits/index.js.map +1 -1
  142. package/dist/gs/mime/index.js +3 -1
  143. package/dist/gs/mime/index.js.map +1 -1
  144. package/dist/gs/net/http/httptest/index.d.ts +20 -1
  145. package/dist/gs/net/http/httptest/index.js +85 -3
  146. package/dist/gs/net/http/httptest/index.js.map +1 -1
  147. package/dist/gs/net/http/index.d.ts +118 -6
  148. package/dist/gs/net/http/index.js +389 -14
  149. package/dist/gs/net/http/index.js.map +1 -1
  150. package/dist/gs/net/http/pprof/index.d.ts +8 -0
  151. package/dist/gs/net/http/pprof/index.js +59 -0
  152. package/dist/gs/net/http/pprof/index.js.map +1 -0
  153. package/dist/gs/os/error.gs.js +9 -7
  154. package/dist/gs/os/error.gs.js.map +1 -1
  155. package/dist/gs/os/types_js.gs.js +95 -15
  156. package/dist/gs/os/types_js.gs.js.map +1 -1
  157. package/dist/gs/os/zero_copy_posix.gs.js +1 -1
  158. package/dist/gs/os/zero_copy_posix.gs.js.map +1 -1
  159. package/dist/gs/path/filepath/match.js.map +1 -1
  160. package/dist/gs/path/filepath/path.d.ts +5 -3
  161. package/dist/gs/path/filepath/path.js +65 -10
  162. package/dist/gs/path/filepath/path.js.map +1 -1
  163. package/dist/gs/reflect/index.d.ts +3 -2
  164. package/dist/gs/reflect/index.js +2 -1
  165. package/dist/gs/reflect/index.js.map +1 -1
  166. package/dist/gs/reflect/iter.js +2 -2
  167. package/dist/gs/reflect/iter.js.map +1 -1
  168. package/dist/gs/reflect/map.js +26 -0
  169. package/dist/gs/reflect/map.js.map +1 -1
  170. package/dist/gs/reflect/type.d.ts +24 -5
  171. package/dist/gs/reflect/type.js +390 -38
  172. package/dist/gs/reflect/type.js.map +1 -1
  173. package/dist/gs/reflect/types.d.ts +1 -0
  174. package/dist/gs/reflect/types.js +3 -1
  175. package/dist/gs/reflect/types.js.map +1 -1
  176. package/dist/gs/reflect/value.d.ts +4 -1
  177. package/dist/gs/reflect/value.js +39 -1
  178. package/dist/gs/reflect/value.js.map +1 -1
  179. package/dist/gs/reflect/visiblefields.js +1 -1
  180. package/dist/gs/reflect/visiblefields.js.map +1 -1
  181. package/dist/gs/runtime/debug/index.d.ts +39 -0
  182. package/dist/gs/runtime/debug/index.js +58 -0
  183. package/dist/gs/runtime/debug/index.js.map +1 -1
  184. package/dist/gs/runtime/pprof/index.d.ts +20 -0
  185. package/dist/gs/runtime/pprof/index.js +85 -0
  186. package/dist/gs/runtime/pprof/index.js.map +1 -0
  187. package/dist/gs/runtime/trace/index.d.ts +19 -0
  188. package/dist/gs/runtime/trace/index.js +64 -0
  189. package/dist/gs/runtime/trace/index.js.map +1 -0
  190. package/dist/gs/slices/slices.d.ts +24 -9
  191. package/dist/gs/slices/slices.js +229 -24
  192. package/dist/gs/slices/slices.js.map +1 -1
  193. package/dist/gs/sort/slice.gs.d.ts +5 -3
  194. package/dist/gs/sort/slice.gs.js +55 -17
  195. package/dist/gs/sort/slice.gs.js.map +1 -1
  196. package/dist/gs/strings/builder.js +26 -17
  197. package/dist/gs/strings/builder.js.map +1 -1
  198. package/dist/gs/strings/iter.js +140 -75
  199. package/dist/gs/strings/iter.js.map +1 -1
  200. package/dist/gs/strings/replace.js +2 -2
  201. package/dist/gs/strings/replace.js.map +1 -1
  202. package/dist/gs/strings/strings.js +52 -6
  203. package/dist/gs/strings/strings.js.map +1 -1
  204. package/dist/gs/sync/sync.d.ts +6 -3
  205. package/dist/gs/sync/sync.js +39 -11
  206. package/dist/gs/sync/sync.js.map +1 -1
  207. package/dist/gs/syscall/errors.d.ts +116 -112
  208. package/dist/gs/syscall/errors.js +38 -1
  209. package/dist/gs/syscall/errors.js.map +1 -1
  210. package/dist/gs/syscall/fs.d.ts +2 -8
  211. package/dist/gs/syscall/fs.js.map +1 -1
  212. package/dist/gs/syscall/js/index.js +20 -12
  213. package/dist/gs/syscall/js/index.js.map +1 -1
  214. package/dist/gs/syscall/types.d.ts +4 -1
  215. package/dist/gs/syscall/types.js.map +1 -1
  216. package/dist/gs/testing/testing.d.ts +4 -3
  217. package/dist/gs/testing/testing.js +21 -4
  218. package/dist/gs/testing/testing.js.map +1 -1
  219. package/dist/gs/time/time.js +22 -0
  220. package/dist/gs/time/time.js.map +1 -1
  221. package/dist/gs/unicode/unicode.js.map +1 -1
  222. package/dist/gs/unique/index.js +7 -2
  223. package/dist/gs/unique/index.js.map +1 -1
  224. package/go.mod +8 -8
  225. package/go.sum +14 -23
  226. package/gs/builtin/builtin.ts +364 -37
  227. package/gs/builtin/channel.ts +208 -38
  228. package/gs/builtin/defer.ts +13 -2
  229. package/gs/builtin/hostio.test.ts +1 -0
  230. package/gs/builtin/hostio.ts +38 -0
  231. package/gs/builtin/map.ts +46 -6
  232. package/gs/builtin/print.ts +12 -3
  233. package/gs/builtin/runtime-contract.test.ts +290 -10
  234. package/gs/builtin/slice.test.ts +70 -0
  235. package/gs/builtin/slice.ts +566 -255
  236. package/gs/builtin/type.ts +63 -10
  237. package/gs/builtin/varRef.ts +2 -0
  238. package/gs/bytes/buffer.gs.ts +28 -28
  239. package/gs/bytes/bytes.gs.ts +19 -10
  240. package/gs/bytes/bytes.test.ts +17 -0
  241. package/gs/bytes/iter.gs.ts +13 -14
  242. package/gs/compress/zlib/index.test.ts +28 -0
  243. package/gs/compress/zlib/index.ts +200 -0
  244. package/gs/compress/zlib/meta.json +3 -0
  245. package/gs/context/context.test.ts +36 -2
  246. package/gs/context/context.ts +9 -4
  247. package/gs/crypto/ecdh/index.test.ts +43 -0
  248. package/gs/crypto/ecdh/index.ts +274 -0
  249. package/gs/crypto/ed25519/index.test.ts +41 -0
  250. package/gs/crypto/ed25519/index.ts +238 -0
  251. package/gs/crypto/ed25519/meta.json +13 -0
  252. package/gs/crypto/internal/constanttime/index.test.ts +25 -0
  253. package/gs/crypto/internal/constanttime/index.ts +22 -0
  254. package/gs/crypto/rand/index.test.ts +89 -1
  255. package/gs/crypto/rand/index.ts +103 -1
  256. package/gs/crypto/rand/meta.json +4 -1
  257. package/gs/crypto/sha1/index.test.ts +28 -0
  258. package/gs/crypto/sha1/index.ts +130 -0
  259. package/gs/crypto/sha1/meta.json +8 -0
  260. package/gs/crypto/sha256/index.test.ts +78 -0
  261. package/gs/crypto/sha256/index.ts +150 -0
  262. package/gs/crypto/sha256/meta.json +9 -0
  263. package/gs/crypto/sha512/index.test.ts +31 -0
  264. package/gs/crypto/sha512/index.ts +161 -0
  265. package/gs/crypto/sha512/meta.json +11 -0
  266. package/gs/encoding/json/index.test.ts +25 -3
  267. package/gs/encoding/json/index.ts +21 -3
  268. package/gs/errors/errors.test.ts +4 -1
  269. package/gs/errors/errors.ts +32 -8
  270. package/gs/fmt/fmt.test.ts +23 -1
  271. package/gs/fmt/fmt.ts +76 -10
  272. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.test.ts +62 -7
  273. package/gs/github.com/aperturerobotics/protobuf-go-lite/index.ts +78 -36
  274. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.test.ts +32 -11
  275. package/gs/github.com/aperturerobotics/protobuf-go-lite/json/index.ts +122 -43
  276. package/gs/github.com/aperturerobotics/starpc/srpc/index.test.ts +31 -0
  277. package/gs/github.com/aperturerobotics/starpc/srpc/index.ts +518 -4
  278. package/gs/github.com/aperturerobotics/starpc/srpc/meta.json +6 -0
  279. package/gs/github.com/aperturerobotics/util/conc/index.test.ts +30 -0
  280. package/gs/github.com/aperturerobotics/util/conc/index.ts +172 -0
  281. package/gs/github.com/aperturerobotics/util/conc/meta.json +9 -0
  282. package/gs/github.com/aperturerobotics/wasivm/wazero/kernel/runtime/browser/browser.ts +1 -4
  283. package/gs/github.com/hack-pad/safejs/internal/catch/index.test.ts +35 -0
  284. package/gs/github.com/hack-pad/safejs/internal/catch/index.ts +65 -0
  285. package/gs/github.com/hack-pad/safejs/internal/catch/meta.json +9 -0
  286. package/gs/github.com/klauspost/compress/internal/le/index.test.ts +2 -1
  287. package/gs/github.com/klauspost/compress/internal/le/index.ts +6 -5
  288. package/gs/github.com/mr-tron/base58/base58/index.test.ts +70 -0
  289. package/gs/github.com/mr-tron/base58/base58/index.ts +231 -0
  290. package/gs/github.com/mr-tron/base58/base58/meta.json +3 -0
  291. package/gs/github.com/zeebo/blake3/internal/consts/index.test.ts +46 -0
  292. package/gs/github.com/zeebo/blake3/internal/consts/index.ts +26 -0
  293. package/gs/go/token/index.ts +17 -4
  294. package/gs/hash/fnv/index.test.ts +67 -0
  295. package/gs/hash/fnv/index.ts +351 -0
  296. package/gs/hash/fnv/meta.json +3 -0
  297. package/gs/hash/index.test.ts +37 -0
  298. package/gs/hash/index.ts +118 -0
  299. package/gs/hash/meta.json +5 -0
  300. package/gs/internal/byteorder/index.test.ts +6 -6
  301. package/gs/io/fs/readlink.ts +40 -48
  302. package/gs/io/fs/walk.ts +10 -2
  303. package/gs/io/io.test.ts +64 -0
  304. package/gs/io/io.ts +34 -13
  305. package/gs/iter/iter.ts +8 -2
  306. package/gs/maps/iter.ts +69 -26
  307. package/gs/maps/maps.test.ts +23 -0
  308. package/gs/maps/maps.ts +6 -6
  309. package/gs/math/bits/index.test.ts +20 -0
  310. package/gs/math/bits/index.ts +15 -28
  311. package/gs/mime/index.ts +8 -2
  312. package/gs/net/http/httptest/index.test.ts +85 -0
  313. package/gs/net/http/httptest/index.ts +113 -3
  314. package/gs/net/http/index.test.ts +159 -1
  315. package/gs/net/http/index.ts +515 -15
  316. package/gs/net/http/meta.json +6 -0
  317. package/gs/net/http/pprof/index.test.ts +47 -0
  318. package/gs/net/http/pprof/index.ts +65 -0
  319. package/gs/os/error.gs.ts +9 -10
  320. package/gs/os/error.test.ts +41 -0
  321. package/gs/os/file_unix_js.test.ts +55 -0
  322. package/gs/os/tempfile.gs.test.ts +37 -10
  323. package/gs/os/types_js.gs.ts +94 -15
  324. package/gs/os/zero_copy_posix.gs.ts +1 -2
  325. package/gs/path/filepath/match.ts +4 -1
  326. package/gs/path/filepath/meta.json +6 -0
  327. package/gs/path/filepath/path.test.ts +57 -2
  328. package/gs/path/filepath/path.ts +91 -12
  329. package/gs/reflect/field.test.ts +63 -0
  330. package/gs/reflect/index.ts +4 -1
  331. package/gs/reflect/iter.ts +2 -2
  332. package/gs/reflect/map.test.ts +24 -2
  333. package/gs/reflect/map.ts +35 -0
  334. package/gs/reflect/type.ts +543 -60
  335. package/gs/reflect/typefor.test.ts +100 -0
  336. package/gs/reflect/types.ts +3 -1
  337. package/gs/reflect/value.ts +50 -1
  338. package/gs/reflect/visiblefields.ts +1 -1
  339. package/gs/runtime/debug/index.test.ts +22 -1
  340. package/gs/runtime/debug/index.ts +88 -0
  341. package/gs/runtime/pprof/index.test.ts +36 -0
  342. package/gs/runtime/pprof/index.ts +104 -0
  343. package/gs/runtime/pprof/meta.json +6 -0
  344. package/gs/runtime/trace/index.test.ts +45 -0
  345. package/gs/runtime/trace/index.ts +97 -0
  346. package/gs/runtime/trace/meta.json +7 -0
  347. package/gs/slices/meta.json +2 -1
  348. package/gs/slices/slices.test.ts +86 -0
  349. package/gs/slices/slices.ts +284 -37
  350. package/gs/sort/slice.gs.ts +73 -23
  351. package/gs/sort/slice.test.ts +40 -0
  352. package/gs/strings/builder.test.ts +8 -0
  353. package/gs/strings/builder.ts +29 -17
  354. package/gs/strings/iter.test.ts +5 -7
  355. package/gs/strings/iter.ts +146 -71
  356. package/gs/strings/replace.test.ts +1 -4
  357. package/gs/strings/replace.ts +6 -6
  358. package/gs/strings/strings.test.ts +4 -0
  359. package/gs/strings/strings.ts +54 -6
  360. package/gs/sync/meta.json +1 -0
  361. package/gs/sync/sync.test.ts +57 -1
  362. package/gs/sync/sync.ts +45 -13
  363. package/gs/syscall/errors.ts +158 -115
  364. package/gs/syscall/fs.ts +8 -8
  365. package/gs/syscall/js/index.ts +49 -22
  366. package/gs/syscall/net.test.ts +26 -0
  367. package/gs/syscall/types.ts +7 -2
  368. package/gs/testing/testing.test.ts +56 -0
  369. package/gs/testing/testing.ts +27 -10
  370. package/gs/time/meta.json +2 -2
  371. package/gs/time/time.test.ts +4 -0
  372. package/gs/time/time.ts +33 -2
  373. package/gs/unicode/unicode.test.ts +14 -3
  374. package/gs/unicode/unicode.ts +1 -5
  375. package/gs/unique/index.ts +9 -2
  376. package/package.json +3 -3
@@ -0,0 +1,40 @@
1
+ import { describe, expect, it } from 'vitest'
2
+ import * as $ from '@goscript/builtin/index.js'
3
+
4
+ import { Slice, SliceIsSorted, SliceStable } from './slice.gs.js'
5
+
6
+ describe('sort.Slice override', () => {
7
+ it('sorts slices with logarithmic comparator growth', async () => {
8
+ const values = $.arrayToSlice<number>(
9
+ Array.from({ length: 128 }, (_, i) => 128 - i),
10
+ )
11
+ let calls = 0
12
+
13
+ await Slice(values, (i, j) => {
14
+ calls++
15
+ return values![i] < values![j]
16
+ })
17
+
18
+ expect(values).toEqual(Array.from({ length: 128 }, (_, i) => i + 1))
19
+ expect(calls).toBeLessThan(1200)
20
+ })
21
+
22
+ it('keeps equal values stable for SliceStable', async () => {
23
+ const values = $.arrayToSlice([
24
+ { group: 1, name: 'a' },
25
+ { group: 0, name: 'b' },
26
+ { group: 1, name: 'c' },
27
+ { group: 0, name: 'd' },
28
+ ])
29
+
30
+ await SliceStable(values, (i, j) => values![i].group < values![j].group)
31
+
32
+ expect(values?.map((value) => value.name)).toEqual(['b', 'd', 'a', 'c'])
33
+ expect(
34
+ await SliceIsSorted(
35
+ values,
36
+ (i, j) => values![i].group < values![j].group,
37
+ ),
38
+ ).toBe(true)
39
+ })
40
+ })
@@ -1,5 +1,6 @@
1
1
  import { describe, it, expect } from 'vitest'
2
2
  import { Builder } from './builder.js'
3
+ import { varRef } from '../builtin/varRef.js'
3
4
 
4
5
  describe('strings/Builder', () => {
5
6
  describe('Builder', () => {
@@ -89,6 +90,13 @@ describe('strings/Builder', () => {
89
90
  expect(b.Len()).toBe(11)
90
91
  })
91
92
 
93
+ it('should accept VarRef pointer receivers', () => {
94
+ const b = varRef(new Builder())
95
+ Builder.prototype.WriteString.call(b, 'hello')
96
+ expect(Builder.prototype.Len.call(b)).toBe(5)
97
+ expect(Builder.prototype.String.call(b)).toBe('hello')
98
+ })
99
+
92
100
  it('should handle empty writes', () => {
93
101
  const b = new Builder()
94
102
  const [n, err] = b.WriteString('')
@@ -1,5 +1,11 @@
1
1
  import * as $ from '@goscript/builtin/index.js'
2
2
 
3
+ function builderValue(
4
+ builder: Builder | $.VarRef<Builder> | null | undefined,
5
+ ): Builder {
6
+ return $.pointerValue<Builder>(builder)
7
+ }
8
+
3
9
  export class Builder {
4
10
  private _content: string = ''
5
11
  private _addr: Builder | null = null
@@ -16,21 +22,22 @@ export class Builder {
16
22
  }
17
23
 
18
24
  private copyCheck(): void {
19
- if (this._addr == null) {
20
- this._addr = this
21
- } else if (this._addr !== this) {
25
+ const builder = builderValue(this)
26
+ if (builder._addr == null) {
27
+ builder._addr = builder
28
+ } else if (builder._addr !== builder) {
22
29
  $.panic('strings: illegal use of non-zero Builder copied by value')
23
30
  }
24
31
  }
25
32
 
26
33
  // String returns the accumulated string.
27
34
  public String(): string {
28
- return this._content
35
+ return builderValue(this)._content
29
36
  }
30
37
 
31
38
  // Len returns the number of accumulated bytes; b.Len() == len(b.String()).
32
39
  public Len(): number {
33
- return this._content.length
40
+ return builderValue(this)._content.length
34
41
  }
35
42
 
36
43
  // Cap returns the capacity of the builder's underlying byte slice. It is the
@@ -38,20 +45,21 @@ export class Builder {
38
45
  // already written.
39
46
  public Cap(): number {
40
47
  // For simplicity, return the current length since JavaScript strings are dynamic
41
- return this._content.length
48
+ return builderValue(this)._content.length
42
49
  }
43
50
 
44
51
  // Reset resets the Builder to be empty.
45
52
  public Reset(): void {
46
- this._addr = null
47
- this._content = ''
53
+ const builder = builderValue(this)
54
+ builder._addr = null
55
+ builder._content = ''
48
56
  }
49
57
 
50
58
  // Grow grows b's capacity, if necessary, to guarantee space for
51
59
  // another n bytes. After Grow(n), at least n bytes can be written to b
52
60
  // without another allocation. If n is negative, Grow panics.
53
61
  public Grow(n: number): void {
54
- this.copyCheck()
62
+ builderValue(this).copyCheck()
55
63
  if (n < 0) {
56
64
  $.panic('strings.Builder.Grow: negative count')
57
65
  }
@@ -61,28 +69,31 @@ export class Builder {
61
69
  // Write appends the contents of p to b's buffer.
62
70
  // Write always returns len(p), nil.
63
71
  public Write(p: $.Bytes): [number, $.GoError] {
64
- this.copyCheck()
72
+ builderValue(this).copyCheck()
73
+ const builder = builderValue(this)
65
74
  // Convert byte array to string
66
75
  const bytes = $.bytesToUint8Array(p)
67
76
  const str = new TextDecoder('utf-8').decode(bytes)
68
- this._content += str
77
+ builder._content += str
69
78
  return [$.len(p), null]
70
79
  }
71
80
 
72
81
  // WriteByte appends the byte c to b's buffer.
73
82
  // The returned error is always nil.
74
83
  public WriteByte(c: number): $.GoError {
75
- this.copyCheck()
76
- this._content += String.fromCharCode(c)
84
+ builderValue(this).copyCheck()
85
+ const builder = builderValue(this)
86
+ builder._content += String.fromCharCode(c)
77
87
  return null
78
88
  }
79
89
 
80
90
  // WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
81
91
  // It returns the length of r and a nil error.
82
92
  public WriteRune(r: number): [number, $.GoError] {
83
- this.copyCheck()
93
+ builderValue(this).copyCheck()
94
+ const builder = builderValue(this)
84
95
  const str = String.fromCodePoint(r)
85
- this._content += str
96
+ builder._content += str
86
97
  // Return the byte length of the UTF-8 encoding
87
98
  const byteLength = new TextEncoder().encode(str).length
88
99
  return [byteLength, null]
@@ -91,8 +102,9 @@ export class Builder {
91
102
  // WriteString appends the contents of s to b's buffer.
92
103
  // It returns the length of s and a nil error.
93
104
  public WriteString(s: string): [number, $.GoError] {
94
- this.copyCheck()
95
- this._content += s
105
+ builderValue(this).copyCheck()
106
+ const builder = builderValue(this)
107
+ builder._content += s
96
108
  return [s.length, null]
97
109
  }
98
110
 
@@ -124,13 +124,11 @@ describe('strings/iter', () => {
124
124
  it('should handle unicode', () => {
125
125
  const parts: string[] = []
126
126
  const seq = SplitSeq('世界,你好', ',')
127
- // This test expects an error due to UTF-8 slicing limitations in JavaScript
128
- expect(() => {
129
- seq((part: string) => {
130
- parts.push(part)
131
- return true
132
- })
133
- }).toThrow('Cannot slice string at byte indices')
127
+ seq((part: string) => {
128
+ parts.push(part)
129
+ return true
130
+ })
131
+ expect(parts).toEqual(['世界', '你好'])
134
132
  })
135
133
  })
136
134
 
@@ -23,38 +23,64 @@ const asciiSpace: { [key: number]: boolean } = {
23
23
  // If s does not end in a newline, the final yielded line will not end in a newline.
24
24
  // It returns a single-use iterator.
25
25
  export function Lines(s: string): iter.Seq<string> {
26
- return (_yield: ((p0: string) => boolean) | null): void => {
27
- for (; $.len(s) > 0; ) {
28
- let line: string
29
- {
30
- let i = IndexByte(s, 10)
31
- if (i >= 0) {
32
- ;[line, s] = [
33
- $.sliceString(s, undefined, i + 1),
34
- $.sliceString(s, i + 1, undefined),
35
- ]
36
- } else {
37
- ;[line, s] = [s, '']
26
+ return (
27
+ _yield: ((p0: string) => iter.YieldResult) | null,
28
+ ): void | globalThis.Promise<void> => {
29
+ const walk = (): void | globalThis.Promise<void> => {
30
+ for (; $.len(s) > 0; ) {
31
+ let line: string
32
+ {
33
+ let i = IndexByte(s, 10)
34
+ if (i >= 0) {
35
+ ;[line, s] = [
36
+ $.sliceString(s, undefined, i + 1),
37
+ $.sliceString(s, i + 1, undefined),
38
+ ]
39
+ } else {
40
+ ;[line, s] = [s, '']
41
+ }
42
+ }
43
+ const keepGoing = _yield!(line)
44
+ if (keepGoing instanceof Promise) {
45
+ return keepGoing.then((next) => {
46
+ if (next) {
47
+ return walk()
48
+ }
49
+ })
50
+ }
51
+ if (!keepGoing) {
52
+ return
38
53
  }
39
- }
40
- if (!_yield!(line)) {
41
- return
42
54
  }
43
55
  }
44
- return
56
+ return walk()
45
57
  }
46
58
  }
47
59
 
48
60
  // explodeSeq returns an iterator over the runes in s.
49
61
  export function explodeSeq(s: string): iter.Seq<string> {
50
- return (_yield: ((p0: string) => boolean) | null): void => {
51
- for (; $.len(s) > 0; ) {
52
- let [, size] = utf8.DecodeRuneInString(s)
53
- if (!_yield!($.sliceString(s, undefined, size))) {
54
- return
62
+ return (
63
+ _yield: ((p0: string) => iter.YieldResult) | null,
64
+ ): void | globalThis.Promise<void> => {
65
+ const walk = (): void | globalThis.Promise<void> => {
66
+ for (; $.len(s) > 0; ) {
67
+ let [, size] = utf8.DecodeRuneInString(s)
68
+ const keepGoing = _yield!($.sliceString(s, undefined, size))
69
+ if (keepGoing instanceof Promise) {
70
+ return keepGoing.then((next) => {
71
+ if (next) {
72
+ s = $.sliceString(s, size, undefined)
73
+ return walk()
74
+ }
75
+ })
76
+ }
77
+ if (!keepGoing) {
78
+ return
79
+ }
80
+ s = $.sliceString(s, size, undefined)
55
81
  }
56
- s = $.sliceString(s, size, undefined)
57
82
  }
83
+ return walk()
58
84
  }
59
85
  }
60
86
 
@@ -68,19 +94,36 @@ export function splitSeq(
68
94
  if ($.len(sep) == 0) {
69
95
  return explodeSeq(s)
70
96
  }
71
- return (_yield: ((p0: string) => boolean) | null): void => {
72
- for (;;) {
73
- let i = Index(s, sep)
74
- if (i < 0) {
75
- break
76
- }
77
- let frag = $.sliceString(s, undefined, i + sepSave)
78
- if (!_yield!(frag)) {
79
- return
97
+ return (
98
+ _yield: ((p0: string) => iter.YieldResult) | null,
99
+ ): void | globalThis.Promise<void> => {
100
+ const walk = (): void | globalThis.Promise<void> => {
101
+ for (;;) {
102
+ let i = Index(s, sep)
103
+ if (i < 0) {
104
+ const keepGoing = _yield!(s)
105
+ if (keepGoing instanceof Promise) {
106
+ return keepGoing.then(() => {})
107
+ }
108
+ return
109
+ }
110
+ let frag = $.sliceString(s, undefined, i + sepSave)
111
+ const keepGoing = _yield!(frag)
112
+ if (keepGoing instanceof Promise) {
113
+ return keepGoing.then((next) => {
114
+ if (next) {
115
+ s = $.sliceString(s, i + $.len(sep), undefined)
116
+ return walk()
117
+ }
118
+ })
119
+ }
120
+ if (!keepGoing) {
121
+ return
122
+ }
123
+ s = $.sliceString(s, i + $.len(sep), undefined)
80
124
  }
81
- s = $.sliceString(s, i + $.len(sep), undefined)
82
125
  }
83
- _yield!(s)
126
+ return walk()
84
127
  }
85
128
  }
86
129
 
@@ -105,31 +148,47 @@ export function SplitAfterSeq(s: string, sep: string): iter.Seq<string> {
105
148
  // The iterator yields the same strings that would be returned by [Fields](s),
106
149
  // but without constructing the slice.
107
150
  export function FieldsSeq(s: string): iter.Seq<string> {
108
- return (_yield: ((p0: string) => boolean) | null): void => {
151
+ return (
152
+ _yield: ((p0: string) => iter.YieldResult) | null,
153
+ ): void | globalThis.Promise<void> => {
109
154
  let start = -1
110
- for (let i = 0; i < $.len(s); ) {
111
- let size = 1
112
- let r = $.indexString(s, i) as number
113
- let isSpace = asciiSpace[$.indexString(s, i)] === true
114
- if (r >= utf8.RuneSelf) {
115
- ;[r, size] = utf8.DecodeRuneInString($.sliceString(s, i, undefined))
116
- isSpace = unicode.IsSpace(r)
117
- }
118
- if (isSpace) {
119
- if (start >= 0) {
120
- if (!_yield!($.sliceString(s, start, i))) {
121
- return
155
+ const walk = (i: number): void | globalThis.Promise<void> => {
156
+ for (; i < $.len(s); ) {
157
+ let size = 1
158
+ let r = $.indexString(s, i) as number
159
+ let isSpace = asciiSpace[$.indexString(s, i)] === true
160
+ if (r >= utf8.RuneSelf) {
161
+ ;[r, size] = utf8.DecodeRuneInString($.sliceString(s, i, undefined))
162
+ isSpace = unicode.IsSpace(r)
163
+ }
164
+ if (isSpace) {
165
+ if (start >= 0) {
166
+ const keepGoing = _yield!($.sliceString(s, start, i))
167
+ start = -1
168
+ if (keepGoing instanceof Promise) {
169
+ return keepGoing.then((next) => {
170
+ if (next) {
171
+ return walk(i + size)
172
+ }
173
+ })
174
+ }
175
+ if (!keepGoing) {
176
+ return
177
+ }
122
178
  }
123
- start = -1
179
+ } else if (start < 0) {
180
+ start = i
181
+ }
182
+ i += size
183
+ }
184
+ if (start >= 0) {
185
+ const keepGoing = _yield!($.sliceString(s, start, undefined))
186
+ if (keepGoing instanceof Promise) {
187
+ return keepGoing.then(() => {})
124
188
  }
125
- } else if (start < 0) {
126
- start = i
127
189
  }
128
- i += size
129
- }
130
- if (start >= 0) {
131
- _yield!($.sliceString(s, start, undefined))
132
190
  }
191
+ return walk(0)
133
192
  }
134
193
  }
135
194
 
@@ -141,31 +200,47 @@ export function FieldsFuncSeq(
141
200
  s: string,
142
201
  f: ((p0: number) => boolean) | null,
143
202
  ): iter.Seq<string> {
144
- return (_yield: ((p0: string) => boolean) | null): void => {
203
+ return (
204
+ _yield: ((p0: string) => iter.YieldResult) | null,
205
+ ): void | globalThis.Promise<void> => {
145
206
  if (f === null) {
146
207
  return
147
208
  }
148
209
  let start = -1
149
- for (let i = 0; i < $.len(s); ) {
150
- let size = 1
151
- let r = $.indexString(s, i) as number
152
- if (r >= utf8.RuneSelf) {
153
- ;[r, size] = utf8.DecodeRuneInString($.sliceString(s, i, undefined))
154
- }
155
- if (f(r)) {
156
- if (start >= 0) {
157
- if (!_yield!($.sliceString(s, start, i))) {
158
- return
210
+ const walk = (i: number): void | globalThis.Promise<void> => {
211
+ for (; i < $.len(s); ) {
212
+ let size = 1
213
+ let r = $.indexString(s, i) as number
214
+ if (r >= utf8.RuneSelf) {
215
+ ;[r, size] = utf8.DecodeRuneInString($.sliceString(s, i, undefined))
216
+ }
217
+ if (f(r)) {
218
+ if (start >= 0) {
219
+ const keepGoing = _yield!($.sliceString(s, start, i))
220
+ start = -1
221
+ if (keepGoing instanceof Promise) {
222
+ return keepGoing.then((next) => {
223
+ if (next) {
224
+ return walk(i + size)
225
+ }
226
+ })
227
+ }
228
+ if (!keepGoing) {
229
+ return
230
+ }
159
231
  }
160
- start = -1
232
+ } else if (start < 0) {
233
+ start = i
234
+ }
235
+ i += size
236
+ }
237
+ if (start >= 0) {
238
+ const keepGoing = _yield!($.sliceString(s, start, undefined))
239
+ if (keepGoing instanceof Promise) {
240
+ return keepGoing.then(() => {})
161
241
  }
162
- } else if (start < 0) {
163
- start = i
164
242
  }
165
- i += size
166
- }
167
- if (start >= 0) {
168
- _yield!($.sliceString(s, start, undefined))
169
243
  }
244
+ return walk(0)
170
245
  }
171
246
  }
@@ -124,10 +124,7 @@ describe('strings/Replacer', () => {
124
124
  it('should handle unicode strings', () => {
125
125
  const r = NewReplacer('世界', '世界!')
126
126
  if (r) {
127
- // This test expects an error due to UTF-8 slicing limitations in JavaScript
128
- expect(() => r.Replace('Hello 世界')).toThrow(
129
- 'Cannot slice string at byte indices',
130
- )
127
+ expect(r.Replace('Hello 世界')).toBe('Hello 世界!')
131
128
  }
132
129
  })
133
130
 
@@ -396,12 +396,12 @@ class trieNode {
396
396
  // what is currently t.prefix[0] will lead to prefixNode, and
397
397
  // looking up key[0] will lead to keyNode.
398
398
  const prefixNode =
399
- $.len(t!.prefix) == 1
400
- ? t!.next
401
- : new trieNode({
402
- next: t!.next,
403
- prefix: $.sliceString(t!.prefix, 1, undefined),
404
- })
399
+ $.len(t!.prefix) == 1 ?
400
+ t!.next
401
+ : new trieNode({
402
+ next: t!.next,
403
+ prefix: $.sliceString(t!.prefix, 1, undefined),
404
+ })
405
405
  let keyNode = new trieNode()
406
406
  t!.table = $.makeSlice<trieNode | null>(r!.tableSize)
407
407
  t!.table![r!.mapping![$.indexString(t!.prefix, 0)]] = prefixNode
@@ -203,6 +203,7 @@ describe('strings', () => {
203
203
  expect(Index('hello world', 'xyz')).toBe(-1)
204
204
  expect(Index('hello', 'hello')).toBe(0)
205
205
  expect(Index('', '')).toBe(0)
206
+ expect(Index('世界,你好', ',')).toBe(6)
206
207
  })
207
208
  })
208
209
 
@@ -219,6 +220,7 @@ describe('strings', () => {
219
220
  expect(IndexByte('hello', 101)).toBe(1) // 'e'
220
221
  expect(IndexByte('hello', 120)).toBe(-1) // 'x'
221
222
  expect(IndexByte('', 97)).toBe(-1) // 'a'
223
+ expect(IndexByte('世界,你好', 44)).toBe(6)
222
224
  })
223
225
  })
224
226
 
@@ -257,6 +259,7 @@ describe('strings', () => {
257
259
  expect(LastIndex('hello world hello', 'hello')).toBe(12)
258
260
  expect(LastIndex('hello world', 'xyz')).toBe(-1)
259
261
  expect(LastIndex('hello', 'hello')).toBe(0)
262
+ expect(LastIndex('世界,你好', '你好')).toBe(7)
260
263
  })
261
264
  })
262
265
 
@@ -273,6 +276,7 @@ describe('strings', () => {
273
276
  expect(LastIndexByte('hello', 108)).toBe(3) // 'l'
274
277
  expect(LastIndexByte('hello', 120)).toBe(-1) // 'x'
275
278
  expect(LastIndexByte('', 97)).toBe(-1) // 'a'
279
+ expect(LastIndexByte('世界,你好', 44)).toBe(6)
276
280
  })
277
281
  })
278
282
 
@@ -56,18 +56,31 @@ export function ContainsFunc(
56
56
 
57
57
  // Index returns the index of the first instance of substr in s, or -1 if substr is not present in s.
58
58
  export function Index(s: string, substr: string): number {
59
- return s.indexOf(substr)
59
+ if (substr === '') {
60
+ return 0
61
+ }
62
+ return indexBytes($.stringToBytes(s), $.stringToBytes(substr))
60
63
  }
61
64
 
62
65
  // LastIndex returns the index of the last instance of substr in s, or -1 if substr is not present in s.
63
66
  export function LastIndex(s: string, substr: string): number {
64
- return s.lastIndexOf(substr)
67
+ const haystack = $.stringToBytes(s)
68
+ const needle = $.stringToBytes(substr)
69
+ if (needle.length === 0) {
70
+ return haystack.length
71
+ }
72
+ return lastIndexBytes(haystack, needle)
65
73
  }
66
74
 
67
75
  // IndexByte returns the index of the first instance of c in s, or -1 if c is not present in s.
68
76
  export function IndexByte(s: string, c: number): number {
69
- const char = String.fromCharCode(c)
70
- return s.indexOf(char)
77
+ const bytes = $.stringToBytes(s)
78
+ for (let i = 0; i < bytes.length; i++) {
79
+ if (bytes[i] === (c & 0xff)) {
80
+ return i
81
+ }
82
+ }
83
+ return -1
71
84
  }
72
85
 
73
86
  // IndexRune returns the index of the first instance of the Unicode code point r, or -1 if rune is not present in s.
@@ -98,8 +111,43 @@ export function LastIndexAny(s: string, chars: string): number {
98
111
 
99
112
  // LastIndexByte returns the index of the last instance of c in s, or -1 if c is not present in s.
100
113
  export function LastIndexByte(s: string, c: number): number {
101
- const char = String.fromCharCode(c)
102
- return s.lastIndexOf(char)
114
+ const bytes = $.stringToBytes(s)
115
+ for (let i = bytes.length - 1; i >= 0; i--) {
116
+ if (bytes[i] === (c & 0xff)) {
117
+ return i
118
+ }
119
+ }
120
+ return -1
121
+ }
122
+
123
+ function indexBytes(haystack: Uint8Array, needle: Uint8Array): number {
124
+ if (needle.length === 0) {
125
+ return 0
126
+ }
127
+ outer: for (let i = 0; i <= haystack.length - needle.length; i++) {
128
+ for (let j = 0; j < needle.length; j++) {
129
+ if (haystack[i + j] !== needle[j]) {
130
+ continue outer
131
+ }
132
+ }
133
+ return i
134
+ }
135
+ return -1
136
+ }
137
+
138
+ function lastIndexBytes(haystack: Uint8Array, needle: Uint8Array): number {
139
+ if (needle.length === 0) {
140
+ return haystack.length
141
+ }
142
+ outer: for (let i = haystack.length - needle.length; i >= 0; i--) {
143
+ for (let j = 0; j < needle.length; j++) {
144
+ if (haystack[i + j] !== needle[j]) {
145
+ continue outer
146
+ }
147
+ }
148
+ return i
149
+ }
150
+ return -1
103
151
  }
104
152
 
105
153
  // IndexFunc returns the index into s of the first Unicode code point satisfying f(c), or -1 if none do.
package/gs/sync/meta.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "unsafe"
4
4
  ],
5
5
  "asyncMethods": {
6
+ "Locker.Lock": true,
6
7
  "Mutex.Lock": true,
7
8
  "RWMutex.Lock": true,
8
9
  "RWMutex.RLock": true,