2010-05-06 10:59:24 +04:00
|
|
|
#include <fiddle.h>
|
|
|
|
|
2020-07-09 15:39:51 +03:00
|
|
|
VALUE
|
|
|
|
rb_fiddle_type_ensure(VALUE type)
|
|
|
|
{
|
|
|
|
VALUE original_type = type;
|
|
|
|
|
|
|
|
if (!RB_SYMBOL_P(type)) {
|
|
|
|
VALUE type_string = rb_check_string_type(type);
|
|
|
|
if (!NIL_P(type_string)) {
|
|
|
|
type = rb_to_symbol(type_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (RB_SYMBOL_P(type)) {
|
|
|
|
ID type_id = rb_sym2id(type);
|
|
|
|
ID void_id;
|
|
|
|
ID voidp_id;
|
|
|
|
ID char_id;
|
|
|
|
ID short_id;
|
|
|
|
ID int_id;
|
|
|
|
ID long_id;
|
|
|
|
#ifdef TYPE_LONG_LONG
|
|
|
|
ID long_long_id;
|
2020-12-11 03:41:12 +03:00
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT8_T
|
|
|
|
ID int8_t_id;
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT16_T
|
|
|
|
ID int16_t_id;
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT32_T
|
|
|
|
ID int32_t_id;
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT64_T
|
|
|
|
ID int64_t_id;
|
2020-07-09 15:39:51 +03:00
|
|
|
#endif
|
|
|
|
ID float_id;
|
|
|
|
ID double_id;
|
|
|
|
ID variadic_id;
|
|
|
|
ID const_string_id;
|
|
|
|
ID size_t_id;
|
|
|
|
ID ssize_t_id;
|
|
|
|
ID ptrdiff_t_id;
|
|
|
|
ID intptr_t_id;
|
|
|
|
ID uintptr_t_id;
|
|
|
|
RUBY_CONST_ID(void_id, "void");
|
|
|
|
RUBY_CONST_ID(voidp_id, "voidp");
|
|
|
|
RUBY_CONST_ID(char_id, "char");
|
|
|
|
RUBY_CONST_ID(short_id, "short");
|
|
|
|
RUBY_CONST_ID(int_id, "int");
|
|
|
|
RUBY_CONST_ID(long_id, "long");
|
|
|
|
#ifdef TYPE_LONG_LONG
|
|
|
|
RUBY_CONST_ID(long_long_id, "long_long");
|
2020-12-11 03:41:12 +03:00
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT8_T
|
|
|
|
RUBY_CONST_ID(int8_t_id, "int8_t");
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT16_T
|
|
|
|
RUBY_CONST_ID(int16_t_id, "int16_t");
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT32_T
|
|
|
|
RUBY_CONST_ID(int32_t_id, "int32_t");
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT64_T
|
|
|
|
RUBY_CONST_ID(int64_t_id, "int64_t");
|
2020-07-09 15:39:51 +03:00
|
|
|
#endif
|
|
|
|
RUBY_CONST_ID(float_id, "float");
|
|
|
|
RUBY_CONST_ID(double_id, "double");
|
|
|
|
RUBY_CONST_ID(variadic_id, "variadic");
|
|
|
|
RUBY_CONST_ID(const_string_id, "const_string");
|
|
|
|
RUBY_CONST_ID(size_t_id, "size_t");
|
|
|
|
RUBY_CONST_ID(ssize_t_id, "ssize_t");
|
|
|
|
RUBY_CONST_ID(ptrdiff_t_id, "ptrdiff_t");
|
|
|
|
RUBY_CONST_ID(intptr_t_id, "intptr_t");
|
|
|
|
RUBY_CONST_ID(uintptr_t_id, "uintptr_t");
|
|
|
|
if (type_id == void_id) {
|
|
|
|
return INT2NUM(TYPE_VOID);
|
|
|
|
}
|
|
|
|
else if (type_id == voidp_id) {
|
|
|
|
return INT2NUM(TYPE_VOIDP);
|
|
|
|
}
|
|
|
|
else if (type_id == char_id) {
|
|
|
|
return INT2NUM(TYPE_CHAR);
|
|
|
|
}
|
|
|
|
else if (type_id == short_id) {
|
|
|
|
return INT2NUM(TYPE_SHORT);
|
|
|
|
}
|
|
|
|
else if (type_id == int_id) {
|
|
|
|
return INT2NUM(TYPE_INT);
|
|
|
|
}
|
|
|
|
else if (type_id == long_id) {
|
|
|
|
return INT2NUM(TYPE_LONG);
|
|
|
|
}
|
|
|
|
#ifdef TYPE_LONG_LONG
|
|
|
|
else if (type_id == long_long_id) {
|
|
|
|
return INT2NUM(TYPE_LONG_LONG);
|
|
|
|
}
|
2020-12-11 03:41:12 +03:00
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT8_T
|
|
|
|
else if (type_id == int8_t_id) {
|
|
|
|
return INT2NUM(TYPE_INT8_T);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT16_T
|
|
|
|
else if (type_id == int16_t_id) {
|
|
|
|
return INT2NUM(TYPE_INT16_T);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT32_T
|
|
|
|
else if (type_id == int32_t_id) {
|
|
|
|
return INT2NUM(TYPE_INT32_T);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef TYPE_INT64_T
|
|
|
|
else if (type_id == int64_t_id) {
|
|
|
|
return INT2NUM(TYPE_INT64_T);
|
|
|
|
}
|
2020-07-09 15:39:51 +03:00
|
|
|
#endif
|
|
|
|
else if (type_id == float_id) {
|
|
|
|
return INT2NUM(TYPE_FLOAT);
|
|
|
|
}
|
|
|
|
else if (type_id == double_id) {
|
|
|
|
return INT2NUM(TYPE_DOUBLE);
|
|
|
|
}
|
|
|
|
else if (type_id == variadic_id) {
|
|
|
|
return INT2NUM(TYPE_VARIADIC);
|
|
|
|
}
|
|
|
|
else if (type_id == const_string_id) {
|
|
|
|
return INT2NUM(TYPE_CONST_STRING);
|
|
|
|
}
|
|
|
|
else if (type_id == size_t_id) {
|
|
|
|
return INT2NUM(TYPE_SIZE_T);
|
|
|
|
}
|
|
|
|
else if (type_id == ssize_t_id) {
|
|
|
|
return INT2NUM(TYPE_SSIZE_T);
|
|
|
|
}
|
|
|
|
else if (type_id == ptrdiff_t_id) {
|
|
|
|
return INT2NUM(TYPE_PTRDIFF_T);
|
|
|
|
}
|
|
|
|
else if (type_id == intptr_t_id) {
|
|
|
|
return INT2NUM(TYPE_INTPTR_T);
|
|
|
|
}
|
|
|
|
else if (type_id == uintptr_t_id) {
|
|
|
|
return INT2NUM(TYPE_UINTPTR_T);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
type = original_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rb_to_int(type);
|
|
|
|
}
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
ffi_type *
|
2020-07-09 14:19:20 +03:00
|
|
|
rb_fiddle_int_to_ffi_type(int type)
|
2010-05-06 10: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 14:57:05 +04:00
|
|
|
return rb_ffi_type_of(long_long);
|
2010-05-06 10:59:24 +04:00
|
|
|
#endif
|
|
|
|
case TYPE_FLOAT:
|
|
|
|
return &ffi_type_float;
|
|
|
|
case TYPE_DOUBLE:
|
|
|
|
return &ffi_type_double;
|
2020-07-09 14:19:20 +03:00
|
|
|
case TYPE_CONST_STRING:
|
|
|
|
return &ffi_type_pointer;
|
2010-05-06 10:59:24 +04:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eRuntimeError, "unknown type %d", type);
|
|
|
|
}
|
|
|
|
return &ffi_type_pointer;
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:19:20 +03:00
|
|
|
ffi_type *
|
|
|
|
int_to_ffi_type(int type)
|
|
|
|
{
|
|
|
|
return rb_fiddle_int_to_ffi_type(type);
|
|
|
|
}
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
void
|
2020-07-09 14:19:20 +03:00
|
|
|
rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst)
|
2010-05-06 10:59:24 +04:00
|
|
|
{
|
|
|
|
switch (type) {
|
|
|
|
case TYPE_VOID:
|
|
|
|
break;
|
|
|
|
case TYPE_VOIDP:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->pointer = NUM2PTR(rb_Integer(*src));
|
2010-05-06 10:59:24 +04:00
|
|
|
break;
|
|
|
|
case TYPE_CHAR:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->schar = (signed char)NUM2INT(*src);
|
2012-02-15 14:57:05 +04:00
|
|
|
break;
|
|
|
|
case -TYPE_CHAR:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->uchar = (unsigned char)NUM2UINT(*src);
|
2011-08-09 09:31:47 +04:00
|
|
|
break;
|
2010-05-06 10:59:24 +04:00
|
|
|
case TYPE_SHORT:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->sshort = (unsigned short)NUM2INT(*src);
|
2012-02-15 14:57:05 +04:00
|
|
|
break;
|
|
|
|
case -TYPE_SHORT:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->sshort = (signed short)NUM2UINT(*src);
|
2011-08-09 09:31:47 +04:00
|
|
|
break;
|
2010-05-06 10:59:24 +04:00
|
|
|
case TYPE_INT:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->sint = NUM2INT(*src);
|
2010-05-06 10:59:24 +04:00
|
|
|
break;
|
2012-02-15 14:57:05 +04:00
|
|
|
case -TYPE_INT:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->uint = NUM2UINT(*src);
|
2012-02-15 14:57:05 +04:00
|
|
|
break;
|
2010-05-06 10:59:24 +04:00
|
|
|
case TYPE_LONG:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->slong = NUM2LONG(*src);
|
2012-02-15 14:57:05 +04:00
|
|
|
break;
|
|
|
|
case -TYPE_LONG:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->ulong = NUM2ULONG(*src);
|
2010-05-06 10:59:24 +04:00
|
|
|
break;
|
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
case TYPE_LONG_LONG:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->slong_long = NUM2LL(*src);
|
2012-02-15 14:57:05 +04:00
|
|
|
break;
|
|
|
|
case -TYPE_LONG_LONG:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->ulong_long = NUM2ULL(*src);
|
2010-05-06 10:59:24 +04:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case TYPE_FLOAT:
|
2020-07-09 14:19:20 +03:00
|
|
|
dst->ffloat = (float)NUM2DBL(*src);
|
2010-05-06 10:59:24 +04:00
|
|
|
break;
|
|
|
|
case TYPE_DOUBLE:
|
2020-07-09 14:19:20 +03: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 10:59:24 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rb_raise(rb_eRuntimeError, "unknown type %d", type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:19:20 +03: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. */
|
2020-11-17 23:18:55 +03:00
|
|
|
rb_fiddle_value_to_generic(type, &src, dst);
|
2020-07-09 14:19:20 +03:00
|
|
|
}
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
VALUE
|
2020-07-09 14:19:20 +03:00
|
|
|
rb_fiddle_generic_to_value(VALUE rettype, fiddle_generic retval)
|
2010-05-06 10: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 14:57:05 +04:00
|
|
|
return INT2NUM((signed char)retval.fffi_sarg);
|
|
|
|
case -TYPE_CHAR:
|
2011-08-09 09:31:47 +04:00
|
|
|
return INT2NUM((unsigned char)retval.fffi_arg);
|
2010-05-06 10:59:24 +04:00
|
|
|
case TYPE_SHORT:
|
2012-02-15 14:57:05 +04:00
|
|
|
return INT2NUM((signed short)retval.fffi_sarg);
|
|
|
|
case -TYPE_SHORT:
|
2011-08-09 09:31:47 +04:00
|
|
|
return INT2NUM((unsigned short)retval.fffi_arg);
|
2010-05-06 10:59:24 +04:00
|
|
|
case TYPE_INT:
|
2012-02-15 14:57:05 +04:00
|
|
|
return INT2NUM((signed int)retval.fffi_sarg);
|
|
|
|
case -TYPE_INT:
|
2011-08-09 09:31:47 +04:00
|
|
|
return UINT2NUM((unsigned int)retval.fffi_arg);
|
2010-05-06 10:59:24 +04:00
|
|
|
case TYPE_LONG:
|
2012-02-15 14:57:05 +04:00
|
|
|
return LONG2NUM(retval.slong);
|
|
|
|
case -TYPE_LONG:
|
2010-05-06 10:59:24 +04:00
|
|
|
return ULONG2NUM(retval.ulong);
|
|
|
|
#if HAVE_LONG_LONG
|
|
|
|
case TYPE_LONG_LONG:
|
2012-02-15 14:57:05 +04:00
|
|
|
return LL2NUM(retval.slong_long);
|
|
|
|
case -TYPE_LONG_LONG:
|
|
|
|
return ULL2NUM(retval.ulong_long);
|
2010-05-06 10: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 14:19:20 +03:00
|
|
|
case TYPE_CONST_STRING:
|
|
|
|
if (retval.pointer) {
|
|
|
|
return rb_str_new_cstr(retval.pointer);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Qnil;
|
|
|
|
}
|
2010-05-06 10:59:24 +04:00
|
|
|
default:
|
|
|
|
rb_raise(rb_eRuntimeError, "unknown type %d", type);
|
|
|
|
}
|
2012-04-14 03:45:37 +04:00
|
|
|
|
|
|
|
UNREACHABLE;
|
2010-05-06 10:59:24 +04:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:19:20 +03:00
|
|
|
VALUE
|
|
|
|
generic_to_value(VALUE rettype, fiddle_generic retval)
|
|
|
|
{
|
|
|
|
return rb_fiddle_generic_to_value(rettype, retval);
|
|
|
|
}
|
|
|
|
|
2010-05-06 10:59:24 +04:00
|
|
|
/* vim: set noet sw=4 sts=4 */
|