shithub: libmujs

Download patch

ref: aba6644234865be0687c7df4888bcc8d4ae0b147
parent: 2db68ab80ff130615b170742018243e52525f96a
author: Tor Andersson <tor.andersson@artifex.com>
date: Tue Mar 1 16:52:00 EST 2016

Add delete callback to userdata objects.

--- a/jsrun.c
+++ b/jsrun.c
@@ -686,6 +686,11 @@
 		if (!strcmp(name, "lastIndex")) goto dontconf;
 	}
 
+	else if (obj->type == JS_CUSERDATA) {
+		if (obj->u.user.delete && obj->u.user.delete(J, obj->u.user.data, name))
+			return 1;
+	}
+
 	ref = jsV_getownproperty(J, obj, name);
 	if (ref) {
 		if (ref->atts & JS_DONTCONF)
--- a/jsvalue.c
+++ b/jsvalue.c
@@ -433,7 +433,7 @@
 	}
 }
 
-void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Finalize finalize)
+void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete delete, js_Finalize finalize)
 {
 	js_Object *prototype = NULL;
 	js_Object *obj;
@@ -447,6 +447,7 @@
 	obj->u.user.data = data;
 	obj->u.user.has = has;
 	obj->u.user.put = put;
+	obj->u.user.delete = delete;
 	obj->u.user.finalize = finalize;
 	js_pushobject(J, obj);
 }
@@ -453,7 +454,7 @@
 
 void js_newuserdata(js_State *J, const char *tag, void *data, js_Finalize finalize)
 {
-	js_newuserdatax(J, tag, data, NULL, NULL, finalize);
+	js_newuserdatax(J, tag, data, NULL, NULL, NULL, finalize);
 }
 
 /* Non-trivial operations on values. These are implemented using the stack. */
--- a/jsvalue.h
+++ b/jsvalue.h
@@ -114,6 +114,7 @@
 			void *data;
 			js_HasProperty has;
 			js_Put put;
+			js_Delete delete;
 			js_Finalize finalize;
 		} user;
 	} u;
--- a/mujs.h
+++ b/mujs.h
@@ -32,6 +32,7 @@
 typedef void (*js_Finalize)(js_State *J, void *p);
 typedef int (*js_HasProperty)(js_State *J, void *p, const char *name);
 typedef int (*js_Put)(js_State *J, void *p, const char *name);
+typedef int (*js_Delete)(js_State *J, void *p, const char *name);
 
 /* Basic functions */
 js_State *js_newstate(js_Alloc alloc, void *actx, int flags);
@@ -143,7 +144,7 @@
 void js_newcfunction(js_State *J, js_CFunction fun, const char *name, unsigned int length);
 void js_newcconstructor(js_State *J, js_CFunction fun, js_CFunction con, const char *name, unsigned 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_Finalize finalize);
+void js_newuserdatax(js_State *J, const char *tag, void *data, js_HasProperty has, js_Put put, js_Delete delete, js_Finalize finalize);
 void js_newregexp(js_State *J, const char *pattern, int flags);
 
 void js_pushiterator(js_State *J, int idx, int own);