fiddle 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/ext/fiddle/fiddle.c CHANGED
@@ -164,137 +164,193 @@ Init_fiddle(void)
164
164
  */
165
165
  rb_eFiddleDLError = rb_define_class_under(mFiddle, "DLError", rb_eFiddleError);
166
166
 
167
- /* Document-const: TYPE_VOID
167
+ VALUE mFiddleTypes = rb_define_module_under(mFiddle, "Types");
168
+
169
+ /* Document-const: Fiddle::Types::VOID
168
170
  *
169
171
  * C type - void
170
172
  */
171
- rb_define_const(mFiddle, "TYPE_VOID", INT2NUM(TYPE_VOID));
173
+ rb_define_const(mFiddleTypes, "VOID", INT2NUM(TYPE_VOID));
172
174
 
173
- /* Document-const: TYPE_VOIDP
175
+ /* Document-const: Fiddle::Types::VOIDP
174
176
  *
175
177
  * C type - void*
176
178
  */
177
- rb_define_const(mFiddle, "TYPE_VOIDP", INT2NUM(TYPE_VOIDP));
179
+ rb_define_const(mFiddleTypes, "VOIDP", INT2NUM(TYPE_VOIDP));
178
180
 
179
- /* Document-const: TYPE_CHAR
181
+ /* Document-const: Fiddle::Types::CHAR
180
182
  *
181
183
  * C type - char
182
184
  */
183
- rb_define_const(mFiddle, "TYPE_CHAR", INT2NUM(TYPE_CHAR));
185
+ rb_define_const(mFiddleTypes, "CHAR", INT2NUM(TYPE_CHAR));
184
186
 
185
- /* Document-const: TYPE_SHORT
187
+ /* Document-const: Fiddle::Types::UCHAR
188
+ *
189
+ * C type - unsigned char
190
+ */
191
+ rb_define_const(mFiddleTypes, "UCHAR", INT2NUM(TYPE_UCHAR));
192
+
193
+ /* Document-const: Fiddle::Types::SHORT
186
194
  *
187
195
  * C type - short
188
196
  */
189
- rb_define_const(mFiddle, "TYPE_SHORT", INT2NUM(TYPE_SHORT));
197
+ rb_define_const(mFiddleTypes, "SHORT", INT2NUM(TYPE_SHORT));
190
198
 
191
- /* Document-const: TYPE_INT
199
+ /* Document-const: Fiddle::Types::USHORT
200
+ *
201
+ * C type - unsigned short
202
+ */
203
+ rb_define_const(mFiddleTypes, "USHORT", INT2NUM(TYPE_USHORT));
204
+
205
+ /* Document-const: Fiddle::Types::INT
192
206
  *
193
207
  * C type - int
194
208
  */
195
- rb_define_const(mFiddle, "TYPE_INT", INT2NUM(TYPE_INT));
209
+ rb_define_const(mFiddleTypes, "INT", INT2NUM(TYPE_INT));
210
+
211
+ /* Document-const: Fiddle::Types::UINT
212
+ *
213
+ * C type - unsigned int
214
+ */
215
+ rb_define_const(mFiddleTypes, "UINT", INT2NUM(TYPE_UINT));
216
+
217
+ /* Document-const: Fiddle::Types::LONG
218
+ *
219
+ * C type - long
220
+ */
221
+ rb_define_const(mFiddleTypes, "LONG", INT2NUM(TYPE_LONG));
196
222
 
197
- /* Document-const: TYPE_LONG
223
+ /* Document-const: Fiddle::Types::ULONG
198
224
  *
199
225
  * C type - long
200
226
  */
201
- rb_define_const(mFiddle, "TYPE_LONG", INT2NUM(TYPE_LONG));
227
+ rb_define_const(mFiddleTypes, "ULONG", INT2NUM(TYPE_ULONG));
202
228
 
203
229
  #if HAVE_LONG_LONG
204
- /* Document-const: TYPE_LONG_LONG
230
+ /* Document-const: Fiddle::Types::LONG_LONG
231
+ *
232
+ * C type - long long
233
+ */
234
+ rb_define_const(mFiddleTypes, "LONG_LONG", INT2NUM(TYPE_LONG_LONG));
235
+
236
+ /* Document-const: Fiddle::Types::ULONG_LONG
205
237
  *
206
238
  * C type - long long
207
239
  */
208
- rb_define_const(mFiddle, "TYPE_LONG_LONG", INT2NUM(TYPE_LONG_LONG));
240
+ rb_define_const(mFiddleTypes, "ULONG_LONG", INT2NUM(TYPE_ULONG_LONG));
209
241
  #endif
210
242
 
211
243
  #ifdef TYPE_INT8_T
212
- /* Document-const: TYPE_INT8_T
244
+ /* Document-const: Fiddle::Types::INT8_T
213
245
  *
214
246
  * C type - int8_t
215
247
  */
216
- rb_define_const(mFiddle, "TYPE_INT8_T", INT2NUM(TYPE_INT8_T));
248
+ rb_define_const(mFiddleTypes, "INT8_T", INT2NUM(TYPE_INT8_T));
249
+
250
+ /* Document-const: Fiddle::Types::UINT8_T
251
+ *
252
+ * C type - uint8_t
253
+ */
254
+ rb_define_const(mFiddleTypes, "UINT8_T", INT2NUM(TYPE_UINT8_T));
217
255
  #endif
218
256
 
219
257
  #ifdef TYPE_INT16_T
220
- /* Document-const: TYPE_INT16_T
258
+ /* Document-const: Fiddle::Types::INT16_T
221
259
  *
222
260
  * C type - int16_t
223
261
  */
224
- rb_define_const(mFiddle, "TYPE_INT16_T", INT2NUM(TYPE_INT16_T));
262
+ rb_define_const(mFiddleTypes, "INT16_T", INT2NUM(TYPE_INT16_T));
263
+
264
+ /* Document-const: Fiddle::Types::UINT16_T
265
+ *
266
+ * C type - uint16_t
267
+ */
268
+ rb_define_const(mFiddleTypes, "UINT16_T", INT2NUM(TYPE_UINT16_T));
225
269
  #endif
226
270
 
227
271
  #ifdef TYPE_INT32_T
228
- /* Document-const: TYPE_INT32_T
272
+ /* Document-const: Fiddle::Types::INT32_T
229
273
  *
230
274
  * C type - int32_t
231
275
  */
232
- rb_define_const(mFiddle, "TYPE_INT32_T", INT2NUM(TYPE_INT32_T));
276
+ rb_define_const(mFiddleTypes, "INT32_T", INT2NUM(TYPE_INT32_T));
277
+
278
+ /* Document-const: Fiddle::Types::UINT32_T
279
+ *
280
+ * C type - uint32_t
281
+ */
282
+ rb_define_const(mFiddleTypes, "UINT32_T", INT2NUM(TYPE_UINT32_T));
233
283
  #endif
234
284
 
235
285
  #ifdef TYPE_INT64_T
236
- /* Document-const: TYPE_INT64_T
286
+ /* Document-const: Fiddle::Types::INT64_T
237
287
  *
238
288
  * C type - int64_t
239
289
  */
240
- rb_define_const(mFiddle, "TYPE_INT64_T", INT2NUM(TYPE_INT64_T));
290
+ rb_define_const(mFiddleTypes, "INT64_T", INT2NUM(TYPE_INT64_T));
291
+
292
+ /* Document-const: Fiddle::Types::UINT64_T
293
+ *
294
+ * C type - uint64_t
295
+ */
296
+ rb_define_const(mFiddleTypes, "UINT64_T", INT2NUM(TYPE_UINT64_T));
241
297
  #endif
242
298
 
243
- /* Document-const: TYPE_FLOAT
299
+ /* Document-const: Fiddle::Types::FLOAT
244
300
  *
245
301
  * C type - float
246
302
  */
247
- rb_define_const(mFiddle, "TYPE_FLOAT", INT2NUM(TYPE_FLOAT));
303
+ rb_define_const(mFiddleTypes, "FLOAT", INT2NUM(TYPE_FLOAT));
248
304
 
249
- /* Document-const: TYPE_DOUBLE
305
+ /* Document-const: Fiddle::Types::DOUBLE
250
306
  *
251
307
  * C type - double
252
308
  */
253
- rb_define_const(mFiddle, "TYPE_DOUBLE", INT2NUM(TYPE_DOUBLE));
309
+ rb_define_const(mFiddleTypes, "DOUBLE", INT2NUM(TYPE_DOUBLE));
254
310
 
255
311
  #ifdef HAVE_FFI_PREP_CIF_VAR
256
- /* Document-const: TYPE_VARIADIC
312
+ /* Document-const: Fiddle::Types::VARIADIC
257
313
  *
258
314
  * C type - ...
259
315
  */
260
- rb_define_const(mFiddle, "TYPE_VARIADIC", INT2NUM(TYPE_VARIADIC));
316
+ rb_define_const(mFiddleTypes, "VARIADIC", INT2NUM(TYPE_VARIADIC));
261
317
  #endif
262
318
 
263
- /* Document-const: TYPE_CONST_STRING
319
+ /* Document-const: Fiddle::Types::CONST_STRING
264
320
  *
265
321
  * C type - const char* ('\0' terminated const char*)
266
322
  */
267
- rb_define_const(mFiddle, "TYPE_CONST_STRING", INT2NUM(TYPE_CONST_STRING));
323
+ rb_define_const(mFiddleTypes, "CONST_STRING", INT2NUM(TYPE_CONST_STRING));
268
324
 
269
- /* Document-const: TYPE_SIZE_T
325
+ /* Document-const: Fiddle::Types::SIZE_T
270
326
  *
271
327
  * C type - size_t
272
328
  */
273
- rb_define_const(mFiddle, "TYPE_SIZE_T", INT2NUM(TYPE_SIZE_T));
329
+ rb_define_const(mFiddleTypes, "SIZE_T", INT2NUM(TYPE_SIZE_T));
274
330
 
275
- /* Document-const: TYPE_SSIZE_T
331
+ /* Document-const: Fiddle::Types::SSIZE_T
276
332
  *
277
333
  * C type - ssize_t
278
334
  */
279
- rb_define_const(mFiddle, "TYPE_SSIZE_T", INT2NUM(TYPE_SSIZE_T));
335
+ rb_define_const(mFiddleTypes, "SSIZE_T", INT2NUM(TYPE_SSIZE_T));
280
336
 
281
- /* Document-const: TYPE_PTRDIFF_T
337
+ /* Document-const: Fiddle::Types::PTRDIFF_T
282
338
  *
283
339
  * C type - ptrdiff_t
284
340
  */
285
- rb_define_const(mFiddle, "TYPE_PTRDIFF_T", INT2NUM(TYPE_PTRDIFF_T));
341
+ rb_define_const(mFiddleTypes, "PTRDIFF_T", INT2NUM(TYPE_PTRDIFF_T));
286
342
 
287
- /* Document-const: TYPE_INTPTR_T
343
+ /* Document-const: Fiddle::Types::INTPTR_T
288
344
  *
289
345
  * C type - intptr_t
290
346
  */
291
- rb_define_const(mFiddle, "TYPE_INTPTR_T", INT2NUM(TYPE_INTPTR_T));
347
+ rb_define_const(mFiddleTypes, "INTPTR_T", INT2NUM(TYPE_INTPTR_T));
292
348
 
293
- /* Document-const: TYPE_UINTPTR_T
349
+ /* Document-const: Fiddle::Types::UINTPTR_T
294
350
  *
295
351
  * C type - uintptr_t
296
352
  */
297
- rb_define_const(mFiddle, "TYPE_UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
353
+ rb_define_const(mFiddleTypes, "UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
298
354
 
299
355
  /* Document-const: ALIGN_VOIDP
300
356
  *
@@ -422,30 +478,60 @@ Init_fiddle(void)
422
478
  */
423
479
  rb_define_const(mFiddle, "SIZEOF_CHAR", INT2NUM(sizeof(char)));
424
480
 
481
+ /* Document-const: SIZEOF_UCHAR
482
+ *
483
+ * size of a unsigned char
484
+ */
485
+ rb_define_const(mFiddle, "SIZEOF_UCHAR", INT2NUM(sizeof(unsigned char)));
486
+
425
487
  /* Document-const: SIZEOF_SHORT
426
488
  *
427
489
  * size of a short
428
490
  */
429
491
  rb_define_const(mFiddle, "SIZEOF_SHORT", INT2NUM(sizeof(short)));
430
492
 
493
+ /* Document-const: SIZEOF_USHORT
494
+ *
495
+ * size of a unsigned short
496
+ */
497
+ rb_define_const(mFiddle, "SIZEOF_USHORT", INT2NUM(sizeof(unsigned short)));
498
+
431
499
  /* Document-const: SIZEOF_INT
432
500
  *
433
501
  * size of an int
434
502
  */
435
503
  rb_define_const(mFiddle, "SIZEOF_INT", INT2NUM(sizeof(int)));
436
504
 
505
+ /* Document-const: SIZEOF_UINT
506
+ *
507
+ * size of an unsigned int
508
+ */
509
+ rb_define_const(mFiddle, "SIZEOF_UINT", INT2NUM(sizeof(unsigned int)));
510
+
437
511
  /* Document-const: SIZEOF_LONG
438
512
  *
439
513
  * size of a long
440
514
  */
441
515
  rb_define_const(mFiddle, "SIZEOF_LONG", INT2NUM(sizeof(long)));
442
516
 
517
+ /* Document-const: SIZEOF_ULONG
518
+ *
519
+ * size of a unsigned long
520
+ */
521
+ rb_define_const(mFiddle, "SIZEOF_ULONG", INT2NUM(sizeof(unsigned long)));
522
+
443
523
  #if HAVE_LONG_LONG
444
524
  /* Document-const: SIZEOF_LONG_LONG
445
525
  *
446
526
  * size of a long long
447
527
  */
448
528
  rb_define_const(mFiddle, "SIZEOF_LONG_LONG", INT2NUM(sizeof(LONG_LONG)));
529
+
530
+ /* Document-const: SIZEOF_ULONG_LONG
531
+ *
532
+ * size of a unsigned long long
533
+ */
534
+ rb_define_const(mFiddle, "SIZEOF_ULONG_LONG", INT2NUM(sizeof(unsigned LONG_LONG)));
449
535
  #endif
450
536
 
451
537
  /* Document-const: SIZEOF_INT8_T
@@ -454,24 +540,48 @@ Init_fiddle(void)
454
540
  */
455
541
  rb_define_const(mFiddle, "SIZEOF_INT8_T", INT2NUM(sizeof(int8_t)));
456
542
 
543
+ /* Document-const: SIZEOF_UINT8_T
544
+ *
545
+ * size of a uint8_t
546
+ */
547
+ rb_define_const(mFiddle, "SIZEOF_UINT8_T", INT2NUM(sizeof(uint8_t)));
548
+
457
549
  /* Document-const: SIZEOF_INT16_T
458
550
  *
459
551
  * size of a int16_t
460
552
  */
461
553
  rb_define_const(mFiddle, "SIZEOF_INT16_T", INT2NUM(sizeof(int16_t)));
462
554
 
555
+ /* Document-const: SIZEOF_UINT16_T
556
+ *
557
+ * size of a uint16_t
558
+ */
559
+ rb_define_const(mFiddle, "SIZEOF_UINT16_T", INT2NUM(sizeof(uint16_t)));
560
+
463
561
  /* Document-const: SIZEOF_INT32_T
464
562
  *
465
563
  * size of a int32_t
466
564
  */
467
565
  rb_define_const(mFiddle, "SIZEOF_INT32_T", INT2NUM(sizeof(int32_t)));
468
566
 
567
+ /* Document-const: SIZEOF_UINT32_T
568
+ *
569
+ * size of a uint32_t
570
+ */
571
+ rb_define_const(mFiddle, "SIZEOF_UINT32_T", INT2NUM(sizeof(uint32_t)));
572
+
469
573
  /* Document-const: SIZEOF_INT64_T
470
574
  *
471
575
  * size of a int64_t
472
576
  */
473
577
  rb_define_const(mFiddle, "SIZEOF_INT64_T", INT2NUM(sizeof(int64_t)));
474
578
 
579
+ /* Document-const: SIZEOF_UINT64_T
580
+ *
581
+ * size of a uint64_t
582
+ */
583
+ rb_define_const(mFiddle, "SIZEOF_UINT64_T", INT2NUM(sizeof(uint64_t)));
584
+
475
585
  /* Document-const: SIZEOF_FLOAT
476
586
  *
477
587
  * size of a float
@@ -540,6 +650,30 @@ Init_fiddle(void)
540
650
  rb_define_module_function(mFiddle, "realloc", rb_fiddle_realloc, 2);
541
651
  rb_define_module_function(mFiddle, "free", rb_fiddle_free, 1);
542
652
 
653
+ /* Document-const: Qtrue
654
+ *
655
+ * The value of Qtrue
656
+ */
657
+ rb_define_const(mFiddle, "Qtrue", INT2NUM(Qtrue));
658
+
659
+ /* Document-const: Qfalse
660
+ *
661
+ * The value of Qfalse
662
+ */
663
+ rb_define_const(mFiddle, "Qfalse", INT2NUM(Qfalse));
664
+
665
+ /* Document-const: Qnil
666
+ *
667
+ * The value of Qnil
668
+ */
669
+ rb_define_const(mFiddle, "Qnil", INT2NUM(Qnil));
670
+
671
+ /* Document-const: Qundef
672
+ *
673
+ * The value of Qundef
674
+ */
675
+ rb_define_const(mFiddle, "Qundef", INT2NUM(Qundef));
676
+
543
677
  Init_fiddle_function();
544
678
  Init_fiddle_closure();
545
679
  Init_fiddle_handle();
data/ext/fiddle/fiddle.h CHANGED
@@ -111,11 +111,16 @@
111
111
  #define TYPE_VOID 0
112
112
  #define TYPE_VOIDP 1
113
113
  #define TYPE_CHAR 2
114
+ #define TYPE_UCHAR -TYPE_CHAR
114
115
  #define TYPE_SHORT 3
116
+ #define TYPE_USHORT -TYPE_SHORT
115
117
  #define TYPE_INT 4
118
+ #define TYPE_UINT -TYPE_INT
116
119
  #define TYPE_LONG 5
120
+ #define TYPE_ULONG -TYPE_LONG
117
121
  #if HAVE_LONG_LONG
118
122
  #define TYPE_LONG_LONG 6
123
+ #define TYPE_ULONG_LONG -TYPE_LONG_LONG
119
124
  #endif
120
125
  #define TYPE_FLOAT 7
121
126
  #define TYPE_DOUBLE 8
@@ -123,11 +128,18 @@
123
128
  #define TYPE_CONST_STRING 10
124
129
 
125
130
  #define TYPE_INT8_T TYPE_CHAR
131
+ #define TYPE_UINT8_T -TYPE_INT8_T
132
+
126
133
  #if SIZEOF_SHORT == 2
127
134
  # define TYPE_INT16_T TYPE_SHORT
128
135
  #elif SIZEOF_INT == 2
129
136
  # define TYPE_INT16_T TYPE_INT
130
137
  #endif
138
+
139
+ #ifdef TYPE_INT16_T
140
+ # define TYPE_UINT16_T -TYPE_INT16_T
141
+ #endif
142
+
131
143
  #if SIZEOF_SHORT == 4
132
144
  # define TYPE_INT32_T TYPE_SHORT
133
145
  #elif SIZEOF_INT == 4
@@ -135,6 +147,11 @@
135
147
  #elif SIZEOF_LONG == 4
136
148
  # define TYPE_INT32_T TYPE_LONG
137
149
  #endif
150
+
151
+ #ifdef TYPE_INT32_T
152
+ #define TYPE_UINT32_T -TYPE_INT32_T
153
+ #endif
154
+
138
155
  #if SIZEOF_INT == 8
139
156
  # define TYPE_INT64_T TYPE_INT
140
157
  #elif SIZEOF_LONG == 8
@@ -143,6 +160,10 @@
143
160
  # define TYPE_INT64_T TYPE_LONG_LONG
144
161
  #endif
145
162
 
163
+ #ifdef TYPE_INT64_T
164
+ #define TYPE_UINT64_T -TYPE_INT64_T
165
+ #endif
166
+
146
167
  #ifndef TYPE_SSIZE_T
147
168
  # if SIZEOF_SIZE_T == SIZEOF_INT
148
169
  # define TYPE_SSIZE_T TYPE_INT
data/ext/fiddle/handle.c CHANGED
@@ -321,8 +321,10 @@ rb_fiddle_handle_s_sym(VALUE self, VALUE sym)
321
321
  return fiddle_handle_sym(RTLD_NEXT, sym);
322
322
  }
323
323
 
324
- static VALUE
325
- fiddle_handle_sym(void *handle, VALUE symbol)
324
+ typedef void (*fiddle_void_func)(void);
325
+
326
+ static fiddle_void_func
327
+ fiddle_handle_find_func(void *handle, VALUE symbol)
326
328
  {
327
329
  #if defined(HAVE_DLERROR)
328
330
  const char *err;
@@ -330,13 +332,13 @@ fiddle_handle_sym(void *handle, VALUE symbol)
330
332
  #else
331
333
  # define CHECK_DLERROR
332
334
  #endif
333
- void (*func)();
335
+ fiddle_void_func func;
334
336
  const char *name = StringValueCStr(symbol);
335
337
 
336
338
  #ifdef HAVE_DLERROR
337
339
  dlerror();
338
340
  #endif
339
- func = (void (*)())(VALUE)dlsym(handle, name);
341
+ func = (fiddle_void_func)(VALUE)dlsym(handle, name);
340
342
  CHECK_DLERROR;
341
343
  #if defined(FUNC_STDCALL)
342
344
  if( !func ){
@@ -379,6 +381,53 @@ fiddle_handle_sym(void *handle, VALUE symbol)
379
381
  xfree(name_n);
380
382
  }
381
383
  #endif
384
+
385
+ return func;
386
+ }
387
+
388
+ static VALUE
389
+ rb_fiddle_handle_s_sym_defined(VALUE self, VALUE sym)
390
+ {
391
+ fiddle_void_func func;
392
+
393
+ func = fiddle_handle_find_func(RTLD_NEXT, sym);
394
+
395
+ if( func ) {
396
+ return PTR2NUM(func);
397
+ }
398
+ else {
399
+ return Qnil;
400
+ }
401
+ }
402
+
403
+ static VALUE
404
+ rb_fiddle_handle_sym_defined(VALUE self, VALUE sym)
405
+ {
406
+ struct dl_handle *fiddle_handle;
407
+ fiddle_void_func func;
408
+
409
+ TypedData_Get_Struct(self, struct dl_handle, &fiddle_handle_data_type, fiddle_handle);
410
+ if( ! fiddle_handle->open ){
411
+ rb_raise(rb_eFiddleDLError, "closed handle");
412
+ }
413
+
414
+ func = fiddle_handle_find_func(fiddle_handle->ptr, sym);
415
+
416
+ if( func ) {
417
+ return PTR2NUM(func);
418
+ }
419
+ else {
420
+ return Qnil;
421
+ }
422
+ }
423
+
424
+ static VALUE
425
+ fiddle_handle_sym(void *handle, VALUE symbol)
426
+ {
427
+ fiddle_void_func func;
428
+
429
+ func = fiddle_handle_find_func(handle, symbol);
430
+
382
431
  if( !func ){
383
432
  rb_raise(rb_eFiddleDLError, "unknown symbol \"%"PRIsVALUE"\"", symbol);
384
433
  }
@@ -468,6 +517,7 @@ Init_fiddle_handle(void)
468
517
  rb_cHandle = rb_define_class_under(mFiddle, "Handle", rb_cObject);
469
518
  rb_define_alloc_func(rb_cHandle, rb_fiddle_handle_s_allocate);
470
519
  rb_define_singleton_method(rb_cHandle, "sym", rb_fiddle_handle_s_sym, 1);
520
+ rb_define_singleton_method(rb_cHandle, "sym_defined?", rb_fiddle_handle_s_sym_defined, 1);
471
521
  rb_define_singleton_method(rb_cHandle, "[]", rb_fiddle_handle_s_sym, 1);
472
522
 
473
523
  /* Document-const: NEXT
@@ -526,6 +576,7 @@ Init_fiddle_handle(void)
526
576
  rb_define_method(rb_cHandle, "close", rb_fiddle_handle_close, 0);
527
577
  rb_define_method(rb_cHandle, "sym", rb_fiddle_handle_sym, 1);
528
578
  rb_define_method(rb_cHandle, "[]", rb_fiddle_handle_sym, 1);
579
+ rb_define_method(rb_cHandle, "sym_defined?", rb_fiddle_handle_sym_defined, 1);
529
580
  rb_define_method(rb_cHandle, "file_name", rb_fiddle_handle_file_name, 0);
530
581
  rb_define_method(rb_cHandle, "disable_close", rb_fiddle_handle_disable_close, 0);
531
582
  rb_define_method(rb_cHandle, "enable_close", rb_fiddle_handle_enable_close, 0);
data/fiddle.gemspec CHANGED
@@ -20,15 +20,12 @@ Gem::Specification.new do |spec|
20
20
  "LICENSE.txt",
21
21
  "README.md",
22
22
  "Rakefile",
23
- "bin/downloader.rb",
24
- "bin/extlibs.rb",
25
23
  "ext/fiddle/closure.c",
26
24
  "ext/fiddle/closure.h",
27
25
  "ext/fiddle/conversions.c",
28
26
  "ext/fiddle/conversions.h",
29
27
  "ext/fiddle/depend",
30
28
  "ext/fiddle/extconf.rb",
31
- "ext/fiddle/extlibs",
32
29
  "ext/fiddle/fiddle.c",
33
30
  "ext/fiddle/fiddle.h",
34
31
  "ext/fiddle/function.c",
@@ -1,6 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
  module Fiddle
3
3
  class Closure
4
+ class << self
5
+ # Create a new closure. If a block is given, the created closure
6
+ # is automatically freed after the given block is executed.
7
+ #
8
+ # The all given arguments are passed to Fiddle::Closure.new. So
9
+ # using this method without block equals to Fiddle::Closure.new.
10
+ #
11
+ # == Example
12
+ #
13
+ # Fiddle::Closure.create(TYPE_INT, [TYPE_INT]) do |closure|
14
+ # # closure is freed automatically when this block is finished.
15
+ # end
16
+ def create(*args)
17
+ if block_given?
18
+ closure = new(*args)
19
+ begin
20
+ yield(closure)
21
+ ensure
22
+ closure.free
23
+ end
24
+ else
25
+ new(*args)
26
+ end
27
+ end
28
+ end
4
29
 
5
30
  # the C type of the return of the FFI closure
6
31
  attr_reader :ctype
@@ -164,23 +164,23 @@ module Fiddle
164
164
  unless Fiddle.const_defined?(:TYPE_LONG_LONG)
165
165
  raise(RuntimeError, "unsupported type: #{ty}")
166
166
  end
167
- return -TYPE_LONG_LONG
167
+ return TYPE_ULONG_LONG
168
168
  when /\A(?:signed\s+)?long(?:\s+int\s+)?(?:\s+\w+)?\z/
169
169
  return TYPE_LONG
170
170
  when /\Aunsigned\s+long(?:\s+int\s+)?(?:\s+\w+)?\z/
171
- return -TYPE_LONG
171
+ return TYPE_ULONG
172
172
  when /\A(?:signed\s+)?int(?:\s+\w+)?\z/
173
173
  return TYPE_INT
174
174
  when /\A(?:unsigned\s+int|uint)(?:\s+\w+)?\z/
175
- return -TYPE_INT
175
+ return TYPE_UINT
176
176
  when /\A(?:signed\s+)?short(?:\s+int\s+)?(?:\s+\w+)?\z/
177
177
  return TYPE_SHORT
178
178
  when /\Aunsigned\s+short(?:\s+int\s+)?(?:\s+\w+)?\z/
179
- return -TYPE_SHORT
179
+ return TYPE_USHORT
180
180
  when /\A(?:signed\s+)?char(?:\s+\w+)?\z/
181
181
  return TYPE_CHAR
182
182
  when /\Aunsigned\s+char(?:\s+\w+)?\z/
183
- return -TYPE_CHAR
183
+ return TYPE_UCHAR
184
184
  when /\Aint8_t(?:\s+\w+)?\z/
185
185
  unless Fiddle.const_defined?(:TYPE_INT8_T)
186
186
  raise(RuntimeError, "unsupported type: #{ty}")
@@ -190,7 +190,7 @@ module Fiddle
190
190
  unless Fiddle.const_defined?(:TYPE_INT8_T)
191
191
  raise(RuntimeError, "unsupported type: #{ty}")
192
192
  end
193
- return -TYPE_INT8_T
193
+ return TYPE_UINT8_T
194
194
  when /\Aint16_t(?:\s+\w+)?\z/
195
195
  unless Fiddle.const_defined?(:TYPE_INT16_T)
196
196
  raise(RuntimeError, "unsupported type: #{ty}")
@@ -200,7 +200,7 @@ module Fiddle
200
200
  unless Fiddle.const_defined?(:TYPE_INT16_T)
201
201
  raise(RuntimeError, "unsupported type: #{ty}")
202
202
  end
203
- return -TYPE_INT16_T
203
+ return TYPE_UINT16_T
204
204
  when /\Aint32_t(?:\s+\w+)?\z/
205
205
  unless Fiddle.const_defined?(:TYPE_INT32_T)
206
206
  raise(RuntimeError, "unsupported type: #{ty}")
@@ -210,7 +210,7 @@ module Fiddle
210
210
  unless Fiddle.const_defined?(:TYPE_INT32_T)
211
211
  raise(RuntimeError, "unsupported type: #{ty}")
212
212
  end
213
- return -TYPE_INT32_T
213
+ return TYPE_UINT32_T
214
214
  when /\Aint64_t(?:\s+\w+)?\z/
215
215
  unless Fiddle.const_defined?(:TYPE_INT64_T)
216
216
  raise(RuntimeError, "unsupported type: #{ty}")
@@ -220,7 +220,7 @@ module Fiddle
220
220
  unless Fiddle.const_defined?(:TYPE_INT64_T)
221
221
  raise(RuntimeError, "unsupported type: #{ty}")
222
222
  end
223
- return -TYPE_INT64_T
223
+ return TYPE_UINT64_T
224
224
  when /\Afloat(?:\s+\w+)?\z/
225
225
  return TYPE_FLOAT
226
226
  when /\Adouble(?:\s+\w+)?\z/