-
Notifications
You must be signed in to change notification settings - Fork 45
provide automatic nop fallback for missing callbacks #338
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
Conversation
lib/luadevice.c
Outdated
| lunatik_defaultfield(L, 1, "open", lunatik_nop); | ||
| lunatik_defaultfield(L, 1, "release", lunatik_nop); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this wasn't what I meant.. I meant to just failover on the call.. not to check the field and store it.. look for the place we are actually calling and failing when such callbacks aren't defined and instead of failing (if type(field) is not function, use luantik_nop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay yeah makes sense , i misinterpreted the requirement , but got it now
lneto
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please, squash your commits into a single one. Could you also review Luadevice documentation to see if it needs to be updated? Thanks!
examples/systrack.lua
Outdated
|
|
||
| local syscalls = {"openat", "read", "write", "readv", "writev", "close"} | ||
|
|
||
| local function nop() end -- do nothing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay , so the runtime fallback is added for device.new and not probe.new so ideally if i remove that, it will break .
so i am wondering if i should add a similar handler for probe as well ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah.. it's used on probe.new below, right? yeah, if it's apply, I think we should do this change as well.. and perhaps add a new macro that could be used for both.. take a look at optinteger().
Yes sure will do that |
lunatik.h
Outdated
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please, mind our style..
lunatik.h
Outdated
| #define lunatik_optfunction(L, idx, field, default_func) \ | ||
| ((lua_getfield((L), (idx), (field)) == LUA_TFUNCTION) ? \ | ||
| (void)0 : \ | ||
| (lua_pop((L), 1), lua_pushcfunction((L), (default_func)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for not following the do { } while (0) approach? btw, I would create a template, e.g., NEWOPT and create optcfunction and optinteger using it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason for not following the
do { } while (0)approach? btw, I would create a template, e.g.,NEWOPTand create optcfunction and optinteger using it.
I thought the ternary operator would do the trick. At the end, both achieve the same result functionally, but you're right that do-while-0 is more idiomatic for statement-like macros.
I'll go ahead and make a NEWOPT template .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lneto but , isnt optinteger being used to access struct fields , while in optcfunction the ideal requirement , is accessing Lua Table fields , so i think its better just refactor optcfunction .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Zapper9982 you are right. Perhaps, we should change optinteger instead to just get the field and have another macro calling it to set the struct field, that's breaking it into two macros. What do you think? (of course, we can tackle it as a separate PR).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Breaking lunatik_optinteger into a getter macro (that just pushes the value or the default onto the stack) and an assignment wrapper would make it much more composable for sure
lunatik.h
Outdated
| return 0; | ||
| } | ||
|
|
||
| #define lunatik_optfunction(L, idx, field, default_func) \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please move it next to optinteger, it will make easier to find this API in the header later =)
lunatik.h
Outdated
| #define lunatik_optcfunction(L, idx, field, default_func) \ | ||
| do { \ | ||
| (lua_getfield(L, idx, field) == LUA_TFUNCTION) ? \ | ||
| (void)0 : \ | ||
| (lua_pop(L, 1), lua_pushcfunction(L, default_func)); \ | ||
| } while (0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| #define lunatik_optcfunction(L, idx, field, default_func) \ | |
| do { \ | |
| (lua_getfield(L, idx, field) == LUA_TFUNCTION) ? \ | |
| (void)0 : \ | |
| (lua_pop(L, 1), lua_pushcfunction(L, default_func)); \ | |
| } while (0) | |
| static inline void lunatik_optcfunction(L, idx, field, opt) | |
| { | |
| if (lua_getfield(L, idx, field) != LUA_TFUNCTION) { | |
| lua_pop(L, 1); | |
| lua_pushcfunction(L, opt)); | |
| } | |
| } |
I'm not fond of using the same name pattern as optinteger as we are not actually setting a struct field as pointed out, but I couldn't think on something better.. anyway, in this case, we don't need a macro.. an inline will do =). Please, move it to next optinteger anyway, it will be easier to maintain. Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done :))
lneto
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's looking good to me.. the only two things are missing is 1) to move the function declaration to the region I mentioned and 2) squash your commits. Thanks!
lib/luadevice.c
Outdated
| pr_err("%s: operation isn't defined for /dev/%s\n", fop, lua_tostring(L, -1)); | ||
| goto err; | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
plase mind that, we don't do double \n
lneto
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry.. I was about to merge and found that \n, can you fix it? then I can merge.. also, could you remove this "feat(core)" from your commit message? we don't use this kind of prefix.. please, check our history to get a better sense of our commit messages..

Changes:
Added:
lunatik_nop1 : A generic no-op lua_CFunction inlunatik.h`lunatik_defaultfield: reusable helper to default table fields to a functionlunatik_nopwhen callbacks are missingTesting :
All tests pass with kernel module loaded successfullyScreenshots :