Skip to content

Instantly share code, notes, and snippets.

@HeinrichHartmann
Created April 19, 2018 09:20
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 HeinrichHartmann/23be0371e199df607d88b37f7be47d2e to your computer and use it in GitHub Desktop.
Save HeinrichHartmann/23be0371e199df607d88b37f7be47d2e to your computer and use it in GitHub Desktop.
Lua C API Examples
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
// Helper function to print the stack contents
// Values other than numbers and strings are printed as "(null)"
void print_stack(lua_State *L, char* title){
int nargs = lua_gettop(L);
printf(" --- STACK at %s ---\n", title);
for(int i=1; i<=nargs; i++) {
printf(" - S[%d] = %s\n", i, lua_tostring(L, i));
}
printf(" ---\n");
}
int l_cls(lua_State *L) {
print_stack(L, "entry cls");
lua_settop(L,0); // clear stack
lua_pushstring(L, "Not returned");
lua_pushvalue(L, lua_upvalueindex(1)); // ret1
lua_pushvalue(L, lua_upvalueindex(2)); // ret2
lua_pushvalue(L, lua_upvalueindex(3)); // ret3
lua_pushstring(L, "Hello world from CLS!"); // ret4
print_stack(L, "exit cls");
return 4;
}
int l_closure(lua_State *L) {
print_stack(L, "entry l_closure");
lua_pushstring(L, "a");
lua_pushstring(L, "b");
lua_pushstring(L, "c");
lua_pushcclosure(L, l_cls, 3);
print_stack(L, "exit l_closure");
return 1;
}
int l_simple(lua_State *L) {
print_stack(L, "entry l_simple");
// push some more garbage onto the stack
lua_pushstring(L, "some ");
lua_pushstring(L, "more");
lua_pushstring(L, "garbage");
// push some return values
lua_pushstring(L, "ret1");
lua_pushstring(L, "ret2");
lua_pushstring(L, "ret3");
// return the top 3 values on the stack
// in order S[-3], S[-2], S[-1]
print_stack(L, "exit l_simple");
return 3;
}
int main(int argc, char **argv){
static const struct luaL_Reg l_lib[] = {
{"simple", l_simple},
{"closure", l_closure},
{NULL, NULL}
};
lua_State *L = lua_open();
luaL_openlibs(L);
luaL_openlib(L, "lib", l_lib, 0);
luaL_dostring(L, "print(\"Simple function call:\")");
luaL_dostring(L, "print(lib.simple(1,2,3))");
luaL_dostring(L, "print(\"Call a closure\")");
luaL_dostring(L, "print(lib.closure()(1,2,3))");
return 0;
}
@HeinrichHartmann
Copy link
Author

HeinrichHartmann commented Apr 19, 2018

Example run:

$ gcc -O0 -g -std=c99 \
   -I/<path to luajit>/include/luajit-2.1 -L/<path to luajit>/lib/ -lluajit-5.1 \
   lua_example.c -o lua_example && \
   ./lua_example

Simple function call:
   --- STACK at entry l_simple ---
   - S[1] = 1
   - S[2] = 2
   - S[3] = 3
   ---
   --- STACK at exit l_simple ---
   - S[1] = 1
   - S[2] = 2
   - S[3] = 3
   - S[4] = some
   - S[5] = more
   - S[6] = garbage
   - S[7] = ret1
   - S[8] = ret2
   - S[9] = ret3
   ---
ret1    ret2    ret3
Call a closure
   --- STACK at entry l_closure ---
   ---
   --- STACK at exit l_closure ---
   - S[1] = (null)
   ---
   --- STACK at entry cls ---
   - S[1] = 1
   - S[2] = 2
   - S[3] = 3
   ---
   --- STACK at exit cls ---
   - S[1] = Not returned
   - S[2] = a
   - S[3] = b
   - S[4] = c
   - S[5] = Hello world from CLS!
   ---
a       b       c       Hello world from CLS!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment