Treat an Android activity's missing optional method as absent

On a Java-free NativeActivity, Kryon's optional callbacks
(onNativeFirstFrame, setTextInputState, setSoftKeyboardVisible) do not
exist. AndroidJniMethod returned GetMethodID's null with the
NoSuchMethodError still pending, and the host thread now keeps its JNIEnv
across frames, so later JNI calls in the session ran with that exception.
The lookup now clears the exception it caused and releases the class
reference it took; AndroidJniCallVoid releases its string argument.
Exceptions that Java methods throw still make the call report failure and
are still described and cleared at detach.

tests/android_jni_threads_test.zi's fake JNIEnv now tracks pending
exceptions and local references, and checks all three absent methods, a
present call and a throwing one.
This commit is contained in:
waozi committed 2026-11-16 07:17:03 -03:00
1 parent 7cae309044
commit e9017b29d0
4 files changed
+84 -7

No files matched your search

+6
View File
@@ -2,6 +2,12 @@
## Unreleased
- On Android, an activity without Kryon's optional Java callbacks, such as a
Java-free NativeActivity without `onNativeFirstFrame`, `setTextInputState`
or `setSoftKeyboardVisible`, no longer leaves a pending
`NoSuchMethodError` that broke the JNI calls after it: a method the
activity lacks counts as absent, and its lookup releases its class
reference.
- A hosted widget's slice parameter (TabBar, SegmentedControl, Dropdown,
TitleBar, Modal actions, ContextMenu) is accepted whether the bundle names
its element type through the package (`[]TabBar.Tab`) or bare (`[]Tab`),
+21 -5
View File
@@ -389,21 +389,36 @@ AndroidJniDetachCurrent :: (session: *AndroidJniSession) {
session.attached = 0
}
// Clears the exception a failed lookup left pending. An absent optional
// method is expected, so it is not described; AndroidJniDetachCurrent still
// describes and clears exceptions that Java methods throw.
AndroidJniClearLookup :: (env: *JniEnv) {
if env.functions.ExceptionCheck(env) != 0 { env.functions.ExceptionClear(env) }
}
// The activity's method `name` with `signature`, or null when the activity
// has none: a Java-free NativeActivity lacks the optional callbacks a Kryon
// activity defines. The lookup's NoSuchMethodError is cleared and its class
// reference released, so an absent method never poisons later calls.
AndroidJniMethod :: (session: *AndroidJniSession, name: string,
signature: string) -> *void {
env := session.env
klass := env.functions.GetObjectClass(env, session.activity)
if klass == null {
return null
}
method_name: [96]u8
method_signature: [192]u8
if !CopyCString(name, method_name[:]) ||
!CopyCString(signature, method_signature[:]) {
return null
}
return env.functions.GetMethodID(env, klass, *method_name[0],
klass := env.functions.GetObjectClass(env, session.activity)
if klass == null {
AndroidJniClearLookup(env)
return null
}
method := env.functions.GetMethodID(env, klass, *method_name[0],
*method_signature[0])
env.functions.DeleteLocalRef(env, klass)
if method == null { AndroidJniClearLookup(env) }
return method
}
AndroidJniString :: (env: *JniEnv, value: *u8) -> *void {
@@ -434,6 +449,7 @@ AndroidJniCallVoid :: (session: *AndroidJniSession, name: string,
}
env.functions.CallVoidMethodA(env, session.activity, method,
*arguments[0])
if text != null { env.functions.DeleteLocalRef(env, arguments[0].reference) }
return env.functions.ExceptionCheck(env) == 0
}
+56 -1
View File
@@ -2,6 +2,7 @@
#import "android_native"
#import "android_native_test_host"
#import "render_dispatch"
#import "std/byte_text_linux"
test_libc :: #system_library "c";
TestThread :: () -> usize #foreign test_libc "pthread_self";
@@ -21,6 +22,12 @@ env: JniEnv;
activity: NativeActivity;
app: NativeAndroidApp;
test_failure: s32;
// The fake JNIEnv's pending exception, live local references and calls.
pending: bool;
local_refs: s32;
calls: s32;
throw_call: bool;
described: s32;
ThreadIndex :: () -> s32 { return ifx TestThread() == owner then 0 else 1 }
@@ -53,7 +60,24 @@ Pop :: (input: *JniEnv, result: *void) -> *void {
local_frames[ThreadIndex()] -= 1
return result
}
Exception :: (input: *JniEnv) -> u8 { unused input; return 0 }
Exception :: (input: *JniEnv) -> u8 { unused input; return ifx pending then cast(u8)1 else cast(u8)0 }
Clear :: (input: *JniEnv) { unused input; pending = false }
Describe :: (input: *JniEnv) { unused input; described += 1 }
ObjectClass :: (input: *JniEnv, object: *void) -> *void { unused input; unused object; local_refs += 1; return cast(*void)cast(usize)2 }
DeleteRef :: (input: *JniEnv, reference: *void) { unused input; unused reference; local_refs -= 1 }
NewString :: (input: *JniEnv, value: *u8) -> *void { unused input; unused value; local_refs += 1; return cast(*void)cast(usize)4 }
// Only "present" exists, as on an activity without Kryon's Java callbacks.
MethodID :: (input: *JniEnv, klass: *void, name: *u8, signature: *u8) -> *void {
unused input; unused klass; unused signature
if TextFromCString(name) == "present" { return cast(*void)cast(usize)3 }
pending = true
return null
}
CallVoid :: (input: *JniEnv, object: *void, method: *void, arguments: *JniValue) {
unused input; unused object; unused method; unused arguments
calls += 1
if throw_call { pending = true }
}
Keep :: (argument: *void) { unused argument; AndroidJniKeepHostThread() }
Release :: (argument: *void) { unused argument; AndroidJniReleaseHostThread() }
CallJava :: (argument: *void) {
@@ -63,6 +87,26 @@ CallJava :: (argument: *void) {
AndroidJniDetachCurrent(*session)
}
// A Java-free NativeActivity has none of Kryon's optional callbacks. Each
// absent lookup clears its NoSuchMethodError and releases its class, so the
// calls after it on the same JNIEnv still work; a present method that throws
// still reports failure.
AbsentMethods :: () -> s32 {
session: AndroidJniSession
if AndroidJniAttachCurrent(*session) != 1 { return 20 }
defer AndroidJniDetachCurrent(*session)
if AndroidJniCallVoid(*session, "onNativeFirstFrame", null) || pending || local_refs != 0 { return 21 }
if AndroidJniCallVoidInteger(*session, "setSoftKeyboardVisible", "(Z)V", 1) || pending || local_refs != 0 { return 22 }
if AndroidJniMethod(*session, "setTextInputState", "(JJ[BIIZZZ)V") != null || pending || local_refs != 0 { return 23 }
if calls != 0 || described != 0 { return 24 }
text: [2]u8 = .[120, 0]
if !AndroidJniCallVoid(*session, "present", *text[0]) || calls != 1 || local_refs != 0 { return 25 }
throw_call = true
if AndroidJniCallVoidInteger(*session, "present", "(Z)V", 1) || !pending || calls != 2 { return 26 }
throw_call = false
return 0
}
#program_export
Check :: () -> s32 {
owner = TestThread()
@@ -73,6 +117,13 @@ Check :: () -> s32 {
env_functions.PushLocalFrame = Push
env_functions.PopLocalFrame = Pop
env_functions.ExceptionCheck = Exception
env_functions.ExceptionClear = Clear
env_functions.ExceptionDescribe = Describe
env_functions.GetObjectClass = ObjectClass
env_functions.DeleteLocalRef = DeleteRef
env_functions.NewStringUTF = NewString
env_functions.GetMethodID = MethodID
env_functions.CallVoidMethodA = CallVoid
vm.functions = *vm_functions
env.functions = *env_functions
activity.vm = *vm
@@ -106,6 +157,10 @@ Check :: () -> s32 {
if attachments[0] != 1 || detachments[0] != 1 || local_frames[0] != 0 { return 11 }
unused TestKeySet(attached_key, null)
if native_test_unexpected_effects != 0 || native_test_finish_calls != 0 { return 12 }
absent := AbsentMethods()
if absent != 0 { return absent }
// The throwing call's exception is described and cleared once, at detach.
if described != 1 || pending || local_refs != 0 { return 29 }
unused TestKeyDelete(attached_key)
return test_failure
}
+1 -1
View File
@@ -33,4 +33,4 @@ C
-Wl,--gc-sections -lm -lpthread -ldl -o "$output/test"
timeout 30s "$output/test"
done
echo 'Local JNI thread tests: independent render/app attachments, local frame balance, explicit cleanup and thread-exit cleanup passed (C source and saved IR)'
echo 'Local JNI thread tests: independent render/app attachments, local frame balance, explicit cleanup, thread-exit cleanup and absent optional methods passed (C source and saved IR)'