From c0fe624ecd4ed6cbfc46da59bd62bb1917cb6df7 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Thu, 19 Feb 2015 16:19:31 -0500 Subject: [PATCH] add node v0.12 and io.js compatibility --- src/compress_png.cpp | 4 ++-- src/node_file_source.cpp | 36 +++++++++++++++++++++++++++-------- src/node_file_source.hpp | 4 ++++ src/node_log.cpp | 9 +++++---- src/node_map.cpp | 8 ++++++-- src/node_mapbox_gl_native.cpp | 3 +-- src/node_request.cpp | 30 +++++++++++++++-------------- src/node_request.hpp | 2 +- 8 files changed, 63 insertions(+), 33 deletions(-) diff --git a/src/compress_png.cpp b/src/compress_png.cpp index 068abe65f..99cec5b1b 100644 --- a/src/compress_png.cpp +++ b/src/compress_png.cpp @@ -8,15 +8,15 @@ public: CompressPNGWorker(NanCallback *callback_, v8::Local buffer_, uint32_t width_, uint32_t height_) : NanAsyncWorker(callback_), - buffer(v8::Persistent::New(buffer_)), data(node::Buffer::Data(buffer_)), width(width_), height(height_) { + NanAssignPersistent(buffer, buffer_); assert(width * height * 4 == node::Buffer::Length(buffer_)); } ~CompressPNGWorker() { - buffer.Dispose(); + NanDisposePersistent(buffer); } void Execute() { diff --git a/src/node_file_source.cpp b/src/node_file_source.cpp index e0b78008b..acf96bf12 100644 --- a/src/node_file_source.cpp +++ b/src/node_file_source.cpp @@ -96,11 +96,18 @@ void NodeFileSource::processAdd(mbgl::Request *req) { queue->ref(); } - auto requestHandle = v8::Local::New(NodeRequest::Create(handle_, req)); - pending.emplace(req, std::move(v8::Persistent::New(requestHandle))); + auto handle = NanObjectWrapHandle(this); + auto requestHandle = NanNew(NodeRequest::Create(handle, request)); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + v8::Persistent requestPersistent; + NanAssignPersistent(requestPersistent, requestHandle); +#else + const v8::UniquePersistent requestPersistent(v8::Isolate::GetCurrent(), requestHandle); +#endif + pending.emplace(request, std::move(requestPersistent)); v8::Local argv[] = { requestHandle }; - NanMakeCallback(handle_, NanNew("request"), 1, argv); + NanMakeCallback(handle, NanNew("request"), 1, argv); } void NodeFileSource::processCancel(mbgl::Request *req) { @@ -111,10 +118,18 @@ void NodeFileSource::processCancel(mbgl::Request *req) { // The response callback was already fired. There is no point in calling the cancelation // callback because the request is already completed. } else { - auto requestHandle = v8::Local::New(it->second); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + auto requestHandle = NanNew(it->second); +#else + auto requestHandle = v8::Local::New(v8::Isolate::GetCurrent(), it->second); +#endif // Dispose and remove the persistent handle - it->second.Dispose(); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + NanDisposePersistent(it->second); +#else + // it->second.Reset(); +#endif pending.erase(it); // Make sure the the loop can exit when there are no pending requests. @@ -122,9 +137,10 @@ void NodeFileSource::processCancel(mbgl::Request *req) { queue->unref(); } - if (handle_->Has(NanNew("cancel"))) { + auto handle = NanObjectWrapHandle(this); + if (handle->Has(NanNew("cancel"))) { v8::Local argv[] = { requestHandle }; - NanMakeCallback(handle_, NanNew("cancel"), 1, argv); + NanMakeCallback(handle, NanNew("cancel"), 1, argv); } // Set the request handle in the request wrapper handle to null @@ -139,7 +155,11 @@ void NodeFileSource::notify(mbgl::Request *req, const std::shared_ptrsecond.Dispose(); +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 + NanDisposePersistent(it->second); +#else + // it->second.Dispose(); +#endif pending.erase(it); // Make sure the the loop can exit when there are no pending requests. diff --git a/src/node_file_source.hpp b/src/node_file_source.hpp index 791b96969..c0dbe58d2 100644 --- a/src/node_file_source.hpp +++ b/src/node_file_source.hpp @@ -46,7 +46,11 @@ private: void processCancel(mbgl::Request*); private: +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 std::map> pending; +#else + std::map &> pending; +#endif Queue *queue = nullptr; }; diff --git a/src/node_log.cpp b/src/node_log.cpp index f10d52d44..d7cf0e0d8 100644 --- a/src/node_log.cpp +++ b/src/node_log.cpp @@ -32,7 +32,7 @@ NodeLogObserver::NodeLogObserver(v8::Handle target) : queue(new Queue(uv_default_loop(), [this](LogMessage &message) { NanScope(); - auto msg = v8::Object::New(); + auto msg = NanNew(); msg->Set(NanNew("class"), NanNew(mbgl::EventClass(message.event).c_str())); msg->Set(NanNew("severity"), NanNew(mbgl::EventSeverityClass(message.severity).c_str())); if (message.code != -1) { @@ -49,11 +49,12 @@ NodeLogObserver::NodeLogObserver(v8::Handle target) } v8::Local argv[] = { NanNew("message"), msg }; - auto emit = module->Get(NanNew("emit"))->ToObject(); - emit->CallAsFunction(module, 2, argv); + auto handle = NanNew(module); + auto emit = handle->Get(NanNew("emit"))->ToObject(); + emit->CallAsFunction(handle, 2, argv); })) { NanScope(); - module = v8::Persistent::New(target); + NanAssignPersistent(module, target); // Don't keep the event loop alive. queue->unref(); diff --git a/src/node_map.cpp b/src/node_map.cpp index b6aae1302..e5b3fda5f 100644 --- a/src/node_map.cpp +++ b/src/node_map.cpp @@ -239,7 +239,7 @@ void NodeMap::renderFinished() { assert(!image); if (img) { - v8::Local result = v8::Object::New(); + auto result = NanNew(); result->Set(NanNew("width"), NanNew(img->width)); result->Set(NanNew("height"), NanNew(img->height)); @@ -283,7 +283,11 @@ NodeMap::NodeMap(v8::Handle source_) : NanAssignPersistent(source, source_); async->data = this; +#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10 uv_async_init(uv_default_loop(), async, [](uv_async_t *as, int) { +#else + uv_async_init(uv_default_loop(), async, [](uv_async_t *as) { +#endif reinterpret_cast(as->data)->renderFinished(); }); @@ -292,7 +296,7 @@ NodeMap::NodeMap(v8::Handle source_) : } NodeMap::~NodeMap() { - source.Dispose(); + NanDisposePersistent(source); uv_close(reinterpret_cast(async), [] (uv_handle_t *handle) { delete reinterpret_cast(handle); diff --git a/src/node_mapbox_gl_native.cpp b/src/node_mapbox_gl_native.cpp index e784681a0..a7a0f0fcf 100644 --- a/src/node_mapbox_gl_native.cpp +++ b/src/node_mapbox_gl_native.cpp @@ -11,7 +11,6 @@ #include "node_request.hpp" #include "compress_png.hpp" - void RegisterModule(v8::Handle exports) { NanScope(); @@ -22,7 +21,7 @@ void RegisterModule(v8::Handle exports) { // Exports Resource constants. auto ConstantProperty = static_cast(v8::ReadOnly | v8::DontDelete); - auto resource = v8::Object::New(); + auto resource = NanNew(); resource->ForceSet(NanNew("Unknown"), NanNew(mbgl::Resource::Unknown), ConstantProperty); resource->ForceSet(NanNew("Tile"), NanNew(mbgl::Resource::Tile), ConstantProperty); resource->ForceSet(NanNew("Glyphs"), NanNew(mbgl::Resource::Glyphs), ConstantProperty); diff --git a/src/node_request.cpp b/src/node_request.cpp index b67fd2097..0fdc05bff 100644 --- a/src/node_request.cpp +++ b/src/node_request.cpp @@ -35,34 +35,35 @@ NAN_METHOD(NodeRequest::New) { return NanThrowTypeError("Cannot create Request objects explicitly"); } - auto source = v8::Persistent::New(args[0]->ToObject()); + v8::Persistent source; + NanAssignPersistent(source, args[0]->ToObject()); + auto request = reinterpret_cast(args[1].As()->Value()); - auto req = new NodeRequest(source, request); + auto req = new NodeRequest(NanNew(std::move(source)), request); req->Wrap(args.This()); NanReturnValue(args.This()); } v8::Handle NodeRequest::Create(v8::Handle source, mbgl::Request *request) { - NanScope(); - v8::Local argv[] = { v8::Local::New(source), NanNew(request) }; - auto instance = constructorTemplate->GetFunction()->NewInstance(2, argv); + NanEscapableScope(); - instance->Set(NanNew("url"), NanNew(request->resource.url), v8::ReadOnly); - instance->Set(NanNew("kind"), NanNew(int(request->resource.kind)), v8::ReadOnly); + v8::Local argv[] = { NanNew(source), NanNew(request) }; + auto instance = NanNew(constructorTemplate)->GetFunction()->NewInstance(2, argv); - NanReturnValue(instance); + instance->ForceSet(NanNew("url"), NanNew(request->resource.url), v8::ReadOnly); + instance->ForceSet(NanNew("kind"), NanNew(int(request->resource.kind)), v8::ReadOnly); + + return NanEscapeScope(instance); } NAN_METHOD(NodeRequest::Respond) { - NanScope(); - auto nodeRequest = ObjectWrap::Unwrap(args.Holder()); if (!nodeRequest->request) { return NanThrowError("Request has already been responded to, or was canceled."); } - auto source = ObjectWrap::Unwrap(nodeRequest->source); + auto source = ObjectWrap::Unwrap(NanNew(nodeRequest->source)); auto request = nodeRequest->request; nodeRequest->request = nullptr; @@ -131,12 +132,13 @@ NAN_METHOD(NodeRequest::Respond) { //////////////////////////////////////////////////////////////////////////////////////////////// // Instance -NodeRequest::NodeRequest(v8::Persistent source_, mbgl::Request *request_) - : source(source_), request(request_) { +NodeRequest::NodeRequest(v8::Local source_, mbgl::Request *request_) + : request(request_) { + NanAssignPersistent(source, source_); } NodeRequest::~NodeRequest() { - source.Dispose(); + NanDisposePersistent(source); } void NodeRequest::cancel() { diff --git a/src/node_request.hpp b/src/node_request.hpp index ebbd962d4..009855066 100644 --- a/src/node_request.hpp +++ b/src/node_request.hpp @@ -27,7 +27,7 @@ public: //////////////////////////////////////////////////////////////////////////////////////////////// // Instance public: - NodeRequest(v8::Persistent source, mbgl::Request *request); + NodeRequest(v8::Local source, mbgl::Request *request); ~NodeRequest(); void cancel();