test: add regression test for 5691
With `CallbackScope`, this has become possible to do properly. Fixes: https://github.com/nodejs/node/issues/5691 PR-URL: https://github.com/nodejs/node/pull/14697 Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
8c8c90b714
commit
bdaa2cb585
@ -1,5 +1,6 @@
|
||||
#include "node.h"
|
||||
#include "v8.h"
|
||||
#include "uv.h"
|
||||
|
||||
#include <assert.h>
|
||||
#include <vector>
|
||||
@ -30,8 +31,39 @@ void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
args.GetReturnValue().Set(ret.ToLocalChecked());
|
||||
}
|
||||
|
||||
static v8::Persistent<v8::Promise::Resolver> 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<v8::Promise::Resolver> local =
|
||||
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
|
||||
local->Resolve(v8::Undefined(isolate));
|
||||
delete req;
|
||||
}
|
||||
|
||||
static void TestResolveAsync(const v8::FunctionCallbackInfo<v8::Value>& 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<v8::Promise::Resolver> local =
|
||||
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
|
||||
|
||||
args.GetReturnValue().Set(local->GetPromise());
|
||||
}
|
||||
|
||||
void Initialize(v8::Local<v8::Object> exports) {
|
||||
NODE_SET_METHOD(exports, "runInCallbackScope", RunInCallbackScope);
|
||||
NODE_SET_METHOD(exports, "testResolveAsync", TestResolveAsync);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
13
test/addons/callback-scope/test-resolve-async.js
Normal file
13
test/addons/callback-scope/test-resolve-async.js
Normal file
@ -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));
|
Loading…
x
Reference in New Issue
Block a user