Created js_InitGCRootClass() and js_DestroyGCRootClass() which should be
authorMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 5 Oct 2006 17:43:06 +0000 (17:43 +0000)
committerMatthew Mondor <mmondor@pulsar-zone.net>
Thu, 5 Oct 2006 17:43:06 +0000 (17:43 +0000)
called to setup the global JSObject *gcroot for use by the various classes.
js_pgsql is currenty using it.  It is important to js_DestroyGCRootClass()
before JS_DestroyContext().

mmsoftware/js/classes/js_gcroot.c [new file with mode: 0644]
mmsoftware/js/classes/js_gcroot.h [new file with mode: 0644]
mmsoftware/js/classes/js_pgsql.c

diff --git a/mmsoftware/js/classes/js_gcroot.c b/mmsoftware/js/classes/js_gcroot.c
new file mode 100644 (file)
index 0000000..8d9ee71
--- /dev/null
@@ -0,0 +1,77 @@
+/* $Id: js_gcroot.c,v 1.1 2006/10/05 17:43:06 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2006, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+
+
+#include <sys/types.h>
+
+#include <stdint.h>
+
+#include <assert.h>
+#include <dirent.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <jsapi.h>
+
+#include <js_gcroot.h>
+
+
+
+/*
+ * Static globals
+ */
+
+/* Dir class */
+static JSClass gcroot_class = {
+       "GCRoot", 0, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+       JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub,
+       JS_FinalizeStub
+};
+
+/*
+ * Exported globals
+ */
+JSObject       *gcroot = NULL;
+
+
+
+/*
+ * GCRoot object control
+ */
+
+JSBool
+js_InitGCRootClass(JSContext *cx)
+{
+
+       /* Instantiate new object */
+       if ((gcroot = JS_NewObject(cx, &gcroot_class, NULL, NULL)) == NULL) {
+               (void) fprintf(stderr, "GCRoot: JS_NewObject(gcroot)\n");
+               return JS_FALSE;
+       }
+       if (!JS_AddRoot(cx, &gcroot)) {
+               (void) fprintf(stderr, "GCRoot: JS_AddRoot(gcroot)\n");
+               gcroot = NULL;
+               return JS_FALSE;
+       }
+
+       return JS_TRUE;
+}
+
+void
+js_DestroyGCRootClass(JSContext *cx)
+{
+
+       if (gcroot != NULL) {
+               if (!JS_RemoveRoot(cx, &gcroot))
+                       (void) fprintf(stderr,
+                           "GCRoot: JS_RemoveRoot(gcroot)\n");
+               gcroot = NULL;
+       }
+}
diff --git a/mmsoftware/js/classes/js_gcroot.h b/mmsoftware/js/classes/js_gcroot.h
new file mode 100644 (file)
index 0000000..03e4e5a
--- /dev/null
@@ -0,0 +1,19 @@
+/* $Id: js_gcroot.h,v 1.1 2006/10/05 17:43:06 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2006, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+#ifndef JSGCROOT_H
+#define JSGCROOT_H
+
+#include <js_gcroot.h>
+
+extern JSBool  js_InitGCRootClass(JSContext *);
+extern void    js_DestroyGCRootClass(JSContext *);
+
+/* This rooted object can be added properties to by any class as needed */
+extern JSObject *gcroot;
+
+#endif
index 3db023a..b72cc70 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: js_pgsql.c,v 1.7 2006/10/04 03:22:05 mmondor Exp $ */
+/* $Id: js_pgsql.c,v 1.8 2006/10/05 17:43:06 mmondor Exp $ */
 
 /*
  * Copyright (c) 2006, Matthew Mondor
@@ -42,6 +42,7 @@
 
 #include <js_pgsql.h>
 #include <js_file.h>
+#include <js_gcroot.h>
 
 
 
@@ -2608,7 +2609,6 @@ pgconn_m_PQtrace(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
 {
        PGconn          *pgc;
        FILE            *fh;
-       JSObject        *ro;
 
        *rval = OBJECT_TO_JSVAL(NULL);
 
@@ -2628,9 +2628,8 @@ pgconn_m_PQtrace(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
         * Unroot previously rooted File object, if any, then root newly
         * provided File object.
         */
-       ro = JSVAL_TO_OBJECT(argv[-1]);
-       (void) JS_DeleteProperty(cx, ro, "trace_file_root");
-       if (!JS_DefineProperty(cx, ro, "trace_file_root", argv[0],
+       (void) JS_DeleteProperty(cx, gcroot, "trace_file_root");
+       if (!JS_DefineProperty(cx, gcroot, "trace_file_root", argv[0],
            NULL, NULL, 0)) {
                QUEUE_EXCEPTION("Internal error!");
                return JS_FALSE;
@@ -2649,7 +2648,6 @@ pgconn_m_PQuntrace(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
     jsval *rval)
 {
        PGconn          *pgc;
-       JSObject        *ro;
 
        *rval = OBJECT_TO_JSVAL(NULL);
 
@@ -2664,8 +2662,7 @@ pgconn_m_PQuntrace(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
        /*
         * Unroot previously rooted File object
         */
-       ro = JSVAL_TO_OBJECT(argv[-1]);
-       (void) JS_DeleteProperty(cx, ro, "trace_file_root");
+       (void) JS_DeleteProperty(cx, gcroot, "trace_file_root");
 
        PQuntrace(pgc);