[mw-devel] MARVIN r83 - branches/smonkey

welshbyte at sucs.org welshbyte at sucs.org
Thu Jul 27 02:04:17 BST 2006


Author: welshbyte
Date: 2006-07-27 02:04:16 +0100 (Thu, 27 Jul 2006)
New Revision: 83

Modified:
   branches/smonkey/js.c
   branches/smonkey/server.c
   branches/smonkey/server.js
Log:
Added a main() for debugging js.c as requested


Modified: branches/smonkey/js.c
===================================================================
--- branches/smonkey/js.c	2006-07-26 02:22:45 UTC (rev 82)
+++ branches/smonkey/js.c	2006-07-27 01:04:16 UTC (rev 83)
@@ -6,6 +6,10 @@
 #include "xml.h"
 #include "js.h"
 
+#ifdef DEBUG_JS
+#  include <readline/readline.h>
+#endif
+
 JSRuntime *jsrt;
 JSContext *jscx;
 JSObject *jsglob;
@@ -21,12 +25,14 @@
 static JSBool
 js_print(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) {
 	JSString *jsmsg;
+	int i;
+	
 	char *ucmsg;
 	if (argc < 1) {
 		return JS_FALSE;
 	}
 	
-	for (int i = 0; i<argc; i++) {
+	for (i = 0; i<argc; i++) {
 		if (JSVAL_IS_STRING(argv[i])) {
 			jsmsg = JS_ValueToString(cx,argv[i]);
 			ucmsg = JS_GetStringBytes(jsmsg);
@@ -43,33 +49,35 @@
    characters in. An appropriate jschar* is created by casting 
    UTF-16 data to jschar*, which is why we encode to UTF-16 here. */
 jschar *
-local2jschars(wchar_t * local) {
+local2jschars(char * local) {
 	jschar * utf16;
+	char * utf16cpy;
 	char * charset;
 	iconv_t conv;
 	int nconv;
 	size_t localbytesleft;
 	size_t utf16bytesleft;
 	char * localcpy;
-	char * utf16cpy;
 
-	charset = "WCHAR_T";
+	/* charset should be replaced with the charset of the locale */
+	charset = "UTF-8";
 	conv = iconv_open("UTF-16", charset);
 	if (conv == (iconv_t)-1) {
 		fprintf(stderr, "local2utf16 bombed.\n");
 		return NULL;
 	}
 	
-	localbytesleft = (wcslen(local)) * sizeof(wchar_t);
-	utf16 = malloc(wcslen(local) * sizeof(jschar));
+	localbytesleft = (strlen(local)) * sizeof(char);
+	utf16 = (jschar *)malloc((strlen(local) + 1) * sizeof(jschar));
+
 	if (utf16 == NULL) {
 		fprintf(stderr, "Could not allocate memory for iconv\n");
 		return NULL;
 	}
 	
-	localcpy = (char *)local;
+	localcpy = local;
 	utf16cpy = (char *)utf16;
-	
+
 	while (localbytesleft > 0) {
 		nconv = iconv(conv, 
 			&localcpy, &localbytesleft, 
@@ -87,7 +95,7 @@
 			} else {
 				/* some other error, recover what we can */
 				fprintf(stderr, "Some other error\n");
-				*(char *)utf16 = '\0';
+				*(char *)utf16cpy = '\0';
 				perror("iconv");
 				errno = 0;
 				break;
@@ -104,7 +112,7 @@
 	va_list ap;
 	double a_double;
 	int an_int;
-	wchar_t * a_string;
+	char * a_string;
 	jschar * js_string;
 	xmlDocPtr a_xml;
 	int argc;
@@ -135,16 +143,15 @@
 			}
 			break;
 		case 's':
-			a_string = va_arg(ap, wchar_t *);
-			/* js will handle this chunk of memory, do not free() strbytes */
-			js_string = (jschar *)JS_malloc(jscx, sizeof(jschar) * wcslen(a_string));
+			a_string = va_arg(ap, char *);
+			js_string = (jschar *)JS_malloc(jscx, sizeof(jschar) * strlen(a_string));
 			if (!js_string || js_string == NULL) {
 				fprintf(stderr, "Could not allocate memory for string. Aborting slang_exec(%s)\n", name);
 				return -1;
 			}
 			js_string = local2jschars(a_string);
 			if (js_string != NULL) {
-				argv[argc] = STRING_TO_JSVAL(JS_NewUCString(jscx, js_string, wcslen(a_string)));
+				argv[argc] = STRING_TO_JSVAL(JS_NewUCString(jscx, js_string, strlen(a_string)));
 			} else {
 				argv[argc] = STRING_TO_JSVAL(JS_NewString(jscx, "(Garbled string)", 16));
 			}
@@ -227,3 +234,24 @@
 	
 	return 1;
 }
+
+#ifdef DEBUG_JS
+int
+main(void) {
+	char * in;
+	size_t size;
+	
+	in = readline("Enter a string to pass to js_exec: ");
+
+	printf("Converting %s...\n", in);
+
+	if (!setup_js()) {
+		printf("Failed to set up js.\n");
+		return -1;
+	}
+	load_js("server.js");
+	js_exec("test","dis",(double)42.3, (int)69, in);
+
+	return 0;
+}
+#endif

Modified: branches/smonkey/server.c
===================================================================
--- branches/smonkey/server.c	2006-07-26 02:22:45 UTC (rev 82)
+++ branches/smonkey/server.c	2006-07-27 01:04:16 UTC (rev 83)
@@ -164,7 +164,7 @@
 		return -1;
 	}
 	load_js("server.js");
-	js_exec("test","dis",(double)42.3, (int)69, L"PootPoot!ツ\n");
+	js_exec("test","dis",(double)42.3, (int)69, "PootPoot!ツ\n");
 
 	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-26 02:22:45 UTC (rev 82)
+++ branches/smonkey/server.js	2006-07-27 01:04:16 UTC (rev 83)
@@ -1,4 +1,4 @@
-print("♫ Does marvin like UTF8? ♫ ... ツ\n");
+// print("♫ Does marvin like UTF8? ♫ ... ツ\n");
 
 function test (foo, bar, baz) {
 	quux = "double: " + foo + " int: " + bar + " string: " + baz + "\n";




More information about the mw-devel mailing list