#!/usr/bin/env python3 """Whole-value ownership through source, canonical AIR, and CLI entry points.""" import json from pathlib import Path import subprocess import tempfile REPO = Path(__file__).resolve().parents[2] AJI = REPO / "zig-out/bin/aji" checks = 0 def cli(*args, code=0): global checks run = subprocess.run([str(AJI), *map(str, args)], capture_output=True, text=True) assert run.returncode == code, (args, run.returncode, run.stdout, run.stderr) checks += 1 return run.stdout def returned(path, *args): record = json.loads(cli("run", "--fuel", "1000000", *args, path)) assert record["status"] == "returned" and record["value"] == 42, record features = json.loads(cli("features")) assert "linear_aggregates" in features["features"] source = REPO / "examples/std/owning-option.aji" returned(source, "--grant", "heap=mem.alloc:64") cli("check", source) canonical = cli("lower", source) assert cli("lower", source) == canonical assert "enum.take" in canonical and "value.swap" in canonical with tempfile.TemporaryDirectory(prefix="aji-linear-") as directory: root = Path(directory) air = root / "result.air" air.write_text(canonical) cli("check", air) returned(air, "--grant", "heap=mem.alloc:64") for fixture, grant in (("pack", 64), ("pack_failure", 8), ("swap_loop", 8), ("heap_array_borrow", 64)): path = REPO / f"tests/conformance/source/linear/{fixture}.aji" lowered = cli("lower", path) assert cli("lower", path) == lowered air.write_text(lowered) cli("check", air) returned(air, "--grant", f"heap=mem.alloc:{grant}") # Resource-bearing nominal types imported across a package boundary. (root / "lib.aji").write_text("module app.lib\nstruct Box { value: T }\nfn extract(box: Box) -> T effects [] { let (value) = struct.unpack(move box); return move value; }\n") (root / "main.aji").write_text("module app.main\nimport app.lib;\nfn main(heap: cap) -> u64 effects [mem.alloc] { let owner = own.new(42u64, heap) or trap; let box = app.lib.Box::> { value: move owner }; let value = app.lib.extract::>(move box); return own.take(move value); }\n") manifest = root / "aji.toml" manifest.write_text('[package]\nname = "fixture"\nversion = "1"\nentry = "main.aji"\n[modules]\napp.lib = "lib.aji"\n') assert json.loads(cli("run", "--fuel", "1000000", "--manifest", manifest, "--grant", "heap=mem.alloc:64"))["value"] == 42 cli("check", "--manifest", manifest) cli("report", "--manifest", manifest) cli("lower", "--manifest", manifest) cli("explore", "--max-runs", "8", "--manifest", manifest, "--grant", "heap=mem.alloc:64") for fixture, expected in (("missing_move", "E7003"), ("ignored_field", "E3002"), ("borrow_conflict", "E2101"), ("recursive_ownership", "E1003")): path = REPO / f"tests/conformance/source/linear/{fixture}.aji" commands = ("check", "lower", "report", "run", "explore") if fixture in ("missing_move", "recursive_ownership") else ("check", "report", "run", "explore") for command in commands: diagnostics = [json.loads(line) for line in cli(command, path, code=1).splitlines()] assert diagnostics[0]["code"] == expected, diagnostics pack = REPO / "tests/conformance/source/linear/pack.aji" report = json.loads(cli("report", pack)) assert sum(function["allocations"] for function in report["functions"]) == 2, report for fixture in ("task", "channel"): path = REPO / f"tests/conformance/source/linear/{fixture}.aji" cli("explore", "--max-runs", "8", "--grant", "heap=mem.alloc:64", path) print(f"source linear CLI integration passed ({checks} checks)")