diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-09-05 19:06:37 +0200 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-09-14 17:38:45 +0200 |
commit | bdaa2cb58520b115cdc170bc46ad6120369486bd (patch) | |
tree | e6cabb550bdd3fe099941ceb08c8d872d99c1137 /test/addons | |
parent | 8c8c90b714756433b3333146a2893572b473dfd6 (diff) | |
download | android-node-v8-bdaa2cb58520b115cdc170bc46ad6120369486bd.tar.gz android-node-v8-bdaa2cb58520b115cdc170bc46ad6120369486bd.tar.bz2 android-node-v8-bdaa2cb58520b115cdc170bc46ad6120369486bd.zip |
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>
Diffstat (limited to 'test/addons')
-rw-r--r-- | test/addons/callback-scope/binding.cc | 32 | ||||
-rw-r--r-- | test/addons/callback-scope/test-resolve-async.js | 13 |
2 files changed, 45 insertions, 0 deletions
diff --git a/test/addons/callback-scope/binding.cc b/test/addons/callback-scope/binding.cc index 31317e5dae..22105360fb 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 <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 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 0000000000..e9f4b9044c --- /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)); |