[mw-devel] MW3 r1102 - trunk/src

psycodom at sucs.org psycodom at sucs.org
Fri Nov 13 11:10:59 GMT 2009


Author: psycodom
Date: 2009-11-13 11:10:59 +0000 (Fri, 13 Nov 2009)
New Revision: 1102

Modified:
   trunk/src/js.c
Log:
Major cleanup of the js.c code because the new version of spidermonkey supports JS_SetCStringsAreUTF8().
Drops lots of iconv calls and fixes a couple of bugs I might have made many moons ago.



Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2009-11-12 22:04:24 UTC (rev 1101)
+++ trunk/src/js.c	2009-11-13 11:10:59 UTC (rev 1102)
@@ -86,27 +86,6 @@
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
-// turns a jsval into a utf-8 string that mw can handle
-// expects the utf8_buffer to have been allocated and be at least buffer_length long
-// used to reduce mass code duplication
-int jsval_to_utf8string(JSContext *cx, jsval the_jsval, char *utf8_buffer, size_t buffer_length)
-{
-	JSString *the_jsstring;
-	size_t ucs2_length;
-	jschar *ucs2_string;
-	int retval;
-	
-	the_jsstring = JS_ValueToString(cx, the_jsval);
-	ucs2_length=JS_GetStringLength(the_jsstring);
-	ucs2_string=JS_GetStringChars(the_jsstring);
-	
-	retval=convert_string_charset((char *)ucs2_string, "UCS-2", ucs2_length*sizeof(jschar), utf8_buffer, "UTF-8", buffer_length, NULL, NULL, NULL, NULL, NULL);
-	
-	if(retval<0) {
-		fprintf(stderr, "jsval_to_utf8string: convert_string_charset failed with error: %d\n", retval);
-	}
-	return retval;
-}
 
 void js_timeout(void *ptr)
 {
@@ -136,23 +115,16 @@
 static JSBool js_print(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
 {
 	uintN i;
-	int conversion_result;
-	char msg[MAXTEXTLENGTH*4];
+	char *msg;
 	if (argc < 1) {
 		return JS_TRUE;
 	}
 	
 	for (i = 0; i<argc; i++) {
-		if (JSVAL_IS_STRING(argv[i])) {
-			conversion_result=jsval_to_utf8string(cx, argv[0], msg, MAXTEXTLENGTH*4);
-			if( conversion_result >= 0) {
-				if( conversion_result & WOUTPUTTOOSHORT ) {
-					printf("JavaScript print() command produced too much text.  It was truncated\n");
-				}
-				display_message(msg, 0, 1);
-			} else {
-				return JS_FALSE;
-			}
+		if (JSVAL_IS_STRING(argv[i]) || JSVAL_IS_NUMBER(argv[i])) {
+			msg = JS_GetStringBytes(JS_ValueToString(cx, argv[i]));
+			display_message(msg, 0, 1);
+			
 		} else 
 		if (JSVAL_IS_NULL(argv[i])) {
 			display_message("jsval is NULL",0,1);
@@ -171,21 +143,21 @@
 static JSBool js_mwexec(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
 {
 	char msg[MAXTEXTLENGTH];
-	int conversion_result;
 	if (argc < 1) {
+		JS_ReportError(cx, "exec() expects an argument.");
 		return JS_FALSE;
 	}
 	
 	if (JSVAL_IS_STRING(argv[0])) {
-		conversion_result=jsval_to_utf8string(cx, argv[0], msg, MAXTEXTLENGTH-100);
-		if( conversion_result >= 0) {
-			if( conversion_result & WOUTPUTTOOSHORT ) {
-				printf("JavaScript exec() command produced too much text.  It was truncated\n");
-			}
-			DoCommand(msg, chattable);
-			return JS_TRUE;
-		}
+		
+		// DoCommand seems to like a buffer of MAXTEXTLENGTH that is can alter so best
+		//  to copy the non-editable result from JS_GetStringBytes
+		strncpy(msg, JS_GetStringBytes(JS_ValueToString(cx, argv[0])), MAXTEXTLENGTH-100);
+		msg[MAXTEXTLENGTH-100] = '\0' ;
+		DoCommand(msg, chattable);
+		return JS_TRUE;
 	}
+	JS_ReportError(cx, "exec() expects a string.");
 	return JS_FALSE;
 }
 
@@ -194,10 +166,10 @@
 static JSBool js_say(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
 {
 	char msg[MAXTEXTLENGTH];
-	int conversion_result;
+	//int conversion_result;
 	if (argc < 1) {
-		return JS_FALSE;
 		JS_ReportError(cx, "say() expects an argument.");
+		return JS_FALSE;
 		
 	}
 
@@ -207,17 +179,17 @@
 		return JS_FALSE;
 	}
 	
-	if (JSVAL_IS_STRING(argv[0])) {
-		conversion_result=jsval_to_utf8string(cx, argv[0], msg, MAXTEXTLENGTH-100);
-		if( conversion_result >= 0) {
-			if( conversion_result & WOUTPUTTOOSHORT ) {
-				JS_ReportError(cx, "Warning: say() command produced too much text.  It was truncated");
-			}
-			chat_say(msg);
-			return JS_TRUE;
-		}
+	if (JSVAL_IS_STRING(argv[0]) || JSVAL_IS_NUMBER(argv[0])) {
+	
+		// chat_say seems to like a buffer of MAXTEXTLENGTH that is can alter so best
+		//  to copy the non-editable result from JS_GetStringBytes
+		strncpy(msg, JS_GetStringBytes(JS_ValueToString(cx, argv[0])), MAXTEXTLENGTH-100);
+		msg[MAXTEXTLENGTH-100] = '\0' ;
+		chat_say(msg);
+		return JS_TRUE;
+	
 	}
-	JS_ReportError(cx, "Error: say() expects a string.");
+	JS_ReportError(cx, "Error: say() expects a string or a number.");
 	
 	return JS_FALSE;
 }
@@ -230,7 +202,6 @@
 	char username[NAMESIZE+1]="";
 	int broadcast=0;
 	
-	int conversion_result;
 	if (argc < 3) {
 		JS_ReportError(cx, "Error: javascript rpc() expects 3 arguments");
 		return JS_FALSE;
@@ -242,27 +213,18 @@
 	}
 	
 	if (JSVAL_IS_STRING(argv[0])) {
-		conversion_result=jsval_to_utf8string(cx, argv[0], username, NAMESIZE+1);
-		if( conversion_result < 0) {
-			JS_ReportError(cx, "Error: javascript rpc(): major argument conversion error");
-			return JS_FALSE;
-		}
+		strncpy(username, JS_GetStringBytes(JS_ValueToString(cx, argv[0])), NAMESIZE);
+		username[NAMESIZE] = '\0' ;
 	}
 	
-	conversion_result=jsval_to_utf8string(cx, argv[1], rpc_type, MAXTEXTLENGTH-100);
-	if( conversion_result < 0) {
-		JS_ReportError(cx, "Error: javascript rpc(): major argument conversion error");
-		
-		return JS_FALSE;
-	}
-	conversion_result=jsval_to_utf8string(cx, argv[2], msg, MAXTEXTLENGTH-100);
-	if( conversion_result < 0) {
-		JS_ReportError(cx, "Error: javascript rpc(): major argument conversion error");
-		return JS_FALSE;
-	}
+	strncpy(rpc_type, JS_GetStringBytes(JS_ValueToString(cx, argv[1])), MAXTEXTLENGTH);
+	rpc_type[MAXTEXTLENGTH-1] = '\0' ;
+
+	strncpy(msg, JS_GetStringBytes(JS_ValueToString(cx, argv[2])), MAXTEXTLENGTH);
+	msg[MAXTEXTLENGTH-1] = '\0' ;
 	// something is empty
 	if( (broadcast==0 && username[0]=='\0') || rpc_type[0]=='\0') {
-		JS_ReportError(cx, "Error: javascript rpc(): invalid arguments");
+		JS_ReportError(cx, "Error: javascript rpc(): invalid arguments - [%s] [%s]", broadcast ? "BROADCAST":username, rpc_type);
 		return JS_FALSE;
 	}
 	
@@ -277,7 +239,6 @@
 	char msg[MAXTEXTLENGTH]="";
 	char username[NAMESIZE+1]="";
 	int broadcast=0;
-	int conversion_result;
 	if (argc < 2) {
 		JS_ReportError(cx, "Error: javascript ipc() expects 2 arguments");
 		return JS_FALSE;
@@ -288,19 +249,14 @@
 		}
 	}
 	if (JSVAL_IS_STRING(argv[0])) {
-		conversion_result=jsval_to_utf8string(cx, argv[0], username, NAMESIZE+1);
-		if( conversion_result < 0) {
-		JS_ReportError(cx, "Error: javascript ipc(): major argument conversion error");
-			return JS_FALSE;
-		}
+		strncpy(username, JS_GetStringBytes(JS_ValueToString(cx, argv[0])), NAMESIZE);
+		username[NAMESIZE] = '\0' ;
 	}
 	
 
-	conversion_result=jsval_to_utf8string(cx, argv[1], msg, MAXTEXTLENGTH-100);
-	if( conversion_result < 0) {
-		JS_ReportError(cx, "Error: javascript ipc(): major argument conversion error");
-		return JS_FALSE;
-	}
+	strncpy(msg, JS_GetStringBytes(JS_ValueToString(cx, argv[1])), MAXTEXTLENGTH);
+	msg[MAXTEXTLENGTH-1] = '\0' ;
+
 	// not broadcast and no username
 	if(broadcast==0 && username[0]=='\0') {
 		JS_ReportError(cx, "Error: javascript ipc(): expects a username or K_BROADCAST");
@@ -324,7 +280,7 @@
 	
 	if(argc > 0) {
 		if(JSVAL_IS_STRING(argv[0])) {
-			// convert prompt to local
+			// convert prompt to local - this bit of ugliness is still needed in case people aren't using utf8
 			the_jsstring = JS_ValueToString(cx, argv[0]);
 			ucs2_length=JS_GetStringLength(the_jsstring);
 			ucs2_string=JS_GetStringChars(the_jsstring);
@@ -357,11 +313,12 @@
 	
 	line_length = sizeof(jschar) * (strlen(line) + 1);
 	
+	// likewise as this has come straight from a readline we need a convert from local charset
 	ucs2_string = malloc( line_length );
 	if(ucs2_string!=NULL) {
 		
 		conv_error=convert_string_charset(line, "LOCAL", strlen(line),
-											(char *)ucs2_string, "UCS-2//TRANSLIT", line_length, 
+											(char *)ucs2_string, "UTF-16//TRANSLIT", line_length, 
 											NULL, NULL, NULL, NULL, NULL);
 		if(conv_error>=0) {
 			jsstr = JS_NewUCStringCopyZ(cx, ucs2_string);
@@ -390,10 +347,10 @@
 		beeps=JSVAL_TO_INT(argv[0]);
 		if(beeps < 1 || beeps > 5) {
 			beeps=0;
-			JS_ReportError(cx, "Warning: javascript beep will only do between 1 and 5 beeps.");
+			fprintf(stderr, "Warning: javascript beep will only do between 1 and 5 beeps.\n");
 		}
 	} else {
-		JS_ReportError(cx, "Warning: javascript beep command expects an integer.");
+		fprintf(stderr, "Warning: javascript beep command expects an integer.\n");
 	}
 	for(i=0;i<beeps;i++)
 	{
@@ -406,55 +363,43 @@
 /* bind something to a javascript function */
 static JSBool js_bind(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
 {
-	char bind[MAXTEXTLENGTH]="";
-	char function_name[MAXTEXTLENGTH]="";
-	int conversion_result;
+	char *bind=NULL;
+	char *function_name=NULL;
+//	int conversion_result;
 	int bind_type=-1;
 	int i=1;
 	if (argc < 2) {
-		JS_ReportError(cx, "Error in javascript: bind expects 2 arguments");
-		return JS_TRUE;
+		JS_ReportError(cx, "Error in javascript: bind expects at least 2 arguments");
+		return JS_FALSE;
 	}
 		
 	if (JSVAL_IS_STRING(argv[0])) {
-		conversion_result=jsval_to_utf8string(cx, argv[0], bind, MAXTEXTLENGTH-100);
-		if( conversion_result != 0) {
-			JS_ReportError(cx, "Error in string conversion binding javascript");
-			return JS_TRUE;
-			
-		}
+		bind = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
 		bind_type=K_BIND;
 	} else if (JSVAL_IS_INT(argv[0])) {
 		bind_type=JSVAL_TO_INT(argv[0]);
 		if(bind_type == K_BIND || bind_type == K_BIND_ALIAS || bind_type == K_BIND_RPC) {
 			i++;
-			conversion_result=jsval_to_utf8string(cx, argv[1], bind, MAXTEXTLENGTH-100);
-			if( conversion_result != 0) {
-				JS_ReportError(cx, "Error in string conversion binding javascript");
-				return JS_TRUE;
-			}	
+			bind = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
+
 		}
 	} else {
 		JS_ReportError(cx, "Error in javascript: bind expects first argument to be a string or a recognised bind id");
-		return JS_TRUE;
+		return JS_FALSE;
 	}
 	if (argc>= i-1 && JSVAL_IS_STRING(argv[i])) {
-		conversion_result=jsval_to_utf8string(cx, argv[i], function_name, MAXTEXTLENGTH-100);
-		if( conversion_result != 0) {
-			JS_ReportError(cx, "Error in string conversion binding javascript");
-			return JS_TRUE;
-			
-		}
+		function_name = JS_GetStringBytes(JS_ValueToString(cx, argv[i]));
+
 	} else {
 		JS_ReportError(cx, "Error in javascript: bind expects final argument to be a string.");
-		return JS_TRUE;
+		return JS_FALSE;
 	}
 
 	switch(bind_type) {
 		case K_BIND:
-			if(bind[0]=='\0') {
+			if(bind == NULL || bind[0]=='\0') {
 				JS_ReportError(cx, "Error: Empty bind");
-				return JS_TRUE;
+				return JS_FALSE;
 			}
 			if (AddLink(&bind_list, bind, function_name))
 			{
@@ -463,9 +408,9 @@
 
 			break;
 		case K_BIND_ALIAS:
-			if(bind[0]=='\0') {
+			if(bind == NULL || bind[0]=='\0') {
 				printf("Error: Empty bind\n");
-				return JS_TRUE;
+				return JS_FALSE;
 			}
 			if (AddLink(&alias_list, bind, function_name))
 			{
@@ -474,7 +419,7 @@
 
 			break;
 		case K_BIND_RPC:
-			if(bind[0]=='\0') {
+			if(bind == NULL || bind[0]=='\0') {
 				printf("Error: Empty bind\n");
 				return JS_TRUE;
 			}
@@ -531,47 +476,37 @@
 /* bind something to a javascript function */
 static JSBool js_unbind(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
 {
-	char bind[MAXTEXTLENGTH]="";
-	char function_name[MAXTEXTLENGTH]="";
-	int conversion_result;
+	char *bind=NULL;
+	char *function_name=NULL;
 	int bind_type=-1;
-	int i=1;
-	if (argc < 2) {
-		JS_ReportError(cx, "Error in javascript: unbind expects 2 arguments");
-		return JS_TRUE;
+	
+	if (argc < 1) {
+		JS_ReportError(cx, "Error in javascript: unbind expects at least 1 argument");
+		return JS_FALSE;
 	}
 		
 	if (JSVAL_IS_STRING(argv[0])) {
-		conversion_result=jsval_to_utf8string(cx, argv[0], bind, MAXTEXTLENGTH-100);
-		if( conversion_result != 0) {
-			JS_ReportError(cx, "Error in string conversion binding javascript");
-			return JS_TRUE;
-			
-		}
+		bind = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
 		bind_type=K_BIND;
 	} else if (JSVAL_IS_INT(argv[0])) {
 		bind_type=JSVAL_TO_INT(argv[0]);
-	} else {
-		JS_ReportError(cx, "Error in javascript: bind expects first argument to be a string or a recognised bind id");
-		return JS_TRUE;
-	}
-	if (argc>= i-1 && JSVAL_IS_STRING(argv[i])) {
-		conversion_result=jsval_to_utf8string(cx, argv[i], function_name, MAXTEXTLENGTH-100);
-		if( conversion_result != 0) {
-			JS_ReportError(cx, "Error in string conversion binding javascript");
-			return JS_TRUE;
-			
+		if(JSVAL_IS_STRING(argv[1])) {
+			bind = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
+			function_name = bind;
+		} else {
+			JS_ReportError(cx, "Error in javascript: bind expects final argument to be a string.");
+			return JS_FALSE;
 		}
+
 	} else {
-		JS_ReportError(cx, "Error in javascript: bind expects final argument to be a string.");
-		return JS_TRUE;
+		JS_ReportError(cx, "Error in javascript: unbind expects first argument to be a string or a recognised bind id");
+		return JS_FALSE;
 	}
-
 	switch(bind_type) {
 		case K_BIND:
-			if(bind[0]=='\0') {
+			if(bind == NULL || bind[0]=='\0') {
 				JS_ReportError(cx, "Error: Empty bind");
-				return JS_TRUE;
+				return JS_FALSE;
 			}
 			if(!DestroyLink(&bind_list, bind))
 			{
@@ -580,7 +515,7 @@
 
 			break;
 		case K_BIND_ALIAS:
-			if(bind[0]=='\0') {
+			if(bind == NULL || bind[0]=='\0') {
 				printf("Error: Empty bind\n");
 				return JS_TRUE;
 			}
@@ -591,7 +526,7 @@
 
 			break;
 		case K_BIND_RPC:
-			if(bind[0]=='\0') {
+			if(bind == NULL || bind[0]=='\0') {
 				printf("Error: Empty bind\n");
 				return JS_TRUE;
 			}
@@ -761,9 +696,7 @@
  */
 static JSBool js_urlget(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
 {
-	uintN i;
-	int conversion_result;
-	char msg[MAXTEXTLENGTH*4];
+	char *url;
 	JSString *jsstr;
 
 	if (argc < 1) {
@@ -771,46 +704,39 @@
 		return JS_FALSE;
 	}
 	
-	for (i = 0; i<argc; i++) {
-		if (JSVAL_IS_STRING(argv[i])) {
-			conversion_result=jsval_to_utf8string(cx, argv[0], msg, MAXTEXTLENGTH*4);
-			if( conversion_result >= 0) {
-				CURL *cl;
-				char cerr[CURL_ERROR_SIZE];
-				struct block_t *answer = block_new(1024);
+	if (JSVAL_IS_STRING(argv[0])) {
+		CURL *cl;
+		char cerr[CURL_ERROR_SIZE];
+		struct block_t *answer = block_new(1024);
 
-				if( conversion_result & WOUTPUTTOOSHORT ) {
-					printf("JavaScript geturl() command: URL too long. It was truncated\n");
-				}
-				cl = curl_easy_init();
-				curl_easy_setopt(cl, CURLOPT_WRITEFUNCTION, urldata);
-				curl_easy_setopt(cl, CURLOPT_WRITEDATA, answer);
-				curl_easy_setopt(cl, CURLOPT_URL, msg);
-				curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
+		url = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
+		cl = curl_easy_init();
+		curl_easy_setopt(cl, CURLOPT_WRITEFUNCTION, urldata);
+		curl_easy_setopt(cl, CURLOPT_WRITEDATA, answer);
+		curl_easy_setopt(cl, CURLOPT_URL, url);
+		curl_easy_setopt(cl, CURLOPT_ERRORBUFFER, cerr);
 #ifdef RELEASE
-                if (atoi(VER_TWK) > 0)
-                        curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN "." VER_TWK);
-                else
-                        curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN);
+		if (atoi(VER_TWK) > 0)
+				curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN "." VER_TWK);
+		else
+				curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN);
 #else
-                curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN "." VER_TWK " (Dev)");
+		curl_easy_setopt(cl, CURLOPT_USERAGENT, "Milliways III v" VER_MAJ "." VER_MIN "." VER_TWK " (Dev)");
 #endif
-                if (curl_easy_perform(cl))
-                        fprintf(stderr, "JavaScript urlget failed %s: %s\n", msg, cerr);
-                curl_easy_cleanup(cl);
+		if (curl_easy_perform(cl))
+				fprintf(stderr, "JavaScript urlget failed %s: %s\n", url, cerr);
+		curl_easy_cleanup(cl);
 
 		jsstr = JS_NewStringCopyZ(cx, answer->p_buffer);
 		*rval = STRING_TO_JSVAL(jsstr);
 
-				free(answer);
-				return JS_TRUE;
+		free(answer);
+		return JS_TRUE;
 
 
-			} else {
-				return JS_FALSE;
-			}
-		} 
-	}
+	} 
+	JS_ReportError(cx, "urlget requires a string.");
+	
 	return JS_FALSE;
 }
 
@@ -819,9 +745,9 @@
 	JSObject *jsdata;
 	JSString *jsstr;
 	jsval jv;
-	jschar *ucsstr;
-	int i, conv_error;
-	size_t data_length;
+	int i; 
+//	int conv_error;
+//	size_t data_length;
 	
 	if (data == NULL || ncols < 1) return NULL;
 
@@ -831,25 +757,8 @@
 	for (i = 0; i < ncols; i++) {
 //		printf("dbdata_to_jsarray: data @ %p -", (void *)data->field[i]);
 //		printf("%s", data->field[i]);
-		data_length = sizeof(jschar) * (strlen(data->field[i]) + 1);
-		
-		ucsstr = malloc( data_length );
-		if(ucsstr!=NULL) {
-			
-			conv_error=convert_string_charset(data->field[i], "UTF-8", strlen(data->field[i]),
-												(char *)ucsstr, "UCS-2", data_length, 
-												NULL, NULL, NULL, NULL, NULL);
-			if(conv_error>=0) {
-				jsstr = JS_NewUCStringCopyZ(cx, ucsstr);
-			} else {
-				jsstr = JS_NewStringCopyZ(cx, "(garbled string)");
-			}	
-			free(ucsstr);
-		} else {
-			jsstr = JS_NewStringCopyZ(cx, "(garbled string)");
-		}
-		//JS_AddRoot(cx, jsstr);
 //		printf(" -> JSString @ %p\n", (void *)jsstr);
+		jsstr = JS_NewStringCopyZ(cx, data->field[i]);
 		jv = STRING_TO_JSVAL(jsstr);
 		JS_SetElement(cx, jsdata, i, &jv);
 	}
@@ -871,7 +780,6 @@
 
 	jsarray = JS_NewArrayObject(cx, 0, NULL);
 	JS_AddRoot(cx, jsarray);
-//	JS_SetArrayLength(cx, jsarray, data->cols);
 
 /*	printf("Making Array(%d)\n", data->cols); */
 	i = 0;
@@ -893,12 +801,8 @@
 // create a javascript array of column names from a db_result
 JSObject *dbresult_to_jscolnames(JSContext *cx, struct db_result *data) {
 	JSObject *jsarray;
-//	JSObject *jsnode;
 	jsval jv;
-//	struct db_data *node;
-	jschar *ucsstr;
-	int i, conv_error;
-	size_t data_length;
+	int i; 
 	JSString *jsstr;
 
 	if (data == NULL) return NULL;
@@ -907,23 +811,7 @@
 	JS_AddRoot(cx, jsarray);
 	
 	for(i=0;i<data->cols;i++) {
-		data_length = sizeof(jschar) * (strlen(data->colNames[i]) + 1);
-		
-		ucsstr = malloc( data_length );
-		if(ucsstr!=NULL) {
-			
-			conv_error=convert_string_charset(data->colNames[i], "UTF-8", strlen(data->colNames[i]),
-												(char *)ucsstr, "UCS-2", data_length, 
-												NULL, NULL, NULL, NULL, NULL);
-			if(conv_error>=0) {
-				jsstr = JS_NewUCStringCopyZ(cx, ucsstr);
-			} else {
-				jsstr = JS_NewStringCopyZ(cx, "(garbled string)");
-			}	
-			free(ucsstr);
-		} else {
-			jsstr = JS_NewStringCopyZ(cx, "(garbled string)");
-		}
+		jsstr = JS_NewStringCopyZ(cx, data->colNames[i]);
 		jv = STRING_TO_JSVAL(jsstr);
 		JS_SetElement(cx, jsarray, i, &jv);
 		
@@ -978,11 +866,8 @@
 static JSBool js_doquery(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) {
 	struct js_db_result *dbres;
 	char *dbname;
-	char query[MAXTEXTLENGTH*2]; // for now queries will be at most 4096 bytes or will fail.
-								 // if this becomes a problem dynamic allocation of the utf8 buffer might be needed although getting the right size is a pita
-	//JSObject *result; // result object were creating
+	char *query;
 	jsval resobject_jsval;
-	int conversion_result;
 	char path[1024];
 	struct passwd *pw;
 	JSBool retval;
@@ -1008,12 +893,7 @@
 	}
 
 	dbname = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
-	conversion_result=jsval_to_utf8string(cx, argv[1], query, MAXTEXTLENGTH*2);
-	//query = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
-	if(conversion_result != 0) {
-		JS_ReportError(cx, "String conversion error");
-		return JS_FALSE;
-	}
+	query = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
 	
 	if (dbname[0] == '/'
 	||  strncmp(dbname, "../", 3)==0
@@ -1099,16 +979,10 @@
 /* Execute some javascript commands */
 int js_exec(char * name, int argc, char **argvc) {
 	int i;
-//	jschar * js_string;
 	jsval rval;
 	jsval *argv;
 	JSBool ret;
-	//jschar *ucarg;
-	jschar unicode_arg[MAXTEXTLENGTH];
-	//size_t uclen;
-	int utferror;
-	
-//	js_string = NULL;
+
 	argv=calloc(argc,sizeof(jsval));
 
 	for (i=0;i<argc;i++) {
@@ -1116,25 +990,7 @@
 			fprintf(stderr, "js_exec: argc %d was NULL!\n",i);
 			continue;
 		}
-		// convert the arguments from utf8 to ucs2 for the javascript engine.
-		utferror=convert_string_charset(argvc[i], "UTF-8", strlen(argvc[i]), (char *)unicode_arg, "UCS-2", sizeof(jschar) * MAXTEXTLENGTH, NULL, NULL, NULL, NULL, "?");
-		if(utferror>=0) {
-			if(utferror & WOUTPUTTOOSHORT) {
-				printf("Argument %d for javascript function '%s' was too long and has been truncated.\n", i, name);
-			}
-			if(utferror & WINVALIDCHARS) {
-				printf("Argument %d for javascript function '%s' contained invalid characters which have been replaced by '?'\n", i, name);
-			}
-			if(utferror & WICONVFAIL) {
-				printf("An iconv error occured processing argument %d for javascript function '%s' and the argument may have been truncated.\n", i, name);
-			}
-			
-			argv[i] = STRING_TO_JSVAL(JS_NewUCStringCopyZ(jscx, unicode_arg));
-		} else {
-			printf("A major error %d occured converting argument %d for javascript function '%s' to unicode.\n", utferror, i, name);
-			argv[i] = STRING_TO_JSVAL(JS_NewStringCopyZ(jscx, "(error argument)"));
-		}
-			
+		argv[i] = STRING_TO_JSVAL(JS_NewStringCopyZ(jscx, argvc[i]));	
 	}
 	js_interrupted=0;
 	js_start_timeout();
@@ -1159,7 +1015,8 @@
 	char *pointer=NULL;
 	char *line=NULL;
 	int len;
-
+	
+	
 	if (er->linebuf != NULL) {
 		len = er->tokenptr - er->linebuf + 1;
 		if(len<2) len=2;
@@ -1198,14 +1055,11 @@
 int load_jsfile(FILE *f, char *filename)
 {
 	char *body;
-	jschar *unicode_body;
 	int where, len;
-	size_t length;
 	JSBool success;
 	JSScript *script = NULL;
 	jsval retval;
 	uintN lineno=0;
-	int utferror;
 
 	where = ftell(f);
 	fseek(f, 0, SEEK_END);
@@ -1224,34 +1078,10 @@
 	body[len]=0;
 	/*convert the script into jsstring, scripts assumed to be utf8*/
 	
-	unicode_body=malloc(sizeof(jschar) * (len+1));
-	
-	/*unicode_body=utf8_to_jsstring(body, &length, &utferror);*/
-	
-	if(unicode_body==NULL) {
-		fprintf(stderr, "load_jsfile: failed to allocate memory for javascript file\n");
-		return 1;
-	}
-	utferror = convert_string_charset( body, "UTF-8", len, (char *)unicode_body, "UCS-2", sizeof(jschar) * (len+1), &length, NULL, NULL, NULL, NULL);
-	if(utferror<0)
-	{
-		fprintf(stderr, "load_jsfile: failed to convert script to jschar string. Error %d\n", utferror);
-		return 1;
-	}
-	
-	if(utferror & WINVALIDCHARS) {
-		printf("The script '%s' does not appear to be utf-8.  Some characters may have been discared.  Please ensure this file is saved as UTF-8\n", filename);
-	}
-	
-	// length is currently the number of bytes iconv used (including the null char it created)
-	// thus the number of actual chars in the string required is:
-	length=(length/sizeof(jschar))-1;
 	/* Compile the js file specified */
-	/* script = JS_CompileScript(jscx, jsroot, body, len, filename, lineno); */
-	script = JS_CompileUCScript(jscx, jsroot, unicode_body, length, filename, lineno);
+	 script = JS_CompileScript(jscx, jsroot, body, len, filename, lineno); 
 
 	free(body);
-	free(unicode_body);
 	
 	if (script == NULL) {
 		printf("Failed to compile js script: %s\n", filename);




More information about the mw-devel mailing list