summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: f6c4f3e)
raw | patch | inline | side by side (parent: f6c4f3e)
author | Ruben Kerkhof <ruben@rubenkerkhof.com> | |
Sun, 14 Aug 2016 10:07:59 +0000 (12:07 +0200) | ||
committer | Ruben Kerkhof <ruben@rubenkerkhof.com> | |
Sun, 14 Aug 2016 10:13:20 +0000 (12:13 +0200) |
Now the module shows up in 'package.loaded':
for name, func in pairs(package.loaded['collectd']) do
print(name, func)
end
returns:
dispatch_values function: 0x7f38b8a472c0
log_debug function: 0x7f38b8a47130
register_write function: 0x7f38b8a47520
log_info function: 0x7f38b8a470d0
register_read function: 0x7f38b8a47360
log_warning function: 0x7f38b8a47070
log_error function: 0x7f38b8a47100
log_notice function: 0x7f38b8a470a0
for name, func in pairs(package.loaded['collectd']) do
print(name, func)
end
returns:
dispatch_values function: 0x7f38b8a472c0
log_debug function: 0x7f38b8a47130
register_write function: 0x7f38b8a47520
log_info function: 0x7f38b8a470d0
register_read function: 0x7f38b8a47360
log_warning function: 0x7f38b8a47070
log_error function: 0x7f38b8a47100
log_notice function: 0x7f38b8a470a0
src/lua.c | patch | blob | history |
diff --git a/src/lua.c b/src/lua.c
index a3f2037e2ffb11b6967ed9e3a76a2501a464aca1..a0e360366ae8a8ad42903b52b168534e88d5906d 100644 (file)
--- a/src/lua.c
+++ b/src/lua.c
return 0;
} /* }}} int lua_cb_register_write */
-static luaL_Reg lua_c_functions[] = {
+static luaL_Reg collectdlib[] = {
{"log_debug", lua_cb_log_debug},
{"log_error", lua_cb_log_error},
{"log_info", lua_cb_log_info},
{"register_read", lua_cb_register_read},
{"register_write", lua_cb_register_write}};
+static int open_collectd(lua_State *L) /* {{{ */
+{
+ luaL_newlib(L, collectdlib);
+ return 1;
+} /* }}} */
+
static void lua_script_free(lua_script_t *script) /* {{{ */
{
if (script == NULL)
/* Open up all the standard Lua libraries. */
luaL_openlibs(script->lua_state);
- /* Register all the functions we implement in C */
- lua_newtable(script->lua_state);
- for (size_t i = 0; i < STATIC_ARRAY_SIZE(lua_c_functions); i++) {
- lua_pushcfunction(script->lua_state, lua_c_functions[i].func);
- lua_setfield(script->lua_state, -2, lua_c_functions[i].name);
- }
- lua_setglobal(script->lua_state, "collectd");
+ /* Load the 'collectd' library */
+ luaL_requiref(script->lua_state, "collectd", open_collectd, 1);
+ lua_pop(script->lua_state, 1);
/* Prepend BasePath to package.path */
if (base_path[0] != '\0') {