From e46338540427296492f312bab5efb3f4e890ed1f Mon Sep 17 00:00:00 2001 From: agra Date: Wed, 20 May 2026 10:47:07 +0300 Subject: [PATCH] ffi 2.16b xfail: omitted env in `#jni_call` inside `#jni_env` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `#jni_call(void)(target, "name", "sig")` (3 args before the first string literal) should work inside an enclosing `#jni_env(env) { ... }` scope, picking up the env from the block's value directly. Today's lowering expects 4+ args and errors with "#jni_call requires env, target, method name, and signature". The make-green follow-up adds a lowering-side env stack maintained across the `#jni_env` body walk, and a disambiguation in `lowerJniCall` that detects "env omitted" via the position of the first string-literal arg (method name at index 1 → omitted; at index 2 → explicit env). --- examples/ffi-jni-env-02-lexical-direct.sx | 30 +++++++++++++++++++ .../ffi-jni-env-02-lexical-direct.exit | 1 + .../ffi-jni-env-02-lexical-direct.txt | 1 + 3 files changed, 32 insertions(+) create mode 100644 examples/ffi-jni-env-02-lexical-direct.sx create mode 100644 tests/expected/ffi-jni-env-02-lexical-direct.exit create mode 100644 tests/expected/ffi-jni-env-02-lexical-direct.txt diff --git a/examples/ffi-jni-env-02-lexical-direct.sx b/examples/ffi-jni-env-02-lexical-direct.sx new file mode 100644 index 0000000..60ed654 --- /dev/null +++ b/examples/ffi-jni-env-02-lexical-direct.sx @@ -0,0 +1,30 @@ +// Phase 2 step 2.16b (PLAN-FFI.md): xfail then green for the +// lexical-direct env resolution inside a `#jni_env` scope. +// +// `#jni_call(T)(target, "name", "sig", args...)` — three args before +// the first string literal — has the env omitted. The lowering walks +// the in-progress AST visit stack to find the enclosing `#jni_env(env)` +// block and uses that env value directly as the IR-level env arg. +// No thread-local read, no per-call lookup; env stays register- +// resident across loop bodies (the hot-path optimisation). +// +// Today's lower expects 4+ args and errors when called with 3. + +#import "modules/std.sx"; + +g_should_call : bool = false; + +unused_jni :: (env: *void, target: *void) { + #jni_env(env) { + // Omitted env — env comes from the enclosing #jni_env scope. + #jni_call(void)(target, "noop", "()V"); + } +} + +main :: () -> s32 { + if g_should_call { + unused_jni(null, null); + } + print("ok\n"); + 0; +} diff --git a/tests/expected/ffi-jni-env-02-lexical-direct.exit b/tests/expected/ffi-jni-env-02-lexical-direct.exit new file mode 100644 index 0000000..d00491f --- /dev/null +++ b/tests/expected/ffi-jni-env-02-lexical-direct.exit @@ -0,0 +1 @@ +1 diff --git a/tests/expected/ffi-jni-env-02-lexical-direct.txt b/tests/expected/ffi-jni-env-02-lexical-direct.txt new file mode 100644 index 0000000..ff683ca --- /dev/null +++ b/tests/expected/ffi-jni-env-02-lexical-direct.txt @@ -0,0 +1 @@ +/Users/agra/projects/sx/examples/ffi-jni-env-02-lexical-direct.sx: error: #jni_call requires env, target, method name, and signature