From 24d68eb4e19e48c80a44af324499e1e16863e768 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Fri, 16 Nov 2012 22:05:38 +0000 Subject: ensure newly created objects survive teh garbage collector long enough to be constructed. --- src/jsapi-libdom.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/jsapi-libdom.c b/src/jsapi-libdom.c index 0711e75..476c99e 100644 --- a/src/jsapi-libdom.c +++ b/src/jsapi-libdom.c @@ -357,7 +357,16 @@ output_class_new(struct binding *binding) "\tif (newobject == NULL) {\n" "\t\tfree(private);\n" "\t\treturn NULL;\n" - "\t}\n" + "\t}\n\n"); + + /* root object to stop it being garbage collected */ + fprintf(binding->outfile, + "\tif (JS_AddRoot(cx, &newobject) != JS_TRUE) {\n" + "\t\tfree(private);\n" + "\t\treturn NULL;\n" + "\t}\n\n"); + + fprintf(binding->outfile, "\n" "\t/* attach private pointer */\n" "\tif (JS_SetPrivate(cx, newobject, private) != JS_TRUE) {\n" @@ -365,6 +374,7 @@ output_class_new(struct binding *binding) "\t\treturn NULL;\n" "\t}\n\n"); + /* attach operations and attributes (functions and properties) */ fprintf(binding->outfile, "\tif (JS_DefineFunctions(cx, newobject, jsclass_functions) != JS_TRUE) {\n" @@ -383,6 +393,12 @@ output_class_new(struct binding *binding) "\t\treturn NULL;\n" "\t}\n"); + /* root object to stop it being garbage collected */ + fprintf(binding->outfile, + "\tif (JS_AddRoot(cx, &newobject) != JS_TRUE) {\n" + "\t\treturn NULL;\n" + "\t}\n\n"); + /* attach operations and attributes (functions and properties) */ fprintf(binding->outfile, "\tif (JS_DefineFunctions(cx, newobject, jsclass_functions) != JS_TRUE) {\n" @@ -395,8 +411,9 @@ output_class_new(struct binding *binding) "\t}\n\n"); } - + /* unroot object and return it */ fprintf(binding->outfile, + "\tJS_RemoveRoot(cx, &newobject);\n" "\n" "\treturn newobject;\n" "}\n"); -- cgit v1.2.3