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
@@ -1,40 +1,99 @@
1
1
  (module eliscript.managed-runtime-lazy-sequence
2
- (import "./collection.mjs" MemoizedSequenceView memoizedSequenceView memoizedSequenceStatus seq)
2
+ (import "./collection.mjs" IChunk IChunkedSeq MemoizedSequenceView isChunkedSeq memoizedSequenceView memoizedSequenceStatus seq)
3
+ (import "./collection-internals.mjs"
4
+ CHUNKED_FIRST CHUNKED_MORE CHUNKED_NEXT CHUNK_COUNT CHUNK_DROP_FIRST CHUNK_NTH
5
+ COLLECTION_COUNT COLLECTION_NTH COLLECTION_REDUCE COLLECTION_SEQ UNBOUNDED_SEQUENCE
6
+ dispatchChunkCount dispatchChunkDropFirst dispatchChunkNth dispatchChunkedFirst dispatchChunkedNext
7
+ isReducedValue registerChunkedPrependHandler registerChunkedSliceHandler unreducedValue validateCollectionCount)
8
+ (import "./list.mjs" EMPTY_LIST)
9
+ (import "./protocol.mjs" implementsProtocol)
3
10
  (import "./transducer.mjs" deduping distincting dropping droppingWhile filtering interposing keeping keepingIndexed
4
11
  mapcatting mapping mappingIndexed partitioningAll partitioningBy removing sequence taking takingNth takingWhile)
5
- (import "../../stdlib/managed-literals.eli" literal-instance? literal-each
6
- literal-private-symbol literal-private-weak-map)
12
+ (import "./truth.mjs" isTruthy)
13
+ (import "../managed/managed-literals.eli" literal-instance? literal-private-symbol literal-private-weak-map)
7
14
 
8
- ;; The lazy-cons state map and the failure sentinel are created on first
9
- ;; lazyCons call: importing the module without building a lazy cons keeps no
10
- ;; resident WeakMap or symbol handle.
15
+ (defconst CHUNK_SIZE 32)
16
+ (defconst host-apply Reflect/apply)
17
+ (defconst no-arguments (Object/freeze (js-array)))
18
+ (defconst iterator-symbol (get globalThis/Symbol :iterator))
19
+
20
+ ;; WeakMaps and sentinel tokens are created lazily on first use so merely
21
+ ;; importing this module retains no extra resident handles.
11
22
  (defvar lazy-cons-states nil)
23
+ (defvar array-chunk-states nil)
24
+ (defvar chunk-buffer-states nil)
25
+ (defvar chunked-cons-states nil)
26
+ (defvar lazy-chunked-states nil)
12
27
  (defvar no-failure nil)
13
- (defun ensure-lazy-cons-states ()
14
- (or lazy-cons-states (setq lazy-cons-states (literal-private-weak-map))))
15
- (defun ensure-no-failure ()
16
- (or no-failure
17
- (setq no-failure (literal-private-symbol "eliscript.lazy-cons.no-failure"))))
28
+ (defvar NO_INITIAL nil)
29
+ (defvar ARRAY_CHUNK_TOKEN nil)
30
+ (defvar ARRAY_CHUNK_MISSING nil)
31
+ (defvar CHUNK_BUFFER_TOKEN nil)
32
+ (defvar CHUNKED_CONS_TOKEN nil)
33
+ (defvar LAZY_CHUNKED_TOKEN nil)
34
+ (defvar UNKNOWN_COUNT nil)
35
+
36
+ (defun ensure-lazy-cons-states () (or lazy-cons-states (setq lazy-cons-states (literal-private-weak-map))))
37
+ (defun ensure-array-chunk-states () (or array-chunk-states (setq array-chunk-states (literal-private-weak-map))))
38
+ (defun ensure-chunk-buffer-states () (or chunk-buffer-states (setq chunk-buffer-states (literal-private-weak-map))))
39
+ (defun ensure-chunked-cons-states () (or chunked-cons-states (setq chunked-cons-states (literal-private-weak-map))))
40
+ (defun ensure-lazy-chunked-states () (or lazy-chunked-states (setq lazy-chunked-states (literal-private-weak-map))))
41
+ (defun ensure-no-failure () (or no-failure (setq no-failure (literal-private-symbol "eliscript.lazy-cons.no-failure"))))
42
+ (defun ensure-no-initial () (or NO_INITIAL (setq NO_INITIAL (literal-private-symbol "eliscript.lazy-chunk.no-initial"))))
43
+ (defun ensure-array-chunk-token () (or ARRAY_CHUNK_TOKEN (setq ARRAY_CHUNK_TOKEN (literal-private-symbol "eliscript.array-chunk.constructor"))))
44
+ (defun ensure-array-chunk-missing () (or ARRAY_CHUNK_MISSING (setq ARRAY_CHUNK_MISSING (literal-private-symbol "eliscript.array-chunk.missing"))))
45
+ (defun ensure-chunk-buffer-token () (or CHUNK_BUFFER_TOKEN (setq CHUNK_BUFFER_TOKEN (literal-private-symbol "eliscript.chunk-buffer.constructor"))))
46
+ (defun ensure-chunked-cons-token () (or CHUNKED_CONS_TOKEN (setq CHUNKED_CONS_TOKEN (literal-private-symbol "eliscript.chunked-cons.constructor"))))
47
+ (defun ensure-lazy-chunked-token () (or LAZY_CHUNKED_TOKEN (setq LAZY_CHUNKED_TOKEN (literal-private-symbol "eliscript.lazy-chunked.constructor"))))
48
+ (defun ensure-unknown-count () (or UNKNOWN_COUNT (setq UNKNOWN_COUNT (literal-private-symbol "eliscript.chunked.unknown-count"))))
49
+
18
50
  (defun fail (message) (throw (new globalThis/TypeError message)))
51
+ (defun range-fail (message) (throw (new globalThis/RangeError message)))
19
52
  (defun callable? (value) (= (js* "typeof value") "function"))
20
- (defun truth (value) (js* "!!value"))
53
+ (defun truth (value) (funcall globalThis/Boolean value))
21
54
  (defun require-thunk (value label)
22
55
  (unless (callable? value) (fail (str label " must be a function"))) value)
23
56
  (defun require-arity (actual expected name)
24
57
  (unless (= actual expected)
25
58
  (fail (str name " requires exactly " expected (if (= expected 1) " argument" " arguments")))))
59
+ (defun require-nonnegative (value label)
60
+ (unless (and (Number/isSafeInteger value) (>= value 0))
61
+ (fail (str label " must be a non-negative safe integer"))) value)
62
+ (defun require-finite (value label)
63
+ (unless (Number/isFinite value)
64
+ (fail (str label " must be a finite number"))) value)
65
+
26
66
  (defun method (implementation name arity)
27
67
  (let ((function (funcall
28
68
  (js* "(fn, name) => { if (typeof name !== \"string\") throw new TypeError(\"method name must be a string\"); return ({ [name](...args) { return Reflect.apply(fn, undefined, [this, ...args]); } })[name]; }")
29
69
  implementation name)))
30
- (Object/defineProperty function "length" (js-object :value arity :configurable t)) function))
70
+ (Object/defineProperty function "length" (js-object :value arity :configurable t))
71
+ function))
72
+
73
+ (defun getter (implementation name)
74
+ (method implementation name 0))
75
+
76
+ (defun define-named-class (implementation name)
77
+ (funcall
78
+ (js* "(implementation, name) => { if (typeof name !== \"string\") throw new TypeError(\"class name must be a string\"); return ({ [name]: class { constructor(...args) { return implementation(this, ...args); } } })[name]; }")
79
+ implementation name))
80
+
81
+ (defun install-method (target key name arity implementation)
82
+ (Object/defineProperty target key
83
+ (js-object :value (method implementation name arity) :writable t :configurable t)))
84
+
85
+ (defun install-getter (target key name implementation)
86
+ (Object/defineProperty target key
87
+ (js-object :get (getter implementation (str "get " name)) :configurable t)))
88
+
31
89
  (defun sequence-iterator (value)
32
90
  (let ((source (seq value)))
33
91
  (if (nil? source)
34
- (let ((empty (js-array))) (js-call (aref empty (get globalThis/Symbol :iterator)) :call empty))
35
- (js-call (aref source (get globalThis/Symbol :iterator)) :call source))))
92
+ (js-call (aref (js-array) iterator-symbol) :call (js-array))
93
+ (js-call (aref source iterator-symbol) :call source))))
94
+
36
95
  (defun realize-tail (state)
37
- (unless (eq (get state :failure) no-failure) (throw (get state :failure)))
96
+ (unless (eq (get state :failure) (ensure-no-failure)) (throw (get state :failure)))
38
97
  (unless (get state :started)
39
98
  (let ((thunk (get state :rest)))
40
99
  (put state :started t)
@@ -43,9 +102,9 @@
43
102
  (put state :rest (funcall thunk))
44
103
  (catch error (put state :failure error) (throw error)))))
45
104
  (get state :rest))
105
+
46
106
  (defun cons-iterator-next (it-state)
47
- (let ((result undefined)
48
- (returned false))
107
+ (let ((result undefined) (returned false))
49
108
  (while (and (not (aref it-state 3)) (not returned))
50
109
  (let ((state (aref it-state 0)))
51
110
  (if (and (not (nil? state)) (aref it-state 1))
@@ -55,7 +114,7 @@
55
114
  (progn
56
115
  (when (not (nil? state))
57
116
  (let* ((tail (realize-tail state))
58
- (tail-state (js-call lazy-cons-states :get tail)))
117
+ (tail-state (js-call (ensure-lazy-cons-states) :get tail)))
59
118
  (if (not (undefined? tail-state))
60
119
  (progn (put it-state 0 tail-state) (put it-state 1 t))
61
120
  (progn (put it-state 0 nil) (put it-state 2 (sequence-iterator tail))))))
@@ -65,74 +124,790 @@
65
124
  (setq result next returned t)
66
125
  (put it-state 3 t))))))))
67
126
  (if returned result (js-object :value undefined :done t))))
127
+
68
128
  (defun cons-iterator-return (it-state value)
69
129
  (let ((tail-iterator (aref it-state 2)))
70
130
  (put it-state 3 t)
71
131
  (when (and (not (nil? tail-iterator)) (callable? (get tail-iterator :return)))
72
132
  (js-call tail-iterator :return))
73
133
  (js-object :value value :done t)))
134
+
74
135
  (defun cons-iterator (initial)
75
136
  (let* ((state (js-array initial t nil false))
76
137
  (iterator (js-object
77
138
  :next (method (lambda (_self) (cons-iterator-next state)) "next" 0)
78
139
  :return (method (lambda (_self value) (cons-iterator-return state value)) "return" 1))))
79
- (put iterator (get globalThis/Symbol :iterator)
80
- (method (lambda (self) self) "[Symbol.iterator]" 0))
140
+ (put iterator iterator-symbol (method (lambda (self) self) "[Symbol.iterator]" 0))
81
141
  iterator))
142
+
143
+ ;; 32-element chunked sequence infrastructure
144
+ (defvar array-chunk-prototype-ready false)
145
+ (defconst ArrayChunk
146
+ (define-named-class
147
+ (lambda (self token array off end)
148
+ (unless (and (not (nil? ARRAY_CHUNK_TOKEN)) (eq token ARRAY_CHUNK_TOKEN))
149
+ (fail "ArrayChunk values must be created with arrayChunk"))
150
+ (ensure-array-chunk-prototype)
151
+ (js-call (ensure-array-chunk-states) :set self
152
+ (Object/freeze (js-object :array array :off off :end end :count (- end off))))
153
+ (Object/freeze self))
154
+ "ArrayChunk"))
155
+
156
+ (defun chunk-state (chunk)
157
+ (if (nil? array-chunk-states) undefined (js-call array-chunk-states :get chunk)))
158
+
159
+ (defun arrayChunk (array &optional off end)
160
+ (unless (Array/isArray array)
161
+ (fail "arrayChunk expects a JavaScript Array"))
162
+ (let* ((total (js-length array))
163
+ (start (if (nullish? off) 0 (validateCollectionCount off)))
164
+ (finish (if (nullish? end) total (validateCollectionCount end))))
165
+ (when (or (> start finish) (> finish total))
166
+ (range-fail "arrayChunk bounds are outside array length"))
167
+ (let ((frozen-items (Object/freeze (js-call array :slice))))
168
+ (new ArrayChunk (ensure-array-chunk-token) frozen-items start finish))))
169
+
170
+ (defun array-chunk-owned (frozen-array off end)
171
+ (new ArrayChunk (ensure-array-chunk-token) frozen-array off end))
172
+
173
+ (defun ensure-array-chunk-prototype ()
174
+ (unless array-chunk-prototype-ready
175
+ (let ((cproto (get ArrayChunk :prototype)))
176
+ (install-getter cproto "count" "count"
177
+ (lambda (self) (get (chunk-state self) :count)))
178
+ (install-getter cproto "size" "size"
179
+ (lambda (self) (get (chunk-state self) :count)))
180
+ (install-method cproto "chunkCount" "chunkCount" 0
181
+ (lambda (self) (get (chunk-state self) :count)))
182
+ (install-method cproto "chunkNth" "chunkNth" 1
183
+ (lambda (self index &rest args)
184
+ (let* ((state (chunk-state self))
185
+ (count (get state :count))
186
+ (missing (ensure-array-chunk-missing))
187
+ (not-found (if (= (js-length args) 0) missing (aref args 0))))
188
+ (if (and (Number/isInteger index) (>= index 0) (< index count))
189
+ (aref (get state :array) (+ (get state :off) index))
190
+ (if (not (eq not-found missing))
191
+ not-found
192
+ (range-fail (str "chunk index " (funcall globalThis/String index) " is outside [0, " count ")")))))))
193
+ (install-method cproto "dropFirst" "dropFirst" 0
194
+ (lambda (self)
195
+ (let ((state (chunk-state self)))
196
+ (when (= (get state :count) 0)
197
+ (range-fail "cannot dropFirst on empty chunk"))
198
+ (array-chunk-owned (get state :array) (+ (get state :off) 1) (get state :end)))))
199
+ (install-method cproto CHUNK_COUNT "[eliscript.chunk.count]" 0
200
+ (lambda (self) (get (chunk-state self) :count)))
201
+ (install-method cproto CHUNK_NTH "[eliscript.chunk.nth]" 1
202
+ (lambda (self index &rest args)
203
+ (funcall host-apply (get cproto :chunkNth) self (js-call (js-array index) :concat args))))
204
+ (install-method cproto CHUNK_DROP_FIRST "[eliscript.chunk.dropFirst]" 0
205
+ (lambda (self) (js-call self :dropFirst)))
206
+ (install-method cproto COLLECTION_COUNT "[eliscript.collection.count]" 0
207
+ (lambda (self) (get (chunk-state self) :count)))
208
+ (install-method cproto COLLECTION_NTH "[eliscript.collection.nth]" 1
209
+ (lambda (self index &rest args)
210
+ (funcall host-apply (get cproto :chunkNth) self (js-call (js-array index) :concat args))))
211
+ (install-method cproto iterator-symbol "[Symbol.iterator]" 0
212
+ (lambda (self)
213
+ (let* ((state (chunk-state self))
214
+ (array (get state :array))
215
+ (end (get state :end))
216
+ (cursor (get state :off)))
217
+ (let ((iter (js-object :next (method
218
+ (lambda (_self)
219
+ (if (< cursor end)
220
+ (let ((value (aref array cursor)))
221
+ (setq cursor (1+ cursor))
222
+ (js-object :value value :done false))
223
+ (js-object :value undefined :done t)))
224
+ "next" 0))))
225
+ (put iter iterator-symbol (method (lambda (s) s) "[Symbol.iterator]" 0))
226
+ iter))))
227
+ (install-getter cproto (get globalThis/Symbol :toStringTag) "[Symbol.toStringTag]"
228
+ (lambda (_self) "EliscriptArrayChunk")))
229
+ (setq array-chunk-prototype-ready t)))
230
+
231
+ (defvar chunk-buffer-prototype-ready false)
232
+ (defconst ChunkBuffer
233
+ (define-named-class
234
+ (lambda (self token capacity)
235
+ (unless (and (not (nil? CHUNK_BUFFER_TOKEN)) (eq token CHUNK_BUFFER_TOKEN))
236
+ (fail "ChunkBuffer values must be created with chunkBuffer"))
237
+ (ensure-chunk-buffer-prototype)
238
+ (js-call (ensure-chunk-buffer-states) :set self
239
+ (js-object :items (js-array) :capacity capacity :consumed false))
240
+ (Object/freeze self))
241
+ "ChunkBuffer"))
242
+
243
+ (defun chunk-buffer-state (buffer)
244
+ (let ((state (if (nil? chunk-buffer-states) undefined (js-call chunk-buffer-states :get buffer))))
245
+ (when (undefined? state)
246
+ (fail "expected an Eliscript ChunkBuffer"))
247
+ state))
248
+
249
+ (defun active-buffer-state (buffer)
250
+ (let ((state (chunk-buffer-state buffer)))
251
+ (when (get state :consumed)
252
+ (fail "ChunkBuffer has already been converted to an immutable chunk"))
253
+ state))
254
+
255
+ (defun chunkBuffer (&optional capacity)
256
+ (let ((limit (if (nullish? capacity) CHUNK_SIZE (require-nonnegative capacity "chunkBuffer capacity"))))
257
+ (new ChunkBuffer (ensure-chunk-buffer-token) limit)))
258
+
259
+ (defun chunkAppend (buffer value)
260
+ (let ((state (active-buffer-state buffer)))
261
+ (when (>= (js-length (get state :items)) (get state :capacity))
262
+ (range-fail "ChunkBuffer capacity exceeded"))
263
+ (js-call (get state :items) :push value)
264
+ buffer))
265
+
266
+ (defun chunkFromBuffer (buffer)
267
+ (let* ((state (active-buffer-state buffer))
268
+ (items (get state :items))
269
+ (len (js-length items)))
270
+ (put state "consumed" t)
271
+ (put state "items" nil)
272
+ (Object/freeze items)
273
+ (array-chunk-owned items 0 len)))
274
+
275
+ (defun ensure-chunk-buffer-prototype ()
276
+ (unless chunk-buffer-prototype-ready
277
+ (let ((bproto (get ChunkBuffer :prototype)))
278
+ (install-getter bproto "count" "count"
279
+ (lambda (self)
280
+ (let ((state (chunk-buffer-state self)))
281
+ (if (get state :consumed) 0 (js-length (get state :items))))))
282
+ (install-getter bproto "size" "size"
283
+ (lambda (self) (get self :count)))
284
+ (install-method bproto "add" "add" 1
285
+ (lambda (self value) (chunkAppend self value)))
286
+ (install-method bproto "chunk" "chunk" 0
287
+ (lambda (self) (chunkFromBuffer self)))
288
+ (install-getter bproto (get globalThis/Symbol :toStringTag) "[Symbol.toStringTag]"
289
+ (lambda (_self) "EliscriptChunkBuffer")))
290
+ (setq chunk-buffer-prototype-ready t)))
291
+
292
+ (defun chunk-count-of (chunk)
293
+ (let ((direct (aref chunk CHUNK_COUNT)))
294
+ (if (callable? direct) (funcall host-apply direct chunk no-arguments) (dispatchChunkCount chunk))))
295
+
296
+ (defun chunk-nth-of (chunk index)
297
+ (let ((direct (aref chunk CHUNK_NTH)))
298
+ (if (callable? direct) (funcall host-apply direct chunk (js-array index)) (dispatchChunkNth chunk index))))
299
+
300
+ (defun chunk-drop-first-of (chunk)
301
+ (let ((direct (aref chunk CHUNK_DROP_FIRST)))
302
+ (if (callable? direct) (funcall host-apply direct chunk no-arguments) (dispatchChunkDropFirst chunk))))
303
+
304
+ (defun resolve-lazy-chunk (lazy-seq)
305
+ (let ((state (js-call (ensure-lazy-chunked-states) :get lazy-seq)))
306
+ (unless (eq (get state :failure) (ensure-no-failure))
307
+ (throw (get state :failure)))
308
+ (unless (get state :started)
309
+ (put state "started" t)
310
+ (try
311
+ (let ((resolved (funcall (get state :thunk))))
312
+ (put state "thunk" nil)
313
+ (while (literal-instance? resolved LazyChunkedSeq)
314
+ (setq resolved (resolve-lazy-chunk resolved)))
315
+ (put state "value" resolved))
316
+ (catch error
317
+ (put state "thunk" nil)
318
+ (put state "failure" error)
319
+ (throw error))))
320
+ (get state :value)))
321
+
322
+ (defun resolve-more-seq (state)
323
+ (unless (eq (get state :failure) (ensure-no-failure))
324
+ (throw (get state :failure)))
325
+ (unless (get state :moreRealized)
326
+ (put state "moreRealized" t)
327
+ (try
328
+ (let ((raw (get state :more)))
329
+ (put state "more" nil)
330
+ (let ((next-val (if (callable? raw) (funcall raw) raw)))
331
+ (while (literal-instance? next-val LazyChunkedSeq)
332
+ (setq next-val (resolve-lazy-chunk next-val)))
333
+ (put state "cachedMore" (if (nil? next-val) nil next-val))))
334
+ (catch error
335
+ (put state "more" nil)
336
+ (put state "failure" error)
337
+ (throw error))))
338
+ (get state :cachedMore))
339
+
340
+ (defun normalize-chunk-head (chunked-seq)
341
+ (if (literal-instance? chunked-seq LazyChunkedSeq)
342
+ (resolve-lazy-chunk chunked-seq)
343
+ chunked-seq))
344
+
345
+ (defun chunked-first-of (chunked-seq)
346
+ (let ((curr (normalize-chunk-head chunked-seq)))
347
+ (if (nil? curr) nil
348
+ (let ((direct (aref curr CHUNKED_FIRST)))
349
+ (if (callable? direct) (funcall host-apply direct curr no-arguments) (dispatchChunkedFirst curr))))))
350
+
351
+ (defun chunked-next-of (chunked-seq)
352
+ (let ((curr (normalize-chunk-head chunked-seq)))
353
+ (if (nil? curr) nil
354
+ (let ((direct (aref curr CHUNKED_NEXT)))
355
+ (if (callable? direct) (funcall host-apply direct curr no-arguments) (dispatchChunkedNext curr))))))
356
+
357
+ (defun chunked-seq-iterator (initial-seq)
358
+ (let* ((current initial-seq)
359
+ (current-array nil)
360
+ (cursor 0)
361
+ (limit 0)
362
+ (current-chunk nil)
363
+ (tail-iterator nil)
364
+ (closed false)
365
+ (close-fn (lambda ()
366
+ (setq closed t current nil current-array nil current-chunk nil)
367
+ (when (and (not (nil? tail-iterator)) (callable? (get tail-iterator :return)))
368
+ (js-call tail-iterator :return))
369
+ (setq tail-iterator nil)))
370
+ (iter (js-object
371
+ :next (lambda ()
372
+ (if closed
373
+ (js-object :value undefined :done t)
374
+ (progn
375
+ (while (and (>= cursor limit) (nil? tail-iterator) (not (nil? current)))
376
+ (let ((resolved (normalize-chunk-head current)))
377
+ (if (nil? resolved)
378
+ (setq current nil)
379
+ (if (isChunkedSeq resolved)
380
+ (let ((chunk (chunked-first-of resolved)))
381
+ (if (or (nil? chunk) (= (chunk-count-of chunk) 0))
382
+ (setq current (chunked-next-of resolved))
383
+ (let ((cstate (chunk-state chunk)))
384
+ (if (not (undefined? cstate))
385
+ (setq current-array (get cstate :array)
386
+ cursor (get cstate :off)
387
+ limit (get cstate :end)
388
+ current-chunk nil
389
+ current (chunked-next-of resolved))
390
+ (setq current-array nil
391
+ cursor 0
392
+ limit (chunk-count-of chunk)
393
+ current-chunk chunk
394
+ current (chunked-next-of resolved))))))
395
+ (setq tail-iterator (sequence-iterator resolved) current nil)))))
396
+ (cond
397
+ ((< cursor limit)
398
+ (let ((val (if (not (nil? current-array))
399
+ (aref current-array cursor)
400
+ (chunk-nth-of current-chunk cursor))))
401
+ (setq cursor (1+ cursor))
402
+ (js-object :value val :done false)))
403
+ ((not (nil? tail-iterator))
404
+ (let ((step (js-call tail-iterator :next)))
405
+ (when (truth (get step :done))
406
+ (setq closed t tail-iterator nil))
407
+ step))
408
+ (t (setq closed t) (js-object :value undefined :done t))))))
409
+ :return (lambda (&rest optional)
410
+ (let ((value (if (= (js-length optional) 0) undefined (get optional 0))))
411
+ (funcall close-fn)
412
+ (js-object :value value :done t)))
413
+ :throw (lambda (&rest optional)
414
+ (let ((error (if (= (js-length optional) 0) undefined (get optional 0))))
415
+ (funcall close-fn)
416
+ (throw error))))))
417
+ (Object/defineProperty iter iterator-symbol
418
+ (js-object :value (lambda () iter) :writable t :configurable t))
419
+ iter))
420
+
421
+ (defun chunked-seq-count (start-seq)
422
+ (let ((total 0) (curr (normalize-chunk-head start-seq)))
423
+ (while (not (nil? curr))
424
+ (if (isChunkedSeq curr)
425
+ (let ((chunk (chunked-first-of curr)))
426
+ (when (not (nil? chunk)) (setq total (+ total (chunk-count-of chunk))))
427
+ (setq curr (chunked-next-of curr)))
428
+ (let ((iter (sequence-iterator curr)) (done false))
429
+ (while (not done)
430
+ (let ((step (js-call iter :next)))
431
+ (if (truth (get step :done)) (setq done t) (setq total (1+ total)))))
432
+ (setq curr nil))))
433
+ total))
434
+
435
+ (defun reduce-chunked-seq (start-seq reducer initial-args)
436
+ (unless (callable? reducer) (fail "collection reducer must be a function"))
437
+ (let* ((no-init (ensure-no-initial))
438
+ (acc (if (= (js-length initial-args) 0) no-init (aref initial-args 0)))
439
+ (curr (normalize-chunk-head start-seq))
440
+ (early false))
441
+ (while (and (not (nil? curr)) (not early))
442
+ (if (isChunkedSeq curr)
443
+ (let ((chunk (chunked-first-of curr)))
444
+ (when (not (nil? chunk))
445
+ (let ((cstate (chunk-state chunk)))
446
+ (if (not (undefined? cstate))
447
+ (let* ((arr (get cstate :array)) (idx (get cstate :off)) (end (get cstate :end)))
448
+ (when (and (< idx end) (eq acc no-init)) (setq acc (aref arr idx) idx (1+ idx)))
449
+ (while (and (< idx end) (not early))
450
+ (setq acc (funcall reducer acc (aref arr idx)))
451
+ (if (isReducedValue acc) (setq acc (unreducedValue acc) early t) (setq idx (1+ idx)))))
452
+ (let ((cnt (chunk-count-of chunk)) (idx 0))
453
+ (when (and (< idx cnt) (eq acc no-init)) (setq acc (chunk-nth-of chunk 0) idx 1))
454
+ (while (and (< idx cnt) (not early))
455
+ (setq acc (funcall reducer acc (chunk-nth-of chunk idx)))
456
+ (if (isReducedValue acc) (setq acc (unreducedValue acc) early t) (setq idx (1+ idx))))))))
457
+ (unless early (setq curr (chunked-next-of curr))))
458
+ (let ((iter (sequence-iterator curr)) (done false))
459
+ (while (and (not done) (not early))
460
+ (let ((step (js-call iter :next)))
461
+ (if (truth (get step :done)) (setq done t)
462
+ (if (eq acc no-init) (setq acc (get step :value))
463
+ (progn
464
+ (setq acc (funcall reducer acc (get step :value)))
465
+ (when (isReducedValue acc) (setq acc (unreducedValue acc) early t)))))))
466
+ (setq curr nil))))
467
+ (when (eq acc no-init) (fail "cannot reduce an empty collection without an initial value"))
468
+ acc))
469
+
470
+ (defvar chunked-cons-prototype-ready false)
471
+ (defconst ChunkedCons
472
+ (define-named-class
473
+ (lambda (self token chunk more count-hint)
474
+ (unless (and (not (nil? CHUNKED_CONS_TOKEN)) (eq token CHUNKED_CONS_TOKEN))
475
+ (fail "ChunkedCons values must be created with chunkedCons"))
476
+ (ensure-chunked-cons-prototype)
477
+ (js-call (ensure-chunked-cons-states) :set self
478
+ (js-object :chunk chunk :more more :countHint count-hint
479
+ :moreRealized false :cachedMore nil :failure (ensure-no-failure)))
480
+ (Object/freeze self))
481
+ "ChunkedCons"))
482
+
483
+ (defun make-chunked-cons (chunk more &optional count-hint)
484
+ (let ((resolved-hint (if (nullish? count-hint) (ensure-unknown-count) count-hint)))
485
+ (new ChunkedCons (ensure-chunked-cons-token) chunk more resolved-hint)))
486
+
487
+ (defun chunkedCons (chunk more)
488
+ (unless (or (literal-instance? chunk ArrayChunk) (implementsProtocol IChunk chunk))
489
+ (fail "chunkedCons expects an IChunk as its first argument"))
490
+ (if (= (chunk-count-of chunk) 0)
491
+ (if (callable? more) (funcall more) (if (nil? more) nil (seq more)))
492
+ (make-chunked-cons chunk more (ensure-unknown-count))))
493
+
494
+ (defun ensure-chunked-cons-prototype ()
495
+ (unless chunked-cons-prototype-ready
496
+ (let ((ccproto (get ChunkedCons :prototype)))
497
+ (install-method ccproto CHUNKED_FIRST "[eliscript.chunked-seq.chunkedFirst]" 0
498
+ (lambda (self) (get (js-call (ensure-chunked-cons-states) :get self) :chunk)))
499
+ (install-method ccproto CHUNKED_NEXT "[eliscript.chunked-seq.chunkedNext]" 0
500
+ (lambda (self)
501
+ (let ((next-val (resolve-more-seq (js-call (ensure-chunked-cons-states) :get self))))
502
+ (if (nil? next-val) nil
503
+ (if (or (literal-instance? next-val ChunkedCons) (literal-instance? next-val LazyChunkedSeq))
504
+ next-val (seq next-val))))))
505
+ (install-method ccproto CHUNKED_MORE "[eliscript.chunked-seq.chunkedMore]" 0
506
+ (lambda (self)
507
+ (let ((next-val (js-call (aref self CHUNKED_NEXT) :call self)))
508
+ (if (nil? next-val) EMPTY_LIST next-val))))
509
+ (install-method ccproto COLLECTION_SEQ "[eliscript.collection.seq]" 0
510
+ (lambda (self) self))
511
+ (install-method ccproto COLLECTION_COUNT "[eliscript.collection.count]" 0
512
+ (lambda (self)
513
+ (let* ((state (js-call (ensure-chunked-cons-states) :get self))
514
+ (hint (get state :countHint)))
515
+ (cond
516
+ ((eq hint UNBOUNDED_SEQUENCE) (range-fail "unbounded sequence does not have a finite count"))
517
+ ((not (eq hint (ensure-unknown-count))) hint)
518
+ (t (let ((computed (chunked-seq-count self))) (put state "countHint" computed) computed))))))
519
+ (install-method ccproto COLLECTION_REDUCE "[eliscript.collection.reduce]" 1
520
+ (lambda (self reducer &rest initial) (reduce-chunked-seq self reducer initial)))
521
+ (install-method ccproto iterator-symbol "[Symbol.iterator]" 0
522
+ (lambda (self) (chunked-seq-iterator self)))
523
+ (install-getter ccproto (get globalThis/Symbol :toStringTag) "[Symbol.toStringTag]"
524
+ (lambda (_self) "EliscriptChunkedCons")))
525
+ (setq chunked-cons-prototype-ready t)))
526
+
527
+ (defvar lazy-chunked-prototype-ready false)
528
+ (defconst LazyChunkedSeq
529
+ (define-named-class
530
+ (lambda (self token thunk count-hint)
531
+ (unless (and (not (nil? LAZY_CHUNKED_TOKEN)) (eq token LAZY_CHUNKED_TOKEN))
532
+ (fail "LazyChunkedSeq values must be created with lazyChunkedSeq"))
533
+ (ensure-lazy-chunked-prototype)
534
+ (js-call (ensure-lazy-chunked-states) :set self
535
+ (js-object :thunk thunk :countHint count-hint :started false :value nil :failure (ensure-no-failure)))
536
+ (Object/freeze self))
537
+ "LazyChunkedSeq"))
538
+
539
+ (defun make-lazy-chunked (thunk &optional count-hint)
540
+ (new LazyChunkedSeq (ensure-lazy-chunked-token) thunk
541
+ (if (nullish? count-hint) (ensure-unknown-count) count-hint)))
542
+
543
+ (defun ensure-lazy-chunked-prototype ()
544
+ (unless lazy-chunked-prototype-ready
545
+ (let ((lcproto (get LazyChunkedSeq :prototype)))
546
+ (install-method lcproto CHUNKED_FIRST "[eliscript.chunked-seq.chunkedFirst]" 0
547
+ (lambda (self)
548
+ (let ((resolved (resolve-lazy-chunk self)))
549
+ (if (nil? resolved) nil (chunked-first-of resolved)))))
550
+ (install-method lcproto CHUNKED_NEXT "[eliscript.chunked-seq.chunkedNext]" 0
551
+ (lambda (self)
552
+ (let ((resolved (resolve-lazy-chunk self)))
553
+ (if (nil? resolved) nil (chunked-next-of resolved)))))
554
+ (install-method lcproto CHUNKED_MORE "[eliscript.chunked-seq.chunkedMore]" 0
555
+ (lambda (self)
556
+ (let ((next-val (js-call (aref self CHUNKED_NEXT) :call self)))
557
+ (if (nil? next-val) EMPTY_LIST next-val))))
558
+ (install-method lcproto COLLECTION_SEQ "[eliscript.collection.seq]" 0
559
+ (lambda (self)
560
+ (let ((state (js-call (ensure-lazy-chunked-states) :get self)))
561
+ (if (eq (get state :countHint) UNBOUNDED_SEQUENCE) self
562
+ (let ((resolved (resolve-lazy-chunk self))) (if (nil? resolved) nil self))))))
563
+ (install-method lcproto COLLECTION_COUNT "[eliscript.collection.count]" 0
564
+ (lambda (self)
565
+ (let* ((state (js-call (ensure-lazy-chunked-states) :get self))
566
+ (hint (get state :countHint)))
567
+ (cond
568
+ ((eq hint UNBOUNDED_SEQUENCE) (range-fail "unbounded sequence does not have a finite count"))
569
+ ((not (eq hint (ensure-unknown-count))) hint)
570
+ (t (let ((computed (chunked-seq-count self))) (put state "countHint" computed) computed))))))
571
+ (install-method lcproto COLLECTION_REDUCE "[eliscript.collection.reduce]" 1
572
+ (lambda (self reducer &rest initial) (reduce-chunked-seq self reducer initial)))
573
+ (install-method lcproto iterator-symbol "[Symbol.iterator]" 0
574
+ (lambda (self) (chunked-seq-iterator self)))
575
+ (install-getter lcproto (get globalThis/Symbol :toStringTag) "[Symbol.toStringTag]"
576
+ (lambda (_self) "EliscriptLazyChunkedSeq")))
577
+ (setq lazy-chunked-prototype-ready t)))
578
+
579
+ (defun build-range-cons (start step total offset)
580
+ (if (and (not (eq total UNBOUNDED_SEQUENCE)) (>= offset total))
581
+ nil
582
+ (let* ((remaining (if (eq total UNBOUNDED_SEQUENCE) CHUNK_SIZE (Math/min CHUNK_SIZE (- total offset))))
583
+ (items (new globalThis/Array remaining))
584
+ (i 0))
585
+ (while (< i remaining)
586
+ (put items i (+ start (* (+ offset i) step)))
587
+ (setq i (1+ i)))
588
+ (Object/freeze items)
589
+ (let ((next-offset (+ offset remaining))
590
+ (hint (if (eq total UNBOUNDED_SEQUENCE) UNBOUNDED_SEQUENCE (- total offset))))
591
+ (make-chunked-cons (array-chunk-owned items 0 remaining)
592
+ (if (or (eq total UNBOUNDED_SEQUENCE) (< next-offset total))
593
+ (lambda () (build-range-cons start step total next-offset))
594
+ nil)
595
+ hint)))))
596
+
597
+ (defun chunkedRange (&rest bounds)
598
+ (when (> (js-length bounds) 3) (fail "range expects zero to three arguments"))
599
+ (if (= (js-length bounds) 0)
600
+ (make-lazy-chunked (lambda () (build-range-cons 0 1 UNBOUNDED_SEQUENCE 0)) UNBOUNDED_SEQUENCE)
601
+ (let* ((start (require-finite (if (= (js-length bounds) 1) 0 (aref bounds 0)) "range start"))
602
+ (end (require-finite (if (= (js-length bounds) 1) (aref bounds 0) (aref bounds 1)) "range end"))
603
+ (step (require-finite (if (= (js-length bounds) 3) (aref bounds 2) 1) "range step")))
604
+ (when (= step 0) (fail "range step must be non-zero"))
605
+ (let* ((distance (/ (- end start) step))
606
+ (length (if (> distance 0) (require-nonnegative (Math/ceil distance) "range count") 0)))
607
+ (if (= length 0) nil
608
+ (make-lazy-chunked (lambda () (build-range-cons start step length 0)) length))))))
609
+
610
+ (defun build-repeat-cons (value remaining)
611
+ (if (and (not (eq remaining UNBOUNDED_SEQUENCE)) (<= remaining 0))
612
+ nil
613
+ (let* ((batch (if (eq remaining UNBOUNDED_SEQUENCE) CHUNK_SIZE (Math/min CHUNK_SIZE remaining)))
614
+ (items (new globalThis/Array batch))
615
+ (i 0))
616
+ (while (< i batch) (put items i value) (setq i (1+ i)))
617
+ (Object/freeze items)
618
+ (let ((next-rem (if (eq remaining UNBOUNDED_SEQUENCE) UNBOUNDED_SEQUENCE (- remaining batch))))
619
+ (make-chunked-cons (array-chunk-owned items 0 batch)
620
+ (if (or (eq next-rem UNBOUNDED_SEQUENCE) (> next-rem 0))
621
+ (lambda () (build-repeat-cons value next-rem))
622
+ nil)
623
+ remaining)))))
624
+
625
+ (defun chunkedRepeat (&rest arguments)
626
+ (cond
627
+ ((= (js-length arguments) 1)
628
+ (let ((value (aref arguments 0)))
629
+ (make-lazy-chunked (lambda () (build-repeat-cons value UNBOUNDED_SEQUENCE)) UNBOUNDED_SEQUENCE)))
630
+ ((= (js-length arguments) 2)
631
+ (let ((length (require-nonnegative (aref arguments 0) "repeat count"))
632
+ (value (aref arguments 1)))
633
+ (if (= length 0) nil (make-lazy-chunked (lambda () (build-repeat-cons value length)) length))))
634
+ (t (fail "repeat expects a value or a count and value"))))
635
+
636
+ (defun chunked-map-seq (transform source)
637
+ (let ((unbounded (and (literal-instance? source LazyChunkedSeq)
638
+ (eq (get (js-call (ensure-lazy-chunked-states) :get source) :countHint) UNBOUNDED_SEQUENCE))))
639
+ (make-lazy-chunked
640
+ (lambda ()
641
+ (let ((resolved (normalize-chunk-head source)))
642
+ (if (nil? resolved) nil
643
+ (if (isChunkedSeq resolved)
644
+ (let* ((chunk (chunked-first-of resolved))
645
+ (cnt (chunk-count-of chunk))
646
+ (buf (chunkBuffer cnt))
647
+ (cstate (chunk-state chunk)))
648
+ (if (not (undefined? cstate))
649
+ (let ((arr (get cstate :array)) (i (get cstate :off)) (end (get cstate :end)))
650
+ (while (< i end) (chunkAppend buf (funcall transform (aref arr i))) (setq i (1+ i))))
651
+ (let ((i 0))
652
+ (while (< i cnt) (chunkAppend buf (funcall transform (chunk-nth-of chunk i))) (setq i (1+ i)))))
653
+ (let ((rest-seq (chunked-next-of resolved)))
654
+ (make-chunked-cons (chunkFromBuffer buf)
655
+ (if (nil? rest-seq) nil (lambda () (chunked-map-seq transform rest-seq)))
656
+ (if unbounded UNBOUNDED_SEQUENCE (ensure-unknown-count)))))
657
+ (sequence (mapping transform) resolved)))))
658
+ (if unbounded UNBOUNDED_SEQUENCE (ensure-unknown-count)))))
659
+
660
+ (defun chunked-filter-seq (predicate source)
661
+ (make-lazy-chunked
662
+ (lambda ()
663
+ (let ((curr (normalize-chunk-head source)) (result nil) (done false))
664
+ (while (and (not (nil? curr)) (not done))
665
+ (if (isChunkedSeq curr)
666
+ (let* ((chunk (chunked-first-of curr))
667
+ (cnt (chunk-count-of chunk))
668
+ (buf (chunkBuffer cnt))
669
+ (cstate (chunk-state chunk)))
670
+ (if (not (undefined? cstate))
671
+ (let ((arr (get cstate :array)) (i (get cstate :off)) (end (get cstate :end)))
672
+ (while (< i end)
673
+ (let ((item (aref arr i)))
674
+ (when (truth (isTruthy (funcall predicate item))) (chunkAppend buf item)))
675
+ (setq i (1+ i))))
676
+ (let ((i 0))
677
+ (while (< i cnt)
678
+ (let ((item (chunk-nth-of chunk i)))
679
+ (when (truth (isTruthy (funcall predicate item))) (chunkAppend buf item)))
680
+ (setq i (1+ i)))))
681
+ (let ((next-curr (chunked-next-of curr)))
682
+ (if (> (get buf :count) 0)
683
+ (setq result (make-chunked-cons (chunkFromBuffer buf)
684
+ (if (nil? next-curr) nil (lambda () (chunked-filter-seq predicate next-curr))))
685
+ done t)
686
+ (setq curr next-curr))))
687
+ (setq result (sequence (filtering predicate) curr) done t)))
688
+ result))))
689
+
690
+ (defun chunked-keep-seq (transform source)
691
+ (make-lazy-chunked
692
+ (lambda ()
693
+ (let ((curr (normalize-chunk-head source)) (result nil) (done false))
694
+ (while (and (not (nil? curr)) (not done))
695
+ (if (isChunkedSeq curr)
696
+ (let* ((chunk (chunked-first-of curr))
697
+ (cnt (chunk-count-of chunk))
698
+ (buf (chunkBuffer cnt))
699
+ (cstate (chunk-state chunk)))
700
+ (if (not (undefined? cstate))
701
+ (let ((arr (get cstate :array)) (i (get cstate :off)) (end (get cstate :end)))
702
+ (while (< i end)
703
+ (let ((kept (funcall transform (aref arr i))))
704
+ (unless (nil? kept) (chunkAppend buf kept)))
705
+ (setq i (1+ i))))
706
+ (let ((i 0))
707
+ (while (< i cnt)
708
+ (let ((kept (funcall transform (chunk-nth-of chunk i))))
709
+ (unless (nil? kept) (chunkAppend buf kept)))
710
+ (setq i (1+ i)))))
711
+ (let ((next-curr (chunked-next-of curr)))
712
+ (if (> (get buf :count) 0)
713
+ (setq result (make-chunked-cons (chunkFromBuffer buf)
714
+ (if (nil? next-curr) nil (lambda () (chunked-keep-seq transform next-curr))))
715
+ done t)
716
+ (setq curr next-curr))))
717
+ (setq result (sequence (keeping transform) curr) done t)))
718
+ result))))
719
+
720
+ (defun chunked-take-seq (limit source)
721
+ (if (= limit 0)
722
+ (make-lazy-chunked (lambda () nil) 0)
723
+ (make-lazy-chunked
724
+ (lambda ()
725
+ (let ((resolved (normalize-chunk-head source)))
726
+ (if (nil? resolved) nil
727
+ (if (isChunkedSeq resolved)
728
+ (let* ((chunk (chunked-first-of resolved))
729
+ (cnt (chunk-count-of chunk))
730
+ (take-cnt (Math/min limit cnt))
731
+ (buf (chunkBuffer take-cnt))
732
+ (cstate (chunk-state chunk)))
733
+ (if (not (undefined? cstate))
734
+ (let ((arr (get cstate :array)) (off (get cstate :off)) (i 0))
735
+ (while (< i take-cnt) (chunkAppend buf (aref arr (+ off i))) (setq i (1+ i))))
736
+ (let ((i 0))
737
+ (while (< i take-cnt) (chunkAppend buf (chunk-nth-of chunk i)) (setq i (1+ i)))))
738
+ (let ((rem (- limit take-cnt)) (next-src (chunked-next-of resolved)))
739
+ (make-chunked-cons (chunkFromBuffer buf)
740
+ (if (or (= rem 0) (nil? next-src)) nil (lambda () (chunked-take-seq rem next-src))))))
741
+ (sequence (taking limit) resolved))))))))
742
+
743
+ (defun chunked-drop-seq (limit source)
744
+ (if (= limit 0) source
745
+ (let ((unbounded (and (literal-instance? source LazyChunkedSeq)
746
+ (eq (get (js-call (ensure-lazy-chunked-states) :get source) :countHint) UNBOUNDED_SEQUENCE))))
747
+ (make-lazy-chunked
748
+ (lambda ()
749
+ (let ((rem limit) (curr (normalize-chunk-head source)) (result nil) (done false))
750
+ (while (and (> rem 0) (not (nil? curr)) (not done))
751
+ (if (isChunkedSeq curr)
752
+ (let* ((chunk (chunked-first-of curr)) (cnt (chunk-count-of chunk)))
753
+ (if (<= cnt rem)
754
+ (setq rem (- rem cnt) curr (chunked-next-of curr))
755
+ (let ((trimmed chunk))
756
+ (while (> rem 0) (setq trimmed (chunk-drop-first-of trimmed) rem (1- rem)))
757
+ (setq result (make-chunked-cons trimmed (chunked-next-of curr)
758
+ (if unbounded UNBOUNDED_SEQUENCE (ensure-unknown-count)))
759
+ done t))))
760
+ (setq result (sequence (dropping rem) curr) done t)))
761
+ (if done result curr)))
762
+ (if unbounded UNBOUNDED_SEQUENCE (ensure-unknown-count))))))
763
+
764
+ (registerChunkedSliceHandler (lambda (source offset) (chunked-drop-seq offset source)))
765
+ (registerChunkedPrependHandler
766
+ (lambda (value source)
767
+ (let* ((items (Object/freeze (js-array value)))
768
+ (single (array-chunk-owned items 0 1))
769
+ (unbounded (and (literal-instance? source LazyChunkedSeq)
770
+ (eq (get (js-call (ensure-lazy-chunked-states) :get source) :countHint) UNBOUNDED_SEQUENCE))))
771
+ (make-chunked-cons single source (if unbounded UNBOUNDED_SEQUENCE (ensure-unknown-count))))))
772
+
773
+ ;; Existing non-chunked lazy sequence & lazyCons path
82
774
  (defun lazySequence (&rest arguments)
83
775
  (unless (= (js-length arguments) 1) (fail "lazySequence requires exactly one thunk"))
84
776
  (let ((thunk (get arguments 0)))
85
777
  (require-thunk thunk "lazySequence thunk")
86
778
  (memoizedSequenceView (lambda () (sequence-iterator (funcall thunk))))))
779
+
87
780
  (defun lazyCons (&rest arguments)
88
781
  (unless (= (js-length arguments) 2) (fail "lazyCons requires a value and tail thunk"))
89
782
  (let ((value (get arguments 0)) (tail-thunk (get arguments 1)))
90
783
  (require-thunk tail-thunk "lazyCons tail thunk")
91
- (ensure-lazy-cons-states) (ensure-no-failure)
784
+ (ensure-lazy-cons-states)
785
+ (ensure-no-failure)
92
786
  (let* ((state (js-object :value value :rest tail-thunk :started false :failure no-failure))
93
787
  (result (memoizedSequenceView (lambda () (cons-iterator state)))))
94
- (js-call lazy-cons-states :set result state) result)))
788
+ (js-call lazy-cons-states :set result state)
789
+ result)))
790
+
95
791
  (defun lazyMap (&rest arguments)
96
- (require-arity (js-length arguments) 2 "lazyMap") (sequence (mapping (get arguments 0)) (get arguments 1)))
792
+ (require-arity (js-length arguments) 2 "lazyMap")
793
+ (let ((transform (get arguments 0)) (collection (get arguments 1)))
794
+ (if (isChunkedSeq collection)
795
+ (progn (unless (callable? transform) (mapping transform)) (chunked-map-seq transform collection))
796
+ (sequence (mapping transform) collection))))
797
+
97
798
  (defun lazyMapIndexed (&rest arguments)
98
- (require-arity (js-length arguments) 2 "lazyMapIndexed") (sequence (mappingIndexed (get arguments 0)) (get arguments 1)))
799
+ (require-arity (js-length arguments) 2 "lazyMapIndexed")
800
+ (sequence (mappingIndexed (get arguments 0)) (get arguments 1)))
801
+
99
802
  (defun lazyKeep (&rest arguments)
100
- (require-arity (js-length arguments) 2 "lazyKeep") (sequence (keeping (get arguments 0)) (get arguments 1)))
803
+ (require-arity (js-length arguments) 2 "lazyKeep")
804
+ (let ((transform (get arguments 0)) (collection (get arguments 1)))
805
+ (if (isChunkedSeq collection)
806
+ (progn (unless (callable? transform) (keeping transform)) (chunked-keep-seq transform collection))
807
+ (sequence (keeping transform) collection))))
808
+
101
809
  (defun lazyKeepIndexed (&rest arguments)
102
- (require-arity (js-length arguments) 2 "lazyKeepIndexed") (sequence (keepingIndexed (get arguments 0)) (get arguments 1)))
810
+ (require-arity (js-length arguments) 2 "lazyKeepIndexed")
811
+ (sequence (keepingIndexed (get arguments 0)) (get arguments 1)))
812
+
103
813
  (defun lazyFilter (&rest arguments)
104
- (require-arity (js-length arguments) 2 "lazyFilter") (sequence (filtering (get arguments 0)) (get arguments 1)))
814
+ (require-arity (js-length arguments) 2 "lazyFilter")
815
+ (let ((predicate (get arguments 0)) (collection (get arguments 1)))
816
+ (if (isChunkedSeq collection)
817
+ (progn (unless (callable? predicate) (filtering predicate)) (chunked-filter-seq predicate collection))
818
+ (sequence (filtering predicate) collection))))
819
+
105
820
  (defun lazyRemove (&rest arguments)
106
- (require-arity (js-length arguments) 2 "lazyRemove") (sequence (removing (get arguments 0)) (get arguments 1)))
821
+ (require-arity (js-length arguments) 2 "lazyRemove")
822
+ (sequence (removing (get arguments 0)) (get arguments 1)))
823
+
107
824
  (defun lazyTake (&rest arguments)
108
- (require-arity (js-length arguments) 2 "lazyTake") (sequence (taking (get arguments 0)) (get arguments 1)))
825
+ (require-arity (js-length arguments) 2 "lazyTake")
826
+ (let ((limit (get arguments 0)) (collection (get arguments 1)))
827
+ (if (isChunkedSeq collection)
828
+ (progn (unless (and (Number/isSafeInteger limit) (>= limit 0)) (taking limit)) (chunked-take-seq limit collection))
829
+ (sequence (taking limit) collection))))
830
+
109
831
  (defun lazyDrop (&rest arguments)
110
- (require-arity (js-length arguments) 2 "lazyDrop") (sequence (dropping (get arguments 0)) (get arguments 1)))
832
+ (require-arity (js-length arguments) 2 "lazyDrop")
833
+ (let ((limit (get arguments 0)) (collection (get arguments 1)))
834
+ (if (isChunkedSeq collection)
835
+ (progn (unless (and (Number/isSafeInteger limit) (>= limit 0)) (dropping limit)) (chunked-drop-seq limit collection))
836
+ (sequence (dropping limit) collection))))
837
+
111
838
  (defun lazyTakeWhile (&rest arguments)
112
- (require-arity (js-length arguments) 2 "lazyTakeWhile") (sequence (takingWhile (get arguments 0)) (get arguments 1)))
839
+ (require-arity (js-length arguments) 2 "lazyTakeWhile")
840
+ (sequence (takingWhile (get arguments 0)) (get arguments 1)))
841
+
113
842
  (defun lazyDropWhile (&rest arguments)
114
- (require-arity (js-length arguments) 2 "lazyDropWhile") (sequence (droppingWhile (get arguments 0)) (get arguments 1)))
843
+ (require-arity (js-length arguments) 2 "lazyDropWhile")
844
+ (sequence (droppingWhile (get arguments 0)) (get arguments 1)))
845
+
115
846
  (defun lazyTakeNth (&rest arguments)
116
- (require-arity (js-length arguments) 2 "lazyTakeNth") (sequence (takingNth (get arguments 0)) (get arguments 1)))
847
+ (require-arity (js-length arguments) 2 "lazyTakeNth")
848
+ (sequence (takingNth (get arguments 0)) (get arguments 1)))
849
+
117
850
  (defun lazyInterpose (&rest arguments)
118
- (require-arity (js-length arguments) 2 "lazyInterpose") (sequence (interposing (get arguments 0)) (get arguments 1)))
851
+ (require-arity (js-length arguments) 2 "lazyInterpose")
852
+ (sequence (interposing (get arguments 0)) (get arguments 1)))
853
+
119
854
  (defun lazyDedupe (&rest arguments)
120
- (require-arity (js-length arguments) 1 "lazyDedupe") (sequence (deduping) (get arguments 0)))
855
+ (require-arity (js-length arguments) 1 "lazyDedupe")
856
+ (sequence (deduping) (get arguments 0)))
857
+
121
858
  (defun lazyDistinct (&rest arguments)
122
- (require-arity (js-length arguments) 1 "lazyDistinct") (sequence (distincting) (get arguments 0)))
859
+ (require-arity (js-length arguments) 1 "lazyDistinct")
860
+ (sequence (distincting) (get arguments 0)))
861
+
123
862
  (defun lazyMapcat (&rest arguments)
124
- (require-arity (js-length arguments) 2 "lazyMapcat") (sequence (mapcatting (get arguments 0)) (get arguments 1)))
863
+ (require-arity (js-length arguments) 2 "lazyMapcat")
864
+ (sequence (mapcatting (get arguments 0)) (get arguments 1)))
865
+
125
866
  (defun lazyPartitionAll (&rest arguments)
126
- (require-arity (js-length arguments) 2 "lazyPartitionAll") (sequence (partitioningAll (get arguments 0)) (get arguments 1)))
867
+ (require-arity (js-length arguments) 2 "lazyPartitionAll")
868
+ (sequence (partitioningAll (get arguments 0)) (get arguments 1)))
869
+
127
870
  (defun lazyPartitionBy (&rest arguments)
128
- (require-arity (js-length arguments) 2 "lazyPartitionBy") (sequence (partitioningBy (get arguments 0)) (get arguments 1)))
129
- (defun isLazySequence (value) (literal-instance? value MemoizedSequenceView))
871
+ (require-arity (js-length arguments) 2 "lazyPartitionBy")
872
+ (sequence (partitioningBy (get arguments 0)) (get arguments 1)))
873
+
874
+ (defun isLazySequence (value)
875
+ (or (literal-instance? value MemoizedSequenceView)
876
+ (literal-instance? value LazyChunkedSeq)
877
+ (literal-instance? value ChunkedCons)))
878
+
130
879
  (defun status (value)
131
- (unless (isLazySequence value) (fail "operation requires a lazy sequence")) (memoizedSequenceStatus value))
880
+ (cond
881
+ ((literal-instance? value MemoizedSequenceView) (memoizedSequenceStatus value))
882
+ ((literal-instance? value LazyChunkedSeq)
883
+ (let ((state (js-call (ensure-lazy-chunked-states) :get value)))
884
+ (Object/freeze (js-object
885
+ :started (get state :started)
886
+ :complete (and (get state :started) (nil? (get state :value)))
887
+ :failed (not (eq (get state :failure) (ensure-no-failure)))
888
+ :realizedCount (if (and (get state :started) (not (nil? (get state :value))))
889
+ (let ((chunk (chunked-first-of (get state :value))))
890
+ (if (nil? chunk) 0 (chunk-count-of chunk)))
891
+ 0)))))
892
+ ((literal-instance? value ChunkedCons)
893
+ (let ((state (js-call (ensure-chunked-cons-states) :get value)))
894
+ (Object/freeze (js-object
895
+ :started t
896
+ :complete (and (get state :moreRealized) (nil? (get state :cachedMore)))
897
+ :failed (not (eq (get state :failure) (ensure-no-failure)))
898
+ :realizedCount (chunk-count-of (get state :chunk))))))
899
+ (t (fail "operation requires a lazy sequence"))))
900
+
132
901
  (defun isLazySequenceRealized (value) (get (status value) :started))
133
902
  (defun realizedLazySequenceCount (value) (get (status value) :realizedCount))
134
903
  (defun realizeLazySequence (value)
135
- (status value) (literal-each value (lambda (element) undefined)) value)
904
+ (status value)
905
+ (let ((iterator (js-call (aref value iterator-symbol) :call value)) (done false))
906
+ (while (not done)
907
+ (let ((step (js-call iterator :next)))
908
+ (when (truth (get step :done)) (setq done t)))))
909
+ value)
910
+
136
911
  (Object/defineProperty lazySequence "length" (js-object :value 1 :configurable t))
137
912
  (Object/defineProperty lazyCons "length" (js-object :value 2 :configurable t))
138
913
  (Object/defineProperty lazyMap "length" (js-object :value 2 :configurable t))
@@ -152,6 +927,9 @@
152
927
  (Object/defineProperty lazyMapcat "length" (js-object :value 2 :configurable t))
153
928
  (Object/defineProperty lazyPartitionAll "length" (js-object :value 2 :configurable t))
154
929
  (Object/defineProperty lazyPartitionBy "length" (js-object :value 2 :configurable t))
155
- (export lazySequence lazyCons lazyMap lazyMapIndexed lazyKeep lazyKeepIndexed lazyFilter lazyRemove lazyTake lazyDrop
930
+
931
+ (export CHUNK_SIZE ArrayChunk ChunkBuffer ChunkedCons LazyChunkedSeq
932
+ arrayChunk chunkBuffer chunkAppend chunkFromBuffer chunkedCons chunkedRange chunkedRepeat
933
+ lazySequence lazyCons lazyMap lazyMapIndexed lazyKeep lazyKeepIndexed lazyFilter lazyRemove lazyTake lazyDrop
156
934
  lazyTakeWhile lazyDropWhile lazyTakeNth lazyInterpose lazyDedupe lazyDistinct lazyMapcat lazyPartitionAll lazyPartitionBy
157
935
  isLazySequence isLazySequenceRealized realizedLazySequenceCount realizeLazySequence))