extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "lobject.h"
}
#define lua_register(L,n,f) (lua_pushcfunction(L, (f)), lua_setglobal(L, (n)))
static int print(lua_State* L);
static int test(lua_State* L);
static const struct luaL_Reg mylib [] = {
{"print", print},
{"test", test},
{NULL, NULL} /* end of array */
};
static int test(lua_State* L)
{
lua_pushinteger(L,5);
lua_pushfstring(L,"test");
lua_pushboolean(L,1);
return 3;
}
static int print(lua_State* L)
{
int nargs = lua_gettop(L);
for (int i=1; i <= nargs; ++i) {
printf( lua_tostring(L, i));
}
printf("\r\n");
return 0;
}
int main()
{
lua_State *l = luaL_newstate();
luaL_openlibs(l);
lua_getglobal(l, "_G");
luaL_setfuncs(l, mylib, 0);
lua_pop(l, 1);
if(luaL_loadfile(l,"D:\\Michael\\Desktop\\lua\\Debug\\test.lua")==0)
{
lua_pcall(l,0,0,0);
}
else
{
printf("Failed to run script: %s\n", lua_tostring(l, -1));
}
lua_close(l);
return 0;
}