Add user data to C functions with extended constructor.

Accessible from C with js_currentfunctiondata(J).
This commit is contained in:
Tor Andersson
2021-10-19 15:47:16 +02:00
parent daa2241087
commit fe8cac61e3
5 changed files with 25 additions and 2 deletions

2
jsgc.c
View File

@@ -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);
}

12
jsrun.c
View File

@@ -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)

View File

@@ -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)
{

View File

@@ -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 {

2
mujs.h
View File

@@ -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);