2010-05-06 02:59:24 -04:00
|
|
|
#include <fiddle.h>
|
|
|
|
|
|
|
|
ffi_type *
|
2020-07-09 07:19:20 -04:00
|
|
|
rb_fiddle_int_to_ffi_type(int type)
|
2010-05-06 02:59:24 -04:00
|
|
|
{
|
|
|
|
int signed_p = 1;
|
|
|
|
|
|
|
|
if (type < 0) {
|
|
|
|
type = -1 * type;
|
|
|
|
signed_p = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define rb_ffi_type_of(t) (signed_p ? &ffi_type_s##t : &ffi_type_u##t)
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_VOID:
|
|
|
|
return &ffi_type_void;
|
|
|
|
case TYPE_VOIDP:
|
|
|
|
return &ffi_type_pointer;
|
|
|
|
case TYPE_CHAR:
|
|
|
|
return rb_ffi_type_of(char);
|
|
|
|
case TYPE_SHORT:
|
|
|
|
return rb_ffi_type_of(short);
|
|
|
|
case TYPE_INT:
|
|
|
|
return rb_ffi_type_of(int);
|
|
|
|
case TYPE_LONG:
|
|
|
|
return rb_ffi_type_of(long);
|
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
case TYPE_LONG_LONG:
|
2012-02-15 05:57:05 -05:00
|
|
|
return rb_ffi_type_of(long_long);
|
2010-05-06 02:59:24 -04:00
|
|
|
#endif
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
return &ffi_type_float;
|
|
|
|
case TYPE_DOUBLE:
|
|
|
|
return &ffi_type_double;
|
2020-07-09 07:19:20 -04:00
|
|
|
case TYPE_CONST_STRING:
|
|
|
|
return &ffi_type_pointer;
|
2010-05-06 02:59:24 -04:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eRuntimeError, "unknown type %d", type);
|
|
|
|
}
|
|
|
|
return &ffi_type_pointer;
|
|
|
|
}
|
|
|
|
|
2020-07-09 07:19:20 -04:00
|
|
|
ffi_type *
|
|
|
|
int_to_ffi_type(int type)
|
|
|
|
{
|
|
|
|
return rb_fiddle_int_to_ffi_type(type);
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
void
|
2020-07-09 07:19:20 -04:00
|
|
|
rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst)
|
2010-05-06 02:59:24 -04:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_VOID:
|
|
|
|
break;
|
|
|
|
case TYPE_VOIDP:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->pointer = NUM2PTR(rb_Integer(*src));
|
2010-05-06 02:59:24 -04:00
|
|
|
break;
|
|
|
|
case TYPE_CHAR:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->schar = (signed char)NUM2INT(*src);
|
2012-02-15 05:57:05 -05:00
|
|
|
break;
|
|
|
|
case -TYPE_CHAR:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->uchar = (unsigned char)NUM2UINT(*src);
|
2011-08-09 01:31:47 -04:00
|
|
|
break;
|
2010-05-06 02:59:24 -04:00
|
|
|
case TYPE_SHORT:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->sshort = (unsigned short)NUM2INT(*src);
|
2012-02-15 05:57:05 -05:00
|
|
|
break;
|
|
|
|
case -TYPE_SHORT:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->sshort = (signed short)NUM2UINT(*src);
|
2011-08-09 01:31:47 -04:00
|
|
|
break;
|
2010-05-06 02:59:24 -04:00
|
|
|
case TYPE_INT:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->sint = NUM2INT(*src);
|
2010-05-06 02:59:24 -04:00
|
|
|
break;
|
2012-02-15 05:57:05 -05:00
|
|
|
case -TYPE_INT:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->uint = NUM2UINT(*src);
|
2012-02-15 05:57:05 -05:00
|
|
|
break;
|
2010-05-06 02:59:24 -04:00
|
|
|
case TYPE_LONG:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->slong = NUM2LONG(*src);
|
2012-02-15 05:57:05 -05:00
|
|
|
break;
|
|
|
|
case -TYPE_LONG:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->ulong = NUM2ULONG(*src);
|
2010-05-06 02:59:24 -04:00
|
|
|
break;
|
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
case TYPE_LONG_LONG:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->slong_long = NUM2LL(*src);
|
2012-02-15 05:57:05 -05:00
|
|
|
break;
|
|
|
|
case -TYPE_LONG_LONG:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->ulong_long = NUM2ULL(*src);
|
2010-05-06 02:59:24 -04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TYPE_FLOAT:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->ffloat = (float)NUM2DBL(*src);
|
2010-05-06 02:59:24 -04:00
|
|
|
break;
|
|
|
|
case TYPE_DOUBLE:
|
2020-07-09 07:19:20 -04:00
|
|
|
dst->ddouble = NUM2DBL(*src);
|
|
|
|
break;
|
|
|
|
case TYPE_CONST_STRING:
|
|
|
|
if (NIL_P(*src)) {
|
|
|
|
dst->pointer = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dst->pointer = rb_string_value_cstr(src);
|
|
|
|
}
|
2010-05-06 02:59:24 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eRuntimeError, "unknown type %d", type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 07:19:20 -04:00
|
|
|
void
|
|
|
|
value_to_generic(int type, VALUE src, fiddle_generic *dst)
|
|
|
|
{
|
|
|
|
/* src isn't safe from GC when type is TYPE_CONST_STRING and src
|
|
|
|
* isn't String. */
|
|
|
|
return rb_fiddle_value_to_generic(type, &src, dst);
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
VALUE
|
2020-07-09 07:19:20 -04:00
|
|
|
rb_fiddle_generic_to_value(VALUE rettype, fiddle_generic retval)
|
2010-05-06 02:59:24 -04:00
|
|
|
{
|
|
|
|
int type = NUM2INT(rettype);
|
|
|
|
VALUE cPointer;
|
|
|
|
|
|
|
|
cPointer = rb_const_get(mFiddle, rb_intern("Pointer"));
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_VOID:
|
|
|
|
return Qnil;
|
|
|
|
case TYPE_VOIDP:
|
|
|
|
return rb_funcall(cPointer, rb_intern("[]"), 1,
|
|
|
|
PTR2NUM((void *)retval.pointer));
|
|
|
|
case TYPE_CHAR:
|
2012-02-15 05:57:05 -05:00
|
|
|
return INT2NUM((signed char)retval.fffi_sarg);
|
|
|
|
case -TYPE_CHAR:
|
2011-08-09 01:31:47 -04:00
|
|
|
return INT2NUM((unsigned char)retval.fffi_arg);
|
2010-05-06 02:59:24 -04:00
|
|
|
case TYPE_SHORT:
|
2012-02-15 05:57:05 -05:00
|
|
|
return INT2NUM((signed short)retval.fffi_sarg);
|
|
|
|
case -TYPE_SHORT:
|
2011-08-09 01:31:47 -04:00
|
|
|
return INT2NUM((unsigned short)retval.fffi_arg);
|
2010-05-06 02:59:24 -04:00
|
|
|
case TYPE_INT:
|
2012-02-15 05:57:05 -05:00
|
|
|
return INT2NUM((signed int)retval.fffi_sarg);
|
|
|
|
case -TYPE_INT:
|
2011-08-09 01:31:47 -04:00
|
|
|
return UINT2NUM((unsigned int)retval.fffi_arg);
|
2010-05-06 02:59:24 -04:00
|
|
|
case TYPE_LONG:
|
2012-02-15 05:57:05 -05:00
|
|
|
return LONG2NUM(retval.slong);
|
|
|
|
case -TYPE_LONG:
|
2010-05-06 02:59:24 -04:00
|
|
|
return ULONG2NUM(retval.ulong);
|
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
case TYPE_LONG_LONG:
|
2012-02-15 05:57:05 -05:00
|
|
|
return LL2NUM(retval.slong_long);
|
|
|
|
case -TYPE_LONG_LONG:
|
|
|
|
return ULL2NUM(retval.ulong_long);
|
2010-05-06 02:59:24 -04:00
|
|
|
#endif
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
return rb_float_new(retval.ffloat);
|
|
|
|
case TYPE_DOUBLE:
|
|
|
|
return rb_float_new(retval.ddouble);
|
2020-07-09 07:19:20 -04:00
|
|
|
case TYPE_CONST_STRING:
|
|
|
|
if (retval.pointer) {
|
|
|
|
return rb_str_new_cstr(retval.pointer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-06 02:59:24 -04:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eRuntimeError, "unknown type %d", type);
|
|
|
|
}
|
2012-04-13 19:45:37 -04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
|
|
|
|
2020-07-09 07:19:20 -04:00
|
|
|
VALUE
|
|
|
|
generic_to_value(VALUE rettype, fiddle_generic retval)
|
|
|
|
{
|
|
|
|
return rb_fiddle_generic_to_value(rettype, retval);
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
/* vim: set noet sw=4 sts=4 */
|