fiddle 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47a26538269c86953175484808ab2a03de57e77437d780ccdb4522dad99a68da
4
- data.tar.gz: a97effcdad9ee1662f66a622344c8b4a51cf4b7c70c6334c559f0151c15aff04
3
+ metadata.gz: 17a63516f0bbb623b730ccaa32e98df18c1b3c447b99027b82125ab788363175
4
+ data.tar.gz: 6b834175e32765e9a13edfb4f32390b48d8914d5bbe8e0b50b215f409b4ee192
5
5
  SHA512:
6
- metadata.gz: d589e00013a957389e90f0ed2c7af551708011f4080713cdaff31c466bab17b71e9204660bb226562df2e04577c84bba7dc9ec5b595eb6ad0b7edc2ffee8c430
7
- data.tar.gz: '091e4325049304f5b2055ab3b9f31a70810e61bc312143f9989b3475227311617cdbe1fc574099cecb4d4d9a234c3104b744803476be4e741b6aa93fa69cf249'
6
+ metadata.gz: 0c0bf76f5288d9241daa1ec0f2dae597a33a428afb893d7cf274cfd6c247c99eb93280f4fe1883b2fbabeb844cae9e272df2209feaba69cd781b689f772fb3f6
7
+ data.tar.gz: '038a55b29aafc16824c2d0498be0e22851c0f5e97956ee961220cd606dcb55a6615cdaca8ec0481e1fb6e9a4d28ec544ac86e78cf640c0e0cb551e65266b0a7d'
data/ext/fiddle/closure.c CHANGED
@@ -1,4 +1,5 @@
1
1
  #include <fiddle.h>
2
+ #include <stdbool.h>
2
3
  #include <ruby/thread.h>
3
4
 
4
5
  int ruby_thread_has_gvl_p(void); /* from internal.h */
@@ -54,10 +55,13 @@ closure_memsize(const void * ptr)
54
55
  }
55
56
 
56
57
  const rb_data_type_t closure_data_type = {
57
- "fiddle/closure",
58
- {0, dealloc, closure_memsize,},
59
- 0, 0,
60
- RUBY_TYPED_FREE_IMMEDIATELY,
58
+ .wrap_struct_name = "fiddle/closure",
59
+ .function = {
60
+ .dmark = 0,
61
+ .dfree = dealloc,
62
+ .dsize = closure_memsize
63
+ },
64
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
61
65
  };
62
66
 
63
67
  struct callback_args {
@@ -136,6 +140,20 @@ with_gvl_callback(void *ptr)
136
140
  rb_ary_push(params,
137
141
  rb_str_new_cstr(*((const char **)(x->args[i]))));
138
142
  break;
143
+ case TYPE_BOOL:
144
+ if (sizeof(bool) == sizeof(char)) {
145
+ rb_ary_push(params, CBOOL2RBBOOL(*(unsigned char *)x->args[i]));
146
+ } else if (sizeof(bool) == sizeof(short)) {
147
+ rb_ary_push(params, CBOOL2RBBOOL(*(unsigned short *)x->args[i]));
148
+ } else if (sizeof(bool) == sizeof(int)) {
149
+ rb_ary_push(params, CBOOL2RBBOOL(*(unsigned int *)x->args[i]));
150
+ } else if (sizeof(bool) == sizeof(long)) {
151
+ rb_ary_push(params, CBOOL2RBBOOL(*(unsigned long *)x->args[i]));
152
+ } else {
153
+ rb_raise(rb_eNotImpError, "bool isn't supported: %u",
154
+ (unsigned int)sizeof(bool));
155
+ }
156
+ break;
139
157
  default:
140
158
  rb_raise(rb_eRuntimeError, "closure args: %d", type);
141
159
  }
@@ -185,6 +203,13 @@ with_gvl_callback(void *ptr)
185
203
  /* Dangerous. Callback must keep reference of the String. */
186
204
  *((const char **)(x->resp)) = StringValueCStr(ret);
187
205
  break;
206
+ case TYPE_BOOL:
207
+ if (sizeof(bool) == sizeof(long)) {
208
+ *(unsigned long *)x->resp = RB_TEST(ret);
209
+ } else {
210
+ *(ffi_arg *)x->resp = RB_TEST(ret);
211
+ }
212
+ break;
188
213
  default:
189
214
  rb_raise(rb_eRuntimeError, "closure retval: %d", type);
190
215
  }
@@ -1,3 +1,5 @@
1
+ #include <stdbool.h>
2
+
1
3
  #include <fiddle.h>
2
4
 
3
5
  VALUE
@@ -44,6 +46,7 @@ rb_fiddle_type_ensure(VALUE type)
44
46
  ID ptrdiff_t_id;
45
47
  ID intptr_t_id;
46
48
  ID uintptr_t_id;
49
+ ID bool_id;
47
50
  RUBY_CONST_ID(void_id, "void");
48
51
  RUBY_CONST_ID(voidp_id, "voidp");
49
52
  RUBY_CONST_ID(char_id, "char");
@@ -74,6 +77,7 @@ rb_fiddle_type_ensure(VALUE type)
74
77
  RUBY_CONST_ID(ptrdiff_t_id, "ptrdiff_t");
75
78
  RUBY_CONST_ID(intptr_t_id, "intptr_t");
76
79
  RUBY_CONST_ID(uintptr_t_id, "uintptr_t");
80
+ RUBY_CONST_ID(bool_id, "bool");
77
81
  if (type_id == void_id) {
78
82
  return INT2NUM(TYPE_VOID);
79
83
  }
@@ -144,6 +148,9 @@ rb_fiddle_type_ensure(VALUE type)
144
148
  else if (type_id == uintptr_t_id) {
145
149
  return INT2NUM(TYPE_UINTPTR_T);
146
150
  }
151
+ else if (type_id == bool_id) {
152
+ return INT2NUM(TYPE_BOOL);
153
+ }
147
154
  else {
148
155
  type = original_type;
149
156
  }
@@ -187,6 +194,20 @@ rb_fiddle_int_to_ffi_type(int type)
187
194
  return &ffi_type_double;
188
195
  case TYPE_CONST_STRING:
189
196
  return &ffi_type_pointer;
197
+ case TYPE_BOOL:
198
+ signed_p = 0;
199
+ if (sizeof(bool) == sizeof(char)) {
200
+ return rb_ffi_type_of(char);
201
+ } else if (sizeof(bool) == sizeof(short)) {
202
+ return rb_ffi_type_of(short);
203
+ } else if (sizeof(bool) == sizeof(int)) {
204
+ return rb_ffi_type_of(int);
205
+ } else if (sizeof(bool) == sizeof(long)) {
206
+ return rb_ffi_type_of(long);
207
+ } else {
208
+ rb_raise(rb_eNotImpError, "bool isn't supported: %u",
209
+ (unsigned int)sizeof(bool));
210
+ }
190
211
  default:
191
212
  rb_raise(rb_eRuntimeError, "unknown type %d", type);
192
213
  }
@@ -209,7 +230,11 @@ rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst)
209
230
  dst->pointer = NUM2PTR(rb_Integer(*src));
210
231
  break;
211
232
  case TYPE_CHAR:
212
- dst->schar = (signed char)NUM2INT(*src);
233
+ if (RB_TYPE_P(*src, RUBY_T_STRING) && RSTRING_LEN(*src) == 1) {
234
+ dst->schar = RSTRING_PTR(*src)[0];
235
+ } else {
236
+ dst->schar = (signed char)NUM2INT(*src);
237
+ }
213
238
  break;
214
239
  case TYPE_UCHAR:
215
240
  dst->uchar = (unsigned char)NUM2UINT(*src);
@@ -254,8 +279,23 @@ rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst)
254
279
  dst->pointer = rb_string_value_cstr(src);
255
280
  }
256
281
  break;
282
+ case TYPE_BOOL:
283
+ if (sizeof(bool) == sizeof(char)) {
284
+ dst->uchar = RB_TEST(*src);
285
+ } else if (sizeof(bool) == sizeof(short)) {
286
+ dst->ushort = RB_TEST(*src);
287
+ } else if (sizeof(bool) == sizeof(int)) {
288
+ dst->uint = RB_TEST(*src);
289
+ } else if (sizeof(bool) == sizeof(long)) {
290
+ dst->ulong = RB_TEST(*src);
291
+ } else {
292
+ rb_raise(rb_eNotImpError, "bool isn't supported: %u",
293
+ (unsigned int)sizeof(bool));
294
+ }
295
+ break;
257
296
  default:
258
297
  rb_raise(rb_eRuntimeError, "unknown type %d", type);
298
+ break;
259
299
  }
260
300
  }
261
301
 
@@ -314,6 +354,19 @@ rb_fiddle_generic_to_value(VALUE rettype, fiddle_generic retval)
314
354
  else {
315
355
  return Qnil;
316
356
  }
357
+ case TYPE_BOOL:
358
+ if (sizeof(bool) == sizeof(char)) {
359
+ return CBOOL2RBBOOL((unsigned char)retval.fffi_arg);
360
+ } else if (sizeof(bool) == sizeof(short)) {
361
+ return CBOOL2RBBOOL((unsigned short)retval.fffi_arg);
362
+ } else if (sizeof(bool) == sizeof(int)) {
363
+ return CBOOL2RBBOOL((unsigned int)retval.fffi_arg);
364
+ } else if (sizeof(bool) == sizeof(long)) {
365
+ return CBOOL2RBBOOL(retval.ulong);
366
+ } else {
367
+ rb_raise(rb_eNotImpError, "bool isn't supported: %u",
368
+ (unsigned int)sizeof(bool));
369
+ }
317
370
  default:
318
371
  rb_raise(rb_eRuntimeError, "unknown type %d", type);
319
372
  }
@@ -50,4 +50,6 @@ VALUE generic_to_value(VALUE rettype, fiddle_generic retval);
50
50
  # define NUM2PTR(x) ((void*)(NUM2ULL(x)))
51
51
  #endif
52
52
 
53
+ #define CBOOL2RBBOOL(cbool) ((cbool) ? RUBY_Qtrue : RUBY_Qfalse)
54
+
53
55
  #endif
@@ -63,6 +63,11 @@ unless bundle
63
63
  end
64
64
  if have_ffi_header && (have_library('ffi') || have_library('libffi'))
65
65
  have_libffi = true
66
+ checking_for("undefined FFI_GO_CLOSURES is used") do
67
+ if egrep_cpp(/warning: 'FFI_GO_CLOSURES' is not defined/, cpp_include(ffi_header), "2>&1")
68
+ $defs.push('-DFFI_GO_CLOSURES=0')
69
+ end
70
+ end
66
71
  end
67
72
  end
68
73
 
data/ext/fiddle/fiddle.c CHANGED
@@ -1,3 +1,5 @@
1
+ #include <stdbool.h>
2
+
1
3
  #include <fiddle.h>
2
4
 
3
5
  VALUE mFiddle;
@@ -58,18 +60,16 @@ rb_fiddle_free(VALUE self, VALUE addr)
58
60
  /*
59
61
  * call-seq: Fiddle.dlunwrap(addr)
60
62
  *
61
- * Returns the hexadecimal representation of a memory pointer address +addr+
63
+ * Returns the Ruby object stored at the memory address +addr+
62
64
  *
63
65
  * Example:
64
66
  *
65
- * lib = Fiddle.dlopen('/lib64/libc-2.15.so')
66
- * => #<Fiddle::Handle:0x00000001342460>
67
- *
68
- * lib['strcpy'].to_s(16)
69
- * => "7f59de6dd240"
70
- *
71
- * Fiddle.dlunwrap(Fiddle.dlwrap(lib['strcpy'].to_s(16)))
72
- * => "7f59de6dd240"
67
+ * x = Object.new
68
+ * # => #<Object:0x0000000107c7d870>
69
+ * Fiddle.dlwrap(x)
70
+ * # => 4425504880
71
+ * Fiddle.dlunwrap(_)
72
+ * # => #<Object:0x0000000107c7d870>
73
73
  */
74
74
  VALUE
75
75
  rb_fiddle_ptr2value(VALUE self, VALUE addr)
@@ -80,15 +80,22 @@ rb_fiddle_ptr2value(VALUE self, VALUE addr)
80
80
  /*
81
81
  * call-seq: Fiddle.dlwrap(val)
82
82
  *
83
- * Returns a memory pointer of a function's hexadecimal address location +val+
83
+ * Returns the memory address of the Ruby object stored at +val+
84
84
  *
85
85
  * Example:
86
86
  *
87
- * lib = Fiddle.dlopen('/lib64/libc-2.15.so')
88
- * => #<Fiddle::Handle:0x00000001342460>
87
+ * x = Object.new
88
+ * # => #<Object:0x0000000107c7d870>
89
+ * Fiddle.dlwrap(x)
90
+ * # => 4425504880
91
+ *
92
+ * In the case +val+ is not a heap allocated object, this method will return
93
+ * the tagged pointer value.
89
94
  *
90
- * Fiddle.dlwrap(lib['strcpy'].to_s(16))
91
- * => 25522520
95
+ * Example:
96
+ *
97
+ * Fiddle.dlwrap(123)
98
+ * # => 247
92
99
  */
93
100
  static VALUE
94
101
  rb_fiddle_value2ptr(VALUE self, VALUE val)
@@ -352,6 +359,12 @@ Init_fiddle(void)
352
359
  */
353
360
  rb_define_const(mFiddleTypes, "UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
354
361
 
362
+ /* Document-const: Fiddle::Types::BOOL
363
+ *
364
+ * C type - bool
365
+ */
366
+ rb_define_const(mFiddleTypes, "BOOL" , INT2NUM(TYPE_BOOL));
367
+
355
368
  /* Document-const: ALIGN_VOIDP
356
369
  *
357
370
  * The alignment size of a void*
@@ -456,6 +469,12 @@ Init_fiddle(void)
456
469
  */
457
470
  rb_define_const(mFiddle, "ALIGN_UINTPTR_T", INT2NUM(ALIGN_OF(uintptr_t)));
458
471
 
472
+ /* Document-const: ALIGN_BOOL
473
+ *
474
+ * The alignment size of a bool
475
+ */
476
+ rb_define_const(mFiddle, "ALIGN_BOOL", INT2NUM(ALIGN_OF(bool)));
477
+
459
478
  /* Document-const: WINDOWS
460
479
  *
461
480
  * Returns a boolean regarding whether the host is WIN32
@@ -630,6 +649,12 @@ Init_fiddle(void)
630
649
  */
631
650
  rb_define_const(mFiddle, "SIZEOF_CONST_STRING", INT2NUM(sizeof(const char*)));
632
651
 
652
+ /* Document-const: SIZEOF_BOOL
653
+ *
654
+ * size of a bool
655
+ */
656
+ rb_define_const(mFiddle, "SIZEOF_BOOL", INT2NUM(sizeof(bool)));
657
+
633
658
  /* Document-const: RUBY_FREE
634
659
  *
635
660
  * Address of the ruby_xfree() function
data/ext/fiddle/fiddle.h CHANGED
@@ -118,7 +118,7 @@
118
118
  #define TYPE_UINT -TYPE_INT
119
119
  #define TYPE_LONG 5
120
120
  #define TYPE_ULONG -TYPE_LONG
121
- #if HAVE_LONG_LONG
121
+ #ifdef HAVE_LONG_LONG
122
122
  #define TYPE_LONG_LONG 6
123
123
  #define TYPE_ULONG_LONG -TYPE_LONG_LONG
124
124
  #endif
@@ -126,6 +126,7 @@
126
126
  #define TYPE_DOUBLE 8
127
127
  #define TYPE_VARIADIC 9
128
128
  #define TYPE_CONST_STRING 10
129
+ #define TYPE_BOOL 11
129
130
 
130
131
  #define TYPE_INT8_T TYPE_CHAR
131
132
  #define TYPE_UINT8_T -TYPE_INT8_T
@@ -196,7 +197,20 @@
196
197
  #endif
197
198
  #define TYPE_UINTPTR_T (-TYPE_INTPTR_T)
198
199
 
199
- #define ALIGN_OF(type) offsetof(struct {char align_c; type align_x;}, align_x)
200
+ /* GCC releases before GCC 4.9 had a bug in _Alignof. See GCC bug 52023
201
+ <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52023>.
202
+ clang versions < 8.0.0 have the same bug. */
203
+ #if defined(HAVE__ALIGNOF)
204
+ # /* Autoconf detected availability of a sane `_Alignof()`. */
205
+ # define ALIGN_OF(type) RB_GNUC_EXTENSION(_Alignof(type))
206
+ #elif (!defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112 \
207
+ || (defined(__GNUC__) && __GNUC__ < 4 + (__GNUC_MINOR__ < 9) \
208
+ && !defined(__clang__)) \
209
+ || (defined(__clang__) && __clang_major__ < 8))
210
+ # define ALIGN_OF(type) offsetof(struct {char align_c; type align_x;}, align_x)
211
+ #else
212
+ # define ALIGN_OF(type) _Alignof(type)
213
+ #endif
200
214
 
201
215
  #define ALIGN_VOIDP ALIGN_OF(void*)
202
216
  #define ALIGN_CHAR ALIGN_OF(char)
@@ -53,8 +53,13 @@ function_memsize(const void *p)
53
53
  }
54
54
 
55
55
  const rb_data_type_t function_data_type = {
56
- "fiddle/function",
57
- {0, deallocate, function_memsize,},
56
+ .wrap_struct_name = "fiddle/function",
57
+ .function = {
58
+ .dmark = 0,
59
+ .dfree = deallocate,
60
+ .dsize = function_memsize
61
+ },
62
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
58
63
  };
59
64
 
60
65
  static VALUE
data/ext/fiddle/handle.c CHANGED
@@ -50,8 +50,13 @@ fiddle_handle_memsize(const void *ptr)
50
50
  }
51
51
 
52
52
  static const rb_data_type_t fiddle_handle_data_type = {
53
- "fiddle/handle",
54
- {0, fiddle_handle_free, fiddle_handle_memsize,},
53
+ .wrap_struct_name = "fiddle/handle",
54
+ .function = {
55
+ .dmark = 0,
56
+ .dfree = fiddle_handle_free,
57
+ .dsize = fiddle_handle_memsize
58
+ },
59
+ .flags = RUBY_TYPED_WB_PROTECTED,
55
60
  };
56
61
 
57
62
  /*
data/ext/fiddle/pointer.c CHANGED
@@ -88,8 +88,13 @@ fiddle_ptr_memsize(const void *ptr)
88
88
  }
89
89
 
90
90
  static const rb_data_type_t fiddle_ptr_data_type = {
91
- "fiddle/pointer",
92
- {fiddle_ptr_mark, fiddle_ptr_free, fiddle_ptr_memsize,},
91
+ .wrap_struct_name = "fiddle/pointer",
92
+ .function = {
93
+ .dmark = fiddle_ptr_mark,
94
+ .dfree = fiddle_ptr_free,
95
+ .dsize = fiddle_ptr_memsize,
96
+ },
97
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED
93
98
  };
94
99
 
95
100
  #ifdef HAVE_RUBY_MEMORY_VIEW_H
@@ -135,8 +140,8 @@ rb_fiddle_ptr_new2(VALUE klass, void *ptr, long size, freefunc_t func, VALUE wra
135
140
  data->free = func;
136
141
  data->freed = false;
137
142
  data->size = size;
138
- data->wrap[0] = wrap0;
139
- data->wrap[1] = wrap1;
143
+ RB_OBJ_WRITE(val, &data->wrap[0], wrap0);
144
+ RB_OBJ_WRITE(val, &data->wrap[1], wrap1);
140
145
 
141
146
  return val;
142
147
  }
@@ -235,8 +240,8 @@ rb_fiddle_ptr_initialize(int argc, VALUE argv[], VALUE self)
235
240
  /* Free previous memory. Use of inappropriate initialize may cause SEGV. */
236
241
  (*(data->free))(data->ptr);
237
242
  }
238
- data->wrap[0] = wrap;
239
- data->wrap[1] = funcwrap;
243
+ RB_OBJ_WRITE(self, &data->wrap[0], wrap);
244
+ RB_OBJ_WRITE(self, &data->wrap[1], funcwrap);
240
245
  data->ptr = p;
241
246
  data->size = s;
242
247
  data->free = f;
@@ -314,7 +319,7 @@ rb_fiddle_ptr_s_malloc(int argc, VALUE argv[], VALUE klass)
314
319
  }
315
320
 
316
321
  obj = rb_fiddle_ptr_malloc(klass, s,f);
317
- if (wrap) RPTR_DATA(obj)->wrap[1] = wrap;
322
+ if (wrap) RB_OBJ_WRITE(obj, &RPTR_DATA(obj)->wrap[1], wrap);
318
323
 
319
324
  if (rb_block_given_p()) {
320
325
  if (!f) {
@@ -795,10 +800,37 @@ rb_fiddle_ptr_s_to_ptr(VALUE self, VALUE val)
795
800
  if (num == val) wrap = 0;
796
801
  ptr = rb_fiddle_ptr_new(NUM2PTR(num), 0, NULL);
797
802
  }
798
- if (wrap) RPTR_DATA(ptr)->wrap[0] = wrap;
803
+ if (wrap) RB_OBJ_WRITE(ptr, &RPTR_DATA(ptr)->wrap[0], wrap);
799
804
  return ptr;
800
805
  }
801
806
 
807
+ /*
808
+ * call-seq:
809
+ * Fiddle::Pointer.read(address, len) => string
810
+ *
811
+ * Or read the memory at address +address+ with length +len+ and return a
812
+ * string with that memory
813
+ */
814
+
815
+ static VALUE
816
+ rb_fiddle_ptr_read_mem(VALUE klass, VALUE address, VALUE len)
817
+ {
818
+ return rb_str_new((char *)NUM2PTR(address), NUM2ULONG(len));
819
+ }
820
+
821
+ /*
822
+ * call-seq:
823
+ * Fiddle::Pointer.write(address, str)
824
+ *
825
+ * Write bytes in +str+ to the location pointed to by +address+.
826
+ */
827
+ static VALUE
828
+ rb_fiddle_ptr_write_mem(VALUE klass, VALUE addr, VALUE str)
829
+ {
830
+ memcpy(NUM2PTR(addr), StringValuePtr(str), RSTRING_LEN(str));
831
+ return str;
832
+ }
833
+
802
834
  void
803
835
  Init_fiddle_pointer(void)
804
836
  {
@@ -815,6 +847,8 @@ Init_fiddle_pointer(void)
815
847
  rb_define_singleton_method(rb_cPointer, "malloc", rb_fiddle_ptr_s_malloc, -1);
816
848
  rb_define_singleton_method(rb_cPointer, "to_ptr", rb_fiddle_ptr_s_to_ptr, 1);
817
849
  rb_define_singleton_method(rb_cPointer, "[]", rb_fiddle_ptr_s_to_ptr, 1);
850
+ rb_define_singleton_method(rb_cPointer, "read", rb_fiddle_ptr_read_mem, 2);
851
+ rb_define_singleton_method(rb_cPointer, "write", rb_fiddle_ptr_write_mem, 2);
818
852
  rb_define_method(rb_cPointer, "initialize", rb_fiddle_ptr_initialize, -1);
819
853
  rb_define_method(rb_cPointer, "free=", rb_fiddle_ptr_free_set, 1);
820
854
  rb_define_method(rb_cPointer, "free", rb_fiddle_ptr_free_get, 0);
@@ -23,7 +23,7 @@ until ARGV.empty?
23
23
  end
24
24
  end
25
25
 
26
- IO.foreach("#{srcdir}/configure.ac") do |line|
26
+ File.foreach("#{srcdir}/configure.ac") do |line|
27
27
  if /^AC_INIT\((.*)\)/ =~ line
28
28
  version = $1.split(/,\s*/)[1]
29
29
  version.gsub!(/\A\[|\]\z/, '')
@@ -38,11 +38,11 @@ conf['TARGET'] = /^x64/ =~ host ? "X86_WIN64" : "X86_WIN32"
38
38
  FileUtils.mkdir_p([builddir, "#{builddir}/include", "#{builddir}/src/x86"])
39
39
  FileUtils.cp("#{basedir}/fficonfig.h", ".", preserve: true)
40
40
 
41
- hdr = IO.binread("#{srcdir}/include/ffi.h.in")
41
+ hdr = File.binread("#{srcdir}/include/ffi.h.in")
42
42
  hdr.gsub!(/@(\w+)@/) {conf[$1] || $&}
43
43
  hdr.gsub!(/^(#if\s+)@\w+@/, '\10')
44
- IO.binwrite("#{builddir}/include/ffi.h", hdr)
44
+ File.binwrite("#{builddir}/include/ffi.h", hdr)
45
45
 
46
- mk = IO.binread("#{basedir}/libffi.mk.tmpl")
46
+ mk = File.binread("#{basedir}/libffi.mk.tmpl")
47
47
  mk.gsub!(/@(\w+)@/) {conf[$1] || $&}
48
- IO.binwrite("Makefile", mk)
48
+ File.binwrite("Makefile", mk)
@@ -165,18 +165,30 @@ module Fiddle
165
165
  raise(RuntimeError, "unsupported type: #{ty}")
166
166
  end
167
167
  return TYPE_ULONG_LONG
168
- when /\A(?:signed\s+)?long(?:\s+int\s+)?(?:\s+\w+)?\z/
169
- return TYPE_LONG
170
- when /\Aunsigned\s+long(?:\s+int\s+)?(?:\s+\w+)?\z/
168
+ when /\Aunsigned\s+long(?:\s+int\s+)?(?:\s+\w+)?\z/,
169
+ /\Aunsigned\s+int\s+long(?:\s+\w+)?\z/,
170
+ /\Along(?:\s+int)?\s+unsigned(?:\s+\w+)?\z/,
171
+ /\Aint\s+unsigned\s+long(?:\s+\w+)?\z/,
172
+ /\A(?:int\s+)?long\s+unsigned(?:\s+\w+)?\z/
171
173
  return TYPE_ULONG
174
+ when /\A(?:signed\s+)?long(?:\s+int\s+)?(?:\s+\w+)?\z/,
175
+ /\A(?:signed\s+)?int\s+long(?:\s+\w+)?\z/,
176
+ /\Along(?:\s+int)?\s+signed(?:\s+\w+)?\z/
177
+ return TYPE_LONG
178
+ when /\Aunsigned\s+short(?:\s+int\s+)?(?:\s+\w+)?\z/,
179
+ /\Aunsigned\s+int\s+short(?:\s+\w+)?\z/,
180
+ /\Ashort(?:\s+int)?\s+unsigned(?:\s+\w+)?\z/,
181
+ /\Aint\s+unsigned\s+short(?:\s+\w+)?\z/,
182
+ /\A(?:int\s+)?short\s+unsigned(?:\s+\w+)?\z/
183
+ return TYPE_USHORT
184
+ when /\A(?:signed\s+)?short(?:\s+int\s+)?(?:\s+\w+)?\z/,
185
+ /\A(?:signed\s+)?int\s+short(?:\s+\w+)?\z/,
186
+ /\Aint\s+(?:signed\s+)?short(?:\s+\w+)?\z/
187
+ return TYPE_SHORT
172
188
  when /\A(?:signed\s+)?int(?:\s+\w+)?\z/
173
189
  return TYPE_INT
174
190
  when /\A(?:unsigned\s+int|uint)(?:\s+\w+)?\z/
175
191
  return TYPE_UINT
176
- when /\A(?:signed\s+)?short(?:\s+int\s+)?(?:\s+\w+)?\z/
177
- return TYPE_SHORT
178
- when /\Aunsigned\s+short(?:\s+int\s+)?(?:\s+\w+)?\z/
179
- return TYPE_USHORT
180
192
  when /\A(?:signed\s+)?char(?:\s+\w+)?\z/
181
193
  return TYPE_CHAR
182
194
  when /\Aunsigned\s+char(?:\s+\w+)?\z/
@@ -235,6 +247,8 @@ module Fiddle
235
247
  return TYPE_INTPTR_T
236
248
  when /\Auintptr_t(?:\s+\w+)?\z/
237
249
  return TYPE_UINTPTR_T
250
+ when "bool"
251
+ return TYPE_BOOL
238
252
  when /\*/, /\[[\s\d]*\]/
239
253
  return TYPE_VOIDP
240
254
  when "..."
data/lib/fiddle/import.rb CHANGED
@@ -119,6 +119,8 @@ module Fiddle
119
119
  return SIZEOF_VOIDP
120
120
  when TYPE_CONST_STRING
121
121
  return SIZEOF_CONST_STRING
122
+ when TYPE_BOOL
123
+ return SIZEOF_BOOL
122
124
  else
123
125
  if defined?(TYPE_LONG_LONG) and
124
126
  ty == TYPE_LONG_LONG
data/lib/fiddle/pack.rb CHANGED
@@ -15,6 +15,7 @@ module Fiddle
15
15
  TYPE_USHORT => ALIGN_SHORT,
16
16
  TYPE_UINT => ALIGN_INT,
17
17
  TYPE_ULONG => ALIGN_LONG,
18
+ TYPE_BOOL => ALIGN_BOOL,
18
19
  }
19
20
 
20
21
  PACK_MAP = {
@@ -30,6 +31,16 @@ module Fiddle
30
31
  TYPE_UINT => "I!",
31
32
  TYPE_ULONG => "L!",
32
33
  }
34
+ case SIZEOF_BOOL
35
+ when SIZEOF_CHAR
36
+ PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_UCHAR]
37
+ when SIZEOF_SHORT
38
+ PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_USHORT]
39
+ when SIZEOF_INT
40
+ PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_UINT]
41
+ when SIZEOF_LONG
42
+ PACK_MAP[TYPE_BOOL] = PACK_MAP[TYPE_ULONG]
43
+ end
33
44
 
34
45
  SIZE_MAP = {
35
46
  TYPE_VOIDP => SIZEOF_VOIDP,
@@ -43,6 +54,7 @@ module Fiddle
43
54
  TYPE_USHORT => SIZEOF_SHORT,
44
55
  TYPE_UINT => SIZEOF_INT,
45
56
  TYPE_ULONG => SIZEOF_LONG,
57
+ TYPE_BOOL => SIZEOF_BOOL,
46
58
  }
47
59
  if defined?(TYPE_LONG_LONG)
48
60
  ALIGN_MAP[TYPE_LONG_LONG] = ALIGN_MAP[TYPE_ULONG_LONG] = ALIGN_LONG_LONG
data/lib/fiddle/value.rb CHANGED
@@ -6,17 +6,17 @@ module Fiddle
6
6
  def unsigned_value(val, ty)
7
7
  case ty.abs
8
8
  when TYPE_CHAR
9
- [val].pack("c").unpack("C")[0]
9
+ [val].pack("c").unpack1("C")
10
10
  when TYPE_SHORT
11
- [val].pack("s!").unpack("S!")[0]
11
+ [val].pack("s!").unpack1("S!")
12
12
  when TYPE_INT
13
- [val].pack("i!").unpack("I!")[0]
13
+ [val].pack("i!").unpack1("I!")
14
14
  when TYPE_LONG
15
- [val].pack("l!").unpack("L!")[0]
15
+ [val].pack("l!").unpack1("L!")
16
16
  else
17
17
  if defined?(TYPE_LONG_LONG) and
18
18
  ty.abs == TYPE_LONG_LONG
19
- [val].pack("q").unpack("Q")[0]
19
+ [val].pack("q").unpack1("Q")
20
20
  else
21
21
  val
22
22
  end
@@ -26,17 +26,17 @@ module Fiddle
26
26
  def signed_value(val, ty)
27
27
  case ty.abs
28
28
  when TYPE_CHAR
29
- [val].pack("C").unpack("c")[0]
29
+ [val].pack("C").unpack1("c")
30
30
  when TYPE_SHORT
31
- [val].pack("S!").unpack("s!")[0]
31
+ [val].pack("S!").unpack1("s!")
32
32
  when TYPE_INT
33
- [val].pack("I!").unpack("i!")[0]
33
+ [val].pack("I!").unpack1("i!")
34
34
  when TYPE_LONG
35
- [val].pack("L!").unpack("l!")[0]
35
+ [val].pack("L!").unpack1("l!")
36
36
  else
37
37
  if defined?(TYPE_LONG_LONG) and
38
38
  ty.abs == TYPE_LONG_LONG
39
- [val].pack("Q").unpack("q")[0]
39
+ [val].pack("Q").unpack1("q")
40
40
  else
41
41
  val
42
42
  end
@@ -80,11 +80,11 @@ module Fiddle
80
80
  else
81
81
  case SIZEOF_VOIDP
82
82
  when SIZEOF_LONG
83
- return [arg].pack("p").unpack("l!")[0]
83
+ return [arg].pack("p").unpack1("l!")
84
84
  else
85
85
  if defined?(SIZEOF_LONG_LONG) and
86
86
  SIZEOF_VOIDP == SIZEOF_LONG_LONG
87
- return [arg].pack("p").unpack("q")[0]
87
+ return [arg].pack("p").unpack1("q")
88
88
  else
89
89
  raise(RuntimeError, "sizeof(void*)?")
90
90
  end
@@ -102,10 +102,8 @@ module Fiddle
102
102
  return val.unpack('C*')
103
103
  end
104
104
  end
105
- return arg
106
- else
107
- return arg
108
105
  end
106
+ return arg
109
107
  else
110
108
  if( arg.respond_to?(:to_ptr) )
111
109
  return arg.to_ptr.to_i
@@ -1,3 +1,3 @@
1
1
  module Fiddle
2
- VERSION = "1.1.1"
2
+ VERSION = "1.1.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fiddle
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Patterson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-12-08 00:00:00.000000000 Z
12
+ date: 2023-11-08 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A libffi wrapper for Ruby.
15
15
  email:
@@ -73,7 +73,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  - !ruby/object:Gem::Version
74
74
  version: '0'
75
75
  requirements: []
76
- rubygems_version: 3.4.0.dev
76
+ rubygems_version: 3.5.0.dev
77
77
  signing_key:
78
78
  specification_version: 4
79
79
  summary: A libffi wrapper for Ruby.