62 lines
1.2 KiB
C++
62 lines
1.2 KiB
C++
|
|
#include <iostream>
|
|
#include <lua.hpp>
|
|
using namespace std;
|
|
|
|
int my_function(lua_State * L)
|
|
{
|
|
int argc = lua_gettop(L);
|
|
|
|
cerr << "-- my_function() called with " << argc
|
|
<< " arguments:" << endl;
|
|
|
|
for ( int n = 1; n <= argc; ++n )
|
|
{
|
|
cerr << "-- argument " << n << ": "
|
|
<< lua_tostring(L, n) << endl;
|
|
}
|
|
|
|
lua_pushnumber(L, 123); // return value
|
|
return 1; // number of return values
|
|
}
|
|
|
|
void report_errors(lua_State *L, int status)
|
|
{
|
|
if ( status!=0 )
|
|
{
|
|
cerr << "-- " << lua_tostring(L, -1) << endl;
|
|
lua_pop(L, 1); // remove error message
|
|
}
|
|
}
|
|
|
|
int main(int argc, char ** argv)
|
|
{
|
|
for ( int n = 1; n < argc; ++n )
|
|
{
|
|
const char * file = argv[n];
|
|
|
|
lua_State * L = lua_open();
|
|
|
|
luaL_openlibs(L);
|
|
|
|
// make my_function() available to Lua programs
|
|
lua_register(L, "my_function", my_function);
|
|
|
|
cerr << "-- Loading file: " << file << endl;
|
|
|
|
int s = luaL_loadfile(L, file);
|
|
|
|
if ( s == 0 )
|
|
{
|
|
// execute Lua program
|
|
s = lua_pcall(L, 0, LUA_MULTRET, 0);
|
|
}
|
|
|
|
report_errors(L, s);
|
|
lua_close(L);
|
|
cerr << endl;
|
|
}
|
|
|
|
return 0;
|
|
}
|