2

I'm migrating from v8 3.x to 4.y and I'm having problems with one of the functions defined in one of my header file.

//JavascriptBase.h
namespace Company {
    class Base {
      protected:
        void registerHandler(v8::Persistent<v8::Function>& func) {
            user_functions_.push_back(func);
        }
      private:
        std::vector<v8::Persistent<v8::Function>> user_functions_;
    }
}

When I try to compile I get the following errors:

third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:667:53: error: assigning to 'v8::Object *volatile' from incompatible type 'v8::Primitive *'
    while (false) { *(static_cast<O* volatile*>(0)) = static_cast<Primitive*>(0); };
                                                    ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:663:5: note: in instantiation of function template specialization 'v8::NonCopyablePersistentTraits<v8::Function>::Uncompilable<v8::Object>' requested here
    Uncompilable<Object>();
    ^
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:7175:6: note: in instantiation of function template specialization 'v8::NonCopyablePersistentTraits<v8::Function>::Copy<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >' requested here
  M::Copy(that, this);
     ^
third-party2/v8/4.7.39/gcc-4.8.1-glibc-2.17-fb/281a9e6/include/v8.h:729:5: note: in instantiation of function template specialization 'v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >::Copy<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >' requested here
    Copy(that);
    ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/ext/new_allocator.h:120:23: note: in instantiation of member function 'v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >::Persistent' requested here
 { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
                      ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/bits/alloc_traits.h:254:8: note: in instantiation of function template specialization '__gnu_cxx::new_allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > >::construct<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, const v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > &>' requested here
 { __a.construct(__p, std::forward<_Args>(__args)...); }
       ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/bits/alloc_traits.h:393:4: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > > >::_S_construct<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, const v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > &>' requested here
 { _S_construct(__a, __p, std::forward<_Args>(__args)...); }
   ^
third-party2/libgcc/4.8.1/gcc-4.8.1-glibc-2.17-fb/8aac7fc/include/c++/4.8.1/bits/stl_vector.h:905:21: note: in instantiation of function template specialization 'std::allocator_traits<std::allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > > >::construct<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, const v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > &>' requested here
     _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
                    ^
./Company/JavaScriptBase.h:84:21: note: in instantiation of member function 'std::vector<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> >, std::allocator<v8::Persistent<v8::Function, v8::NonCopyablePersistentTraits<v8::Function> > > >::push_back' requested here
    user_functions_.push_back(func);
                    ^
1 error generated.

I've done some googling and found that Persistent objects aren't assignable but I'm not sure what to do with this information. Any help would be appreciated.

Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
  • Possible duplicate of [Storing handles to objects in a hashmap or set in Google's V8 engine](http://stackoverflow.com/questions/21239249/storing-handles-to-objects-in-a-hashmap-or-set-in-googles-v8-engine) – smirnoff Feb 27 '16 at 19:41

2 Answers2

3

If they aren't assignable then you (obviously) can't assign them. And since std::vector<> relies on copying values using the assignment operator, your approach won't work.

You can however store pointers to them instead, since copying pointers of anything is well defined.

Blindy
  • 65,249
  • 10
  • 91
  • 131
0

You can make a copyable persistent with this "typedef"

using CopyablePersistent = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;

This assumes it's inside a templated class, but you can make the T whatever you want.

edit: apparently this is a "very bad" thing to do, but I don't know why.

xaxxon
  • 19,189
  • 5
  • 50
  • 80
  • Why it's a "very bad" thing? Could you provide link to the source? – smirnoff Feb 27 '16 at 19:42
  • Nope. Just been told that by devs in #v8 on freenode. They said it will lead to intermittent crashes. It's really never needed, though. You can pass around Global& in your code and for use in STL containers or you can always make a new one with Global's normal constructors, too. – xaxxon Feb 27 '16 at 19:46