lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I'm looking for a way to find out a function from it's name.

I understood from the documentation that function are handled as "normal" object, so with my test code, I'm expecting to find it in global variable table.
So I'm calling lua_getglobal() on the function name but it fails and returns a NIL.

I did this little test code .

--- Test.lua ---
function UPSBatCharge( topic, val )
    print(topic, val);

---- FindFunc.c ---
#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

lua_State *L;

void clean_lua(void){

int findUserFunc( const char *fn ){
    lua_getglobal(L, fn);
    if( lua_type(L, -1) != LUA_TFUNCTION ){
        fprintf(stderr, "*E* \"%s\" is not a function, a %s.\n", fn, lua_typename(L, lua_type(L, -1)) );
        lua_pop(L, 1);
        return LUA_REFNIL;
    /* tbd */
    return LUA_REFNIL;

void main( void ){
    L = lua_open();

    int err;
    if(( err = luaL_loadfile(L, "Test.lua") )){
        fprintf(stderr, "*F*  %s\n", lua_tostring(L, -1));

    puts("File loaded");


My need : I created a tool named Marcel ( which is, among other things, call Lua function when it receive some MQTT data. Those functions aim to take decision to raise an alert for example.
And the user has only to provide in the configuration file the name of the function to be called.

Thanks for your help.



The misspelling master is on the Web.
  _________ 100 % Dictionnary Free !
/                    /(
/ Dico          / / Pleins d'autres fautes sur
/________ / /
(########( /
Quoi, des fautes d'orthographe! Pas possible ;-D.