• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/17_lib_ffi/28_dlopen_cdefs

  1 The `ffi.dlopen()` function now accepts an optional third argument containing C definitions that are automatically parsed and wrapped.
  2 
  3 Basic function wrapping with zlib.
  4 
  5 -- Testcase --
  6 {%
  7 import * as ffi from 'ffi';
  8 
  9 let libz = ffi.dlopen('z', false, 'const char *zlibVersion(void);');
 10 print('zlibVersion = ', replace(ffi.string(libz.zlibVersion()), /^([0-9]+)\..+$/, '$1.*'), '\n');
 11 -- End --
 12 
 13 -- Expect stdout --
 14 zlibVersion = 1.*
 15 -- End --
 16 
 17 
 18 Global C library with single function.
 19 
 20 -- Testcase --
 21 {%
 22 import * as ffi from 'ffi';
 23 
 24 let libc = ffi.dlopen(null, false, 'int getpid(void);');
 25 let pid = libc.getpid();
 26 print('getpid works\n');
 27 -- End --
 28 
 29 -- Expect stdout --
 30 getpid works
 31 -- End --
 32 
 33 
 34 Multiple functions in single cdef.
 35 
 36 -- Testcase --
 37 {%
 38 import * as ffi from 'ffi';
 39 
 40 let libc = ffi.dlopen(null, false, 'int getpid(void);\nint getppid(void);');
 41 let pid = libc.getpid();
 42 let ppid = libc.getppid();
 43 print('getpid and getppid work\n');
 44 -- End --
 45 
 46 -- Expect stdout --
 47 getpid and getppid work
 48 -- End --
 49 
 50 
 51 Backward compatibility - dlopen without cdefs.
 52 
 53 -- Testcase --
 54 {%
 55 import * as ffi from 'ffi';
 56 
 57 let libz = ffi.dlopen('z');
 58 print('dlopen without cdefs works\n');
 59 -- End --
 60 
 61 -- Expect stdout --
 62 dlopen without cdefs works
 63 -- End --
 64 
 65 
 66 Error handling - invalid cdefs syntax.
 67 
 68 -- Testcase --
 69 {%
 70 import * as ffi from 'ffi';
 71 
 72 try {
 73     ffi.dlopen(null, false, 'invalid syntax here');
 74     print('ERROR - should have thrown\n');
 75 } catch (e) {
 76     print('Correctly caught error\n');
 77 }
 78 -- End --
 79 
 80 -- Expect stdout --
 81 Correctly caught error
 82 -- End --
 83 
 84 
 85 Functions with integer parameters.
 86 
 87 -- Testcase --
 88 {%
 89 import * as ffi from 'ffi';
 90 
 91 let libz = ffi.dlopen('z', false, 'int compressBound(long srcLen);');
 92 let result = libz.compressBound(1024);
 93 print('compressBound(1024) =', result, '\n');
 94 -- End --
 95 
 96 -- Expect stdout --
 97 compressBound(1024) =1037
 98 -- End --
 99 
100 
101 Functions with pointer parameters (using ffi.string).
102 
103 -- Testcase --
104 {%
105 import * as ffi from 'ffi';
106 
107 let libc = ffi.dlopen(null, false, 'char *strerror(int errnum);');
108 let msg = libc.strerror(0);
109 print('strerror(0) works\n');
110 -- End --
111 
112 -- Expect stdout --
113 strerror(0) works
114 -- End --
115 
116 
117 Empty cdefs string (should work, no functions added).
118 
119 -- Testcase --
120 {%
121 import * as ffi from 'ffi';
122 
123 let libz = ffi.dlopen('z', false, '');
124 print('empty cdefs works\n');
125 -- End --
126 
127 -- Expect stdout --
128 empty cdefs works
129 -- End --
130 
131 
132 Null library name with cdefs (global library).
133 
134 -- Testcase --
135 {%
136 import * as ffi from 'ffi';
137 
138 let libc = ffi.dlopen(null, false, 'int abs(int);');
139 let result = libc.abs(-42);
140 print('abs(-42) =', result, '\n');
141 -- End --
142 
143 -- Expect stdout --
144 abs(-42) =42
145 -- End --
146 
147 
148 Mix of type definitions and function wrapping.
149 
150 -- Testcase --
151 {%
152 import * as ffi from 'ffi';
153 
154 ffi.cdef('typedef long mylong;');
155 let libz = ffi.dlopen('z', false, 'const char *zlibVersion(void);');
156 let ver = ffi.string(libz.zlibVersion());
157 print('zlibVersion with prior cdef = ', replace(ver, /^([0-9]+)\..+$/, '$1.*'), '\n');
158 -- End --
159 
160 -- Expect stdout --
161 zlibVersion with prior cdef = 1.*
162 -- End --
163 
164 
165 Cdefs with struct (types registered).
166 
167 -- Testcase --
168 {%
169 import * as ffi from 'ffi';
170 
171 // Structs are registered as types
172 let libc = ffi.dlopen(null, false, 'typedef struct { int x; int y; } point_t;');
173 let Point = ffi.ctype('point_t');
174 let p = ffi.cast('point_t*', ffi.cast('void*', 0));
175 print('struct type registered\n');
176 -- End --
177 
178 -- Expect stdout --
179 struct type registered
180 -- End --
181 
182 
183 Direct property access returns callable function.
184 
185 -- Testcase --
186 {%
187 import * as ffi from 'ffi';
188 
189 let libz = ffi.dlopen('z', false, 'const char *zlibVersion(void);');
190 let fn = libz.zlibVersion;
191 let ver = ffi.string(fn());
192 print('Direct property access returns callable function\n');
193 -- End --
194 
195 -- Expect stdout --
196 Direct property access returns callable function
197 -- End --
198 
199 
200 Repeated dlopen with cdefs adds to same global library.
201 
202 -- Testcase --
203 {%
204 import * as ffi from 'ffi';
205 
206 let libc1 = ffi.dlopen(null, false, 'int getpid(void);');
207 let libc2 = ffi.dlopen(null, false, 'int getppid(void);');
208 let pid = libc1.getpid();
209 let ppid = libc2.getppid();
210 print('global lib cdefs accumulate\n');
211 -- End --
212 
213 -- Expect stdout --
214 global lib cdefs accumulate
215 -- End --
216 
217 
218 Function with const char* return and void parameter.
219 
220 -- Testcase --
221 {%
222 import * as ffi from 'ffi';
223 
224 let libz = ffi.dlopen('z', false, 'const char *zlibVersion(void);');
225 let ver_ptr = libz.zlibVersion();
226 let ver_str = ffi.string(ver_ptr);
227 print('ver = ', replace(ver_str, /^([0-9]+)\..+$/, '$1.*'), '\n');
228 -- End --
229 
230 -- Expect stdout --
231 ver = 1.*
232 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt