diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 380159ee..dabbe4fc 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -256,7 +256,7 @@ let common_prelude = {| const Some = (value) => ({ tag: "Some", value }); const None = { tag: "None" }; const Ok = (value) => ({ tag: "Ok", value }); -const Err = (error) => ({ tag: "Err", error }); +const Err = (value) => ({ tag: "Err", value }); const Unit = null; const __as_regexMatch = (s, pat) => new RegExp(pat).test(String(s)); const __as_wasmInstance = (bytes) => diff --git a/lib/js_codegen.ml b/lib/js_codegen.ml index e80c0bf2..c98aa0a4 100644 --- a/lib/js_codegen.ml +++ b/lib/js_codegen.ml @@ -60,7 +60,7 @@ let prelude = {|// ---- AffineScript JS runtime (MVP) ---- const Some = (value) => ({ tag: "Some", value }); const None = { tag: "None" }; const Ok = (value) => ({ tag: "Ok", value }); -const Err = (error) => ({ tag: "Err", error }); +const Err = (value) => ({ tag: "Err", value }); const Unit = null; const print = (s) => { (typeof Deno !== "undefined" ? Deno.stdout.writeSync(new TextEncoder().encode(String(s))) : process.stdout.write(String(s))); }; const println = (s) => { console.log(String(s)); }; diff --git a/lib/lua_codegen.ml b/lib/lua_codegen.ml index 8e7e165d..74ca5264 100644 --- a/lib/lua_codegen.ml +++ b/lib/lua_codegen.ml @@ -22,7 +22,7 @@ local function as_print(s) io.write(tostring(s)) end local Some = function(v) return { tag = "Some", value = v } end local None = { tag = "None" } local Ok = function(v) return { tag = "Ok", value = v } end -local Err = function(e) return { tag = "Err", error = e } end +local Err = function(v) return { tag = "Err", value = v } end local function read_line() local l = io.read("l"); return l or "" end |} diff --git a/tests/codegen-bun/result_payload.affine b/tests/codegen-bun/result_payload.affine new file mode 100644 index 00000000..20f0e24f --- /dev/null +++ b/tests/codegen-bun/result_payload.affine @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MPL-2.0 +// Regression: an `Err(e)` arm must bind the payload. The runtime prelude +// built Err as { tag, error } while every match lowering reads the +// single-payload convention `.value`, so `e` was `undefined` (found while +// writing kyX, metadatastician/kyx). + +use prelude::{ Result, Ok, Err, Option, Some, None }; + +fn check(s: String) -> Result { + if s == "ok" { Ok(1) } else { Err("bad: " ++ s) } +} + +pub fn err_message(s: String) -> String { + match check(s) { Ok(n) => "ok", Err(m) => m } +} + +pub fn ok_value(s: String) -> Int { + match check(s) { Ok(n) => n, Err(m) => 0 } +} + +pub fn nested_err() -> String { + let r: Option> = Some(Err("inner")); + match r { Some(Err(m)) => m, Some(Ok(n)) => "ok", None => "none" } +} diff --git a/tests/codegen-bun/result_payload.harness.mjs b/tests/codegen-bun/result_payload.harness.mjs new file mode 100644 index 00000000..73a260b9 --- /dev/null +++ b/tests/codegen-bun/result_payload.harness.mjs @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: MPL-2.0 +import * as subject from "./result_payload.bun.js"; + +const expect = (label, got, want) => { + if (got !== want) throw new Error(`${label}: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`); +}; +expect("Err payload binds", subject.err_message("x"), "bad: x"); +expect("Ok arm still binds", subject.ok_value("ok"), 1); +expect("Err payload in a nested pattern", subject.nested_err(), "inner"); +console.log("result_payload: ok"); diff --git a/tools/run_codegen_bun_tests.sh b/tools/run_codegen_bun_tests.sh index df95adac..9ab77a13 100755 --- a/tools/run_codegen_bun_tests.sh +++ b/tools/run_codegen_bun_tests.sh @@ -85,6 +85,15 @@ harness_total=0 for js in "$TEST_DIR"/*.harness.mjs; do name="$(basename "$js")" harness_total=$((harness_total + 1)) + # Each harness imports .bun.js; compile its subject first (step 1 + # already compiled host_profile). + subject_src="$TEST_DIR/${name%.harness.mjs}.affine" + if [ "$subject_src" != "$src" ]; then + if ! "${COMPILE_CMD[@]}" "$subject_src" -o "${subject_src%.affine}.bun.js" --bun-esm; then + fail "compile failed for $(basename "$subject_src")" + continue + fi + fi if ! (cd "$TEST_DIR" && AFFINESCRIPT_BUN_PROBE=estate bun "$name" alpha beta); then fail "bun harness failed: $name" fi