Method: Fiddle::Closure#initialize
- Defined in:
- ext/fiddle/closure.c
#initialize(argv[], self) ⇒ Object
call-seq: new(ret, args, abi = Fiddle::DEFAULT)
Construct a new Closure object.
-
retis the C type to be returned -
argsis an Array of arguments, passed to the callback function -
abiis the abi of the closure
If there is an error in preparing the ffi_cif or ffi_prep_closure, then a RuntimeError will be raised.
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'ext/fiddle/closure.c', line 227 static VALUE initialize(int , VALUE argv[], VALUE self) { VALUE ret; VALUE args; VALUE normalized_args; VALUE abi; fiddle_closure * cl; ffi_cif * cif; ffi_closure *pcl; ffi_status result; int i, argc; if (2 == rb_scan_args(, argv, "21", &ret, &args, &abi)) abi = INT2NUM(FFI_DEFAULT_ABI); Check_Type(args, T_ARRAY); argc = RARRAY_LENINT(args); TypedData_Get_Struct(self, fiddle_closure, &closure_data_type, cl); cl->argv = (ffi_type **)xcalloc(argc + 1, sizeof(ffi_type *)); normalized_args = rb_ary_new_capa(argc); for (i = 0; i < argc; i++) { VALUE arg = rb_fiddle_type_ensure(RARRAY_AREF(args, i)); rb_ary_push(normalized_args, arg); cl->argv[i] = rb_fiddle_int_to_ffi_type(NUM2INT(arg)); } cl->argv[argc] = NULL; ret = rb_fiddle_type_ensure(ret); rb_iv_set(self, "@ctype", ret); rb_iv_set(self, "@args", normalized_args); cif = &cl->cif; pcl = cl->pcl; result = ffi_prep_cif(cif, NUM2INT(abi), argc, rb_fiddle_int_to_ffi_type(NUM2INT(ret)), cl->argv); if (FFI_OK != result) rb_raise(rb_eRuntimeError, "error prepping CIF %d", result); #if USE_FFI_CLOSURE_ALLOC result = ffi_prep_closure_loc(pcl, cif, callback, (void *)self, cl->code); #else result = ffi_prep_closure(pcl, cif, callback, (void *)self); cl->code = (void *)pcl; i = mprotect(pcl, sizeof(*pcl), PROT_READ | PROT_EXEC); if (i) { rb_sys_fail("mprotect"); } #endif if (FFI_OK != result) rb_raise(rb_eRuntimeError, "error prepping closure %d", result); return self; } |