-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Review all uses of lua numbers to determine if they ought to be integers #3219
Comments
Is this behavior because of same reasons? Example code: data = {
mixed_data = node.chipid()..'/data',
mixed_status = node.chipid()..'/status',
integer = 500,
integer2 = 1000,
boolean_f = false,
boolean_t = true,
}
print('Integer: '..tostring(data.integer)..' boolean: '..tostring(data.boolean_f))
sjson.encode(data)
ok, json = pcall(sjson.encode, data)
json_obj = sjson.decode(json)
print(type(json_obj))
for k, v in pairs(json_obj) do print(k..' = '..tostring(v)..',') end Result:
I'm using Edit: forgot to write that it's on dev 5.3 |
|
Yes, now numbers are represented correctly. Not sure why string order is changing, not a problem for me (for config file) but possibly could be issue for someone who will be parsing json string with fixed position (like parsing object in Node-Red and adding values to array renaming them). Thank you |
The order of elements in a Json object are not defined. Some systems have
ways of forcing alphabetical ordering, but not nodemcu.
…On Sun, Jul 19, 2020, 09:51 Modestas Bunokas ***@***.***> wrote:
@KT819GM <https://github.com/KT819GM> Take a look at #3222
<#3222> -- this should
fix your problem.
Yes, now numbers are represented correctly. Not sure why string order is
changing, not a problem for me (for config file) but possibly could be
issue for someone who will be parsing json string with fixed position (like
parsing object in Node-Red and adding values to array renaming them). Thank
you
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3219 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALQLTMFUWDN7QV4I2T5DLLR4L25PANCNFSM4O5KW77A>
.
|
Couldn't be more clear. Thank you |
The C API defines the following number and integer functions. The former use type
Note that:
I recommend that:
I will post a fully code analysis next, but I have to be sociable to visitors for the next few hours. |
Slight change of subjectWhen I 've been going through this code, I find myself slipping into #1028, in that the way that we've coded up much of the parameter validation is just so sloppy; this can be done faster, generated less code, and also tighter. A typical coding pattern is lua_getfield(L, 1, "start_ch");
if (!lua_isnil(L, -1)){
if(lua_isnumber(L, -1)){
start_ch = (uint8)luaL_checkinteger(L, -1);
luaL_argcheck(L, (start_ch >= 1 && start_ch <= 14), 1, "start_ch: Range:1-14");
cfg.schan = start_ch;
}
else
luaL_argerror(L, 1, "start_ch: must be a number");
}
else
cfg.schan = 1; "Some parameter is an integer in the range x to y; either with no default or with a specific default" is a really common coding pattern in our modules, and this is coded "long hand" in about 4 different ways. So in this case omitting #define luaL_opt(L,f,n,d) (lua_isnoneornil(L,(n)) ? (d) : f(L,(n))) Lua itself give some good templates, for example in lua_Integer pos = luaL_optinteger(L, 2, size);
if (pos != size)
luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); Our coded equivalents might be a dozen lines instead of 3 and not be as robust. Pull my hair out time. In my bones I would like to sort out this, but being realistic with every edit there is a small chance of making a breaking change, and so this sort of code change really needs testing. So on reflection and however tempting, this isn't something that we should realistically as part of this lua numbers review.
Defer the API cleanup to a second pass, starting with Thoughts? N + G + @jmattsson @pjsg @galjonsfigur ... ? |
Definitely defer this, I'd say. While I absolutely agree it's something that should get done, let's get all the big LVM stuff landed and settled. I'd like to get to the point where we can resume merging the 8266 and esp32 branches, but it'd be madness to have two major tickets running concurrently :D |
I agree with @jmattsson - It's true that many of C modules are interacting with Lua C API in sloppy way, but they are proven to work. For example, there were no big changes of Lua interface for ta least 2 years in |
#if LUA_VERSION_NUM == 501
us = luaL_checknumber(L, 1);
#else /* 503 */
us = lua_isinteger(L, 1) ? lua_tounsigned(L, 1) : (uint64) lua_tonumber(L, 1);
#endif
See latest push to #3221. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Not safe to close this one. |
As Philip has correctly pointed out, we have a soft break introduced in Lua 5.3 in the we've moved our default build to
sint32
/float32
sub types so that aTValue
can fit into 8 rather than 12 bytes. The standard Lua C API has a set of calls relating to number and integer handling that are largely preserved.A lot of our modules adopt rather sloppy use of the API for example
int val = lua_tonumber(L,1)
. In Lua 5.1 the API call returns adouble
which is then converted inline to a typeint
orunsigned int
. This is lossless if the underlying Lua value contained this type.float
that can only store 23 bit integers without loss of precision.This issue largely subsumes the sub issues: #3213, #3214, #3215, #3216, #3217 and #3218, I suggest that we handle general number type issues here. See my further analysis below.
[Philip's original lede] It turns out that it isn't just lua_pushnumber, but the reading functions can have problems as well.
rtcmem.write32
has this problem (from code inspection). I think that the json decoder will have problems....The text was updated successfully, but these errors were encountered: