Background:

I need to integrate the functionality of v8 engine in a project written in 
c. So i wrap a js object as:

typedef struct {
    v8::Persistent<v8::Object> obj;
} v8_obj;


typedef struct {
    v8::Persistent<v8::Function> fun;
} v8_fn;


void* create_v8_obj(Persistent<Object> obj) {
    v8_obj * _v8_obj = (v8_obj *)calloc(1, sizeof (v8_obj));
    _v8_obj->obj = obj;
    return _v8_obj;
}


void* create_v8_fun(Persistent<Function> fun) {
    v8_fn * _v8_fn = (v8_fn *)calloc(1, sizeof (v8_fn));
    _v8_fn->fun = fun;
    return _v8_fn;
}


fb_script_cb_t* v8_obj_get(void *p_v8_obj, const char *key) {
    if (p_v8_obj != NULL && key != NULL) {
        v8_obj *_v8_obj = reinterpret_cast<v8_obj *>(p_v8_obj);
        LOGE("_v8_obj: %p, *obj:%p", _v8_obj, *(_v8_obj->obj));
        Local<Value> val = _v8_obj->obj->Get(String::New(key)); // crashed here
        if (val.IsEmpty()) {
            return NULL;
        }

        fb_script_cb_t *cb = fb_script_cb_new();
        if (val->IsFunction()) {
            void* v8_fun = 
create_v8_fun(Persistent<Function>::New(Handle<Function>::Cast(val)));
            fb_script_cb_set_js(cb, v8_fun);
        } else if (val->IsObject()) {
            void* v8_obj = 
create_v8_obj(Persistent<Object>::New(Handle<Object>::Cast(val)));
            fb_script_cb_set_obj(cb, v8_obj);
        } else if (val->IsNumber()) {
            fb_script_cb_set_int(cb, val->ToInt32()->Value());
        }
        return cb;
    }
    return NULL;
}


My scenario is:


1) in a accessor implement i create a v8_obj through create_v8_obj() to 
persistent reference(let's call it R1) to a js object(such as 

{

    "cell": function() {


    },

    ...

}

)


2) after some time, i need to access the property of R1 through v8_obj_get(..., 
"cell"), "cell" is one of the property of the js object mentioned in 1)


then the Persistent<Object> can't be used anyway, and crashed at the line in 
red. I verified that the address of the wrapper structure and the target object 
in Persistent<Object> is right. But the target object is gone away.


"

3583 03-04 20:18:50.368 27163 27163 E faywong : _v8_obj: 0xafdb0ef0, 
*obj:0x9f2bc050

3584 03-04 20:18:50.368 27163 27163 F libc    : Fatal signal 5 (SIGTRAP), code 
4 in tid 27163

"



So what's the problem in my solution? What's the best practice to retain a js 
object in c?



-- 
-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
--- 
You received this message because you are subscribed to the Google Groups 
"v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to