Method: Fiddle::Function#initialize

Defined in:
ext/fiddle/function.c

#new(ptr) ⇒ Object #argsObject #ret_typeObject #abi=(DEFAULT) ⇒ Object #nameObject #need_gvlObject

Constructs a Function object.

  • ptr is a referenced function, of a Fiddle::Handle

  • 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

  • name is the name of the function

  • need_gvl is whether GVL is needed to call the function



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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
# File 'ext/fiddle/function.c', line 121

static VALUE
initialize(int argc, VALUE argv[], VALUE self)
{
    ffi_cif * cif;
    VALUE ptr, arg_types, ret_type, abi, kwargs;
    VALUE name = Qnil;
    VALUE need_gvl = Qfalse;
    int c_ret_type;
    bool is_variadic = false;
    ffi_abi c_ffi_abi;
    void *cfunc;

    rb_scan_args(argc, argv, "31:", &ptr, &arg_types, &ret_type, &abi, &kwargs);
    rb_iv_set(self, "@closure", ptr);

    if (!NIL_P(kwargs)) {
        enum {
            kw_name,
            kw_need_gvl,
            kw_max_,
        };
        static ID kw[kw_max_];
        VALUE args[kw_max_];
        if (!kw[0]) {
            kw[kw_name] = rb_intern_const("name");
            kw[kw_need_gvl] = rb_intern_const("need_gvl");
        }
        rb_get_kwargs(kwargs, kw, 0, kw_max_, args);
        if (args[kw_name] != Qundef) {
            name = args[kw_name];
        }
        if (args[kw_need_gvl] != Qundef) {
            need_gvl = args[kw_need_gvl];
        }
    }
    rb_iv_set(self, "@name", name);
    rb_iv_set(self, "@need_gvl", need_gvl);

    ptr = rb_Integer(ptr);
    cfunc = NUM2PTR(ptr);
    PTR2NUM(cfunc);
    c_ffi_abi = NIL_P(abi) ? FFI_DEFAULT_ABI : NUM2INT(abi);
    abi = INT2FIX(c_ffi_abi);
    ret_type = rb_fiddle_type_ensure(ret_type);
    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");
    }
#endif

    rb_iv_set(self, "@ptr", ptr);
    rb_iv_set(self, "@argument_types", arg_types);
    rb_iv_set(self, "@return_type", ret_type);
    rb_iv_set(self, "@abi", abi);
    rb_iv_set(self, "@is_variadic", is_variadic ? Qtrue : Qfalse);

    TypedData_Get_Struct(self, ffi_cif, &function_data_type, cif);
    cif->arg_types = NULL;

    return self;
}