diff --git a/test/addons/callback-scope/binding.cc b/test/addons/callback-scope/binding.cc index 31317e5dae2..22105360fbf 100644 --- a/test/addons/callback-scope/binding.cc +++ b/test/addons/callback-scope/binding.cc @@ -1,5 +1,6 @@ #include "node.h" #include "v8.h" +#include "uv.h" #include #include @@ -30,8 +31,39 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo& args) { 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), {0, 0}); + + v8::Local local = + v8::Local::New(isolate, persistent); + local->Resolve(v8::Undefined(isolate)); + 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)); + + uv_work_t* req = new uv_work_t; + + uv_queue_work(uv_default_loop(), 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); } } // namespace diff --git a/test/addons/callback-scope/test-resolve-async.js b/test/addons/callback-scope/test-resolve-async.js new file mode 100644 index 00000000000..e9f4b9044c0 --- /dev/null +++ b/test/addons/callback-scope/test-resolve-async.js @@ -0,0 +1,13 @@ +'use strict'; + +const common = require('../../common'); +const assert = require('assert'); +const { testResolveAsync } = require(`./build/${common.buildType}/binding`); + +let called = false; +testResolveAsync().then(common.mustCall(() => { + called = true; +})); + +setTimeout(common.mustCall(() => { assert(called); }), + common.platformTimeout(20));