Skip to content

Example 4: Calling C Functions From Lua

kamih edited this page Jul 14, 2012 · 1 revision

The FFI feature of LuaJit allows Lua script to call exported C functions directly, but it is currently unable to pass Lua tables and functions as arguments. For this you will need to define your own Lua C functions and register them with the LuaState.

This example shows you how to do this with LuaUtils.

This C++ code example is taken directly from VoidStorm. Here I add a Lua C function that will receive a LuaTable describing an event that the Lua script is triggering:

int vsAddEvent(lua_State *L)
{
    LuaUtils::LuaStateCFunc state(L);
         
    // Get event ID from first argument
    int eventID;
    state.checkArg(1, eventID);
     
    // Check that the eventID is valid
    state.checkArgCondition(1, EventManager::checkValidEvent(eventID), "Invalid eventID");
 
    // Check that Lua is allowed to create an event with this eventID argument
    state.checkArgCondition(1, EventManager::checkValidLuaEvent(eventID), "Invalid Lua eventID");
 
    // Get optional event table
    LuaUtils::LuaTableCFunc table;
    state.getArg(2, table);
     
    // Add event
    EventManager::addLuaEvent(eventID, table);
 
    // No results
    return 0;
}

void    initLuaCFuncs(LuaUtils::LuaState *state)
{
    state->setValue("vsAddEvent", vsAddEvent");
}

The initLuaCFuncs function will set the vsAddEvent function as a new global that can be called by Lua script.

You’ll have noticed that we are using LuaStateCFunc and LuaTableCFunc classes here. These are extensions of the regular LuaState and LuaTable that add special functions that should only be called from a Lua C function.

LuaStateCFunc::checkArg and LuaStateCFunc::getArg behave like LuaState::getValue, but for Lua C function arguments.

The difference with checkArg is that it will throw a Lua error and abort the current Lua C function if there is no valid argument of the required type at that argument index. This is useful because you will see an error that shows you which Lua script line called this function. LuaTableCFunc has a similar function called checkValue.

LuaStateCFunc::checkArgCondition will throw an error about the specified argument index if the passed condition is false. This is useful to enforce value conditions on certain arguments. In this example, we want to make sure that the incoming eventID is valid.

This example doesn’t return any results, but if you need to, use LuaStateCFunc::pushValue, and return the number of results pushed.

Clone this wiki locally