Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Compile node bindings with Nan 2.0 #2261

Merged
merged 4 commits into from
Sep 9, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ matrix:
apt:
packages: [ 'lib32stdc++6' ]
- os: linux
env: FLAVOR=node CXX=clang++-3.5 BUILDTYPE=Release NODE_VERSION=iojs-v2
env: FLAVOR=node CXX=clang++-3.5 BUILDTYPE=Release NODE_VERSION=iojs-v3
addons:
apt:
sources: [ 'ubuntu-toolchain-r-test', 'llvm-toolchain-precise-3.5' ]
Expand All @@ -79,7 +79,7 @@ matrix:
- os: osx
osx_image: xcode6.4
compiler: clang
env: FLAVOR=node NODE_VERSION=iojs-v2
env: FLAVOR=node NODE_VERSION=iojs-v3
- os: osx
osx_image: xcode6.4
compiler: clang
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"dependencies": {
"nan": "^1.9.0",
"nan": "nodejs/nan#a71301e0f62877ca017c4ebec7fbac9f4359038e",
"node-pre-gyp": "^0.6.9"
},
"bundledDependencies": [
Expand Down
1 change: 1 addition & 0 deletions platform/node/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
render options.
- Adds support for rendering v8 styles.
- No longer load resources before a render request is made.
- Adds io.js v3.x support.

# 1.1.3

Expand Down
49 changes: 12 additions & 37 deletions platform/node/src/node_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct NodeFileSource::Action {
mbgl::Resource const resource;
};

NodeFileSource::NodeFileSource(v8::Handle<v8::Object> options_) :
NodeFileSource::NodeFileSource(v8::Local<v8::Object> options_) :
queue(new Queue(uv_default_loop(), [this](Action &action) {
if (action.type == Action::Add) {
processAdd(action.resource);
Expand All @@ -20,7 +20,7 @@ NodeFileSource::NodeFileSource(v8::Handle<v8::Object> options_) :
}
}))
{
NanAssignPersistent(options, options_->ToObject());
options.Reset(options_);

// Make sure that the queue doesn't block the loop from exiting.
queue->unref();
Expand All @@ -30,7 +30,7 @@ NodeFileSource::~NodeFileSource() {
queue->stop();
queue = nullptr;

NanDisposePersistent(options);
options.Reset();
}

mbgl::Request* NodeFileSource::request(const mbgl::Resource& resource, uv_loop_t* loop, Callback callback) {
Expand Down Expand Up @@ -68,77 +68,52 @@ void NodeFileSource::cancel(mbgl::Request* req) {
}

void NodeFileSource::processAdd(const mbgl::Resource& resource) {
NanScope();
Nan::HandleScope scope;

// Make sure the loop stays alive as long as request is pending.
if (pending.empty()) {
queue->ref();
}

auto requestHandle = NanNew<v8::Object>(NodeRequest::Create(this, resource));

v8::Persistent<v8::Object> requestPersistent;
NanAssignPersistent(requestPersistent, requestHandle);
pending.emplace(resource, std::move(requestPersistent));

#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
auto requestFunction = v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), options)->Get(NanNew("request")).As<v8::Function>();
#else
auto requestFunction = options->Get(NanNew("request")).As<v8::Function>();
#endif
v8::Local<v8::Object> requestHandle = NodeRequest::Create(this, resource)->ToObject();
pending.emplace(resource, requestHandle);

v8::Local<v8::Value> argv[] = { requestHandle };
NanMakeCallback(NanGetCurrentContext()->Global(), requestFunction, 1, argv);
Nan::MakeCallback(Nan::New(options), "request", 1, argv);
}

void NodeFileSource::processCancel(const mbgl::Resource& resource) {
NanScope();
Nan::HandleScope scope;

auto it = pending.find(resource);
if (it == pending.end()) {
// The response callback was already fired. There is no point in calling the cancelation
// callback because the request is already completed.
} else {
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
auto requestHandle = v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), it->second);
v8::Local<v8::Object> requestHandle = Nan::New(it->second);
it->second.Reset();
#else
auto requestHandle = NanNew<v8::Object>(it->second);
NanDisposePersistent(it->second);
#endif
pending.erase(it);

// Make sure the the loop can exit when there are no pending requests.
if (pending.empty()) {
queue->unref();
}

#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
auto optionsObject = v8::Local<v8::Object>::New(v8::Isolate::GetCurrent(), options);
if (optionsObject->Has(NanNew("cancel"))) {
auto cancelFunction = optionsObject->Get(NanNew("cancel")).As<v8::Function>();
#else
if (options->Has(NanNew("cancel"))) {
auto cancelFunction = options->Get(NanNew("cancel")).As<v8::Function>();
#endif
if (Nan::Has(Nan::New(options), Nan::New("cancel").ToLocalChecked()).FromJust()) {
v8::Local<v8::Value> argv[] = { requestHandle };
NanMakeCallback(NanGetCurrentContext()->Global(), cancelFunction, 1, argv);
Nan::MakeCallback(Nan::New(options), "cancel", 1, argv);
}

// Set the request handle in the request wrapper handle to null
node::ObjectWrap::Unwrap<NodeRequest>(requestHandle)->cancel();
Nan::ObjectWrap::Unwrap<NodeRequest>(requestHandle)->cancel();
}
}

void NodeFileSource::notify(const mbgl::Resource& resource, const std::shared_ptr<const mbgl::Response>& response) {
// First, remove the request, since it might be destructed at any point now.
auto it = pending.find(resource);
if (it != pending.end()) {
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
it->second.Reset();
#else
NanDisposePersistent(it->second);
#endif
pending.erase(it);

// Make sure the the loop can exit when there are no pending requests.
Expand Down
12 changes: 3 additions & 9 deletions platform/node/src/node_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check to see if nan.h compiles cleanly without disabling these warnings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were originally added because of unused params in V8 itself IIRC.

#pragma GCC diagnostic ignored "-Wshadow"
#include <node.h>
#include <node_version.h>
#include <nan.h>
#pragma GCC diagnostic pop

Expand All @@ -20,7 +18,7 @@ namespace util { template <typename T> class AsyncQueue; }

class NodeFileSource : public mbgl::FileSource {
public:
NodeFileSource(v8::Handle<v8::Object>);
NodeFileSource(v8::Local<v8::Object>);
~NodeFileSource();

mbgl::Request* request(const mbgl::Resource&, uv_loop_t*, Callback);
Expand All @@ -36,14 +34,10 @@ class NodeFileSource : public mbgl::FileSource {
void processAdd(const mbgl::Resource&);
void processCancel(const mbgl::Resource&);

v8::Persistent<v8::Object> options;
Nan::Persistent<v8::Object> options;

private:
#if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
std::unordered_map<mbgl::Resource, v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object>>, mbgl::Resource::Hash> pending;
#else
std::unordered_map<mbgl::Resource, v8::Persistent<v8::Object>, mbgl::Resource::Hash> pending;
#endif
std::unordered_map<mbgl::Resource, Nan::Persistent<v8::Object>, mbgl::Resource::Hash> pending;

// The observers list will hold pointers to all the requests waiting
// for a particular resource. The access must be guarded by a mutex
Expand Down
35 changes: 22 additions & 13 deletions platform/node/src/node_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,43 @@ struct NodeLogObserver::LogMessage {
text(text_) {}
};

NodeLogObserver::NodeLogObserver(v8::Handle<v8::Object> target)
NodeLogObserver::NodeLogObserver(v8::Local<v8::Object> target)
: queue(new Queue(uv_default_loop(), [this](LogMessage &message) {
NanScope();
Nan::HandleScope scope;

auto msg = Nan::New<v8::Object>();

Nan::Set(msg, Nan::New("class").ToLocalChecked(),
Nan::New(mbgl::EventClass(message.event).c_str()).ToLocalChecked());

Nan::Set(msg, Nan::New("severity").ToLocalChecked(),
Nan::New(mbgl::EventSeverityClass(message.severity).c_str()).ToLocalChecked());

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) {
msg->Set(NanNew("code"), NanNew<v8::Number>(message.code));
Nan::Set(msg, Nan::New("code").ToLocalChecked(),
Nan::New<v8::Number>(message.code));
}

if (!message.text.empty()) {
msg->Set(NanNew("text"), NanNew(message.text));
Nan::Set(msg, Nan::New("text").ToLocalChecked(),
Nan::New(message.text).ToLocalChecked());
}

v8::Local<v8::Value> argv[] = { NanNew("message"), msg };
auto handle = NanNew<v8::Object>(module);
auto emit = handle->Get(NanNew("emit"))->ToObject();
emit->CallAsFunction(handle, 2, argv);
v8::Local<v8::Value> argv[] = { Nan::New("message").ToLocalChecked(), msg };
auto handle = Nan::New<v8::Object>(module);
auto emit = Nan::Get(handle, Nan::New("emit").ToLocalChecked()).ToLocalChecked()->ToObject();
Nan::CallAsFunction(emit, handle, 2, argv);
})) {
NanScope();
NanAssignPersistent(module, target);
Nan::HandleScope scope;
module.Reset(target);

// Don't keep the event loop alive.
queue->unref();
}

NodeLogObserver::~NodeLogObserver() {
queue->stop();
module.Reset();
}

bool NodeLogObserver::onRecord(mbgl::EventSeverity severity, mbgl::Event event, int64_t code, const std::string &text) {
Expand Down
5 changes: 2 additions & 3 deletions platform/node/src/node_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wshadow"
#include <node.h>
#include <nan.h>
#pragma GCC diagnostic pop

Expand All @@ -15,14 +14,14 @@ namespace util { template <typename T> class AsyncQueue; }

class NodeLogObserver : public mbgl::Log::Observer {
public:
NodeLogObserver(v8::Handle<v8::Object> target);
NodeLogObserver(v8::Local<v8::Object> target);
~NodeLogObserver();

// Log::Observer implementation
virtual bool onRecord(mbgl::EventSeverity severity, mbgl::Event event, int64_t code, const std::string &msg) override;

private:
v8::Persistent<v8::Object> module;
Nan::Persistent<v8::Object> module;

struct LogMessage;
using Queue = util::AsyncQueue<LogMessage>;
Expand Down
Loading