#include "LuaBindings.h"
#include "lua-5.3.2/src/lua.h"
#include "lua-5.3.2/src/lauxlib.h"
#include <stdarg.h>
#include <vector>
#include <string>
#include <functional>
#include <cstdint>
#include <cassert>
// Various plain 'C' functions with varying number of args and parameters for testing
void testF(const char* msg)
{
printf("C: %s \n", msg);
}
void printMessage1(const char* msg, int i=4, double d=5.0, const char*three="x")
{
printf("C: %s %i %f %s\n", msg, i, d, three);
}
void noArgs()
{
printf("C: noargs\n");
}
// Normal kind of way to wrap this with manually built bindings
static int printMessage(lua_State *lua)
{
assert(lua_isstring (lua,1));
const char *msg = lua_tostring(lua, 1);
printMessage1(msg);
return 0;
}
#define AUTO_BIND_C_FUNTION_TO_LUA(func) \
{ #func, [](lua_State*lua)->int { return MakeCallableFunc(lua, func); } },
// Import table
static const struct luaL_Reg scriptCallableFunctions[] =
{
AUTO_BIND_C_FUNTION_TO_LUA(printMessage1)
AUTO_BIND_C_FUNTION_TO_LUA(noArgs)
AUTO_BIND_C_FUNTION_TO_LUA(testF)
// Example inline binding
{ "blah", [](lua_State*)->int {return 0; } },
// Example adding a manual bound function
{ "pm", printMessage }
};
void registerLuaBinding(lua_State* L, const char* name, lua_CFunction fn)
{
lua_pushcclosure(L, fn, 0);
lua_setglobal(L, name);
}
void registerLuaBindings(lua_State* L)
{
// Register all of our bindings
for (luaL_Reg r : scriptCallableFunctions)
registerLuaBinding(L, r.name, r.func);
}