Files
sx/tests/cross_compile.sh
agra c02b6b3b1b ffi #jni_main: Alias.new(args) constructor dispatch via JNI NewObject
Adds the constructor-invocation arm of the foreign-class DSL:
`SurfaceView.new(ctx)` (where `SurfaceView` is a `#foreign #jni_class`
with `static new :: (ctx: *Context) -> *Self;`) lowers to
`FindClass(env, "android/view/SurfaceView") + GetMethodID(env, cls,
"<init>", "(args)V") + NewObject(env, cls, mid, args...)`. Returns
the fresh jobject.

  - inst.zig: `JniMsgSend.is_constructor` flag + `parent_class_path`
    re-purposed to carry the class being constructed (alongside its
    existing nonvirtual-super-class use). Mutually exclusive with
    `is_static` / `is_nonvirtual`.
  - lower.zig: `lowerCall.field_access` arm now recognises
    `Alias.method(args)` where `Alias` resolves in `foreign_class_map`
    and the matching member is `static`. `new` routes to a new
    `lowerForeignStaticCall` that derives a `(args)V` JNI descriptor
    and emits a `JniMsgSend` with `is_constructor=true`. Non-`new`
    static calls report a clear "use #jni_static_call" diagnostic
    until that sugar lands.
  - emit_llvm.zig: new `NewObject` vtable slot (28) + `emitJniConstructor`
    helper expanding the FindClass+GetMethodID+NewObject chain. The
    jni_msg_send arm short-circuits to it when `is_constructor` is set.

Smoke `ffi-jni-main-03-ctor.sx` exercises both this slice and the
previous super-dispatch slice in a single `onCreate` body: calls
`super.onCreate(b)` then constructs a `SurfaceView` with the Activity
as Context. IR shows the expected six-stage chain (FindClass+GetMethodID+
CallNonvirtual + FindClass+GetMethodID+NewObject); APK builds clean.

Naming caveat: the Java type `android.content.Context` clashes with
sx stdlib's `Context :: struct {...}` (heap-context). The smoke aliases
it `JContext` — future work could add a path-prefix or `as` rename
form on `#jni_class` to avoid the manual rename.

133 host / 6 cross / zig build test all green.
2026-05-20 17:14:51 +03:00

97 lines
3.4 KiB
Bash
Executable File

#!/bin/bash
# Cross-compile regression runner.
#
# For each (target, example) tuple, runs `./sx build --target <t> <example>`
# and asserts (a) exit 0 and (b) the expected output file was produced.
# Compile correctness only — these examples can't be executed on the host
# (iOS Obj-C runtime / Android NDK).
#
# Tuple list starts empty and grows as Phase 0 / 1 / 2 / 3 of the FFI plan
# add cross-only examples. Skips with a warning (still exits 0) when the
# required toolchain isn't installed, so contributors without the iOS SDK
# or Android NDK aren't blocked.
#
# Usage: ./tests/cross_compile.sh
set -uo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
SX="$ROOT_DIR/zig-out/bin/sx"
TMP_DIR="${TMPDIR:-/tmp}/sx-cross-compile"
mkdir -p "$TMP_DIR"
# Tuple format: "<target>|<example_path>"
# Add entries as cross-only examples land. Verifies the example
# compiles cleanly for the target's NDK / SDK without needing the
# host to actually run it.
TUPLES=(
"android|examples/ffi-objc-call-10-os-gate.sx"
"android|examples/ffi-jni-call-02-void.sx"
# Step 1.24: verify the inverse OS gate — `inline if OS == .android
# { #jni_call(...) }` must strip its body before lowering on iOS so
# emit_llvm doesn't try to use libjvm symbols the iOS SDK lacks.
"ios-sim|examples/ffi-jni-call-02-void.sx"
# #jni_main pipeline slice 2: an example carrying a `#jni_main
# #jni_class(...)` decl must continue to lower + link cleanly for
# android even without an APK build (compile-only check).
"android|examples/ffi-jni-main-01-emit.sx"
# `super.method(args)` dispatch: lowers to JNI CallNonvirtualVoidMethod
# against the parent class (Activity by default). Compile-only check
# — runtime correctness is verified by on-device chess deploy.
"android|examples/ffi-jni-main-02-super.sx"
# `Alias.new(args)` constructor dispatch: lowers to FindClass +
# GetMethodID("<init>") + NewObject. Compile-only — runtime via chess.
"android|examples/ffi-jni-main-03-ctor.sx"
)
PASS=0
FAIL=0
SKIP=0
toolchain_available() {
local target="$1"
case "$target" in
ios|ios-sim)
xcrun --sdk iphonesimulator --show-sdk-path >/dev/null 2>&1
;;
android|android-arm64)
# discoverAndroidNdk in target.zig accepts $ANDROID_NDK_HOME,
# $ANDROID_NDK_ROOT, or a scan of $HOME/Library/Android/sdk/ndk.
[[ -n "${ANDROID_NDK_HOME:-}" || -n "${ANDROID_NDK_ROOT:-}" ]] \
|| [[ -d "$HOME/Library/Android/sdk/ndk" ]]
;;
*)
return 1
;;
esac
}
for tuple in "${TUPLES[@]:-}"; do
[[ -z "$tuple" ]] && continue
target="${tuple%%|*}"
example="${tuple#*|}"
label="$target / $(basename "$example" .sx)"
if ! toolchain_available "$target"; then
SKIP=$((SKIP + 1))
printf " %-50s SKIP (no toolchain)\n" "$label"
continue
fi
out_obj="$TMP_DIR/$(basename "$example" .sx).$target.o"
printf " %-50s" "$label"
"$SX" build --target "$target" -o "$out_obj" "$ROOT_DIR/$example" >/dev/null 2>&1
rc=$?
if [[ $rc -eq 0 && -s "$out_obj" ]]; then
PASS=$((PASS + 1))
echo "ok"
else
FAIL=$((FAIL + 1))
echo "FAIL (exit=$rc, output=$out_obj)"
fi
done
echo "$PASS passed, $FAIL failed, $SKIP skipped"
[[ $FAIL -eq 0 ]]