[mw-devel] MARVIN r95 - branches/smonkey

arthur at sucs.org arthur at sucs.org
Wed Mar 7 20:14:17 GMT 2007


Author: arthur
Date: 2007-03-07 20:14:16 +0000 (Wed, 07 Mar 2007)
New Revision: 95

Modified:
   branches/smonkey/js.c
   branches/smonkey/server.js
   branches/smonkey/server_main.c
   branches/smonkey/sqlite.c
Log:
some of the jsvals were being handled wrongly, fixed, and changed the way a db result is returned.


Modified: branches/smonkey/js.c
===================================================================
--- branches/smonkey/js.c	2006-12-21 09:53:10 UTC (rev 94)
+++ branches/smonkey/js.c	2007-03-07 20:14:16 UTC (rev 95)
@@ -14,15 +14,14 @@
 JSContext *jscx;
 JSObject *jsglob;
 JSClass globclass = {
-	"milliways",0,
+	"milliways", 0,
 	JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
 	JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub,
     JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
 JSClass js_dbresultclass = {
-    "dbresult",
-    0,
+    "dbresult", JSCLASS_HAS_PRIVATE,
     JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
     JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
     JSCLASS_NO_OPTIONAL_MEMBERS
@@ -60,13 +59,13 @@
 	utf16cpy = utf16;
 
 	while (utf16bytesleft > 0) {
-		printf("Before: localbytesleft: %d utf16bytesleft: %d\n",
-			(int)localbytesleft, (int)utf16bytesleft);
+/*		printf("Before: localbytesleft: %d utf16bytesleft: %d\n",
+			(int)localbytesleft, (int)utf16bytesleft); */
 		nconv = iconv(conv, 
 			&utf16cpy, &utf16bytesleft, 
 			&localcpy, &localbytesleft);
-		printf("After: localbytesleft: %d utf16bytesleft: %d\n", 
-			(int)localbytesleft, (int)utf16bytesleft);
+/*		printf("After: localbytesleft: %d utf16bytesleft: %d\n", 
+			(int)localbytesleft, (int)utf16bytesleft); */
 		if (nconv == (size_t)-1) {
 			fprintf(stderr, "utf16tolocal barfed (%d) ", errno);
 			/* iconv barfed, but why? */
@@ -113,11 +112,15 @@
 			/* 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])) {
+		} else 
+		if (JSVAL_IS_NULL(argv[i])) {
+			printf("jsval is NULL\n");
+		} 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);
+	//		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");
 			}
@@ -131,6 +134,7 @@
 dbdata_to_jsarray(JSContext *cx, struct db_data *data, int ncols) {
 	JSObject *jsdata;
 	JSString *jsstr;
+	jsval jv;
 	int i;
 
 	if (data == NULL || ncols < 1) return NULL;
@@ -143,8 +147,10 @@
 		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));
+		jv = STRING_TO_JSVAL(jsstr);
+		JS_SetElement(cx, jsdata, i, &jv);
 	}
+	JS_RemoveRoot(cx, jsdata);
 
 	return jsdata;
 }
@@ -155,6 +161,7 @@
 dbresult_to_jsarray(JSContext *cx, struct db_result *data) {
 	JSObject *jsarray;
 	JSObject *jsnode;
+	jsval jv;
 	struct db_data *node;
 	int i;
 
@@ -164,16 +171,19 @@
 	JS_AddRoot(cx, jsarray);
 	JS_SetArrayLength(cx, jsarray, data->cols);
 
+	printf("Making Array(%d)\n", 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));
+		jv = OBJECT_TO_JSVAL(jsnode);
+		JS_SetElement(cx, jsarray, i, &jv);
 
 		node = node->next;
 		i++;
 	}
+	JS_RemoveRoot(cx, jsarray);
 
 	return jsarray;
 }
@@ -184,7 +194,8 @@
 	struct db_result *dbres;
 	char *dbname;
 	char *query;
-	JSObject *result;
+	struct JSPropertySpec myspec[3];
+	JSObject *result; // result object were creating
 	JSObject *resarray;
 	JSBool ok;
 
@@ -216,15 +227,12 @@
 	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;
-	}
+	JS_DefineProperty(cx, result, "numrows", INT_TO_JSVAL(dbres->rows), NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT);
+	JS_DefineProperty(cx, result, "data", OBJECT_TO_JSVAL(resarray), NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_ENUMERATE);
 
+	JS_SetPrivate(cx, result, dbres);
+
 	*rval = OBJECT_TO_JSVAL(result);
-	printf("Return value %p @ %p\n", (void *)result, (void *)rval);
-
 	return JS_TRUE;
 }
 

Modified: branches/smonkey/server.js
===================================================================
--- branches/smonkey/server.js	2006-12-21 09:53:10 UTC (rev 94)
+++ branches/smonkey/server.js	2007-03-07 20:14:16 UTC (rev 95)
@@ -16,5 +16,12 @@
 
 print("Calling doquery()...\n");
 foo = doquery("users.db", "Select * FROM user WHERE id != 3 LIMIT 3;");
-bar = foo.rows;
-print(bar);
+print("Result has "+foo.numrows+" rows\n");
+print("Which looks like an array of "+foo.data.length+" elements\n");
+for (var row in foo.data) {
+	print("row "+row+" has "+foo.data[row].length+" entries: \n");
+	for (var col in foo.data[row]) {
+		print(" - '"+col+"'='"+foo.data[row][col]+"'\n");
+	}
+	print("\n");
+}

Modified: branches/smonkey/server_main.c
===================================================================
--- branches/smonkey/server_main.c	2006-12-21 09:53:10 UTC (rev 94)
+++ branches/smonkey/server_main.c	2007-03-07 20:14:16 UTC (rev 95)
@@ -14,7 +14,7 @@
 #include "xml.h"
 #include "js.h"
 
-int debug=1;
+int debug=0;
 
 int marvin_server = 1;
 

Modified: branches/smonkey/sqlite.c
===================================================================
--- branches/smonkey/sqlite.c	2006-12-21 09:53:10 UTC (rev 94)
+++ branches/smonkey/sqlite.c	2007-03-07 20:14:16 UTC (rev 95)
@@ -70,7 +70,7 @@
 		return NULL;
 	}
 
-	new = malloc(sizeof(struct db_result));
+	new = malloc(sizeof(*new));
 	new->rows = -1;
 	new->cols = -1;
 	new->colNames = NULL;
@@ -81,9 +81,11 @@
 	if (ret != SQLITE_OK) {
 		printf("Error %s (%d) on query %s\n", error, ret, query);
 		sqlite3_free(error);
+		db_close(db);
 		return NULL;
 	}
 
+	db_close(db);
 	return new;
 }
 





More information about the mw-devel mailing list