cesanta / v7

Embedded JavaScript engine for C/C++

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Make user_data property as _V7_PROPERTY_OFF_HEAP

opened this issue · comments

I need to attach the full (void*) data into v7 objects, the problem is that v7 make my user_data as foreign, so special datas like (void*)0 / (void*)1 are forbidden.

I hack v7 source and find a solution. I make the user_data property as _V7_PROPERTY_OFF_HEAP, and fixed some code to be friendly with it.

Source change list:

In get_or_create_user_data_property, make the made property as _V7_PROPERTY_OFF_HEAP:

// p->attributes |= _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR | _V7_PROPERTY_HIDDEN;
p->attributes |= _V7_PROPERTY_USER_DATA_AND_DESTRUCTOR | _V7_PROPERTY_HIDDEN | 
                 _V7_PROPERTY_OFF_HEAP;

In v7_set_user_data, set user_data as raw:

// p->value = v7_mk_foreign(v7, ud);
p->value = ud;

In v7_get_user_data, return the raw user_data:

// return v7_get_ptr(v7, p->value);
return p->value;

In v7_set_destructor_cb, set cb as raw:

// p->name = v7_mk_foreign(v7, fu.v);
p->name = fu.v;

In generic_object_destructor, use raw cb and user_data:

// v7_destructor_cb_t *cb = 
  // (v7_destructor_cb_t *) v7_get_ptr(v7, p->name);
// cb(v7, v7_get_ptr(v7, p->value));
v7_destructor_cb_t *cb = p->name;
cb(v7, p->value);

Finally in gc_mark , I think there should be an BUG ??

  /* mark object itself, and its properties */
  for ((prop = obj_base->properties), MARK(obj_base); prop != NULL;
       prop = next) {
    if (prop->attributes & _V7_PROPERTY_OFF_HEAP) {
      //break;
      next = prop->next;
      MARK(prop);  // mark or gc_sweep will free it
      continue;
    }
    ...
  }

Am I missing something?