also looking at this
feat(android): getCalendarEvents and createCalendarEvent in Zig — ratchet 86 → 84
#158
7 files
+1167
-20
| @@ -642,6 +642,14 @@ pub const Jni = struct { | ||
| 642 | 642 | return result != JNI_FALSE; |
| 643 | 643 | } |
| 644 | 644 | |
| 645 | pub fn callLongMethodA(self: Self, obj: jobject, id: jmethodID, args: []const jvalue) JniError!jlong { | |
| 646 | const call: *const fn (JNIEnv, jobject, jmethodID, [*]const jvalue) callconv(.c) jlong = | |
| 647 | @ptrCast(self.table().CallLongMethodA orelse return JniError.NotFound); | |
| 648 | const result = call(self.env, obj, id, args.ptr); | |
| 649 | try self.check(); | |
| 650 | return result; | |
| 651 | } | |
| 652 | ||
| 645 | 653 | pub fn callVoidMethodA(self: Self, obj: jobject, id: jmethodID, args: []const jvalue) JniError!void { |
| 646 | 654 | const call: *const fn (JNIEnv, jobject, jmethodID, [*]const jvalue) callconv(.c) void = |
| 647 | 655 | @ptrCast(self.table().CallVoidMethodA orelse return JniError.NotFound); |
| @@ -706,10 +714,14 @@ pub const Jni = struct { | ||
| 706 | 714 | |
| 707 | 715 | // --- Arrays ----------------------------------------------------------- |
| 708 | 716 | // |
| 709 | // Only `long[]` so far, because only `VibrationEffect.createWaveform` | |
| 710 | // needs one. `SetLongArrayRegion` copies from a Zig slice in one call | |
| 711 | // rather than element by element, which is both faster and the only form | |
| 712 | // that cannot leave a partially-filled array behind on an error. | |
| 717 | // `long[]` for `VibrationEffect.createWaveform`, and `String[]` for the | |
| 718 | // projection and selection arguments a `ContentResolver.query` takes. | |
| 719 | // | |
| 720 | // The two are shaped differently because JNI is: a primitive array can be | |
| 721 | // filled from a Zig slice in one `SetLongArrayRegion` call, which is both | |
| 722 | // faster and the only form that cannot leave a partially-filled array | |
| 723 | // behind on an error. An object array has no region form and has to be | |
| 724 | // written a slot at a time. | |
| 713 | 725 | |
| 714 | 726 | pub fn newLongArray(self: Self, values: []const jlong) JniError!jobject { |
| 715 | 727 | const new: *const fn (JNIEnv, jsize) callconv(.c) jobject = |
| @@ -728,6 +740,29 @@ pub const Jni = struct { | ||
| 728 | 740 | return array; |
| 729 | 741 | } |
| 730 | 742 | |
| 743 | /// `new <element_class>[len]`, every slot null. | |
| 744 | /// | |
| 745 | /// `NewObjectArray` takes an initial element for every slot; null is what | |
| 746 | /// the Kotlin's `arrayOf(...)` starts from before its entries are written, | |
| 747 | /// and every caller here fills the array immediately. | |
| 748 | pub fn newObjectArray(self: Self, len: usize, element_class: jclass) JniError!jobject { | |
| 749 | const new: *const fn (JNIEnv, jsize, jclass, jobject) callconv(.c) jobject = | |
| 750 | @ptrCast(self.table().NewObjectArray orelse return JniError.NotFound); | |
| 751 | const array = new(self.env, @intCast(len), element_class, null) orelse { | |
| 752 | try self.check(); | |
| 753 | return JniError.OutOfMemory; | |
| 754 | }; | |
| 755 | try self.check(); | |
| 756 | return array; | |
| 757 | } | |
| 758 | ||
| 759 | pub fn setObjectArrayElement(self: Self, array: jobject, index: usize, value: jobject) JniError!void { | |
| 760 | const set: *const fn (JNIEnv, jobject, jsize, jobject) callconv(.c) void = | |
| 761 | @ptrCast(self.table().SetObjectArrayElement orelse return JniError.NotFound); | |
| 762 | set(self.env, array, @intCast(index), value); | |
| 763 | try self.check(); | |
| 764 | } | |
| 765 | ||
| 731 | 766 | /// Make a Java `String` from UTF-8. |
| 732 | 767 | /// |
| 733 | 768 | /// The JVM wants *modified* UTF-8 here, the same encoding `stringToUtf8` |
| @@ -783,6 +818,14 @@ pub const Jni = struct { | ||
| 783 | 818 | return result; |
| 784 | 819 | } |
| 785 | 820 | |
| 821 | pub fn callStaticLongMethodA(self: Self, cls: jclass, id: jmethodID, args: []const jvalue) JniError!jlong { | |
| 822 | const call: *const fn (JNIEnv, jclass, jmethodID, [*]const jvalue) callconv(.c) jlong = | |
| 823 | @ptrCast(self.table().CallStaticLongMethodA orelse return JniError.NotFound); | |
| 824 | const result = call(self.env, cls, id, args.ptr); | |
| 825 | try self.check(); | |
| 826 | return result; | |
| 827 | } | |
| 828 | ||
| 786 | 829 | pub fn callStaticVoidMethodA(self: Self, cls: jclass, id: jmethodID, args: []const jvalue) JniError!void { |
| 787 | 830 | const call: *const fn (JNIEnv, jclass, jmethodID, [*]const jvalue) callconv(.c) void = |
| 788 | 831 | @ptrCast(self.table().CallStaticVoidMethodA orelse return JniError.NotFound); |