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

Compile node bindings with Nan 2.0 #2261

merged 4 commits into from
Sep 9, 2015

Conversation

jfirebaugh
Copy link
Contributor

This is failing on render tests because the callback is getting called with an object that has "pixels": "pixels" -- yes, the value is literally the string "pixels" rather than a Buffer containing pixel data. I don't know why that's happening. Presumably something about how I'm using Nan::NewBuffer is incorrect but I don't see what.

👀 @mikemorris

@@ -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.

@mikemorris
Copy link
Contributor

Huh, from a quick read-through usage of Nan::NewBuffer and Nan::Set looks correct...

@mikemorris
Copy link
Contributor

This is great btw, I had been meaning to do this but hadn't got around to it yet, I think this might even add iojs-v3.x support, or at least unblock it.

@jfirebaugh
Copy link
Contributor Author

Compile errors on iojs 2.5:

In file included from ../platform/node/src/node_map.cpp:1:
In file included from ../platform/node/src/node_map.hpp:3:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:27:
/home/travis/.node-gyp/2.5.0/src/node_object_wrap.h:34:12: error: static_cast
      from 'node::ObjectWrap *' to 'node_mbgl::NodeMap *' is not allowed
    return static_cast<T*>(wrap);
           ^~~~~~~~~~~~~~~~~~~~~
../platform/node/src/node_map.cpp:100:38: note: in instantiation of function
      template specialization 'node::ObjectWrap::Unwrap<node_mbgl::NodeMap>'
      requested here
    auto nodeMap = node::ObjectWrap::Unwrap<NodeMap>(info.Holder());
                                     ^
1 error generated.
make[2]: *** [Release/obj.target/mapbox-gl-native/platform/node/src/node_map.o] Error 1
make[2]: *** Waiting for unfinished jobs....
In file included from ../platform/node/src/node_file_source.cpp:1:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:303:
../node_modules/nan/nan_persistent_12_inl.h:22:7: error: type
      'v8::Persistent<Object, NonCopyablePersistentTraits<Object> >' is not a
      direct or virtual base of 'Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> >'
      v8::Persistent<T, M2>(v8::Isolate::GetCurrent(), that) {}
      ^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_pair.h:139:16: note: 
      in instantiation of function template specialization
      'Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object>
      >::Persistent<v8::Object, v8::NonCopyablePersistentTraits<v8::Object> >'
      requested here
        : first(__x), second(std::forward<_U2>(__y)) { }
                      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/alloc_traits.h:253:8: note: 
      in instantiation of function template specialization
      '__gnu_cxx::new_allocator<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >
      >::construct<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >, const mbgl::Resource &,
      Nan::Persistent<v8::Object, v8::NonCopyablePersistentTraits<v8::Object> >
      >' requested here
        { __a.construct(__p, std::forward<_Args>(__args)...); }
              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/alloc_traits.h:399:4: note: 
      in instantiation of function template specialization
      'std::allocator_traits<std::allocator<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >
      > >::_S_construct<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >,
      const mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/hashtable_policy.h:1957:28: note: 
      in instantiation of function template specialization
      'std::allocator_traits<std::allocator<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >
      > >::construct<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >, const mbgl::Resource &,
      Nan::Persistent<v8::Object, v8::NonCopyablePersistentTraits<v8::Object> >
      >' requested here
            __value_alloc_traits::construct(__a, __n->_M_valptr(),
                                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/hashtable.h:1541:30: note: 
      in instantiation of function template specialization
      'std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const
      mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >, true> >
      >::_M_allocate_node<const mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        __node_type* __node = this->_M_allocate_node(std::forward<_Args>...
                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/hashtable.h:718:11: note: 
      in instantiation of function template specialization
      'std::_Hashtable<mbgl::Resource, std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > >,
      std::__detail::_Select1st, std::equal_to<mbgl::Resource>,
      mbgl::Resource::Hash, std::__detail::_Mod_range_hashing,
      std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy,
      std::__detail::_Hashtable_traits<true, false, true> >::_M_emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        { return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
                 ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/unordered_map.h:341:16: note: 
      in instantiation of function template specialization
      'std::_Hashtable<mbgl::Resource, std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > >,
      std::__detail::_Select1st, std::equal_to<mbgl::Resource>,
      mbgl::Resource::Hash, std::__detail::_Mod_range_hashing,
      std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy,
      std::__detail::_Hashtable_traits<true, false, true> >::emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        { return _M_h.emplace(std::forward<_Args>(__args)...); }
                      ^
../platform/node/src/node_file_source.cpp:82:13: note: in instantiation of
      function template specialization 'std::unordered_map<mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> >,
      mbgl::Resource::Hash, std::equal_to<mbgl::Resource>,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > > >::emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
    pending.emplace(resource, std::move(requestPersistent));
            ^
In file included from ../platform/node/src/node_file_source.cpp:1:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:27:
/home/travis/.node-gyp/2.5.0/src/node_object_wrap.h:34:12: error: static_cast
      from 'node::ObjectWrap *' to 'node_mbgl::NodeRequest *' is not allowed
    return static_cast<T*>(wrap);
           ^~~~~~~~~~~~~~~~~~~~~
../platform/node/src/node_file_source.cpp:111:27: note: in instantiation of
      function template specialization
      'node::ObjectWrap::Unwrap<node_mbgl::NodeRequest>' requested here
        node::ObjectWrap::Unwrap<NodeRequest>(requestHandle)->cancel();
                          ^
2 errors generated.
make[2]: *** [Release/obj.target/mapbox-gl-native/platform/node/src/node_file_source.o] Error 1
make[2]: Leaving directory `/home/travis/build/mapbox/mapbox-gl-native/build'

@jfirebaugh
Copy link
Contributor Author

Node 0.12:

In file included from ../platform/node/src/node_file_source.cpp:1:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:303:
../node_modules/nan/nan_persistent_12_inl.h:22:7: error: type
      'v8::Persistent<Object, NonCopyablePersistentTraits<Object> >' is not a
      direct or virtual base of 'Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> >'
      v8::Persistent<T, M2>(v8::Isolate::GetCurrent(), that) {}
      ^~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/stl_pair.h:139:16: note: 
      in instantiation of function template specialization
      'Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object>
      >::Persistent<v8::Object, v8::NonCopyablePersistentTraits<v8::Object> >'
      requested here
        : first(__x), second(std::forward<_U2>(__y)) { }
                      ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/alloc_traits.h:253:8: note: 
      in instantiation of function template specialization
      '__gnu_cxx::new_allocator<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >
      >::construct<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >, const mbgl::Resource &,
      Nan::Persistent<v8::Object, v8::NonCopyablePersistentTraits<v8::Object> >
      >' requested here
        { __a.construct(__p, std::forward<_Args>(__args)...); }
              ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/alloc_traits.h:399:4: note: 
      in instantiation of function template specialization
      'std::allocator_traits<std::allocator<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >
      > >::_S_construct<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >,
      const mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
          ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/hashtable_policy.h:1957:28: note: 
      in instantiation of function template specialization
      'std::allocator_traits<std::allocator<std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >
      > >::construct<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >, const mbgl::Resource &,
      Nan::Persistent<v8::Object, v8::NonCopyablePersistentTraits<v8::Object> >
      >' requested here
            __value_alloc_traits::construct(__a, __n->_M_valptr(),
                                  ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/hashtable.h:1541:30: note: 
      in instantiation of function template specialization
      'std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const
      mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >, true> >
      >::_M_allocate_node<const mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        __node_type* __node = this->_M_allocate_node(std::forward<_Args>...
                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/hashtable.h:718:11: note: 
      in instantiation of function template specialization
      'std::_Hashtable<mbgl::Resource, std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > >,
      std::__detail::_Select1st, std::equal_to<mbgl::Resource>,
      mbgl::Resource::Hash, std::__detail::_Mod_range_hashing,
      std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy,
      std::__detail::_Hashtable_traits<true, false, true> >::_M_emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        { return _M_emplace(__unique_keys(), std::forward<_Args>(__args)...); }
                 ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/4.9/../../../../include/c++/4.9/bits/unordered_map.h:341:16: note: 
      in instantiation of function template specialization
      'std::_Hashtable<mbgl::Resource, std::pair<const mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> > >,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > >,
      std::__detail::_Select1st, std::equal_to<mbgl::Resource>,
      mbgl::Resource::Hash, std::__detail::_Mod_range_hashing,
      std::__detail::_Default_ranged_hash, std::__detail::_Prime_rehash_policy,
      std::__detail::_Hashtable_traits<true, false, true> >::emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
        { return _M_h.emplace(std::forward<_Args>(__args)...); }
                      ^
../platform/node/src/node_file_source.cpp:82:13: note: in instantiation of
      function template specialization 'std::unordered_map<mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> >,
      mbgl::Resource::Hash, std::equal_to<mbgl::Resource>,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > > >::emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      v8::NonCopyablePersistentTraits<v8::Object> > >' requested here
    pending.emplace(resource, std::move(requestPersistent));
            ^
In file included from ../platform/node/src/node_file_source.cpp:1:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:27:
/home/travis/.node-gyp/0.12.7/src/node_object_wrap.h:55:12: error: static_cast
      from 'node::ObjectWrap *' to 'node_mbgl::NodeRequest *' is not allowed
    return static_cast<T*>(wrap);
           ^~~~~~~~~~~~~~~~~~~~~
../platform/node/src/node_file_source.cpp:111:27: note: in instantiation of
      function template specialization
      'node::ObjectWrap::Unwrap<node_mbgl::NodeRequest>' requested here
        node::ObjectWrap::Unwrap<NodeRequest>(requestHandle)->cancel();
                          ^
2 errors generated.
make[2]: *** [Release/obj.target/mapbox-gl-native/platform/node/src/node_file_source.o] Error 1
make[2]: *** Waiting for unfinished jobs....
In file included from ../platform/node/src/node_map.cpp:1:
In file included from ../platform/node/src/node_map.hpp:3:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:27:
/home/travis/.node-gyp/0.12.7/src/node_object_wrap.h:55:12: error: static_cast
      from 'node::ObjectWrap *' to 'node_mbgl::NodeMap *' is not allowed
    return static_cast<T*>(wrap);
           ^~~~~~~~~~~~~~~~~~~~~
../platform/node/src/node_map.cpp:100:38: note: in instantiation of function
      template specialization 'node::ObjectWrap::Unwrap<node_mbgl::NodeMap>'
      requested here
    auto nodeMap = node::ObjectWrap::Unwrap<NodeMap>(info.Holder());
                                     ^
1 error generated.
make[2]: *** [Release/obj.target/mapbox-gl-native/platform/node/src/node_map.o] Error 1
make[2]: Leaving directory `/home/travis/build/mapbox/mapbox-gl-native/build'

@mikemorris
Copy link
Contributor

I think v8::Persistent either needs to change to v8::PersistentBase if we still need to specify non-copyable traits, or we might be able to drop the split and just use v8::Persistent everywhere again. The finickiness of this stems from v8::Persistent<T> no longer inherits from v8::Handle<T>.

@mikemorris
Copy link
Contributor

I wonder if the static_cast errors are just from using auto, nodejs/nan@00ee0fe added a test for this that looks pretty much the same as what we're doing.

MyObject* obj = ObjectWrap::Unwrap<MyObject>(info.This());

@jfirebaugh
Copy link
Contributor Author

@mikemorris AFAICT we shouldn't need to maintain a persistent handle to the request on the C++ side at all: we hand it off to the JS side and then it will be referenced by the callback's closure, so not GC'd:

        request: function (req) {
            request(req.url, {encoding: null}, function (err, response, body) {
                // This closure has a reference to `req`, so it isn't going to be GC'd so long as the closure isn't.
                req.respond(err, {data: body});
            });
        }

I'm going to try switching pending to a std::unordered_set<mbgl::Resource, mbgl::Resource::Hash>.

@mikemorris
Copy link
Contributor

it will be referenced by the callback's closure, so not GC'd

I swear I remember testing and this being insufficient to prevent the request from being GC'd, worth double-checking though.

EDIT: I think that was when we were still storing mbgl::Request objects on the pending map, might be unnecessary with mbgl::Resource objects.

@jfirebaugh
Copy link
Contributor Author

New error:

In file included from ../platform/node/src/node_file_source.cpp:1:
In file included from ../platform/node/src/node_file_source.hpp:8:
In file included from ../node_modules/nan/nan.h:24:
In file included from /home/travis/.node-gyp/2.5.0/src/node.h:42:
/home/travis/.node-gyp/2.5.0/deps/v8/include/v8.h:6477:6: error: no matching
      member function for call to 'Copy'
  M::Copy(that, this);
  ~~~^~~~
/home/travis/.node-gyp/2.5.0/deps/v8/include/v8.h:713:5: note: in instantiation
      of function template specialization 'v8::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> >::Copy<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> >' requested here
    Copy(that);
    ^
../node_modules/nan/nan_persistent_12_inl.h:12:40: note: in instantiation of
      member function 'v8::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> >::Persistent' requested here
template<typename T, typename M> class Persistent :
                                       ^
[snip]
../platform/node/src/node_file_source.cpp:82:13: note: in instantiation of
      function template specialization 'std::unordered_map<mbgl::Resource,
      Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object> >,
      mbgl::Resource::Hash, std::equal_to<mbgl::Resource>,
      std::allocator<std::pair<const mbgl::Resource, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > > > >::emplace<const
      mbgl::Resource &, Nan::Persistent<v8::Object,
      Nan::CopyablePersistentTraits<v8::Object> > >' requested here
    pending.emplace(resource, std::move(requestPersistent));
            ^
/home/travis/.node-gyp/2.5.0/deps/v8/include/v8.h:666:25: note: candidate
      function [with S = v8::Object, M =
      Nan::CopyablePersistentTraits<v8::Object>] not viable: no known conversion
      from 'v8::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object>
      > *' to 'CopyablePersistent *' (aka 'Persistent<v8::Object,
      CopyablePersistentTraits<v8::Object> > *') for 2nd argument
  static V8_INLINE void Copy(const Persistent<S, M>& source,
                        ^
1 error generated.

@mikemorris
Copy link
Contributor

@jfirebaugh Perhaps Nan::CopyablePersistentTraits doesn't implement move semantics for pending.emplace(resource, std::move(requestPersistent));? Pretty sure this would work with Nan::NonCopyablePersistentTraits

@mikemorris
Copy link
Contributor

the value is literally the string "pixels" rather than a Buffer containing pixel data

I'm only seeing this on Node v0.10.x (on Linux and OS X), probably just missing a NAN wrapper somewhere.

@mikemorris
Copy link
Contributor

Fixed the "pixels" bug for real this time nodejs/nan#443

@mikemorris
Copy link
Contributor

Can we squash/merge this @jfirebaugh? My patch landed in nan@2.0.9

@jfirebaugh
Copy link
Contributor Author

^ squashed, merge if it's green. This is sweet!

@jfirebaugh jfirebaugh merged commit 532bbd5 into master Sep 9, 2015
@jfirebaugh jfirebaugh deleted the nan-2 branch September 9, 2015 22:38
@mikemorris mikemorris added the Node.js node-mapbox-gl-native label Sep 10, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Node.js node-mapbox-gl-native
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants