[mw-devel] MW3 r1310 - trunk/src

welshbyte at sucs.org welshbyte at sucs.org
Sat Dec 8 05:30:38 GMT 2012


Author: welshbyte
Date: 2012-12-08 05:30:38 +0000 (Sat, 08 Dec 2012)
New Revision: 1310

Modified:
   trunk/src/Makefile
   trunk/src/add.c
   trunk/src/edit.c
   trunk/src/js.c
   trunk/src/js.h
   trunk/src/log.c
   trunk/src/main.c
   trunk/src/newmain.c
   trunk/src/talker.c
Log:
Update the JS bits to API version 1.8.5 (from 1.8.0). This fixes the build on Fedora (yay). Roughly involved:
- Upgraded silver's libmozjs-dev to the one in the debian-backports repo
- Fixed the Makefile to sniff the lib version with pkg-config and work around a silly JSScript/JSObject difference between Fedora and Debian
- Lots of replacing JS_GetStringBytes calls with JS_EncodeString
- Added a few includes which are needed to build on Fedora
- Changing the signature of the js function definitions (a bit prettier now)
- Various other tweaks to satisfy the API changes


Modified: trunk/src/Makefile
===================================================================
--- trunk/src/Makefile	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/Makefile	2012-12-08 05:30:38 UTC (rev 1310)
@@ -6,13 +6,22 @@
 STATEDIR := $(localstatedir)/lib/mw
 HOMEPATH := $(libdir)/mw
 
-# Name of the js library (differs between distros)
-JSLIB=$(shell if `echo "main(){}" | gcc -x c -ljs - 2> /dev/null`; then echo js; else echo mozjs; fi; rm -f a.out)
-CFLAGS+=-I/usr/include/mozjs
+ifeq ($(JSFLAGS),)
+#Fedora
+JSFLAGS=$(shell pkg-config --cflags libjs 2>/dev/null)
+JSLIBS=$(shell pkg-config --libs-only-l libjs 2>/dev/null)
+JSSCRIPTTYPE=JSObject # Some idiot made the script APIs different without changing the version
+endif
+ifeq ($(JSFLAGS),)
+#Debian
+JSFLAGS=$(shell pkg-config --cflags mozilla-js)
+JSLIBS=$(shell pkg-config --libs-only-l mozilla-js)
+JSSCRIPTTYPE=JSScript
+endif
 
 # cflags for standard 'cc' compiler
-CFLAGS+= -Wall -Wshadow -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith -Wwrite-strings -Wcast-align -Wbad-function-cast -Wmissing-format-attribute -Wformat=2 -Wformat-security -Wformat-nonliteral -Wno-long-long -Wno-strict-aliasing -pedantic -std=gnu99 -D_GNU_SOURCE
-LDLIBS+= -lreadline -ltermcap -lcrypt -l$(JSLIB) -lsqlite3 -lcurl -lpthread -lgnutls-openssl -ljansson
+CFLAGS+= -Wall -Wshadow -Wmissing-prototypes -Wstrict-prototypes -Wpointer-arith -Wwrite-strings -Wcast-align -Wbad-function-cast -Wmissing-format-attribute -Wformat=2 -Wformat-security -Wformat-nonliteral -Wno-long-long -Wno-strict-aliasing -pedantic -std=gnu99 -D_GNU_SOURCE $(JSFLAGS) -DJSSCRIPTTYPE=$(JSSCRIPTTYPE)
+LDLIBS+= -lreadline -ltermcap -lcrypt $(JSLIBS) -lsqlite3 -lcurl -lpthread -lcrypto -ljansson
 
 ifdef WITHPIE
 CFLAGS += -fpie
@@ -35,7 +44,7 @@
 CFLAGS+= -ggdb -g -D__NO_STRING_INLINE -fstack-protector-all
 
 ### Optimisation - uncomment for release & extra testing
-CFLAGS+=-O3
+CFLAGS+=-O0
 
 CFLAGS += $(DEFS)
 

Modified: trunk/src/add.c
===================================================================
--- trunk/src/add.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/add.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -10,6 +10,7 @@
 #include <signal.h>
 #include <errno.h>
 #include <sys/types.h>
+#include <sys/stat.h>
 #include <unistd.h>
 #include <sys/wait.h>
 #include <time.h>

Modified: trunk/src/edit.c
===================================================================
--- trunk/src/edit.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/edit.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -15,6 +15,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <stdbool.h>
+#include <sys/stat.h>
 #include "talker_privs.h"
 #include "special.h"
 #include "incoming.h"

Modified: trunk/src/js.c
===================================================================
--- trunk/src/js.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/js.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -1,7 +1,5 @@
 /* Add javascript functionality using spidermonkey */
 
-#define XP_UNIX
-
 #include <fcntl.h>
 #include <stdlib.h>
 #include <string.h>
@@ -10,8 +8,18 @@
 #include <pwd.h>
 #include <readline/readline.h>
 #include <curl/curl.h>
+#include <errno.h>
 
+/* Ugly but necessary (this is how gjs shuts up the warnings,
+   only gcc 4.6 has a nicer (push/pop) way to do it */
+#pragma GCC diagnostic ignored "-Wstrict-prototypes"
+#pragma GCC diagnostic ignored "-Winvalid-offsetof"
+#pragma GCC diagnostic ignored "-Wbad-function-cast"
+#include <jsapi.h>
 #include <jsdbgapi.h>
+#pragma GCC diagnostic warning "-Wbad-function-cast"
+#pragma GCC diagnostic warning "-Winvalid-offsetof"
+#pragma GCC diagnostic warning "-Wstrict-prototypes"
 
 #include "bb.h"
 #include "sqlite.h"
@@ -28,6 +36,7 @@
 #include "init.h"
 #include "who.h"
 #include "js.h"
+#include "util.h"
 
 extern Alias alias_list;
 extern Alias bind_list;
@@ -56,36 +65,36 @@
 #define K_BROADCAST 1
 
 JSClass globclass = {
-	"milliways", JSCLASS_HAS_PRIVATE,
-	JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,
+	"milliways", JSCLASS_GLOBAL_FLAGS,
+	JS_PropertyStub,JS_PropertyStub,JS_PropertyStub,JS_StrictPropertyStub,
 	JS_EnumerateStub,JS_ResolveStub,JS_ConvertStub,JS_FinalizeStub,
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
 JSClass js_dbresultclass = {
 	"dbresult", JSCLASS_HAS_PRIVATE,
-	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
 	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
 JSClass js_whoclass = {
 	"who", JSCLASS_HAS_PRIVATE,
-	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
 	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
 JSClass js_userrecordclass = {
 	"userrecord", JSCLASS_HAS_PRIVATE,
-	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
 	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
 
 JSClass js_termsizeclass = {
 	"termsize", JSCLASS_HAS_PRIVATE,
-	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub,
+	JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
 	JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
 	JSCLASS_NO_OPTIONAL_MEMBERS
 };
@@ -138,25 +147,25 @@
 /* Function for printing to standard out from javascript (helpful for
  * debugging and demonstrates how to call C from js) - also useful for event functions
  */
-static JSBool js_print(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_print(JSContext *cx, uintN argc, jsval *vp) 
 {
 	uintN i;
-	char *msg;
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER msg = NULL;
 	if (argc < 1) {
 		return JS_TRUE;
 	}
 	
 	for (i = 0; i<argc; i++) {
 		if (JSVAL_IS_STRING(argv[i]) || JSVAL_IS_NUMBER(argv[i])) {
-			msg = JS_GetStringBytes(JS_ValueToString(cx, argv[i]));
+			msg = JS_EncodeString(cx, JS_ValueToString(cx, argv[i]));
 			display_message(msg, 0, 1);
-			
 		} else 
 		if (JSVAL_IS_NULL(argv[i])) {
 			display_message("jsval is NULL",0,1);
 		} else
 		if (JSVAL_IS_OBJECT(argv[i])) {
-			printf("jsval at %p\n", (void *)argv[i]);
+			printf("jsval at %p\n", (void *)JSVAL_TO_OBJECT(argv[i]));
 			if (JS_IsArrayObject(cx, JSVAL_TO_OBJECT(argv[i]))) {
 				display_message("jsval is an (Array)",0,1);
 			}
@@ -166,20 +175,17 @@
 }
 
 /* execute a talker command */
-static JSBool js_mwexec(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_mwexec(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char msg[MAXTEXTLENGTH];
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER msg = NULL;
 	if (argc < 1) {
 		JS_ReportError(cx, "exec() expects an argument.");
 		return JS_FALSE;
 	}
 	
 	if (JSVAL_IS_STRING(argv[0])) {
-		
-		// 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' ;
+		msg = JS_EncodeString(cx, JS_ValueToString(cx, argv[0]));
 		DoCommand(msg, chattable);
 		return JS_TRUE;
 	}
@@ -189,14 +195,14 @@
 
 
 /* say to the talker */
-static JSBool js_say(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_say(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char msg[MAXTEXTLENGTH];
-	//int conversion_result;
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER msg = NULL;
+
 	if (argc < 1) {
 		JS_ReportError(cx, "say() expects an argument.");
 		return JS_FALSE;
-		
 	}
 
 	flood++;
@@ -204,30 +210,26 @@
 		JS_ReportError(cx, "FLOOD: This script has flooded the room.");
 		return JS_FALSE;
 	}
-	
+
 	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' ;
+		msg = JS_EncodeString(cx, JS_ValueToString(cx, argv[0]));
 		chat_say(msg);
 		return JS_TRUE;
-	
 	}
 	JS_ReportError(cx, "Error: say() expects a string or a number.");
-	
+
 	return JS_FALSE;
 }
 
 /* send an rpc/rpb */
-static JSBool js_rpc(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_rpc(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char msg[MAXTEXTLENGTH]="";
-	char rpc_type[MAXTEXTLENGTH]="";
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER msg = NULL;
+	AUTOFREE_BUFFER rpc_type = NULL;
 	char username[NAMESIZE+1]="";
 	int broadcast=0;
-	
+
 	if (argc < 3) {
 		JS_ReportError(cx, "Error: javascript rpc() expects 3 arguments");
 		return JS_FALSE;
@@ -237,33 +239,31 @@
 			broadcast=1;
 		}
 	}
-	
+
 	if (JSVAL_IS_STRING(argv[0])) {
-		strncpy(username, JS_GetStringBytes(JS_ValueToString(cx, argv[0])), NAMESIZE);
+		JS_EncodeStringToBuffer(JS_ValueToString(cx, argv[0]), username, NAMESIZE);
 		username[NAMESIZE] = '\0' ;
 	}
-	
-	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') {
+	rpc_type = JS_EncodeString(cx, JS_ValueToString(cx, argv[1]));
+	msg = JS_EncodeString(cx, JS_ValueToString(cx, argv[2]));
+
+	if((!broadcast && (!username[0])) || !rpc_type || !rpc_type[0]) {
 		JS_ReportError(cx, "Error: javascript rpc(): invalid arguments - [%s] [%s]", broadcast ? "BROADCAST":username, rpc_type);
 		return JS_FALSE;
 	}
-	
+
 	sendrpc(username, rpc_type, msg, broadcast);
-	
+
 	return JS_TRUE;
 }
 
 /* send an ipc/ipb */
-static JSBool js_ipc(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_ipc(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char msg[MAXTEXTLENGTH]="";
-	char username[NAMESIZE+1]="";
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER msg = NULL;
+	AUTOFREE_BUFFER username = NULL;
 	int broadcast=0;
 	if (argc < 2) {
 		JS_ReportError(cx, "Error: javascript ipc() expects 2 arguments");
@@ -275,31 +275,28 @@
 		}
 	}
 	if (JSVAL_IS_STRING(argv[0])) {
-		strncpy(username, JS_GetStringBytes(JS_ValueToString(cx, argv[0])), NAMESIZE);
-		username[NAMESIZE] = '\0' ;
+		username = JS_EncodeString(cx, JS_ValueToString(cx, argv[0]));
 	}
-	
 
-	strncpy(msg, JS_GetStringBytes(JS_ValueToString(cx, argv[1])), MAXTEXTLENGTH);
-	msg[MAXTEXTLENGTH-1] = '\0' ;
+	msg = JS_EncodeString(cx, JS_ValueToString(cx, argv[1]));
 
 	// not broadcast and no username
-	if(broadcast==0 && username[0]=='\0') {
+	if(broadcast==0 && (!username || username[0]=='\0')) {
 		JS_ReportError(cx, "Error: javascript ipc(): expects a username or K_BROADCAST");
-
 		return JS_FALSE;
 	}
-	
+
 	sendipc(username, msg, broadcast);
 	return JS_TRUE;
 }
 
 /* ask a user for extra input */
-static JSBool js_input(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) 
+static JSBool js_input(JSContext *cx, uintN argc, jsval *vp) 
 {
+	jsval *argv = JS_ARGV(cx, vp);
 	JSString *the_jsstring, *jsstr;
 	size_t ucs2_length, prompt_length, line_length;
-	jschar *ucs2_string;
+	const jschar *ucs2_string;
 	int conv_error;
 	
 	char *prompt=NULL, *line;
@@ -309,7 +306,7 @@
 			// 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);
+			ucs2_string=JS_GetStringCharsZ(cx, the_jsstring);
 			prompt_length=sizeof(char)*((ucs2_length*3)+1); // bit ugly
 			prompt=malloc(prompt_length); 
 			if(prompt!=NULL) {
@@ -319,7 +316,7 @@
 					free(prompt);
 					prompt=NULL;
 				}
-			}		
+			}
 		} else {
 			js_warning(cx, "When a parameter is specified to javascript input() command it should be a string to use as the prompt");
 		}
@@ -334,37 +331,36 @@
 	if ((line=readline(prompt))==NULL) line=strdup("");
 	js_start_timeout();
 	busy--;
-		
+
 	free(prompt);
 	
 	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) {
-		
+	jschar *u2_string = malloc(line_length);
+	if(u2_string!=NULL) {
 		conv_error=convert_string_charset(line, "LOCAL", strlen(line),
-											(char *)ucs2_string, "UTF-16//TRANSLIT", line_length, 
-											NULL, NULL, NULL, NULL, NULL);
+		         (char *)u2_string, "UTF-16//TRANSLIT", line_length, 
+		          NULL, NULL, NULL, NULL, NULL);
 		if(conv_error>=0) {
-			jsstr = JS_NewUCStringCopyZ(cx, ucs2_string);
+			jsstr = JS_NewUCStringCopyZ(cx, u2_string);
 		} else {
 			jsstr = JS_NewStringCopyZ(cx, "(garbled string)");
 		}
-	
-		free(ucs2_string);
+
+		free(u2_string);
 	} else {
-		jsstr = JS_NewStringCopyZ(cx, "(garbled string)");		
+		jsstr = JS_NewStringCopyZ(cx, "(garbled string)");
 	}
-	*rval=STRING_TO_JSVAL(jsstr);
-	
+	JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(jsstr));
 	return JS_TRUE;
 }
 
 
 /* beep */
-static JSBool js_beep(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_beep(JSContext *cx, uintN argc, jsval *vp)
 {
+	jsval *argv = JS_ARGV(cx, vp);
 	int i, beeps=0;
 	if(argc < 1) {
 		beeps=1;
@@ -387,11 +383,11 @@
 }
 
 /* bind something to a javascript function */
-static JSBool js_bind(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_bind(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char *jbind=NULL;
-	char *function_name=NULL;
-//	int conversion_result;
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER jbind = NULL;
+	AUTOFREE_BUFFER function_name = NULL;
 	int bind_type=-1;
 	int i=1;
 	char msg[MAXTEXTLENGTH];
@@ -404,21 +400,20 @@
 	}
 		
 	if (JSVAL_IS_STRING(argv[0])) {
-		jbind = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
+		jbind = JS_EncodeString(cx, 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++;
-			jbind = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
-
+			jbind = JS_EncodeString(cx, 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_FALSE;
 	}
 	if (argc>= i-1 && JSVAL_IS_STRING(argv[i])) {
-		function_name = JS_GetStringBytes(JS_ValueToString(cx, argv[i]));
+		function_name = JS_EncodeString(cx, JS_ValueToString(cx, argv[i]));
 
 	} else {
 		JS_ReportError(cx, "Error in javascript: bind expects final argument to be a string.");
@@ -514,9 +509,10 @@
 }
 
 /* bind something to a javascript function */
-static JSBool js_unbind(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_unbind(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char *jbind=NULL;
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER jbind = NULL;
 	char *function_name=NULL;
 	int bind_type=-1;
 	char msg[MAXTEXTLENGTH];
@@ -529,12 +525,12 @@
 	}
 		
 	if (JSVAL_IS_STRING(argv[0])) {
-		jbind = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
+		jbind = JS_EncodeString(cx, JS_ValueToString(cx, argv[0]));
 		bind_type=K_BIND;
 	} else if (JSVAL_IS_INT(argv[0])) {
 		bind_type=JSVAL_TO_INT(argv[0]);
 		if(JSVAL_IS_STRING(argv[1])) {
-			jbind = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
+			jbind = JS_EncodeString(cx, JS_ValueToString(cx, argv[1]));
 			function_name = jbind;
 		} else {
 			JS_ReportError(cx, "Error in javascript: bind expects final argument to be a string.");
@@ -634,10 +630,9 @@
 }
 
 // return the users terminal dimensions
-static JSBool js_termsize(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) 
+static JSBool js_termsize(JSContext *cx, uintN argc, jsval *vp) 
 {
 	JSObject *result_object;
-	jsval jv;
 	int width, height;
 	
 	result_object=JS_NewObject(cx, &js_termsizeclass, NULL, NULL);
@@ -647,21 +642,19 @@
 
 		return JS_FALSE;
 	}
-	jv=OBJECT_TO_JSVAL(result_object);
-	
+
 	width=screen_w();
 	height=screen_h();
-	
+
 	JS_DefineProperty(cx, result_object, "width", INT_TO_JSVAL(width), NULL, NULL, 0);
 	JS_DefineProperty(cx, result_object, "height", INT_TO_JSVAL(height), NULL, NULL, 0);
-	
-	
-	*rval=jv;
+	JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(result_object));
+
 	return JS_TRUE;
 }
 
 // Provides a javascript function to query the wholist
-static JSBool js_wholist(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) {
+static JSBool js_wholist(JSContext *cx, uintN argc, jsval *vp) {
 	struct person u;
 	struct who w;
 	int ufile, wfile;
@@ -676,7 +669,7 @@
 		return JS_FALSE;
 	}
 	res = JS_NewArrayObject(cx, 0, NULL);
-	JS_AddRoot(cx, res);
+	JS_AddObjectRoot(cx, &res);
 
 	while (read(wfile,&w,sizeof(w))) {
 		JSObject *user_record;
@@ -729,8 +722,8 @@
 	close(wfile);
 	close(ufile);
 
-	JS_RemoveRoot(cx, res);
-	*rval = OBJECT_TO_JSVAL(res);
+	JS_RemoveObjectRoot(cx, &res);
+	JS_SET_RVAL(cx, vp, OBJECT_TO_JSVAL(res));
 	return JS_TRUE;
 }
 
@@ -747,9 +740,10 @@
 
 /* Function to make a url GET request and return the resulting page
  */
-static JSBool js_urlget(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval __attribute__((unused)) *rval) 
+static JSBool js_urlget(JSContext *cx, uintN argc, jsval *vp) 
 {
-	char *url;
+	jsval *argv = JS_ARGV(cx, vp);
+	AUTOFREE_BUFFER url = NULL;
 	JSString *jsstr;
 	char msg[MAXTEXTLENGTH];
 	
@@ -765,7 +759,7 @@
 		char cerr[CURL_ERROR_SIZE];
 		struct block_t *answer = block_new(1024);
 
-		url = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
+		url = JS_EncodeString(cx, JS_ValueToString(cx, argv[0]));
 		cl = curl_easy_init();
 		curl_easy_setopt(cl, CURLOPT_WRITEFUNCTION, urldata);
 		curl_easy_setopt(cl, CURLOPT_WRITEDATA, answer);
@@ -782,7 +776,7 @@
 		curl_easy_cleanup(cl);
 
 		jsstr = JS_NewStringCopyZ(cx, answer->p_buffer);
-		*rval = STRING_TO_JSVAL(jsstr);
+		JS_SET_RVAL(cx, vp, STRING_TO_JSVAL(jsstr));
 
 		free(answer);
 		return JS_TRUE;
@@ -806,7 +800,7 @@
 	if (data == NULL || ncols < 1) return NULL;
 
 	jsdata = JS_NewArrayObject(cx, 0, NULL);
-	JS_AddRoot(cx, jsdata);
+	JS_AddObjectRoot(cx, &jsdata);
 
 	for (i = 0; i < ncols; i++) {
 //		printf("dbdata_to_jsarray: data @ %p -", (void *)data->field[i]);
@@ -821,7 +815,7 @@
 		JS_SetElement(cx, jsdata, i, &jv);
 
 	}
-	JS_RemoveRoot(cx, jsdata);
+	JS_RemoveObjectRoot(cx, &jsdata);
 
 	return jsdata;
 }
@@ -838,7 +832,7 @@
 	if (data == NULL) return NULL;
 
 	jsarray = JS_NewArrayObject(cx, 0, NULL);
-	JS_AddRoot(cx, jsarray);
+	JS_AddObjectRoot(cx, &jsarray);
 
 /*	printf("Making Array(%d)\n", data->cols); */
 	i = 0;
@@ -852,7 +846,7 @@
 		node = node->next;
 		i++;
 	}
-	JS_RemoveRoot(cx, jsarray);
+	JS_RemoveObjectRoot(cx, &jsarray);
 
 	return jsarray;
 }
@@ -867,7 +861,7 @@
 	if (data == NULL) return NULL;
 
 	jsarray = JS_NewArrayObject(cx, 0, NULL);
-	JS_AddRoot(cx, jsarray);
+	JS_AddObjectRoot(cx, &jsarray);
 	
 	for(i=0;i<data->cols;i++) {
 		jsstr = JS_NewStringCopyZ(cx, data->colNames[i]);
@@ -876,7 +870,7 @@
 		
 	}
 	
-	JS_RemoveRoot(cx, jsarray);
+	JS_RemoveObjectRoot(cx, &jsarray);
 
 	return jsarray;
 }
@@ -922,15 +916,17 @@
 // Provides a javascript function to query an sqlite3 database
 // This probably wants updating to not return JS_FALSE as that halts js execution
 // far better to return an error code in rsval which the javascript can handle
-static JSBool js_doquery(JSContext *cx, JSObject __attribute__((unused)) *obj, uintN argc, jsval *argv, jsval *rval) {
+static JSBool js_doquery(JSContext *cx, uintN argc, jsval *vp)
+{
+	jsval *argv = JS_ARGV(cx, vp);
 	struct js_db_result *dbres;
-	char *dbname;
-	char *query;
-	jsval resobject_jsval = 0;
+	AUTOFREE_BUFFER dbname = NULL;
+	AUTOFREE_BUFFER query = NULL;
+	jsval resobject_jsval;
 	char path[1024];
 	struct passwd *pw;
 	JSBool retval;
-	
+
 	if ((pw=getpwuid(getuid()))==NULL) {
 		JS_ReportError(cx, "Error getting user information");
 		return JS_FALSE;
@@ -951,10 +947,10 @@
 		return JS_FALSE;
 	}
 
-	dbname = JS_GetStringBytes(JS_ValueToString(cx, argv[0]));
-	query = JS_GetStringBytes(JS_ValueToString(cx, argv[1]));
-	
-	if (dbname[0] == '/'
+	dbname = JS_EncodeString(cx, JS_ValueToString(cx, argv[0]));
+	query = JS_EncodeString(cx, JS_ValueToString(cx, argv[1]));
+
+	if (!dbname || dbname[0] == '/'
 	||  strncmp(dbname, "../", 3)==0
 	||  strstr(dbname, "/../")) {
 		JS_ReportError(cx, "Illegal path element in dbname '%s'", dbname);
@@ -972,18 +968,18 @@
 	}
 
 	retval = dbresult_to_jsdbobject(cx, dbres, &resobject_jsval);
-	
-	
 	js_db_free(dbres);
-	
-	*rval = resobject_jsval;
+
+	JS_SET_RVAL(cx, vp, resobject_jsval);
 	return retval;
 }
 
-static JSBool js_store_get(JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
+static JSBool js_store_get(JSContext *cx, JSObject *obj, jsid idval, jsval *vp)
 {
-	if (JSVAL_IS_STRING(idval)) {
-		char *key = JS_GetStringBytes(JS_ValueToString(cx, idval));
+	jsval sval;
+	JSBool ret = JS_IdToValue(cx, idval, &sval);
+	if (ret == JS_TRUE && JSVAL_IS_STRING(sval)) {
+		AUTOFREE_BUFFER key = JS_EncodeString(cx, JS_ValueToString(cx, sval));
 		char *val = userdb_get(USERDB_PUBLIC, user->name, key);
 		if (val == NULL) {
 			*vp = JSVAL_VOID;
@@ -996,11 +992,13 @@
 	return JS_TRUE;
 }
 
-static JSBool js_store_set(JSContext *cx, JSObject *obj, jsval idval, jsval *vp)
+static JSBool js_store_set(JSContext *cx, JSObject *obj, jsid idval, JSBool strict, 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));
+	jsval sval;
+	JSBool ret = JS_IdToValue(cx, idval, &sval);
+	if (ret == JS_TRUE && JSVAL_IS_STRING(sval) && JSVAL_IS_STRING(*vp)) {
+		AUTOFREE_BUFFER key = JS_EncodeString(cx, JS_ValueToString(cx, sval));
+		AUTOFREE_BUFFER val = JS_EncodeString(cx, JS_ValueToString(cx, *vp));
 		userdb_set(USERDB_PUBLIC, user->name, key, val);
 	}
 	return JS_TRUE;
@@ -1116,35 +1114,16 @@
 /* Non utf-8 chars are stripped */
 int load_jsfile(FILE *f, const char *filename)
 {
-	char *body;
-	int where, len;
 	JSBool success;
-	JSScript *script = NULL;
+#ifdef JSSCRIPTTYPE
+	JSSCRIPTTYPE *script;
+#else
+#error "JSSCRIPTTYPE not defined"
+#endif
 	jsval retval;
-	uintN lineno=0;
 
-	where = ftell(f);
-	fseek(f, 0, SEEK_END);
-	len = ftell(f);
-	fseek(f, where, SEEK_SET);
-
-/*	printf("Loading %d bytes from %s\n", len, filename); */
-
-	body = malloc(len+1);
-	if(body==NULL) {
-		fprintf(stderr, "load_jsfile: could not allocate memory for javascript file\n");
-		return 1;
-	}
-	
-	fread(body, 1, len, f);
-	body[len]=0;
-	/*convert the script into jsstring, scripts assumed to be utf8*/
-	
 	/* Compile the js file specified */
-	 script = JS_CompileScript(jscx, jsroot, body, len, filename, lineno); 
-
-	free(body);
-	
+	script = JS_CompileFileHandle(jscx, jsroot, filename, f);
 	if (script == NULL) {
 		printf("Failed to compile js script: %s\n", filename);
 		return 1;
@@ -1159,9 +1138,6 @@
 		return 1;
 	}
 	js_clear_timeout();
-	// now the script has been run we can destroy it (the context retains the functions/objects it created)
-	
-	JS_DestroyScript(jscx, script);
 	return 0;
 }
 
@@ -1245,11 +1221,19 @@
 		printf("Error creating JS Context\n");
 		return -1;
 	}
-	/* register an error handler */
+
+	JS_BeginRequest(jscx);
+	JS_SetOptions(jscx, JSOPTION_VAROBJFIX | JSOPTION_METHODJIT);
+	JS_SetVersion(jscx, JSVERSION_LATEST);
 	JS_SetErrorReporter(jscx, js_error_handler);
 
 	/* create the root object */
-	jsroot = JS_NewObject(jscx, &globclass, NULL, NULL);
+	jsroot = JS_NewCompartmentAndGlobalObject(jscx, &globclass, NULL);
+	if (jsroot == NULL) {
+		printf("Failed to create global js object\n");
+		JS_EndRequest(jscx);
+		return -1;
+	}
 
 	/* initiate builtin classes */
 	JS_InitStandardClasses(jscx, jsroot);
@@ -1307,6 +1291,7 @@
 	 * - one to load another script (include?) - possibly although most scripts are loaded from the .mwrc or using .load
 	 * - get system date/time - no, 'new Date()' will get this information for you in a Date object.
 	 */
+	JS_EndRequest(jscx);
 
 	return 0;
 }

Modified: trunk/src/js.h
===================================================================
--- trunk/src/js.h	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/js.h	2012-12-08 05:30:38 UTC (rev 1310)
@@ -6,7 +6,6 @@
 int js_isrunning(void);
 int js_exec(char *name, int argc, const char **argvc);
 int load_jsfile(FILE *f, const char *filename);
-int load_js(char *filename);
 int is_js(char *name);
 void js_stop_execution(void);
 int stop_js(void);

Modified: trunk/src/log.c
===================================================================
--- trunk/src/log.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/log.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -5,7 +5,7 @@
 #include <regex.h>
 #include <pthread.h>
 #include <sqlite3.h>
-#include <gnutls/openssl.h>
+#include <openssl/md5.h>
 #include <curl/curl.h>
 
 #include "strings.h"

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/main.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -11,6 +11,7 @@
 #include <stdarg.h>
 #include <termcap.h>
 #include <stdbool.h>
+#include <sys/stat.h>
 
 #include "command.h"
 #include "alarm.h"

Modified: trunk/src/newmain.c
===================================================================
--- trunk/src/newmain.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/newmain.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <time.h>
 #include <stdbool.h>
+#include <locale.h>
 
 #include "iconv.h"
 #include "strings.h"
@@ -607,7 +608,7 @@
 	while(read(file,&hdr,sizeof(hdr))>0)
 	{
 		struct listing *listing = head;
-		while (listing != NULL && strcasecmp(hdr.from, ptr->name))
+		while (listing != NULL && strcasecmp(hdr.from, listing->name))
 			listing = listing->next;
 
 		if (listing != NULL)

Modified: trunk/src/talker.c
===================================================================
--- trunk/src/talker.c	2012-11-30 22:37:39 UTC (rev 1309)
+++ trunk/src/talker.c	2012-12-08 05:30:38 UTC (rev 1310)
@@ -211,8 +211,9 @@
 	/* we have terminal information */
 	if (g_boTermCap == 1)
 	{
+		char li[] = "li";
 		/* check if line count exists */
-		nLines = tgetnum("li");
+		nLines = tgetnum(li);
 	}
 
 	/* not using termcap, or lines not found */
@@ -238,8 +239,9 @@
 	/* we have terminal information */
 	if (g_boTermCap == 1)
 	{
+		char co[] = "co";
 		/* check if column count exists */
-		nCols = tgetnum("co");
+		nCols = tgetnum(co);
 	}
 
 	/* not using termcap, or columns not found */




More information about the mw-devel mailing list