Package: jaffl / 0.5.9-5

string_array_support.diff Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
Description: Backport String[] to 0.5_branch
 The commits include:
 Yours from master, with modifications to merge successfully
 Tweaks to use 0.5 versions of memory allocation, dispose (i.e. finalize), and platform bits.
 All tests pass, including the backported StringArrayTest. execv works properly with this build and updated jnr-posix called from JRuby.
Origin: https://github.com/wmeissner/jaffl/pull/2
Author: Charles Oliver Nutter <headius>
--- a/src/com/kenai/jaffl/Pointer.java
+++ b/src/com/kenai/jaffl/Pointer.java
@@ -1,6 +1,8 @@
 
 package com.kenai.jaffl;
 
+import java.nio.charset.Charset;
+
 public interface Pointer {
     public static final int SIZE = Platform.getPlatform().addressSize();
 
@@ -31,6 +33,8 @@
     abstract public Pointer getPointer(long offset);
     abstract public void putPointer(long offset, Pointer value);
     abstract public String getString(long offset);
+    abstract public String getString(long offset, int maxLength, Charset cs);
+    abstract public void putString(long offset, String string, int maxLength, Charset cs);
     abstract public long address();
     abstract public boolean isDirect();
 }
--- a/src/com/kenai/jaffl/provider/jffi/AsmLibraryLoader.java
+++ b/src/com/kenai/jaffl/provider/jffi/AsmLibraryLoader.java
@@ -98,6 +98,7 @@
                 || Struct.class.isAssignableFrom(type)
                 || (type.isArray() && Struct.class.isAssignableFrom(type.getComponentType()))
                 || (type.isArray() && Pointer.class.isAssignableFrom(type.getComponentType()))
+                || (type.isArray() && CharSequence.class.isAssignableFrom(type.getComponentType()))
                 || CharSequence.class.isAssignableFrom(type)
                 || ByReference.class.isAssignableFrom(type)
                 || StringBuilder.class.isAssignableFrom(type)
@@ -562,6 +563,11 @@
                 // stack should be: [ Buffer, array, flags ]
                 marshal(mv, CharSequence.class);
 
+            } else if (parameterTypes[i].isArray() && CharSequence.class.isAssignableFrom(parameterTypes[i].getComponentType())) {
+                mv.pushInt(parameterFlags);
+                mv.pushInt(nativeArrayFlags);
+                sessionmarshal(mv, CharSequence[].class, int.class, int.class);
+
             } else if (Struct.class.isAssignableFrom(parameterTypes[i])) {
                 mv.pushInt(parameterFlags);
                 mv.pushInt(nativeArrayFlags);
@@ -1008,7 +1014,9 @@
         return StringBuilder.class.isAssignableFrom(parameterType)
                 || StringBuffer.class.isAssignableFrom(parameterType)
                 || ByReference.class.isAssignableFrom(parameterType)
-                || (parameterType.isArray() && Pointer.class.isAssignableFrom(parameterType.getComponentType()));
+                || (parameterType.isArray() && Pointer.class.isAssignableFrom(parameterType.getComponentType()))
+                || (parameterType.isArray() && CharSequence.class.isAssignableFrom(parameterType.getComponentType()))
+                ;
     }
 
     private static boolean isSessionRequired(Class[] parameterTypes) {
--- a/src/com/kenai/jaffl/provider/jffi/AsmRuntime.java
+++ b/src/com/kenai/jaffl/provider/jffi/AsmRuntime.java
@@ -269,23 +269,70 @@
         }
     }
 
+    public static final void marshal(InvocationSession session, InvocationBuffer buffer,
+            final CharSequence[] strings, final int inout, int nativeArrayFlags) {
+        if (strings == null) {
+            buffer.putAddress(0L);
+
+        } else {
+            final AllocatedDirectMemoryIO[] pointers = new AllocatedDirectMemoryIO[strings.length];
+            final StringIO io = StringIO.getStringIO();
+
+            if (ParameterFlags.isIn(inout)) {
+                for (int i = 0; i < strings.length; ++i) {
+                    if (strings[i] != null) {
+                        ByteBuffer buf = io.toNative(strings[i], strings[i].length(), ParameterFlags.isIn(inout));
+                        AllocatedDirectMemoryIO ptr = new AllocatedDirectMemoryIO(buf.remaining(), false);
+                        ptr.put(0, buf.array(), buf.arrayOffset() + buf.position(), buf.remaining());
+                        pointers[i] = ptr;
+
+                    } else {
+                        pointers[i] = null;
+                    }
+                }
+            }
+
+            // pass it as an array of pointers
+            final Pointer[] tmp = new Pointer[pointers.length];
+            System.arraycopy(pointers, 0, tmp, 0, tmp.length);
+            marshal(session, buffer, tmp, inout, nativeArrayFlags);
+
+            // Reload any elements of the native array that were changed, and convert back to java strings
+            // the PostInvoke also keeps the native memory alive until after the function call
+            session.addPostInvoke(new InvocationSession.PostInvoke() {
+
+                public void postInvoke() {
+                    if (ParameterFlags.isOut(inout)) {
+                        for (int i = 0; i < pointers.length; ++i) {
+                            if (tmp[i] != null) {
+                                strings[i] = tmp[i].getString(0);
+                            }
+                        }
+                    }
+                }
+            });
+
+        }
+    }
+
+
     public static final void marshal(InvocationBuffer buffer, final Enum parameter) {
         buffer.putInt(EnumMapper.getInstance().intValue(parameter));
     }
 
 
-    public static final void marshal(InvocationSession session, InvocationBuffer buffer, Pointer[] parameter, int inout, int nativeArrayFlags) {
-        if (parameter == null) {
+    public static final void marshal(InvocationSession session, InvocationBuffer buffer, 
+            final Pointer[] pointers, int inout, int nativeArrayFlags) {
+        if (pointers == null) {
             buffer.putAddress(0L);
         } else {
-            final Pointer[] array = parameter;
             if (Pointer.SIZE == 32) {
-                final int[] raw = new int[array.length];
-                for (int i = 0; i < raw.length; ++i) {
-                    if (!array[i].isDirect()) {
+                final int[] raw = new int[pointers.length + 1];
+                for (int i = 0; i < pointers.length; ++i) {
+                    if (pointers[i] != null && !pointers[i].isDirect()) {
                         throw new IllegalArgumentException("invalid pointer in array at index " + i);
                     }
-                    raw[i] = (int) array[i].address();
+                    raw[i] = pointers[i] != null ? (int) pointers[i].address() : 0;
                 }
                 buffer.putArray(raw, 0, raw.length, nativeArrayFlags);
 
@@ -293,19 +340,19 @@
                     session.addPostInvoke(new InvocationSession.PostInvoke() {
 
                         public void postInvoke() {
-                            for (int i = 0; i < raw.length; ++i) {
-                                array[i] = MemoryUtil.newPointer(raw[i]);
+                            for (int i = 0; i < pointers.length; ++i) {
+                                pointers[i] = MemoryUtil.newPointer(raw[i]);
                             }
                         }
                     });
                 }
             } else {
-                final long[] raw = new long[array.length];
-                for (int i = 0; i < raw.length; ++i) {
-                    if (!array[i].isDirect()) {
+                final long[] raw = new long[pointers.length + 1];
+                for (int i = 0; i < pointers.length; ++i) {
+                    if (pointers[i] != null && !pointers[i].isDirect()) {
                         throw new IllegalArgumentException("invalid pointer in array at index " + i);
                     }
-                    raw[i] = array[i].address();
+                    raw[i] = pointers[i] != null ? pointers[i].address() : 0;
                 }
 
                 buffer.putArray(raw, 0, raw.length, nativeArrayFlags);
@@ -314,8 +361,8 @@
                     session.addPostInvoke(new InvocationSession.PostInvoke() {
 
                         public void postInvoke() {
-                            for (int i = 0; i < raw.length; ++i) {
-                                array[i] = MemoryUtil.newPointer(raw[i]);
+                            for (int i = 0; i < pointers.length; ++i) {
+                                pointers[i] = MemoryUtil.newPointer(raw[i]);
                             }
                         }
                     });
--- a/src/com/kenai/jaffl/provider/jffi/DefaultInvokerFactory.java
+++ b/src/com/kenai/jaffl/provider/jffi/DefaultInvokerFactory.java
@@ -254,8 +254,13 @@
             return new StringBufferMarshaller(getParameterFlags(annotations));
         } else if (StringBuilder.class.isAssignableFrom(type)) {
             return new StringBuilderMarshaller(getParameterFlags(annotations));
+
         } else if (CharSequence.class.isAssignableFrom(type)) {
             return CharSequenceMarshaller.INSTANCE;
+
+        } else if (type.isArray() && CharSequence.class.isAssignableFrom(type.getComponentType())) {
+            return new StringArrayMarshaller(getParameterFlags(annotations));
+
         } else if (ByReference.class.isAssignableFrom(type)) {
             return new ByReferenceMarshaller(getParameterFlags(annotations));
         } else if (Struct.class.isAssignableFrom(type)) {
@@ -286,6 +291,10 @@
             return new DoubleArrayMarshaller(getParameterFlags(annotations));
         } else if (type.isArray() && Struct.class.isAssignableFrom(type.getComponentType())) {
             return new StructArrayMarshaller(getParameterFlags(annotations));
+
+        } else if (type.isArray() && Pointer.class.isAssignableFrom(type.getComponentType())) {
+            return new PointerArrayMarshaller(getParameterFlags(annotations));
+
         } else {
             throw new IllegalArgumentException("Unsupported parameter type: " + type);
         }
@@ -574,11 +583,25 @@
         }
     }
 
+    static final class StringArrayMarshaller extends SessionRequiredMarshaller {
+        private final int nflags, inout;
+        public StringArrayMarshaller(int inout) {
+            this.inout = inout;
+            this.nflags = getNativeArrayFlags(inout | (ParameterFlags.isIn(inout) ? ParameterFlags.NULTERMINATE : 0));
+        }
+
+
+        @Override
+        public void marshal(InvocationSession session, InvocationBuffer buffer, Object parameter) {
+            AsmRuntime.marshal(session, buffer, (CharSequence[]) parameter, inout, nflags);
+        }
+    }
+
     static final class StringBufferMarshaller extends SessionRequiredMarshaller {
         private final int nflags, inout;
         public StringBufferMarshaller(int inout) {
             this.inout = inout;
-            this.nflags = getNativeArrayFlags(inout | (ParameterFlags.isIn(inout) ? ParameterFlags.NULTERMINATE : 0));
+            this.nflags = getNativeArrayFlags(inout);
         }
 
 
@@ -652,6 +675,19 @@
         }
     }
 
+    static final class PointerArrayMarshaller extends SessionRequiredMarshaller {
+        private final int nflags, inout;
+        public PointerArrayMarshaller(int inout) {
+            this.inout = inout;
+            this.nflags = getNativeArrayFlags(inout);
+        }
+
+        @Override
+        public void marshal(InvocationSession session, InvocationBuffer buffer, Object parameter) {
+            AsmRuntime.marshal(session, buffer, (Pointer[]) parameter, inout, nflags);
+        }
+    }
+
     static final class ByteBufferMarshaller extends BaseMarshaller {
         private final int flags;
         public ByteBufferMarshaller(int flags) {
--- /dev/null
+++ b/test/com/kenai/jaffl/StringArrayTest.java
@@ -0,0 +1,78 @@
+
+package com.kenai.jaffl;
+
+import com.kenai.jaffl.annotations.In;
+import com.kenai.jaffl.annotations.Out;
+import java.nio.charset.Charset;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+/**
+ *
+ * @author wayne
+ */
+public class StringArrayTest {
+
+    public StringArrayTest() {
+    }
+
+    public static interface TestLib {
+        String ptr_return_array_element(@In String[] array, int index);
+        void ptr_set_array_element(@Out String[] array, int index, Pointer value);
+    }
+    
+    static TestLib testlib;
+    static Runtime runtime;
+    @BeforeClass
+    public static void setUpClass() throws Exception {
+        testlib = TstUtil.loadTestLib(TestLib.class);
+    }
+
+    @AfterClass
+    public static void tearDownClass() throws Exception {
+    }
+
+    @Before
+    public void setUp() {
+    }
+
+    @After
+    public void tearDown() {
+    }
+
+    // TODO add test methods here.
+    // The methods must be annotated with annotation @Test. For example:
+    //
+    // @Test
+    // public void hello() {}
+    @Test public void lastElementOfStringArrayShouldBeNull() {
+        String[] strings = { "test" };
+        String result = testlib.ptr_return_array_element(strings, 1);
+        assertNull("last element of string array was not null", result);
+    }
+
+    @Test public void firstElementOfStringArrayShouldNotBeNull() {
+        final String MAGIC = "test";
+        String[] strings = { MAGIC };
+        assertNotNull(testlib.ptr_return_array_element(strings, 0));
+    }
+
+    @Test public void firstElementOfStringArrayShouldEqualOriginalValue() {
+        final String MAGIC = "test";
+        String[] strings = { MAGIC };
+        assertEquals(MAGIC, testlib.ptr_return_array_element(strings, 0));
+    }
+
+    @Test public void elementsSetByNativeCodeShouldBeReloaded() {
+        final String MAGIC = "test";
+        String[] strings = new String[1];
+        Pointer ptr = FFIProvider.getProvider().getMemoryManager().allocateDirect(1024);
+        ptr.putString(0, MAGIC, 1024, Charset.defaultCharset());
+        testlib.ptr_set_array_element(strings, 0, ptr);
+        assertEquals(MAGIC, strings[0]);
+    }
+}
\ No newline at end of file