[mw-devel] MARVIN r81 - branches/smonkey

welshbyte at sucs.org welshbyte at sucs.org
Fri Jul 21 14:38:34 BST 2006


Author: welshbyte
Date: 2006-07-21 14:38:31 +0100 (Fri, 21 Jul 2006)
New Revision: 81

Modified:
   branches/smonkey/js.c
   branches/smonkey/server.c
   branches/smonkey/server.js
   branches/smonkey/session.c
Log:
Implemented most of js_exec. Todo: Decide how to represent XML messages in
javascript and replace push_xmldoc() accordingly.


Modified: branches/smonkey/js.c
===================================================================
--- branches/smonkey/js.c	2006-06-12 00:52:17 UTC (rev 80)
+++ branches/smonkey/js.c	2006-07-21 13:38:31 UTC (rev 81)
@@ -1,3 +1,7 @@
+#include <stdarg.h>
+#include <string.h>
+
+#include "xml.h"
 #include "js.h"
 
 JSRuntime *jsrt;
@@ -9,16 +13,104 @@
         JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub
 	};
 
+/* 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;
+	char *ucmsg;
+	if (argc < 1) {
+		return JS_FALSE;
+	}
+	
+	for (int i = 0; i<argc; i++) {
+		if (JSVAL_IS_STRING(argv[i])) {
+			jsmsg = JS_ValueToString(cx,argv[i]);
+			ucmsg = JS_GetStringBytes(jsmsg);
+			printf("%s", ucmsg);
+		}
+	}
+	return JS_TRUE;
+}
+
 /* Execute some javascript commands */
 int
 js_exec(char * name, char * fmt, ...) {
-	return 0;
+	va_list ap;
+	double a_double;
+	int an_int;
+	char * a_string;
+	char * strbytes;
+	xmlDocPtr a_xml;
+	int argc;
+	jsval argv[10]; /* Static for now, 10 should suffice */
+	jsval rval;
+	JSBool ret;
+
+	va_start(ap, fmt);
+	argc = 0;
+
+	for (; *fmt != '\0'; fmt++) {
+		switch (*fmt) {
+		case 'd':
+			a_double = va_arg(ap, double);
+			ret = JS_NewDoubleValue(jscx, (jsdouble)a_double, &(argv[argc]));
+			if (!ret) {
+				fprintf(stderr, "Creating jsdouble failed, aborting js_exec(%s)\n", name);
+				return -1;
+			}
+			break;
+		case 'i':
+			an_int = va_arg(ap, int);
+			/* js thinks in doubles */
+			ret = JS_NewDoubleValue(jscx, (jsdouble)an_int, &(argv[argc]));
+			if (!ret) {
+				fprintf(stderr, "Creating jsdouble (from int) failed, aborting js_exec(%s)\n", name);
+				return -1;
+			}
+			break;
+		case 's':
+			a_string = va_arg(ap, char *);
+			/* js will handle this chunk of memory, do not free() strbytes */
+			strbytes = (char *)JS_malloc(jscx, sizeof(a_string));
+			if (strbytes == NULL) {
+				fprintf(stderr, "Could not allocate memory for string. Aborting slang_exec(%s)\n", name);
+				return -1;
+			}
+			strcpy(strbytes, a_string);
+			argv[argc] = STRING_TO_JSVAL(JS_NewString(jscx, strbytes, strlen(strbytes)));
+			break;
+		case 'x':
+			a_xml = va_arg(ap, xmlDocPtr);
+			/* if (push_xmldoc(a_xml) == -1) {
+				fprintf(stderr, "Pushing XML doc failed, aborting slang_exec(%s)\n", name);
+				return -1;
+			} */
+			break;
+
+		default:
+			fprintf(stderr, "Aaaargh, bad type specifier to js_exec() : %c\n", *fmt);
+			return -1 ;
+		}
+		argc++;
+		if (argc > 9)
+			break;
+	}
+	
+	va_end(ap);
+	ret = JS_CallFunctionName(jscx, jsglob, name, argc, argv, &rval);
+	if (!ret) {
+		printf("JS function '%s' (args: %d) not found.\n", name, argc);
+	}
+	return ret ? 1 : 0;
 }
 
 /* Set up the javascript runtime, context and global object */
 int
 setup_js() {
 	JSBool builtins;
+	JSFunction *ok;
 	
 	/* Set up the global js runtime */
 	jsrt = JS_NewRuntime(8*1024*1024);
@@ -39,6 +131,8 @@
 	/* Init js standard classes, again global */
 	builtins = JS_InitStandardClasses(jscx, jsglob);
 
+	ok = JS_DefineFunction(jscx, jsglob, "print", js_print, 1, 0);
+
 	return 1;
 }
 

Modified: branches/smonkey/server.c
===================================================================
--- branches/smonkey/server.c	2006-06-12 00:52:17 UTC (rev 80)
+++ branches/smonkey/server.c	2006-07-21 13:38:31 UTC (rev 81)
@@ -164,6 +164,7 @@
 		return -1;
 	}
 	load_js("server.js");
+	js_exec("test","dis",(double)42, (int)69, "PootPoot!");
 
 	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-06-12 00:52:17 UTC (rev 80)
+++ branches/smonkey/server.js	2006-07-21 13:38:31 UTC (rev 81)
@@ -1 +1,6 @@
-var foo = 1;
+print("♫ Does marvin like UTF8? ♫\n");
+
+function test (foo, bar, baz) {
+	quux = "double: " + foo + " int: " + bar + " string: " + baz + "\n";
+	print(quux)
+}

Modified: branches/smonkey/session.c
===================================================================
--- branches/smonkey/session.c	2006-06-12 00:52:17 UTC (rev 80)
+++ branches/smonkey/session.c	2006-07-21 13:38:31 UTC (rev 81)
@@ -389,33 +389,30 @@
 	free(data); 
 
 	/* TODO: Change to js calls */
-	/*
 	disable_rl(1);
 	switch (s->recv_state) {
 	case CMD:
 		if (marvin_server)
-		slang_exec("incoming_command", "iisx", s->socket, msgid,  type, doc);
+		js_exec("msg_recv", "iisx", s->socket, msgid,  type, doc);
 		else
-		slang_exec("incoming_command", "isx", msgid, type, doc);
+		js_exec("msg_recv", "isx", msgid, type, doc);
 		break;
 	case RPY:
 		if (marvin_server)
-		slang_exec("incoming_reply", "iisx", s->socket, msgid,  type, doc);
+		js_exec("msg_recv", "iisx", s->socket, msgid,  type, doc);
 		else
-		slang_exec("incoming_reply", "isx", msgid, type, doc);
+		js_exec("msg_recv", "isx", msgid, type, doc);
 		break;
 	case ERR:
 		if (marvin_server)
-		slang_exec("incoming_error", "iisx", s->socket, msgid, type, doc);
+		js_exec("msg_recv", "iisx", s->socket, msgid, type, doc);
 		else
-		slang_exec("incoming_error", "isx", msgid, type, doc);
+		js_exec("msg_recv", "isx", msgid, type, doc);
 		break;
 	default:
 		printf("Got unknown ");
 		break;
 	}
 	enable_rl();
-	*/
-	
 	xml_destroy(doc);
 }




More information about the mw-devel mailing list