|
#include <lua.h> #include <lauxlib.h> static int recursive_closure(lua_State *L) { fprintf(stderr, "Recursive call %d\n", luaL_checkint(L, lua_upvalueindex(1))); if (!lua_isnoneornil(L, lua_upvalueindex(2))) { luaL_checktype(L, lua_upvalueindex(2), LUA_TFUNCTION); lua_pushvalue(L, lua_upvalueindex(2)); lua_call(L, 1, 0); } } static void push_recursive_closure(lua_State *L, int i) { fprintf(stderr, "Called %s(%p, %d)\n", __func__, L, i); lua_pushinteger(L, i); if (i > 0) push_recursive_closure(L, i-1); lua_pushcclosure(L, recursive_closure, i > 0 ? 2 : 1); } static int getrclosure(lua_State *L) { push_recursive_closure(L, luaL_checkint(L, 1)); return 1; } int luaopen_rclosure(lua_State *L) { lua_pushcfunction(L, getrclosure); lua_setfield(L, LUA_GLOBALSINDEX, "rclosure"); return 0; }
Attachment:
recurse.lua
Description: Binary data
Attachment:
recurse_output
Description: Binary data