[ruby/fiddle] Add support for bool
GitHub: fix https://github.com/ruby/fiddle/pull/130 Reported by Benoit Daloze. Thanks!!! https://github.com/ruby/fiddle/commit/bc6c66bbb9
This commit is contained in:
parent
10588fa121
commit
15e8cf7ad9
@ -1,5 +1,24 @@
|
|||||||
#include <fiddle.h>
|
#include <fiddle.h>
|
||||||
|
|
||||||
|
VALUE
|
||||||
|
rb_fiddle_type_bool(void)
|
||||||
|
{
|
||||||
|
if (sizeof(bool) == sizeof(char)) {
|
||||||
|
return INT2NUM(TYPE_UCHAR);
|
||||||
|
} else if (sizeof(bool) == sizeof(short)) {
|
||||||
|
return INT2NUM(TYPE_USHORT);
|
||||||
|
} else if (sizeof(bool) == sizeof(int)) {
|
||||||
|
return INT2NUM(TYPE_UINT);
|
||||||
|
} else if (sizeof(bool) == sizeof(long)) {
|
||||||
|
return INT2NUM(TYPE_ULONG);
|
||||||
|
} else {
|
||||||
|
rb_raise(rb_eNotImpError,
|
||||||
|
"bool isn't supported: %u",
|
||||||
|
(unsigned int)sizeof(bool));
|
||||||
|
return RUBY_Qnil;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
VALUE
|
VALUE
|
||||||
rb_fiddle_type_ensure(VALUE type)
|
rb_fiddle_type_ensure(VALUE type)
|
||||||
{
|
{
|
||||||
@ -44,6 +63,7 @@ rb_fiddle_type_ensure(VALUE type)
|
|||||||
ID ptrdiff_t_id;
|
ID ptrdiff_t_id;
|
||||||
ID intptr_t_id;
|
ID intptr_t_id;
|
||||||
ID uintptr_t_id;
|
ID uintptr_t_id;
|
||||||
|
ID bool_id;
|
||||||
RUBY_CONST_ID(void_id, "void");
|
RUBY_CONST_ID(void_id, "void");
|
||||||
RUBY_CONST_ID(voidp_id, "voidp");
|
RUBY_CONST_ID(voidp_id, "voidp");
|
||||||
RUBY_CONST_ID(char_id, "char");
|
RUBY_CONST_ID(char_id, "char");
|
||||||
@ -74,6 +94,7 @@ rb_fiddle_type_ensure(VALUE type)
|
|||||||
RUBY_CONST_ID(ptrdiff_t_id, "ptrdiff_t");
|
RUBY_CONST_ID(ptrdiff_t_id, "ptrdiff_t");
|
||||||
RUBY_CONST_ID(intptr_t_id, "intptr_t");
|
RUBY_CONST_ID(intptr_t_id, "intptr_t");
|
||||||
RUBY_CONST_ID(uintptr_t_id, "uintptr_t");
|
RUBY_CONST_ID(uintptr_t_id, "uintptr_t");
|
||||||
|
RUBY_CONST_ID(bool_id, "bool");
|
||||||
if (type_id == void_id) {
|
if (type_id == void_id) {
|
||||||
return INT2NUM(TYPE_VOID);
|
return INT2NUM(TYPE_VOID);
|
||||||
}
|
}
|
||||||
@ -144,6 +165,9 @@ rb_fiddle_type_ensure(VALUE type)
|
|||||||
else if (type_id == uintptr_t_id) {
|
else if (type_id == uintptr_t_id) {
|
||||||
return INT2NUM(TYPE_UINTPTR_T);
|
return INT2NUM(TYPE_UINTPTR_T);
|
||||||
}
|
}
|
||||||
|
else if (type_id == bool_id) {
|
||||||
|
return rb_fiddle_type_bool();
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
type = original_type;
|
type = original_type;
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,7 @@ typedef union
|
|||||||
void * pointer; /* ffi_type_pointer */
|
void * pointer; /* ffi_type_pointer */
|
||||||
} fiddle_generic;
|
} fiddle_generic;
|
||||||
|
|
||||||
|
VALUE rb_fiddle_type_bool(void);
|
||||||
VALUE rb_fiddle_type_ensure(VALUE type);
|
VALUE rb_fiddle_type_ensure(VALUE type);
|
||||||
ffi_type * rb_fiddle_int_to_ffi_type(int type);
|
ffi_type * rb_fiddle_int_to_ffi_type(int type);
|
||||||
void rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst);
|
void rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst);
|
||||||
|
@ -357,6 +357,12 @@ Init_fiddle(void)
|
|||||||
*/
|
*/
|
||||||
rb_define_const(mFiddleTypes, "UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
|
rb_define_const(mFiddleTypes, "UINTPTR_T", INT2NUM(TYPE_UINTPTR_T));
|
||||||
|
|
||||||
|
/* Document-const: Fiddle::Types::BOOL
|
||||||
|
*
|
||||||
|
* C type - bool
|
||||||
|
*/
|
||||||
|
rb_define_const(mFiddleTypes, "BOOL" , rb_fiddle_type_bool());
|
||||||
|
|
||||||
/* Document-const: ALIGN_VOIDP
|
/* Document-const: ALIGN_VOIDP
|
||||||
*
|
*
|
||||||
* The alignment size of a void*
|
* The alignment size of a void*
|
||||||
@ -635,6 +641,12 @@ Init_fiddle(void)
|
|||||||
*/
|
*/
|
||||||
rb_define_const(mFiddle, "SIZEOF_CONST_STRING", INT2NUM(sizeof(const char*)));
|
rb_define_const(mFiddle, "SIZEOF_CONST_STRING", INT2NUM(sizeof(const char*)));
|
||||||
|
|
||||||
|
/* Document-const: SIZEOF_BOOL
|
||||||
|
*
|
||||||
|
* size of a bool
|
||||||
|
*/
|
||||||
|
rb_define_const(mFiddle, "SIZEOF_BOOL", INT2NUM(sizeof(bool)));
|
||||||
|
|
||||||
/* Document-const: RUBY_FREE
|
/* Document-const: RUBY_FREE
|
||||||
*
|
*
|
||||||
* Address of the ruby_xfree() function
|
* Address of the ruby_xfree() function
|
||||||
|
@ -247,6 +247,8 @@ module Fiddle
|
|||||||
return TYPE_INTPTR_T
|
return TYPE_INTPTR_T
|
||||||
when /\Auintptr_t(?:\s+\w+)?\z/
|
when /\Auintptr_t(?:\s+\w+)?\z/
|
||||||
return TYPE_UINTPTR_T
|
return TYPE_UINTPTR_T
|
||||||
|
when "bool"
|
||||||
|
return TYPE_BOOL
|
||||||
when /\*/, /\[[\s\d]*\]/
|
when /\*/, /\[[\s\d]*\]/
|
||||||
return TYPE_VOIDP
|
return TYPE_VOIDP
|
||||||
when "..."
|
when "..."
|
||||||
|
@ -121,6 +121,10 @@ module Fiddle
|
|||||||
assert_equal(TYPE_UINTPTR_T, parse_ctype("const uintptr_t"))
|
assert_equal(TYPE_UINTPTR_T, parse_ctype("const uintptr_t"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_bool_ctype
|
||||||
|
assert_equal(TYPE_BOOL, parse_ctype('bool'))
|
||||||
|
end
|
||||||
|
|
||||||
def test_undefined_ctype
|
def test_undefined_ctype
|
||||||
assert_raise(DLError) { parse_ctype('DWORD') }
|
assert_raise(DLError) { parse_ctype('DWORD') }
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user