diff options
Diffstat (limited to 'platform/node')
-rw-r--r-- | platform/node/src/node_map.cpp | 23 | ||||
-rw-r--r-- | platform/node/src/node_map.hpp | 2 | ||||
-rw-r--r-- | platform/node/src/node_request.cpp | 37 | ||||
-rw-r--r-- | platform/node/src/node_request.hpp | 10 |
4 files changed, 41 insertions, 31 deletions
diff --git a/platform/node/src/node_map.cpp b/platform/node/src/node_map.cpp index 3c44155401..eb2ad64700 100644 --- a/platform/node/src/node_map.cpp +++ b/platform/node/src/node_map.cpp @@ -737,20 +737,21 @@ NodeMap::~NodeMap() { if (map) release(); } -std::unique_ptr<mbgl::AsyncRequest> NodeMap::request(const mbgl::Resource& resource, Callback callback_) { - Nan::HandleScope handleScope; +std::unique_ptr<mbgl::AsyncRequest> NodeMap::request(const mbgl::Resource& resource, mbgl::FileSource::Callback callback_) { + Nan::HandleScope scope; + + v8::Local<v8::Value> argv[] = { + Nan::New<v8::External>(this), + Nan::New<v8::External>(&callback_) + }; - // 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); + auto instance = Nan::New(NodeRequest::constructor)->NewInstance(2, argv); - auto requestHandle = NodeRequest::Create(resource, callback_)->ToObject(); - auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(requestHandle); - auto callbackHandle = Nan::New<v8::Function>(NodeRequest::Respond, requestHandle); + Nan::Set(instance, Nan::New("url").ToLocalChecked(), Nan::New(resource.url).ToLocalChecked()); + Nan::Set(instance, Nan::New("kind").ToLocalChecked(), Nan::New<v8::Integer>(resource.kind)); - v8::Local<v8::Value> argv[] = { requestHandle, callbackHandle }; - Nan::MakeCallback(handle()->GetInternalField(1)->ToObject(), "request", 2, argv); + auto request = Nan::ObjectWrap::Unwrap<NodeRequest>(instance); + request->Execute(); return std::make_unique<NodeRequest::NodeAsyncRequest>(request); } diff --git a/platform/node/src/node_map.hpp b/platform/node/src/node_map.hpp index d64d58013d..ac793c8178 100644 --- a/platform/node/src/node_map.hpp +++ b/platform/node/src/node_map.hpp @@ -45,7 +45,7 @@ public: NodeMap(v8::Local<v8::Object>); ~NodeMap(); - std::unique_ptr<mbgl::AsyncRequest> request(const mbgl::Resource&, Callback); + std::unique_ptr<mbgl::AsyncRequest> request(const mbgl::Resource&, mbgl::FileSource::Callback); mbgl::HeadlessView view; std::unique_ptr<mbgl::Map> map; diff --git a/platform/node/src/node_request.cpp b/platform/node/src/node_request.cpp index 5f2460ff7f..d7931ac572 100644 --- a/platform/node/src/node_request.cpp +++ b/platform/node/src/node_request.cpp @@ -1,4 +1,5 @@ #include "node_request.hpp" +#include "node_map.hpp" #include <mbgl/storage/response.hpp> #include <mbgl/util/chrono.hpp> @@ -6,8 +7,11 @@ namespace node_mbgl { -NodeRequest::NodeRequest(mbgl::FileSource::Callback callback_) +NodeRequest::NodeRequest( + NodeMap* target_, + mbgl::FileSource::Callback callback_) : AsyncWorker(nullptr), + target(target_), callback(callback_) { } @@ -32,27 +36,30 @@ NAN_MODULE_INIT(NodeRequest::Init) { Nan::Set(target, Nan::New("Request").ToLocalChecked(), tpl->GetFunction()); } -NAN_METHOD(NodeRequest::New) { - auto req = new NodeRequest(*reinterpret_cast<mbgl::FileSource::Callback*>(info[0].As<v8::External>()->Value())); - req->Wrap(info.This()); +void NodeRequest::New(const Nan::FunctionCallbackInfo<v8::Value>& info) { + auto target = reinterpret_cast<NodeMap*>(info[0].As<v8::External>()->Value()); + auto callback = reinterpret_cast<mbgl::FileSource::Callback*>(info[1].As<v8::External>()->Value()); + + auto request = new NodeRequest(target, *callback); + + request->Wrap(info.This()); info.GetReturnValue().Set(info.This()); } -v8::Handle<v8::Object> NodeRequest::Create(const mbgl::Resource& resource, mbgl::FileSource::Callback callback) { - Nan::EscapableHandleScope scope; +void NodeRequest::Execute() { + Nan::HandleScope handleScope; - v8::Local<v8::Value> argv[] = { - Nan::New<v8::External>(const_cast<mbgl::FileSource::Callback*>(&callback)) - }; - auto instance = Nan::New(constructor)->NewInstance(1, argv); + // 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); - Nan::Set(instance, Nan::New("url").ToLocalChecked(), Nan::New(resource.url).ToLocalChecked()); - Nan::Set(instance, Nan::New("kind").ToLocalChecked(), Nan::New<v8::Integer>(int(resource.kind))); + auto fn = Nan::New<v8::Function>(NodeRequest::Respond, handle()); - return scope.Escape(instance); -} + v8::Local<v8::Value> argv[] = { handle(), fn }; -void NodeRequest::Execute() {} + Nan::MakeCallback(Nan::To<v8::Object>(target->handle()->GetInternalField(1)).ToLocalChecked(), "request", 2, argv); +} NAN_METHOD(NodeRequest::Respond) { using Error = mbgl::Response::Error; diff --git a/platform/node/src/node_request.hpp b/platform/node/src/node_request.hpp index 140f668aa0..e6df54bb1f 100644 --- a/platform/node/src/node_request.hpp +++ b/platform/node/src/node_request.hpp @@ -11,22 +11,22 @@ namespace node_mbgl { +class NodeMap; + class NodeRequest : public Nan::ObjectWrap, public Nan::AsyncWorker { public: - NodeRequest(mbgl::FileSource::Callback); + NodeRequest(NodeMap*, mbgl::FileSource::Callback); ~NodeRequest(); static Nan::Persistent<v8::Function> constructor; static NAN_MODULE_INIT(Init); - static NAN_METHOD(New); + static void New(const Nan::FunctionCallbackInfo<v8::Value>&); static NAN_METHOD(Respond); void Execute(); - static v8::Handle<v8::Object> Create(const mbgl::Resource&, mbgl::FileSource::Callback); - struct NodeAsyncRequest : public mbgl::AsyncRequest { NodeAsyncRequest(NodeRequest*); ~NodeAsyncRequest() override; @@ -34,7 +34,9 @@ public: }; private: + NodeMap* target; mbgl::FileSource::Callback callback; + NodeAsyncRequest* asyncRequest = nullptr; }; |