#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
int lproxycall(lua_State * L) {
lua_CFunction f = (lua_CFunction)lua_touserdata(L, 1);
lua_remove(L, 1);
return f(L);
}
int myluacpcall(lua_State * L, lua_CFunction func,
int nargs, int nresults, int errfunc)
{
lua_pushvalue(L, 1);
lua_insert(L, -nargs-1);
lua_pushlightuserdata(L, func);
lua_insert(L, -nargs-1);
return lua_pcall(L, nargs+1, nresults, errfunc);
}
int test(lua_State * L) {
// printf("1\n");
lua_pushnumber(L, lua_tonumber(L, -1) + 1); return 1;
//return 0;
}
int main() {
int i;
lua_State * L = luaL_newstate();
if (L == NULL) return 1;
/* warning: these might raise */
lua_pushcfunction(L, lproxycall);
lua_pushcfunction(L, test);
for(i=0; i<10000000; i++) {
// method #1 - 17 s
// if (lua_cpcall(L, test, 0) != 0) {
// printf("error:%s\n", lua_tostring(L, -1));
// lua_pop(L, 1);
// }
// method #2 - 2.5 s
// lua_pushvalue(L, 2);
// lua_pcall(L, 0,0,0);
// method #3 - 3.3 s
//lua_pushnumber(L, 4);
if (myluacpcall(L, test, 0, 0, 0) != 0) { // or 1,1,0
printf("error:%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
}
//printf("%f\n", lua_tonumber(L, -1));
//lua_pop(L, 1);
}
return 0;
}