Last active
December 14, 2015 03:29
-
-
Save fur-q/5021416 to your computer and use it in GitHub Desktop.
example C extension for Lua
This file contains 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 <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; | |
} |
This file contains 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
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 |
This file contains 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
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