summaryrefslogtreecommitdiff
path: root/ext/fiddle/conversions.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-15 10:57:05 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-02-15 10:57:05 +0000
commit34be46a32e6a8c88866c6dd9e42705ed63ed5d2a (patch)
treed763c9a7485543bc32f9a5700e691b8943f10442 /ext/fiddle/conversions.c
parentfa65df0d084077f6766c8a2580016de49cbf42f9 (diff)
downloadruby-34be46a32e6a8c88866c6dd9e42705ed63ed5d2a.tar.gz
* ext/fiddle/closure.c (callback): deal with unsinged integers.
[ruby-core:42458][Bug #5991][Bug #6022] * ext/fiddle/conversions.c (value_to_generic, generic_to_value): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34627 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/fiddle/conversions.c')
-rw-r--r--ext/fiddle/conversions.c58
1 files changed, 31 insertions, 27 deletions
diff --git a/ext/fiddle/conversions.c b/ext/fiddle/conversions.c
index 14b444fb65..f2c1511778 100644
--- a/ext/fiddle/conversions.c
+++ b/ext/fiddle/conversions.c
@@ -27,7 +27,7 @@ int_to_ffi_type(int type)
return rb_ffi_type_of(long);
#if HAVE_LONG_LONG
case TYPE_LONG_LONG:
- return rb_ffi_type_of(int64);
+ return rb_ffi_type_of(long_long);
#endif
case TYPE_FLOAT:
return &ffi_type_float;
@@ -42,13 +42,6 @@ int_to_ffi_type(int type)
void
value_to_generic(int type, VALUE src, fiddle_generic * dst)
{
- int signed_p = 1;
-
- if (type < 0) {
- type = -1 * type;
- signed_p = 0;
- }
-
switch (type) {
case TYPE_VOID:
break;
@@ -56,23 +49,35 @@ value_to_generic(int type, VALUE src, fiddle_generic * dst)
dst->pointer = NUM2PTR(rb_Integer(src));
break;
case TYPE_CHAR:
- dst->schar = NUM2INT(src);
+ dst->schar = (signed char)NUM2INT(src);
+ break;
+ case -TYPE_CHAR:
+ dst->uchar = (unsigned char)NUM2UINT(src);
break;
case TYPE_SHORT:
- dst->sshort = NUM2INT(src);
+ dst->sshort = (unsigned short)NUM2INT(src);
+ break;
+ case -TYPE_SHORT:
+ dst->sshort = (signed short)NUM2UINT(src);
break;
case TYPE_INT:
dst->sint = NUM2INT(src);
break;
+ case -TYPE_INT:
+ dst->uint = NUM2UINT(src);
+ break;
case TYPE_LONG:
- if (signed_p)
- dst->slong = NUM2LONG(src);
- else
- dst->ulong = NUM2LONG(src);
+ dst->slong = NUM2LONG(src);
+ break;
+ case -TYPE_LONG:
+ dst->ulong = NUM2ULONG(src);
break;
#if HAVE_LONG_LONG
case TYPE_LONG_LONG:
- dst->long_long = NUM2ULL(src);
+ dst->slong_long = NUM2LL(src);
+ break;
+ case -TYPE_LONG_LONG:
+ dst->ulong_long = NUM2ULL(src);
break;
#endif
case TYPE_FLOAT:
@@ -89,17 +94,11 @@ value_to_generic(int type, VALUE src, fiddle_generic * dst)
VALUE
generic_to_value(VALUE rettype, fiddle_generic retval)
{
- int signed_p = 1;
int type = NUM2INT(rettype);
VALUE cPointer;
cPointer = rb_const_get(mFiddle, rb_intern("Pointer"));
- if (type < 0) {
- type = -1 * type;
- signed_p = 0;
- }
-
switch (type) {
case TYPE_VOID:
return Qnil;
@@ -107,21 +106,26 @@ generic_to_value(VALUE rettype, fiddle_generic retval)
return rb_funcall(cPointer, rb_intern("[]"), 1,
PTR2NUM((void *)retval.pointer));
case TYPE_CHAR:
- if (signed_p) return INT2NUM((char)retval.fffi_sarg);
+ return INT2NUM((signed char)retval.fffi_sarg);
+ case -TYPE_CHAR:
return INT2NUM((unsigned char)retval.fffi_arg);
case TYPE_SHORT:
- if (signed_p) return INT2NUM((short)retval.fffi_sarg);
+ return INT2NUM((signed short)retval.fffi_sarg);
+ case -TYPE_SHORT:
return INT2NUM((unsigned short)retval.fffi_arg);
case TYPE_INT:
- if (signed_p) return INT2NUM((int)retval.fffi_sarg);
+ return INT2NUM((signed int)retval.fffi_sarg);
+ case -TYPE_INT:
return UINT2NUM((unsigned int)retval.fffi_arg);
case TYPE_LONG:
- if (signed_p) return LONG2NUM(retval.slong);
+ return LONG2NUM(retval.slong);
+ case -TYPE_LONG:
return ULONG2NUM(retval.ulong);
#if HAVE_LONG_LONG
case TYPE_LONG_LONG:
- return rb_ll2inum(retval.long_long);
- break;
+ return LL2NUM(retval.slong_long);
+ case -TYPE_LONG_LONG:
+ return ULL2NUM(retval.ulong_long);
#endif
case TYPE_FLOAT:
return rb_float_new(retval.ffloat);