Last active
January 2, 2016 21:39
-
-
Save jeremyong/8365105 to your computer and use it in GitHub Desktop.
sample function call to lua file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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