2016-01-11 101 views
2

我正在从v8 3.x迁移到4.y,并且遇到了其中一个头文件中定义的某个函数的问题。不能将持久<Function>添加到向量中<Persistent <Function>>

//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_; 
    } 
} 

当我尝试编译我收到以下错误:

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. 

我做了一些谷歌上搜索,发现Persistent对象是不可转让的,但我不知道该怎么做此信息。任何帮助,将不胜感激。

+0

[将句柄存储到散列表中的对象或Google的V8引擎中的集合](http://stackoverflow.com/questions/21239249/storing-handles-to-objects-in-a-hashmap-or- set-in-googles-v8-engine) – smirnoff

回答

3

如果他们不能分配,那么你(显然)不能分配他们。并且由于std::vector<>依赖于使用赋值运算符复制值,因此您的方法无效。

然而,您可以存储指向它们的指针,因为任何东西的复制指针都已定义好。

+0

从技术上说,复制悬挂/无效指针是未定义的。但我想这些不是事物的指针。 :) –

+0

这是真的吗?我知道标准试图假装指针是某种特殊的魔法构造,而不仅仅是一堆数字,因为它现在是最常见的,但它是一个无效指针的事实有什么区别呢? – Blindy

+0

@LightnessRacesinOrbit,我相信,这一点是辩论? – SergeyA

0

可以使能够复制的执着与这个“类型定义”

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

这是假定它是一个模板类里面,但你可以做任何你想要的吨。

编辑:显然这是一个“非常糟糕的”事情,但我不知道为什么。

+0

为什么这是一件“非常糟糕”的事情?你能提供链接到源代码吗? – smirnoff

+0

没有。刚刚被freenode的#v8上的开发者告知。他们表示,这将导致间歇性崩溃。尽管如此,它确实不需要。你可以在你的代码中传递Global&,并在STL容器中使用,或者你也可以用Global的普通构造函数创建一个新的。 – xaxxon

相关问题