Skip to content

Instantly share code, notes, and snippets.

@fur-q
Last active December 14, 2015 03:29
Show Gist options
  • Save fur-q/5021416 to your computer and use it in GitHub Desktop.
Save fur-q/5021416 to your computer and use it in GitHub Desktop.
example C extension for Lua
#include <unistd.h>
#include <lua.h>
#include <lauxlib.h>
static int lua_sleep(lua_State *L) {
int ms = luaL_checkinteger(L, 1);
usleep(ms * 1000);
return 0;
}
int luaopen_lsleep(lua_State *L) {
static const luaL_Reg lsleep[] = {
{ "sleep", lua_sleep },
{ NULL, NULL }
};
lua_newtable(L);
#if LUA_VERSION_NUM > 501 // lua 5.2
luaL_setfuncs(L, lsleep, 0);
#else // lua 5.1
luaL_register(L, NULL, lsleep);
#endif
return 1;
}
PC = pkg-config
LUA = lua5.1
CFLAGS = `$(PC) --cflags $(LUA)` -Wall -fPIC
LDFLAGS = `$(PC) --libs $(LUA)`
lsleep.so: lsleep.c
gcc -shared $(CFLAGS) $(LDFLAGS) -o $@ $^
clean:
-rm lsleep.so
local s = require "lsleep"
s.sleep(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment