[mw-devel] MW3 r1099 - trunk/src

welshbyte at sucs.org welshbyte at sucs.org
Tue Nov 10 14:21:34 GMT 2009


Author: welshbyte
Date: 2009-11-10 14:21:34 +0000 (Tue, 10 Nov 2009)
New Revision: 1099

Modified:
   trunk/src/js.c
   trunk/src/sqlite.c
Log:
Add a userdb-backed 'persistent' object. Currently only works for strings.


Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2009-11-06 13:31:57 UTC (rev 1098)
+++ trunk/src/js.c	2009-11-10 14:21:34 UTC (rev 1099)
@@ -55,7 +55,7 @@
 	"milliways", JSCLASS_HAS_PRIVATE,
 	JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
 	JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub,
-    JSCLASS_NO_OPTIONAL_MEMBERS
+	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
 JSClass js_dbresultclass = {
@@ -1041,6 +1041,34 @@
 	return retval;
 }
 
+static JSBool js_persistent_get(JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
+{
+	if (JSVAL_IS_STRING(idval)) {
+		char *key = JS_GetStringBytes(JS_ValueToString(cx, idval));
+		char *val = userdb_get(USERDB_PUBLIC, user->name, key);
+		JSString *str = JS_NewStringCopyZ(cx, val);
+		*vp = STRING_TO_JSVAL(str);
+	}
+	return JS_TRUE;
+}
+
+static JSBool js_persistent_set(JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
+{
+	if (JSVAL_IS_STRING(idval) && JSVAL_IS_STRING(*vp)) {
+		char *key = JS_GetStringBytes(JS_ValueToString(cx, idval));
+		char *val = JS_GetStringBytes(JS_ValueToString(cx, *vp));
+		userdb_set(USERDB_PUBLIC, user->name, key, val);
+	}
+	return JS_TRUE;
+}
+
+static JSClass js_persistentclass = {
+	"persistent", JSCLASS_HAS_PRIVATE,
+	JS_PropertyStub, JS_PropertyStub, js_persistent_get, js_persistent_set,
+	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
+	JSCLASS_NO_OPTIONAL_MEMBERS
+};
+
 /* prints the type of a jsval */
 void show_type(char *name, jsval j)
 {
@@ -1313,6 +1341,7 @@
 		is_local=0;
 	}
 
+	JS_SetCStringsAreUTF8();
 	/* create global runtime, allocate memory */
 	if (!(jsrt = JS_NewRuntime(8*1024*1024))) {
 		printf("Error creating JS runtime\n");
@@ -1368,6 +1397,8 @@
 	// Broadcast constant (for ipc,rpc)
 	JS_DefineProperty(jscx, jsroot, "K_BROADCAST", INT_TO_JSVAL(K_BROADCAST), NULL, NULL, JSPROP_READONLY|JSPROP_PERMANENT);
 	
+	/* A stateful object backed with persistent db storage */
+	JS_DefineObject(jscx, jsroot, "persistent", &js_persistentclass, NULL, JSPROP_READONLY|JSPROP_PERMANENT);
 
 	/* not for bbs user */
 	if (is_local) {

Modified: trunk/src/sqlite.c
===================================================================
--- trunk/src/sqlite.c	2009-11-06 13:31:57 UTC (rev 1098)
+++ trunk/src/sqlite.c	2009-11-10 14:21:34 UTC (rev 1099)
@@ -322,7 +322,7 @@
 		db_free(db_query(path, query, 1));
 		sqlite3_free(query);
 	}else
-	if (old != NULL) {
+	if (old != NULL && old->rows > 0) {
 		char *query = sqlite3_mprintf("UPDATE %q SET arg=%Q WHERE user=%Q AND opt=%Q", table, arg, user, opt);
 		db_free(db_query(path, query, 1));
 		sqlite3_free(query);




More information about the mw-devel mailing list