ffi: define-by-default #jni_class + #foreign modifier + #jni_main token

Flip the surface semantics for type-introducer directives: bare
`Foo :: #jni_class("path") { ... }` now means "DEFINE a new Java class
at that path" (sx-side provides the implementations). The `#foreign`
prefix modifier flips it back to "REFERENCE an existing class on the
foreign runtime." Matches how `#foreign` already reads in sx for C
function declarations (`printf :: ... #foreign;`).

    Foo :: #foreign  #jni_class("path/to/Foo") { ... }  // reference
    Foo ::           #jni_class("path/to/Foo") { ... }  // define
    Foo :: #jni_main #jni_class("path/to/Foo") { ... }  // define + main Activity

Compiler-side changes:
- New `hash_jni_main` lexer token (the launchable-Activity marker).
  Existing `hash_foreign` is reused; no new modifier token there.
- `ForeignClassDecl` gains `is_foreign: bool` + `is_main: bool`.
  `ForeignMethodDecl` gains `body: ?*Node` so defined-class methods
  can carry sx-side implementations (foreign-class methods stay
  `;`-terminated).
- Parser learns `tryParseForeignClassPrefix` — peek-and-consume the
  modifier tokens, then dispatch to the unchanged
  `parseForeignClassDecl` with the flags threaded through.
- Sema rejects two illegal combinations: `#foreign + #jni_main`
  (can't be both an external reference and the app's main entry),
  and bodied methods on `#foreign` decls (foreign methods are
  runtime-provided).
- Lower's foreign-class dispatch errors on non-foreign decls with
  a pointer to the runtime-synthesis follow-up; defined-class
  codegen (Java class emission, RegisterNatives wiring, manifest
  entry generation) lands in a separate session.

Migration:
- `library/modules/platform/android_jni.sx`: all four foreign class
  decls (`Activity`, `Window`, `View`, `WindowInsets`) gain `#foreign`.
- `examples/ffi-jni-class-{01..08}*.sx`: every test's `#jni_class` /
  `#jni_interface` / `#objc_class` / `#objc_protocol` / `#swift_class`
  / `#swift_struct` / `#swift_protocol` usage gains `#foreign`. All
  9 files mechanical perl rename; snapshots unchanged.

Verified locally:
- `zig build test` clean.
- `bash tests/run_examples.sh` 129/129.
- `bash tests/cross_compile.sh` 3/3.
- Chess APK rebuilds, reinstalls, launches on Pixel; safe-area
  clearance preserved.
This commit is contained in:
agra
2026-05-20 12:46:40 +03:00
parent 60f3ffed46
commit 8d1816018a
16 changed files with 136 additions and 25 deletions

View File

@@ -1,5 +1,5 @@
// Phase 2 step 2.0 (PLAN-FFI.md): xfail parser test for the
// type-introducer `Foo :: #jni_class("java/path/Foo") { }` directive.
// type-introducer `Foo :: #foreign #jni_class("java/path/Foo") { }` directive.
//
// Today's parser doesn't recognize `#jni_class` as a known hash
// directive after `::`, so it falls through to expression parsing
@@ -10,7 +10,7 @@
#import "modules/std.sx";
Foo :: #jni_class("java/path/Foo") { }
Foo :: #foreign #jni_class("java/path/Foo") { }
main :: () -> s32 {
print("parse-only ok\n");

View File

@@ -10,7 +10,7 @@
// `Self` here refers to the enclosing `View` type — resolved at
// 2.x sema, not at parse time.
View :: #jni_class("android/view/View") {
View :: #foreign #jni_class("android/view/View") {
getId :: (self: *Self) -> s32;
}

View File

@@ -9,7 +9,7 @@
#import "modules/std.sx";
Math :: #jni_class("java/lang/Math") {
Math :: #foreign #jni_class("java/lang/Math") {
static abs :: (n: s32) -> s32;
}

View File

@@ -12,9 +12,9 @@
#import "modules/std.sx";
View :: #jni_class("android/view/View") { }
View :: #foreign #jni_class("android/view/View") { }
Window :: #jni_class("android/view/Window") {
Window :: #foreign #jni_class("android/view/Window") {
#extends View;
getDecorView :: (self: *Self) -> *View;
}

View File

@@ -8,7 +8,7 @@
#import "modules/std.sx";
Point :: #jni_class("android/graphics/Point") {
Point :: #foreign #jni_class("android/graphics/Point") {
x: s32;
y: s32;
}

View File

@@ -10,7 +10,7 @@
#import "modules/std.sx";
Foo :: #jni_class("com/example/Foo") {
Foo :: #foreign #jni_class("com/example/Foo") {
weirdMethod :: (self: *Self) -> s32 #jni_method_descriptor("()I");
}

View File

@@ -17,27 +17,27 @@
#import "modules/std.sx";
IFoo :: #jni_interface("com/example/IFoo") {
IFoo :: #foreign #jni_interface("com/example/IFoo") {
bar :: (self: *Self) -> s32;
}
NSString :: #objc_class("NSString") {
NSString :: #foreign #objc_class("NSString") {
length :: (self: *Self) -> s32;
}
NSCopying :: #objc_protocol("NSCopying") {
NSCopying :: #foreign #objc_protocol("NSCopying") {
copy :: (self: *Self) -> *Self;
}
URL :: #swift_class("Foundation.URL") {
URL :: #foreign #swift_class("Foundation.URL") {
absoluteString :: (self: *Self) -> *void;
}
Date :: #swift_struct("Foundation.Date") {
Date :: #foreign #swift_struct("Foundation.Date") {
timeIntervalSince1970 :: (self: *Self) -> f64;
}
Hashable :: #swift_protocol("Swift.Hashable") {
Hashable :: #foreign #swift_protocol("Swift.Hashable") {
hash :: (self: *Self) -> s32;
}

View File

@@ -8,7 +8,7 @@
#import "modules/std.sx";
Activity :: #jni_class("android/app/Activity") {
Activity :: #foreign #jni_class("android/app/Activity") {
getWindow :: (self: *Self) -> *void;
}