Skip to content

Commit 91fe0d4

Browse files
committed
unix: Fix modffi to be able to return double on x86 machines.
1 parent 03281b3 commit 91fe0d4

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

unix/modffi.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,19 @@ mp_obj_t ffifunc_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const
334334
valueptrs[i] = &values[i];
335335
}
336336

337-
ffi_arg retval;
338-
ffi_call(&self->cif, self->func, &retval, valueptrs);
339-
return return_ffi_value(retval, self->rettype);
337+
// If ffi_arg is not big enough to hold a double, then we must pass along a
338+
// pointer to a memory location of the correct size.
339+
// TODO check if this needs to be done for other types which don't fit into
340+
// ffi_arg.
341+
if (sizeof(ffi_arg) == 4 && self->rettype == 'd') {
342+
double retval;
343+
ffi_call(&self->cif, self->func, &retval, valueptrs);
344+
return mp_obj_new_float(retval);
345+
} else {
346+
ffi_arg retval;
347+
ffi_call(&self->cif, self->func, &retval, valueptrs);
348+
return return_ffi_value(retval, self->rettype);
349+
}
340350

341351
error:
342352
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "Don't know how to pass object to native function"));

0 commit comments

Comments
 (0)