[mw-devel] MARVIN r94 - branches/smonkey

welshbyte at sucs.org welshbyte at sucs.org
Thu Dec 21 09:53:11 GMT 2006


Author: welshbyte
Date: 2006-12-21 09:53:10 +0000 (Thu, 21 Dec 2006)
New Revision: 94

Modified:
   branches/smonkey/Makefile
   branches/smonkey/js.c
   branches/smonkey/server.js
Log:
Added functions for dealing with database queries in js. Still need to hunt down an
elusive segfault.


Modified: branches/smonkey/Makefile
===================================================================
--- branches/smonkey/Makefile	2006-12-21 02:13:06 UTC (rev 93)
+++ branches/smonkey/Makefile	2006-12-21 09:53:10 UTC (rev 94)
@@ -19,7 +19,7 @@
 #	-Wextra		- Enable yet more compiler warnings :)
 #	-fPIC		- Enable position independent code (for PowerPC?)
 # We also include the DEFS variable we defined above
-CFLAGS		= -std=gnu99 -g -O2 -Wall -pedantic -Wextra -ggdb -fPIC $(DEFS)
+CFLAGS		= -std=gnu99 -g -O0 -Wall -pedantic -Wextra -ggdb -fPIC $(DEFS)
 LDFLAGS 	= -g
 
 # The names of all object files that make up the final binaries.  Just take the

Modified: branches/smonkey/js.c
===================================================================
--- branches/smonkey/js.c	2006-12-21 02:13:06 UTC (rev 93)
+++ branches/smonkey/js.c	2006-12-21 09:53:10 UTC (rev 94)
@@ -4,6 +4,7 @@
 
 #include "xml.h"
 #include "js.h"
+#include "sqlite.h"
 
 #ifdef DEBUG_JS
 #  include <readline/readline.h>
@@ -15,9 +16,18 @@
 JSClass globclass = {
 	"milliways",0,
 	JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
-	JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
+	JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub,
+    JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
+JSClass js_dbresultclass = {
+    "dbresult",
+    0,
+    JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
+    JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
 char *
 utf16tolocal(char * utf16, size_t len) {
 	char * local;
@@ -51,12 +61,12 @@
 
 	while (utf16bytesleft > 0) {
 		printf("Before: localbytesleft: %d utf16bytesleft: %d\n",
-			localbytesleft, utf16bytesleft);
+			(int)localbytesleft, (int)utf16bytesleft);
 		nconv = iconv(conv, 
 			&utf16cpy, &utf16bytesleft, 
 			&localcpy, &localbytesleft);
 		printf("After: localbytesleft: %d utf16bytesleft: %d\n", 
-			localbytesleft, utf16bytesleft);
+			(int)localbytesleft, (int)utf16bytesleft);
 		if (nconv == (size_t)-1) {
 			fprintf(stderr, "utf16tolocal barfed (%d) ", errno);
 			/* iconv barfed, but why? */
@@ -103,11 +113,121 @@
 			/* TODO: Might need to convert ucmsg to the local charset here */
 			msg = utf16tolocal((char *)ucmsg, len);
 			printf("%s", msg);
+		} else if (JSVAL_IS_OBJECT(argv[i])) {
+			printf("jsval at %p\n", (void *)argv[i]);
+			JSObject *obj;
+			JS_ValueToObject(cx, argv[i], &obj);
+			printf("Object at %p\n", (void *)obj);
+			if (JS_IsArrayObject(cx, JSVAL_TO_OBJECT(argv[i]))) {
+				printf("(Array)\n");
+			}
 		}
 	}
 	return JS_TRUE;
 }
 
+// Create a javascript array of strings from a struct db_data
+JSObject *
+dbdata_to_jsarray(JSContext *cx, struct db_data *data, int ncols) {
+	JSObject *jsdata;
+	JSString *jsstr;
+	int i;
+
+	if (data == NULL || ncols < 1) return NULL;
+
+	jsdata = JS_NewArrayObject(cx, 0, NULL);
+	JS_AddRoot(cx, jsdata);
+
+	for (i = 0; i < ncols; i++) {
+		printf("dbdata_to_jsarray: data @ %p", (void *)data->field[i]);
+		jsstr = JS_NewStringCopyZ(cx, data->field[i]);
+		JS_AddRoot(cx, jsstr);
+		printf(" -> JSString @ %p\n", (void *)jsstr);
+		JS_SetElement(cx, jsdata, i, (jsval *)STRING_TO_JSVAL(jsstr));
+	}
+
+	return jsdata;
+}
+
+// Create a javascript array of arrays (see dbdata_to_jsarray()) from
+// a struct db_result
+JSObject *
+dbresult_to_jsarray(JSContext *cx, struct db_result *data) {
+	JSObject *jsarray;
+	JSObject *jsnode;
+	struct db_data *node;
+	int i;
+
+	if (data == NULL) return NULL;
+
+	jsarray = JS_NewArrayObject(cx, 0, NULL);
+	JS_AddRoot(cx, jsarray);
+	JS_SetArrayLength(cx, jsarray, data->cols);
+
+	i = 0;
+	node = data->data;
+	while (node) {
+		printf("dbresult_to_jsarray: node @ %p\n", (void *)node);
+		jsnode = dbdata_to_jsarray(cx, node, data->cols);
+		JS_SetElement(cx, jsarray, i, (jsval *)OBJECT_TO_JSVAL(jsnode));
+
+		node = node->next;
+		i++;
+	}
+
+	return jsarray;
+}
+
+// Provides a javascript function to query an sqlite3 database
+static JSBool
+js_doquery(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
+	struct db_result *dbres;
+	char *dbname;
+	char *query;
+	JSObject *result;
+	JSObject *resarray;
+	JSBool ok;
+
+	if (argc != 2) {
+		return JS_FALSE;
+	}
+
+	if (!(JSVAL_IS_STRING(argv[0]) && JSVAL_IS_STRING(argv[1]))) {
+		return JS_FALSE;
+	}
+
+	dbname = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
+	query = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
+
+	dbres = db_query(dbname, query);
+	if (!dbres) {
+		return JS_FALSE;
+	}
+
+	if (0 == dbres->rows) {
+		// No results found (not an error)
+		return JS_TRUE;
+	}
+
+	resarray = dbresult_to_jsarray(cx, dbres);
+	if (!resarray) {
+		return JS_FALSE;
+	}
+	result = JS_NewObject(cx, &js_dbresultclass, NULL, NULL);
+	JS_AddRoot(cx, result);
+
+	ok = JS_SetProperty(cx, result, "rows", (jsval*)OBJECT_TO_JSVAL(resarray));
+	if (!ok) {
+		printf("Setting rows property failed :(\n");
+		return JS_FALSE;
+	}
+
+	*rval = OBJECT_TO_JSVAL(result);
+	printf("Return value %p @ %p\n", (void *)result, (void *)rval);
+
+	return JS_TRUE;
+}
+
 /* Convert a string from local charset to a string of jschar which
    JS_NewUCString() needs to create a new JSString with unicode 
    characters in. An appropriate jschar* is created by casting 
@@ -320,6 +440,7 @@
 	/* Init js standard classes, again global */
 	builtins = JS_InitStandardClasses(jscx, jsglob);
 
+	ok = JS_DefineFunction(jscx, jsglob, "doquery", js_doquery, 2, 0);
 	ok = JS_DefineFunction(jscx, jsglob, "print", js_print, 1, 0);
 
 	return 1;

Modified: branches/smonkey/server.js
===================================================================
--- branches/smonkey/server.js	2006-12-21 02:13:06 UTC (rev 93)
+++ branches/smonkey/server.js	2006-12-21 09:53:10 UTC (rev 94)
@@ -13,3 +13,8 @@
 function session_gone(sess) {
 	message = "session_gone: " + sess + "\n";
 }
+
+print("Calling doquery()...\n");
+foo = doquery("users.db", "Select * FROM user WHERE id != 3 LIMIT 3;");
+bar = foo.rows;
+print(bar);





More information about the mw-devel mailing list