2010-05-06 02:59:24 -04:00
|
|
|
#include <fiddle.h>
|
2015-11-23 16:20:56 -05:00
|
|
|
#include <ruby/thread.h>
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2020-06-26 18:27:05 -04:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
2014-01-15 03:16:44 -05:00
|
|
|
#ifdef PRIsVALUE
|
|
|
|
# define RB_OBJ_CLASSNAME(obj) rb_obj_class(obj)
|
|
|
|
# define RB_OBJ_STRING(obj) (obj)
|
|
|
|
#else
|
|
|
|
# define PRIsVALUE "s"
|
|
|
|
# define RB_OBJ_CLASSNAME(obj) rb_obj_classname(obj)
|
|
|
|
# define RB_OBJ_STRING(obj) StringValueCStr(obj)
|
|
|
|
#endif
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
VALUE cFiddleFunction;
|
|
|
|
|
2014-03-08 23:51:17 -05:00
|
|
|
#define MAX_ARGS (SIZE_MAX / (sizeof(void *) + sizeof(fiddle_generic)) - 1)
|
|
|
|
|
|
|
|
#define Check_Max_Args(name, len) \
|
2015-12-13 04:35:58 -05:00
|
|
|
Check_Max_Args_(name, len, "")
|
|
|
|
#define Check_Max_Args_Long(name, len) \
|
|
|
|
Check_Max_Args_(name, len, "l")
|
|
|
|
#define Check_Max_Args_(name, len, fmt) \
|
2020-06-26 18:10:37 -04:00
|
|
|
do { \
|
|
|
|
if ((size_t)(len) >= MAX_ARGS) { \
|
|
|
|
rb_raise(rb_eTypeError, \
|
|
|
|
"%s is so large " \
|
|
|
|
"that it can cause integer overflow (%"fmt"d)", \
|
|
|
|
(name), (len)); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2014-03-08 23:51:17 -05:00
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
static void
|
|
|
|
deallocate(void *p)
|
|
|
|
{
|
2020-06-26 18:11:19 -04:00
|
|
|
ffi_cif *cif = p;
|
|
|
|
if (cif->arg_types) xfree(cif->arg_types);
|
|
|
|
xfree(cif);
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
function_memsize(const void *p)
|
|
|
|
{
|
|
|
|
/* const */ffi_cif *ptr = (ffi_cif *)p;
|
|
|
|
size_t size = 0;
|
|
|
|
|
2015-12-08 20:01:17 -05:00
|
|
|
size += sizeof(*ptr);
|
2010-05-06 02:59:24 -04:00
|
|
|
#if !defined(FFI_NO_RAW_API) || !FFI_NO_RAW_API
|
2015-12-08 20:01:17 -05:00
|
|
|
size += ffi_raw_size(ptr);
|
2010-05-06 02:59:24 -04:00
|
|
|
#endif
|
2015-12-08 20:01:17 -05:00
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
const rb_data_type_t function_data_type = {
|
|
|
|
"fiddle/function",
|
2010-07-20 05:23:08 -04:00
|
|
|
{0, deallocate, function_memsize,},
|
2010-05-06 02:59:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
allocate(VALUE klass)
|
|
|
|
{
|
|
|
|
ffi_cif * cif;
|
|
|
|
|
|
|
|
return TypedData_Make_Struct(klass, ffi_cif, &function_data_type, cif);
|
|
|
|
}
|
|
|
|
|
2012-11-27 14:54:50 -05:00
|
|
|
VALUE
|
|
|
|
rb_fiddle_new_function(VALUE address, VALUE arg_types, VALUE ret_type)
|
|
|
|
{
|
|
|
|
VALUE argv[3];
|
|
|
|
|
|
|
|
argv[0] = address;
|
|
|
|
argv[1] = arg_types;
|
|
|
|
argv[2] = ret_type;
|
|
|
|
|
|
|
|
return rb_class_new_instance(3, argv, cFiddleFunction);
|
|
|
|
}
|
|
|
|
|
2012-12-11 05:41:00 -05:00
|
|
|
static int
|
|
|
|
parse_keyword_arg_i(VALUE key, VALUE value, VALUE self)
|
|
|
|
{
|
2014-01-15 03:16:32 -05:00
|
|
|
if (key == ID2SYM(rb_intern("name"))) {
|
|
|
|
rb_iv_set(self, "@name", value);
|
|
|
|
} else {
|
2014-01-15 03:16:44 -05:00
|
|
|
rb_raise(rb_eArgError, "unknown keyword: %"PRIsVALUE,
|
|
|
|
RB_OBJ_STRING(key));
|
2014-01-15 03:16:32 -05:00
|
|
|
}
|
|
|
|
return ST_CONTINUE;
|
2012-12-11 05:41:00 -05:00
|
|
|
}
|
|
|
|
|
2020-06-26 18:25:47 -04:00
|
|
|
static VALUE
|
|
|
|
normalize_argument_types(const char *name,
|
|
|
|
VALUE arg_types,
|
|
|
|
bool *is_variadic)
|
|
|
|
{
|
|
|
|
VALUE normalized_arg_types;
|
|
|
|
int i;
|
|
|
|
int n_arg_types;
|
|
|
|
*is_variadic = false;
|
|
|
|
|
|
|
|
Check_Type(arg_types, T_ARRAY);
|
|
|
|
n_arg_types = RARRAY_LENINT(arg_types);
|
|
|
|
Check_Max_Args(name, n_arg_types);
|
|
|
|
|
|
|
|
normalized_arg_types = rb_ary_new_capa(n_arg_types);
|
|
|
|
for (i = 0; i < n_arg_types; i++) {
|
|
|
|
VALUE arg_type = RARRAY_AREF(arg_types, i);
|
2020-07-09 08:39:51 -04:00
|
|
|
int c_arg_type;
|
|
|
|
arg_type = rb_fiddle_type_ensure(arg_type);
|
|
|
|
c_arg_type = NUM2INT(arg_type);
|
2020-06-26 18:25:47 -04:00
|
|
|
if (c_arg_type == TYPE_VARIADIC) {
|
|
|
|
if (i != n_arg_types - 1) {
|
|
|
|
rb_raise(rb_eArgError,
|
|
|
|
"Fiddle::TYPE_VARIADIC must be the last argument type: "
|
|
|
|
"%"PRIsVALUE,
|
|
|
|
arg_types);
|
|
|
|
}
|
|
|
|
*is_variadic = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
(void)INT2FFI_TYPE(c_arg_type); /* raise */
|
|
|
|
}
|
|
|
|
rb_ary_push(normalized_arg_types, INT2FIX(c_arg_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* freeze to prevent inconsistency at calling #to_int later */
|
|
|
|
OBJ_FREEZE(normalized_arg_types);
|
|
|
|
return normalized_arg_types;
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
static VALUE
|
|
|
|
initialize(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
|
|
|
ffi_cif * cif;
|
2020-06-26 18:25:47 -04:00
|
|
|
VALUE ptr, arg_types, ret_type, abi, kwds;
|
|
|
|
int c_ret_type;
|
|
|
|
bool is_variadic = false;
|
|
|
|
ffi_abi c_ffi_abi;
|
2015-12-13 04:35:58 -05:00
|
|
|
void *cfunc;
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2020-06-26 18:25:47 -04:00
|
|
|
rb_scan_args(argc, argv, "31:", &ptr, &arg_types, &ret_type, &abi, &kwds);
|
2019-01-09 19:22:26 -05:00
|
|
|
rb_iv_set(self, "@closure", ptr);
|
|
|
|
|
2015-12-13 04:35:58 -05:00
|
|
|
ptr = rb_Integer(ptr);
|
|
|
|
cfunc = NUM2PTR(ptr);
|
|
|
|
PTR2NUM(cfunc);
|
2020-06-26 18:25:47 -04:00
|
|
|
c_ffi_abi = NIL_P(abi) ? FFI_DEFAULT_ABI : NUM2INT(abi);
|
|
|
|
abi = INT2FIX(c_ffi_abi);
|
2020-07-09 08:39:51 -04:00
|
|
|
ret_type = rb_fiddle_type_ensure(ret_type);
|
2020-06-26 18:25:47 -04:00
|
|
|
c_ret_type = NUM2INT(ret_type);
|
|
|
|
(void)INT2FFI_TYPE(c_ret_type); /* raise */
|
|
|
|
ret_type = INT2FIX(c_ret_type);
|
|
|
|
|
|
|
|
arg_types = normalize_argument_types("argument types",
|
|
|
|
arg_types,
|
|
|
|
&is_variadic);
|
|
|
|
#ifndef HAVE_FFI_PREP_CIF_VAR
|
|
|
|
if (is_variadic) {
|
|
|
|
rb_raise(rb_eNotImpError,
|
|
|
|
"ffi_prep_cif_var() is required in libffi "
|
|
|
|
"for variadic arguments");
|
2015-12-13 04:35:58 -05:00
|
|
|
}
|
2020-06-26 18:25:47 -04:00
|
|
|
#endif
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
rb_iv_set(self, "@ptr", ptr);
|
2020-06-26 18:25:47 -04:00
|
|
|
rb_iv_set(self, "@argument_types", arg_types);
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_iv_set(self, "@return_type", ret_type);
|
|
|
|
rb_iv_set(self, "@abi", abi);
|
2020-06-26 18:25:47 -04:00
|
|
|
rb_iv_set(self, "@is_variadic", is_variadic ? Qtrue : Qfalse);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2012-12-11 05:41:00 -05:00
|
|
|
if (!NIL_P(kwds)) rb_hash_foreach(kwds, parse_keyword_arg_i, self);
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif);
|
2020-06-26 18:25:47 -04:00
|
|
|
cif->arg_types = NULL;
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
struct nogvl_ffi_call_args {
|
|
|
|
ffi_cif *cif;
|
|
|
|
void (*fn)(void);
|
|
|
|
void **values;
|
|
|
|
fiddle_generic retval;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void *
|
|
|
|
nogvl_ffi_call(void *ptr)
|
|
|
|
{
|
|
|
|
struct nogvl_ffi_call_args *args = ptr;
|
|
|
|
|
|
|
|
ffi_call(args->cif, args->fn, &args->retval, args->values);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-05-06 02:59:24 -04:00
|
|
|
static VALUE
|
|
|
|
function_call(int argc, VALUE argv[], VALUE self)
|
|
|
|
{
|
2015-11-23 16:20:56 -05:00
|
|
|
struct nogvl_ffi_call_args args = { 0 };
|
2010-05-06 02:59:24 -04:00
|
|
|
fiddle_generic *generic_args;
|
2020-06-26 18:25:47 -04:00
|
|
|
VALUE cfunc;
|
|
|
|
VALUE abi;
|
|
|
|
VALUE arg_types;
|
|
|
|
VALUE cPointer;
|
|
|
|
VALUE is_variadic;
|
|
|
|
int n_arg_types;
|
|
|
|
int n_fixed_args = 0;
|
|
|
|
int n_call_args = 0;
|
2010-05-06 02:59:24 -04:00
|
|
|
int i;
|
2020-06-26 18:25:47 -04:00
|
|
|
int i_call;
|
2020-07-09 07:19:20 -04:00
|
|
|
VALUE converted_args = Qnil;
|
2014-03-08 23:51:38 -05:00
|
|
|
VALUE alloc_buffer = 0;
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
cfunc = rb_iv_get(self, "@ptr");
|
2020-06-26 18:25:47 -04:00
|
|
|
abi = rb_iv_get(self, "@abi");
|
|
|
|
arg_types = rb_iv_get(self, "@argument_types");
|
2010-05-06 02:59:24 -04:00
|
|
|
cPointer = rb_const_get(mFiddle, rb_intern("Pointer"));
|
2020-06-26 18:25:47 -04:00
|
|
|
is_variadic = rb_iv_get(self, "@is_variadic");
|
|
|
|
|
|
|
|
n_arg_types = RARRAY_LENINT(arg_types);
|
|
|
|
n_fixed_args = n_arg_types;
|
|
|
|
if (RTEST(is_variadic)) {
|
|
|
|
if (argc < n_arg_types) {
|
|
|
|
rb_error_arity(argc, n_arg_types, UNLIMITED_ARGUMENTS);
|
|
|
|
}
|
|
|
|
if (((argc - n_arg_types) % 2) != 0) {
|
|
|
|
rb_raise(rb_eArgError,
|
|
|
|
"variadic arguments must be type and value pairs: "
|
|
|
|
"%"PRIsVALUE,
|
|
|
|
rb_ary_new_from_values(argc, argv));
|
|
|
|
}
|
|
|
|
n_call_args = n_arg_types + ((argc - n_arg_types) / 2);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (argc != n_arg_types) {
|
|
|
|
rb_error_arity(argc, n_arg_types, n_arg_types);
|
|
|
|
}
|
|
|
|
n_call_args = n_arg_types;
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
2020-06-26 18:25:47 -04:00
|
|
|
Check_Max_Args("the number of arguments", n_call_args);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
TypedData_Get_Struct(self, ffi_cif, &function_data_type, args.cif);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2020-06-26 18:25:47 -04:00
|
|
|
if (is_variadic && args.cif->arg_types) {
|
|
|
|
xfree(args.cif->arg_types);
|
|
|
|
args.cif->arg_types = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!args.cif->arg_types) {
|
|
|
|
VALUE fixed_arg_types = arg_types;
|
|
|
|
VALUE return_type;
|
|
|
|
int c_return_type;
|
|
|
|
ffi_type *ffi_return_type;
|
|
|
|
ffi_type **ffi_arg_types;
|
|
|
|
ffi_status result;
|
|
|
|
|
|
|
|
arg_types = rb_ary_dup(fixed_arg_types);
|
|
|
|
for (i = n_fixed_args; i < argc; i += 2) {
|
|
|
|
VALUE arg_type = argv[i];
|
2020-07-09 08:39:51 -04:00
|
|
|
int c_arg_type;
|
|
|
|
arg_type = rb_fiddle_type_ensure(arg_type);
|
|
|
|
c_arg_type = NUM2INT(arg_type);
|
2020-06-26 18:25:47 -04:00
|
|
|
(void)INT2FFI_TYPE(c_arg_type); /* raise */
|
|
|
|
rb_ary_push(arg_types, INT2FIX(c_arg_type));
|
|
|
|
}
|
|
|
|
|
|
|
|
return_type = rb_iv_get(self, "@return_type");
|
|
|
|
c_return_type = FIX2INT(return_type);
|
|
|
|
ffi_return_type = INT2FFI_TYPE(c_return_type);
|
|
|
|
|
|
|
|
ffi_arg_types = xcalloc(n_call_args + 1, sizeof(ffi_type *));
|
|
|
|
for (i_call = 0; i_call < n_call_args; i_call++) {
|
|
|
|
VALUE arg_type;
|
|
|
|
int c_arg_type;
|
|
|
|
arg_type = RARRAY_AREF(arg_types, i_call);
|
|
|
|
c_arg_type = FIX2INT(arg_type);
|
|
|
|
ffi_arg_types[i_call] = INT2FFI_TYPE(c_arg_type);
|
|
|
|
}
|
|
|
|
ffi_arg_types[i_call] = NULL;
|
|
|
|
|
|
|
|
if (is_variadic) {
|
|
|
|
#ifdef HAVE_FFI_PREP_CIF_VAR
|
|
|
|
result = ffi_prep_cif_var(args.cif,
|
|
|
|
FIX2INT(abi),
|
|
|
|
n_fixed_args,
|
|
|
|
n_call_args,
|
|
|
|
ffi_return_type,
|
|
|
|
ffi_arg_types);
|
|
|
|
#else
|
|
|
|
/* This code is never used because ffi_prep_cif_var()
|
|
|
|
* availability check is done in #initialize. */
|
|
|
|
result = FFI_BAD_TYPEDEF;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = ffi_prep_cif(args.cif,
|
|
|
|
FIX2INT(abi),
|
|
|
|
n_call_args,
|
|
|
|
ffi_return_type,
|
|
|
|
ffi_arg_types);
|
|
|
|
}
|
|
|
|
if (result != FFI_OK) {
|
|
|
|
xfree(ffi_arg_types);
|
|
|
|
args.cif->arg_types = NULL;
|
|
|
|
rb_raise(rb_eRuntimeError, "error creating CIF %d", result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-08 23:51:38 -05:00
|
|
|
generic_args = ALLOCV(alloc_buffer,
|
2020-06-26 18:25:47 -04:00
|
|
|
sizeof(fiddle_generic) * n_call_args +
|
|
|
|
sizeof(void *) * (n_call_args + 1));
|
2015-11-23 16:20:56 -05:00
|
|
|
args.values = (void **)((char *)generic_args +
|
2020-06-26 18:25:47 -04:00
|
|
|
sizeof(fiddle_generic) * n_call_args);
|
|
|
|
|
|
|
|
for (i = 0, i_call = 0;
|
|
|
|
i < argc && i_call < n_call_args;
|
|
|
|
i++, i_call++) {
|
|
|
|
VALUE arg_type;
|
|
|
|
int c_arg_type;
|
2020-07-09 07:19:20 -04:00
|
|
|
VALUE original_src;
|
2020-06-26 18:25:47 -04:00
|
|
|
VALUE src;
|
|
|
|
arg_type = RARRAY_AREF(arg_types, i_call);
|
|
|
|
c_arg_type = FIX2INT(arg_type);
|
|
|
|
if (i >= n_fixed_args) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
src = argv[i];
|
|
|
|
|
|
|
|
if (c_arg_type == TYPE_VOIDP) {
|
|
|
|
if (NIL_P(src)) {
|
|
|
|
src = INT2FIX(0);
|
|
|
|
}
|
|
|
|
else if (cPointer != CLASS_OF(src)) {
|
|
|
|
src = rb_funcall(cPointer, rb_intern("[]"), 1, src);
|
2020-07-09 07:19:20 -04:00
|
|
|
if (NIL_P(converted_args)) {
|
|
|
|
converted_args = rb_ary_new();
|
|
|
|
}
|
|
|
|
rb_ary_push(converted_args, src);
|
2020-06-26 18:25:47 -04:00
|
|
|
}
|
|
|
|
src = rb_Integer(src);
|
|
|
|
}
|
|
|
|
|
2020-07-09 07:19:20 -04:00
|
|
|
original_src = src;
|
2020-06-26 18:25:47 -04:00
|
|
|
VALUE2GENERIC(c_arg_type, src, &generic_args[i_call]);
|
2020-07-09 07:19:20 -04:00
|
|
|
if (src != original_src) {
|
|
|
|
if (NIL_P(converted_args)) {
|
|
|
|
converted_args = rb_ary_new();
|
|
|
|
}
|
|
|
|
rb_ary_push(converted_args, src);
|
|
|
|
}
|
2020-06-26 18:25:47 -04:00
|
|
|
args.values[i_call] = (void *)&generic_args[i_call];
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
2020-06-26 18:25:47 -04:00
|
|
|
args.values[i_call] = NULL;
|
2018-02-16 03:49:09 -05:00
|
|
|
args.fn = (void(*)(void))NUM2PTR(cfunc);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
(void)rb_thread_call_without_gvl(nogvl_ffi_call, &args, 0, 0);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
|
|
|
rb_funcall(mFiddle, rb_intern("last_error="), 1, INT2NUM(errno));
|
2011-01-04 22:13:18 -05:00
|
|
|
#if defined(_WIN32)
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_funcall(mFiddle, rb_intern("win32_last_error="), 1, INT2NUM(errno));
|
|
|
|
#endif
|
|
|
|
|
2014-03-08 23:51:38 -05:00
|
|
|
ALLOCV_END(alloc_buffer);
|
2010-05-06 02:59:24 -04:00
|
|
|
|
2015-11-23 16:20:56 -05:00
|
|
|
return GENERIC2VALUE(rb_iv_get(self, "@return_type"), args.retval);
|
2010-05-06 02:59:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Init_fiddle_function(void)
|
|
|
|
{
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-class: Fiddle::Function
|
|
|
|
*
|
|
|
|
* == Description
|
|
|
|
*
|
|
|
|
* A representation of a C function
|
|
|
|
*
|
|
|
|
* == Examples
|
|
|
|
*
|
|
|
|
* === 'strcpy'
|
|
|
|
*
|
2012-11-27 18:26:07 -05:00
|
|
|
* @libc = Fiddle.dlopen "/lib/libc.so.6"
|
|
|
|
* #=> #<Fiddle::Handle:0x00000001d7a8d8>
|
|
|
|
* f = Fiddle::Function.new(
|
|
|
|
* @libc['strcpy'],
|
|
|
|
* [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOIDP],
|
|
|
|
* Fiddle::TYPE_VOIDP)
|
|
|
|
* #=> #<Fiddle::Function:0x00000001d8ee00>
|
2011-08-15 19:48:59 -04:00
|
|
|
* buff = "000"
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> "000"
|
2011-08-15 19:48:59 -04:00
|
|
|
* str = f.call(buff, "123")
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> #<Fiddle::Pointer:0x00000001d0c380 ptr=0x000000018a21b8 size=0 free=0x00000000000000>
|
2011-08-15 19:48:59 -04:00
|
|
|
* str.to_s
|
|
|
|
* => "123"
|
|
|
|
*
|
|
|
|
* === ABI check
|
|
|
|
*
|
2014-10-31 17:13:09 -04:00
|
|
|
* @libc = Fiddle.dlopen "/lib/libc.so.6"
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> #<Fiddle::Handle:0x00000001d7a8d8>
|
2011-08-15 19:48:59 -04:00
|
|
|
* f = Fiddle::Function.new(@libc['strcpy'], [TYPE_VOIDP, TYPE_VOIDP], TYPE_VOIDP)
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> #<Fiddle::Function:0x00000001d8ee00>
|
2011-08-15 19:48:59 -04:00
|
|
|
* f.abi == Fiddle::Function::DEFAULT
|
2012-11-27 18:26:07 -05:00
|
|
|
* #=> true
|
2011-08-15 19:48:59 -04:00
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
cFiddleFunction = rb_define_class_under(mFiddle, "Function", rb_cObject);
|
|
|
|
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-const: DEFAULT
|
|
|
|
*
|
|
|
|
* Default ABI
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_const(cFiddleFunction, "DEFAULT", INT2NUM(FFI_DEFAULT_ABI));
|
|
|
|
|
2012-11-30 10:57:21 -05:00
|
|
|
#ifdef HAVE_CONST_FFI_STDCALL
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-const: STDCALL
|
|
|
|
*
|
|
|
|
* FFI implementation of WIN32 stdcall convention
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_const(cFiddleFunction, "STDCALL", INT2NUM(FFI_STDCALL));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rb_define_alloc_func(cFiddleFunction, allocate);
|
|
|
|
|
2011-08-15 19:48:59 -04:00
|
|
|
/*
|
|
|
|
* Document-method: call
|
|
|
|
*
|
2015-12-11 02:43:09 -05:00
|
|
|
* Calls the constructed Function, with +args+.
|
|
|
|
* Caller must ensure the underlying function is called in a
|
|
|
|
* thread-safe manner if running in a multi-threaded process.
|
2011-08-15 19:48:59 -04:00
|
|
|
*
|
|
|
|
* For an example see Fiddle::Function
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_method(cFiddleFunction, "call", function_call, -1);
|
2011-08-15 19:48:59 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Document-method: new
|
2012-12-06 23:35:43 -05:00
|
|
|
* call-seq: new(ptr, args, ret_type, abi = DEFAULT)
|
2011-08-15 19:48:59 -04:00
|
|
|
*
|
|
|
|
* Constructs a Function object.
|
2012-11-27 18:26:07 -05:00
|
|
|
* * +ptr+ is a referenced function, of a Fiddle::Handle
|
2011-08-15 19:48:59 -04:00
|
|
|
* * +args+ is an Array of arguments, passed to the +ptr+ function
|
|
|
|
* * +ret_type+ is the return type of the function
|
|
|
|
* * +abi+ is the ABI of the function
|
|
|
|
*
|
|
|
|
*/
|
2010-05-06 02:59:24 -04:00
|
|
|
rb_define_method(cFiddleFunction, "initialize", initialize, -1);
|
|
|
|
}
|
|
|
|
/* vim: set noet sws=4 sw=4: */
|