I have done a bit of work with the luci lua interface to ubus and I have found the following issues The libubus-lua library does the following with the ubus types NOTE: Usually the only digit type in Lua is "number" which is represented as a double float, some lua versions do support integers
Ubus method | Ubus Object Type | Lua Type | Comments | blobmesg_add_u8() | BLOBMSG_TYPE_INT8 | Boolean | False if 0, otherwise True | blobmesg_add_u16() | BLOBMSG_TYPE_INT16 | Number | Treated as an c integer | blobmesg_add_u32() | BLOBMSG_TYPE_INT32 | Number | Treated as an c integer | blobmesg_add_u64() | BLOBMSG_TYPE_INT64 | Number | The INT64 type is treated as if it was a double float | blobmesg_add_string() | BLOBMSG_TYPE_STRING | String | As expected
| blobmesg_add_double() | BLOBMSG_TYPE_DOUBLE | nil | Surprisingly this is not supported because numbers are usually represent as double float in Lua. I expected this to be the same a INT64, buts its not | | | | |
When converting back Lua values are mapped as shown below: NOTE: there is no double returned. In my view it would have been better to return numbers as doubles
Lua Type | Ubus Object type | Comments | Number | BLOBMSG_TYPE_INT32 | Although most numbers are stored a double floats in Lua they are returned as an integer in BLOBMSG_TYPE_INT32. This means that they are rounded in an unpredictable manner. I expected that a number would be returned in a BLOBMSG_TYPE_DOUBLE, but its not | String | BLOBMSG_TYPE_STRING | As expected
| Boolean | BLOBMSG_TYPE_INT8 | true => 1, false => 0. There is no boolean in c, this returns an unsigned char | | | |
Since the library only allows me to pass floats via INT64, and I can't return them, and I can't modify the library without breaking other utilities that rely on its "broken"/missing functionality I have decided to pass all numbers from Lua to C as strings and then do the conversion to numbers in the receiving system. This is convenient and reasonably straight forward in C with the ati() and atof() functions. I am also doing the same when passing from c to Lua with the tonumber() function
|