[mw-devel] MARVIN r84 - branches/smonkey

welshbyte at sucs.org welshbyte at sucs.org
Wed Aug 30 22:55:28 BST 2006


Author: welshbyte
Date: 2006-08-30 22:55:27 +0100 (Wed, 30 Aug 2006)
New Revision: 84

Modified:
   branches/smonkey/js.c
   branches/smonkey/server.c
   branches/smonkey/server.js
Log:
A bit more work on js.c. Plugged a leak, added js error reporting etc.


Modified: branches/smonkey/js.c
===================================================================
--- branches/smonkey/js.c	2006-07-27 01:04:16 UTC (rev 83)
+++ branches/smonkey/js.c	2006-08-30 21:55:27 UTC (rev 84)
@@ -1,7 +1,6 @@
 #include <stdarg.h>
 #include <string.h>
 #include <errno.h>
-#include <wchar.h>
 
 #include "xml.h"
 #include "js.h"
@@ -19,27 +18,90 @@
 	JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
 };
 
+char *
+utf16tolocal(char * utf16, size_t len) {
+	char * local;
+	char * utf16cpy;
+	char * charset;
+	iconv_t conv;
+	int nconv;
+	size_t localbytesleft;
+	size_t utf16bytesleft;
+	char * localcpy;
+
+	/* TODO: charset should be replaced with the charset of the locale */
+	charset = "UTF-8";
+	conv = iconv_open(charset, "UTF16");
+	if (conv == (iconv_t)-1) {
+		fprintf(stderr, "utf16tolocal bombed.\n");
+		return NULL;
+	}
+	
+	localbytesleft = (len) * sizeof(char) * 2; /* Urgh, x2 is kludge.. but how else? */
+	utf16bytesleft = (len) * sizeof(jschar);
+	local = (char *)malloc(localbytesleft);
+
+	if (local == NULL) {
+		fprintf(stderr, "Could not allocate memory for iconv\n");
+		return NULL;
+	}
+	
+	localcpy = local;
+	utf16cpy = utf16;
+
+	while (utf16bytesleft > 0) {
+		printf("Before: localbytesleft: %d utf16bytesleft: %d\n",
+			localbytesleft, utf16bytesleft);
+		nconv = iconv(conv, 
+			&utf16cpy, &utf16bytesleft, 
+			&localcpy, &localbytesleft);
+		printf("After: localbytesleft: %d utf16bytesleft: %d\n", 
+			localbytesleft, utf16bytesleft);
+		if (nconv == (size_t)-1) {
+			fprintf(stderr, "utf16tolocal barfed (%d) ", errno);
+			/* iconv barfed, but why? */
+			if (errno == EILSEQ || errno == EINVAL) {
+				/* invalid input sequence, skip it */
+				fprintf(stderr, "Invalid input sequence\n");
+				utf16++;
+				utf16bytesleft--;
+				errno = 0;
+				continue;
+			} else {
+				/* some other error, recover what we can */
+				*(char *)localcpy = '\0';
+				perror("iconv");
+				errno = 0;
+				break;
+			}
+		}
+	}
+	iconv_close(conv);
+	return local;
+}
+
 /* Function for printing to standard out from javascript (helpful for
  * debugging and demonstrates how to call C from js)
  */
 static JSBool
 js_print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
 	JSString *jsmsg;
+	size_t len;
 	int i;
 	
-	char *ucmsg;
+	jschar * ucmsg;
+	char * msg;
 	if (argc < 1) {
-		return JS_FALSE;
+		return JS_TRUE;
 	}
 	
 	for (i = 0; i<argc; i++) {
-		if (JSVAL_IS_STRING(argv[i])) {
 			jsmsg = JS_ValueToString(cx,argv[i]);
-			ucmsg = JS_GetStringBytes(jsmsg);
+			len = JS_GetStringLength(jsmsg);
+			ucmsg = JS_GetStringChars(jsmsg);
 			/* TODO: Might need to convert ucmsg to the local charset here */
-			printf("%s", ucmsg);
-		}
-		/* TODO: Cases where argv[i] is of a different type */
+			msg = utf16tolocal((char *)ucmsg, len);
+			printf("%s", msg);
 	}
 	return JS_TRUE;
 }
@@ -50,7 +112,7 @@
    UTF-16 data to jschar*, which is why we encode to UTF-16 here. */
 jschar *
 local2jschars(char * local) {
-	jschar * utf16;
+	char * utf16;
 	char * utf16cpy;
 	char * charset;
 	iconv_t conv;
@@ -59,16 +121,18 @@
 	size_t utf16bytesleft;
 	char * localcpy;
 
-	/* charset should be replaced with the charset of the locale */
+	/* TODO: charset should be replaced with the charset of the locale */
 	charset = "UTF-8";
-	conv = iconv_open("UTF-16", charset);
+	/* Little endian UTF-16 seems to be the correct encoding. */
+	conv = iconv_open("UTF-16LE", charset);
 	if (conv == (iconv_t)-1) {
-		fprintf(stderr, "local2utf16 bombed.\n");
+		fprintf(stderr, "local2jschars bombed.\n");
 		return NULL;
 	}
 	
 	localbytesleft = (strlen(local)) * sizeof(char);
-	utf16 = (jschar *)malloc((strlen(local) + 1) * sizeof(jschar));
+	utf16bytesleft = (strlen(local)) * sizeof(jschar);
+	utf16 = (char *)malloc(utf16bytesleft);
 
 	if (utf16 == NULL) {
 		fprintf(stderr, "Could not allocate memory for iconv\n");
@@ -76,12 +140,16 @@
 	}
 	
 	localcpy = local;
-	utf16cpy = (char *)utf16;
+	utf16cpy = utf16;
 
 	while (localbytesleft > 0) {
+		/* printf("Before: localbytesleft: %d utf16bytesleft: %d\n",
+			localbytesleft, utf16bytesleft); */
 		nconv = iconv(conv, 
 			&localcpy, &localbytesleft, 
 			&utf16cpy, &utf16bytesleft);
+		/* printf("After: localbytesleft: %d utf16bytesleft: %d\n",
+			localbytesleft, utf16bytesleft); */
 		if (nconv == (size_t)-1) {
 			fprintf(stderr, "local2jschars barfed (%d)\n", errno);
 			/* iconv barfed, but why? */
@@ -103,7 +171,7 @@
 		}
 	}
 	iconv_close(conv);
-	return utf16;
+	return (jschar *)utf16;
 }
 
 /* Execute some javascript commands */
@@ -122,6 +190,7 @@
 
 	va_start(ap, fmt);
 	argc = 0;
+	js_string = NULL;
 
 	for (; *fmt != '\0'; fmt++) {
 		switch (*fmt) {
@@ -178,9 +247,50 @@
 	if (!ret) {
 		printf("JS function '%s' (args: %d) not found.\n", name, argc);
 	}
+	JS_free(jscx, js_string);
 	return ret ? 1 : 0;
 }
 
+/* Prints error reports to stdout when a javascript error occurs */
+/* Taken from the spidermonkey tutorial at Kicken's World */
+static void 
+js_error_handler(JSContext *cx, const char *msg, JSErrorReport *er)
+{
+	char *pointer=NULL;
+	char *line=NULL;
+	int len;
+
+	if (er->linebuf != NULL) {
+		len = er->tokenptr - er->linebuf + 1;
+		pointer = malloc(len); memset(pointer,
+			'-', len); pointer[len-1]='\0'; 
+		pointer[len-2]='^'; 
+		len = strlen(er->linebuf)+1;
+		line = malloc(len);
+		strncpy(line, er->linebuf, len);
+		line[len-1] = '\0';
+	} else {
+		len = 0;
+		pointer = malloc(1);
+		line = malloc(1);
+		pointer[0]='\0';
+		line[0] = '\0';
+	}
+
+	while (len > 0 && (line[len-1] == '\r' || line[len-1] == '\n')) {
+		line[len-1]='\0';
+		len--;
+	}
+	
+	printf("JS Error: %s\nFile: %s:%u\n", msg, er->filename, er->lineno);
+	
+	if (line[0]) {
+		printf("%s\n%s\n", line, pointer);
+	}
+	free(pointer);
+	free(line);
+}
+
 /* Set up the javascript runtime, context and global object */
 int
 setup_js() {
@@ -200,6 +310,8 @@
 		return 0;
 	}
 
+	JS_SetErrorReporter(jscx, js_error_handler);
+
 	/* Create global object */
 	jsglob = JS_NewObject(jscx, &globclass, NULL, NULL);
 	

Modified: branches/smonkey/server.c
===================================================================
--- branches/smonkey/server.c	2006-07-27 01:04:16 UTC (rev 83)
+++ branches/smonkey/server.c	2006-08-30 21:55:27 UTC (rev 84)
@@ -164,7 +164,7 @@
 		return -1;
 	}
 	load_js("server.js");
-	js_exec("test","dis",(double)42.3, (int)69, "PootPoot!ツ\n");
+	js_exec("test","dis",(double)42.3, (int)69, "Testing js_exec. Calling the js test() function. Unicode stuff: ♫ツπ");
 
 	getsockname(main_socket, (struct sockaddr *)&addr, &size);
 	printf("Server started on %s port %d\n", inet_ntop(addr.sin6_family,&(addr.sin6_addr),addr_buff,128), ntohs(addr.sin6_port) );

Modified: branches/smonkey/server.js
===================================================================
--- branches/smonkey/server.js	2006-07-27 01:04:16 UTC (rev 83)
+++ branches/smonkey/server.js	2006-08-30 21:55:27 UTC (rev 84)
@@ -1,7 +1,7 @@
-// print("♫ Does marvin like UTF8? ♫ ... ツ\n");
+print("Test: musical note: ♫ smiley face thing: ツ new line:\n");
 
 function test (foo, bar, baz) {
-	quux = "double: " + foo + " int: " + bar + " string: " + baz + "\n";
+	quux = "\n\ndouble: " + foo + " int: " + bar + " string: " + baz + "Hmmm\n\n";
 	print(quux);
 }
 




More information about the mw-devel mailing list