Skip to content

Instantly share code, notes, and snippets.

@exallium
Created February 19, 2012 01:06
Show Gist options
  • Save exallium/1861506 to your computer and use it in GitHub Desktop.
Save exallium/1861506 to your computer and use it in GitHub Desktop.
load and execute lua function
g++ -o test test.cpp -llua
#include <iostream>
#include <lua.hpp>
using namespace std;
int main(int argc, char **argv)
{
if (argc != 2) {
cout << "test: usage: test <filename>" << endl;
return 0;
}
const char *file = argv[1];
lua_State *L = lua_open();
luaL_openlibs(L);
cout << "Loading file " << file << endl;
int s;
if((s = (luaL_loadfile(L, file) || lua_pcall(L, 0, 0, 0)))) {
cout << "Error: " << lua_tostring(L, -1);
}
if (s == 0) {
// make the call
lua_getglobal(L, "test");
lua_pushinteger(L, 4);
s = lua_pcall(L, 1, 0, 0);
if (s) cout << "Error: " << lua_tostring(L, -1);
}
lua_close(L);
return 0;
}
function test (x)
if x == 1 then
io.write("hello");
else
io.write("not a one");
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment