also looking at this
feat(android): getMemoryUsage and log — ratchet 102 -> 100
#143
7 files
+377
-3
| @@ -37,6 +37,7 @@ const std = @import("std"); | ||
| 37 | 37 | const builtin = @import("builtin"); |
| 38 | 38 | const jni = @import("jni_runtime.zig"); |
| 39 | 39 | const device = @import("bridge_android_device.zig"); |
| 40 | const system = @import("bridge_android_system.zig"); | |
| 40 | 41 | |
| 41 | 42 | const Jni = jni.Jni; |
| 42 | 43 | |
| @@ -122,6 +123,20 @@ const natives = [_]jni.JNINativeMethod{ | ||
| 122 | 123 | .signature = "(Landroid/app/Activity;)Ljava/lang/String;", |
| 123 | 124 | .fnPtr = @ptrCast(&nativeGetDeviceInfo), |
| 124 | 125 | }, |
| 126 | // No Activity parameter: `Runtime` and `Log` are static, so neither of | |
| 127 | // these needs anything the app hands over. The signature says so, and a | |
| 128 | // signature that asked for one would fail registration rather than be | |
| 129 | // quietly ignored. | |
| 130 | .{ | |
| 131 | .name = "nativeGetMemoryUsage", | |
| 132 | .signature = "()Ljava/lang/String;", | |
| 133 | .fnPtr = @ptrCast(&nativeGetMemoryUsage), | |
| 134 | }, | |
| 135 | .{ | |
| 136 | .name = "nativeLog", | |
| 137 | .signature = "(Ljava/lang/String;)Z", | |
| 138 | .fnPtr = @ptrCast(&nativeLog), | |
| 139 | }, | |
| 125 | 140 | }; |
| 126 | 141 | |
| 127 | 142 | /// How binding went. A value rather than a log line, so every path is |
| @@ -184,6 +199,49 @@ fn nativeGetDeviceInfo(env: jni.JNIEnv, _: jni.jobject, activity: jni.jobject) c | ||
| 184 | 199 | return j.newStringUtf(owned.ptr) catch null; |
| 185 | 200 | } |
| 186 | 201 | |
| 202 | /// `nativeGetMemoryUsage()` — the JVM heap, as JSON. | |
| 203 | fn nativeGetMemoryUsage(env: jni.JNIEnv, _: jni.jobject) callconv(.c) jni.jstring { | |
| 204 | const j = Jni.init(env); | |
| 205 | ||
| 206 | var arena = std.heap.ArenaAllocator.init(backing); | |
| 207 | defer arena.deinit(); | |
| 208 | const allocator = arena.allocator(); | |
| 209 | ||
| 210 | const usage = system.readMemory(j) catch |err| { | |
| 211 | std.log.warn("craft: getMemoryUsage fell through to the shim ({s})", .{@errorName(err)}); | |
| 212 | return null; | |
| 213 | }; | |
| 214 | ||
| 215 | const json = system.renderMemory(allocator, usage) catch return null; | |
| 216 | const owned = allocator.allocSentinel(u8, json.len, 0) catch return null; | |
| 217 | @memcpy(owned, json); | |
| 218 | ||
| 219 | return j.newStringUtf(owned.ptr) catch null; | |
| 220 | } | |
| 221 | ||
| 222 | /// `nativeLog(message)` — returns whether Zig wrote the line. | |
| 223 | /// | |
| 224 | /// A boolean rather than void, because "not mine" has to be expressible. | |
| 225 | /// `void` would leave the Kotlin unable to tell a successful native log from a | |
| 226 | /// runtime that declined, and it would log the message twice or not at all. | |
| 227 | fn nativeLog(env: jni.JNIEnv, _: jni.jobject, message: jni.jstring) callconv(.c) jni.jboolean { | |
| 228 | const j = Jni.init(env); | |
| 229 | ||
| 230 | var arena = std.heap.ArenaAllocator.init(backing); | |
| 231 | defer arena.deinit(); | |
| 232 | const allocator = arena.allocator(); | |
| 233 | ||
| 234 | const text = j.stringToUtf8(allocator, message) catch return jni.JNI_FALSE; | |
| 235 | const owned = allocator.allocSentinel(u8, text.len, 0) catch return jni.JNI_FALSE; | |
| 236 | @memcpy(owned, text); | |
| 237 | ||
| 238 | system.writeLog(j, owned.ptr) catch |err| { | |
| 239 | std.log.warn("craft: log fell through to the shim ({s})", .{@errorName(err)}); | |
| 240 | return jni.JNI_FALSE; | |
| 241 | }; | |
| 242 | return jni.JNI_TRUE; | |
| 243 | } | |
| 244 | ||
| 187 | 245 | // ============================================================================= |
| 188 | 246 | // Tests |
| 189 | 247 | // ============================================================================= |
| @@ -295,7 +353,7 @@ test "the registered natives name methods the Kotlin actually declares" { | ||
| 295 | 353 | // A descriptor is checked by the JVM at registration, so a wrong one fails |
| 296 | 354 | // at load rather than at call — but only if the *name* matches something. |
| 297 | 355 | // These two strings are the contract with CraftBridge.kt. |
| 298 | try testing.expectEqual(@as(usize, 1), natives.len); | |
| 356 | try testing.expectEqual(@as(usize, 3), natives.len); | |
| 299 | 357 | try testing.expectEqualStrings("nativeGetDeviceInfo", std.mem.span(natives[0].name)); |
| 300 | 358 | |
| 301 | 359 | // And the class they bind to is the fixed one, not the templated bridge. |