static void LuaIO_sleep_timeout(uv_timer_t *handle) { lua_State *L = handle->data; LuaIO_timer_free(handle); lua_pushinteger(L, 0); LuaIO_resume(L, 1); }
static void LuaIO_fs_callback(uv_fs_t* req) { LuaIO_fs_req_t* fs_req = container_of(req, LuaIO_fs_req_t, req); lua_State* L = fs_req->current_thread; int result = req->result; switch (req->fs_type) { case UV_FS_ACCESS: case UV_FS_CLOSE: case UV_FS_RENAME: case UV_FS_UNLINK: case UV_FS_RMDIR: case UV_FS_MKDIR: case UV_FS_FTRUNCATE: case UV_FS_FSYNC: case UV_FS_FDATASYNC: case UV_FS_LINK: case UV_FS_SYMLINK: case UV_FS_CHMOD: case UV_FS_FCHMOD: case UV_FS_CHOWN: case UV_FS_FCHOWN: case UV_FS_UTIME: case UV_FS_FUTIME: case UV_FS_OPEN: case UV_FS_SENDFILE: lua_pushinteger(L, result); LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 1); return; case UV_FS_READ: if (result > 0) { fs_req->read_buffer->write_pos += result; } lua_pushinteger(L, result); LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 1); return; case UV_FS_WRITE: if (result < 0) { lua_pushinteger(L, result); } else { lua_pushinteger(L, fs_req->bytes); } luaL_unref(L, LUA_REGISTRYINDEX, fs_req->write_data_ref); LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 1); return; case UV_FS_STAT: case UV_FS_LSTAT: case UV_FS_FSTAT: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { LuaIO_fs_create_stat(L, &req->statbuf); lua_pushinteger(L, 0); } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; case UV_FS_MKDTEMP: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { lua_pushstring(L, req->path); lua_pushinteger(L, 0); } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; case UV_FS_READLINK: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { lua_pushstring(L, (char*)req->ptr); lua_pushinteger(L, 0); } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; case UV_FS_SCANDIR: if (result < 0) { lua_pushnil(L); lua_pushinteger(L, result); } else { int ret; lua_createtable(L, 0, 0); for (int i = 1; ; i++) { uv_dirent_t ent; ret = uv_fs_scandir_next(req, &ent); if (ret) break; lua_pushstring(L, ent.name); lua_rawseti(L, -2, i); } if (ret && ret != UV_EOF) { lua_pop(L, 1); lua_pushnil(L); lua_pushinteger(L, ret); } else { lua_pushinteger(L, 0); } } LuaIO_pfree(&LuaIO_fs_req_pool, fs_req); LuaIO_resume(L, 2); return; default: luaL_error(L, "fs_native module error: unknown fs type(%d)", req->fs_type); } }