eliscript 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (417) hide show
  1. package/README.md +59 -0
  2. package/bootstrap/README.md +26 -0
  3. package/bootstrap/backends/README.md +515 -0
  4. package/bootstrap/backends/async.eli +230 -0
  5. package/bootstrap/backends/binary.eli +173 -0
  6. package/bootstrap/backends/capabilities.eli +74 -0
  7. package/bootstrap/backends/elf.eli +246 -0
  8. package/bootstrap/backends/managed.eli +5976 -0
  9. package/bootstrap/backends/memory.eli +100 -0
  10. package/bootstrap/backends/native.eli +2000 -0
  11. package/bootstrap/backends/numeric.eli +1362 -0
  12. package/bootstrap/backends/optimize.eli +79 -0
  13. package/bootstrap/backends/regexp.eli +2263 -0
  14. package/bootstrap/backends/sha256.eli +138 -0
  15. package/bootstrap/backends/wasm.eli +1161 -0
  16. package/bootstrap/compiler/analyzer.eli +165 -77
  17. package/bootstrap/compiler/compiler.eli +43 -19
  18. package/bootstrap/compiler/emitter.eli +780 -170
  19. package/bootstrap/compiler/expander.eli +136 -45
  20. package/bootstrap/compiler/formatter.eli +8 -1
  21. package/bootstrap/compiler/ir.eli +148 -45
  22. package/bootstrap/compiler/lower.eli +29 -2
  23. package/bootstrap/compiler/project.eli +51 -21
  24. package/bootstrap/compiler/reader.eli +128 -36
  25. package/bootstrap/compiler/source-map.eli +46 -4
  26. package/bootstrap/compiler/symbol.eli +2 -1
  27. package/bootstrap/compiler/syntax.eli +47 -12
  28. package/bootstrap/compiler/transient-analysis.eli +40 -4
  29. package/bootstrap/host/check-cli-legacy.mjs +163 -0
  30. package/bootstrap/host/check-cli.mjs +59 -158
  31. package/bootstrap/host/check-cli.mjs.map +1 -0
  32. package/bootstrap/host/compiler-session.eli +1579 -0
  33. package/bootstrap/host/compiler-tools.eli +513 -0
  34. package/bootstrap/host/eliscript-cli-legacy.mjs +37 -0
  35. package/bootstrap/host/eliscript-cli.mjs +53 -33
  36. package/bootstrap/host/eliscript-cli.mjs.map +1 -0
  37. package/bootstrap/host/project-cli-legacy.mjs +295 -0
  38. package/bootstrap/host/project-cli.mjs +74 -289
  39. package/bootstrap/host/project-cli.mjs.map +1 -0
  40. package/bootstrap/host/project-legacy.mjs +1080 -0
  41. package/bootstrap/host/project.mjs +210 -1060
  42. package/bootstrap/host/project.mjs.map +1 -0
  43. package/bootstrap/runtime/UNICODE-LICENSE.txt +39 -0
  44. package/bootstrap/runtime/memory.eli +30379 -0
  45. package/browser/host.mjs +89 -0
  46. package/compiler/eliscript-analyzer.el +371 -4
  47. package/compiler/eliscript-cli.el +84 -1
  48. package/compiler/eliscript-emitter.el +1189 -256
  49. package/compiler/eliscript-expander.el +147 -10
  50. package/compiler/eliscript-ir-emitter.el +725 -208
  51. package/compiler/eliscript-lower.el +127 -1
  52. package/compiler/eliscript-portable.el +177 -51
  53. package/compiler/eliscript-project.el +20 -2
  54. package/compiler/eliscript-reader.el +141 -57
  55. package/compiler/eliscript-source-map.el +26 -12
  56. package/compiler/eliscript-transient-analysis.el +382 -19
  57. package/dist/backends/async.mjs +561 -0
  58. package/dist/backends/async.mjs.macro-origins.json +1 -0
  59. package/dist/backends/async.mjs.map +1 -0
  60. package/dist/backends/binary.mjs +381 -0
  61. package/dist/backends/binary.mjs.macro-origins.json +1 -0
  62. package/dist/backends/binary.mjs.map +1 -0
  63. package/dist/backends/capabilities.mjs +237 -0
  64. package/dist/backends/capabilities.mjs.macro-origins.json +1 -0
  65. package/dist/backends/capabilities.mjs.map +1 -0
  66. package/dist/backends/elf.mjs +540 -0
  67. package/dist/backends/elf.mjs.macro-origins.json +1 -0
  68. package/dist/backends/elf.mjs.map +1 -0
  69. package/dist/backends/managed.mjs +10616 -0
  70. package/dist/backends/managed.mjs.macro-origins.json +1 -0
  71. package/dist/backends/managed.mjs.map +1 -0
  72. package/dist/backends/memory.mjs +278 -0
  73. package/dist/backends/memory.mjs.macro-origins.json +1 -0
  74. package/dist/backends/memory.mjs.map +1 -0
  75. package/dist/backends/native.mjs +4099 -0
  76. package/dist/backends/native.mjs.macro-origins.json +1 -0
  77. package/dist/backends/native.mjs.map +1 -0
  78. package/dist/backends/numeric.mjs +2458 -0
  79. package/dist/backends/numeric.mjs.macro-origins.json +1 -0
  80. package/dist/backends/numeric.mjs.map +1 -0
  81. package/dist/backends/optimize.mjs +204 -0
  82. package/dist/backends/optimize.mjs.macro-origins.json +1 -0
  83. package/dist/backends/optimize.mjs.map +1 -0
  84. package/dist/backends/regexp.mjs +2033 -0
  85. package/dist/backends/regexp.mjs.macro-origins.json +1 -0
  86. package/dist/backends/regexp.mjs.map +1 -0
  87. package/dist/backends/sha256.mjs +310 -0
  88. package/dist/backends/sha256.mjs.macro-origins.json +1 -0
  89. package/dist/backends/sha256.mjs.map +1 -0
  90. package/dist/backends/wasm.mjs +2258 -0
  91. package/dist/backends/wasm.mjs.macro-origins.json +1 -0
  92. package/dist/backends/wasm.mjs.map +1 -0
  93. package/dist/bootstrap/analyzer.mjs +1908 -776
  94. package/dist/bootstrap/analyzer.mjs.macro-origins.json +1 -0
  95. package/dist/bootstrap/analyzer.mjs.map +1 -1
  96. package/dist/bootstrap/compiler.mjs +450 -142
  97. package/dist/bootstrap/compiler.mjs.macro-origins.json +1 -0
  98. package/dist/bootstrap/compiler.mjs.map +1 -1
  99. package/dist/bootstrap/emitter.mjs +3083 -909
  100. package/dist/bootstrap/emitter.mjs.macro-origins.json +1 -0
  101. package/dist/bootstrap/emitter.mjs.map +1 -1
  102. package/dist/bootstrap/expander.mjs +2887 -1233
  103. package/dist/bootstrap/expander.mjs.macro-origins.json +1 -0
  104. package/dist/bootstrap/expander.mjs.map +1 -1
  105. package/dist/bootstrap/formatter.mjs +582 -218
  106. package/dist/bootstrap/formatter.mjs.macro-origins.json +1 -0
  107. package/dist/bootstrap/formatter.mjs.map +1 -1
  108. package/dist/bootstrap/ir.mjs +670 -116
  109. package/dist/bootstrap/ir.mjs.macro-origins.json +1 -0
  110. package/dist/bootstrap/ir.mjs.map +1 -1
  111. package/dist/bootstrap/lower.mjs +866 -257
  112. package/dist/bootstrap/lower.mjs.macro-origins.json +1 -0
  113. package/dist/bootstrap/lower.mjs.map +1 -1
  114. package/dist/bootstrap/package.json +1 -0
  115. package/dist/bootstrap/project.mjs +1168 -498
  116. package/dist/bootstrap/project.mjs.macro-origins.json +1 -0
  117. package/dist/bootstrap/project.mjs.map +1 -1
  118. package/dist/bootstrap/reader.mjs +674 -175
  119. package/dist/bootstrap/reader.mjs.macro-origins.json +1 -0
  120. package/dist/bootstrap/reader.mjs.map +1 -1
  121. package/dist/bootstrap/runtime/math.mjs +1117 -0
  122. package/dist/bootstrap/runtime/math.mjs.map +1 -0
  123. package/dist/bootstrap/runtime/unicode-case-data.mjs +10125 -0
  124. package/dist/bootstrap/runtime/unicode-case-data.mjs.map +1 -0
  125. package/dist/bootstrap/runtime/unicode-case.mjs +281 -0
  126. package/dist/bootstrap/runtime/unicode-case.mjs.map +1 -0
  127. package/dist/bootstrap/source-map.mjs +430 -119
  128. package/dist/bootstrap/source-map.mjs.macro-origins.json +1 -0
  129. package/dist/bootstrap/source-map.mjs.map +1 -1
  130. package/dist/bootstrap/symbol.mjs +229 -53
  131. package/dist/bootstrap/symbol.mjs.macro-origins.json +1 -0
  132. package/dist/bootstrap/symbol.mjs.map +1 -1
  133. package/dist/bootstrap/syntax.mjs +205 -7
  134. package/dist/bootstrap/syntax.mjs.macro-origins.json +1 -0
  135. package/dist/bootstrap/syntax.mjs.map +1 -1
  136. package/dist/bootstrap/transient-analysis.mjs +1185 -489
  137. package/dist/bootstrap/transient-analysis.mjs.macro-origins.json +1 -0
  138. package/dist/bootstrap/transient-analysis.mjs.map +1 -1
  139. package/dist/browser/compiler.js +199 -6478
  140. package/dist/browser/host.mjs +446 -0
  141. package/dist/browser/loader.js +1 -0
  142. package/dist/browser/loader.mjs +231 -0
  143. package/dist/browser/worker.mjs +60 -0
  144. package/dist/compiler-artifact.json +1 -0
  145. package/dist/host/compiler-session.mjs +2980 -0
  146. package/dist/host/compiler-session.mjs.macro-origins.json +1 -0
  147. package/dist/host/compiler-session.mjs.map +1 -0
  148. package/dist/host/compiler-tools.mjs +1084 -0
  149. package/dist/host/compiler-tools.mjs.macro-origins.json +1 -0
  150. package/dist/host/compiler-tools.mjs.map +1 -0
  151. package/dist/literal-sources/runtime/core/collection-internals.eli +60 -0
  152. package/dist/literal-sources/runtime/core/collection.eli +579 -0
  153. package/dist/literal-sources/runtime/core/data-text.eli +290 -0
  154. package/dist/literal-sources/runtime/core/data.eli +22 -0
  155. package/dist/literal-sources/runtime/core/identifier.eli +44 -0
  156. package/dist/literal-sources/runtime/core/lazy-sequence.eli +131 -0
  157. package/dist/literal-sources/runtime/core/list.eli +19 -0
  158. package/dist/literal-sources/runtime/core/map.eli +9 -0
  159. package/dist/literal-sources/runtime/core/metadata-internals.eli +6 -0
  160. package/dist/literal-sources/runtime/core/metadata.eli +23 -0
  161. package/dist/literal-sources/runtime/core/object.eli +8 -0
  162. package/dist/literal-sources/runtime/core/order.eli +123 -0
  163. package/dist/literal-sources/runtime/core/persistent-kind.eli +28 -0
  164. package/dist/literal-sources/runtime/core/protocol-error.eli +32 -0
  165. package/dist/literal-sources/runtime/core/protocol.eli +28 -0
  166. package/dist/literal-sources/runtime/core/queue.eli +9 -0
  167. package/dist/literal-sources/runtime/core/record.eli +191 -0
  168. package/dist/literal-sources/runtime/core/regex.eli +158 -0
  169. package/dist/literal-sources/runtime/core/sequence.eli +305 -0
  170. package/dist/literal-sources/runtime/core/set-algebra.eli +28 -0
  171. package/dist/literal-sources/runtime/core/set.eli +9 -0
  172. package/dist/literal-sources/runtime/core/sorted.eli +408 -0
  173. package/dist/literal-sources/runtime/core/text.eli +14 -0
  174. package/dist/literal-sources/runtime/core/transducer.eli +268 -0
  175. package/dist/literal-sources/runtime/core/transient.eli +17 -0
  176. package/dist/literal-sources/runtime/core/truth.eli +4 -0
  177. package/dist/literal-sources/runtime/core/type.eli +232 -0
  178. package/dist/literal-sources/runtime/core/value-internals.eli +13 -0
  179. package/dist/literal-sources/runtime/core/value.eli +19 -0
  180. package/dist/literal-sources/runtime/core/vector.eli +20 -0
  181. package/dist/literal-sources/runtime/literals.eli +23 -0
  182. package/dist/literal-sources/runtime/testing/value.eli +6 -0
  183. package/dist/literal-sources/runtime/wire/http-rpc.eli +362 -0
  184. package/dist/literal-sources/runtime/wire/markdown.eli +275 -0
  185. package/dist/literal-sources/runtime/wire/schema.eli +193 -0
  186. package/dist/literal-sources/stdlib/bit.eli +36 -0
  187. package/dist/literal-sources/stdlib/core/data.eli +306 -0
  188. package/dist/literal-sources/stdlib/core/object.eli +89 -0
  189. package/dist/literal-sources/stdlib/core/protocol.eli +451 -0
  190. package/dist/literal-sources/stdlib/core/set.eli +312 -0
  191. package/dist/literal-sources/stdlib/core/text.eli +103 -0
  192. package/dist/literal-sources/stdlib/managed-literal-protocols.eli +483 -0
  193. package/dist/literal-sources/stdlib/managed-literals.eli +1414 -0
  194. package/dist/literal-sources/stdlib/managed-queue-state.eli +65 -0
  195. package/dist/literal-sources/stdlib/managed-sorted-state.eli +233 -0
  196. package/dist/literal-sources/stdlib/managed-transient-state.eli +348 -0
  197. package/dist/literal-sources/stdlib/managed-value-state.eli +153 -0
  198. package/dist/literal-sources/stdlib/managed-vector-view-state.eli +102 -0
  199. package/dist/literal-sources/stdlib/persistent-list.eli +145 -0
  200. package/dist/literal-sources/stdlib/persistent-map.eli +724 -0
  201. package/dist/literal-sources/stdlib/persistent-set.eli +236 -0
  202. package/dist/literal-sources/stdlib/persistent-vector.eli +359 -0
  203. package/dist/literal-sources/stdlib/value.eli +325 -0
  204. package/dist/package.json +10 -0
  205. package/dist/runtime/math.mjs +1116 -0
  206. package/dist/runtime/math.mjs.macro-origins.json +1 -0
  207. package/dist/runtime/math.mjs.map +1 -0
  208. package/dist/runtime/unicode-case-data.mjs +10124 -0
  209. package/dist/runtime/unicode-case-data.mjs.macro-origins.json +1 -0
  210. package/dist/runtime/unicode-case-data.mjs.map +1 -0
  211. package/dist/runtime/unicode-case.mjs +280 -0
  212. package/dist/runtime/unicode-case.mjs.macro-origins.json +1 -0
  213. package/dist/runtime/unicode-case.mjs.map +1 -0
  214. package/dist/runtime.eli +30379 -0
  215. package/dist/stage3-release.json +1 -0
  216. package/editor/README.md +1 -1
  217. package/editor/eliscript-mode.el +1 -1
  218. package/editor/eliscript-repl.el +1 -1
  219. package/package.json +97 -10
  220. package/platform/http.mjs +20 -0
  221. package/runtime/array-text.mjs +159 -0
  222. package/runtime/core/map-internals.mjs +27 -9
  223. package/runtime/core/object-impl.mjs +26 -17
  224. package/runtime/core/object-impl.mjs.map +1 -1
  225. package/runtime/core/protocol-impl.mjs +682 -255
  226. package/runtime/core/protocol-impl.mjs.map +1 -1
  227. package/runtime/core/protocol.mjs +4 -0
  228. package/runtime/core/record.mjs +23 -2
  229. package/runtime/core/regex.mjs +4 -0
  230. package/runtime/core/sorted.mjs +53 -0
  231. package/runtime/core/text-impl.mjs +167 -48
  232. package/runtime/core/text-impl.mjs.map +1 -1
  233. package/runtime/core/type.mjs +75 -3
  234. package/runtime/core/vector.mjs +6 -0
  235. package/runtime/math.eli +429 -0
  236. package/runtime/math.mjs +1116 -0
  237. package/runtime/math.mjs.map +1 -0
  238. package/runtime/number-radix.mjs +105 -0
  239. package/runtime/unicode-case-data.eli +29 -0
  240. package/runtime/unicode-case-data.mjs +10124 -0
  241. package/runtime/unicode-case-data.mjs.map +1 -0
  242. package/runtime/unicode-case.eli +100 -0
  243. package/runtime/unicode-case.mjs +280 -0
  244. package/runtime/unicode-case.mjs.map +1 -0
  245. package/runtime/wire/http-rpc.mjs +168 -0
  246. package/runtime/wire/markdown.mjs +196 -0
  247. package/runtime/wire/schema.mjs +109 -0
  248. package/stdlib/README.md +54 -0
  249. package/stdlib/core/protocol.eli +81 -17
  250. package/stdlib/core/seq.eli +1 -1
  251. package/stdlib/core/zipper.eli +114 -47
  252. package/stdlib/gepo.eli +12 -0
  253. package/stdlib/interop/js.eli +56 -15
  254. package/stdlib/managed-literal-protocols.eli +483 -0
  255. package/stdlib/managed-literals.eli +1414 -0
  256. package/stdlib/managed-protocol-error.eli +32 -0
  257. package/stdlib/managed-queue-state.eli +65 -0
  258. package/stdlib/managed-runtime-collection-internals.eli +60 -0
  259. package/stdlib/managed-runtime-collection.eli +579 -0
  260. package/stdlib/managed-runtime-data-text.eli +290 -0
  261. package/stdlib/managed-runtime-data.eli +22 -0
  262. package/stdlib/managed-runtime-identifier.eli +44 -0
  263. package/stdlib/managed-runtime-lazy-sequence.eli +131 -0
  264. package/stdlib/managed-runtime-list.eli +19 -0
  265. package/stdlib/managed-runtime-literals.eli +23 -0
  266. package/stdlib/managed-runtime-map.eli +9 -0
  267. package/stdlib/managed-runtime-metadata-internals.eli +6 -0
  268. package/stdlib/managed-runtime-metadata.eli +23 -0
  269. package/stdlib/managed-runtime-object.eli +8 -0
  270. package/stdlib/managed-runtime-order.eli +123 -0
  271. package/stdlib/managed-runtime-persistent-kind.eli +28 -0
  272. package/stdlib/managed-runtime-protocol.eli +28 -0
  273. package/stdlib/managed-runtime-queue.eli +9 -0
  274. package/stdlib/managed-runtime-record.eli +191 -0
  275. package/stdlib/managed-runtime-regex.eli +158 -0
  276. package/stdlib/managed-runtime-sequence.eli +305 -0
  277. package/stdlib/managed-runtime-set-algebra.eli +28 -0
  278. package/stdlib/managed-runtime-set.eli +9 -0
  279. package/stdlib/managed-runtime-sorted.eli +408 -0
  280. package/stdlib/managed-runtime-text.eli +14 -0
  281. package/stdlib/managed-runtime-transducer.eli +268 -0
  282. package/stdlib/managed-runtime-transient.eli +17 -0
  283. package/stdlib/managed-runtime-truth.eli +4 -0
  284. package/stdlib/managed-runtime-type.eli +232 -0
  285. package/stdlib/managed-runtime-value-internals.eli +13 -0
  286. package/stdlib/managed-runtime-value.eli +19 -0
  287. package/stdlib/managed-runtime-vector.eli +20 -0
  288. package/stdlib/managed-sorted-state.eli +233 -0
  289. package/stdlib/managed-testing-value.eli +6 -0
  290. package/stdlib/managed-transient-state.eli +348 -0
  291. package/stdlib/managed-value-state.eli +153 -0
  292. package/stdlib/managed-vector-view-state.eli +102 -0
  293. package/stdlib/managed-wire-http-rpc.eli +362 -0
  294. package/stdlib/managed-wire-markdown.eli +275 -0
  295. package/stdlib/managed-wire-schema.eli +193 -0
  296. package/stdlib/mdp.eli +7 -0
  297. package/stdlib/mds.eli +4 -0
  298. package/stdlib/persistent-vector.eli +23 -6
  299. package/tools/acceptance/check-legacy.mjs +687 -0
  300. package/tools/acceptance/check.eli +431 -0
  301. package/tools/acceptance/compile-evidence.el +49 -0
  302. package/tools/acceptance/finalize-legacy.mjs +498 -0
  303. package/tools/acceptance/finalize.eli +270 -0
  304. package/tools/backends/benchmark.mjs +175 -0
  305. package/tools/backends/build.mjs +135 -0
  306. package/tools/backends/compile.mjs +94 -0
  307. package/tools/backends/compiler-bootstrap.mjs +1001 -0
  308. package/tools/backends/compiler-browser.mjs +361 -0
  309. package/tools/backends/compiler-build.mjs +1628 -0
  310. package/tools/backends/compiler-cli-legacy.mjs +334 -0
  311. package/tools/backends/compiler-cli.mjs +29 -0
  312. package/tools/backends/compiler-corpus-bit.mjs +234 -0
  313. package/tools/backends/compiler-corpus-cli-adapters-legacy.mjs +362 -0
  314. package/tools/backends/compiler-corpus-cli-adapters.mjs +247 -0
  315. package/tools/backends/compiler-corpus-debugging.mjs +221 -0
  316. package/tools/backends/compiler-corpus-emitter.mjs +324 -0
  317. package/tools/backends/compiler-corpus-evaluation-adapters.mjs +65 -0
  318. package/tools/backends/compiler-corpus-execution-legacy.mjs +769 -0
  319. package/tools/backends/compiler-corpus-execution.mjs +3324 -0
  320. package/tools/backends/compiler-corpus-fuzz.mjs +229 -0
  321. package/tools/backends/compiler-corpus-generations.mjs +208 -0
  322. package/tools/backends/compiler-corpus-host-context-legacy.mjs +114 -0
  323. package/tools/backends/compiler-corpus-host-context.mjs +550 -0
  324. package/tools/backends/compiler-corpus-identity-host.mjs +553 -0
  325. package/tools/backends/compiler-corpus-identity-value-adapters-legacy.mjs +298 -0
  326. package/tools/backends/compiler-corpus-identity-value-adapters.mjs +69 -0
  327. package/tools/backends/compiler-corpus-isolated-exports.mjs +21 -0
  328. package/tools/backends/compiler-corpus-legacy.mjs +1155 -0
  329. package/tools/backends/compiler-corpus-literal-provider.mjs +282 -0
  330. package/tools/backends/compiler-corpus-managed-adapters-legacy.mjs +334 -0
  331. package/tools/backends/compiler-corpus-managed-adapters.mjs +937 -0
  332. package/tools/backends/compiler-corpus-managed-cases.mjs +179 -0
  333. package/tools/backends/compiler-corpus-managed-fixtures-legacy.mjs +879 -0
  334. package/tools/backends/compiler-corpus-managed-fixtures.mjs +1287 -0
  335. package/tools/backends/compiler-corpus-matchers.mjs +173 -0
  336. package/tools/backends/compiler-corpus-original-program.mjs +612 -0
  337. package/tools/backends/compiler-corpus-process.mjs +765 -0
  338. package/tools/backends/compiler-corpus-program-driver.mjs +306 -0
  339. package/tools/backends/compiler-corpus-project-adapters-legacy.mjs +497 -0
  340. package/tools/backends/compiler-corpus-project-adapters.mjs +340 -0
  341. package/tools/backends/compiler-corpus-project-entry-host.mjs +511 -0
  342. package/tools/backends/compiler-corpus-project-transport.mjs +151 -0
  343. package/tools/backends/compiler-corpus-raw-fixture-observer.mjs +1438 -0
  344. package/tools/backends/compiler-corpus-raw-language-fixtures.mjs +1456 -0
  345. package/tools/backends/compiler-corpus-raw-math-fixture.mjs +346 -0
  346. package/tools/backends/compiler-corpus-raw-special-fixtures.mjs +523 -0
  347. package/tools/backends/compiler-corpus-reference-legacy.mjs +263 -0
  348. package/tools/backends/compiler-corpus-reference.mjs +241 -0
  349. package/tools/backends/compiler-corpus-runtime-adapters-legacy.mjs +843 -0
  350. package/tools/backends/compiler-corpus-runtime-adapters.mjs +663 -0
  351. package/tools/backends/compiler-corpus-source-archive.mjs +206 -0
  352. package/tools/backends/compiler-corpus-tools-artifact-adapters.mjs +410 -0
  353. package/tools/backends/compiler-corpus-tools-cli.mjs +363 -0
  354. package/tools/backends/compiler-corpus-tools-reference.mjs +264 -0
  355. package/tools/backends/compiler-corpus.mjs +2054 -0
  356. package/tools/backends/compiler-debug.mjs +142 -0
  357. package/tools/backends/compiler-matrix.mjs +1955 -0
  358. package/tools/backends/compiler-metadata.mjs +203 -0
  359. package/tools/backends/compiler-package-browser.mjs +120 -0
  360. package/tools/backends/compiler-package-legacy.mjs +654 -0
  361. package/tools/backends/compiler-package.mjs +464 -0
  362. package/tools/backends/compiler-project.mjs +126 -0
  363. package/tools/backends/compiler-session.mjs +843 -0
  364. package/tools/backends/compiler-sha256.mjs +152 -0
  365. package/tools/backends/compiler-unicode-profile-host.mjs +196 -0
  366. package/tools/backends/compiler-worker.mjs +573 -0
  367. package/tools/backends/dist/bootstrap-js-stages.mjs +919 -0
  368. package/tools/backends/dist/bootstrap-js-stages.mjs.map +1 -0
  369. package/tools/backends/dist/managed-ir-inventory.mjs +488 -0
  370. package/tools/backends/dist/managed-ir-inventory.mjs.map +1 -0
  371. package/tools/backends/dist/record-grid.mjs +1256 -0
  372. package/tools/backends/dist/record-grid.mjs.map +1 -0
  373. package/tools/backends/dist/release-cli.mjs +460 -0
  374. package/tools/backends/dist/release-cli.mjs.map +1 -0
  375. package/tools/backends/dist/run-artifact.mjs +517 -0
  376. package/tools/backends/dist/run-artifact.mjs.map +1 -0
  377. package/tools/backends/eliscript/compiler-corpus-bit.eli +77 -0
  378. package/tools/backends/eliscript/compiler-corpus-debugging.eli +70 -0
  379. package/tools/backends/eliscript/compiler-corpus-project-entry-host.eli +256 -0
  380. package/tools/backends/managed-foreign-binding.mjs +497 -0
  381. package/tools/backends/managed-host-legacy.mjs +1181 -0
  382. package/tools/backends/managed-host-lifecycle.mjs +310 -0
  383. package/tools/backends/managed-host-object-provider.mjs +155 -0
  384. package/tools/backends/managed-host-object-realm.mjs +797 -0
  385. package/tools/backends/managed-host-proxy.mjs +71 -0
  386. package/tools/backends/managed-host-runtime.mjs +5868 -0
  387. package/tools/backends/managed-host.mjs +716 -0
  388. package/tools/backends/managed-module-graph-legacy.mjs +308 -0
  389. package/tools/backends/managed-module-graph.mjs +694 -0
  390. package/tools/backends/managed-runtime-host-legacy.mjs +195 -0
  391. package/tools/backends/managed-runtime.mjs +939 -0
  392. package/tools/backends/managed-source-view-legacy.mjs +332 -0
  393. package/tools/backends/managed-source-view.mjs +959 -0
  394. package/tools/backends/memory-runtime.mjs +100 -0
  395. package/tools/backends/native-benchmark.mjs +88 -0
  396. package/tools/backends/native-managed-runtime.mjs +677 -0
  397. package/tools/backends/native-memory.mjs +768 -0
  398. package/tools/backends/native-node-memory.mjs +142 -0
  399. package/tools/backends/native-node-runner.mjs +99 -0
  400. package/tools/backends/native-node-watchdog.mjs +19 -0
  401. package/tools/backends/native-node-worker.mjs +75 -0
  402. package/tools/backends/stage3-targets.mjs +486 -0
  403. package/tools/bootstrap-command.el +13 -0
  404. package/tools/bootstrap-runtime.el +101 -0
  405. package/tools/bootstrap-seed.el +55 -0
  406. package/tools/project/check-cli.eli +31 -0
  407. package/tools/project/project-cli.eli +42 -0
  408. package/tools/project/project.eli +122 -0
  409. package/tools/runtime/unicode-case-profiles.mjs +13 -0
  410. package/tools/runtime/unicode-case-runtime.el +57 -0
  411. package/tools/testing/source-debugging-corpus-host.eli +16 -0
  412. package/tools/testing/source-debugging.eli +139 -0
  413. package/tools/testing/stdlib-bit-corpus-host.eli +21 -0
  414. package/tools/testing/stdlib-bit-corpus-host.mjs +118 -0
  415. package/tools/testing/stdlib-bit.eli +104 -0
  416. package/tools/testing/stdlib-bit.mjs +255 -0
  417. package/bin/eliscript-bootstrap +0 -15
@@ -11,6 +11,11 @@
11
11
  ir-property)
12
12
  (import "./source-map.mjs" create-source-map)
13
13
 
14
+ ;; Identical capsules share cycle guards and deterministic Number method
15
+ ;; formatting without replacing global prototypes or native function values.
16
+ (defconst array-text-import
17
+ "import { c as __eliscript_at, d as __eliscript_at_direct } from \"data:text/javascript;base64,dmFyIG09UmVmbGVjdC5hcHBseSx0dD1OdW1iZXIucHJvdG90eXBlLnZhbHVlT2Ysaj1OdW1iZXIucHJvdG90eXBlLnRvU3RyaW5nLFU9TnVtYmVyLG50PU9iamVjdC5jcmVhdGUsaD1CaWdJbnQsZXQ9RGF0YVZpZXcsb3Q9QXJyYXlCdWZmZXIsaXQ9RGF0YVZpZXcucHJvdG90eXBlLnNldEZsb2F0NjQsSj1EYXRhVmlldy5wcm90b3R5cGUuZ2V0VWludDMyLHJ0PVJhbmdlRXJyb3I7ZnVuY3Rpb24gY3QodCxuKXtsZXQgZT1oKG4pLG89aCh0KSxpPSIiO2lmKG88MG4pbz0tbztkb3tsZXQgZj1vJWU7aT0iMDEyMzQ1Njc4OWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6IltVKGYpXStpLG8vPWV9d2hpbGUobyE9PTBuKTtyZXR1cm4gdDwwPyItIitpOml9ZnVuY3Rpb24gc3QodCxuKXtsZXQgZT1uZXcgZXQobmV3IG90KDgpKTttKGl0LGUsWzAsdCwhMV0pO2xldCBvPW0oSixlLFswLCExXSksaT1tKEosZSxbNCwhMV0pLGY9bz4+PjIwJjIwNDcsYj1vJjEwNDg1NzUscD1vPj4+MzEscj1oKGIpPDwzMm58aChpKTtpZihmIT09MClyKz0xbjw8NTJuO3I8PD0ybjtsZXQgbD00bix1PWY+MSYmYj09PTAmJmk9PT0wPzFuOjJuLGs9Mm47aWYoZj49MTA3NSl7bGV0IGM9aChmLTEwNzUpO3I8PD1jLHU8PD1jLGs8PD1jfWVsc2UgbDw8PWgoZj09PTA/MTA3NDoxMDc1LWYpO2xldCBkPWgobiksZz1udChudWxsKSxhPTAsUz0wLEQ9MDtpZihyPGwpd2hpbGUocjxsKXtpZihyKjJuPmwmJnIraz5sKWJyZWFrO3IqPWQsdSo9ZCxrKj1kLFMtLX1lbHNlIHdoaWxlKHI+PWwqZClsKj1kLFMrKztmb3IoOzspe2xldCBjPVUoci9sKTtyJT1sO2xldCBJPXI8dSx4PXIraz5sO2dbYSsrXT1jLEQ9RCoobiYxKStjJjE7bGV0IHE9cioybixQPXE+bHx8cT09PWwmJkQ9PT0xO2lmKFAmJnh8fCFQJiZJKXtpZihQKXtsZXQgUj1hLEI9ITA7d2hpbGUoUj4wJiZCKXtSLS07bGV0IEg9Z1tSXSsxO2lmKEg9PT1uKWdbUl09MDtlbHNlIGdbUl09SCxCPSExfWlmKEIpZ1swXT0xLFMrK31icmVha31pZihhPj02NCl0aHJvdyBuZXcgcnQoIm51bWJlciByYWRpeCBmb3JtYXR0aW5nIGRpZCBub3QgY29udmVyZ2UiKTtyKj1kLHUqPWQsayo9ZH13aGlsZShhPjEmJmdbYS0xXT09PTApYS0tO2xldCB3PSIiO2lmKFM8MCl7dz0iMC4iO2ZvcihsZXQgYz0wO2M8LTEtUztjKyspdys9IjAiO2ZvcihsZXQgYz0wO2M8YTtjKyspdys9IjAxMjM0NTY3ODlhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5eiJbZ1tjXV19ZWxzZXtsZXQgYz1TKzEsST1jPmE/YzphO2ZvcihsZXQgeD0wO3g8STt4Kyspe2lmKHg9PT1jJiZjPGEpdys9Ii4iO3crPXg8YT8iMDEyMzQ1Njc4OWFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6IltnW3hdXToiMCJ9fXJldHVybiBwPyItIit3Ond9ZnVuY3Rpb24gRyh0LG4pe2xldCBlO3RyeXtlPW0odHQsdCxbXSl9Y2F0Y2h7cmV0dXJuIG0oaix0LFtuXSl9bGV0IG89MTA7aWYobiE9PXZvaWQgMCl7bGV0IGk9K247aWYobz1pIT09aT8wOmktaSUxLG88Mnx8bz4zNnx8byE9PW8pcmV0dXJuIG0oaixlLFtvXSl9aWYobz09PTEwfHxlPT09MHx8ZSE9PWV8fGU9PT0xLzB8fGU9PT0tMS8wKXJldHVybiBtKGosZSxbMTBdKTtyZXR1cm4gZSUxPT09MD9jdChlLG8pOnN0KGUsbyl9dmFyIHM9UmVmbGVjdC5hcHBseSxNPUZ1bmN0aW9uLnByb3RvdHlwZS5jYWxsLE49RnVuY3Rpb24ucHJvdG90eXBlLmFwcGx5LEU9RnVuY3Rpb24ucHJvdG90eXBlLmJpbmQsVD1PYmplY3QuZGVmaW5lUHJvcGVydHksbHQ9T2JqZWN0LmdldE93blByb3BlcnR5RGVzY3JpcHRvcixmdD1PYmplY3QuZ2V0UHJvdG90eXBlT2YsdXQ9T2JqZWN0LnNldFByb3RvdHlwZU9mLGF0PVJlZmxlY3Qub3duS2V5cyxwdD1TeW1ib2wuaGFzSW5zdGFuY2UsVj1BcnJheS5pc0FycmF5LEs9QXJyYXkucHJvdG90eXBlLmpvaW4sej1BcnJheS5wcm90b3R5cGUudG9TdHJpbmcseXQ9T2JqZWN0LnByb3RvdHlwZS50b1N0cmluZyxidD1TdHJpbmcsQz1UeXBlRXJyb3IsZHQ9U3ltYm9sLnRvUHJpbWl0aXZlLGd0PVdlYWtTZXQucHJvdG90eXBlLmhhcyx3dD1XZWFrU2V0LnByb3RvdHlwZS5hZGQseHQ9V2Vha1NldC5wcm90b3R5cGUuZGVsZXRlLEY9bmV3IFdlYWtTZXQsTz1udWxsLFc9KHQpPT50IT09bnVsbCYmKHR5cGVvZiB0PT09Im9iamVjdCJ8fHR5cGVvZiB0PT09ImZ1bmN0aW9uInx8dHlwZW9mIHQ+InUiJiZ0IT09dm9pZCAwKSxBPSh0KT0+dHlwZW9mIHQ9PT0iZnVuY3Rpb24ifHx0eXBlb2YgdD4idSImJnQhPT12b2lkIDA7ZnVuY3Rpb24gaHQoKXtsZXQgbj1uZXcgQygiY3ljbGljIGFycmF5IHRleHQgY29udmVyc2lvbiIpO3Rocm93IG4uZWxpc2NyaXB0RGlhZ25vc3RpYz17Zm9ybWF0OiJlbGlzY3JpcHQtZGlhZ25vc3RpYyIsdmVyc2lvbjoxLGNvZGU6IkVMSS1SVDAwMDkiLHNldmVyaXR5OiJlcnJvciIscGhhc2U6InJ1bnRpbWUiLG1lc3NhZ2U6ImN5Y2xpYyBhcnJheSB0ZXh0IGNvbnZlcnNpb24ifSxufWZ1bmN0aW9uIG10KHQpe2lmKCFXKHQpKXJldHVybiB0O2xldCBuPXRbZHRdO2lmKG4hPT12b2lkIDAmJm4hPT1udWxsKXtsZXQgZT1zKG4sdCxbInN0cmluZyJdKTtpZighVyhlKSlyZXR1cm4gZTt0aHJvdyBuZXcgQygiQ2Fubm90IGNvbnZlcnQgb2JqZWN0IHRvIHByaW1pdGl2ZSB2YWx1ZSIpfWZvcihsZXQgZT0wO2U8MjtlKyspe2xldCBvPXRbZT09PTA/InRvU3RyaW5nIjoidmFsdWVPZiJdO2lmKEEobykpe2xldCBpPW89PT16JiZWKHQpP3YodCk6cyhvLHQsW10pO2lmKCFXKGkpKXJldHVybiBpfX10aHJvdyBuZXcgQygiQ2Fubm90IGNvbnZlcnQgb2JqZWN0IHRvIHByaW1pdGl2ZSB2YWx1ZSIpfWZ1bmN0aW9uIFEodCl7bGV0IG49bXQodCk7aWYodHlwZW9mIG49PT0ic3ltYm9sIil0aHJvdyBuZXcgQygiQ2Fubm90IGNvbnZlcnQgYSBTeW1ib2wgdmFsdWUgdG8gYSBzdHJpbmciKTtyZXR1cm4gYnQobil9ZnVuY3Rpb24gXyh0LG4pe2xldCBlPSt0Lmxlbmd0aCxvPWUhPT1lfHxlPD0wPzA6ZT49OTAwNzE5OTI1NDc0MDk5MT85MDA3MTk5MjU0NzQwOTkxOmUtZSUxLGk9bj09PXZvaWQgMD8iLCI6UShuKSxmPU89PT1udWxsO2lmKGYpTz10O2Vsc2V7aWYodD09PU98fHMoZ3QsRixbdF0pKWh0KCk7cyh3dCxGLFt0XSl9dHJ5e2xldCBiPSIiO2ZvcihsZXQgcD0wO3A8bztwKyspe2lmKHApYis9aTtsZXQgcj10W3BdO2lmKHIhPT1udWxsJiZyIT09dm9pZCAwKWIrPVEocil9cmV0dXJuIGJ9ZmluYWxseXtpZihmKU89bnVsbDtlbHNlIHMoeHQsRixbdF0pfX1mdW5jdGlvbiB2KHQpe2xldCBuPXQuam9pbjtyZXR1cm4gQShuKT9uPT09Sz9fKHQsdm9pZCAwKTpzKG4sdCxbXSk6cyh5dCx0LFtdKX1mdW5jdGlvbiB5KHQsbil7cmV0dXJuIG48dC5sZW5ndGg/dFtuXTp2b2lkIDB9ZnVuY3Rpb24gWCh0LG4pe2xldCBlPVtdO2ZvcihsZXQgbz1uO288dC5sZW5ndGg7bysrKVQoZSxlLmxlbmd0aCx7dmFsdWU6dFtvXSx3cml0YWJsZTohMCxlbnVtZXJhYmxlOiEwLGNvbmZpZ3VyYWJsZTohMH0pO3JldHVybiBlfWZ1bmN0aW9uIFN0KHQsbil7bGV0IGU9W107Zm9yKGxldCBvPTA7bzx0Lmxlbmd0aCtuLmxlbmd0aDtvKyspVChlLG8se3ZhbHVlOm88dC5sZW5ndGg/dFtvXTpuW28tdC5sZW5ndGhdLHdyaXRhYmxlOiEwLGVudW1lcmFibGU6ITAsY29uZmlndXJhYmxlOiEwfSk7cmV0dXJuIGV9ZnVuY3Rpb24gWSguLi50KXtyZXR1cm4gdH1mdW5jdGlvbiBaKHQsbil7cmV0dXJuIG4/cyhOLFksW3ZvaWQgMCx0XSk6cyhZLHZvaWQgMCx0KX1mdW5jdGlvbiBMKHQsbixlKXtmb3IoOzspe2lmKHQ9PT1qKXJldHVybiBHKG4seShlLDApKTtpZih0PT09SyYmVihuKSlyZXR1cm4gXyhuLHkoZSwwKSk7aWYodD09PXomJlYobikpcmV0dXJuIHYobik7aWYodD09PU0pe2lmKCFBKG4pKXJldHVybiBzKHQsbixlKTtsZXQgbz1uLGk9eShlLDApO2U9WChlLDEpLHQ9byxuPWk7Y29udGludWV9aWYodD09PU4pe2lmKCFBKG4pKXJldHVybiBzKHQsbixlKTtsZXQgbz1uLGk9eShlLDApO2U9Wih5KGUsMSksITApLHQ9byxuPWk7Y29udGludWV9aWYodD09PXMpe2xldCBvPXkoZSwwKSxpPXkoZSwxKTtpZighQShvKSlyZXR1cm4gcyh0LG4sZSk7ZT1aKHkoZSwyKSwhMSksdD1vLG49aTtjb250aW51ZX1pZih0PT09RSl7bGV0IG89cyh0LG4sZSk7aWYobiE9PWomJm4hPT1LJiZuIT09eiYmbiE9PU0mJm4hPT1OJiZuIT09cyYmbiE9PUUpcmV0dXJuIG87bGV0IGk9bixmPXkoZSwwKSxiPVgoZSwxKSxwPSguLi51KT0+TChpLGYsU3QoYix1KSk7VChwLHB0LHt2YWx1ZToodSk9PnUgaW5zdGFuY2VvZiBpLGNvbmZpZ3VyYWJsZTohMH0pO2xldCByPXMoRSxwLFt2b2lkIDBdKTt1dChyLGZ0KG8pKTtsZXQgbD1hdChvKTtmb3IobGV0IHU9MDt1PGwubGVuZ3RoO3UrKylUKHIsbFt1XSxsdChvLGxbdV0pKTtyZXR1cm4gcn1yZXR1cm4gcyh0LG4sZSl9fWZ1bmN0aW9uIFJ0KHQsbil7bGV0IGU9dFtuXTtyZXR1cm4oLi4ubyk9PkwoZSx0LG8pfWZ1bmN0aW9uIEF0KHQsbiwuLi5lKXtsZXQgbz10W25dO3JldHVybiBMKG8sdCxlKX1leHBvcnR7UnQgYXMgYyxBdCBhcyBkfTs=\";\n")
18
+
14
19
  (defconst literal-runtime-import
15
20
  "import { hashMap as __eliscript_hash_map, hashSet as __eliscript_hash_set, keyword as __eliscript_keyword, list as __eliscript_list, queue as __eliscript_queue, symbol as __eliscript_symbol, vector as __eliscript_vector } from \"eliscript/runtime/literals.mjs\";\n")
16
21
  (defconst collection-runtime-import
@@ -21,29 +26,215 @@
21
26
  "import { equalValues as __eliscript_equal } from \"eliscript/runtime/core/value.mjs\";\n")
22
27
  (defconst binding-runtime
23
28
  "import { get as __eliscript_get } from \"eliscript/runtime/core/collection.mjs\";\nconst __eliscript_binding_missing = Symbol(\"eliscript.binding.missing\");\nconst __eliscript_binding_get = (value, key, notFound) => { if (value == null) return notFound; const prototype = typeof value === \"object\" ? Object.getPrototypeOf(value) : undefined; if (prototype === Object.prototype || prototype === null) { const type = key && Object.getOwnPropertyDescriptor(key, Symbol.for(\"eliscript.value.type\")); if (type?.value === \"keyword\" || type?.value === \"symbol\") key = key.qualifiedName; } return __eliscript_get(value, key, notFound); };\n")
29
+ (defconst number-conversion-intrinsics
30
+ "const __eliscript_number_intrinsic = globalThis.Number;\nconst __eliscript_bigint_intrinsic = globalThis.BigInt;\nconst __eliscript_numeric_apply = globalThis.Reflect.apply;\nconst __eliscript_numeric_symbol = globalThis.Symbol.toPrimitive;\nconst __eliscript_numeric_error = globalThis.TypeError;\nconst __eliscript_numeric_trim = globalThis.String.prototype.trim;\nconst __eliscript_numeric_slice = globalThis.String.prototype.slice;\nconst __eliscript_numeric_exec = globalThis.RegExp.prototype.exec;\n")
31
+ (defconst number-conversion-capsule
32
+ "import { __eliscript_number_intrinsic, __eliscript_bigint_intrinsic, __eliscript_numeric_apply, __eliscript_numeric_symbol, __eliscript_numeric_error, __eliscript_numeric_trim, __eliscript_numeric_slice, __eliscript_numeric_exec } from \"data:text/javascript;base64,ZXhwb3J0IGNvbnN0IF9fZWxpc2NyaXB0X251bWJlcl9pbnRyaW5zaWMgPSBnbG9iYWxUaGlzLk51bWJlcjsKZXhwb3J0IGNvbnN0IF9fZWxpc2NyaXB0X2JpZ2ludF9pbnRyaW5zaWMgPSBnbG9iYWxUaGlzLkJpZ0ludDsKZXhwb3J0IGNvbnN0IF9fZWxpc2NyaXB0X251bWVyaWNfYXBwbHkgPSBnbG9iYWxUaGlzLlJlZmxlY3QuYXBwbHk7CmV4cG9ydCBjb25zdCBfX2VsaXNjcmlwdF9udW1lcmljX3N5bWJvbCA9IGdsb2JhbFRoaXMuU3ltYm9sLnRvUHJpbWl0aXZlOwpleHBvcnQgY29uc3QgX19lbGlzY3JpcHRfbnVtZXJpY19lcnJvciA9IGdsb2JhbFRoaXMuVHlwZUVycm9yOwpleHBvcnQgY29uc3QgX19lbGlzY3JpcHRfbnVtZXJpY190cmltID0gZ2xvYmFsVGhpcy5TdHJpbmcucHJvdG90eXBlLnRyaW07CmV4cG9ydCBjb25zdCBfX2VsaXNjcmlwdF9udW1lcmljX3NsaWNlID0gZ2xvYmFsVGhpcy5TdHJpbmcucHJvdG90eXBlLnNsaWNlOwpleHBvcnQgY29uc3QgX19lbGlzY3JpcHRfbnVtZXJpY19leGVjID0gZ2xvYmFsVGhpcy5SZWdFeHAucHJvdG90eXBlLmV4ZWM7Cg==\";\n")
33
+ (defconst number-conversion-body
34
+ "const __eliscript_numeric_radix = /^(?:0[xX][0-9a-fA-F]+|0[oO][0-7]+|0[bB][01]+)$/;\nconst __eliscript_numeric_object = (value) => value !== null && (typeof value === \"object\" || typeof value === \"function\" || (typeof value === \"undefined\" && value !== void 0));\nconst __eliscript_numeric_primitive = (value) => {\n if (!__eliscript_numeric_object(value)) return value;\n const exotic = value[__eliscript_numeric_symbol];\n if (exotic !== void 0 && exotic !== null) {\n const result = __eliscript_numeric_apply(exotic, value, [\"number\"]);\n if (!__eliscript_numeric_object(result)) return result;\n throw new __eliscript_numeric_error(\"Cannot convert object to primitive value\");\n }\n for (let index = 0; index < 2; index++) {\n const method = value[index === 0 ? \"valueOf\" : \"toString\"];\n if (typeof method === \"function\" || (typeof method === \"undefined\" && method !== void 0)) {\n const result = __eliscript_numeric_apply(method, value, []);\n if (!__eliscript_numeric_object(result)) return result;\n }\n }\n throw new __eliscript_numeric_error(\"Cannot convert object to primitive value\");\n};\nconst __eliscript_to_number = (value) => {\n const primitive = __eliscript_numeric_primitive(value);\n if (typeof primitive === \"string\") {\n const text = __eliscript_numeric_apply(__eliscript_numeric_trim, primitive, []);\n if (__eliscript_numeric_apply(__eliscript_numeric_exec, __eliscript_numeric_radix, [text]) !== null) {\n let first = 2;\n while (first < text.length && text[first] === \"0\") first++;\n if (first === text.length) return 0;\n const prefix = text[1];\n const limit = prefix === \"x\" || prefix === \"X\" ? 256 : prefix === \"b\" || prefix === \"B\" ? 1024 : 342;\n if (text.length - first > limit) return 1 / 0;\n const token = __eliscript_numeric_apply(__eliscript_numeric_slice, text, [0, 2]) + __eliscript_numeric_apply(__eliscript_numeric_slice, text, [first]);\n return __eliscript_number_intrinsic(__eliscript_bigint_intrinsic(token));\n }\n }\n return __eliscript_number_intrinsic(primitive);\n};\n")
35
+ (defconst number-conversion-helper
36
+ (str number-conversion-intrinsics number-conversion-body))
37
+ (defconst numeric-operator-helper
38
+ "const __eliscript_numeric = (value) => {\n if (typeof value === \"number\" || typeof value === \"bigint\") return value;\n const primitive = __eliscript_numeric_primitive(value);\n if (typeof primitive === \"number\" || typeof primitive === \"bigint\") return primitive;\n return typeof primitive === \"string\" ? __eliscript_to_number(primitive) : +primitive;\n};\nconst __eliscript_implicit_number = (value) => {\n if (typeof value === \"number\") return value;\n const primitive = __eliscript_numeric_primitive(value);\n if (typeof primitive === \"bigint\") throw new __eliscript_numeric_error(\"Cannot convert a BigInt value to a number\");\n return typeof primitive === \"string\" ? __eliscript_to_number(primitive) : +primitive;\n};\nconst __eliscript_numeric_decrement = (value) => __eliscript_implicit_number(value) - 1;\nconst __eliscript_numeric_sub = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a - b; return __eliscript_numeric(a) - __eliscript_numeric(b); };\nconst __eliscript_numeric_mul = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a * b; return __eliscript_numeric(a) * __eliscript_numeric(b); };\nconst __eliscript_numeric_div = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a / b; return __eliscript_numeric(a) / __eliscript_numeric(b); };\nconst __eliscript_numeric_rem = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a % b; return __eliscript_numeric(a) % __eliscript_numeric(b); };\nconst __eliscript_numeric_and = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a & b; return __eliscript_numeric(a) & __eliscript_numeric(b); };\nconst __eliscript_numeric_or = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a | b; return __eliscript_numeric(a) | __eliscript_numeric(b); };\nconst __eliscript_numeric_xor = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a ^ b; return __eliscript_numeric(a) ^ __eliscript_numeric(b); };\nconst __eliscript_numeric_shl = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a << b; return __eliscript_numeric(a) << __eliscript_numeric(b); };\nconst __eliscript_numeric_shr = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a >> b; return __eliscript_numeric(a) >> __eliscript_numeric(b); };\nconst __eliscript_numeric_ushr = (a, b) => { if (typeof a === \"number\" && typeof b === \"number\") return a >>> b; return __eliscript_numeric(a) >>> __eliscript_numeric(b); };\nconst __eliscript_numeric_neg = (a) => typeof a === \"number\" ? -a : -__eliscript_numeric(a);\nconst __eliscript_numeric_int32 = (a) => typeof a === \"number\" ? a | 0 : __eliscript_numeric(a) | 0;\nconst __eliscript_numeric_uint32 = (a) => typeof a === \"number\" ? a >>> 0 : __eliscript_numeric(a) >>> 0;\nconst __eliscript_numeric_not = (a) => typeof a === \"number\" ? ~a : ~__eliscript_numeric(a);\nconst __eliscript_numeric_relation = (operator, left, right) => {\n if (typeof left === \"number\" && typeof right === \"number\") {\n if (operator === \"<\") return left < right;\n if (operator === \"<=\") return left <= right;\n if (operator === \">\") return left > right;\n return left >= right;\n }\n let a = __eliscript_numeric_primitive(left);\n let b = __eliscript_numeric_primitive(right);\n const x = typeof a, y = typeof b;\n if (!((x === \"string\" && y === \"string\") || (x === \"bigint\" && y === \"string\") || (x === \"string\" && y === \"bigint\"))) {\n a = __eliscript_numeric(a);\n b = __eliscript_numeric(b);\n }\n if (operator === \"<\") return a < b;\n if (operator === \"<=\") return a <= b;\n if (operator === \">\") return a > b;\n return a >= b;\n};\n")
39
+ (defconst imul-intrinsic
40
+ "const __eliscript_numeric_imul_intrinsic = globalThis.Math.imul;\n")
41
+ (defconst imul-capsule
42
+ "import { __eliscript_numeric_imul_intrinsic } from \"data:text/javascript;base64,ZXhwb3J0IGNvbnN0IF9fZWxpc2NyaXB0X251bWVyaWNfaW11bF9pbnRyaW5zaWMgPSBnbG9iYWxUaGlzLk1hdGguaW11bDsK\";\n")
43
+ (defconst imul-operator-helper
44
+ "const __eliscript_numeric_imul = (a, b) => __eliscript_numeric_imul_intrinsic(__eliscript_implicit_number(a), __eliscript_implicit_number(b));\n")
45
+ (defvar numeric-emission-profile false)
24
46
  (defconst host-identity-token-helper
25
47
  "const __eliscript_host_identity_token = (() => { const objects = new WeakMap(); const symbols = new Map(); let next = 1; return (value) => { const type = typeof value; if ((type !== \"object\" || value === null) && type !== \"function\" && type !== \"symbol\") throw new TypeError(\"host-identity-token expects an object, function, or symbol\"); const identities = type === \"symbol\" ? symbols : objects; const cached = identities.get(value); if (cached !== undefined) return cached; if (next > Number.MAX_SAFE_INTEGER) throw new RangeError(\"host identity token space exhausted\"); const token = next; next += 1; identities.set(value, token); return token; }; })();\n")
26
48
  (defconst fail
27
- (js* "(message) => { throw new Error(message); }"))
49
+ (lambda (message)
50
+ (throw (new (js* "Error") message))))
51
+ (defconst prefix-locate-mark (js* "(head, tail) => [head, ...tail]"))
52
+ (defvar function-source-profile false)
28
53
  (defconst native-locate
29
- (js* "(span, output) => { const fragment = typeof output === \"string\" ? { text: output, marks: [] } : output; const marks = fragment.marks; if (fragment.text.length === 0 || span == null || span === false || (marks.length > 0 && marks[0].offset === 0)) return fragment; return { text: fragment.text, marks: [{ offset: 0, span }, ...marks] }; }"))
54
+ (lambda (span output)
55
+ (let* ((fragment (if (= (value-type output) "string")
56
+ (js-object :text output :marks (js-array)) output))
57
+ (marks (aref fragment "marks")))
58
+ ;; Function text capture consumes function ranges only. Building the
59
+ ;; unrelated per-expression Source Map marks here copies every child's
60
+ ;; marks through its ancestors while retaining the complete source IR.
61
+ ;; Ordinary JS and Source Map emission keep the original locate path.
62
+ (if (or function-source-profile
63
+ (= (aref (aref fragment "text") "length") 0)
64
+ (nullish? span) (eq span false)
65
+ (and (> (aref marks "length") 0) (= (aref (aref marks 0) "offset") 0)))
66
+ fragment
67
+ (let ((result (js-object :text (aref fragment "text")
68
+ :marks (prefix-locate-mark (js-object :offset 0 :span span) marks))))
69
+ (when function-source-profile (put result :functions (get fragment :functions (js-array))))
70
+ result)))))
30
71
  (defvar temporary-counter 0)
31
72
  (defvar binding-temporaries (new globalThis.Map))
32
-
33
- (defun make-fragment (text marks)
34
- (js-object :text text :marks marks))
73
+ (defconst tdz-import "import { tdz as __eliscript_tdz_error, immutable as __eliscript_const_error } from \"data:text/javascript;base64,Y29uc3QgRSA9IFJlZmVyZW5jZUVycm9yOyBjb25zdCBUID0gVHlwZUVycm9yOyBleHBvcnQgZnVuY3Rpb24gdGR6KG5hbWUpIHsgdGhyb3cgbmV3IEUoIkNhbm5vdCBhY2Nlc3MgJyIgKyBuYW1lICsgIicgYmVmb3JlIGluaXRpYWxpemF0aW9uIik7IH0gZXhwb3J0IGZ1bmN0aW9uIGltbXV0YWJsZSgpIHsgdGhyb3cgbmV3IFQoIkFzc2lnbm1lbnQgdG8gY29uc3RhbnQgdmFyaWFibGUuIik7IH0K\";\n")
74
+ ;; Resolve module bindings once, without rewriting source names or raw JS.
75
+ (defvar tdz-state nil)
76
+ (defun tdz-ready-name (record) (str "__eliscript_tdz_ready_" (get record :index)))
77
+ (defun tdz-root-name (name)
78
+ (let ((dot (js-call name :indexOf ".")) (slash (js-call name :indexOf "/")))
79
+ (let ((end (if (< dot 0) slash (if (< slash 0) dot (if (< dot slash) dot slash)))))
80
+ (if (< end 0) name (js-call name :slice 0 end)))))
81
+ (defun tdz-scope (parent) (js-object :parent parent :names (new globalThis.Map)))
82
+ (defun tdz-find (scope name)
83
+ (let ((current scope) (found false) (record nil) (key (binding-name name)))
84
+ (while (and current (not found))
85
+ (let ((names (get current :names)))
86
+ (if (js-call names :has key)
87
+ (progn (setq record (js-call names :get key)) (setq found t))
88
+ (setq current (get current :parent))))) record))
89
+ (defun tdz-pattern-names (node scope)
90
+ (let ((kind (ir-kind node)) (nodes (children node)))
91
+ (cond
92
+ ((= kind "binding-name") (js-call (get scope :names) :set (binding-name (ir-value node)) nil))
93
+ ((= kind "map-binding-entry") (tdz-pattern-names (js-nth 0 nodes) scope))
94
+ ((or (= kind "array-binding-pattern") (= kind "map-binding-pattern"))
95
+ (js-call nodes :forEach (lambda (child) (tdz-pattern-names child scope)))))))
96
+ (defun tdz-bind (node scope)
97
+ (if (ir-property node "pattern") (tdz-pattern-names (js-nth 0 (children node)) scope)
98
+ (js-call (get scope :names) :set (binding-name (ir-value node)) nil)))
99
+ (defun tdz-pattern-defaults (node scope state)
100
+ (let ((kind (ir-kind node)) (nodes (children node)))
101
+ (cond
102
+ ((= kind "map-binding-entry")
103
+ (when (> (js-length nodes) 2) (tdz-walk (js-nth 2 nodes) scope state))
104
+ (tdz-pattern-defaults (js-nth 0 nodes) scope state))
105
+ ((or (= kind "array-binding-pattern") (= kind "map-binding-pattern"))
106
+ (js-call nodes :forEach (lambda (child) (tdz-pattern-defaults child scope state)))))))
107
+ (defun tdz-defaults (node scope state)
108
+ (when (ir-property node "pattern")
109
+ (tdz-pattern-defaults (js-nth 0 (children node)) scope state)))
110
+ (defun tdz-predeclare (nodes scope state)
111
+ (js-call nodes :forEach
112
+ (lambda (node)
113
+ (let ((kind (ir-kind node)))
114
+ (cond
115
+ ((= kind "module-declaration") (tdz-predeclare (children node) scope state))
116
+ ((= kind "variable-declaration")
117
+ (let ((record (js-object :name (ir-value node) :index (js-length (get state :variables)) :mutable (ir-property node "mutable"))))
118
+ (js-call (get state :variables) :push record)
119
+ (js-call (get state :declarations) :set node record)
120
+ (js-call (get scope :names) :set (binding-name (ir-value node)) record)))
121
+ ((= kind "function-declaration") (js-call (get scope :names) :set (binding-name (ir-value node)) nil))
122
+ ((= kind "import-declaration")
123
+ (js-call (children node) :forEach
124
+ (lambda (child) (js-call (get scope :names) :set (binding-name (ir-value child)) nil)))))))))
125
+ (defun tdz-walk (node scope state)
126
+ (let ((kind (ir-kind node)) (nodes (children node)))
127
+ (cond
128
+ ((or (= kind "reference") (= kind "assignment-pair"))
129
+ (let ((record (tdz-find scope (tdz-root-name (ir-value node)))))
130
+ (when record
131
+ (js-call (get state (if (= kind "reference") "reads" "writes")) :set node record)))
132
+ (js-call nodes :forEach (lambda (child) (tdz-walk child scope state))))
133
+ ((or (= kind "function-declaration") (= kind "function-expression"))
134
+ (let ((child (tdz-scope scope)) (count (ir-property node "parameterCount")) (index 0))
135
+ (while (< index count) (tdz-defaults (js-nth index nodes) scope state) (setq index (1+ index)))
136
+ (setq index 0)
137
+ (while (< index count) (tdz-bind (js-nth index nodes) child) (setq index (1+ index)))
138
+ (while (< index (js-length nodes)) (tdz-walk (js-nth index nodes) child state) (setq index (1+ index)))))
139
+ ((or (= kind "lexical-bindings") (= kind "binding-loop"))
140
+ (let ((child (tdz-scope scope)) (count (ir-property node "bindingCount"))
141
+ (sequential (ir-property node "sequential")) (index 0))
142
+ (while (< index count)
143
+ (let* ((binding (js-nth index nodes)) (values (children binding))
144
+ (outer (if sequential child scope)) (start (if (ir-property binding "pattern") 1 0)))
145
+ (while (< start (js-length values)) (tdz-walk (js-nth start values) outer state) (setq start (1+ start)))
146
+ (tdz-defaults binding outer state)
147
+ (when sequential (tdz-bind binding child)))
148
+ (setq index (1+ index)))
149
+ (unless sequential
150
+ (setq index 0)
151
+ (while (< index count) (tdz-bind (js-nth index nodes) child) (setq index (1+ index))))
152
+ (while (< index (js-length nodes)) (tdz-walk (js-nth index nodes) child state) (setq index (1+ index)))))
153
+ ((= kind "catch-clause")
154
+ (let ((child (tdz-scope scope)) (binding (js-nth 0 nodes)) (index 1))
155
+ (tdz-defaults binding scope state) (tdz-bind binding child)
156
+ (while (< index (js-length nodes)) (tdz-walk (js-nth index nodes) child state) (setq index (1+ index)))))
157
+ ((or (= kind "quoted-literal") (= kind "raw-javascript") (= kind "import-declaration")) nil)
158
+ (t (js-call nodes :forEach (lambda (child) (tdz-walk child scope state)))))))
159
+ (defun tdz-prepare (program)
160
+ (let ((state (js-object :variables (js-array) :declarations (new globalThis.Map)
161
+ :reads (new globalThis.Map) :writes (new globalThis.Map)))
162
+ (scope (tdz-scope nil)))
163
+ (tdz-predeclare (program-body program) scope state)
164
+ ;; With no tracked module variable, every possible lookup is absent.
165
+ ;; Function-only modules therefore need no TDZ traversal of their bodies.
166
+ (when (> (js-length (get state :variables)) 0)
167
+ (js-call (program-body program) :forEach (lambda (node) (tdz-walk node scope state)))) state))
168
+ (defun tdz-reader-name (record) (str "__eliscript_tdz_read_" (get record :index)))
169
+ (defun tdz-writer-name (record) (str "__eliscript_tdz_write_" (get record :index)))
170
+ (defun tdz-prologue ()
171
+ (if (or (nil? tdz-state) (= (js-length (get tdz-state :variables)) 0)) ""
172
+ (str tdz-import
173
+ (js-call (js-call (get tdz-state :variables) :map
174
+ (lambda (record)
175
+ (let ((ready (tdz-ready-name record)) (name (binding-name (get record :name)))
176
+ (argument (str "__eliscript_tdz_value_" (get record :index))))
177
+ (str "var " ready ";\n"
178
+ "function " (tdz-reader-name record) "() { if (" ready
179
+ " !== true) __eliscript_tdz_error(" (json-string name) "); return " name "; }\n"
180
+ "function " (tdz-writer-name record) "(" argument ") { if (" ready
181
+ " !== true) __eliscript_tdz_error(" (json-string name) "); "
182
+ (if (get record :mutable) (str "return (" name " = " argument ");") "return __eliscript_const_error();") " }\n")))) :join ""))))
183
+ (defun tdz-read (node)
184
+ (let ((record (and tdz-state (js-call (get tdz-state :reads) :get node))))
185
+ (if (not record) (text-fragment (reference-name (ir-value node)))
186
+ (let* ((name (binding-name (get record :name))) (reference (reference-name (ir-value node))))
187
+ (text-fragment (str "(" (tdz-reader-name record) "())" (js-call reference :slice (js-length name))))))))
188
+ (defun tdz-assignment (pair)
189
+ (let* ((record (and tdz-state (js-call (get tdz-state :writes) :get pair)))
190
+ (value (emit-expression (js-nth 0 (children pair)))))
191
+ (if (not record) (combine (js-array "(" (emit-binding-name pair) " = " value ")"))
192
+ (combine (js-array (tdz-writer-name record) "(" value ")")))))
193
+
194
+
195
+
196
+ (defun make-fragment (text marks &optional functions)
197
+ (let ((fragment (js-object :text text :marks marks)))
198
+ (when function-source-profile (put fragment :functions (or functions (js-array)))) fragment))
199
+
200
+ (defun function-source-fragment (node output start end)
201
+ (let ((fragment (as-fragment output)))
202
+ (when function-source-profile
203
+ (let ((functions (js-call (get fragment :functions (js-array)) :slice)))
204
+ (js-call functions :push (js-object :node node :start start :end end))
205
+ (put fragment :functions functions))) fragment))
206
+
207
+ (defun function-indented-offset (starts offset)
208
+ (let ((low 0) (high (js-length starts)))
209
+ (while (< low high)
210
+ (let ((middle (+ low (Math/floor (/ (- high low) 2)))))
211
+ (if (<= (js-nth middle starts) offset) (setq low (1+ middle)) (setq high middle))))
212
+ (+ offset (* low 2))))
213
+
214
+ (defun indent-function-sources (source functions)
215
+ (let ((starts (js-array)) (parts (js-call source :split "\n")) (offset 0) (index 0))
216
+ (while (< index (js-length parts))
217
+ (let ((line (js-nth index parts)))
218
+ (when (> (js-length line) 0) (js-call starts :push offset))
219
+ (setq offset (+ offset (js-length line) 1)))
220
+ (setq index (1+ index)))
221
+ (js-call functions :map (lambda (record)
222
+ (js-object :node (get record :node)
223
+ :start (function-indented-offset starts (get record :start))
224
+ :end (function-indented-offset starts (get record :end)))))))
35
225
 
36
226
  (defun text-fragment (text)
37
227
  (make-fragment text (js-array)))
38
228
 
39
229
  (defun as-fragment (part)
40
- (if (= (js* "typeof part") "string")
230
+ (if (= (value-type part) "string")
41
231
  (text-fragment part)
42
232
  part))
43
233
 
44
234
  (defun combine (parts)
45
235
  (let ((text "")
46
236
  (marks (js-array))
237
+ (functions (js-array))
47
238
  (part-index 0)
48
239
  (fragment nil)
49
240
  (offset 0)
@@ -63,8 +254,12 @@
63
254
  :span (get mark :span)))
64
255
  (setq mark-index (1+ mark-index)))
65
256
  (setq text (str text (get fragment :text)))
257
+ (when function-source-profile
258
+ (js-call (get fragment :functions (js-array)) :forEach
259
+ (lambda (record) (js-call functions :push
260
+ (js-object :node (get record :node) :start (+ offset (get record :start)) :end (+ offset (get record :end)))))))
66
261
  (setq part-index (1+ part-index)))
67
- (make-fragment text marks)))
262
+ (make-fragment text marks functions)))
68
263
 
69
264
  (defun join-fragments (parts separator)
70
265
  (let ((joined (js-array))
@@ -99,47 +294,44 @@
99
294
  (setq text (str text (js-call source :charAt index)))
100
295
  (setq line-start (= code 10))
101
296
  (setq index (1+ index))))
102
- (make-fragment text marks)))
297
+ (make-fragment text marks (when function-source-profile
298
+ (indent-function-sources source (get input :functions (js-array)))))))
299
+
300
+ (defconst native-indent
301
+ (lambda (input)
302
+ (let* ((source (aref input "text"))
303
+ (source-marks (aref input "marks"))
304
+ (parts (js-call source :split "\n"))
305
+ (marks (js-array))
306
+ (start 0)
307
+ (added 0)
308
+ (mark-index 0)
309
+ (mark (aref source-marks 0))
310
+ (index 0))
311
+ (while (< index (aref parts "length"))
312
+ (let* ((line (aref parts index))
313
+ (end (+ (+ start (aref line "length"))
314
+ (if (< (+ index 1) (aref parts "length")) 1 0))))
315
+ (when (> (aref line "length") 0)
316
+ (put parts index (+ " " line))
317
+ (setq added (+ added 2)))
318
+ (while (and (not (eq mark undefined))
319
+ (>= (aref mark "offset") start)
320
+ (< (aref mark "offset") end))
321
+ (js-call marks :push
322
+ (js-object :offset (+ (aref mark "offset") added)
323
+ :span (aref mark "span")))
324
+ (setq mark-index (+ mark-index 1))
325
+ (setq mark (aref source-marks mark-index)))
326
+ (setq start end))
327
+ (setq index (+ index 1)))
328
+ (js-object :text (js-call parts :join "\n") :marks marks))))
103
329
 
104
330
  (defun indent (fragment)
105
- (let* ((input (as-fragment fragment))
106
- (source (get input :text))
107
- (source-marks (get input :marks))
108
- (text-parts (js-array))
109
- (marks (js-array))
110
- (source-length (get source :length))
111
- (output-length 0)
112
- (source-index 0)
113
- (mark-index 0)
114
- (mark-count (get source-marks :length)))
115
- (while (< source-index source-length)
116
- (let* ((newline-index (js-call source :indexOf "\n" source-index))
117
- (segment-end
118
- (if (= newline-index -1)
119
- source-length
120
- (1+ newline-index)))
121
- (indented (not (= (js-call source :charCodeAt source-index) 10))))
122
- (when indented
123
- (js-call text-parts :push " ")
124
- (setq output-length (+ output-length 2)))
125
- (while (and (< mark-index mark-count)
126
- (>= (get (js-nth mark-index source-marks) :offset)
127
- source-index)
128
- (< (get (js-nth mark-index source-marks) :offset)
129
- segment-end))
130
- (let ((mark (js-nth mark-index source-marks)))
131
- (js-call marks :push
132
- (js-object
133
- :offset (+ output-length
134
- (- (get mark :offset) source-index))
135
- :span (get mark :span))))
136
- (setq mark-index (1+ mark-index)))
137
- (js-call text-parts :push
138
- (js-call source :slice source-index segment-end))
139
- (setq output-length
140
- (+ output-length (- segment-end source-index)))
141
- (setq source-index segment-end)))
142
- (make-fragment (js-call text-parts :join "") marks)))
331
+ (let* ((input (as-fragment fragment)) (output (funcall native-indent input)))
332
+ (when function-source-profile
333
+ (put output :functions (indent-function-sources (get input :text) (get input :functions (js-array)))))
334
+ output))
143
335
 
144
336
  (defun reference-locate (node output)
145
337
  (let* ((fragment (as-fragment output))
@@ -155,7 +347,7 @@
155
347
  (js-array (js-object :offset 0 :span (ir-span node)))))
156
348
  (js-call marks :forEach
157
349
  (lambda (mark) (js-call located :push mark)))
158
- (make-fragment (get fragment :text) located)))))
350
+ (make-fragment (get fragment :text) located (get fragment :functions (js-array)))))))
159
351
 
160
352
  (defun locate (node output)
161
353
  (native-locate (ir-span node) output))
@@ -187,11 +379,19 @@
187
379
  ", got " count))))))
188
380
 
189
381
  (defun require-node-arity (node minimum maximum)
190
- (require-arity
191
- (or (ir-value node) (ir-kind node))
192
- (children node)
193
- minimum
194
- maximum))
382
+ (try
383
+ (require-arity
384
+ (or (ir-value node) (ir-kind node))
385
+ (children node)
386
+ minimum
387
+ maximum)
388
+ (catch error
389
+ ;; The node that owns this arity boundary is authoritative. Preserve
390
+ ;; the original exception even when it cannot accept extra metadata.
391
+ (try
392
+ (unless (Object/hasOwn error "eliscriptIRSpan")
393
+ (put error :eliscriptIRSpan (ir-span node)))
394
+ (finally (throw error))))))
195
395
 
196
396
  (defun emit-binding-name (node)
197
397
  (native-locate (ir-span node) (binding-name (ir-value node))))
@@ -336,33 +536,200 @@
336
536
  (js-call nodes :map (lambda (node) (emit-expression node)))
337
537
  ", "))
338
538
 
539
+ (defun qualified-apply-reference-p (node)
540
+ (and (node-p node) (= (ir-kind node) "reference")
541
+ (= (value-type (ir-value node)) "string")
542
+ (let ((name (js-call (ir-value node) :replaceAll "/" ".")))
543
+ (while (js-call name :endsWith ".") (setq name (js-call name :slice 0 -1)))
544
+ (js-call name :endsWith ".apply"))))
545
+
339
546
  (defun emit-callee (node)
340
547
  (let ((output (emit-expression node)))
341
- (if (= (ir-kind node) "reference")
342
- output
343
- (combine (js-array "(" output ")")))))
548
+ (cond
549
+ ((qualified-apply-reference-p node)
550
+ (combine (js-array "__eliscript_at("
551
+ (native-locate (ir-span node)
552
+ (text-fragment (js-call (get output :text) :slice 0 -6)))
553
+ ", \"apply\")")))
554
+ ((= (ir-kind node) "reference") output)
555
+ (t (combine (js-array "(" output ")"))))))
556
+
557
+ (defun boolean-reference-text-p (text)
558
+ (let ((length (js-length text)) (index 2) (valid t))
559
+ (when (or (< length 3) (/= (js-call text :charCodeAt 0) 33)
560
+ (/= (js-call text :charCodeAt 1) 33)) (setq valid false))
561
+ (while (and valid (< index length))
562
+ (let ((code (js-call text :charCodeAt index)))
563
+ (unless (or (and (>= code 65) (<= code 90))
564
+ (and (>= code 97) (<= code 122))
565
+ (= code 95) (= code 36)
566
+ (and (> index 2) (>= code 48) (<= code 57)))
567
+ (setq valid false)))
568
+ (setq index (1+ index))) valid))
569
+
570
+ (defun boolean-p (node)
571
+ (let ((kind (ir-kind node)) (value (ir-value node)))
572
+ (cond
573
+ ((= kind "literal") (or (eq value t) (eq value false)))
574
+ ((= kind "raw-javascript")
575
+ (let ((nodes (children node)))
576
+ (and (= (js-length nodes) 1) (= (ir-kind (js-nth 0 nodes)) "literal")
577
+ (= (value-type (ir-value (js-nth 0 nodes))) "string")
578
+ (boolean-reference-text-p (js-call (ir-value (js-nth 0 nodes)) :trim)))))
579
+ ((= kind "intrinsic")
580
+ (not (= (js-call
581
+ (js-array "not" "=" "/=" "not=" "<" "<=" ">" ">="
582
+ "eq" "equal" "nil?" "undefined?" "null" "nullish?")
583
+ :indexOf value) -1)))
584
+ ((= kind "short-circuit")
585
+ (let ((nodes (children node)))
586
+ (and (> (get nodes :length) 0)
587
+ (js-call nodes :every boolean-p))))
588
+ (t false))))
589
+
590
+ (defun emit-test (node)
591
+ (let ((output (emit-expression node)))
592
+ (if (boolean-p node) output
593
+ (combine (js-array "__eliscript_truthy(" output ")")))))
594
+
595
+ ;; Share a sequential scope only when no initializer can observe its TDZ.
596
+ ;; Other initializers retain their original individual binding scopes.
597
+ (defun context-sequential-safe-p (bindings)
598
+ (if (<= (js-length bindings) 1) false
599
+ (let ((names (new globalThis.Map)) (index 0) (safe t))
600
+ (while (and safe (< index (js-length bindings)))
601
+ (let ((name (binding-name (ir-value (js-nth index bindings)))))
602
+ (if (js-call names :has name) (setq safe false) (js-call names :set name index)))
603
+ (setq index (1+ index)))
604
+ (setq index 0)
605
+ (while (and safe (< index (js-length bindings)))
606
+ (let* ((initializer (binding-initializer (js-nth index bindings)))
607
+ (pending (if initializer (js-array initializer) (js-array))))
608
+ (while (and safe (> (js-length pending) 0))
609
+ (let* ((node (js-call pending :pop)) (kind (ir-kind node)))
610
+ (if (= kind "raw-javascript") (setq safe false)
611
+ (progn
612
+ ;; Include reads inside closures and assignment targets. A
613
+ ;; conservative extra scope is preferable to a new TDZ or
614
+ ;; capturing a binding declared later in the same block.
615
+ (when (or (= kind "reference") (= kind "assignment-pair"))
616
+ (let* ((name (reference-name (ir-value node)))
617
+ (root (js-nth 0 (js-call name :split "."))))
618
+ (when (and (js-call names :has root) (>= (js-call names :get root) index))
619
+ (setq safe false))))
620
+ (js-call (children node) :forEach (lambda (child) (js-call pending :push child))))))))
621
+ (setq index (1+ index))) safe)))
622
+
623
+ (defun emit-context-let (bindings body sequential returning)
624
+ (if (= (get bindings :length) 0)
625
+ (if returning (emit-returning-body body) (emit-statement-body body))
626
+ (if (and sequential (context-sequential-safe-p bindings))
627
+ (let ((declarations (js-array)))
628
+ (js-call bindings :forEach (lambda (binding)
629
+ (let ((initializer (binding-initializer binding)))
630
+ (js-call declarations :push (combine (js-array "let " (emit-binding-target binding) " = "
631
+ (if initializer (emit-expression initializer) "null") ";"))))))
632
+ (js-call declarations :push (if returning (emit-returning-body body) (emit-statement-body body)))
633
+ (combine (js-array "{\n" (indent (join-fragments declarations "\n")) "\n}")))
634
+ (let* ((group (if sequential (js-array (js-nth 0 bindings)) bindings))
635
+ (temporaries (temporary-names group))
636
+ (initializers (js-array))
637
+ (declarations (js-array))
638
+ (index 0))
639
+ (while (< index (get group :length))
640
+ (let* ((binding (js-nth index group))
641
+ (initializer (binding-initializer binding)))
642
+ (js-call initializers :push
643
+ (combine
644
+ (js-array "const " (js-nth index temporaries) " = "
645
+ (if initializer (emit-expression initializer) "null") ";"))))
646
+ (setq index (1+ index)))
647
+ (setq index 0)
648
+ (while (< index (get group :length))
649
+ (js-call declarations :push
650
+ (combine
651
+ (js-array "let " (emit-binding-target (js-nth index group)) " = "
652
+ (js-nth index temporaries) ";")))
653
+ (setq index (1+ index)))
654
+ (js-call declarations :push
655
+ (emit-context-let
656
+ (if sequential (js-call bindings :slice 1) (js-array))
657
+ body sequential returning))
658
+ (js-call initializers :push
659
+ (combine (js-array "{\n" (indent (join-fragments declarations "\n")) "\n}")))
660
+ (combine (js-array "{\n" (indent (join-fragments initializers "\n")) "\n}"))))))
661
+
662
+ (defun emit-context-cond (clauses returning)
663
+ (if (= (get clauses :length) 0)
664
+ (text-fragment (if returning "return null;" ""))
665
+ (let* ((clause (js-nth 0 clauses))
666
+ (nodes (children clause))
667
+ (test (js-nth 0 nodes))
668
+ (body (js-call nodes :slice 1)))
669
+ (native-locate (ir-span clause)
670
+ (if (and (= (ir-kind test) "literal") (= (ir-value test) t))
671
+ (if returning (emit-returning-body body) (emit-statement-body body))
672
+ (combine
673
+ (js-array "if (" (emit-test test) ") {\n"
674
+ (indent (if returning (emit-returning-body body) (emit-statement-body body)))
675
+ "\n} else {\n"
676
+ (indent (emit-context-cond (js-call clauses :slice 1) returning)) "\n}")))))))
677
+
678
+ (defun emit-context (node returning)
679
+ (let ((kind (ir-kind node)) (nodes (children node)))
680
+ (locate node
681
+ (cond
682
+ ;; Preserve async expression boundaries and their promise scheduling.
683
+ ((contains-await-p node)
684
+ (combine (js-array (if returning "return " "") (emit-expression node) ";")))
685
+ ((= kind "sequence")
686
+ (if returning (emit-returning-body nodes) (emit-statement-body nodes)))
687
+ ((= kind "conditional")
688
+ (combine
689
+ (js-array "if (" (emit-test (js-nth 0 nodes)) ") {\n"
690
+ (indent (emit-context (js-nth 1 nodes) returning)) "\n}"
691
+ (if (or returning (> (get nodes :length) 2))
692
+ (combine (js-array " else {\n"
693
+ (indent (if (> (get nodes :length) 2)
694
+ (emit-context (js-nth 2 nodes) returning)
695
+ (text-fragment "return null;"))) "\n}")) ""))))
696
+ ((= kind "conditional-sugar")
697
+ (combine
698
+ (js-array "if ("
699
+ (if (= (ir-value node) "when") (emit-test (js-nth 0 nodes))
700
+ (combine (js-array "(!" (emit-test (js-nth 0 nodes)) ")")))
701
+ ") {\n"
702
+ (indent (if returning (emit-returning-body (js-call nodes :slice 1))
703
+ (emit-statement-body (js-call nodes :slice 1)))) "\n}"
704
+ (if returning " else {\n return null;\n}" ""))))
705
+ ((= kind "conditional-chain") (emit-context-cond nodes returning))
706
+ ((= kind "loop")
707
+ (combine
708
+ (js-array "while (" (emit-test (js-nth 0 nodes)) ") {\n"
709
+ (indent (emit-statement-body (js-call nodes :slice 1))) "\n}"
710
+ (if returning "\nreturn null;" ""))))
711
+ ((and (= kind "lexical-bindings")
712
+ (js-call (js-call nodes :slice 0 (ir-property node "bindingCount"))
713
+ :every (lambda (binding) (not (ir-property binding "pattern")))))
714
+ (let ((parts (split-counted-children node "bindingCount")))
715
+ (emit-context-let (js-nth 0 parts) (js-nth 1 parts)
716
+ (ir-property node "sequential") returning)))
717
+ (t
718
+ (combine (js-array (if returning "return " "") (emit-expression node) ";")))))))
344
719
 
345
720
  (defun emit-returning-body (nodes)
346
721
  (if (= (get nodes :length) 0)
347
722
  (text-fragment "return null;")
348
- (let ((parts (js-array))
349
- (index 0)
350
- (last-index (1- (get nodes :length))))
723
+ (let ((parts (js-array)) (index 0) (last-index (1- (get nodes :length))))
351
724
  (while (< index last-index)
352
- (js-call parts :push (emit-expression (js-nth index nodes)))
353
- (js-call parts :push ";\n")
725
+ (js-call parts :push (emit-context (js-nth index nodes) false))
354
726
  (setq index (1+ index)))
355
- (js-call parts :push "return ")
356
- (js-call parts :push (emit-expression (js-nth last-index nodes)))
357
- (js-call parts :push ";")
358
- (combine parts))))
727
+ (js-call parts :push (emit-context (js-nth last-index nodes) t))
728
+ (join-fragments parts "\n"))))
359
729
 
360
730
  (defun emit-statement-body (nodes)
361
- (let ((parts (js-array)))
362
- (js-call nodes :forEach
363
- (lambda (node)
364
- (js-call parts :push (combine (js-array (emit-expression node) ";")))))
365
- (join-fragments parts "\n")))
731
+ (join-fragments
732
+ (js-call nodes :map (lambda (node) (emit-context node false))) "\n"))
366
733
 
367
734
  (defun contains-await-p (value)
368
735
  (let ((pending (js-array value))
@@ -384,7 +751,7 @@
384
751
  (while (>= index 0)
385
752
  (js-call pending :push (js-nth index nodes))
386
753
  (setq index (1- index))))))
387
- ((js* "Array.isArray(current)")
754
+ ((Array/isArray current)
388
755
  (setq index (1- (get current :length)))
389
756
  (while (>= index 0)
390
757
  (js-call pending :push (js-nth index current))
@@ -417,15 +784,16 @@
417
784
  (let* ((parts (split-counted-children node "parameterCount"))
418
785
  (parameters (js-nth 0 parts))
419
786
  (body (js-nth 1 parts)))
420
- (combine
421
- (js-array (if (ir-property node "async") "async " "") "("
787
+ (let ((fragment (combine
788
+ (js-array "(" (if (ir-property node "async") "async " "") "("
422
789
  (join-fragments
423
790
  (js-call parameters :map
424
791
  (lambda (parameter) (emit-parameter parameter)))
425
792
  ", ")
426
793
  ") => {\n"
427
794
  (indent (emit-function-body parameters body))
428
- "\n}"))))
795
+ "\n})"))))
796
+ (function-source-fragment node fragment 1 (1- (js-length (get fragment :text)))))))
429
797
 
430
798
  (defun contains-recur-target-p (value target-kind)
431
799
  (let ((pending (js-array value))
@@ -449,7 +817,7 @@
449
817
  (while (>= index 0)
450
818
  (js-call pending :push (js-nth index nodes))
451
819
  (setq index (1- index))))))
452
- ((js* "Array.isArray(current)")
820
+ ((Array/isArray current)
453
821
  (setq index (1- (get current :length)))
454
822
  (while (>= index 0)
455
823
  (js-call pending :push (js-nth index current))
@@ -626,7 +994,71 @@
626
994
  (emit-tail-short-circuit nodes (ir-value node) target))
627
995
  (t (combine (js-array "return " (emit-expression node) ";"))))))
628
996
 
997
+ (defconst closed-scalar-operators
998
+ (js-array "+" "-" "*" "/" "%" "mod" "1+" "1-" "=" "eq" "<" "<=" ">" ">=" "/=" "not=" "not"))
999
+
1000
+ (defun closed-scalar-node-p (node scope state)
1001
+ (if (nullish? node) t
1002
+ (let ((kind (ir-kind node)) (nodes (children node)))
1003
+ (cond
1004
+ ((= kind "literal")
1005
+ (or (= (ir-value node) nil) (= (value-type (aref node "value")) "number")
1006
+ (= (value-type (aref node "value")) "boolean") (= (value-type (aref node "value")) "undefined")))
1007
+ ((= kind "reference") (>= (js-call scope :indexOf (ir-value node)) 0))
1008
+ ((= kind "lexical-bindings")
1009
+ (let* ((parts (split-counted-children node "bindingCount"))
1010
+ (bindings (js-nth 0 parts)) (inner (js-call scope :slice)) (valid t))
1011
+ (js-call bindings :forEach
1012
+ (lambda (binding)
1013
+ (when (or (not (= (ir-kind binding) "lexical-binding"))
1014
+ (ir-property binding "pattern")
1015
+ (not (closed-scalar-node-p (binding-initializer binding)
1016
+ (if (ir-property node "sequential") inner scope) state)))
1017
+ (setq valid false))
1018
+ (js-call inner :push (ir-value binding))))
1019
+ (and valid (js-call (js-nth 1 parts) :every
1020
+ (lambda (child) (closed-scalar-node-p child inner state))))))
1021
+ ((= kind "assignment")
1022
+ (js-call nodes :every
1023
+ (lambda (pair)
1024
+ (and (= (ir-kind pair) "assignment-pair")
1025
+ (>= (js-call scope :indexOf (ir-value pair)) 0)
1026
+ (closed-scalar-node-p (js-nth 0 (children pair)) scope state)))))
1027
+ ((or (and (= kind "intrinsic") (>= (js-call closed-scalar-operators :indexOf (ir-value node)) 0))
1028
+ (= kind "conditional") (= kind "conditional-sugar") (= kind "short-circuit")
1029
+ (= kind "sequence") (= kind "loop"))
1030
+ (when (= kind "loop") (put state :loop t))
1031
+ (and (or (not (= kind "intrinsic")) (not (relational-operator-p (ir-value node)))
1032
+ (= (js-length nodes) 2))
1033
+ (js-call nodes :every (lambda (child) (closed-scalar-node-p child scope state)))))
1034
+ (t false)))))
1035
+
1036
+ (defun closed-scalar-loop-p (parameters body)
1037
+ (let ((state (js-object :loop false))
1038
+ (scope (js-call parameters :map (lambda (parameter) (ir-value parameter)))))
1039
+ (and (> (js-length parameters) 0)
1040
+ (js-call parameters :every
1041
+ (lambda (parameter)
1042
+ (and (= (ir-property parameter "parameterKind") "required")
1043
+ (not (ir-property parameter "pattern")))))
1044
+ (js-call body :every (lambda (node) (closed-scalar-node-p node scope state)))
1045
+ (get state :loop))))
1046
+
629
1047
  (defun emit-function-body (parameters body)
1048
+ (if (and (not numeric-emission-profile) (closed-scalar-loop-p parameters body))
1049
+ (let ((fast nil) (slow nil) (previous numeric-emission-profile))
1050
+ (setq numeric-emission-profile t)
1051
+ (try (setq fast (emit-function-body-unversioned parameters body))
1052
+ (finally (setq numeric-emission-profile previous)))
1053
+ (setq slow (emit-function-body-unversioned parameters body))
1054
+ (combine
1055
+ (js-array "if ("
1056
+ (js-call (js-call parameters :map
1057
+ (lambda (parameter) (str "typeof " (binding-name (ir-value parameter)) " === \"number\""))) :join " && ")
1058
+ ") {\n" (indent fast) "\n} else {\n" (indent slow) "\n}")))
1059
+ (emit-function-body-unversioned parameters body)))
1060
+
1061
+ (defun emit-function-body-unversioned (parameters body)
630
1062
  (let ((setups (emit-binding-setups parameters)))
631
1063
  (if (not (contains-recur-target-p body "function"))
632
1064
  (if (= (get (get setups :text) :length) 0)
@@ -647,9 +1079,9 @@
647
1079
 
648
1080
  (defun emit-if (nodes)
649
1081
  (combine
650
- (js-array "(__eliscript_truthy("
651
- (emit-expression (js-nth 0 nodes))
652
- ") ? "
1082
+ (js-array "("
1083
+ (emit-test (js-nth 0 nodes))
1084
+ " ? "
653
1085
  (emit-expression (js-nth 1 nodes))
654
1086
  " : "
655
1087
  (if (> (get nodes :length) 2)
@@ -659,13 +1091,12 @@
659
1091
 
660
1092
  (defun emit-conditional-sugar (node)
661
1093
  (let* ((nodes (children node))
662
- (test (emit-expression (js-nth 0 nodes)))
1094
+ (test (emit-test (js-nth 0 nodes)))
663
1095
  (body (emit-do (js-call nodes :slice 1))))
664
- (if (= (ir-value node) "when")
665
- (combine (js-array "(__eliscript_truthy(" test ") ? " body " : null)"))
666
- (combine
667
- (js-array "(__eliscript_truthy((!__eliscript_truthy("
668
- test "))) ? " body " : null)")))))
1096
+ (combine
1097
+ (js-array "("
1098
+ (if (= (ir-value node) "when") test
1099
+ (combine (js-array "(!" test ")"))) " ? " body " : null)"))))
669
1100
 
670
1101
  (defun emit-cond (clauses)
671
1102
  (if (= (get clauses :length) 0)
@@ -679,9 +1110,9 @@
679
1110
  (if (and (= (ir-kind test) "literal") (= (ir-value test) t))
680
1111
  (emit-do body)
681
1112
  (combine
682
- (js-array "(__eliscript_truthy("
683
- (emit-expression test)
684
- ") ? "
1113
+ (js-array "("
1114
+ (emit-test test)
1115
+ " ? "
685
1116
  (emit-do body)
686
1117
  " : "
687
1118
  (emit-cond (js-call clauses :slice 1))
@@ -796,40 +1227,20 @@
796
1227
  (defun emit-assignment (node)
797
1228
  (let ((assignments (js-array)))
798
1229
  (js-call (children node) :forEach
799
- (lambda (pair)
800
- (js-call assignments :push
801
- (native-locate
802
- (ir-span pair)
803
- (combine
804
- (js-array "("
805
- (emit-binding-name pair)
806
- " = "
807
- (emit-expression (js-nth 0 (children pair)))
808
- ")"))))))
809
- (if (= (ir-value node) "set!")
810
- (js-nth 0 assignments)
1230
+ (lambda (pair) (js-call assignments :push (native-locate (ir-span pair) (tdz-assignment pair)))))
1231
+ (if (= (ir-value node) "set!") (js-nth 0 assignments)
811
1232
  (combine (js-array "(" (join-fragments assignments ", ") ")")))))
812
1233
 
813
1234
  (defun emit-loop (node)
814
1235
  (let* ((nodes (children node))
815
- (test (js-nth 0 nodes))
816
- (body (js-call nodes :slice 1))
817
- (test-output (emit-expression test))
818
- (statements (js-array)))
819
- (js-call body :forEach
820
- (lambda (child)
821
- (js-call statements :push (combine (js-array (emit-expression child) ";")))))
822
- (emit-iife
823
- ""
1236
+ (test-output (emit-test (js-nth 0 nodes)))
1237
+ (body (js-call nodes :slice 1)))
1238
+ (emit-iife ""
824
1239
  (indent
825
1240
  (combine
826
- (js-array "while (__eliscript_truthy("
827
- test-output
828
- ")) {\n"
829
- (indent (join-fragments statements "\n"))
830
- "\n}\nreturn null;")))
831
- ""
832
- (contains-await-p nodes))))
1241
+ (js-array "while (" test-output ") {\n"
1242
+ (indent (emit-statement-body body)) "\n}\nreturn null;")))
1243
+ "" (contains-await-p nodes))))
833
1244
 
834
1245
  (defun emit-binding-loop (node)
835
1246
  (let* ((parts (split-counted-children node "bindingCount"))
@@ -869,6 +1280,12 @@
869
1280
  ((= (get nodes :length) 0)
870
1281
  (text-fragment (if (= kind "and") "true" "null")))
871
1282
  ((= (get nodes :length) 1) (emit-expression (js-nth 0 nodes)))
1283
+ ((and (js-call nodes :every boolean-p) (not (contains-await-p nodes)))
1284
+ (combine
1285
+ (js-array "("
1286
+ (join-fragments
1287
+ (js-call nodes :map (lambda (node) (emit-expression node)))
1288
+ (if (= kind "and") " && " " || ")) ")")))
872
1289
  (t
873
1290
  (let ((temporary (fresh-name))
874
1291
  (first (emit-expression (js-nth 0 nodes)))
@@ -885,20 +1302,50 @@
885
1302
  (js-array "(await (async (" temporary ") => " body ")(" first "))"))
886
1303
  (combine (js-array "((" temporary ") => " body ")(" first ")"))))))))
887
1304
 
1305
+ (defun numeric-operator-tag (operator)
1306
+ (cond ((= operator "-") "sub") ((= operator "*") "mul") ((= operator "/") "div")
1307
+ ((= operator "%") "rem") ((= operator "&") "and") ((= operator "|") "or")
1308
+ ((= operator "^") "xor") ((= operator "<<") "shl") ((= operator ">>") "shr")
1309
+ ((= operator ">>>") "ushr") ((= operator "imul") "imul") (t nil)))
1310
+ (defun relational-operator-p (operator)
1311
+ (or (= operator "<") (= operator "<=") (= operator ">") (= operator ">=")))
1312
+ (defun emit-numeric-binary (operator left right)
1313
+ (if numeric-emission-profile
1314
+ (combine (js-array "(" left " " operator " " right ")"))
1315
+ ;; A Number literal one has no evaluation effects and subtraction cannot
1316
+ ;; return BigInt. A unary helper saves recursive-call argument registers.
1317
+ (if (and (= operator "-") (= (get (as-fragment right) :text) "1"))
1318
+ (combine (js-array "__eliscript_numeric_decrement(" left ")"))
1319
+ (if (relational-operator-p operator)
1320
+ (combine (js-array "__eliscript_numeric_relation(" (json-string operator) ", " left ", " right ")"))
1321
+ (let ((tag (numeric-operator-tag operator)))
1322
+ (if tag (combine (js-array "__eliscript_numeric_" tag "(" left ", " right ")"))
1323
+ (combine (js-array "(" left " " operator " " right ")"))))))))
1324
+ (defun emit-numeric-unary (tag operand)
1325
+ (combine (js-array "__eliscript_numeric_" tag "(" (emit-expression operand) ")")))
1326
+ (defun numeric-coercion-intrinsic-p (name)
1327
+ (or (numeric-operator-tag name) (relational-operator-p name)
1328
+ (= name "mod") (= name "1-") (= name "int32") (= name "uint32") (= name "imul32")
1329
+ (= name "bit-not") (= name "bit-and") (= name "bit-or") (= name "bit-xor")
1330
+ (= name "bit-shift-left") (= name "bit-shift-right") (= name "unsigned-bit-shift-right")))
888
1331
  (defun emit-infix (name nodes operator minimum)
889
1332
  (require-arity name nodes minimum nil)
890
- (combine
891
- (js-array "("
892
- (join-fragments
893
- (js-call nodes :map (lambda (node) (emit-expression node)))
894
- (str " " operator " "))
895
- ")")))
1333
+ (if (and (not numeric-emission-profile) (numeric-operator-tag operator) (> (js-length nodes) 1))
1334
+ (let ((result (emit-expression (js-nth 0 nodes))) (index 1))
1335
+ (while (< index (js-length nodes))
1336
+ (setq result (emit-numeric-binary operator result (emit-expression (js-nth index nodes))))
1337
+ (setq index (1+ index))) result)
1338
+ (combine
1339
+ (js-array "("
1340
+ (join-fragments
1341
+ (js-call nodes :map (lambda (node) (emit-expression node)))
1342
+ (str " " operator " "))
1343
+ ")"))))
896
1344
 
897
- (defun emit-binary-infix (name nodes operator)
898
- (require-arity name nodes 2 2)
899
- (combine
900
- (js-array "(" (emit-expression (js-nth 0 nodes)) " " operator " "
901
- (emit-expression (js-nth 1 nodes)) ")")))
1345
+ (defun emit-binary-infix (node operator)
1346
+ (require-node-arity node 2 2)
1347
+ (let ((nodes (children node)))
1348
+ (emit-numeric-binary operator (emit-expression (js-nth 0 nodes)) (emit-expression (js-nth 1 nodes)))))
902
1349
 
903
1350
  (defun temporary-names (nodes)
904
1351
  (let ((result (js-array)))
@@ -909,16 +1356,15 @@
909
1356
  (defun emit-comparison (name nodes operator)
910
1357
  (require-arity name nodes 2 nil)
911
1358
  (if (= (get nodes :length) 2)
912
- (combine
913
- (js-array "(" (emit-expression (js-nth 0 nodes)) " " operator " "
914
- (emit-expression (js-nth 1 nodes)) ")"))
1359
+ (emit-numeric-binary operator (emit-expression (js-nth 0 nodes)) (emit-expression (js-nth 1 nodes)))
915
1360
  (let ((parameters (temporary-names nodes))
916
1361
  (comparisons (js-array))
917
1362
  (index 0))
918
1363
  (while (< index (1- (get parameters :length)))
919
1364
  (js-call comparisons :push
920
- (str (js-nth index parameters) " " operator " "
921
- (js-nth (1+ index) parameters)))
1365
+ (if (and (not numeric-emission-profile) (relational-operator-p operator))
1366
+ (str "__eliscript_numeric_relation(" (json-string operator) ", " (js-nth index parameters) ", " (js-nth (1+ index) parameters) ")")
1367
+ (str (js-nth index parameters) " " operator " " (js-nth (1+ index) parameters))))
922
1368
  (setq index (1+ index)))
923
1369
  (combine
924
1370
  (js-array "(("
@@ -956,7 +1402,7 @@
956
1402
  (cond
957
1403
  ((= name "not")
958
1404
  (require-node-arity node 1 1)
959
- (combine (js-array "(!__eliscript_truthy(" (emit-expression (js-nth 0 nodes)) "))")))
1405
+ (combine (js-array "(!" (emit-test (js-nth 0 nodes)) ")")))
960
1406
  ((= name "+")
961
1407
  (if (= (get nodes :length) 0)
962
1408
  (text-fragment "0")
@@ -969,44 +1415,44 @@
969
1415
  ((= name "*")
970
1416
  (if (= (get nodes :length) 0)
971
1417
  (text-fragment "1")
972
- (combine
973
- (js-array "("
974
- (join-fragments
975
- (js-call nodes :map (lambda (node) (emit-expression node)))
976
- " * ")
977
- ")"))))
1418
+ (emit-infix name nodes "*" 1)))
978
1419
  ((= name "-")
979
1420
  (require-node-arity node 1 nil)
980
1421
  (if (= (get nodes :length) 1)
981
- (combine (js-array "(-" (emit-expression (js-nth 0 nodes)) ")"))
1422
+ (if numeric-emission-profile
1423
+ (combine (js-array "(-" (emit-expression (js-nth 0 nodes)) ")"))
1424
+ (emit-numeric-unary "neg" (js-nth 0 nodes)))
982
1425
  (emit-infix "-" nodes "-" 2)))
983
1426
  ((= name "/")
984
1427
  (require-node-arity node 1 nil)
985
1428
  (if (= (get nodes :length) 1)
986
- (combine (js-array "(1 / " (emit-expression (js-nth 0 nodes)) ")"))
1429
+ (emit-numeric-binary "/" (text-fragment "1") (emit-expression (js-nth 0 nodes)))
987
1430
  (emit-infix "/" nodes "/" 2)))
988
1431
  ((or (= name "%") (= name "mod")) (emit-infix name nodes "%" 2))
989
1432
  ((= name "int32")
990
1433
  (require-node-arity node 1 1)
991
- (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " | 0)")))
1434
+ (if numeric-emission-profile (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " | 0)"))
1435
+ (emit-numeric-unary "int32" (js-nth 0 nodes))))
992
1436
  ((= name "uint32")
993
1437
  (require-node-arity node 1 1)
994
- (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " >>> 0)")))
1438
+ (if numeric-emission-profile (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " >>> 0)"))
1439
+ (emit-numeric-unary "uint32" (js-nth 0 nodes))))
995
1440
  ((= name "imul32")
996
1441
  (require-node-arity node 2 2)
997
- (combine
998
- (js-array "Math.imul(" (emit-expression (js-nth 0 nodes)) ", "
999
- (emit-expression (js-nth 1 nodes)) ")")))
1000
- ((= name "bit-and") (emit-binary-infix name nodes "&"))
1001
- ((= name "bit-or") (emit-binary-infix name nodes "|"))
1002
- ((= name "bit-xor") (emit-binary-infix name nodes "^"))
1442
+ (if numeric-emission-profile
1443
+ (combine (js-array "Math.imul(" (emit-expression (js-nth 0 nodes)) ", " (emit-expression (js-nth 1 nodes)) ")"))
1444
+ (emit-numeric-binary "imul" (emit-expression (js-nth 0 nodes)) (emit-expression (js-nth 1 nodes)))))
1445
+ ((= name "bit-and") (emit-binary-infix node "&"))
1446
+ ((= name "bit-or") (emit-binary-infix node "|"))
1447
+ ((= name "bit-xor") (emit-binary-infix node "^"))
1003
1448
  ((= name "bit-not")
1004
1449
  (require-node-arity node 1 1)
1005
- (combine (js-array "(~" (emit-expression (js-nth 0 nodes)) ")")))
1006
- ((= name "bit-shift-left") (emit-binary-infix name nodes "<<"))
1007
- ((= name "bit-shift-right") (emit-binary-infix name nodes ">>"))
1450
+ (if numeric-emission-profile (combine (js-array "(~" (emit-expression (js-nth 0 nodes)) ")"))
1451
+ (emit-numeric-unary "not" (js-nth 0 nodes))))
1452
+ ((= name "bit-shift-left") (emit-binary-infix node "<<"))
1453
+ ((= name "bit-shift-right") (emit-binary-infix node ">>"))
1008
1454
  ((= name "unsigned-bit-shift-right")
1009
- (emit-binary-infix name nodes ">>>"))
1455
+ (emit-binary-infix node ">>>"))
1010
1456
  ((= name "value-type")
1011
1457
  (require-node-arity node 1 1)
1012
1458
  (combine
@@ -1028,7 +1474,7 @@
1028
1474
  (js-array "String.fromCharCode(" (emit-expression (js-nth 0 nodes)) ")")))
1029
1475
  ((= name "string-to-number")
1030
1476
  (require-node-arity node 1 1)
1031
- (combine (js-array "Number(" (emit-expression (js-nth 0 nodes)) ")")))
1477
+ (combine (js-array "__eliscript_to_number(" (emit-expression (js-nth 0 nodes)) ")")))
1032
1478
  ((= name "string-to-bigint")
1033
1479
  (require-node-arity node 1 1)
1034
1480
  (combine
@@ -1050,7 +1496,7 @@
1050
1496
  (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " + 1)")))
1051
1497
  ((= name "1-")
1052
1498
  (require-node-arity node 1 1)
1053
- (combine (js-array "(" (emit-expression (js-nth 0 nodes)) " - 1)")))
1499
+ (emit-numeric-binary "-" (emit-expression (js-nth 0 nodes)) (text-fragment "1")))
1054
1500
  ((= name "eq")
1055
1501
  (require-node-arity node 2 2)
1056
1502
  (combine
@@ -1167,17 +1613,17 @@
1167
1613
  ((null value) (text-fragment "null"))
1168
1614
  ((eq value t) (text-fragment "true"))
1169
1615
  ((eq value false) (text-fragment "false"))
1170
- ((= (js* "typeof value") "number")
1616
+ ((= (value-type value) "number")
1171
1617
  (unless (Number/isFinite value)
1172
1618
  (fail (str "non-finite numbers are not supported: " value)))
1173
- (text-fragment (str value)))
1174
- ((= (js* "typeof value") "string")
1619
+ (text-fragment (if (and (= value 0) (< (/ 1 value) 0)) "-0.0" (str value))))
1620
+ ((= (value-type value) "string")
1175
1621
  (text-fragment (json-string value)))
1176
1622
  (t (fail (str "unsupported IR literal: " value))))))
1177
1623
 
1178
1624
  (defun datum-kind (value)
1179
1625
  (and value
1180
- (= (js* "typeof value") "object")
1626
+ (= (value-type value) "object")
1181
1627
  (get value :kind)))
1182
1628
 
1183
1629
  (defun emit-quoted (value)
@@ -1187,8 +1633,9 @@
1187
1633
  ((eq value t) (text-fragment "true"))
1188
1634
  ((eq value false)
1189
1635
  (text-fragment "__eliscript_symbol(\"false\")"))
1190
- ((= (js* "typeof value") "number") (text-fragment (str value)))
1191
- ((= (js* "typeof value") "string")
1636
+ ((= (value-type value) "number")
1637
+ (text-fragment (if (and (= value 0) (< (/ 1 value) 0)) "-0.0" (str value))))
1638
+ ((= (value-type value) "string")
1192
1639
  (text-fragment (json-string value)))
1193
1640
  ((= kind "undefined") (text-fragment "undefined"))
1194
1641
  ((= kind "symbol")
@@ -1231,10 +1678,21 @@
1231
1678
  (ir-span node)
1232
1679
  (if (and (= (ir-kind node) "literal")
1233
1680
  (or (= (literal-kind node) "keyword")
1234
- (= (js* "typeof node.value") "string")))
1681
+ (= (value-type (aref node "value")) "string")))
1235
1682
  (text-fragment (json-string (ir-value node)))
1236
1683
  (emit-expression node))))
1237
1684
 
1685
+ (defun array-text-key-p (node)
1686
+ (or (not (= (ir-kind node) "literal"))
1687
+ (= (ir-value node) "join") (= (ir-value node) "toString")
1688
+ (= (ir-value node) "call") (= (ir-value node) "apply") (= (ir-value node) "bind")))
1689
+
1690
+ (defun primitive-literal-arguments-p (nodes)
1691
+ (js-call nodes :every
1692
+ (lambda (node)
1693
+ (and (= (ir-kind node) "literal")
1694
+ (not (= (literal-kind node) "keyword"))))))
1695
+
1238
1696
  (defun emit-expression (node)
1239
1697
  (unless (node-p node)
1240
1698
  (fail "expected an IR expression node"))
@@ -1244,7 +1702,7 @@
1244
1702
  (ir-span node)
1245
1703
  (cond
1246
1704
  ((= kind "literal") (emit-literal node))
1247
- ((= kind "reference") (text-fragment (reference-name (ir-value node))))
1705
+ ((= kind "reference") (tdz-read node))
1248
1706
  ((= kind "array-literal") (combine (js-array "[" (emit-arguments nodes) "]")))
1249
1707
  ((= kind "persistent-list-literal")
1250
1708
  (combine (js-array "__eliscript_list(" (emit-arguments nodes) ")")))
@@ -1289,14 +1747,19 @@
1289
1747
  (emit-property-key (js-nth 1 nodes)) "] = "
1290
1748
  (emit-expression (js-nth 2 nodes)) ")")))
1291
1749
  ((= kind "method-call")
1292
- (combine
1293
- (js-array "(" (emit-expression (js-nth 0 nodes)) ")["
1294
- (emit-property-key (js-nth 1 nodes)) "]("
1295
- (emit-arguments (js-call nodes :slice 2)) ")")))
1750
+ (let* ((arguments (js-call nodes :slice 2))
1751
+ (array-text (array-text-key-p (js-nth 1 nodes)))
1752
+ (direct (and array-text (primitive-literal-arguments-p arguments))))
1753
+ (combine
1754
+ (js-array (if direct "__eliscript_at_direct(" (if array-text "__eliscript_at(" "("))
1755
+ (emit-expression (js-nth 0 nodes)) (if array-text ", " ")[")
1756
+ (emit-property-key (js-nth 1 nodes))
1757
+ (if direct (if (> (get arguments :length) 0) ", " "") (if array-text ")(" "]("))
1758
+ (emit-arguments arguments) ")"))))
1296
1759
  ((= kind "constructor-call")
1297
1760
  (combine
1298
- (js-array "new " (emit-expression (js-nth 0 nodes)) "("
1299
- (emit-arguments (js-call nodes :slice 1)) ")")))
1761
+ (js-array "new (" (emit-expression (js-nth 0 nodes))
1762
+ ")(" (emit-arguments (js-call nodes :slice 1)) ")")))
1300
1763
  ((= kind "raw-javascript")
1301
1764
  ;; The raw form is exactly one string. Accepting several arguments
1302
1765
  ;; emitted only the first, so the module's braces did not close and
@@ -1379,12 +1842,14 @@
1379
1842
  (if (> (get nodes :length) 0)
1380
1843
  (emit-expression (js-nth 0 nodes))
1381
1844
  "null")
1382
- ";")))
1845
+ ";"
1846
+ (let ((record (and tdz-state (js-call (get tdz-state :declarations) :get node))))
1847
+ (if record (str "\n" (tdz-ready-name record) " = true;") "")))))
1383
1848
  ((= kind "function-declaration")
1384
1849
  (let* ((parts (split-counted-children node "parameterCount"))
1385
1850
  (parameters (js-nth 0 parts))
1386
1851
  (body (js-nth 1 parts)))
1387
- (combine
1852
+ (let ((fragment (combine
1388
1853
  (js-array (if (ir-property node "async") "async " "")
1389
1854
  "function " (binding-name (ir-value node)) "("
1390
1855
  (join-fragments
@@ -1394,6 +1859,7 @@
1394
1859
  ") {\n"
1395
1860
  (indent (emit-function-body parameters body))
1396
1861
  "\n}"))))
1862
+ (function-source-fragment node fragment 0 (js-length (get fragment :text))))))
1397
1863
  ((= kind "export-declaration")
1398
1864
  (combine
1399
1865
  (js-array "export {"
@@ -1408,6 +1874,17 @@
1408
1874
  (combine (js-array (emit-expression (js-nth 0 nodes)) ";")))
1409
1875
  (t (fail (str "IR node is not valid at module top level: " kind)))))))
1410
1876
 
1877
+ (defun node-uses-number-conversion-p (node)
1878
+ (if (and (= (ir-kind node) "intrinsic")
1879
+ (= (ir-value node) "string-to-number"))
1880
+ t
1881
+ (let ((found false))
1882
+ (js-call (children node) :forEach
1883
+ (lambda (child)
1884
+ (when (node-uses-number-conversion-p child)
1885
+ (setq found t))))
1886
+ found)))
1887
+
1411
1888
  (defun node-uses-host-identity-token-p (node)
1412
1889
  (if (and (= (ir-kind node) "intrinsic")
1413
1890
  (= (ir-value node) "host-identity-token"))
@@ -1510,6 +1987,14 @@
1510
1987
  (setq found t))))
1511
1988
  found)))
1512
1989
 
1990
+ (defun program-uses-number-conversion-p (program)
1991
+ (let ((found false))
1992
+ (js-call (program-body program) :forEach
1993
+ (lambda (node)
1994
+ (when (node-uses-number-conversion-p node)
1995
+ (setq found t))))
1996
+ found))
1997
+
1513
1998
  (defun program-uses-host-identity-token-p (program)
1514
1999
  (let ((found false))
1515
2000
  (js-call (program-body program) :forEach
@@ -1558,8 +2043,21 @@
1558
2043
  (setq found t))))
1559
2044
  found))
1560
2045
 
2046
+ (defun node-uses-array-text-p (node)
2047
+ (let ((found (or (and (= (ir-kind node) "method-call")
2048
+ (array-text-key-p (js-nth 1 (children node))))
2049
+ (and (js-call (js-array "call" "invoke" "apply-call") :includes (ir-kind node))
2050
+ (qualified-apply-reference-p (js-nth 0 (children node)))))))
2051
+ (js-call (children node) :forEach
2052
+ (lambda (child) (when (node-uses-array-text-p child) (setq found t)))) found))
2053
+
1561
2054
  (defun reference-runtime-requirements (program)
1562
2055
  (js-object
2056
+ :math (js-call (program-body program) :some node-uses-math-runtime-p)
2057
+ :numericCoercion (program-uses-numeric-coercion-p program)
2058
+ :imul (js-call (program-body program) :some node-uses-imul-p)
2059
+ :arrayText (js-call (program-body program) :some node-uses-array-text-p)
2060
+ :numberConversion (program-uses-number-conversion-p program)
1563
2061
  :hostIdentityToken (program-uses-host-identity-token-p program)
1564
2062
  :literal (program-uses-literal-runtime-p program)
1565
2063
  :collection (program-uses-collection-runtime-p program)
@@ -1567,6 +2065,30 @@
1567
2065
  :list (program-uses-list-runtime-p program)
1568
2066
  :value (program-uses-value-runtime-p program)))
1569
2067
 
2068
+ (defun math-runtime-reference-p (value)
2069
+ (and (= (value-type value) "string")
2070
+ (or (js-call (js-array "Math" "globalThis/Math" "globalThis.Math") :includes value)
2071
+ (js-call (js-array "Math/" "Math." "globalThis/Math/" "globalThis/Math."
2072
+ "globalThis.Math/" "globalThis.Math.") :some
2073
+ (lambda (prefix) (js-call value :startsWith prefix))))))
2074
+ (defun node-uses-math-runtime-p (node)
2075
+ (or (and (js-call (js-array "reference" "raw-javascript") :includes (ir-kind node))
2076
+ (math-runtime-reference-p (ir-value node)))
2077
+ (js-call (children node) :some node-uses-math-runtime-p)))
2078
+
2079
+ (defun node-uses-imul-p (node)
2080
+ (or (and (= (ir-kind node) "intrinsic") (= (ir-value node) "imul32"))
2081
+ (js-call (children node) :some node-uses-imul-p)))
2082
+
2083
+ (defun node-uses-numeric-coercion-p (node)
2084
+ (or (and (= (ir-kind node) "intrinsic") (numeric-coercion-intrinsic-p (ir-value node))
2085
+ (or (/= (ir-value node) "*") (> (js-length (children node)) 1)))
2086
+ (let ((found false))
2087
+ (js-call (children node) :forEach (lambda (child) (when (node-uses-numeric-coercion-p child) (setq found t)))) found)))
2088
+ (defun program-uses-numeric-coercion-p (program)
2089
+ (let ((found false))
2090
+ (js-call (program-body program) :forEach (lambda (node) (when (node-uses-numeric-coercion-p node) (setq found t)))) found))
2091
+
1570
2092
  (defun static-literal-key-p (node index static-key-index)
1571
2093
  (and static-key-index
1572
2094
  (= index static-key-index)
@@ -1581,6 +2103,13 @@
1581
2103
  (static-key-index (literal-runtime-static-key-index node))
1582
2104
  (index 0)
1583
2105
  (child nil))
2106
+ (when (or (and (= kind "method-call") (array-text-key-p (js-nth 1 nodes)))
2107
+ (and (js-call (js-array "call" "invoke" "apply-call") :includes kind)
2108
+ (qualified-apply-reference-p (js-nth 0 nodes))))
2109
+ (put requirements :arrayText t))
2110
+ (when (and (js-call (js-array "reference" "raw-javascript") :includes kind)
2111
+ (math-runtime-reference-p value))
2112
+ (put requirements :math t))
1584
2113
  (cond
1585
2114
  ((= kind "map-binding-pattern")
1586
2115
  (put requirements :binding t))
@@ -1600,7 +2129,12 @@
1600
2129
  (= (literal-kind node) "keyword"))
1601
2130
  (put requirements :literal t))
1602
2131
  ((= kind "intrinsic")
2132
+ (when (= value "imul32") (put requirements :imul t))
2133
+ (when (and (numeric-coercion-intrinsic-p value)
2134
+ (or (/= value "*") (> (js-length nodes) 1)))
2135
+ (put requirements :numericCoercion t))
1603
2136
  (cond
2137
+ ((= value "string-to-number") (put requirements :numberConversion t))
1604
2138
  ((= value "host-identity-token")
1605
2139
  (put requirements :hostIdentityToken t))
1606
2140
  ((or (= value "nth") (= value "length"))
@@ -1622,6 +2156,11 @@
1622
2156
  (defun runtime-requirements (program)
1623
2157
  (let ((requirements
1624
2158
  (js-object
2159
+ :math false
2160
+ :numericCoercion false
2161
+ :imul false
2162
+ :arrayText false
2163
+ :numberConversion false
1625
2164
  :hostIdentityToken false
1626
2165
  :literal false
1627
2166
  :collection false
@@ -1677,7 +2216,44 @@
1677
2216
  ", ")
1678
2217
  "]));"))))))
1679
2218
 
1680
- (defun emit-module-artifact (program)
2219
+ ;; An embedded module gives captured intrinsics their own lexical scope when
2220
+ ;; user source binds globalThis. It requires no host package or dynamic eval.
2221
+ (defun node-names-global-root-p (node)
2222
+ (or (and (= (ir-value node) "globalThis")
2223
+ (not (= (ir-kind node) "literal"))
2224
+ (not (= (ir-kind node) "raw-javascript")))
2225
+ (js-call (children node) :some node-names-global-root-p)))
2226
+
2227
+ (defun number-conversion-prologue (program)
2228
+ (if (js-call (program-body program) :some node-names-global-root-p)
2229
+ (str number-conversion-capsule number-conversion-body)
2230
+ number-conversion-helper))
2231
+
2232
+ (defun imul-prologue (program)
2233
+ (str (if (js-call (program-body program) :some node-names-global-root-p)
2234
+ imul-capsule imul-intrinsic) imul-operator-helper))
2235
+
2236
+ (defun emit-module-artifact-in-profile (program)
2237
+ (let ((previous tdz-state))
2238
+ (setq tdz-state (tdz-prepare program))
2239
+ (try (emit-module-artifact-in-profile-ready program)
2240
+ (finally (setq tdz-state previous)))))
2241
+
2242
+ (defun unicode-bootstrap-capsule-p (program)
2243
+ (let* ((filename (program-filename program)) (body (program-body program))
2244
+ (module (if (= (js-length body) 1) (aref body 0) nil)))
2245
+ (and module (= (ir-kind module) "module-declaration")
2246
+ (= (value-type filename) "string")
2247
+ (js-call (js-array
2248
+ (js-array "runtime/unicode-case.eli" "eliscript.runtime.unicode-case")
2249
+ (js-array "runtime/unicode-case-data.eli" "eliscript.runtime.unicode-case-data"))
2250
+ :some (lambda (entry)
2251
+ (and (= (ir-value module) (aref entry 1))
2252
+ (or (= filename (aref entry 0))
2253
+ (and (js-call filename :startsWith "/")
2254
+ (js-call filename :endsWith (+ "/" (aref entry 0)))))))))))
2255
+
2256
+ (defun emit-module-artifact-in-profile-ready (program)
1681
2257
  (setq temporary-counter 0)
1682
2258
  (setq binding-temporaries (new globalThis.Map))
1683
2259
  (let ((requirements (runtime-requirements program))
@@ -1688,6 +2264,10 @@
1688
2264
  "\n\n")))
1689
2265
  (combine
1690
2266
  (js-array "// Generated by Eliscript. Do not edit.\n"
2267
+ (tdz-prologue)
2268
+ (if (unicode-bootstrap-capsule-p program) "" "import \"eliscript/runtime/unicode-case.mjs\";\n")
2269
+ (if (get requirements :math) "import \"eliscript/runtime/math.mjs\";\n" "")
2270
+ (if (get requirements :arrayText) array-text-import "")
1691
2271
  (if (get requirements :literal)
1692
2272
  literal-runtime-import
1693
2273
  "")
@@ -1704,6 +2284,10 @@
1704
2284
  value-runtime-import
1705
2285
  "")
1706
2286
  "const __eliscript_truthy = (value) => value !== false && value != null;\n"
2287
+ (if (or (get requirements :numberConversion)
2288
+ (and (not numeric-emission-profile) (get requirements :numericCoercion))) (number-conversion-prologue program) "")
2289
+ (if (and (not numeric-emission-profile) (get requirements :numericCoercion)) numeric-operator-helper "")
2290
+ (if (and (not numeric-emission-profile) (get requirements :imul)) (imul-prologue program) "")
1707
2291
  (if (get requirements :hostIdentityToken)
1708
2292
  host-identity-token-helper
1709
2293
  "")
@@ -1712,6 +2296,30 @@
1712
2296
  (emit-portable-manifest program)
1713
2297
  "\n"))))
1714
2298
 
2299
+ (defun emit-module-artifact (program)
2300
+ (let ((previous numeric-emission-profile))
2301
+ (setq numeric-emission-profile false)
2302
+ (try (emit-module-artifact-in-profile program)
2303
+ (finally (setq numeric-emission-profile previous)))))
2304
+ (defun emit-module-function-sources (program)
2305
+ (let ((previous function-source-profile))
2306
+ (setq function-source-profile t)
2307
+ (try
2308
+ (let* ((artifact (emit-module-artifact program)) (javascript (get artifact :text)))
2309
+ (js-object :javascript javascript
2310
+ :functions (js-call (get artifact :functions (js-array)) :map
2311
+ (lambda (record)
2312
+ (js-object :node (get record :node) :start (get record :start) :end (get record :end)
2313
+ :sourceText (js-call javascript :slice (get record :start) (get record :end)))))))
2314
+ (finally (setq function-source-profile previous)))))
2315
+ ;; Internal code generation entry. The numeric backend must validate the
2316
+ ;; complete original program before optimization and before calling this.
2317
+ (defun emit-module-numeric (program)
2318
+ (let ((previous numeric-emission-profile))
2319
+ (setq numeric-emission-profile t)
2320
+ (try (get (emit-module-artifact-in-profile program) :text)
2321
+ (finally (setq numeric-emission-profile previous)))))
2322
+
1715
2323
  (defun emit-module (program)
1716
2324
  (get (emit-module-artifact program) :text))
1717
2325
 
@@ -1739,7 +2347,9 @@
1739
2347
  emit-expression
1740
2348
  emit-top-level
1741
2349
  emit-module-artifact
2350
+ emit-module-function-sources
1742
2351
  emit-module
2352
+ emit-module-numeric
1743
2353
  emit-module-with-source-map
1744
2354
  runtime-requirements
1745
2355
  reference-runtime-requirements))