--- /dev/null
+/* $Id: js_cgi.c,v 1.1 2006/09/08 08:04:58 mmondor Exp $ */
+
+/*
+ * Copyright (c) 2006, Matthew Mondor
+ * ALL RIGHTS RESERVED.
+ */
+
+
+
+#include <stdint.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <jsapi.h>
+
+#include <js_cgi.h>
+
+
+
+
+#define QUEUE_EXCEPTION(s) do { \
+ JS_SetPendingException(cx, \
+ STRING_TO_JSVAL(JS_NewStringCopyZ(cx, (s)))); \
+} while (/* CONSTCOND */0)
+
+
+struct property_spec {
+ const char *name;
+ int value;
+};
+
+
+
+/*
+ * Static prototypes
+ */
+static JSBool cgi_constructor(JSContext *, JSObject *, uintN, jsval *,
+ jsval *);
+
+static JSBool cgi_sm_JSON(JSContext *, JSObject *, uintN, jsval *, jsval *);
+
+
+
+/*
+ * Static globals
+ */
+
+/*
+ * General purpose string buffer (note that this is not thread-safe).
+ * Allows to optimize functions such as PQescapeStringConn().
+ */
+static char *buffer = NULL;
+static size_t buffer_size = 0;
+static Oid *param_types = NULL;
+static char **param_values = NULL;
+static int *param_lengths = NULL;
+static int *param_formats = NULL;
+static int param_entries = 0;
+
+/* PG class */
+static JSClass cgi_class = {
+ "CGI", 0, JS_PropertyStub, JS_PropertyStub,
+ JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub,
+ JS_ConvertStub, JS_FinalizeStub
+};
+
+/* Provided static methods */
+static JSFunctionSpec cgi_smethods[] = {
+ { "JSON", cgi_sm_json, 2, 0, 0 }, /* XXX Check naming */
+ { NULL, NULL, 0, 0, 0 }
+};
+
+/* Provided static properties */
+
+#define SP(n) \
+ { #n, n }
+
+static struct property_spec cgi_sprops[] = {
+ { NULL, 0 }
+};
+
+#undef SP
+
+
+/*
+ * CGI object control
+ */
+
+JSObject *
+js_InitCGIClass(JSContext *cx, JSObject *obj)
+{
+ JSObject *proto, *ctor;
+ struct property_spec *sp;
+
+ if ((proto = JS_InitClass(cx, obj, NULL, &cgi_class, cgi_constructor, 0,
+ NULL, NULL, NULL, cgi_smethods)) == NULL) {
+ (void) fprintf(stderr, "Error initializing CGI class\n");
+ goto err;
+ }
+
+ /* Create static properties */
+ if ((ctor = JS_GetConstructor(cx, proto)) == NULL) {
+ (void) fprintf(stderr, "CGI: JS_GetConstructor == NULL\n");
+ goto err;
+ }
+ for (sp = cgi_sprops; sp->name != NULL; sp++) {
+ if (!JS_DefineProperty(cx, ctor, sp->name,
+ INT_TO_JSVAL(sp->value), NULL, NULL,
+ JSPROP_READONLY | JSPROP_PERMANENT)) {
+ (void) fprintf(stderr,
+ "CGI: Error defining property %s\n", sp->name);
+ goto err;
+ }
+ }
+
+ return proto;
+
+err:
+ return NULL;
+}
+
+/* Non instanciable */
+static JSBool
+cgi_constructor(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+
+ QUEUE_EXCEPTION("CGI class uninstanciable");
+
+ return JS_FALSE;
+}
+
+
+/*
+ * CGI object static methods
+ */
+
+/*
+ * Secure replacement for eval('o = ' + user_defined_data); which is unsafe.
+ * We only support Strings, (using ""), Arrays (using []) objects (using {})
+ * and numbers (using no special marker). This function allows recursiveness
+ * up to a certain level allowed by the function. We return an object with
+ * the unserialized data, or null on error. We don't allow functions.
+ */
+static JSBool
+cgi_sm_json(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
+ jsval *rval)
+{
+
+ if (argc != 2) {
+ QUEUE_EXCEPTION("Wrong number of arguments");
+ return JS_FALSE;
+ }
+ if (!JS_IS_STRING(argv[0])) {
+ QUEUE_EXCEPTION("Argument 1 not a String");
+ return JS_FALSE;
+ }
+ if (!JS_IS_INT(argv[1])) {
+ QUEUE_EXCEPTION("Argument 2 not an Integer");
+ return JS_FALSE;
+ }
+
+ /* XXX */
+ json_level = 0;
+}
+
+static int json_level;
+
+static JSObject *
+json_object(const char *s)
+{
+}
+
+static JSArray *
+json_array(const char *s)
+{
+}
+
+static JSString *
+json_string(const char *)
+{
+}
+
+static jsval
+json_number(const char *)
+{
+}