Skip to content

Instantly share code, notes, and snippets.

@jeremyong
Last active January 2, 2016 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyong/8365105 to your computer and use it in GitHub Desktop.
Save jeremyong/8365105 to your computer and use it in GitHub Desktop.
sample function call to lua file
#include <string>
#include <cassert>
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main() {
lua_State *l = luaL_newstate();
luaL_dofile(l, "example.lua");
// Push function name to the stack
lua_getglobal(l, "subtract_and_hello");
// Push two numbers to the stack
lua_pushinteger(l, 1);
lua_pushinteger(l, 3);
// Call the function
const int num_args = 2;
const int num_return_values = 2;
lua_call(l, num_args, num_return_values);
// Note that the stack is 1-indexed from the bottom
const int diff = lua_tointeger(l, 1);
const std::string greeting = lua_tostring(l, 2);
// Pop the returned values from the stack
lua_pop(l, 2);
// Check that things worked correctly
assert(diff == -2 && greeting == "hello");
// Clean up the lua context
lua_close(l);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment