diff --git a/jsgc.c b/jsgc.c index 3b65bdf..b6dd406 100644 --- a/jsgc.c +++ b/jsgc.c @@ -46,6 +46,8 @@ static void jsG_freeobject(js_State *J, js_Object *obj) jsG_freeiterator(J, obj->u.iter.head); if (obj->type == JS_CUSERDATA && obj->u.user.finalize) obj->u.user.finalize(J, obj->u.user.data); + if (obj->type == JS_CCFUNCTION && obj->u.c.finalize) + obj->u.c.finalize(J, obj->u.c.data); js_free(J, obj); } diff --git a/jsrun.c b/jsrun.c index 990d355..e6d6a96 100644 --- a/jsrun.c +++ b/jsrun.c @@ -176,10 +176,20 @@ void js_pushglobal(js_State *J) void js_currentfunction(js_State *J) { CHECKSTACK(1); - STACK[TOP] = STACK[BOT-1]; + if (BOT > 0) + STACK[TOP] = STACK[BOT-1]; + else + STACK[TOP].type = JS_TUNDEFINED; ++TOP; } +void *js_currentfunctiondata(js_State *J) +{ + if (BOT > 0) + return STACK[BOT-1].u.object->u.c.data; + return NULL; +} + /* Read values from stack */ static js_Value *stackidx(js_State *J, int idx) diff --git a/jsvalue.c b/jsvalue.c index a465a0b..b97a580 100644 --- a/jsvalue.c +++ b/jsvalue.c @@ -463,13 +463,15 @@ void js_newscript(js_State *J, js_Function *fun, js_Environment *scope) js_pushobject(J, obj); } -void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, int length) +void js_newcfunctionx(js_State *J, js_CFunction cfun, const char *name, int length, void *data, js_Finalize finalize) { js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype); obj->u.c.name = name; obj->u.c.function = cfun; obj->u.c.constructor = NULL; obj->u.c.length = length; + obj->u.c.data = data; + obj->u.c.finalize = finalize; js_pushobject(J, obj); { js_pushnumber(J, length); @@ -483,6 +485,11 @@ void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, int lengt } } +void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, int length) +{ + js_newcfunctionx(J, cfun, name, length, NULL, NULL); +} + /* prototype -- constructor */ void js_newcconstructor(js_State *J, js_CFunction cfun, js_CFunction ccon, const char *name, int length) { diff --git a/jsvalue.h b/jsvalue.h index dbb7b4b..880efe4 100644 --- a/jsvalue.h +++ b/jsvalue.h @@ -103,6 +103,8 @@ struct js_Object js_CFunction function; js_CFunction constructor; int length; + void *data; + js_Finalize finalize; } c; js_Regexp r; struct { diff --git a/mujs.h b/mujs.h index 3cc6a56..421e81a 100644 --- a/mujs.h +++ b/mujs.h @@ -149,6 +149,7 @@ void js_setindex(js_State *J, int idx, int i); void js_delindex(js_State *J, int idx, int i); void js_currentfunction(js_State *J); +void *js_currentfunctiondata(js_State *J); void js_pushglobal(js_State *J); void js_pushundefined(js_State *J); void js_pushnull(js_State *J); @@ -165,6 +166,7 @@ void js_newboolean(js_State *J, int v); void js_newnumber(js_State *J, double v); void js_newstring(js_State *J, const char *v); void js_newcfunction(js_State *J, js_CFunction fun, const char *name, int length); +void js_newcfunctionx(js_State *J, js_CFunction fun, const char *name, int length, void *data, js_Finalize finalize); void js_newcconstructor(js_State *J, js_CFunction fun, js_CFunction con, const char *name, int length); void js_newuserdata(js_State *J, const char *tag, void *data, js_Finalize finalize); void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete del, js_Finalize finalize);