@@ -349,6 +349,31 @@ class fs_req_wrap {
349
349
DISALLOW_COPY_AND_ASSIGN (fs_req_wrap);
350
350
};
351
351
352
+ // Template counterpart of ASYNC_DEST_CALL
353
+ template <typename Func, typename ... Args>
354
+ inline FSReqWrap* AsyncDestCall (Environment* env, Local<Object> req,
355
+ const char * dest, enum encoding enc, const char * syscall,
356
+ Func fn, Args... args) {
357
+ FSReqWrap* req_wrap = FSReqWrap::New (env, req, syscall, dest, enc);
358
+ int err = fn (env->event_loop (), req_wrap->req (), args..., After);
359
+ req_wrap->Dispatched ();
360
+ if (err < 0 ) {
361
+ uv_fs_t * uv_req = req_wrap->req ();
362
+ uv_req->result = err;
363
+ uv_req->path = nullptr ;
364
+ After (uv_req);
365
+ req_wrap = nullptr ;
366
+ }
367
+
368
+ return req_wrap;
369
+ }
370
+
371
+ // Template counterpart of ASYNC_CALL
372
+ template <typename Func, typename ... Args>
373
+ inline FSReqWrap* AsyncCall (Environment* env, Local<Object> req,
374
+ enum encoding enc, const char * syscall, Func fn, Args... args) {
375
+ return AsyncDestCall (env, req, nullptr , enc, syscall, fn, args...);
376
+ }
352
377
353
378
#define ASYNC_DEST_CALL (func, request, dest, encoding, ...) \
354
379
Environment* env = Environment::GetCurrent(args); \
@@ -373,6 +398,28 @@ class fs_req_wrap {
373
398
#define ASYNC_CALL (func, req, encoding, ...) \
374
399
ASYNC_DEST_CALL (func, req, nullptr , encoding, __VA_ARGS__) \
375
400
401
+ // Template counterpart of SYNC_DEST_CALL
402
+ template <typename Func, typename ... Args>
403
+ inline void SyncDestCall (Environment* env, Local<Value> ctx,
404
+ const char * path, const char * dest, const char * syscall,
405
+ Func fn, Args... args) {
406
+ fs_req_wrap req_wrap;
407
+ env->PrintSyncTrace ();
408
+ int err = fn (env->event_loop (), &req_wrap.req , args..., nullptr );
409
+ if (err) {
410
+ Local<Context> context = env->context ();
411
+ Local<Object> ctx_obj = ctx->ToObject (context).ToLocalChecked ();
412
+ env->CollectUVExceptionInfo (ctx_obj, err, syscall, nullptr , path, dest);
413
+ }
414
+ }
415
+
416
+ // Template counterpart of SYNC_CALL
417
+ template <typename Func, typename ... Args>
418
+ inline void SyncCall (Environment* env, Local<Value> ctx,
419
+ const char * path, const char * syscall, Func fn, Args... args) {
420
+ return SyncDestCall (env, ctx, path, nullptr , syscall, fn, args...);
421
+ }
422
+
376
423
#define SYNC_DEST_CALL (func, path, dest, ...) \
377
424
fs_req_wrap req_wrap; \
378
425
env->PrintSyncTrace (); \
@@ -394,21 +441,22 @@ class fs_req_wrap {
394
441
void Access (const FunctionCallbackInfo<Value>& args) {
395
442
Environment* env = Environment::GetCurrent (args.GetIsolate ());
396
443
HandleScope scope (env->isolate ());
397
-
398
- if (args.Length () < 2 )
399
- return TYPE_ERROR (" path and mode are required" );
400
- if (!args[1 ]->IsInt32 ())
401
- return TYPE_ERROR (" mode must be an integer" );
444
+ Local<Context> context = env->context ();
445
+ CHECK_GE (args.Length (), 2 );
446
+ CHECK (args[1 ]->IsInt32 ());
402
447
403
448
BufferValue path (env->isolate (), args[0 ]);
404
- ASSERT_PATH (path)
405
-
406
- int mode = static_cast <int >(args[1 ]->Int32Value ());
449
+ int mode = static_cast <int >(args[1 ]->Int32Value (context).FromJust ());
407
450
408
451
if (args[2 ]->IsObject ()) {
409
- ASYNC_CALL (access, args[2 ], UTF8, *path, mode);
452
+ Local<Object> req_obj = args[2 ]->ToObject (context).ToLocalChecked ();
453
+ FSReqWrap* req_wrap = AsyncCall (
454
+ env, req_obj, UTF8, " access" , uv_fs_access, *path, mode);
455
+ if (req_wrap != nullptr ) {
456
+ args.GetReturnValue ().Set (req_wrap->persistent ());
457
+ }
410
458
} else {
411
- SYNC_CALL (access, *path, *path, mode);
459
+ SyncCall (env, args[ 3 ], *path, " access " , uv_fs_access , *path, mode);
412
460
}
413
461
}
414
462
0 commit comments