static int luv_queue_work(lua_State* L) { luv_work_ctx_t* ctx; luv_work_t* work; int ret; int top = lua_gettop(L); ctx = luv_check_work_ctx(L, 1); // ctx should ref up work = lua_newuserdata(L, sizeof(*work)); //work should ref up memset(work, 0, sizeof(*work)); luv_thread_arg_set(L, &work->arg, 2, top); work->ctx = ctx; work->work.data = work; ret = uv_queue_work(luv_loop(L), &work->work, luv_work_cb, luv_after_work_cb); if (ret < 0) return luv_error(L, ret); lua_pushlightuserdata(L, work->ctx); lua_pushvalue(L, 1); lua_settable(L, LUA_REGISTRYINDEX); lua_pushlightuserdata(L, work); lua_pushvalue(L, -2); lua_settable(L, LUA_REGISTRYINDEX); lua_pushboolean(L, 1); return 1; }
static int luv_work_ctx_gc(lua_State *L) { luv_work_ctx_t* ctx = luv_check_work_ctx(L, 1); free(ctx->code); luaL_unref(L, LUA_REGISTRYINDEX, ctx->after_work_cb); luaL_unref(L, LUA_REGISTRYINDEX, ctx->async_cb); return 0; }
static int luv_queue_work(lua_State* L) { int top = lua_gettop(L); luv_work_ctx_t* ctx = luv_check_work_ctx(L, 1); luv_work_t* work = malloc(sizeof(*work)); int ret; luv_thread_arg_set(L, &work->arg, 2, top); work->ctx = ctx; work->work.data = work; ret = uv_queue_work(luv_loop(L), &work->work, luv_work_cb, luv_after_work_cb); if (ret < 0) { free(work); return luv_error(L, ret); } //ref up to ctx lua_pushlightuserdata(L, work); lua_pushvalue(L, 1); lua_rawset(L, LUA_REGISTRYINDEX); lua_pushboolean(L, 1); return 1; }
static int luv_work_ctx_tostring(lua_State* L) { luv_work_ctx_t* ctx = luv_check_work_ctx(L, 1); lua_pushfstring(L, "luv_work_ctx_t: %p", ctx); return 1; }