From edec56e6c55bdf992fb2d3f92d770db57c737e3e Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Wed, 29 Jun 2016 21:35:24 +0300 Subject: [node] switch to NodeRequest member fn callback For (hopefully) better performance than creating a new v8::Context to wrap each callback while still avoiding leaking memory with v8::FunctionTemplate. Adds a JavaScript shim in front of module.exports.Map to wrap the req.respond API internally and preserve the public callback-passing API, while still exporting the correct prototype. --- platform/node/src/node_request.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'platform/node/src/node_request.cpp') diff --git a/platform/node/src/node_request.cpp b/platform/node/src/node_request.cpp index aa84147ad6..607e6e8b03 100644 --- a/platform/node/src/node_request.cpp +++ b/platform/node/src/node_request.cpp @@ -32,6 +32,8 @@ NAN_MODULE_INIT(NodeRequest::Init) { tpl->InstanceTemplate()->SetInternalFieldCount(1); tpl->SetClassName(Nan::New("Request").ToLocalChecked()); + Nan::SetPrototypeMethod(tpl, "respond", HandleCallback); + constructor.Reset(tpl->GetFunction()); // TODO: Remove this from the public JavaScript API @@ -49,8 +51,9 @@ void NodeRequest::New(const Nan::FunctionCallbackInfo& info) { } void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo& info) { + auto request = Nan::ObjectWrap::Unwrap(info.Holder()); + // Move out of the object so callback() can only be fired once. - auto request = Nan::ObjectWrap::Unwrap(info.Data().As()); auto callback = std::move(request->callback); if (!callback) { return info.GetReturnValue().SetUndefined(); @@ -122,14 +125,9 @@ void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo& inf } void NodeRequest::Execute() { - // Enter a new v8::Context to avoid leaking v8::FunctionTemplate - // from Nan::New - v8::Local context = v8::Context::New(v8::Isolate::GetCurrent()); - v8::Context::Scope scope(context); - - v8::Local argv[] = { handle(), Nan::New(NodeRequest::HandleCallback, handle()) }; + v8::Local argv[] = { handle() }; - Nan::MakeCallback(Nan::To(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 2, argv); + Nan::MakeCallback(Nan::To(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 1, argv); } NodeRequest::NodeAsyncRequest::NodeAsyncRequest(NodeRequest* request_) : request(request_) { -- cgit v1.2.1