Forked from JakSprats/Redisql lua embedding proof of concept
Created
July 18, 2014 08:17
-
-
Save fatihky/1d6cdaae5789535a902b to your computer and use it in GitHub Desktop.
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 "lua.h" | |
#include "lualib.h" | |
#include "lauxlib.h" | |
#include "math.h" | |
int myCfunc(lua_State *L) { | |
printf (" START running C redis (LUA)\n"); | |
double d = lua_tonumber(L, 1); | |
lua_pushnumber(L, d * d); | |
printf (" END running C redis (LUA)\n"); | |
return 1; | |
} | |
int main() { | |
lua_State *L = lua_open(); | |
luaL_openlibs(L); | |
lua_register(L, "redis", myCfunc); | |
if (luaL_loadfile(L, "ccall.lua") || lua_pcall(L, 0, 0, 0)) { | |
printf("0: error: %s", lua_tostring(L, -1)); | |
return -1; | |
} | |
lua_getglobal(L, "squarer"); | |
if(!lua_isfunction(L, -1)) { | |
printf("function not found\n"); | |
lua_pop(L,1); | |
return -1; | |
} | |
lua_pushnumber(L, 12); /* push 1st argument */ | |
/* do the call (2 arguments, 1 result) */ | |
printf("START: calling from C main()\n"); | |
if (lua_pcall(L, 1, 1, 0) != 0) { | |
printf("error running function `f': %s\n",lua_tostring(L, -1)); | |
return -1; | |
} | |
printf("END: calling from C main()\n"); | |
/* retrieve result */ | |
if (!lua_isnumber(L, -1)) { | |
printf("function `redis' must return a number\n"); | |
return -1; | |
} | |
double z = lua_tonumber(L, -1); | |
printf("Result: %f\n",z); | |
lua_pop(L, 1); | |
lua_close(L); | |
return 0; | |
} |
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
function squarer(x) | |
io.write(" lua level -> START call C\n"); | |
sum = redis(x); | |
io.write(" lua level -> END call C\n"); | |
return sum; | |
end |
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
START: calling from C main() | |
lua level -> START call C | |
START running C redis (LUA) | |
END running C redis (LUA) | |
lua level -> END call C | |
END: calling from C main() | |
Result: 144.000000 |
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
#!/bin/bash | |
echo gcc -I/usr/include/lua5.1/ -L/usr/local/lib/ -llua -o call_lua{,.c} -llua -ldl -lm | |
gcc -I/usr/include/lua5.1/ -L/usr/local/lib/ -llua -o call_lua{,.c} -llua -ldl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment