Skip to content

Commit 50b6203

Browse files
addaleaxjasnell
authored andcommitted
test: add regression test for 5691
With `CallbackScope`, this has become possible to do properly. Fixes: #5691 PR-URL: #14697 Reviewed-By: James M Snell <[email protected]>
1 parent 774e42b commit 50b6203

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

test/addons/callback-scope/binding.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "node.h"
22
#include "v8.h"
3+
#include "uv.h"
34

45
#include <assert.h>
56
#include <vector>
@@ -30,8 +31,39 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
3031
args.GetReturnValue().Set(ret.ToLocalChecked());
3132
}
3233

34+
static v8::Persistent<v8::Promise::Resolver> persistent;
35+
36+
static void Callback(uv_work_t* req, int ignored) {
37+
v8::Isolate* isolate = v8::Isolate::GetCurrent();
38+
v8::HandleScope scope(isolate);
39+
node::CallbackScope callback_scope(isolate, v8::Object::New(isolate), {0, 0});
40+
41+
v8::Local<v8::Promise::Resolver> local =
42+
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
43+
local->Resolve(v8::Undefined(isolate));
44+
delete req;
45+
}
46+
47+
static void TestResolveAsync(const v8::FunctionCallbackInfo<v8::Value>& args) {
48+
v8::Isolate* isolate = args.GetIsolate();
49+
50+
if (persistent.IsEmpty()) {
51+
persistent.Reset(isolate, v8::Promise::Resolver::New(isolate));
52+
53+
uv_work_t* req = new uv_work_t;
54+
55+
uv_queue_work(uv_default_loop(), req, [](uv_work_t*) {}, Callback);
56+
}
57+
58+
v8::Local<v8::Promise::Resolver> local =
59+
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
60+
61+
args.GetReturnValue().Set(local->GetPromise());
62+
}
63+
3364
void Initialize(v8::Local<v8::Object> exports) {
3465
NODE_SET_METHOD(exports, "runInCallbackScope", RunInCallbackScope);
66+
NODE_SET_METHOD(exports, "testResolveAsync", TestResolveAsync);
3567
}
3668

3769
} // namespace
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const common = require('../../common');
4+
const assert = require('assert');
5+
const { testResolveAsync } = require(`./build/${common.buildType}/binding`);
6+
7+
let called = false;
8+
testResolveAsync().then(common.mustCall(() => {
9+
called = true;
10+
}));
11+
12+
setTimeout(common.mustCall(() => { assert(called); }),
13+
common.platformTimeout(20));

0 commit comments

Comments
 (0)