diff options
Diffstat (limited to 'src/node/ext/server.cc')
-rw-r--r-- | src/node/ext/server.cc | 202 |
1 files changed, 105 insertions, 97 deletions
diff --git a/src/node/ext/server.cc b/src/node/ext/server.cc index 32a8ff55b1..87363fc446 100644 --- a/src/node/ext/server.cc +++ b/src/node/ext/server.cc @@ -50,6 +50,15 @@ namespace grpc { namespace node { +using Nan::Callback; +using Nan::EscapableHandleScope; +using Nan::HandleScope; +using Nan::Maybe; +using Nan::MaybeLocal; +using Nan::ObjectWrap; +using Nan::Persistent; +using Nan::Utf8String; + using std::unique_ptr; using v8::Array; using v8::Boolean; @@ -57,16 +66,13 @@ using v8::Date; using v8::Exception; using v8::Function; using v8::FunctionTemplate; -using v8::Handle; -using v8::HandleScope; using v8::Local; using v8::Number; using v8::Object; -using v8::Persistent; using v8::String; using v8::Value; -NanCallback *Server::constructor; +Nan::Callback *Server::constructor; Persistent<FunctionTemplate> Server::fun_tpl; class NewCallOp : public Op { @@ -82,22 +88,26 @@ class NewCallOp : public Op { grpc_metadata_array_destroy(&request_metadata); } - Handle<Value> GetNodeValue() const { - NanEscapableScope(); + Local<Value> GetNodeValue() const { + Nan::EscapableHandleScope scope; if (call == NULL) { - return NanEscapeScope(NanNull()); + return scope.Escape(Nan::Null()); } - Handle<Object> obj = NanNew<Object>(); - obj->Set(NanNew("call"), Call::WrapStruct(call)); - obj->Set(NanNew("method"), NanNew(details.method)); - obj->Set(NanNew("host"), NanNew(details.host)); - obj->Set(NanNew("deadline"), - NanNew<Date>(TimespecToMilliseconds(details.deadline))); - obj->Set(NanNew("metadata"), ParseMetadata(&request_metadata)); - return NanEscapeScope(obj); + Local<Object> obj = Nan::New<Object>(); + Nan::Set(obj, Nan::New("call").ToLocalChecked(), Call::WrapStruct(call)); + Nan::Set(obj, Nan::New("method").ToLocalChecked(), + Nan::New(details.method).ToLocalChecked()); + Nan::Set(obj, Nan::New("host").ToLocalChecked(), + Nan::New(details.host).ToLocalChecked()); + Nan::Set(obj, Nan::New("deadline").ToLocalChecked(), + Nan::New<Date>( + TimespecToMilliseconds(details.deadline)).ToLocalChecked()); + Nan::Set(obj, Nan::New("metadata").ToLocalChecked(), + ParseMetadata(&request_metadata)); + return scope.Escape(obj); } - bool ParseOp(Handle<Value> value, grpc_op *out, + bool ParseOp(Local<Value> value, grpc_op *out, shared_ptr<Resources> resources) { return true; } @@ -124,35 +134,25 @@ Server::~Server() { grpc_completion_queue_destroy(this->shutdown_queue); } -void Server::Init(Handle<Object> exports) { - NanScope(); - Local<FunctionTemplate> tpl = NanNew<FunctionTemplate>(New); - tpl->SetClassName(NanNew("Server")); +void Server::Init(Local<Object> exports) { + HandleScope scope; + Local<FunctionTemplate> tpl = Nan::New<FunctionTemplate>(New); + tpl->SetClassName(Nan::New("Server").ToLocalChecked()); tpl->InstanceTemplate()->SetInternalFieldCount(1); - NanSetPrototypeTemplate(tpl, "requestCall", - NanNew<FunctionTemplate>(RequestCall)->GetFunction()); - - NanSetPrototypeTemplate( - tpl, "addHttp2Port", - NanNew<FunctionTemplate>(AddHttp2Port)->GetFunction()); - - NanSetPrototypeTemplate(tpl, "start", - NanNew<FunctionTemplate>(Start)->GetFunction()); - - NanSetPrototypeTemplate(tpl, "tryShutdown", - NanNew<FunctionTemplate>(TryShutdown)->GetFunction()); - NanSetPrototypeTemplate( - tpl, "forceShutdown", - NanNew<FunctionTemplate>(ForceShutdown)->GetFunction()); - - NanAssignPersistent(fun_tpl, tpl); - Handle<Function> ctr = tpl->GetFunction(); - constructor = new NanCallback(ctr); - exports->Set(NanNew("Server"), ctr); + Nan::SetPrototypeMethod(tpl, "requestCall", RequestCall); + Nan::SetPrototypeMethod(tpl, "addHttp2Port", AddHttp2Port); + Nan::SetPrototypeMethod(tpl, "start", Start); + Nan::SetPrototypeMethod(tpl, "tryShutdown", TryShutdown); + Nan::SetPrototypeMethod(tpl, "forceShutdown", ForceShutdown); + fun_tpl.Reset(tpl); + Local<Function> ctr = Nan::GetFunction(tpl).ToLocalChecked(); + Nan::Set(exports, Nan::New("Server").ToLocalChecked(), ctr); + constructor = new Callback(ctr); } -bool Server::HasInstance(Handle<Value> val) { - return NanHasInstance(fun_tpl, val); +bool Server::HasInstance(Local<Value> val) { + HandleScope scope; + return Nan::New(fun_tpl)->HasInstance(val); } void Server::ShutdownServer() { @@ -165,64 +165,77 @@ void Server::ShutdownServer() { } NAN_METHOD(Server::New) { - NanScope(); - /* If this is not a constructor call, make a constructor call and return the result */ - if (!args.IsConstructCall()) { + if (!info.IsConstructCall()) { const int argc = 1; - Local<Value> argv[argc] = {args[0]}; - NanReturnValue(constructor->GetFunction()->NewInstance(argc, argv)); + Local<Value> argv[argc] = {info[0]}; + MaybeLocal<Object> maybe_instance = constructor->GetFunction()->NewInstance( + argc, argv); + if (maybe_instance.IsEmpty()) { + // There's probably a pending exception + return; + } else { + info.GetReturnValue().Set(maybe_instance.ToLocalChecked()); + return; + } } grpc_server *wrapped_server; grpc_completion_queue *queue = CompletionQueueAsyncWorker::GetQueue(); - if (args[0]->IsUndefined()) { + if (info[0]->IsUndefined()) { wrapped_server = grpc_server_create(NULL, NULL); - } else if (args[0]->IsObject()) { - Handle<Object> args_hash(args[0]->ToObject()); - Handle<Array> keys(args_hash->GetOwnPropertyNames()); + } else if (info[0]->IsObject()) { + Local<Object> args_hash = Nan::To<Object>(info[0]).ToLocalChecked(); + Local<Array> keys = Nan::GetOwnPropertyNames(args_hash).ToLocalChecked(); grpc_channel_args channel_args; channel_args.num_args = keys->Length(); channel_args.args = reinterpret_cast<grpc_arg *>( calloc(channel_args.num_args, sizeof(grpc_arg))); /* These are used to keep all strings until then end of the block, then destroy them */ - std::vector<NanUtf8String *> key_strings(keys->Length()); - std::vector<NanUtf8String *> value_strings(keys->Length()); + std::vector<Utf8String *> key_strings(keys->Length()); + std::vector<Utf8String *> value_strings(keys->Length()); for (unsigned int i = 0; i < channel_args.num_args; i++) { - Handle<String> current_key(keys->Get(i)->ToString()); - Handle<Value> current_value(args_hash->Get(current_key)); - key_strings[i] = new NanUtf8String(current_key); + MaybeLocal<String> maybe_key = Nan::To<String>( + Nan::Get(keys, i).ToLocalChecked()); + if (maybe_key.IsEmpty()) { + free(channel_args.args); + return Nan::ThrowTypeError("Arg keys must be strings"); + } + Local<String> current_key = maybe_key.ToLocalChecked(); + Local<Value> current_value = Nan::Get(args_hash, + current_key).ToLocalChecked(); + key_strings[i] = new Utf8String(current_key); channel_args.args[i].key = **key_strings[i]; if (current_value->IsInt32()) { channel_args.args[i].type = GRPC_ARG_INTEGER; - channel_args.args[i].value.integer = current_value->Int32Value(); + channel_args.args[i].value.integer = Nan::To<int32_t>( + current_value).FromJust(); } else if (current_value->IsString()) { channel_args.args[i].type = GRPC_ARG_STRING; - value_strings[i] = new NanUtf8String(current_value); + value_strings[i] = new Utf8String(current_value); channel_args.args[i].value.string = **value_strings[i]; } else { free(channel_args.args); - return NanThrowTypeError("Arg values must be strings"); + return Nan::ThrowTypeError("Arg values must be strings"); } } wrapped_server = grpc_server_create(&channel_args, NULL); free(channel_args.args); } else { - return NanThrowTypeError("Server expects an object"); + return Nan::ThrowTypeError("Server expects an object"); } grpc_server_register_completion_queue(wrapped_server, queue, NULL); Server *server = new Server(wrapped_server); - server->Wrap(args.This()); - NanReturnValue(args.This()); + server->Wrap(info.This()); + info.GetReturnValue().Set(info.This()); } NAN_METHOD(Server::RequestCall) { - NanScope(); - if (!HasInstance(args.This())) { - return NanThrowTypeError("requestCall can only be called on a Server"); + if (!HasInstance(info.This())) { + return Nan::ThrowTypeError("requestCall can only be called on a Server"); } - Server *server = ObjectWrap::Unwrap<Server>(args.This()); + Server *server = ObjectWrap::Unwrap<Server>(info.This()); NewCallOp *op = new NewCallOp(); unique_ptr<OpVec> ops(new OpVec()); ops->push_back(unique_ptr<Op>(op)); @@ -230,79 +243,74 @@ NAN_METHOD(Server::RequestCall) { server->wrapped_server, &op->call, &op->details, &op->request_metadata, CompletionQueueAsyncWorker::GetQueue(), CompletionQueueAsyncWorker::GetQueue(), - new struct tag(new NanCallback(args[0].As<Function>()), ops.release(), + new struct tag(new Callback(info[0].As<Function>()), ops.release(), shared_ptr<Resources>(nullptr))); if (error != GRPC_CALL_OK) { - return NanThrowError(nanErrorWithCode("requestCall failed", error)); + return Nan::ThrowError(nanErrorWithCode("requestCall failed", error)); } CompletionQueueAsyncWorker::Next(); - NanReturnUndefined(); } NAN_METHOD(Server::AddHttp2Port) { - NanScope(); - if (!HasInstance(args.This())) { - return NanThrowTypeError( + if (!HasInstance(info.This())) { + return Nan::ThrowTypeError( "addHttp2Port can only be called on a Server"); } - if (!args[0]->IsString()) { - return NanThrowTypeError( + if (!info[0]->IsString()) { + return Nan::ThrowTypeError( "addHttp2Port's first argument must be a String"); } - if (!ServerCredentials::HasInstance(args[1])) { - return NanThrowTypeError( + if (!ServerCredentials::HasInstance(info[1])) { + return Nan::ThrowTypeError( "addHttp2Port's second argument must be ServerCredentials"); } - Server *server = ObjectWrap::Unwrap<Server>(args.This()); + Server *server = ObjectWrap::Unwrap<Server>(info.This()); ServerCredentials *creds_object = ObjectWrap::Unwrap<ServerCredentials>( - args[1]->ToObject()); + Nan::To<Object>(info[1]).ToLocalChecked()); grpc_server_credentials *creds = creds_object->GetWrappedServerCredentials(); int port; if (creds == NULL) { port = grpc_server_add_insecure_http2_port(server->wrapped_server, - *NanUtf8String(args[0])); + *Utf8String(info[0])); } else { port = grpc_server_add_secure_http2_port(server->wrapped_server, - *NanUtf8String(args[0]), + *Utf8String(info[0]), creds); } - NanReturnValue(NanNew<Number>(port)); + info.GetReturnValue().Set(Nan::New<Number>(port)); } NAN_METHOD(Server::Start) { - NanScope(); - if (!HasInstance(args.This())) { - return NanThrowTypeError("start can only be called on a Server"); + Nan::HandleScope scope; + if (!HasInstance(info.This())) { + return Nan::ThrowTypeError("start can only be called on a Server"); } - Server *server = ObjectWrap::Unwrap<Server>(args.This()); + Server *server = ObjectWrap::Unwrap<Server>(info.This()); grpc_server_start(server->wrapped_server); - NanReturnUndefined(); } NAN_METHOD(Server::TryShutdown) { - NanScope(); - if (!HasInstance(args.This())) { - return NanThrowTypeError("tryShutdown can only be called on a Server"); + Nan::HandleScope scope; + if (!HasInstance(info.This())) { + return Nan::ThrowTypeError("tryShutdown can only be called on a Server"); } - Server *server = ObjectWrap::Unwrap<Server>(args.This()); + Server *server = ObjectWrap::Unwrap<Server>(info.This()); unique_ptr<OpVec> ops(new OpVec()); grpc_server_shutdown_and_notify( server->wrapped_server, CompletionQueueAsyncWorker::GetQueue(), - new struct tag(new NanCallback(args[0].As<Function>()), ops.release(), + new struct tag(new Nan::Callback(info[0].As<Function>()), ops.release(), shared_ptr<Resources>(nullptr))); CompletionQueueAsyncWorker::Next(); - NanReturnUndefined(); } NAN_METHOD(Server::ForceShutdown) { - NanScope(); - if (!HasInstance(args.This())) { - return NanThrowTypeError("forceShutdown can only be called on a Server"); + Nan::HandleScope scope; + if (!HasInstance(info.This())) { + return Nan::ThrowTypeError("forceShutdown can only be called on a Server"); } - Server *server = ObjectWrap::Unwrap<Server>(args.This()); + Server *server = ObjectWrap::Unwrap<Server>(info.This()); server->ShutdownServer(); - NanReturnUndefined(); } } // namespace node |