1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/fiddle/conversions.c

179 lines
4 KiB
C
Raw Normal View History

#include <fiddle.h>
ffi_type *
rb_fiddle_int_to_ffi_type(int type)
{
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:
return rb_ffi_type_of(long_long);
#endif
case TYPE_FLOAT:
return &ffi_type_float;
case TYPE_DOUBLE:
return &ffi_type_double;
case TYPE_CONST_STRING:
return &ffi_type_pointer;
default:
rb_raise(rb_eRuntimeError, "unknown type %d", type);
}
return &ffi_type_pointer;
}
ffi_type *
int_to_ffi_type(int type)
{
return rb_fiddle_int_to_ffi_type(type);
}
void
rb_fiddle_value_to_generic(int type, VALUE *src, fiddle_generic *dst)
{
switch (type) {
case TYPE_VOID:
break;
case TYPE_VOIDP:
dst->pointer = NUM2PTR(rb_Integer(*src));
break;
case TYPE_CHAR:
dst->schar = (signed char)NUM2INT(*src);
break;
case -TYPE_CHAR:
dst->uchar = (unsigned char)NUM2UINT(*src);
break;
case TYPE_SHORT:
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:
dst->slong = NUM2LONG(*src);
break;
case -TYPE_LONG:
dst->ulong = NUM2ULONG(*src);
break;
#if HAVE_LONG_LONG
case TYPE_LONG_LONG:
dst->slong_long = NUM2LL(*src);
break;
case -TYPE_LONG_LONG:
dst->ulong_long = NUM2ULL(*src);
break;
#endif
case TYPE_FLOAT:
dst->ffloat = (float)NUM2DBL(*src);
break;
case TYPE_DOUBLE:
dst->ddouble = NUM2DBL(*src);
break;
case TYPE_CONST_STRING:
if (NIL_P(*src)) {
dst->pointer = NULL;
}
else {
dst->pointer = rb_string_value_cstr(src);
}
break;
default:
rb_raise(rb_eRuntimeError, "unknown type %d", type);
}
}
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);
}
VALUE
rb_fiddle_generic_to_value(VALUE rettype, fiddle_generic retval)
{
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:
return INT2NUM((signed char)retval.fffi_sarg);
case -TYPE_CHAR:
return INT2NUM((unsigned char)retval.fffi_arg);
case TYPE_SHORT:
return INT2NUM((signed short)retval.fffi_sarg);
case -TYPE_SHORT:
return INT2NUM((unsigned short)retval.fffi_arg);
case TYPE_INT:
return INT2NUM((signed int)retval.fffi_sarg);
case -TYPE_INT:
return UINT2NUM((unsigned int)retval.fffi_arg);
case TYPE_LONG:
return LONG2NUM(retval.slong);
case -TYPE_LONG:
return ULONG2NUM(retval.ulong);
#if HAVE_LONG_LONG
case TYPE_LONG_LONG:
return LL2NUM(retval.slong_long);
case -TYPE_LONG_LONG:
return ULL2NUM(retval.ulong_long);
#endif
case TYPE_FLOAT:
return rb_float_new(retval.ffloat);
case TYPE_DOUBLE:
return rb_float_new(retval.ddouble);
case TYPE_CONST_STRING:
if (retval.pointer) {
return rb_str_new_cstr(retval.pointer);
}
else {
return Qnil;
}
default:
rb_raise(rb_eRuntimeError, "unknown type %d", type);
}
* encoding.c (rb_enc_codepoint_len): Use UNREACHABLE to avoid "control reaches end of non-void function" warnings. [ruby-trunk - Bug #6066] * re.c (name_to_backref_number): ditto. * object.c (rb_Float): ditto. * io.c (io_readpartial): ditto. * io.c (io_read_nonblock): ditto. * pack.c (rb_uv_to_utf8): ditto. * proc.c (rb_method_entry_arity): ditto. * vm_method.c (rb_f_notimplement): ditto. * struct.c (rb_struct_aset_id): ditto. * class.c (rb_scan_args): ditto. * process.c (rlimit_resource_type): ditto. * process.c (rlimit_resource_value): ditto. * process.c (p_uid_switch): ditto. * process.c (p_gid_switch): ditto. * ext/digest/digest.c (rb_digest_instance_update): ditto. * ext/digest/digest.c (rb_digest_instance_finish): ditto. * ext/digest/digest.c (rb_digest_instance_reset): ditto. * ext/digest/digest.c (rb_digest_instance_block_length): ditto. * ext/bigdecimal/bigdecimal.c (BigDecimalCmp): ditto. * ext/dl/handle.c (rb_dlhandle_close): ditto. * ext/tk/tcltklib.c (pending_exception_check0): ditto. * ext/tk/tcltklib.c (pending_exception_check1): ditto. * ext/tk/tcltklib.c (ip_cancel_eval_core): ditto. * ext/tk/tcltklib.c (lib_get_reltype_name): ditto. * ext/tk/tcltklib.c (create_dummy_encoding_for_tk_core): ditto. * ext/tk/tkutil/tkutil.c (tk_hash_kv): ditto. * ext/openssl/ossl_ssl.c (ossl_ssl_session_reused): ditto. * ext/openssl/ossl_pkey_ec.c (ossl_ec_key_dsa_verify_asn1): ditto. * ext/openssl/ossl_pkey_ec.c (ossl_ec_point_is_at_infinit): ditto. * ext/openssl/ossl_pkey_ec.c (ossl_ec_point_is_on_curve): ditto. * ext/fiddle/conversions.c (generic_to_value): ditto. * ext/socket/raddrinfo.c (rsock_io_socket_addrinfo): ditto. * ext/socket/socket.c (sock_s_getnameinfo): ditto. * ext/ripper/eventids2.c (ripper_token2eventid): ditto. * cont.c (return_fiber): ditto. * dmydln.c (dln_load): ditto. * vm_insnhelper.c (vm_search_normal_superclass): ditto. * bignum.c (big_fdiv): ditto. * marshal.c (r_symlink): ditto. * marshal.c (r_symbol): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35321 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2012-04-13 19:45:37 -04:00
UNREACHABLE;
}
VALUE
generic_to_value(VALUE rettype, fiddle_generic retval)
{
return rb_fiddle_generic_to_value(rettype, retval);
}
/* vim: set noet sw=4 sts=4 */