eliscript 0.1.3 → 0.1.4

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 (526) hide show
  1. package/COPYING.RUNTIME +93 -0
  2. package/README.md +15 -3
  3. package/bin/eliscript-test +29 -0
  4. package/bootstrap/backends/README.md +5 -4
  5. package/bootstrap/backends/binary.eli +89 -22
  6. package/bootstrap/backends/capabilities.eli +9 -4
  7. package/bootstrap/backends/managed-layout.eli +93 -81
  8. package/bootstrap/backends/managed.eli +651 -125
  9. package/bootstrap/backends/native.eli +1791 -324
  10. package/bootstrap/backends/numeric.eli +71 -7
  11. package/bootstrap/backends/sha256.eli +165 -68
  12. package/bootstrap/backends/wasm.eli +461 -52
  13. package/bootstrap/compiler/analyzer.eli +692 -2
  14. package/bootstrap/compiler/compiler.eli +16 -3
  15. package/bootstrap/compiler/emitter.eli +80 -35
  16. package/bootstrap/compiler/project.eli +175 -1
  17. package/bootstrap/compiler/transient-analysis.eli +2 -2
  18. package/bootstrap/host/build.eli +1 -0
  19. package/bootstrap/host/build.mjs +1 -1
  20. package/bootstrap/host/build.mjs.map +1 -1
  21. package/bootstrap/host/bun.mjs +7 -9
  22. package/bootstrap/host/bun.mjs.map +1 -1
  23. package/bootstrap/host/check-cli-ground.eli +47 -5
  24. package/bootstrap/host/check-cli-ground.mjs +60 -19
  25. package/bootstrap/host/check-cli-ground.mjs.map +1 -1
  26. package/bootstrap/host/check-cli.mjs +41 -1
  27. package/bootstrap/host/check-cli.mjs.map +1 -1
  28. package/bootstrap/host/compiler-session.eli +136 -22
  29. package/bootstrap/host/eliscript-cli.mjs +1 -1
  30. package/bootstrap/host/eliscript-cli.mjs.map +1 -1
  31. package/bootstrap/host/evaluation-cli.eli +33 -1
  32. package/bootstrap/host/evaluation-cli.mjs +65 -28
  33. package/bootstrap/host/evaluation-cli.mjs.map +1 -1
  34. package/bootstrap/host/evaluation.eli +651 -42
  35. package/bootstrap/host/evaluation.mjs +884 -72
  36. package/bootstrap/host/evaluation.mjs.map +1 -1
  37. package/bootstrap/host/project-cli-ground.eli +6 -1
  38. package/bootstrap/host/project-cli-ground.mjs +33 -32
  39. package/bootstrap/host/project-cli-ground.mjs.map +1 -1
  40. package/bootstrap/host/project-cli.mjs +1 -1
  41. package/bootstrap/host/project-cli.mjs.map +1 -1
  42. package/bootstrap/host/project-ground.eli +266 -45
  43. package/bootstrap/host/project-ground.mjs +429 -159
  44. package/bootstrap/host/project-ground.mjs.map +1 -1
  45. package/bootstrap/host/project.mjs +30 -40
  46. package/bootstrap/host/project.mjs.map +1 -1
  47. package/bootstrap/host/test-cli.eli +379 -0
  48. package/bootstrap/host/test-cli.mjs +494 -0
  49. package/bootstrap/host/test-cli.mjs.map +1 -0
  50. package/browser/README.md +7 -0
  51. package/browser/host.mjs +21 -29
  52. package/browser/host.mjs.map +1 -1
  53. package/browser/worker.mjs +7 -9
  54. package/browser/worker.mjs.map +1 -1
  55. package/compiler/eliscript-analyzer.el +694 -0
  56. package/compiler/eliscript-cli.el +66 -22
  57. package/compiler/eliscript-emitter.el +67 -43
  58. package/compiler/eliscript-ir-emitter.el +71 -36
  59. package/compiler/eliscript-project.el +360 -80
  60. package/compiler/eliscript-transient-analysis.el +5 -5
  61. package/compiler/eliscript.el +6 -0
  62. package/dist/backends/binary.mjs +148 -26
  63. package/dist/backends/binary.mjs.map +1 -1
  64. package/dist/backends/capabilities.mjs +11 -4
  65. package/dist/backends/capabilities.mjs.map +1 -1
  66. package/dist/backends/elf.mjs +4 -4
  67. package/dist/backends/elf.mjs.map +1 -1
  68. package/dist/backends/managed-layout.mjs +53 -56
  69. package/dist/backends/managed-layout.mjs.map +1 -1
  70. package/dist/backends/managed.mjs +1116 -375
  71. package/dist/backends/managed.mjs.map +1 -1
  72. package/dist/backends/memory.mjs +24 -26
  73. package/dist/backends/memory.mjs.map +1 -1
  74. package/dist/backends/native.mjs +2529 -434
  75. package/dist/backends/native.mjs.map +1 -1
  76. package/dist/backends/numeric.mjs +140 -33
  77. package/dist/backends/numeric.mjs.map +1 -1
  78. package/dist/backends/optimize.mjs +2 -2
  79. package/dist/backends/optimize.mjs.map +1 -1
  80. package/dist/backends/regexp.mjs +33 -35
  81. package/dist/backends/regexp.mjs.map +1 -1
  82. package/dist/backends/sha256.mjs +159 -59
  83. package/dist/backends/sha256.mjs.map +1 -1
  84. package/dist/backends/wasm.mjs +609 -73
  85. package/dist/backends/wasm.mjs.map +1 -1
  86. package/dist/bootstrap/analyzer.mjs +1018 -2
  87. package/dist/bootstrap/analyzer.mjs.map +1 -1
  88. package/dist/bootstrap/compiler.mjs +30 -23
  89. package/dist/bootstrap/compiler.mjs.map +1 -1
  90. package/dist/bootstrap/emitter.mjs +206 -161
  91. package/dist/bootstrap/emitter.mjs.macro-origins.json +16 -16
  92. package/dist/bootstrap/emitter.mjs.map +1 -1
  93. package/dist/bootstrap/expander.mjs +27 -31
  94. package/dist/bootstrap/expander.mjs.map +1 -1
  95. package/dist/bootstrap/ir.mjs +22 -24
  96. package/dist/bootstrap/ir.mjs.map +1 -1
  97. package/dist/bootstrap/project.mjs +246 -1
  98. package/dist/bootstrap/project.mjs.map +1 -1
  99. package/dist/bootstrap/reader.mjs +2 -2
  100. package/dist/bootstrap/reader.mjs.map +1 -1
  101. package/dist/bootstrap/source-map.mjs +34 -48
  102. package/dist/bootstrap/source-map.mjs.map +1 -1
  103. package/dist/bootstrap/transient-analysis.mjs +2 -2
  104. package/dist/bootstrap/transient-analysis.mjs.map +1 -1
  105. package/dist/browser/compiler.js +106 -106
  106. package/dist/browser/host.mjs +21 -29
  107. package/dist/browser/loader.js +1 -1
  108. package/dist/browser/worker.mjs +95 -95
  109. package/dist/compiler-artifact.json +1 -1
  110. package/dist/host/compiler-session.mjs +405 -228
  111. package/dist/host/compiler-session.mjs.map +1 -1
  112. package/dist/host/compiler-tools.mjs +44 -46
  113. package/dist/host/compiler-tools.mjs.map +1 -1
  114. package/dist/literal-sources/runtime/core/array-map.eli +45 -0
  115. package/dist/literal-sources/runtime/core/collection-internals.eli +22 -2
  116. package/dist/literal-sources/runtime/core/collection.eli +282 -23
  117. package/dist/literal-sources/runtime/core/data-text.eli +196 -13
  118. package/dist/literal-sources/runtime/core/data.eli +1 -1
  119. package/dist/literal-sources/runtime/core/identifier.eli +1 -1
  120. package/dist/literal-sources/runtime/core/lazy-sequence.eli +822 -44
  121. package/dist/literal-sources/runtime/core/list.eli +1 -1
  122. package/dist/literal-sources/runtime/core/map.eli +1 -1
  123. package/dist/literal-sources/runtime/core/metadata-internals.eli +2 -2
  124. package/dist/literal-sources/runtime/core/metadata.eli +2 -2
  125. package/dist/literal-sources/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
  126. package/dist/literal-sources/runtime/core/object.eli +1 -1
  127. package/dist/literal-sources/runtime/core/order.eli +3 -3
  128. package/dist/literal-sources/runtime/core/persistent-kind.eli +1 -1
  129. package/dist/literal-sources/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
  130. package/dist/literal-sources/runtime/core/protocol.eli +5 -3
  131. package/dist/literal-sources/runtime/core/queue.eli +1 -1
  132. package/dist/literal-sources/runtime/core/record.eli +3 -3
  133. package/dist/literal-sources/runtime/core/regex.eli +2 -2
  134. package/dist/literal-sources/runtime/core/sequence.eli +1 -1
  135. package/dist/literal-sources/runtime/core/set-algebra.eli +1 -1
  136. package/dist/literal-sources/runtime/core/set.eli +1 -1
  137. package/dist/literal-sources/runtime/core/sorted.eli +4 -4
  138. package/dist/literal-sources/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
  139. package/dist/literal-sources/runtime/core/text.eli +1 -1
  140. package/dist/literal-sources/runtime/core/transducer.eli +24 -2
  141. package/dist/literal-sources/runtime/core/transient.eli +7 -5
  142. package/dist/literal-sources/runtime/core/type.eli +1 -1
  143. package/dist/literal-sources/runtime/core/value-internals.eli +3 -3
  144. package/dist/literal-sources/runtime/core/value.eli +2 -2
  145. package/dist/literal-sources/runtime/core/vector.eli +5 -2
  146. package/dist/literal-sources/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
  147. package/dist/literal-sources/{stdlib → runtime/managed}/managed-literals.eli +406 -81
  148. package/{stdlib → dist/literal-sources/runtime/managed}/managed-queue-state.eli +1 -1
  149. package/{stdlib → dist/literal-sources/runtime/managed}/managed-sorted-state.eli +1 -1
  150. package/dist/literal-sources/runtime/managed/managed-transient-state.eli +1206 -0
  151. package/{stdlib → dist/literal-sources/runtime/managed}/managed-value-state.eli +30 -6
  152. package/dist/literal-sources/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
  153. package/dist/literal-sources/runtime/wire/http-rpc.eli +1 -1
  154. package/dist/literal-sources/runtime/wire/markdown.eli +1 -1
  155. package/dist/literal-sources/runtime/wire/schema.eli +1 -1
  156. package/dist/literal-sources/stdlib/bit.eli +89 -1
  157. package/dist/literal-sources/stdlib/data.eli +522 -0
  158. package/dist/literal-sources/stdlib/interop/object.eli +39 -0
  159. package/dist/literal-sources/stdlib/persistent-list.eli +60 -21
  160. package/dist/literal-sources/stdlib/persistent-map.eli +335 -324
  161. package/dist/literal-sources/stdlib/persistent-set.eli +8 -6
  162. package/dist/literal-sources/stdlib/persistent-vector.eli +18 -18
  163. package/dist/literal-sources/stdlib/protocol.eli +63 -0
  164. package/{stdlib/core → dist/literal-sources/stdlib}/set.eli +8 -8
  165. package/dist/literal-sources/stdlib/string.eli +362 -0
  166. package/dist/runtime/lisp/async-runtime.mjs +55 -59
  167. package/dist/runtime/lisp/async-runtime.mjs.map +1 -1
  168. package/dist/runtime/lisp/builtins.mjs +232 -280
  169. package/dist/runtime/lisp/builtins.mjs.map +1 -1
  170. package/dist/runtime/lisp/cli.mjs +159 -167
  171. package/dist/runtime/lisp/cli.mjs.map +1 -1
  172. package/dist/runtime/lisp/compiler.mjs +359 -419
  173. package/dist/runtime/lisp/compiler.mjs.map +1 -1
  174. package/dist/runtime/lisp/core.mjs +24 -24
  175. package/dist/runtime/lisp/core.mjs.map +1 -1
  176. package/dist/runtime/lisp/evaluator.mjs +315 -385
  177. package/dist/runtime/lisp/evaluator.mjs.map +1 -1
  178. package/dist/runtime/lisp/extensions.mjs +85 -103
  179. package/dist/runtime/lisp/extensions.mjs.map +1 -1
  180. package/dist/runtime/lisp/host-io.mjs +106 -122
  181. package/dist/runtime/lisp/host-io.mjs.map +1 -1
  182. package/dist/runtime/lisp/io-worker.mjs +117 -135
  183. package/dist/runtime/lisp/io-worker.mjs.map +1 -1
  184. package/dist/runtime/lisp/keymap.mjs +5 -5
  185. package/dist/runtime/lisp/keymap.mjs.map +1 -1
  186. package/dist/runtime/lisp/macros-forms.mjs +49 -51
  187. package/dist/runtime/lisp/macros-forms.mjs.map +1 -1
  188. package/dist/runtime/lisp/reader-format.mjs +31 -31
  189. package/dist/runtime/lisp/reader-format.mjs.map +1 -1
  190. package/dist/runtime/lisp/regexp-buffer.mjs +9 -9
  191. package/dist/runtime/lisp/regexp-buffer.mjs.map +1 -1
  192. package/dist/runtime/math.mjs +32 -38
  193. package/dist/runtime/math.mjs.map +1 -1
  194. package/dist/runtime/number-radix.mjs +5 -7
  195. package/dist/runtime/number-radix.mjs.map +1 -1
  196. package/dist/runtime/primitives.mjs +81 -46
  197. package/dist/runtime/primitives.mjs.macro-origins.json +294 -44
  198. package/dist/runtime/primitives.mjs.map +1 -1
  199. package/dist/runtime/unicode-case.mjs +6 -6
  200. package/dist/runtime/unicode-case.mjs.map +1 -1
  201. package/dist/runtime.eli +3068 -1882
  202. package/dist/stage3-release.json +1 -1
  203. package/dist/stdlib/async.mjs +1838 -0
  204. package/dist/stdlib/bit.mjs +116 -0
  205. package/dist/stdlib/collection.mjs +193 -0
  206. package/dist/stdlib/core.mjs +28 -0
  207. package/dist/stdlib/data-text.mjs +54 -0
  208. package/dist/stdlib/data.mjs +601 -0
  209. package/dist/stdlib/devtools.mjs +565 -0
  210. package/dist/stdlib/diff.mjs +180 -0
  211. package/dist/stdlib/fn.mjs +164 -0
  212. package/dist/stdlib/hierarchy.mjs +189 -0
  213. package/dist/stdlib/identifier.mjs +33 -0
  214. package/dist/stdlib/interop/js.mjs +706 -0
  215. package/dist/stdlib/interop/object.mjs +40 -0
  216. package/dist/stdlib/json.mjs +843 -0
  217. package/dist/stdlib/lazy-sequence.mjs +117 -0
  218. package/dist/stdlib/math.mjs +386 -0
  219. package/dist/stdlib/meta.mjs +21 -0
  220. package/dist/stdlib/multimethod.mjs +345 -0
  221. package/dist/stdlib/numeric.mjs +327 -0
  222. package/dist/stdlib/order.mjs +194 -0
  223. package/dist/stdlib/persistent-list.mjs +171 -0
  224. package/dist/stdlib/persistent-map.mjs +781 -0
  225. package/dist/stdlib/persistent-queue.mjs +44 -0
  226. package/dist/stdlib/persistent-set.mjs +225 -0
  227. package/dist/stdlib/persistent-vector.mjs +316 -0
  228. package/dist/stdlib/portable/data-text.mjs +1094 -0
  229. package/dist/stdlib/portable/data.mjs +62 -0
  230. package/dist/stdlib/portable/identifier.mjs +179 -0
  231. package/dist/stdlib/portable/metadata.mjs +63 -0
  232. package/dist/stdlib/portable/object.mjs +140 -0
  233. package/dist/stdlib/portable/sequence.mjs +142 -0
  234. package/dist/stdlib/portable/text.mjs +111 -0
  235. package/dist/stdlib/pprint.mjs +807 -0
  236. package/dist/stdlib/protocol.mjs +61 -0
  237. package/dist/stdlib/record.mjs +25 -0
  238. package/dist/stdlib/regex.mjs +115 -0
  239. package/dist/stdlib/result.mjs +198 -0
  240. package/dist/stdlib/schema.mjs +2828 -0
  241. package/dist/stdlib/seq.mjs +913 -0
  242. package/dist/stdlib/set.mjs +317 -0
  243. package/dist/stdlib/sorted.mjs +56 -0
  244. package/dist/stdlib/state.mjs +451 -0
  245. package/dist/stdlib/string.mjs +494 -0
  246. package/dist/stdlib/tap.mjs +187 -0
  247. package/dist/stdlib/test.mjs +669 -0
  248. package/dist/stdlib/time.mjs +522 -0
  249. package/dist/stdlib/transducer.mjs +85 -0
  250. package/dist/stdlib/transient.mjs +32 -0
  251. package/dist/stdlib/type.mjs +28 -0
  252. package/dist/stdlib/uuid.mjs +377 -0
  253. package/dist/stdlib/value.mjs +334 -0
  254. package/dist/stdlib/walk.mjs +243 -0
  255. package/dist/stdlib/wire/gepo.mjs +28 -0
  256. package/dist/stdlib/wire/mdp.mjs +13 -0
  257. package/dist/stdlib/wire/mds.mjs +10 -0
  258. package/dist/stdlib/zipper.mjs +1025 -0
  259. package/editor/README.md +15 -0
  260. package/editor/eliscript-data.el +221 -8
  261. package/editor/eliscript-mode.el +207 -48
  262. package/editor/eliscript-runtime.el +6 -3
  263. package/package.json +49 -15
  264. package/platform/README.md +6 -0
  265. package/platform/browser.mjs +10 -12
  266. package/platform/browser.mjs.map +1 -1
  267. package/runtime/README.md +12 -5
  268. package/runtime/core/array-map.eli +538 -0
  269. package/runtime/core/array-map.mjs +651 -0
  270. package/runtime/core/array-map.mjs.map +1 -0
  271. package/runtime/core/collection-internals.eli +85 -33
  272. package/runtime/core/collection-internals.mjs +245 -193
  273. package/runtime/core/collection-internals.mjs.map +1 -1
  274. package/runtime/core/collection.eli +105 -3
  275. package/runtime/core/collection.mjs +190 -53
  276. package/runtime/core/collection.mjs.map +1 -1
  277. package/runtime/core/data-text.eli +224 -16
  278. package/runtime/core/data-text.mjs +345 -83
  279. package/runtime/core/data-text.mjs.map +1 -1
  280. package/runtime/core/data.eli +50 -33
  281. package/runtime/core/data.mjs +72 -36
  282. package/runtime/core/data.mjs.map +1 -1
  283. package/runtime/core/identifier.mjs +15 -19
  284. package/runtime/core/identifier.mjs.map +1 -1
  285. package/runtime/core/lazy-sequence.eli +885 -15
  286. package/runtime/core/lazy-sequence.mjs +1265 -38
  287. package/runtime/core/lazy-sequence.mjs.map +1 -1
  288. package/runtime/core/map-internals.eli +209 -1
  289. package/runtime/core/map-internals.mjs +359 -71
  290. package/runtime/core/map-internals.mjs.map +1 -1
  291. package/runtime/core/map.eli +75 -7
  292. package/runtime/core/map.mjs +122 -47
  293. package/runtime/core/map.mjs.map +1 -1
  294. package/{stdlib/core/object.eli → runtime/core/object-impl.eli} +6 -6
  295. package/runtime/core/object-impl.mjs +5 -5
  296. package/runtime/core/object-impl.mjs.map +1 -1
  297. package/runtime/core/order.eli +15 -3
  298. package/runtime/core/order.mjs +23 -8
  299. package/runtime/core/order.mjs.map +1 -1
  300. package/runtime/core/persistent-kind.mjs +9 -13
  301. package/runtime/core/persistent-kind.mjs.map +1 -1
  302. package/runtime/core/protocol-error.mjs +9 -11
  303. package/runtime/core/protocol-error.mjs.map +1 -1
  304. package/{stdlib/core/protocol.eli → runtime/core/protocol-impl.eli} +2 -2
  305. package/runtime/core/protocol-impl.mjs +10 -10
  306. package/runtime/core/protocol-impl.mjs.map +1 -1
  307. package/runtime/core/queue.mjs +14 -16
  308. package/runtime/core/queue.mjs.map +1 -1
  309. package/runtime/core/record.eli +2 -2
  310. package/runtime/core/record.mjs +13 -17
  311. package/runtime/core/record.mjs.map +1 -1
  312. package/runtime/core/regex.mjs +18 -22
  313. package/runtime/core/regex.mjs.map +1 -1
  314. package/runtime/core/sequence.eli +33 -31
  315. package/runtime/core/sequence.mjs +60 -93
  316. package/runtime/core/sequence.mjs.map +1 -1
  317. package/runtime/core/set.eli +53 -27
  318. package/runtime/core/set.mjs +113 -82
  319. package/runtime/core/set.mjs.map +1 -1
  320. package/runtime/core/sorted.mjs +91 -108
  321. package/runtime/core/sorted.mjs.map +1 -1
  322. package/{stdlib/core/text.eli → runtime/core/text-impl.eli} +2 -2
  323. package/runtime/core/text-impl.mjs +1 -1
  324. package/runtime/core/text-impl.mjs.map +1 -1
  325. package/runtime/core/transducer.eli +24 -0
  326. package/runtime/core/transducer.mjs +27 -1
  327. package/runtime/core/transducer.mjs.map +1 -1
  328. package/runtime/core/transient-internals.eli +12 -4
  329. package/runtime/core/transient-internals.mjs +19 -6
  330. package/runtime/core/transient-internals.mjs.map +1 -1
  331. package/runtime/core/transient.eli +31 -0
  332. package/runtime/core/transient.mjs +33 -2
  333. package/runtime/core/transient.mjs.map +1 -1
  334. package/runtime/core/value-internals.mjs +29 -35
  335. package/runtime/core/value-internals.mjs.map +1 -1
  336. package/runtime/core/vector.eli +73 -20
  337. package/runtime/core/vector.mjs +159 -156
  338. package/runtime/core/vector.mjs.map +1 -1
  339. package/runtime/literals.eli +16 -9
  340. package/runtime/literals.mjs +25 -9
  341. package/runtime/literals.mjs.map +1 -1
  342. package/{stdlib → runtime/managed}/managed-literal-protocols.eli +100 -34
  343. package/{stdlib → runtime/managed}/managed-literals.eli +406 -81
  344. package/{dist/literal-sources/stdlib → runtime/managed}/managed-queue-state.eli +1 -1
  345. package/runtime/managed/managed-runtime-array-map.eli +45 -0
  346. package/{stdlib → runtime/managed}/managed-runtime-collection-internals.eli +22 -2
  347. package/{stdlib → runtime/managed}/managed-runtime-collection.eli +282 -23
  348. package/{stdlib → runtime/managed}/managed-runtime-data-text.eli +196 -13
  349. package/{stdlib → runtime/managed}/managed-runtime-data.eli +1 -1
  350. package/{stdlib → runtime/managed}/managed-runtime-identifier.eli +1 -1
  351. package/runtime/managed/managed-runtime-lazy-sequence.eli +935 -0
  352. package/{stdlib → runtime/managed}/managed-runtime-list.eli +1 -1
  353. package/{stdlib → runtime/managed}/managed-runtime-map.eli +1 -1
  354. package/{stdlib → runtime/managed}/managed-runtime-metadata-internals.eli +2 -2
  355. package/{stdlib → runtime/managed}/managed-runtime-metadata.eli +2 -2
  356. package/{stdlib → runtime/managed}/managed-runtime-object.eli +1 -1
  357. package/{stdlib → runtime/managed}/managed-runtime-order.eli +3 -3
  358. package/{stdlib → runtime/managed}/managed-runtime-persistent-kind.eli +1 -1
  359. package/{stdlib → runtime/managed}/managed-runtime-protocol.eli +5 -3
  360. package/{stdlib → runtime/managed}/managed-runtime-queue.eli +1 -1
  361. package/{stdlib → runtime/managed}/managed-runtime-record.eli +3 -3
  362. package/{stdlib → runtime/managed}/managed-runtime-regex.eli +2 -2
  363. package/{stdlib → runtime/managed}/managed-runtime-sequence.eli +1 -1
  364. package/{stdlib → runtime/managed}/managed-runtime-set-algebra.eli +1 -1
  365. package/{stdlib → runtime/managed}/managed-runtime-set.eli +1 -1
  366. package/{stdlib → runtime/managed}/managed-runtime-sorted.eli +4 -4
  367. package/{stdlib → runtime/managed}/managed-runtime-text.eli +1 -1
  368. package/{stdlib → runtime/managed}/managed-runtime-transducer.eli +24 -2
  369. package/{stdlib → runtime/managed}/managed-runtime-transient.eli +7 -5
  370. package/{stdlib → runtime/managed}/managed-runtime-type.eli +1 -1
  371. package/{stdlib → runtime/managed}/managed-runtime-value-internals.eli +3 -3
  372. package/{stdlib → runtime/managed}/managed-runtime-value.eli +2 -2
  373. package/{stdlib → runtime/managed}/managed-runtime-vector.eli +5 -2
  374. package/{dist/literal-sources/stdlib → runtime/managed}/managed-sorted-state.eli +1 -1
  375. package/runtime/managed/managed-transient-state.eli +1206 -0
  376. package/{dist/literal-sources/stdlib → runtime/managed}/managed-value-state.eli +30 -6
  377. package/{stdlib → runtime/managed}/managed-vector-view-state.eli +1 -1
  378. package/{stdlib → runtime/managed}/managed-wire-http-rpc.eli +1 -1
  379. package/{stdlib → runtime/managed}/managed-wire-markdown.eli +1 -1
  380. package/{stdlib → runtime/managed}/managed-wire-schema.eli +1 -1
  381. package/runtime/math.mjs +32 -38
  382. package/runtime/math.mjs.map +1 -1
  383. package/runtime/number-radix.mjs +5 -7
  384. package/runtime/number-radix.mjs.map +1 -1
  385. package/runtime/primitives.eli +19 -6
  386. package/runtime/primitives.mjs +81 -46
  387. package/runtime/primitives.mjs.map +1 -1
  388. package/runtime/source-mapping.mjs +17 -23
  389. package/runtime/source-mapping.mjs.map +1 -1
  390. package/runtime/testing/map.eli +70 -41
  391. package/runtime/testing/map.mjs +57 -34
  392. package/runtime/testing/map.mjs.map +1 -1
  393. package/runtime/unicode-case.mjs +6 -6
  394. package/runtime/unicode-case.mjs.map +1 -1
  395. package/runtime/wire/http-rpc.mjs +12 -18
  396. package/runtime/wire/http-rpc.mjs.map +1 -1
  397. package/runtime/wire/markdown.mjs +42 -42
  398. package/runtime/wire/markdown.mjs.map +1 -1
  399. package/runtime/wire/schema.mjs +13 -13
  400. package/runtime/wire/schema.mjs.map +1 -1
  401. package/runtime/worker-value-codec.mjs +5 -7
  402. package/runtime/worker-value-codec.mjs.map +1 -1
  403. package/runtime/worker-value-stream.mjs +128 -135
  404. package/runtime/worker-value-stream.mjs.map +1 -1
  405. package/runtime/worker.mjs +75 -89
  406. package/runtime/worker.mjs.map +1 -1
  407. package/stdlib/README.md +25 -18
  408. package/stdlib/async.eli +1245 -0
  409. package/stdlib/bit.eli +89 -1
  410. package/stdlib/bit.mjs +89 -2
  411. package/stdlib/bit.mjs.map +1 -1
  412. package/stdlib/{core/collection.eli → collection.eli} +39 -4
  413. package/stdlib/core.eli +956 -0
  414. package/stdlib/data-text.eli +62 -1091
  415. package/stdlib/data.eli +517 -37
  416. package/stdlib/devtools.eli +399 -0
  417. package/stdlib/diff.eli +134 -0
  418. package/stdlib/{function.eli → fn.eli} +1 -1
  419. package/stdlib/identifier.eli +21 -136
  420. package/stdlib/interop/object.eli +39 -0
  421. package/stdlib/{core/lazy-sequence.eli → lazy-sequence.eli} +28 -2
  422. package/stdlib/math.eli +267 -0
  423. package/stdlib/{core/metadata.eli → meta.eli} +2 -2
  424. package/stdlib/{core/order.eli → order.eli} +6 -6
  425. package/stdlib/persistent-list.eli +60 -21
  426. package/stdlib/persistent-map.eli +335 -324
  427. package/stdlib/persistent-set.eli +8 -6
  428. package/stdlib/persistent-vector.eli +18 -18
  429. package/stdlib/portable/data-text.eli +1092 -0
  430. package/stdlib/portable/data.eli +42 -0
  431. package/stdlib/portable/identifier.eli +147 -0
  432. package/stdlib/{metadata.eli → portable/metadata.eli} +5 -5
  433. package/stdlib/{object.eli → portable/object.eli} +1 -1
  434. package/stdlib/{sequence.eli → portable/sequence.eli} +1 -1
  435. package/stdlib/{text.eli → portable/text.eli} +1 -1
  436. package/stdlib/pprint.eli +588 -0
  437. package/stdlib/protocol.eli +63 -0
  438. package/stdlib/{core/record.eli → record.eli} +2 -2
  439. package/stdlib/regex.eli +70 -0
  440. package/stdlib/result.eli +53 -44
  441. package/stdlib/schema.eli +1886 -0
  442. package/stdlib/{core/seq.eli → seq.eli} +97 -90
  443. package/{dist/literal-sources/stdlib/core → stdlib}/set.eli +8 -8
  444. package/stdlib/{core/sorted.eli → sorted.eli} +2 -2
  445. package/stdlib/{state/atom.eli → state.eli} +137 -7
  446. package/stdlib/string.eli +362 -0
  447. package/stdlib/tap.eli +135 -0
  448. package/stdlib/test.eli +566 -0
  449. package/stdlib/time.eli +419 -0
  450. package/stdlib/{core/transducer.eli → transducer.eli} +4 -2
  451. package/stdlib/{core/transient.eli → transient.eli} +11 -5
  452. package/stdlib/{core/type.eli → type.eli} +2 -2
  453. package/stdlib/uuid.eli +288 -0
  454. package/stdlib/{core/walk.eli → walk.eli} +6 -6
  455. package/stdlib/{gepo.eli → wire/gepo.eli} +2 -2
  456. package/stdlib/{mdp.eli → wire/mdp.eli} +2 -2
  457. package/stdlib/{mds.eli → wire/mds.eli} +2 -2
  458. package/stdlib/{core/zipper.eli → zipper.eli} +179 -138
  459. package/tests/helpers/wasm-managed-math-node.mjs +495 -582
  460. package/tools/acceptance/check-ground.mjs +106 -132
  461. package/tools/acceptance/check.mjs +16 -16
  462. package/tools/acceptance/retained-performance-snapshot.mjs +265 -149
  463. package/tools/acceptance/retained-soak.mjs +5 -7
  464. package/tools/acceptance/retained-transient.mjs +5 -7
  465. package/tools/backends/build.mjs +373 -95
  466. package/tools/backends/compiler-bootstrap.mjs +271 -317
  467. package/tools/backends/compiler-browser.mjs +7 -9
  468. package/tools/backends/compiler-build.mjs +77 -97
  469. package/tools/backends/compiler-cli-ground.mjs +47 -47
  470. package/tools/backends/compiler-data-transport.mjs +27 -31
  471. package/tools/backends/compiler-host-iteration.mjs +13 -21
  472. package/tools/backends/compiler-host-record.mjs +28 -34
  473. package/tools/backends/compiler-metadata.mjs +145 -53
  474. package/tools/backends/compiler-package-ground.mjs +39 -47
  475. package/tools/backends/compiler-package-loader.mjs +164 -206
  476. package/tools/backends/compiler-session.mjs +432 -324
  477. package/tools/backends/compiler-worker.mjs +79 -89
  478. package/tools/backends/dist/bootstrap-js-stages.mjs +17 -19
  479. package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -1
  480. package/tools/backends/dist/managed-ir-inventory.mjs +1 -1
  481. package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -1
  482. package/tools/backends/dist/release-cli.mjs +151 -22
  483. package/tools/backends/dist/release-cli.mjs.map +1 -1
  484. package/tools/backends/dist/run-artifact.mjs +54 -76
  485. package/tools/backends/dist/run-artifact.mjs.map +1 -1
  486. package/tools/backends/managed-foreign-binding.mjs +370 -114
  487. package/tools/backends/managed-host-ground.mjs +14 -17
  488. package/tools/backends/managed-host-lifecycle.mjs +42 -53
  489. package/tools/backends/managed-host-object-realm.mjs +121 -111
  490. package/tools/backends/managed-host-proxy.mjs +5 -7
  491. package/tools/backends/managed-host-runtime.mjs +6295 -4350
  492. package/tools/backends/managed-host.mjs +234 -188
  493. package/tools/backends/managed-layout.mjs +53 -56
  494. package/tools/backends/managed-module-graph-ground.mjs +488 -568
  495. package/tools/backends/managed-module-graph.mjs +62 -76
  496. package/tools/backends/managed-runtime-host-ground.mjs +352 -400
  497. package/tools/backends/managed-runtime.mjs +227 -266
  498. package/tools/backends/managed-source-view-ground.mjs +165 -161
  499. package/tools/backends/managed-source-view.mjs +415 -154
  500. package/tools/backends/native-managed-runtime.mjs +96 -138
  501. package/tools/backends/native-memory.mjs +1122 -742
  502. package/tools/backends/native-node-ffi.mjs +2439 -0
  503. package/tools/backends/native-node-memory.mjs +793 -383
  504. package/tools/backends/native-node-runner.mjs +1458 -579
  505. package/tools/backends/native-node-watchdog.mjs +12 -14
  506. package/tools/backends/native-node-worker.mjs +46 -50
  507. package/tools/backends/stage3-targets.mjs +2 -2
  508. package/tools/bootstrap-runtime.el +4 -4
  509. package/tools/collections/transient-builder-benchmark.mjs +17 -17
  510. package/tools/project/check-cli.eli +27 -3
  511. package/tools/project/project-cli.eli +1 -1
  512. package/tools/testing/maintained-suite-source.mjs +5 -7
  513. package/tools/testing/maintained-suite-source.mjs.map +1 -1
  514. package/dist/literal-sources/stdlib/core/data.eli +0 -306
  515. package/dist/literal-sources/stdlib/managed-transient-state.eli +0 -353
  516. package/stdlib/core/data-text.eli +0 -16
  517. package/stdlib/core/data.eli +0 -306
  518. package/stdlib/core/identifier.eli +0 -32
  519. package/stdlib/core/regex.eli +0 -17
  520. package/stdlib/deferred.eli +0 -70
  521. package/stdlib/managed-runtime-lazy-sequence.eli +0 -157
  522. package/stdlib/managed-transient-state.eli +0 -353
  523. /package/{stdlib → runtime/managed}/managed-protocol-error.eli +0 -0
  524. /package/{stdlib → runtime/managed}/managed-runtime-literals.eli +0 -0
  525. /package/{stdlib → runtime/managed}/managed-runtime-truth.eli +0 -0
  526. /package/{stdlib → runtime/managed}/managed-testing-value.eli +0 -0
@@ -0,0 +1,1245 @@
1
+ (module eliscript.async
2
+ (import "../runtime/core/collection.mjs" :as Collection)
3
+ (import "../runtime/core/identifier.mjs" keyword)
4
+ (import "../runtime/core/map.mjs" EMPTY_MAP)
5
+ (import "../runtime/core/vector.mjs" persistentVector)
6
+
7
+ (defconst KW_DEFAULT (keyword "default"))
8
+ (defconst MISSING (js-object))
9
+
10
+ (defvar fallback-async-bindings EMPTY_MAP)
11
+
12
+ (defconst async-local-storage
13
+ (try
14
+ (let ((proc (js* "typeof process !== 'undefined' ? process : null")))
15
+ (if (and proc (= (value-type (get proc :getBuiltinModule)) "function"))
16
+ (let* ((mod (js-call proc :getBuiltinModule "node:async_hooks"))
17
+ (ALS (if mod (get mod :AsyncLocalStorage) nil)))
18
+ (if ALS (new ALS) nil))
19
+ nil))
20
+ (catch _err nil)))
21
+
22
+ (defun truthy? (value)
23
+ (not (or (nil? value) (undefined? value) (eq value false))))
24
+
25
+ (defun read-opt (options k1 &optional k2 fallback)
26
+ (cond
27
+ ((nullish? options) fallback)
28
+ ((Collection/isMap options)
29
+ (let ((v1 (Collection/get options (keyword k1) MISSING)))
30
+ (if (not (eq v1 MISSING))
31
+ v1
32
+ (let ((s1 (Collection/get options k1 MISSING)))
33
+ (if (not (eq s1 MISSING))
34
+ s1
35
+ (if (not (nullish? k2))
36
+ (let ((v2 (Collection/get options (keyword k2) MISSING)))
37
+ (if (not (eq v2 MISSING)) v2 (Collection/get options k2 fallback)))
38
+ fallback))))))
39
+ ((= (value-type options) "object")
40
+ (cond
41
+ ((Object/hasOwn options k1) (aref options k1))
42
+ ((and (not (nullish? k2)) (Object/hasOwn options k2)) (aref options k2))
43
+ (t fallback)))
44
+ (t fallback)))
45
+
46
+ (defun has-opt? (options key-name)
47
+ (cond
48
+ ((nullish? options) false)
49
+ ((Collection/isMap options)
50
+ (or (Collection/contains options (keyword key-name))
51
+ (Collection/contains options key-name)))
52
+ ((= (value-type options) "object")
53
+ (Object/hasOwn options key-name))
54
+ (t false)))
55
+
56
+ ;; ============================================================================
57
+ ;; Async Dynamic Bindings
58
+ ;; ============================================================================
59
+
60
+ (defun current-async-bindings ()
61
+ (if (not (nil? async-local-storage))
62
+ (let ((store (js-call async-local-storage :getStore)))
63
+ (if (undefined? store) fallback-async-bindings store))
64
+ fallback-async-bindings))
65
+
66
+ (defun merge-bindings (base extra)
67
+ (cond
68
+ ((nullish? extra) base)
69
+ ((Collection/isMap extra)
70
+ (let ((entries (js-array))
71
+ (next base)
72
+ (i 0))
73
+ (Collection/reduceKV extra
74
+ (lambda (_acc k v) (js-call entries :push (js-array k v)) nil)
75
+ nil)
76
+ (while (< i (js-length entries))
77
+ (let ((pair (aref entries i)))
78
+ (setq next (Collection/assoc next (aref pair 0) (aref pair 1))))
79
+ (setq i (1+ i)))
80
+ next))
81
+ ((= (value-type extra) "object")
82
+ (let ((ks (Object/keys extra))
83
+ (next base)
84
+ (i 0))
85
+ (while (< i (js-length ks))
86
+ (let ((k (aref ks i)))
87
+ (setq next (Collection/assoc next (keyword k) (aref extra k))))
88
+ (setq i (1+ i)))
89
+ next))
90
+ (t
91
+ (throw (new globalThis/TypeError "with-async-bindings expects a map or object")))))
92
+
93
+ (defun run-with-bindings (captured thunk &rest args)
94
+ (if (not (nil? async-local-storage))
95
+ (js-call async-local-storage :run captured (lambda () (apply thunk args)))
96
+ (let ((prev fallback-async-bindings))
97
+ (setq fallback-async-bindings captured)
98
+ (try
99
+ (let ((res (apply thunk args)))
100
+ (if (and res (= (value-type (get res :then)) "function"))
101
+ (js-call res :finally (lambda () (setq fallback-async-bindings prev)))
102
+ (progn
103
+ (setq fallback-async-bindings prev)
104
+ res)))
105
+ (catch err
106
+ (setq fallback-async-bindings prev)
107
+ (throw err))))))
108
+
109
+ (defun with-async-bindings (bindings thunk)
110
+ (unless (= (value-type thunk) "function")
111
+ (throw (new globalThis/TypeError "with-async-bindings expects a thunk function")))
112
+ (let ((merged (merge-bindings (current-async-bindings) bindings)))
113
+ (run-with-bindings merged thunk)))
114
+
115
+ ;; ============================================================================
116
+ ;; Buffers: Fixed, Dropping, Sliding
117
+ ;; ============================================================================
118
+
119
+ (defconst ChannelBuffer
120
+ (js* "(class ChannelBuffer { constructor(kind, capacity) { this.kind = kind; this.capacity = capacity; this.items = []; Object.freeze(this); } })"))
121
+
122
+ (defun buffer? (value)
123
+ (js* "value instanceof ChannelBuffer"))
124
+
125
+ (defun buffer (n)
126
+ (unless (and (Number/isSafeInteger n) (>= n 0))
127
+ (throw (new globalThis/RangeError "buffer capacity must be a non-negative safe integer")))
128
+ (new ChannelBuffer "fixed" n))
129
+
130
+ (defun dropping-buffer (n)
131
+ (unless (and (Number/isSafeInteger n) (>= n 1))
132
+ (throw (new globalThis/RangeError "dropping-buffer capacity must be >= 1")))
133
+ (new ChannelBuffer "dropping" n))
134
+
135
+ (defun sliding-buffer (n)
136
+ (unless (and (Number/isSafeInteger n) (>= n 1))
137
+ (throw (new globalThis/RangeError "sliding-buffer capacity must be >= 1")))
138
+ (new ChannelBuffer "sliding" n))
139
+
140
+ (defun buffer-count (buf)
141
+ (js-length (get buf :items)))
142
+
143
+ (defun buffer-full? (buf)
144
+ (let ((kind (get buf :kind)))
145
+ (cond
146
+ ((= kind "fixed") (>= (js-length (get buf :items)) (get buf :capacity)))
147
+ ((or (= kind "dropping") (= kind "sliding")) false)
148
+ (t t))))
149
+
150
+ (defun buffer-add! (buf val)
151
+ (let ((kind (get buf :kind))
152
+ (items (get buf :items))
153
+ (cap (get buf :capacity)))
154
+ (cond
155
+ ((= kind "dropping")
156
+ (when (< (js-length items) cap)
157
+ (js-call items :push val)))
158
+ ((= kind "sliding")
159
+ (when (>= (js-length items) cap)
160
+ (js-call items :shift))
161
+ (js-call items :push val))
162
+ (t
163
+ (js-call items :push val)))))
164
+
165
+ (defun buffer-remove! (buf)
166
+ (js-call (get buf :items) :shift))
167
+
168
+ ;; ============================================================================
169
+ ;; Channels (CSP + Transducers + Non-blocking + Alt Handlers)
170
+ ;; ============================================================================
171
+
172
+ (defconst AsyncChannel
173
+ (js* "(class AsyncChannel { constructor(buf, xform, exHandler) { this.buf = buf; this.closed = false; this.takes = []; this.puts = []; this.xform = xform || null; this.exHandler = exHandler || null; this.xformCompleted = false; } })"))
174
+
175
+ (defun chan? (value)
176
+ (js* "value instanceof AsyncChannel"))
177
+
178
+ (defun require-chan (value op)
179
+ (unless (chan? value)
180
+ (throw (new globalThis/TypeError (str op " expects a channel"))))
181
+ value)
182
+
183
+ (defun default-ex-handler (err)
184
+ nil)
185
+
186
+ (defun make-channel-rf (ch)
187
+ (lambda (&rest args)
188
+ (let ((argc (js-length args)))
189
+ (cond
190
+ ((= argc 0) (get ch :buf))
191
+ ((= argc 1) (aref args 0))
192
+ (t
193
+ (buffer-add! (get ch :buf) (aref args 1))
194
+ (aref args 0))))))
195
+
196
+ (defun chan (&optional buf-or-n xform ex-handler)
197
+ (let ((buf nil))
198
+ (cond
199
+ ((nullish? buf-or-n) (setq buf nil))
200
+ ((Number/isSafeInteger buf-or-n)
201
+ (when (< buf-or-n 0)
202
+ (throw (new globalThis/RangeError "channel buffer size must be >= 0")))
203
+ (when (> buf-or-n 0)
204
+ (setq buf (buffer buf-or-n))))
205
+ ((buffer? buf-or-n) (setq buf buf-or-n))
206
+ (t (throw (new globalThis/TypeError "chan buffer must be nil, integer, or Buffer"))))
207
+ (when (and (not (nullish? xform)) (not (= (value-type xform) "function")))
208
+ (throw (new globalThis/TypeError "chan xform must be a transducer function")))
209
+ (when (and (not (nullish? ex-handler)) (not (= (value-type ex-handler) "function")))
210
+ (throw (new globalThis/TypeError "chan ex-handler must be a function")))
211
+ (when (and (not (nullish? xform)) (nil? buf))
212
+ ;; Transducers require a buffer to support 0-to-many expansions.
213
+ (setq buf (buffer 1)))
214
+ (let ((ch (new AsyncChannel buf nil
215
+ (if (nullish? ex-handler) default-ex-handler ex-handler))))
216
+ (when (not (nullish? xform))
217
+ (let ((step (funcall xform (make-channel-rf ch))))
218
+ (put ch :xform step)))
219
+ ch)))
220
+
221
+ (defun closed? (ch)
222
+ (get (require-chan ch "closed?") :closed))
223
+
224
+ (defun handler-active? (h)
225
+ (get (get h :lock) :active))
226
+
227
+ (defun commit-handler (h)
228
+ (let ((lock (get h :lock)))
229
+ (if (get lock :active)
230
+ (progn
231
+ (put lock :active false)
232
+ t)
233
+ false)))
234
+
235
+ (defun pop-active! (queue)
236
+ (let ((found nil))
237
+ (while (and (nil? found) (> (js-length queue) 0))
238
+ (let ((h (js-call queue :shift)))
239
+ (when (handler-active? h)
240
+ (setq found h))))
241
+ found))
242
+
243
+ (defun commit-next-active! (queue)
244
+ (let ((found nil))
245
+ (while (and (nil? found) (> (js-length queue) 0))
246
+ (let ((h (js-call queue :shift)))
247
+ (when (commit-handler h)
248
+ (setq found h))))
249
+ found))
250
+
251
+ (defun invoke-waiter (waiter arg)
252
+ (let ((bindings (get waiter :bindings))
253
+ (cb (get waiter :fn)))
254
+ (if (not (nil? bindings))
255
+ (run-with-bindings bindings cb arg)
256
+ (funcall cb arg))))
257
+
258
+ (defun drain-buffer-to-takers! (ch)
259
+ (let ((buf (get ch :buf)))
260
+ (when (not (nil? buf))
261
+ (let ((keep-going t))
262
+ (while (and keep-going (> (buffer-count buf) 0))
263
+ (let ((taker (commit-next-active! (get ch :takes))))
264
+ (if (nil? taker)
265
+ (setq keep-going false)
266
+ (invoke-waiter taker (buffer-remove! buf)))))))))
267
+
268
+ (defun run-xform-step (ch val)
269
+ (let ((xf (get ch :xform))
270
+ (exh (get ch :exHandler))
271
+ (should-close false))
272
+ (try
273
+ (let ((res (funcall xf (get ch :buf) val)))
274
+ (when (Collection/isReduced res)
275
+ (setq should-close t)))
276
+ (catch err
277
+ (let ((recovered (funcall exh err)))
278
+ (unless (nullish? recovered)
279
+ (buffer-add! (get ch :buf) recovered)))))
280
+ (drain-buffer-to-takers! ch)
281
+ should-close))
282
+
283
+ (defun drain-waiters-on-close! (ch)
284
+ (let ((takes (get ch :takes))
285
+ (puts (get ch :puts)))
286
+ (while (> (js-length takes) 0)
287
+ (let ((taker (commit-next-active! takes)))
288
+ (unless (nil? taker)
289
+ (invoke-waiter taker nil))))
290
+ (while (> (js-length puts) 0)
291
+ (let ((putter (commit-next-active! puts)))
292
+ (unless (nil? putter)
293
+ (invoke-waiter putter false))))))
294
+
295
+ (defun close! (ch)
296
+ (require-chan ch "close!")
297
+ (unless (get ch :closed)
298
+ (put ch :closed t)
299
+ (let ((xf (get ch :xform))
300
+ (buf (get ch :buf)))
301
+ (when (and (not (nil? xf)) (not (get ch :xformCompleted)))
302
+ (put ch :xformCompleted t)
303
+ (try
304
+ (funcall xf buf)
305
+ (catch err
306
+ (let ((recovered (funcall (get ch :exHandler) err)))
307
+ (unless (nullish? recovered)
308
+ (buffer-add! buf recovered))))))
309
+ (drain-buffer-to-takers! ch))
310
+ (drain-waiters-on-close! ch))
311
+ nil)
312
+
313
+ (defun pump-buffer! (ch)
314
+ (let ((buf (get ch :buf)))
315
+ (when (and (not (nil? buf)) (not (get ch :closed)))
316
+ (let ((puts (get ch :puts)))
317
+ (while (and (not (get ch :closed))
318
+ (not (buffer-full? buf))
319
+ (> (js-length puts) 0))
320
+ (let ((putter (commit-next-active! puts)))
321
+ (unless (nil? putter)
322
+ (let ((val (get putter :val)))
323
+ (if (nil? (get ch :xform))
324
+ (progn
325
+ (buffer-add! buf val)
326
+ (invoke-waiter putter t)
327
+ (drain-buffer-to-takers! ch))
328
+ (let ((stop (run-xform-step ch val)))
329
+ (invoke-waiter putter t)
330
+ (when stop (close! ch))))))))))))
331
+
332
+ (defun try-put-with-lock! (ch val putter)
333
+ (require-chan ch "put!")
334
+ (when (nullish? val)
335
+ (throw (new globalThis/TypeError "cannot put nil or undefined onto a channel")))
336
+ (cond
337
+ ((get ch :closed)
338
+ (if (commit-handler putter)
339
+ (js-object :done t :accepted false)
340
+ (js-object :done t :accepted false)))
341
+ ((and (nil? (get ch :buf)) (nil? (get ch :xform)))
342
+ ;; Unbuffered direct rendezvous
343
+ (let ((taker (pop-active! (get ch :takes))))
344
+ (if (nil? taker)
345
+ (js-object :done false)
346
+ (if (commit-handler putter)
347
+ (if (commit-handler taker)
348
+ (progn
349
+ (invoke-waiter taker val)
350
+ (js-object :done t :accepted t))
351
+ ;; Rare concurrent alts taker consumed right between pop & commit; retry take queue
352
+ (progn
353
+ ;; Put back lock is already spent only for taker, putter still active — re-enable lock for retry
354
+ (put (get putter :lock) :active t)
355
+ (try-put-with-lock! ch val putter)))
356
+ (progn
357
+ ;; Our putter lock was already taken concurrently
358
+ (js-call (get ch :takes) :unshift taker)
359
+ (js-object :done t :accepted false))))))
360
+ (t
361
+ (let ((buf (get ch :buf)))
362
+ (if (buffer-full? buf)
363
+ (js-object :done false)
364
+ (if (commit-handler putter)
365
+ (progn
366
+ (if (nil? (get ch :xform))
367
+ (if (and (= (buffer-count buf) 0) (> (js-length (get ch :takes)) 0))
368
+ (let ((taker (commit-next-active! (get ch :takes))))
369
+ (if (not (nil? taker))
370
+ (invoke-waiter taker val)
371
+ (buffer-add! buf val)))
372
+ (progn
373
+ (buffer-add! buf val)
374
+ (drain-buffer-to-takers! ch)))
375
+ (when (run-xform-step ch val)
376
+ (close! ch)))
377
+ (js-object :done t :accepted t))
378
+ (js-object :done t :accepted false)))))))
379
+
380
+ (defun try-take-with-lock! (ch taker)
381
+ (require-chan ch "take!")
382
+ (let ((buf (get ch :buf)))
383
+ (cond
384
+ ((and (not (nil? buf)) (> (buffer-count buf) 0))
385
+ (if (commit-handler taker)
386
+ (let ((val (buffer-remove! buf)))
387
+ (pump-buffer! ch)
388
+ (js-object :done t :value val))
389
+ (js-object :done t :value nil)))
390
+ ((get ch :closed)
391
+ (if (commit-handler taker)
392
+ (js-object :done t :value nil)
393
+ (js-object :done t :value nil)))
394
+ (t
395
+ ;; Try taking directly from a waiting putter
396
+ (let ((found false)
397
+ (res-val nil))
398
+ (while (and (not found) (> (js-length (get ch :puts)) 0))
399
+ (let ((putter (pop-active! (get ch :puts))))
400
+ (when (and (not (nil? putter)) (commit-handler taker))
401
+ (if (commit-handler putter)
402
+ (progn
403
+ (setq found t res-val (get putter :val))
404
+ (invoke-waiter putter t))
405
+ (put (get taker :lock) :active t)))))
406
+ (if found
407
+ (js-object :done t :value res-val)
408
+ (js-object :done false)))))))
409
+
410
+ (defun make-waiter (lock fn &optional val)
411
+ (js-object :lock lock :fn fn :val val :bindings (current-async-bindings)))
412
+
413
+ (defun offer! (ch val)
414
+ (require-chan ch "offer!")
415
+ (when (nullish? val)
416
+ (throw (new globalThis/TypeError "cannot offer nil or undefined onto a channel")))
417
+ (let* ((lock (js-object :active t))
418
+ (waiter (make-waiter lock (lambda (_v) nil) val))
419
+ (res (try-put-with-lock! ch val waiter)))
420
+ (if (get res :done) (get res :accepted) false)))
421
+
422
+ (defun poll! (ch)
423
+ (require-chan ch "poll!")
424
+ (let* ((lock (js-object :active t))
425
+ (waiter (make-waiter lock (lambda (_v) nil)))
426
+ (res (try-take-with-lock! ch waiter)))
427
+ (if (get res :done) (get res :value) nil)))
428
+
429
+ (defun put! (ch val &optional on-complete)
430
+ (require-chan ch "put!")
431
+ (when (nullish? val)
432
+ (throw (new globalThis/TypeError "cannot put nil or undefined onto a channel")))
433
+ (new globalThis/Promise
434
+ (lambda (resolve)
435
+ (let* ((lock (js-object :active t))
436
+ (done-fn (lambda (accepted)
437
+ (unless (nullish? on-complete)
438
+ (funcall on-complete accepted))
439
+ (funcall resolve accepted)))
440
+ (waiter (make-waiter lock done-fn val))
441
+ (res (try-put-with-lock! ch val waiter)))
442
+ (if (get res :done)
443
+ (funcall done-fn (get res :accepted))
444
+ (js-call (get ch :puts) :push waiter))))))
445
+
446
+ (defun take! (ch &optional on-complete)
447
+ (require-chan ch "take!")
448
+ (new globalThis/Promise
449
+ (lambda (resolve)
450
+ (let* ((lock (js-object :active t))
451
+ (done-fn (lambda (val)
452
+ (unless (nullish? on-complete)
453
+ (funcall on-complete val))
454
+ (funcall resolve val)))
455
+ (waiter (make-waiter lock done-fn))
456
+ (res (try-take-with-lock! ch waiter)))
457
+ (if (get res :done)
458
+ (funcall done-fn (get res :value))
459
+ (js-call (get ch :takes) :push waiter))))))
460
+
461
+ (defun timeout (ms)
462
+ (let ((dur (Math/max 0 (if (nullish? ms) 0 ms)))
463
+ (ch (chan)))
464
+ (globalThis/setTimeout (lambda () (close! ch)) dur)
465
+ ch))
466
+
467
+ (defun to-chan! (coll)
468
+ (let ((items (js-array)))
469
+ (cond
470
+ ((nullish? coll) nil)
471
+ ((Array/isArray coll)
472
+ (let ((i 0))
473
+ (while (< i (js-length coll))
474
+ (js-call items :push (aref coll i))
475
+ (setq i (1+ i)))))
476
+ ((Collection/isSeqable coll)
477
+ (Collection/reduce coll (lambda (_a v) (js-call items :push v) nil) nil))
478
+ (t
479
+ (throw (new globalThis/TypeError "to-chan! expects a seqable collection or array"))))
480
+ (let ((ch (chan (Math/max 1 (js-length items))))
481
+ (i 0))
482
+ (while (< i (js-length items))
483
+ (offer! ch (aref items i))
484
+ (setq i (1+ i)))
485
+ (close! ch)
486
+ ch)))
487
+
488
+ (defasync onto-chan! (ch coll &optional close?)
489
+ (let ((should-close (if (nullish? close?) t (truthy? close?)))
490
+ (items (js-array)))
491
+ (cond
492
+ ((nullish? coll) nil)
493
+ ((Array/isArray coll) (setq items coll))
494
+ ((Collection/isSeqable coll)
495
+ (Collection/reduce coll (lambda (_a v) (js-call items :push v) nil) nil))
496
+ (t
497
+ (throw (new globalThis/TypeError "onto-chan! expects a seqable collection or array"))))
498
+ (let ((i 0)
499
+ (open t))
500
+ (while (and open (< i (js-length items)))
501
+ (setq open (await (put! ch (aref items i))))
502
+ (setq i (1+ i))))
503
+ (when should-close
504
+ (close! ch))
505
+ ch))
506
+
507
+ ;; ============================================================================
508
+ ;; Multiplexing: alts!
509
+ ;; ============================================================================
510
+
511
+ (defun normalize-alts-ports (ports)
512
+ (let ((raw (cond
513
+ ((Array/isArray ports) ports)
514
+ ((Collection/isSeqable ports)
515
+ (let ((arr (js-array)))
516
+ (Collection/reduce ports (lambda (_a p) (js-call arr :push p) nil) nil)
517
+ arr))
518
+ (t (throw (new globalThis/TypeError "alts! expects an array or vector of ports"))))))
519
+ (when (= (js-length raw) 0)
520
+ (throw (new globalThis/RangeError "alts! requires at least one port")))
521
+ (js-call raw :map
522
+ (lambda (entry)
523
+ (cond
524
+ ((chan? entry) (js-object :kind "take" :ch entry :port entry))
525
+ ((or (Array/isArray entry) (Collection/isVector entry))
526
+ (let* ((arr (if (Array/isArray entry)
527
+ entry
528
+ (let ((tmp (js-array)))
529
+ (Collection/reduce entry (lambda (_a v) (js-call tmp :push v) nil) nil)
530
+ tmp)))
531
+ (_check (unless (= (js-length arr) 2)
532
+ (throw (new globalThis/TypeError "alts! put port must be [ch val]"))))
533
+ (ch (require-chan (aref arr 0) "alts!"))
534
+ (val (aref arr 1)))
535
+ (when (nullish? val)
536
+ (throw (new globalThis/TypeError "alts! cannot put nil or undefined onto channel")))
537
+ (js-object :kind "put" :ch ch :val val :port entry)))
538
+ (t
539
+ (throw (new globalThis/TypeError "alts! port must be channel or [channel value]"))))))))
540
+
541
+ (defun shuffle-indices (n)
542
+ (let ((indices (js-array))
543
+ (i 0))
544
+ (while (< i n)
545
+ (js-call indices :push i)
546
+ (setq i (1+ i)))
547
+ (let ((j (1- n)))
548
+ (while (> j 0)
549
+ (let* ((k (Math/floor (* (Math/random) (1+ j))))
550
+ (tmp (aref indices j)))
551
+ (put indices j (aref indices k))
552
+ (put indices k tmp))
553
+ (setq j (1- j))))
554
+ indices))
555
+
556
+ (defun alts! (ports &optional options)
557
+ (let* ((normalized (normalize-alts-ports ports))
558
+ (priority? (truthy? (read-opt options "priority" nil false)))
559
+ (has-default? (has-opt? options "default"))
560
+ (default-val (if has-default? (read-opt options "default" nil nil) nil))
561
+ (n (js-length normalized))
562
+ (order (if priority?
563
+ (let ((arr (js-array)) (i 0))
564
+ (while (< i n) (js-call arr :push i) (setq i (1+ i)))
565
+ arr)
566
+ (shuffle-indices n))))
567
+ (new globalThis/Promise
568
+ (lambda (resolve)
569
+ (let ((lock (js-object :active t))
570
+ (settled false)
571
+ (idx 0))
572
+ (while (and (not settled) (< idx n))
573
+ (let* ((spec (aref normalized (aref order idx)))
574
+ (ch (get spec :ch))
575
+ (port (get spec :port)))
576
+ (if (= (get spec :kind) "take")
577
+ (let* ((waiter (make-waiter lock (lambda (v) (funcall resolve (persistentVector v ch)))))
578
+ (res (try-take-with-lock! ch waiter)))
579
+ (when (get res :done)
580
+ (setq settled t)
581
+ (funcall resolve (persistentVector (get res :value) ch))))
582
+ (let* ((waiter (make-waiter lock (lambda (ok) (funcall resolve (persistentVector ok ch))) (get spec :val)))
583
+ (res (try-put-with-lock! ch (get spec :val) waiter)))
584
+ (when (get res :done)
585
+ (setq settled t)
586
+ (funcall resolve (persistentVector (get res :accepted) ch)))
587
+ ;; Keep original port ref unused note: standard alts returns winning channel (ch)
588
+ (unless (nullish? port) nil))))
589
+ (setq idx (1+ idx)))
590
+ (when (not settled)
591
+ (if (and has-default? (commit-handler (js-object :lock lock)))
592
+ (funcall resolve (persistentVector default-val KW_DEFAULT))
593
+ (let ((pi 0))
594
+ (while (< pi n)
595
+ (let* ((spec (aref normalized (aref order pi)))
596
+ (ch (get spec :ch)))
597
+ (if (= (get spec :kind) "take")
598
+ (js-call (get ch :takes) :push
599
+ (make-waiter lock (lambda (v) (funcall resolve (persistentVector v ch)))))
600
+ (js-call (get ch :puts) :push
601
+ (make-waiter lock (lambda (ok) (funcall resolve (persistentVector ok ch))) (get spec :val)))))
602
+ (setq pi (1+ pi)))))))))))
603
+
604
+ ;; ============================================================================
605
+ ;; Structured Concurrency Scopes
606
+ ;; ============================================================================
607
+
608
+ (defconst AsyncScope
609
+ (js* "(class AsyncScope { constructor() { this.cancelled = false; this.reason = null; this.tasks = []; this.channels = []; this.listeners = []; this.aborted = typeof AbortController === 'function' ? new AbortController() : null; } })"))
610
+
611
+ (defun scope-cancelled? (scope)
612
+ (get scope :cancelled))
613
+
614
+ (defun scope-signal (scope)
615
+ (let ((ctrl (get scope :aborted)))
616
+ (if (nil? ctrl) nil (get ctrl :signal))))
617
+
618
+ (defun scope-cancel! (scope &optional reason)
619
+ (unless (get scope :cancelled)
620
+ (put scope :cancelled t)
621
+ (put scope :reason (if (nullish? reason) (new globalThis/Error "Async scope cancelled") reason))
622
+ (let ((ctrl (get scope :aborted)))
623
+ (unless (nil? ctrl)
624
+ (js-call ctrl :abort (get scope :reason))))
625
+ (let ((channels (get scope :channels))
626
+ (i 0))
627
+ (while (< i (js-length channels))
628
+ (close! (aref channels i))
629
+ (setq i (1+ i))))
630
+ (let ((listeners (get scope :listeners))
631
+ (j 0))
632
+ (while (< j (js-length listeners))
633
+ (try (funcall (aref listeners j) (get scope :reason))
634
+ (catch _err nil))
635
+ (setq j (1+ j)))))
636
+ nil)
637
+
638
+ (defun scope-channel (scope &optional buf-or-n xform ex-handler)
639
+ (let ((ch (chan buf-or-n xform ex-handler)))
640
+ (if (get scope :cancelled)
641
+ (close! ch)
642
+ (js-call (get scope :channels) :push ch))
643
+ ch))
644
+
645
+ (defun scope-spawn (scope task-fn)
646
+ (unless (= (value-type task-fn) "function")
647
+ (throw (new globalThis/TypeError "scope-spawn expects a function")))
648
+ (let ((captured (current-async-bindings)))
649
+ (if (get scope :cancelled)
650
+ (Promise/resolve nil)
651
+ (let ((p (Promise/resolve)))
652
+ (let ((task-promise
653
+ (js-call
654
+ (js-call p :then
655
+ (lambda ()
656
+ (when (get scope :cancelled)
657
+ (throw (get scope :reason)))
658
+ (run-with-bindings captured task-fn)))
659
+ :catch
660
+ (lambda (err)
661
+ (unless (get scope :cancelled)
662
+ (scope-cancel! scope err)
663
+ (put scope :firstError err))
664
+ (throw err)))))
665
+ (js-call (get scope :tasks) :push task-promise)
666
+ task-promise)))))
667
+
668
+ (defasync with-async-scope (body-fn)
669
+ (unless (= (value-type body-fn) "function")
670
+ (throw (new globalThis/TypeError "with-async-scope expects a function")))
671
+ (let ((scope (new AsyncScope))
672
+ (body-err nil)
673
+ (has-body-err false)
674
+ (result nil))
675
+ (try
676
+ (setq result (await (funcall body-fn scope)))
677
+ (catch err
678
+ (setq body-err err has-body-err t)
679
+ (scope-cancel! scope err)))
680
+ (let ((settled (await (Promise/allSettled (get scope :tasks)))))
681
+ ;; Always close owned channels when scope exits so no waiters leak.
682
+ (let ((channels (get scope :channels))
683
+ (ci 0))
684
+ (while (< ci (js-length channels))
685
+ (close! (aref channels ci))
686
+ (setq ci (1+ ci))))
687
+ (when has-body-err
688
+ (throw body-err))
689
+ (unless (nil? (get scope :firstError))
690
+ (throw (get scope :firstError)))
691
+ (let ((i 0))
692
+ (while (< i (js-length settled))
693
+ (let ((s (aref settled i)))
694
+ (when (= (get s :status) "rejected")
695
+ (throw (get s :reason))))
696
+ (setq i (1+ i)))))
697
+ result))
698
+
699
+ ;; ============================================================================
700
+ ;; Channel Composition Combinators
701
+ ;; ============================================================================
702
+
703
+ (defun pipe (from to &optional close?)
704
+ (let ((should-close (if (nullish? close?) t (truthy? close?))))
705
+ (js-call
706
+ ((lambda ()
707
+ (let ((step nil))
708
+ (setq step
709
+ (lambda ()
710
+ (js-call (take! from) :then
711
+ (lambda (v)
712
+ (if (nil? v)
713
+ (when should-close (close! to))
714
+ (js-call (put! to v) :then
715
+ (lambda (ok)
716
+ (when ok (funcall step)))))))))
717
+ (funcall step))))
718
+ :catch (lambda (_err) nil))
719
+ to))
720
+
721
+ (defun run-xf-to-array (xf ex-handler v)
722
+ (let ((out (js-array))
723
+ (step-fn (funcall xf (lambda (&rest args)
724
+ (let ((argc (js-length args)))
725
+ (cond
726
+ ((= argc 0) (js-array))
727
+ ((= argc 1) (aref args 0))
728
+ (t
729
+ (js-call (aref args 0) :push (aref args 1))
730
+ (aref args 0))))))))
731
+ (try
732
+ (let ((r1 (funcall step-fn out v)))
733
+ (let ((unr (if (Collection/isReduced r1) (Collection/unreduced r1) r1)))
734
+ (funcall step-fn unr)))
735
+ (catch err
736
+ (let ((recovered (funcall ex-handler err)))
737
+ (unless (nullish? recovered)
738
+ (js-call out :push recovered)))))
739
+ out))
740
+
741
+ (defun host-map? (x)
742
+ (js* "x instanceof globalThis.Map"))
743
+
744
+ (defun pipeline* (mode parallelism to fn-or-xf from close? ex-handler)
745
+ (unless (and (Number/isSafeInteger parallelism) (>= parallelism 1))
746
+ (throw (new globalThis/RangeError "pipeline parallelism n must be >= 1")))
747
+ (let* ((should-close (if (nullish? close?) t (truthy? close?)))
748
+ (exh (if (nullish? ex-handler) default-ex-handler ex-handler))
749
+ (jobs (chan parallelism))
750
+ (results (chan parallelism))
751
+ (stopped false))
752
+ ;; Feeder task
753
+ ((lambda ()
754
+ (let ((feed-step nil))
755
+ (setq feed-step
756
+ (lambda ()
757
+ (unless stopped
758
+ (js-call (take! from) :then
759
+ (lambda (v)
760
+ (if (or stopped (nil? v))
761
+ (progn
762
+ (close! jobs)
763
+ (close! results))
764
+ (let ((res-ch (chan 1)))
765
+ (js-call (put! jobs (js-object :val v :res res-ch)) :then
766
+ (lambda (ok1)
767
+ (if ok1
768
+ (js-call (put! results res-ch) :then
769
+ (lambda (ok2)
770
+ (when ok2 (funcall feed-step))))
771
+ (close! res-ch)))))))))))
772
+ (funcall feed-step))))
773
+ ;; Spawn n bounded workers
774
+ (let ((wi 0))
775
+ (while (< wi parallelism)
776
+ ((lambda ()
777
+ (let ((work-step nil))
778
+ (setq work-step
779
+ (lambda ()
780
+ (js-call (take! jobs) :then
781
+ (lambda (job)
782
+ (unless (nil? job)
783
+ (let ((val (get job :val))
784
+ (res-ch (get job :res)))
785
+ (if (= mode "sync")
786
+ (let ((items (run-xf-to-array fn-or-xf exh val)))
787
+ (js-call (onto-chan! res-ch items t) :then
788
+ (lambda () (funcall work-step))))
789
+ (try
790
+ (let ((maybe-p (funcall fn-or-xf val res-ch)))
791
+ (if (and maybe-p (= (value-type (get maybe-p :then)) "function"))
792
+ (js-call maybe-p :then
793
+ (lambda () (funcall work-step))
794
+ (lambda () (close! res-ch) (funcall work-step)))
795
+ (funcall work-step)))
796
+ (catch _err
797
+ (close! res-ch)
798
+ (funcall work-step))))))))))
799
+ (funcall work-step))))
800
+ (setq wi (1+ wi))))
801
+ ;; Ordered collector writing to `to`
802
+ ((lambda ()
803
+ (let ((collect-outer nil)
804
+ (drain-res nil))
805
+ (setq drain-res
806
+ (lambda (res-ch)
807
+ (js-call (take! res-ch) :then
808
+ (lambda (item)
809
+ (if (nil? item)
810
+ (funcall collect-outer)
811
+ (js-call (put! to item) :then
812
+ (lambda (ok)
813
+ (if ok
814
+ (funcall drain-res res-ch)
815
+ (progn
816
+ (setq stopped t)
817
+ (close! jobs)
818
+ (close! results))))))))))
819
+ (setq collect-outer
820
+ (lambda ()
821
+ (js-call (take! results) :then
822
+ (lambda (res-ch)
823
+ (if (nil? res-ch)
824
+ (when should-close (close! to))
825
+ (funcall drain-res res-ch))))))
826
+ (funcall collect-outer))))
827
+ to))
828
+
829
+ (defun pipeline (n to xf from &optional close? ex-handler)
830
+ (unless (= (value-type xf) "function")
831
+ (throw (new globalThis/TypeError "pipeline expects a transducer function")))
832
+ (pipeline* "sync" n to xf from close? ex-handler))
833
+
834
+ (defun pipeline-async (n to af from &optional close?)
835
+ (unless (= (value-type af) "function")
836
+ (throw (new globalThis/TypeError "pipeline-async expects an async function")))
837
+ (pipeline* "async" n to af from close? nil))
838
+
839
+ (defun merge (chs &optional buf-or-n)
840
+ (let* ((arr (cond
841
+ ((Array/isArray chs) chs)
842
+ ((Collection/isSeqable chs)
843
+ (let ((tmp (js-array)))
844
+ (Collection/reduce chs (lambda (_a c) (js-call tmp :push c) nil) nil)
845
+ tmp))
846
+ (t (throw (new globalThis/TypeError "merge expects an array or collection of channels")))))
847
+ (out (chan buf-or-n))
848
+ (remaining (js-length arr)))
849
+ (if (= remaining 0)
850
+ (close! out)
851
+ (let ((i 0))
852
+ (while (< i (js-length arr))
853
+ (let ((src (require-chan (aref arr i) "merge")))
854
+ ((lambda (ch-src)
855
+ (let ((pump nil))
856
+ (setq pump
857
+ (lambda ()
858
+ (js-call (take! ch-src) :then
859
+ (lambda (v)
860
+ (if (nil? v)
861
+ (progn
862
+ (setq remaining (1- remaining))
863
+ (when (= remaining 0)
864
+ (close! out)))
865
+ (js-call (put! out v) :then
866
+ (lambda (ok)
867
+ (when ok (funcall pump)))))))))
868
+ (funcall pump)))
869
+ src))
870
+ (setq i (1+ i)))))
871
+ out))
872
+
873
+ (defun split (pred ch &optional t-buf f-buf)
874
+ (unless (= (value-type pred) "function")
875
+ (throw (new globalThis/TypeError "split expects a predicate function")))
876
+ (require-chan ch "split")
877
+ (let ((t-ch (chan t-buf))
878
+ (f-ch (chan f-buf)))
879
+ ((lambda ()
880
+ (let ((step nil))
881
+ (setq step
882
+ (lambda ()
883
+ (js-call (take! ch) :then
884
+ (lambda (v)
885
+ (if (nil? v)
886
+ (progn (close! t-ch) (close! f-ch))
887
+ (let ((target (if (truthy? (funcall pred v)) t-ch f-ch)))
888
+ (js-call (put! target v) :then
889
+ (lambda (_ok)
890
+ (unless (and (closed? t-ch) (closed? f-ch))
891
+ (funcall step))))))))))
892
+ (funcall step))))
893
+ (persistentVector t-ch f-ch)))
894
+
895
+ ;; ============================================================================
896
+ ;; Broadcast: mult / tap / untap
897
+ ;; ============================================================================
898
+
899
+ (defconst ChannelMult
900
+ (js* "(class ChannelMult { constructor(ch) { this.ch = ch; this.taps = []; this.done = false; } })"))
901
+
902
+ (defun mult? (value)
903
+ (js* "value instanceof ChannelMult"))
904
+
905
+ (defun mult (ch)
906
+ (require-chan ch "mult")
907
+ (let ((m (new ChannelMult ch)))
908
+ ((lambda ()
909
+ (let ((step nil))
910
+ (setq step
911
+ (lambda ()
912
+ (js-call (take! ch) :then
913
+ (lambda (v)
914
+ (if (nil? v)
915
+ (progn
916
+ (put m :done t)
917
+ (let ((taps (js-call (get m :taps) :slice))
918
+ (i 0))
919
+ (while (< i (js-length taps))
920
+ (let ((entry (aref taps i)))
921
+ (when (get entry :close)
922
+ (close! (get entry :ch))))
923
+ (setq i (1+ i)))))
924
+ (let ((taps (js-call (get m :taps) :slice)))
925
+ (if (= (js-length taps) 0)
926
+ (funcall step)
927
+ (let ((puts (js-array))
928
+ (i 0))
929
+ (while (< i (js-length taps))
930
+ (let ((entry (aref taps i)))
931
+ (js-call puts :push
932
+ (js-call (put! (get entry :ch) v) :then
933
+ (lambda (ok)
934
+ (unless ok
935
+ (untap m (get entry :ch)))))))
936
+ (setq i (1+ i)))
937
+ (js-call (Promise/all puts) :then (lambda () (funcall step)))))))))))
938
+ (funcall step))))
939
+ m))
940
+
941
+ (defun tap (m ch &optional close?)
942
+ (unless (mult? m)
943
+ (throw (new globalThis/TypeError "tap expects a mult")))
944
+ (require-chan ch "tap")
945
+ (let ((should-close (if (nullish? close?) t (truthy? close?))))
946
+ (if (get m :done)
947
+ (when should-close (close! ch))
948
+ (let ((taps (get m :taps))
949
+ (found false)
950
+ (i 0))
951
+ (while (< i (js-length taps))
952
+ (when (eq (get (aref taps i) :ch) ch)
953
+ (put (aref taps i) :close should-close)
954
+ (setq found t))
955
+ (setq i (1+ i)))
956
+ (unless found
957
+ (js-call taps :push (js-object :ch ch :close should-close)))))
958
+ ch))
959
+
960
+ (defun untap (m ch)
961
+ (unless (mult? m)
962
+ (throw (new globalThis/TypeError "untap expects a mult")))
963
+ (put m :taps
964
+ (js-call (get m :taps) :filter (lambda (entry) (not (eq (get entry :ch) ch)))))
965
+ nil)
966
+
967
+ (defun untap-all (m)
968
+ (unless (mult? m)
969
+ (throw (new globalThis/TypeError "untap-all expects a mult")))
970
+ (put m :taps (js-array))
971
+ nil)
972
+
973
+ ;; ============================================================================
974
+ ;; Topic Publish/Subscribe: pub / sub / unsub
975
+ ;; ============================================================================
976
+
977
+ (defconst ChannelPub
978
+ (js* "(class ChannelPub { constructor(ch, topicFn, bufFn) { this.ch = ch; this.topicFn = topicFn; this.bufFn = bufFn; this.mults = EMPTY_MAP; this.done = false; } })"))
979
+
980
+ (defun pub? (value)
981
+ (js* "value instanceof ChannelPub"))
982
+
983
+ (defun ensure-topic-mult (p topic)
984
+ (let ((existing (Collection/get (get p :mults) topic nil)))
985
+ (if (not (nil? existing))
986
+ existing
987
+ (let* ((buf (if (nil? (get p :bufFn)) nil (funcall (get p :bufFn) topic)))
988
+ (tch (chan buf))
989
+ (m (mult tch)))
990
+ (put p :mults (Collection/assoc (get p :mults) topic (js-object :ch tch :mult m)))
991
+ (Collection/get (get p :mults) topic)))))
992
+
993
+ (defun pub (ch topic-fn &optional buf-fn)
994
+ (require-chan ch "pub")
995
+ (unless (= (value-type topic-fn) "function")
996
+ (throw (new globalThis/TypeError "pub expects a topic function")))
997
+ (let ((p (new ChannelPub ch topic-fn (if (nullish? buf-fn) nil buf-fn))))
998
+ ((lambda ()
999
+ (let ((step nil))
1000
+ (setq step
1001
+ (lambda ()
1002
+ (js-call (take! ch) :then
1003
+ (lambda (v)
1004
+ (if (nil? v)
1005
+ (progn
1006
+ (put p :done t)
1007
+ (let ((topic-chs (js-array))
1008
+ (i 0))
1009
+ (Collection/reduceKV (get p :mults)
1010
+ (lambda (_a _k entry) (js-call topic-chs :push (get entry :ch)) nil)
1011
+ nil)
1012
+ (while (< i (js-length topic-chs))
1013
+ (close! (aref topic-chs i))
1014
+ (setq i (1+ i)))))
1015
+ (let* ((topic (funcall topic-fn v))
1016
+ (entry (Collection/get (get p :mults) topic nil)))
1017
+ (if (nil? entry)
1018
+ (funcall step)
1019
+ (js-call (put! (get entry :ch) v) :then (lambda (_ok) (funcall step))))))))))
1020
+ (funcall step))))
1021
+ p))
1022
+
1023
+ (defun sub (p topic ch &optional close?)
1024
+ (unless (pub? p)
1025
+ (throw (new globalThis/TypeError "sub expects a pub")))
1026
+ (require-chan ch "sub")
1027
+ (let ((should-close (if (nullish? close?) t (truthy? close?))))
1028
+ (if (get p :done)
1029
+ (when should-close (close! ch))
1030
+ (let ((entry (ensure-topic-mult p topic)))
1031
+ (tap (get entry :mult) ch should-close)))
1032
+ ch))
1033
+
1034
+ (defun unsub (p topic ch)
1035
+ (unless (pub? p)
1036
+ (throw (new globalThis/TypeError "unsub expects a pub")))
1037
+ (let ((entry (Collection/get (get p :mults) topic nil)))
1038
+ (unless (nil? entry)
1039
+ (untap (get entry :mult) ch)))
1040
+ nil)
1041
+
1042
+ (defun unsub-all (p &optional topic)
1043
+ (unless (pub? p)
1044
+ (throw (new globalThis/TypeError "unsub-all expects a pub")))
1045
+ (if (not (nullish? topic))
1046
+ (let ((entry (Collection/get (get p :mults) topic nil)))
1047
+ (unless (nil? entry)
1048
+ (untap-all (get entry :mult))))
1049
+ (let ((all-mults (js-array))
1050
+ (i 0))
1051
+ (Collection/reduceKV (get p :mults)
1052
+ (lambda (_a _k entry) (js-call all-mults :push (get entry :mult)) nil)
1053
+ nil)
1054
+ (while (< i (js-length all-mults))
1055
+ (untap-all (aref all-mults i))
1056
+ (setq i (1+ i)))))
1057
+ nil)
1058
+
1059
+ ;; ============================================================================
1060
+ ;; Dynamic Input Mixer: mix / admix / unmix / toggle / solo-mode
1061
+ ;; ============================================================================
1062
+
1063
+ (defconst ChannelMix
1064
+ (js* "(class ChannelMix { constructor(out) { this.out = out; this.inputs = new globalThis.Map(); this.soloMode = 'mute'; this.wakeCh = null; this.running = true; } })"))
1065
+
1066
+ (defun mix? (value)
1067
+ (js* "value instanceof ChannelMix"))
1068
+
1069
+ (defun read-mix-state (opts)
1070
+ (let ((s (js-object :mute false :pause false :solo false)))
1071
+ (when (not (nullish? opts))
1072
+ (put s :mute (truthy? (read-opt opts "mute" nil false)))
1073
+ (put s :pause (truthy? (read-opt opts "pause" nil false)))
1074
+ (put s :solo (truthy? (read-opt opts "solo" nil false))))
1075
+ s))
1076
+
1077
+ (defun mix-wake! (m)
1078
+ (let ((old (get m :wakeCh)))
1079
+ (unless (nil? old)
1080
+ (close! old))
1081
+ (put m :wakeCh (chan))))
1082
+
1083
+ (defun mix-run-loop (m)
1084
+ (let ((step nil))
1085
+ (setq step
1086
+ (lambda ()
1087
+ (when (and (get m :running) (not (closed? (get m :out))))
1088
+ (let ((any-solo false)
1089
+ (solo-mode-val (get m :soloMode))
1090
+ (active-ports (js-array))
1091
+ (state-by-ch (new globalThis.Map))
1092
+ (entries (Array/from (js-call (get m :inputs) :entries)))
1093
+ (i 0))
1094
+ (while (< i (js-length entries))
1095
+ (when (get (aref (aref entries i) 1) :solo)
1096
+ (setq any-solo t))
1097
+ (setq i (1+ i)))
1098
+ (setq i 0)
1099
+ (while (< i (js-length entries))
1100
+ (let* ((pair (aref entries i))
1101
+ (ch (aref pair 0))
1102
+ (st (aref pair 1))
1103
+ (solo? (get st :solo))
1104
+ (paused? (get st :pause))
1105
+ (effectively-paused (or paused? (and any-solo (not solo?) (= solo-mode-val "pause"))))
1106
+ (effectively-muted (or (get st :mute) (and any-solo (not solo?) (= solo-mode-val "mute")))))
1107
+ (unless effectively-paused
1108
+ (js-call state-by-ch :set ch (js-object :muted effectively-muted))
1109
+ (js-call active-ports :push ch)))
1110
+ (setq i (1+ i)))
1111
+ (let ((wake (get m :wakeCh)))
1112
+ (js-call active-ports :push wake)
1113
+ (js-call (alts! active-ports) :then
1114
+ (lambda (pair)
1115
+ (let ((val (js-call pair :nth 0))
1116
+ (win-ch (js-call pair :nth 1)))
1117
+ (cond
1118
+ ((eq win-ch wake) (funcall step))
1119
+ ((nil? val)
1120
+ (js-call (get m :inputs) :delete win-ch)
1121
+ (funcall step))
1122
+ (t
1123
+ (let ((info (js-call state-by-ch :get win-ch)))
1124
+ (if (and info (get info :muted))
1125
+ (funcall step)
1126
+ (js-call (put! (get m :out) val) :then
1127
+ (lambda (ok)
1128
+ (when ok (funcall step))))))))))))))))
1129
+ (funcall step)))
1130
+
1131
+ (defun mix (out)
1132
+ (require-chan out "mix")
1133
+ (let ((m (new ChannelMix out)))
1134
+ (put m :wakeCh (chan))
1135
+ (mix-run-loop m)
1136
+ m))
1137
+
1138
+ (defun admix (m ch)
1139
+ (unless (mix? m) (throw (new globalThis/TypeError "admix expects a mix")))
1140
+ (require-chan ch "admix")
1141
+ (unless (js-call (get m :inputs) :has ch)
1142
+ (js-call (get m :inputs) :set ch (read-mix-state nil))
1143
+ (mix-wake! m))
1144
+ m)
1145
+
1146
+ (defun unmix (m ch)
1147
+ (unless (mix? m) (throw (new globalThis/TypeError "unmix expects a mix")))
1148
+ (when (js-call (get m :inputs) :delete ch)
1149
+ (mix-wake! m))
1150
+ m)
1151
+
1152
+ (defun unmix-all (m)
1153
+ (unless (mix? m) (throw (new globalThis/TypeError "unmix-all expects a mix")))
1154
+ (js-call (get m :inputs) :clear)
1155
+ (mix-wake! m)
1156
+ m)
1157
+
1158
+ (defun solo-mode (m mode)
1159
+ (unless (mix? m) (throw (new globalThis/TypeError "solo-mode expects a mix")))
1160
+ (let ((name (cond
1161
+ ((= mode "mute") "mute")
1162
+ ((= mode "pause") "pause")
1163
+ ((= (str mode) ":mute") "mute")
1164
+ ((= (str mode) ":pause") "pause")
1165
+ (t (throw (new globalThis/RangeError "solo-mode must be :mute or :pause"))))))
1166
+ (put m :soloMode name)
1167
+ (mix-wake! m))
1168
+ m)
1169
+
1170
+ (defun toggle (m state-map)
1171
+ (unless (mix? m) (throw (new globalThis/TypeError "toggle expects a mix")))
1172
+ (let ((pairs (js-array))
1173
+ (i 0))
1174
+ (cond
1175
+ ((Collection/isMap state-map)
1176
+ (Collection/reduceKV state-map
1177
+ (lambda (_a k v) (js-call pairs :push (js-array k v)) nil)
1178
+ nil))
1179
+ ((host-map? state-map)
1180
+ (js-call state-map :forEach (lambda (v k) (js-call pairs :push (js-array k v)))))
1181
+ (t (throw (new globalThis/TypeError "toggle expects a map of channels to state maps"))))
1182
+ (while (< i (js-length pairs))
1183
+ (let* ((pair (aref pairs i))
1184
+ (ch (require-chan (aref pair 0) "toggle"))
1185
+ (next-st (read-mix-state (aref pair 1))))
1186
+ (js-call (get m :inputs) :set ch next-st))
1187
+ (setq i (1+ i))))
1188
+ (mix-wake! m)
1189
+ m)
1190
+
1191
+ (export
1192
+ AsyncChannel
1193
+ AsyncScope
1194
+ ChannelBuffer
1195
+ ChannelMix
1196
+ ChannelMult
1197
+ ChannelPub
1198
+ admix
1199
+ alts!
1200
+ buffer
1201
+ buffer-count
1202
+ buffer-full?
1203
+ buffer?
1204
+ chan
1205
+ chan?
1206
+ close!
1207
+ closed?
1208
+ current-async-bindings
1209
+ dropping-buffer
1210
+ merge
1211
+ mix
1212
+ mix?
1213
+ mult
1214
+ mult?
1215
+ offer!
1216
+ onto-chan!
1217
+ pipe
1218
+ pipeline
1219
+ pipeline-async
1220
+ poll!
1221
+ pub
1222
+ pub?
1223
+ put!
1224
+ scope-cancel!
1225
+ scope-cancelled?
1226
+ scope-channel
1227
+ scope-signal
1228
+ scope-spawn
1229
+ sliding-buffer
1230
+ solo-mode
1231
+ split
1232
+ sub
1233
+ take!
1234
+ tap
1235
+ timeout
1236
+ to-chan!
1237
+ toggle
1238
+ unmix
1239
+ unmix-all
1240
+ unsub
1241
+ unsub-all
1242
+ untap
1243
+ untap-all
1244
+ with-async-bindings
1245
+ with-async-scope))