Register Version String in C-Library for Lua

63 views
Skip to first unread message

Kritzel Kratzel

unread,
Jun 7, 2024, 3:54:38 PMJun 7
to lua-l
I have written a small C-library (Windows DLL) for implementing the ping command for Lua, in which functions are registered as metamethods in the usual way. I like to extend this code with a version string feature:

lp = require "luaping"
print(lp._VERSION)
LuaPing 1.0

Is it possible to do this via the C API of Lua? If yes, how do I do this?

Thanks,
KK

...
static const struct luaL_Reg luaping_metamethods [] = {
  {"__call", luaping_ping},
  {"__call", luaping_timeout},
  {"__call", luaping_settimeout},
  {NULL, NULL}
};

static const struct luaL_Reg luaping_funcs [] = {
  {"ping", luaping_ping},
  {"timeout", luaping_timeout},
  {"settimeout", luaping_settimeout},
  {NULL, NULL}
};

DLL int luaopen_luaping(lua_State *L){
  luaL_newlib(L, luaping_funcs);
  luaL_newlib(L, luaping_metamethods);
  lua_setmetatable(L, -2);
  return 1;
}

Sean Conner

unread,
Jun 7, 2024, 3:58:46 PMJun 7
to 'Kritzel Kratzel' via lua-l
It was thus said that the Great 'Kritzel Kratzel' via lua-l once stated:
> I have written a small C-library (Windows DLL) for implementing the *ping*
> command for Lua, in which functions are registered as metamethods in the
> usual way. I like to extend this code with a version string feature:
>
> lp = require "luaping"
> print(lp._VERSION)
> LuaPing 1.0
>
> Is it possible to do this via the C API of Lua? If yes, how do I do this?
>
> Thanks,
> KK
>
> DLL int luaopen_luaping(lua_State *L){
> luaL_newlib(L, luaping_funcs);
> luaL_newlib(L, luaping_metamethods);
> lua_setmetatable(L, -2);
> return 1;
> }

Yes, thusly:

DLL int luaopen_luaping(lua_State *L){
luaL_newlib(L,luaping_funcs);
luaL_newlib(L,luaping_metamethods);
lua_setmetatable(L, -2);
lua_pushlstring(L,"LuaPing 1.0");
lua_setfield(L,-2,"_VERSION");
return 1;
}

-spc

Sainan

unread,
Jun 7, 2024, 4:14:47 PMJun 7
to lu...@googlegroups.com
> lua_pushlstring(L,"LuaPing 1.0");

You probably meant lua_pushliteral or lua_pushstring.

Sean Conner

unread,
Jun 7, 2024, 4:33:43 PMJun 7
to 'Sainan' via lua-l
It was thus said that the Great 'Sainan' via lua-l once stated:
> > lua_pushlstring(L,"LuaPing 1.0");
>
> You probably meant lua_pushliteral or lua_pushstring.

Yes. I think I was partly on qutopilot there.

-spc

Kritzel Kratzel

unread,
Jun 8, 2024, 4:16:00 AMJun 8
to lu...@googlegroups.com
Many thanks, works as expected.

-KK
> --
> You received this message because you are subscribed to a topic in the Google Groups "lua-l" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/lua-l/WpibUWwcXFQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to lua-l+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/lua-l/20240607195843.GJ23445%40brevard.conman.org.

Reply all
Reply to author
Forward
0 new messages