summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVincent Sanders <vince@kyllikki.org>2012-11-16 22:05:38 +0000
committerVincent Sanders <vince@kyllikki.org>2012-11-16 22:05:38 +0000
commit24d68eb4e19e48c80a44af324499e1e16863e768 (patch)
tree573ee590e57eb23a206d36da8d01715de4f67a44
parentbb6e9d7f5f71677aa4fc79fed01919ea0bbb20c4 (diff)
downloadnsgenbind-24d68eb4e19e48c80a44af324499e1e16863e768.tar.gz
nsgenbind-24d68eb4e19e48c80a44af324499e1e16863e768.tar.bz2
ensure newly created objects survive teh garbage collector long enough to be constructed.
-rw-r--r--src/jsapi-libdom.c21
1 files 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");