@@ -63,11 +63,19 @@ struct JNIStructTests {
6363 """
6464 @Override
6565 protected Runnable $createDestroyFunction() {
66- long $selfPointer = this.pointer();
66+ long self$ = this.$memoryAddress();
67+ if (CallTraces.TRACE_DOWNCALLS) {
68+ CallTraces.traceDowncall( " MyStruct.$createDestroyFunction " ,
69+ " this " , this,
70+ " self " , self$);
71+ }
6772 return new Runnable() {
6873 @Override
6974 public void run() {
70- MyStruct.$destroy($selfPointer);
75+ if (CallTraces.TRACE_DOWNCALLS) {
76+ CallTraces.traceDowncall( " MyStruct.$destroy " , " self " , self$);
77+ }
78+ MyStruct.$destroy(self$);
7179 }
7280 };
7381 }
@@ -90,8 +98,8 @@ struct JNIStructTests {
9098 * }
9199 */
92100 public static MyStruct init(long x, long y, SwiftArena swiftArena$) {
93- long selfPointer = MyStruct.allocatingInit(x, y);
94- return new MyStruct(selfPointer , swiftArena$);
101+ long self$ = MyStruct.allocatingInit(x, y);
102+ return new MyStruct(self$ , swiftArena$);
95103 }
96104 """ ,
97105 """
@@ -112,9 +120,9 @@ struct JNIStructTests {
112120 """
113121 @_cdecl( " Java_com_example_swift_MyStruct_allocatingInit__JJ " )
114122 func Java_com_example_swift_MyStruct_allocatingInit__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, y: jlong) -> jlong {
115- let selfPointer = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
116- selfPointer .initialize(to: MyStruct(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
117- return Int64(Int(bitPattern: selfPointer )).getJNIValue(in: environment)
123+ let self$ = UnsafeMutablePointer<MyStruct>.allocate(capacity: 1)
124+ self$ .initialize(to: MyStruct(x: Int64(fromJNI: x, in: environment!), y: Int64(fromJNI: y, in: environment!)))
125+ return Int64(Int(bitPattern: self$ )).getJNIValue(in: environment)
118126 }
119127 """
120128 ]
@@ -127,14 +135,21 @@ struct JNIStructTests {
127135 input: source,
128136 . jni,
129137 . swift,
130- detectChunkByInitialLines: 1 ,
131138 expectedChunks: [
132139 """
133140 @_cdecl( " Java_com_example_swift_MyStruct__00024destroy__J " )
134141 func Java_com_example_swift_MyStruct__00024destroy__J(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, selfPointer: jlong) {
135- let pointer = UnsafeMutablePointer<MyStruct>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
136- pointer.deinitialize(count: 1)
137- pointer.deallocate()
142+ guard let env$ = environment else {
143+ fatalError( " Missing JNIEnv in downcall to \\ (#function) " )
144+ }
145+ assert(selfPointer != 0, " selfPointer memory address was null " )
146+ let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
147+ assert(selfBits$ != 0, " $self memory address was null: selfPointer = \\ (selfPointer) " )
148+ guard let self$ = UnsafeMutablePointer<MyStruct>(bitPattern: selfBits$) else {
149+ fatalError( " Missing self pointer in call to \\ (#function)! " )
150+ }
151+ self$.deinitialize(count: 1)
152+ self$.deallocate()
138153 }
139154 """
140155 ]
@@ -156,8 +171,8 @@ struct JNIStructTests {
156171 * }
157172 */
158173 public void doSomething(long x) {
159- long selfPointer = this.pointer ();
160- MyStruct.$doSomething(x, selfPointer );
174+ long self$ = this.$memoryAddress ();
175+ MyStruct.$doSomething(x, self$ );
161176 }
162177 """ ,
163178 """
@@ -178,7 +193,15 @@ struct JNIStructTests {
178193 """
179194 @_cdecl( " Java_com_example_swift_MyStruct__00024doSomething__JJ " )
180195 func Java_com_example_swift_MyStruct__00024doSomething__JJ(environment: UnsafeMutablePointer<JNIEnv?>!, thisClass: jclass, x: jlong, selfPointer: jlong) {
181- let self$ = UnsafeMutablePointer<MyStruct>(bitPattern: Int(Int64(fromJNI: selfPointer, in: environment!)))!
196+ guard let env$ = environment else {
197+ fatalError( " Missing JNIEnv in downcall to \\ (#function) " )
198+ }
199+ assert(selfPointer != 0, " selfPointer memory address was null " )
200+ let selfBits$ = Int(Int64(fromJNI: selfPointer, in: env$))
201+ assert(selfBits$ != 0, " $self memory address was null: selfPointer = \\ (selfPointer) " )
202+ guard let self$ = UnsafeMutablePointer<MyStruct>(bitPattern: selfBits$) else {
203+ fatalError( " Missing self pointer in call to \\ (#function)! " )
204+ }
182205 self$.pointee.doSomething(x: Int64(fromJNI: x, in: environment!))
183206 }
184207 """ ,
0 commit comments