Commit caf11fe
feat(codegen): Deno-ESM target — direct AST→ES module backend (Refs #122)
Adds `lib/codegen_deno.ml`, a direct AffineScript-AST → ES-module
transpiler, plus a `--deno-esm` flag (and `.deno.js` extension) wired
into both compile dispatch paths in `bin/main.ml`.
Why a direct transpiler, not a wasm-wrapping ESM shim (the handoff's
original Phase 1): the motivating consumer (ubicity storage.ts /
wasm-bridge.ts) is pure JS-value orchestration — it JSON-stringifies
opaque objects, JSON-parses file contents back into JS objects, and
returns those to JS callers. AffineScript's wasm ABI is i32-only
(Codegen: params/ret all I32); arbitrary JS objects / JSON.stringify
cannot cross it. There is essentially no computation that belongs in
wasm, so source-to-source emission is the correct tool.
Backend behaviour:
- `pub fn`/`pub const`/`pub enum` -> `export` (ESM, no require).
- `extern fn` -> direct host calls via a lowering table
(Deno.*Sync / JSON / WebAssembly / path helpers), inlined so output
is genuinely drop-in (no extra package to resolve).
- struct + receiver-first free functions -> `export class`:
constructor synthesised from the struct-returning fn (record literal
-> `this.f = e`), methods from fns whose first param is the struct
(receiver -> `this`), cross-method calls rewritten to `this.m(...)`.
This is necessary because the current grammar accepts neither
inherent `impl Type {}` nor a `self` expression (SELF_KW has no
expression production — even stdlib/traits.affine fails to parse), so
an "instance method" must be a free function taking the struct first.
- Methods emitted `async`: the consumer surface is entirely async and
callers `await`; `await` on a synchronously-returned value is valid
JS, so the exact API is preserved with no async-extern ABI (#103 not
required for this consumer; remains documented future work).
- js_reserved trimmed to real ECMAScript reserved words (drops the
Java-ism primitives Js_codegen carries: double/int/boolean/... are
valid JS identifiers; mangling them would corrupt a consumer's
required ESM export surface).
Regression: tests/codegen-deno/class_basic.{affine,harness.mjs} +
tools/run_codegen_deno_tests.sh + a CI step. The harness runs under
Node 20 (CI has no Deno; Phase 1 fixtures are pure logic and the
generated module only touches the Deno global lazily in unused
helpers). `dune build` clean; `dune runtest` unchanged (the 2
pre-existing E2E Node-CJS `--vscode-extension` failures, #116/#117
territory, exist identically on pristine HEAD and are out of #122
scope).
Refs #122, #35, #103.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent d2875a5 commit caf11fe
8 files changed
Lines changed: 931 additions & 6 deletions
File tree
- .github/workflows
- bin
- lib
- tests/codegen-deno
- tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
40 | 40 | | |
41 | 41 | | |
42 | 42 | | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
43 | 49 | | |
44 | 50 | | |
45 | 51 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
478 | 478 | | |
479 | 479 | | |
480 | 480 | | |
481 | | - | |
| 481 | + | |
482 | 482 | | |
483 | 483 | | |
484 | 484 | | |
| |||
512 | 512 | | |
513 | 513 | | |
514 | 514 | | |
| 515 | + | |
515 | 516 | | |
516 | | - | |
| 517 | + | |
517 | 518 | | |
518 | 519 | | |
519 | 520 | | |
| |||
534 | 535 | | |
535 | 536 | | |
536 | 537 | | |
537 | | - | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
538 | 549 | | |
539 | 550 | | |
540 | 551 | | |
| |||
725 | 736 | | |
726 | 737 | | |
727 | 738 | | |
| 739 | + | |
728 | 740 | | |
729 | | - | |
| 741 | + | |
730 | 742 | | |
731 | 743 | | |
732 | 744 | | |
| |||
747 | 759 | | |
748 | 760 | | |
749 | 761 | | |
750 | | - | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
| 767 | + | |
| 768 | + | |
| 769 | + | |
| 770 | + | |
| 771 | + | |
| 772 | + | |
| 773 | + | |
751 | 774 | | |
752 | 775 | | |
753 | 776 | | |
| |||
1178 | 1201 | | |
1179 | 1202 | | |
1180 | 1203 | | |
| 1204 | + | |
| 1205 | + | |
| 1206 | + | |
| 1207 | + | |
| 1208 | + | |
| 1209 | + | |
| 1210 | + | |
| 1211 | + | |
| 1212 | + | |
| 1213 | + | |
| 1214 | + | |
| 1215 | + | |
| 1216 | + | |
| 1217 | + | |
1181 | 1218 | | |
1182 | 1219 | | |
1183 | 1220 | | |
| |||
1524 | 1561 | | |
1525 | 1562 | | |
1526 | 1563 | | |
1527 | | - | |
| 1564 | + | |
1528 | 1565 | | |
1529 | 1566 | | |
1530 | 1567 | | |
| |||
0 commit comments