summaryrefslogtreecommitdiff
path: root/platform/node/src/node_request.cpp
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <thiago@mapbox.com>2016-06-29 21:35:24 +0300
committerMike Morris <mikemorris@users.noreply.github.com>2016-09-06 18:14:22 -0400
commitedec56e6c55bdf992fb2d3f92d770db57c737e3e (patch)
tree51fd82feb9e7bffda6a653e15ecd331ee6095593 /platform/node/src/node_request.cpp
parentd6f667a5e762ce1faec80bee774b805fe7ef5e11 (diff)
downloadqtlocation-mapboxgl-edec56e6c55bdf992fb2d3f92d770db57c737e3e.tar.gz
[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.
Diffstat (limited to 'platform/node/src/node_request.cpp')
-rw-r--r--platform/node/src/node_request.cpp14
1 files changed, 6 insertions, 8 deletions
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<v8::Value>& info) {
}
void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo<v8::Value>& info) {
+ auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(info.Holder());
+
// Move out of the object so callback() can only be fired once.
- auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(info.Data().As<v8::Object>());
auto callback = std::move(request->callback);
if (!callback) {
return info.GetReturnValue().SetUndefined();
@@ -122,14 +125,9 @@ void NodeRequest::HandleCallback(const Nan::FunctionCallbackInfo<v8::Value>& inf
}
void NodeRequest::Execute() {
- // Enter a new v8::Context to avoid leaking v8::FunctionTemplate
- // from Nan::New<v8::Function>
- v8::Local<v8::Context> context = v8::Context::New(v8::Isolate::GetCurrent());
- v8::Context::Scope scope(context);
-
- v8::Local<v8::Value> argv[] = { handle(), Nan::New<v8::Function>(NodeRequest::HandleCallback, handle()) };
+ v8::Local<v8::Value> argv[] = { handle() };
- Nan::MakeCallback(Nan::To<v8::Object>(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 2, argv);
+ Nan::MakeCallback(Nan::To<v8::Object>(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 1, argv);
}
NodeRequest::NodeAsyncRequest::NodeAsyncRequest(NodeRequest* request_) : request(request_) {