static int luv_fs_write(lua_State* L) { uv_file file = luaL_checkinteger(L, 1); uv_buf_t buf; int64_t offset; int ref; uv_fs_t* req; size_t count; uv_buf_t *bufs = NULL; if (lua_istable(L, 2)) { bufs = luv_prep_bufs(L, 2, &count); } else if (lua_isstring(L, 2)) { luv_check_buf(L, 2, &buf); count = 1; } else { return luaL_argerror(L, 2, "data must be string or table of strings"); } offset = luaL_checkinteger(L, 3); ref = luv_check_continuation(L, 4); req = lua_newuserdata(L, sizeof(*req)); req->data = luv_setup_req(L, ref); req->ptr = buf.base; ((luv_req_t*)req->data)->data = bufs; FS_CALL(write, req, file, bufs ? bufs : &buf, count, offset); }
static int luv_write2(lua_State* L) { uv_stream_t* handle = luv_check_stream(L, 1); uv_write_t* req; int ret, ref; uv_stream_t* send_handle; send_handle = luv_check_stream(L, 3); ref = luv_check_continuation(L, 4); req = lua_newuserdata(L, sizeof(*req)); req->data = luv_setup_req(L, ref); if (lua_istable(L, 2)) { size_t count; uv_buf_t *bufs = luv_prep_bufs(L, 2, &count); ret = uv_write2(req, handle, bufs, count, send_handle, luv_write_cb); free(bufs); } else if (lua_isstring(L, 2)) { uv_buf_t buf; luv_check_buf(L, 2, &buf); ret = uv_write2(req, handle, &buf, 1, send_handle, luv_write_cb); } else { return luaL_argerror(L, 2, "data must be string or table of strings"); } if (ret < 0) { lua_pop(L, 1); return luv_error(L, ret); } lua_pushvalue(L, 2); ((luv_req_t*)req->data)->data_ref = luaL_ref(L, LUA_REGISTRYINDEX); return 1; }
static int luv_try_write(lua_State* L) { uv_stream_t* handle = luv_check_stream(L, 1); int ret; if (lua_istable(L, 2)) { size_t count; uv_buf_t *bufs = luv_prep_bufs(L, 2, &count); ret = uv_try_write(handle, bufs, count); free(bufs); } else if (lua_isstring(L, 2)) { uv_buf_t buf; luv_check_buf(L, 2, &buf); ret = uv_try_write(handle, &buf, 1); } else { return luaL_argerror(L, 2, "data must be string or table of strings"); } if (ret < 0) return luv_error(L, ret); lua_pushinteger(L, ret); return 1; }