add node v0.12 and io.js compatibility

This commit is contained in:
Mike Morris
2015-02-19 16:19:31 -05:00
parent 1455822111
commit c0fe624ecd
8 changed files with 63 additions and 33 deletions

View File

@@ -8,15 +8,15 @@ public:
CompressPNGWorker(NanCallback *callback_, v8::Local<v8::Object> buffer_, uint32_t width_,
uint32_t height_)
: NanAsyncWorker(callback_),
buffer(v8::Persistent<v8::Object>::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() {

View File

@@ -96,11 +96,18 @@ void NodeFileSource::processAdd(mbgl::Request *req) {
queue->ref();
}
auto requestHandle = v8::Local<v8::Object>::New(NodeRequest::Create(handle_, req));
pending.emplace(req, std::move(v8::Persistent<v8::Object>::New(requestHandle)));
auto handle = NanObjectWrapHandle(this);
auto requestHandle = NanNew<v8::Object>(NodeRequest::Create(handle, request));
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
v8::Persistent<v8::Object> requestPersistent;
NanAssignPersistent(requestPersistent, requestHandle);
#else
const v8::UniquePersistent<v8::Object> requestPersistent(v8::Isolate::GetCurrent(), requestHandle);
#endif
pending.emplace(request, std::move(requestPersistent));
v8::Local<v8::Value> 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<v8::Object>::New(it->second);
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
auto requestHandle = NanNew<v8::Object>(it->second);
#else
auto requestHandle = v8::Local<v8::Object>::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<v8::Value> 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_ptr<const mbgl
// First, remove the request, since it might be destructed at any point now.
auto it = pending.find(req);
if (it != pending.end()) {
it->second.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.

View File

@@ -46,7 +46,11 @@ private:
void processCancel(mbgl::Request*);
private:
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
std::map<mbgl::Request*, v8::Persistent<v8::Object>> pending;
#else
std::map<mbgl::Request*, const v8::UniquePersistent<v8::Object> &> pending;
#endif
Queue *queue = nullptr;
};

View File

@@ -32,7 +32,7 @@ NodeLogObserver::NodeLogObserver(v8::Handle<v8::Object> target)
: queue(new Queue(uv_default_loop(), [this](LogMessage &message) {
NanScope();
auto msg = v8::Object::New();
auto msg = NanNew<v8::Object>();
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<v8::Object> target)
}
v8::Local<v8::Value> argv[] = { NanNew("message"), msg };
auto emit = module->Get(NanNew("emit"))->ToObject();
emit->CallAsFunction(module, 2, argv);
auto handle = NanNew<v8::Object>(module);
auto emit = handle->Get(NanNew("emit"))->ToObject();
emit->CallAsFunction(handle, 2, argv);
})) {
NanScope();
module = v8::Persistent<v8::Object>::New(target);
NanAssignPersistent(module, target);
// Don't keep the event loop alive.
queue->unref();

View File

@@ -239,7 +239,7 @@ void NodeMap::renderFinished() {
assert(!image);
if (img) {
v8::Local<v8::Object> result = v8::Object::New();
auto result = NanNew<v8::Object>();
result->Set(NanNew("width"), NanNew(img->width));
result->Set(NanNew("height"), NanNew(img->height));
@@ -283,7 +283,11 @@ NodeMap::NodeMap(v8::Handle<v8::Object> 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<NodeMap *>(as->data)->renderFinished();
});
@@ -292,7 +296,7 @@ NodeMap::NodeMap(v8::Handle<v8::Object> source_) :
}
NodeMap::~NodeMap() {
source.Dispose();
NanDisposePersistent(source);
uv_close(reinterpret_cast<uv_handle_t *>(async), [] (uv_handle_t *handle) {
delete reinterpret_cast<uv_async_t *>(handle);

View File

@@ -11,7 +11,6 @@
#include "node_request.hpp"
#include "compress_png.hpp"
void RegisterModule(v8::Handle<v8::Object> exports) {
NanScope();
@@ -22,7 +21,7 @@ void RegisterModule(v8::Handle<v8::Object> exports) {
// Exports Resource constants.
auto ConstantProperty = static_cast<v8::PropertyAttribute>(v8::ReadOnly | v8::DontDelete);
auto resource = v8::Object::New();
auto resource = NanNew<v8::Object>();
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);

View File

@@ -35,34 +35,35 @@ NAN_METHOD(NodeRequest::New) {
return NanThrowTypeError("Cannot create Request objects explicitly");
}
auto source = v8::Persistent<v8::Object>::New(args[0]->ToObject());
v8::Persistent<v8::Object> source;
NanAssignPersistent(source, args[0]->ToObject());
auto request = reinterpret_cast<mbgl::Request *>(args[1].As<v8::External>()->Value());
auto req = new NodeRequest(source, request);
auto req = new NodeRequest(NanNew<v8::Object>(std::move(source)), request);
req->Wrap(args.This());
NanReturnValue(args.This());
}
v8::Handle<v8::Object> NodeRequest::Create(v8::Handle<v8::Object> source, mbgl::Request *request) {
NanScope();
v8::Local<v8::Value> argv[] = { v8::Local<v8::Object>::New(source), NanNew<v8::External>(request) };
auto instance = constructorTemplate->GetFunction()->NewInstance(2, argv);
NanEscapableScope();
instance->Set(NanNew("url"), NanNew(request->resource.url), v8::ReadOnly);
instance->Set(NanNew("kind"), NanNew<v8::Integer>(int(request->resource.kind)), v8::ReadOnly);
v8::Local<v8::Value> argv[] = { NanNew<v8::Object>(source), NanNew<v8::External>(request) };
auto instance = NanNew<v8::FunctionTemplate>(constructorTemplate)->GetFunction()->NewInstance(2, argv);
NanReturnValue(instance);
instance->ForceSet(NanNew("url"), NanNew(request->resource.url), v8::ReadOnly);
instance->ForceSet(NanNew("kind"), NanNew<v8::Integer>(int(request->resource.kind)), v8::ReadOnly);
return NanEscapeScope(instance);
}
NAN_METHOD(NodeRequest::Respond) {
NanScope();
auto nodeRequest = ObjectWrap::Unwrap<NodeRequest>(args.Holder());
if (!nodeRequest->request) {
return NanThrowError("Request has already been responded to, or was canceled.");
}
auto source = ObjectWrap::Unwrap<NodeFileSource>(nodeRequest->source);
auto source = ObjectWrap::Unwrap<NodeFileSource>(NanNew<v8::Object>(nodeRequest->source));
auto request = nodeRequest->request;
nodeRequest->request = nullptr;
@@ -131,12 +132,13 @@ NAN_METHOD(NodeRequest::Respond) {
////////////////////////////////////////////////////////////////////////////////////////////////
// Instance
NodeRequest::NodeRequest(v8::Persistent<v8::Object> source_, mbgl::Request *request_)
: source(source_), request(request_) {
NodeRequest::NodeRequest(v8::Local<v8::Object> source_, mbgl::Request *request_)
: request(request_) {
NanAssignPersistent(source, source_);
}
NodeRequest::~NodeRequest() {
source.Dispose();
NanDisposePersistent(source);
}
void NodeRequest::cancel() {

View File

@@ -27,7 +27,7 @@ public:
////////////////////////////////////////////////////////////////////////////////////////////////
// Instance
public:
NodeRequest(v8::Persistent<v8::Object> source, mbgl::Request *request);
NodeRequest(v8::Local<v8::Object> source, mbgl::Request *request);
~NodeRequest();
void cancel();