Skip to content

Conversation

@Zapper9982
Copy link
Contributor

Changes:

Added:

  • lunatik_nop1 : A generic no-op lua_CFunction in lunatik.h`
  • lunatik_defaultfield : reusable helper to default table fields to a function
  • Modified device.new to automatically apply lunatik_nop when callbacks are missing

Testing :

  • Verified with devices using default callbacks
  • Verified custom callbacks are preserved
  • Verified mixed scenarios (custom + default)
    All tests pass with kernel module loaded successfully

Screenshots :

image

lib/luadevice.c Outdated
Comment on lines 366 to 367
lunatik_defaultfield(L, 1, "open", lunatik_nop);
lunatik_defaultfield(L, 1, "release", lunatik_nop);
Copy link
Contributor

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.

Copy link
Contributor Author

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

Copy link
Contributor

@lneto lneto left a 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!


local syscalls = {"openat", "read", "write", "readv", "writev", "close"}

local function nop() end -- do nothing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused, right?

Copy link
Contributor Author

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 ?

Copy link
Contributor

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().

@Zapper9982
Copy link
Contributor Author

Please, squash your commits into a single one. Could you also review Luadevice documentation to see if it needs to be updated? Thanks!

Yes sure will do that

lunatik.h Outdated
Comment on lines 114 to 115


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please, mind our style..

Suggested change

lunatik.h Outdated
Comment on lines 110 to 113
#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))))
Copy link
Contributor

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.

Copy link
Contributor Author

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.

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 .

Copy link
Contributor Author

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 .

Copy link
Contributor

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).

Copy link
Contributor Author

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) \
Copy link
Contributor

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
Comment on lines 335 to 340
#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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :))

@Zapper9982 Zapper9982 requested a review from lneto December 23, 2025 15:32
Copy link
Contributor

@lneto lneto left a 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!

@Zapper9982
Copy link
Contributor Author

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!

Its already there :))
image

but yes ill squash my commits now , was waiting on getting the final green flag , so ill squash them at once

lib/luadevice.c Outdated
pr_err("%s: operation isn't defined for /dev/%s\n", fop, lua_tostring(L, -1));
goto err;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Contributor

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

Copy link
Contributor

@lneto lneto left a 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..

@Zapper9982 Zapper9982 changed the title feat(device) : add nop default for open/release callbacks provide automatic nop fallback for missing callbacks Dec 24, 2025
@lneto lneto merged commit 94bd2b6 into luainkernel:master Dec 24, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants