Node.js Addon Development

readdir (async)

#define ASYNC_DEST_CALL(func, callback, dest_path, ...)          \
  Environment* env = Environment::GetCurrent(args.GetIsolate()); \
  FSReqWrap* req_wrap;                                           \
  char* dest_str = (dest_path);                                  \
  int dest_len = dest_str == NULL ? 0 : strlen(dest_str);        \
  char* storage = new char[sizeof(*req_wrap) + dest_len];        \
  req_wrap = new(storage) FSReqWrap(env, #func);                 \
  req_wrap->dest_len(dest_len);                                  \
  if (dest_str != NULL) {                                        \
    memcpy(const_cast<char*>(req_wrap->dest()),                  \
           dest_str,                                             \
           dest_len + 1);                                        \
  }                                                              \
  int err = uv_fs_ ## func(env->event_loop() ,                   \
                           &req_wrap->req_,                      \
                           __VA_ARGS__,                          \
                           After);                               \
  req_wrap->object()->Set(env->oncomplete_string(), callback);   \
  req_wrap->Dispatched();                                        \
  if (err < 0) {                                                 \
    uv_fs_t* req = &req_wrap->req_;                              \
    req->result = err;                                           \
    req->path = NULL;                                            \
    After(req);                                                  \
  }                                                              \
  args.GetReturnValue().Set(req_wrap->persistent());