// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The few JNI calls the game makes, for what only Java can say: the //! system's accent color, the window's insets, and hiding the system bars. //! //! JNI is a table of function pointers handed to native code, not a library, //! so there is nothing to link. The table's layout is fixed by the JNI //! specification, and each function is found by its index in it; the "A" //! form of each call is used, which takes its arguments as an array rather //! than as C varargs. const std = @import("std"); pub const Object = *opaque {}; pub const Class = Object; pub const String = Object; pub const MethodId = *opaque {}; pub const FieldId = *opaque {}; pub const Value = extern union { z: u8, b: i8, c: u16, s: i16, i: i32, j: i64, f: f32, d: f64, l: ?Object, }; pub const JavaVM = extern struct { functions: *const [8]?*const anyopaque, }; /// A thread's `JNIEnv`, which is a pointer to the table. pub const Env = extern struct { functions: *const [229]?*const anyopaque, fn get(env: *Env, comptime index: usize, comptime T: type) T { return @ptrCast(@alignCast(env.functions[index].?)); } pub fn findClass(env: *Env, name: [*:0]const u8) ?Class { return env.get(6, *const fn (*Env, [*:0]const u8) callconv(.c) ?Class)(env, name); } pub fn exceptionClear(env: *Env) void { env.get(17, *const fn (*Env) callconv(.c) void)(env); } pub fn deleteLocalRef(env: *Env, object: ?Object) void { if (object) |o| env.get(23, *const fn (*Env, Object) callconv(.c) void)(env, o); } pub fn getObjectClass(env: *Env, object: Object) Class { return env.get(31, *const fn (*Env, Object) callconv(.c) Class)(env, object); } pub fn getMethodId(env: *Env, class: Class, name: [*:0]const u8, signature: [*:0]const u8) ?MethodId { return env.get(33, *const fn (*Env, Class, [*:0]const u8, [*:0]const u8) callconv(.c) ?MethodId)(env, class, name, signature); } pub fn callObjectMethod(env: *Env, object: Object, method: MethodId, args: []const Value) ?Object { return env.get(36, *const fn (*Env, Object, MethodId, [*]const Value) callconv(.c) ?Object)(env, object, method, args.ptr); } pub fn callIntMethod(env: *Env, object: Object, method: MethodId, args: []const Value) i32 { return env.get(51, *const fn (*Env, Object, MethodId, [*]const Value) callconv(.c) i32)(env, object, method, args.ptr); } pub fn callVoidMethod(env: *Env, object: Object, method: MethodId, args: []const Value) void { env.get(63, *const fn (*Env, Object, MethodId, [*]const Value) callconv(.c) void)(env, object, method, args.ptr); } pub fn getFieldId(env: *Env, class: Class, name: [*:0]const u8, signature: [*:0]const u8) ?FieldId { return env.get(94, *const fn (*Env, Class, [*:0]const u8, [*:0]const u8) callconv(.c) ?FieldId)(env, class, name, signature); } pub fn getIntField(env: *Env, object: Object, field: FieldId) i32 { return env.get(100, *const fn (*Env, Object, FieldId) callconv(.c) i32)(env, object, field); } pub fn getStaticMethodId(env: *Env, class: Class, name: [*:0]const u8, signature: [*:0]const u8) ?MethodId { return env.get(113, *const fn (*Env, Class, [*:0]const u8, [*:0]const u8) callconv(.c) ?MethodId)(env, class, name, signature); } pub fn callStaticIntMethod(env: *Env, class: Class, method: MethodId, args: []const Value) i32 { return env.get(131, *const fn (*Env, Class, MethodId, [*]const Value) callconv(.c) i32)(env, class, method, args.ptr); } pub fn newStringUtf(env: *Env, text: [*:0]const u8) ?String { return env.get(167, *const fn (*Env, [*:0]const u8) callconv(.c) ?String)(env, text); } pub fn exceptionCheck(env: *Env) bool { return env.get(228, *const fn (*Env) callconv(.c) u8)(env) != 0; } /// Whether the last call threw, clearing what it threw: nothing here is /// worth more than carrying on without it. pub fn threw(env: *Env) bool { if (!env.exceptionCheck()) return false; env.exceptionClear(); return true; } /// `object.name()`, for a method returning an object. pub fn callObject(env: *Env, object: Object, name: [*:0]const u8, signature: [*:0]const u8, args: []const Value) ?Object { const class = env.getObjectClass(object); defer env.deleteLocalRef(class); const method = env.getMethodId(class, name, signature) orelse { _ = env.threw(); return null; }; const result = env.callObjectMethod(object, method, args); if (env.threw()) return null; return result; } /// `object.name()`, for a method returning an int. pub fn callInt(env: *Env, object: Object, name: [*:0]const u8, signature: [*:0]const u8, args: []const Value) ?i32 { const class = env.getObjectClass(object); defer env.deleteLocalRef(class); const method = env.getMethodId(class, name, signature) orelse { _ = env.threw(); return null; }; const result = env.callIntMethod(object, method, args); if (env.threw()) return null; return result; } /// `object.name()`, for a method returning nothing; false if it threw. pub fn callVoid(env: *Env, object: Object, name: [*:0]const u8, signature: [*:0]const u8, args: []const Value) bool { const class = env.getObjectClass(object); defer env.deleteLocalRef(class); const method = env.getMethodId(class, name, signature) orelse { _ = env.threw(); return false; }; env.callVoidMethod(object, method, args); return !env.threw(); } /// `Class.name()`, for a static method returning an int. pub fn callStaticInt(env: *Env, class_name: [*:0]const u8, name: [*:0]const u8, signature: [*:0]const u8, args: []const Value) ?i32 { const class = env.findClass(class_name) orelse { _ = env.threw(); return null; }; defer env.deleteLocalRef(class); const method = env.getStaticMethodId(class, name, signature) orelse { _ = env.threw(); return null; }; const result = env.callStaticIntMethod(class, method, args); if (env.threw()) return null; return result; } /// `object.name`, for an int field. pub fn intField(env: *Env, object: Object, name: [*:0]const u8) ?i32 { const class = env.getObjectClass(object); defer env.deleteLocalRef(class); const field = env.getFieldId(class, name, "I") orelse { _ = env.threw(); return null; }; return env.getIntField(object, field); } }; comptime { std.debug.assert(@sizeOf(Value) == 8); }