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().
--- /dev/null
+/* $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;
+ }
+}
--- /dev/null
+/* $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
-/* $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
#include <js_pgsql.h>
#include <js_file.h>
+#include <js_gcroot.h>
{
PGconn *pgc;
FILE *fh;
- JSObject *ro;
*rval = OBJECT_TO_JSVAL(NULL);
* 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;
jsval *rval)
{
PGconn *pgc;
- JSObject *ro;
*rval = OBJECT_TO_JSVAL(NULL);
/*
* 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);