Skip to content

Commit bc6c66b

Browse files
committed
Add support for bool
GitHub: fix GH-130 Reported by Benoit Daloze. Thanks!!!
1 parent 6cdf537 commit bc6c66b

File tree

5 files changed

+43
-0
lines changed

5 files changed

+43
-0
lines changed

ext/fiddle/conversions.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
#include <fiddle.h>
22

3+
VALUE
4+
rb_fiddle_type_bool(void)
5+
{
6+
if (sizeof(bool) == sizeof(char)) {
7+
return INT2NUM(TYPE_UCHAR);
8+
} else if (sizeof(bool) == sizeof(short)) {
9+
return INT2NUM(TYPE_USHORT);
10+
} else if (sizeof(bool) == sizeof(int)) {
11+
return INT2NUM(TYPE_UINT);
12+
} else if (sizeof(bool) == sizeof(long)) {
13+
return INT2NUM(TYPE_ULONG);
14+
} else {
15+
rb_raise(rb_eNotImpError,
16+
"bool isn't supported: %u",
17+
(unsigned int)sizeof(bool));
18+
return RUBY_Qnil;
19+
}
20+
}
21+
322
VALUE
423
rb_fiddle_type_ensure(VALUE type)
524
{
@@ -44,6 +63,7 @@ rb_fiddle_type_ensure(VALUE type)
4463
ID ptrdiff_t_id;
4564
ID intptr_t_id;
4665
ID uintptr_t_id;
66+
ID bool_id;
4767
RUBY_CONST_ID(void_id, "void");
4868
RUBY_CONST_ID(voidp_id, "voidp");
4969
RUBY_CONST_ID(char_id, "char");
@@ -74,6 +94,7 @@ rb_fiddle_type_ensure(VALUE type)
7494
RUBY_CONST_ID(ptrdiff_t_id, "ptrdiff_t");
7595
RUBY_CONST_ID(intptr_t_id, "intptr_t");
7696
RUBY_CONST_ID(uintptr_t_id, "uintptr_t");
97+
RUBY_CONST_ID(bool_id, "bool");
7798
if (type_id == void_id) {
7899
return INT2NUM(TYPE_VOID);
79100
}
@@ -144,6 +165,9 @@ rb_fiddle_type_ensure(VALUE type)
144165
else if (type_id == uintptr_t_id) {
145166
return INT2NUM(TYPE_UINTPTR_T);
146167
}
168+
else if (type_id == bool_id) {
169+
return rb_fiddle_type_bool();
170+
}
147171
else {
148172
type = original_type;
149173
}

ext/fiddle/conversions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ typedef union
2424
void * pointer; /* ffi_type_pointer */
2525
} fiddle_generic;
2626

27+
VALUE rb_fiddle_type_bool(void);
2728
VALUE rb_fiddle_type_ensure(VALUE type);
2829
ffi_type * rb_fiddle_int_to_ffi_type(int type);
2930
void rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst);

ext/fiddle/fiddle.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,12 @@ Init_fiddle(void)
357357
*/
358358
rb_define_const(mFiddleTypes, "UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
359359

360+
/* Document-const: Fiddle::Types::BOOL
361+
*
362+
* C type - bool
363+
*/
364+
rb_define_const(mFiddleTypes, "BOOL" , rb_fiddle_type_bool());
365+
360366
/* Document-const: ALIGN_VOIDP
361367
*
362368
* The alignment size of a void*
@@ -635,6 +641,12 @@ Init_fiddle(void)
635641
*/
636642
rb_define_const(mFiddle, "SIZEOF_CONST_STRING", INT2NUM(sizeof(const char*)));
637643

644+
/* Document-const: SIZEOF_BOOL
645+
*
646+
* size of a bool
647+
*/
648+
rb_define_const(mFiddle, "SIZEOF_BOOL", INT2NUM(sizeof(bool)));
649+
638650
/* Document-const: RUBY_FREE
639651
*
640652
* Address of the ruby_xfree() function

lib/fiddle/cparser.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ def parse_ctype(ty, tymap=nil)
247247
return TYPE_INTPTR_T
248248
when /\Auintptr_t(?:\s+\w+)?\z/
249249
return TYPE_UINTPTR_T
250+
when "bool"
251+
return TYPE_BOOL
250252
when /\*/, /\[[\s\d]*\]/
251253
return TYPE_VOIDP
252254
when "..."

test/fiddle/test_cparser.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ def test_uintptr_t_ctype
121121
assert_equal(TYPE_UINTPTR_T, parse_ctype("const uintptr_t"))
122122
end
123123

124+
def test_bool_ctype
125+
assert_equal(TYPE_BOOL, parse_ctype('bool'))
126+
end
127+
124128
def test_undefined_ctype
125129
assert_raise(DLError) { parse_ctype('DWORD') }
126130
end

0 commit comments

Comments
 (0)