Skip to content

Instantly share code, notes, and snippets.

@hnakamur
Created September 12, 2012 18:05
Show Gist options
  • Save hnakamur/3708688 to your computer and use it in GitHub Desktop.
Save hnakamur/3708688 to your computer and use it in GitHub Desktop.
fs.open using macros
#define SETUP_REQ \
uv_fs_cb cb = NULL; \
uv_loop_t *loop = luv_get_loop(L); \
fs_req_holder *holder = (fs_req_holder *)create_obj_init_ref(L, \
sizeof(fs_req_holder), "lev.fs"); \
/* NOTE: set_call needs "object" to be stack at index 1 */ \
lua_insert(L, 1); \
uv_fs_t *req = &holder->req;
#define SET_OPT_CB(index, c_callback) \
if (lua_isfunction(L, (index))) { \
set_callback(L, CALLBACK_NAME, (index)); \
lev_handle_ref(L, (LevRefStruct_t *)holder, -1); \
cb = (c_callback); \
}
#define POST_OPERATION \
/* NOTE: remove "object" */ \
lua_remove(L, 1); \
if (req->result == -1) { \
lev_push_uv_err(L, LEV_UV_ERR_FROM_REQ(req)); \
return 1; \
} \
if (cb) { \
return 0; \
} else { \
int ret_n = push_results(L, req); \
uv_fs_req_cleanup(req); \
return ret_n; \
}
/*
* fs.open
*/
static int fs_open(lua_State* L) {
SETUP_REQ;
/* NOTE: index is added by 1 because of holder above. */
const char *path = luaL_checkstring(L, 2);
int flags = fs_checkflags(L, 3);
int mode = luaL_optint(L, 4, 0666);
SET_OPT_CB(5, on_fs_callback);
uv_fs_open(loop, req, path, flags, mode, cb);
POST_OPERATION;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment