#include "node.h" #include "v8.h" #include "uv.h" #include #include namespace { void RunInCallbackScope(const v8::FunctionCallbackInfo& args) { v8::Isolate* isolate = args.GetIsolate(); assert(args.Length() == 4); assert(args[0]->IsObject()); assert(args[1]->IsNumber()); assert(args[2]->IsNumber()); assert(args[3]->IsFunction()); node::async_context asyncContext = { args[1].As()->Value(), args[2].As()->Value() }; node::CallbackScope scope(isolate, args[0].As(), asyncContext); v8::Local fn = args[3].As(); v8::MaybeLocal ret = fn->Call(isolate->GetCurrentContext(), args[0], 0, nullptr); if (!ret.IsEmpty()) args.GetReturnValue().Set(ret.ToLocalChecked()); } static v8::Persistent persistent; static void Callback(uv_work_t* req, int ignored) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::HandleScope scope(isolate); node::CallbackScope callback_scope(isolate, v8::Object::New(isolate), node::async_context{0, 0}); v8::Local local = v8::Local::New(isolate, persistent); local->Resolve(isolate->GetCurrentContext(), v8::Undefined(isolate)).ToChecked(); delete req; } static void TestResolveAsync(const v8::FunctionCallbackInfo& args) { v8::Isolate* isolate = args.GetIsolate(); if (persistent.IsEmpty()) { persistent.Reset(isolate, v8::Promise::Resolver::New( isolate->GetCurrentContext()).ToLocalChecked()); uv_work_t* req = new uv_work_t; uv_queue_work(node::GetCurrentEventLoop(isolate), req, [](uv_work_t*) {}, Callback); } v8::Local local = v8::Local::New(isolate, persistent); args.GetReturnValue().Set(local->GetPromise()); } void Initialize(v8::Local exports) { NODE_SET_METHOD(exports, "runInCallbackScope", RunInCallbackScope); NODE_SET_METHOD(exports, "testResolveAsync", TestResolveAsync); } } // anonymous namespace NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)