0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/test/addons/callback-scope/binding.cc
Daniel Bevenius 695590e386 test: fix compiler warnings in callback-scope
Currently there are two compiler warnings generated from the addons test
callback-scope:

../binding.cc:44:10:
warning: 'Resolve' is deprecated [-Wdeprecated-declarations]
  local->Resolve(v8::Undefined(isolate));
         ^
../../../../deps/v8/include/v8.h:3893:45:
note: 'Resolve' has been explicitly marked deprecated here
V8_DEPRECATED("Use maybe version", void Resolve(Local<Value> value));
         ^

../binding.cc:52:54:
warning: 'New' is deprecated [-Wdeprecated-declarations]
    persistent.Reset(isolate, v8::Promise::Resolver::New(isolate));
                                                     ^
../../../../deps/v8/include/v8.h:3880:42:
note: 'New' has been explicitly marked deprecated here
Local<Resolver> New(Isolate* isolate));

This commit updates the test to use non-deprecated functions.

PR-URL: https://github.com/nodejs/node/pull/19252
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Franziska Hinkelmann <franziska.hinkelmann@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
2018-03-11 16:13:39 +01:00

78 lines
2.2 KiB
C++

#include "node.h"
#include "v8.h"
#include "uv.h"
#include <assert.h>
#include <vector>
namespace {
void RunInCallbackScope(const v8::FunctionCallbackInfo<v8::Value>& 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<v8::Number>()->Value(),
args[2].As<v8::Number>()->Value()
};
node::CallbackScope scope(isolate, args[0].As<v8::Object>(), asyncContext);
v8::Local<v8::Function> fn = args[3].As<v8::Function>();
v8::MaybeLocal<v8::Value> ret =
fn->Call(isolate->GetCurrentContext(), args[0], 0, nullptr);
if (!ret.IsEmpty())
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),
node::async_context{0, 0});
v8::Local<v8::Promise::Resolver> local =
v8::Local<v8::Promise::Resolver>::New(isolate, persistent);
local->Resolve(isolate->GetCurrentContext(),
v8::Undefined(isolate)).ToChecked();
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->GetCurrentContext()).ToLocalChecked());
uv_work_t* req = new uv_work_t;
uv_queue_work(node::GetCurrentEventLoop(isolate),
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);
}
} // anonymous namespace
NODE_MODULE(NODE_GYP_MODULE_NAME, Initialize)