v8 (javascript) String::新断言

发布于 2024-12-23 06:25:45 字数 4697 浏览 0 评论 0原文

我最近使用 v8(谷歌的 javascript 引擎)。当 GC 运行时,String::New 总是在 ASSERT(state_ != NEAR_DEATH)(global-handles.cc 237line) 处触发断言。确实有一些建议。

这是我的一些源代码片段:

void javascript_ctx_impl::call_obj_func (v8::persistent<object> object, const char* method, int argc, handle<value> argv[]) 
{ 
    handlescope handle_scope; 
    local<value> cb = object->get(string::new(method)); 
    if (!cb->isfunction()) { 
        std::cerr << "method = " << method << std::endl; 
                return; 
    } 
    local<function> do_action = local<function>::cast(cb); 
    trycatch try_catch; 
    /**ASSERT HERE **/
    **do_action->call(object, argc, argv);** 
    /**ASSERT HERE **/
    if (try_catch.hascaught()) { 
                v8::local<v8::message> msg = try_catch.message (); 
        if (!msg->getscriptresourcename().isempty() && !msg- 
>getscriptresourcename()->isundefined()) 

        { 
            v8::string::asciivalue name (msg- 
>getscriptresourcename()); 

            std::cerr << *name << std::endl; 
        } 
        else { 
            std::cerr << "call_obj_func: runtime error." << std::endl; 
        } 
    } 
} 

template <typename T> 
class write_handle : public handle_impl_base 
{ 
    public: 
       write_handle (boost::asio::io_service& io, v8::Persistent<Object> 
local,v8::Persistent<Object> h) 
       : handle_impl_base (io), handle_ (h), session_ (local) 
       { 
       } 
    public: 
        void operator () (const boost::system::error_code& ec, 
std::size_t bytes_transferred) 
        { 
            HandleScope handle_scope; 
            if (!ec) { 
                Handle<Value> args[3] = { 
                    js::instance ().safe_new_value (session_), 
                                        js::instance ().safe_new_value ("TRUE"), 
                                        js::instance ().safe_new_value (bytes_transferred) 
                }; 
                                js::instance ().call_obj_func (handle_, "onHandle", 3, args); 
            } 
            else { 
                Handle<Value> args[3] = { 
                    js::instance ().safe_new_value (session_), 
                                        js::instance ().safe_new_value ("FALSE"), 
                                        js::instance ().safe_new_value (bytes_transferred) 
                }; 
                                js::instance ().call_obj_func (handle_, "onHandle", 3, args); 
            } 
            handle_.Dispose (); session_.Dispose (); 
        } 
        static void handle_weak (Persistent<Value> object, void* 
parameter) 
        { 
            object.Dispose ();
        } 
    private: 
        v8::Persistent<Object> handle_; 
        v8::Persistent<Object> session_; 
}; 

v8::Handle<v8::Value> js_asio_socket_ip_tcp_function::async_write (const v8::Arguments& args) 
{ 
    HandleScope hScope; 
    js_asio_socket_ip_tcp_function* native_obj = 
unwrap<js_asio_socket_ip_tcp_function>(args.This()); 
    if (args.Length () < 4) { 
        return ThrowException (Exception::TypeError(String::New( 
                        "async_resolve need 4 parameters.")) 
                ); 
    } 
    /** Argument check here */ 
    js_stream_function* s = unwrap<js_stream_function> (args[1]- 
>ToObject ()); 

    if (s == NULL) { 
        return ThrowException (Exception::TypeError(String::New( 
                        "async_resolve parameter 2 error.")) 
                ); 
    } 
    v8::Local<v8::Object>  p0 = args[0]->ToObject (); 
    v8::Local<v8::Integer> p2 = args[2]->ToUint32 (); 
    v8::Persistent<Object> handle; 
    v8::Persistent<Object> sessin; 
    if (args[3]->ToObject ()->IsFunction ()) { 
        v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(args[3]->ToObject()); 
        handle = v8::Persistent<v8::Object>::New(f); 
    } 
    else { 
        handle = v8::Persistent<Object>::New (args[3]->ToObject ()); 
    } 
    handle.MakeWeak (NULL, write_handle<void>::handle_weak); 
    handle.MarkIndependent (); 
    sessin = v8::Persistent<Object>::New (p0); 
    boost::asio::async_write (*(native_obj->socket_), 
            boost::asio::buffer (s->get (), p2->Value ()), 
            boost::asio::transfer_all (), 
            make_concrete_handle (write_handle <void> (native_obj- 
>socket_->get_io_service (), sessin, handle) 

                ) 
            ); 
    return v8::Undefined (); 
} 

I work with v8 (google's javascript engine) recently. When the GC is running, String::New always trigger a assert at ASSERT(state_ != NEAR_DEATH)(global-handles.cc 237line). Does have some suggestion.

Here is some of my source snippet:

void javascript_ctx_impl::call_obj_func (v8::persistent<object> object, const char* method, int argc, handle<value> argv[]) 
{ 
    handlescope handle_scope; 
    local<value> cb = object->get(string::new(method)); 
    if (!cb->isfunction()) { 
        std::cerr << "method = " << method << std::endl; 
                return; 
    } 
    local<function> do_action = local<function>::cast(cb); 
    trycatch try_catch; 
    /**ASSERT HERE **/
    **do_action->call(object, argc, argv);** 
    /**ASSERT HERE **/
    if (try_catch.hascaught()) { 
                v8::local<v8::message> msg = try_catch.message (); 
        if (!msg->getscriptresourcename().isempty() && !msg- 
>getscriptresourcename()->isundefined()) 

        { 
            v8::string::asciivalue name (msg- 
>getscriptresourcename()); 

            std::cerr << *name << std::endl; 
        } 
        else { 
            std::cerr << "call_obj_func: runtime error." << std::endl; 
        } 
    } 
} 

template <typename T> 
class write_handle : public handle_impl_base 
{ 
    public: 
       write_handle (boost::asio::io_service& io, v8::Persistent<Object> 
local,v8::Persistent<Object> h) 
       : handle_impl_base (io), handle_ (h), session_ (local) 
       { 
       } 
    public: 
        void operator () (const boost::system::error_code& ec, 
std::size_t bytes_transferred) 
        { 
            HandleScope handle_scope; 
            if (!ec) { 
                Handle<Value> args[3] = { 
                    js::instance ().safe_new_value (session_), 
                                        js::instance ().safe_new_value ("TRUE"), 
                                        js::instance ().safe_new_value (bytes_transferred) 
                }; 
                                js::instance ().call_obj_func (handle_, "onHandle", 3, args); 
            } 
            else { 
                Handle<Value> args[3] = { 
                    js::instance ().safe_new_value (session_), 
                                        js::instance ().safe_new_value ("FALSE"), 
                                        js::instance ().safe_new_value (bytes_transferred) 
                }; 
                                js::instance ().call_obj_func (handle_, "onHandle", 3, args); 
            } 
            handle_.Dispose (); session_.Dispose (); 
        } 
        static void handle_weak (Persistent<Value> object, void* 
parameter) 
        { 
            object.Dispose ();
        } 
    private: 
        v8::Persistent<Object> handle_; 
        v8::Persistent<Object> session_; 
}; 

v8::Handle<v8::Value> js_asio_socket_ip_tcp_function::async_write (const v8::Arguments& args) 
{ 
    HandleScope hScope; 
    js_asio_socket_ip_tcp_function* native_obj = 
unwrap<js_asio_socket_ip_tcp_function>(args.This()); 
    if (args.Length () < 4) { 
        return ThrowException (Exception::TypeError(String::New( 
                        "async_resolve need 4 parameters.")) 
                ); 
    } 
    /** Argument check here */ 
    js_stream_function* s = unwrap<js_stream_function> (args[1]- 
>ToObject ()); 

    if (s == NULL) { 
        return ThrowException (Exception::TypeError(String::New( 
                        "async_resolve parameter 2 error.")) 
                ); 
    } 
    v8::Local<v8::Object>  p0 = args[0]->ToObject (); 
    v8::Local<v8::Integer> p2 = args[2]->ToUint32 (); 
    v8::Persistent<Object> handle; 
    v8::Persistent<Object> sessin; 
    if (args[3]->ToObject ()->IsFunction ()) { 
        v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(args[3]->ToObject()); 
        handle = v8::Persistent<v8::Object>::New(f); 
    } 
    else { 
        handle = v8::Persistent<Object>::New (args[3]->ToObject ()); 
    } 
    handle.MakeWeak (NULL, write_handle<void>::handle_weak); 
    handle.MarkIndependent (); 
    sessin = v8::Persistent<Object>::New (p0); 
    boost::asio::async_write (*(native_obj->socket_), 
            boost::asio::buffer (s->get (), p2->Value ()), 
            boost::asio::transfer_all (), 
            make_concrete_handle (write_handle <void> (native_obj- 
>socket_->get_io_service (), sessin, handle) 

                ) 
            ); 
    return v8::Undefined (); 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-12-30 06:25:45

您的 write_handle::handle_weak 为空。它永远不应该为空,因为这违反了 https://github.com/v8/v8/blob/master/include/v8.h#L124-132

Your write_handle<T>::handle_weak is empty. It should never be empty as this violates its contract documented at https://github.com/v8/v8/blob/master/include/v8.h#L124-132

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文