GNOME Bugzilla – Bug 593245
Support input arrays of float and double
Last modified: 2016-11-29 05:41:30 UTC
Convert JavaScript arrays to C float/double.
Created attachment 141814 [details] [review] Support input arrays of float and double
static JSBool +gjs_array_to_floatarray(JSContext *context, + jsval array_value, + unsigned int length, + void **arr_p, + gboolean is_double) Havoc would say boolean arguments to functions are evil, maybe you could pass the type tag here instead? + if (!JS_GetElement(context, JSVAL_TO_OBJECT(array_value), + i, &elem)) { + g_free(result); + gjs_throw(context, + "Missing array element %u", + i); + return JS_FALSE; + } + + if (!JS_ValueToNumber(context, elem, &value)) { + g_free(result); + gjs_throw(context, + "Invalid element in string array"); + return JS_FALSE; + } + I think JS_ValueToNumber and JS_GetElement would already have thrown an exception if they return FALSE, so you don't need to throw another one. Looks good to me otherwise
Hey, Wanting to try to use Cogl instrospection data with Gjs, I've written a small patch to support float/double arrays arguments. it's only *after* doing so that I realized that someone could have already done it and indeed someone had. Stupid me. As it's done, I'm just adding it here, a few notes: * I chose to have 2 separate functions to handle float and double arrays. * JS_GetElement() and JS_ValueToNumber() documentation state: "If the search fails with an error or exception, JS_GetElement returns JS_FALSE". gjs_throw() checks if there's a pending exception before throwing a new one. * There's a small typo in the "Invalid element in string array", it should be float/double array * I've added an exception in the case we are trying to convert an array to a float array with numbers that just don't fit in a float.
Created attachment 143583 [details] [review] add float/double arrays in gi
It seems Giovanni committed this a long time ago.