Class: FFI::VariadicInvoker
- Inherits:
-
Object
- Object
- FFI::VariadicInvoker
- Defined in:
- lib/ffi/variadic.rb,
ext/ffi_c/Variadic.c
Instance Method Summary collapse
- #init(arg_types, type_map) ⇒ Object
- #initialize(rbFunction, rbParameterTypes, rbReturnType, options) ⇒ Object constructor
- #invoke(parameterTypes, parameterValues) ⇒ Object (also: #call)
Constructor Details
#initialize(rbFunction, rbParameterTypes, rbReturnType, options) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'ext/ffi_c/Variadic.c', line 89
static VALUE
variadic_initialize(VALUE self, VALUE rbFunction, VALUE rbParameterTypes, VALUE rbReturnType, VALUE options)
{
VariadicInvoker* invoker = NULL;
VALUE retval = Qnil;
VALUE convention = Qnil;
VALUE fixed = Qnil;
int i;
Check_Type(options, T_HASH);
convention = rb_hash_aref(options, ID2SYM(rb_intern("convention")));
Data_Get_Struct(self, VariadicInvoker, invoker);
invoker->rbEnums = rb_hash_aref(options, ID2SYM(rb_intern("enums")));
invoker->rbAddress = rbFunction;
invoker->function = rbffi_AbstractMemory_Cast(rbFunction, rbffi_PointerClass)->address;
#if defined(_WIN32) || defined(__WIN32__)
invoker->abi = (RTEST(convention) && strcmp(StringValueCStr(convention), "stdcall") == 0)
? FFI_STDCALL : FFI_DEFAULT_ABI;
#else
invoker->abi = FFI_DEFAULT_ABI;
#endif
invoker->rbReturnType = rbffi_Type_Lookup(rbReturnType);
if (!RTEST(invoker->rbReturnType)) {
VALUE typeName = rb_funcall2(rbReturnType, rb_intern("inspect"), 0, NULL);
rb_raise(rb_eTypeError, "Invalid return type (%s)", RSTRING_PTR(typeName));
}
Data_Get_Struct(rbReturnType, Type, invoker->returnType);
invoker->paramCount = -1;
fixed = rb_ary_new2(RARRAY_LEN(rbParameterTypes) - 1);
for (i = 0; i < RARRAY_LEN(rbParameterTypes); ++i) {
VALUE entry = rb_ary_entry(rbParameterTypes, i);
VALUE rbType = rbffi_Type_Lookup(entry);
Type* type;
if (!RTEST(rbType)) {
VALUE typeName = rb_funcall2(entry, rb_intern("inspect"), 0, NULL);
rb_raise(rb_eTypeError, "Invalid parameter type (%s)", RSTRING_PTR(typeName));
}
Data_Get_Struct(rbType, Type, type);
if (type->nativeType != NATIVE_VARARGS) {
rb_ary_push(fixed, entry);
}
}
/*
* @fixed and @type_map are used by the parameter mangling ruby code
*/
rb_iv_set(self, "@fixed", fixed);
rb_iv_set(self, "@type_map", rb_hash_aref(options, ID2SYM(rb_intern("type_map"))));
return retval;
}
|
Instance Method Details
#init(arg_types, type_map) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/ffi/variadic.rb', line 3 def init(arg_types, type_map) @fixed = Array.new @type_map = type_map arg_types.each_with_index do |type, i| @fixed << type unless type == FFI::NativeType::VARARGS end end |
#invoke(parameterTypes, parameterValues) ⇒ Object Also known as: call
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'ext/ffi_c/Variadic.c', line 164
static VALUE
variadic_call(VALUE self, VALUE parameterTypes, VALUE parameterValues)
{
VariadicInvoker* invoker;
FFIStorage* params;
ffi_cif cif;
void** ffiValues;
ffi_type** ffiParamTypes;
ffi_type* ffiReturnType;
NativeType* paramTypes;
VALUE* argv;
int paramCount = 0, i;
ffi_status ffiStatus;
Check_Type(parameterTypes, T_ARRAY);
Check_Type(parameterValues, T_ARRAY);
Data_Get_Struct(self, VariadicInvoker, invoker);
paramCount = RARRAY_LEN(parameterTypes);
paramTypes = ALLOCA_N(NativeType, paramCount);
ffiParamTypes = ALLOCA_N(ffi_type *, paramCount);
params = ALLOCA_N(FFIStorage, paramCount);
ffiValues = ALLOCA_N(void*, paramCount);
argv = ALLOCA_N(VALUE, paramCount);
for (i = 0; i < paramCount; ++i) {
VALUE entry = rb_ary_entry(parameterTypes, i);
int paramType = rbffi_Type_GetIntValue(entry);
Type* type;
Data_Get_Struct(entry, Type, type);
switch (paramType) {
case NATIVE_INT8:
case NATIVE_INT16:
case NATIVE_INT32:
case NATIVE_ENUM:
paramType = NATIVE_INT32;
ffiParamTypes[i] = &ffi_type_sint;
break;
case NATIVE_UINT8:
case NATIVE_UINT16:
case NATIVE_UINT32:
paramType = NATIVE_UINT32;
ffiParamTypes[i] = &ffi_type_uint;
break;
case NATIVE_FLOAT32:
paramType = NATIVE_FLOAT64;
ffiParamTypes[i] = &ffi_type_double;
break;
default:
ffiParamTypes[i] = type->ffiType;
break;
}
paramTypes[i] = paramType;
if (ffiParamTypes[i] == NULL) {
rb_raise(rb_eArgError, "Invalid parameter type #%x", paramType);
}
argv[i] = rb_ary_entry(parameterValues, i);
}
ffiReturnType = invoker->returnType->ffiType;
if (ffiReturnType == NULL) {
rb_raise(rb_eArgError, "Invalid return type");
}
ffiStatus = ffi_prep_cif(&cif, invoker->abi, paramCount, ffiReturnType, ffiParamTypes);
switch (ffiStatus) {
case FFI_BAD_ABI:
rb_raise(rb_eArgError, "Invalid ABI specified");
case FFI_BAD_TYPEDEF:
rb_raise(rb_eArgError, "Invalid argument type specified");
case FFI_OK:
break;
default:
rb_raise(rb_eArgError, "Unknown FFI error");
}
rbffi_SetupCallParams(paramCount, argv, -1, paramTypes, params,
ffiValues, NULL, 0, invoker->rbEnums);
return ffi_invoke(&cif, invoker->function, invoker->returnType,
ffiValues, invoker->rbReturnType, invoker->rbEnums);
}
|