shithub: libmujs

Download patch

ref: fb32b5c893a0685dceaa2bb72bf64880e0cc4572
parent: 50281c9cee10c8068649f446e896004ff56f6e04
author: Tor Andersson <tor.andersson@gmail.com>
date: Tue May 16 07:55:49 EDT 2017

Add a js_setreport callback function to report warnings.

Don't print error and warning messages directly to stderr.

--- a/docs/reference.html
+++ b/docs/reference.html
@@ -255,7 +255,7 @@
 
 <p>
 Compile and execute the script in the zero-terminated string in source argument.
-If any errors occur, print the error message to stderr and return 1.
+If any errors occur, call the report callback function and return 1.
 Return 0 on success.
 
 <pre>
@@ -264,7 +264,7 @@
 
 <p>
 Load the script from the file with the given filename, then compile and execute it.
-If any errors occur, print the error message to stderr and return 1.
+If any errors occur, call the report callback function and return 1.
 Return 0 on success.
 
 <h3>Protected environments</h3>
--- a/jsgc.c
+++ b/jsgc.c
@@ -210,9 +210,12 @@
 		++nstr;
 	}
 
-	if (report)
-		printf("garbage collected: %d/%d envs, %d/%d funs, %d/%d objs, %d/%d strs\n",
+	if (report) {
+		char buf[256];
+		snprintf(buf, sizeof buf, "garbage collected: %d/%d envs, %d/%d funs, %d/%d objs, %d/%d strs",
 			genv, nenv, gfun, nfun, gobj, nobj, gstr, nstr);
+		js_report(J, buf);
+	}
 }
 
 void js_freestate(js_State *J)
--- a/jsi.h
+++ b/jsi.h
@@ -147,6 +147,7 @@
 	void *actx;
 	void *uctx;
 	js_Alloc alloc;
+	js_Report report;
 	js_Panic panic;
 
 	js_StringNode *strings;
--- a/jsparse.c
+++ b/jsparse.c
@@ -43,11 +43,15 @@
 static void jsP_warning(js_State *J, const char *fmt, ...)
 {
 	va_list ap;
-	fprintf(stderr, "%s:%d: warning: ", J->filename, J->lexline);
+	char buf[512];
+	char msg[256];
+
 	va_start(ap, fmt);
-	vfprintf(stderr, fmt, ap);
+	vsnprintf(msg, sizeof msg, fmt, ap);
 	va_end(ap);
-	fprintf(stderr, "\n");
+
+	snprintf(buf, sizeof buf, "%s:%d: warning: %s", J->filename, J->lexline, msg);
+	js_report(J, buf);
 }
 
 static js_Ast *jsP_newnode(js_State *J, enum js_AstType type, js_Ast *a, js_Ast *b, js_Ast *c, js_Ast *d)
--- a/jsstate.c
+++ b/jsstate.c
@@ -16,9 +16,15 @@
 	return realloc(ptr, (size_t)size);
 }
 
+static void js_defaultreport(js_State *J, const char *message)
+{
+	fputs(message, stderr);
+	fputc('\n', stderr);
+}
+
 static void js_defaultpanic(js_State *J)
 {
-	fprintf(stderr, "uncaught exception\n");
+	js_report(J, "uncaught exception");
 	/* return to javascript to abort */
 }
 
@@ -138,7 +144,7 @@
 int js_dostring(js_State *J, const char *source)
 {
 	if (js_try(J)) {
-		fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
+		js_report(J, js_trystring(J, -1, "Error"));
 		js_pop(J, 1);
 		return 1;
 	}
@@ -153,7 +159,7 @@
 int js_dofile(js_State *J, const char *filename)
 {
 	if (js_try(J)) {
-		fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
+		js_report(J, js_trystring(J, -1, "Error"));
 		js_pop(J, 1);
 		return 1;
 	}
@@ -172,6 +178,17 @@
 	return old;
 }
 
+void js_report(js_State *J, const char *message)
+{
+	if (J->report)
+		J->report(J, message);
+}
+
+void js_setreport(js_State *J, js_Report report)
+{
+	J->report = report;
+}
+
 void js_setcontext(js_State *J, void *uctx)
 {
 	J->uctx = uctx;
@@ -206,6 +223,7 @@
 	J->trace[0].file = "native";
 	J->trace[0].line = 0;
 
+	J->report = js_defaultreport;
 	J->panic = js_defaultpanic;
 
 	J->stack = alloc(actx, NULL, JS_STACKSIZE * sizeof *J->stack);
--- a/mujs.h
+++ b/mujs.h
@@ -39,11 +39,13 @@
 typedef int (*js_HasProperty)(js_State *J, void *p, const char *name);
 typedef int (*js_Put)(js_State *J, void *p, const char *name);
 typedef int (*js_Delete)(js_State *J, void *p, const char *name);
+typedef void (*js_Report)(js_State *J, const char *message);
 
 /* Basic functions */
 js_State *js_newstate(js_Alloc alloc, void *actx, int flags);
 void js_setcontext(js_State *J, void *uctx);
 void *js_getcontext(js_State *J);
+void js_setreport(js_State *J, js_Report report);
 js_Panic js_atpanic(js_State *J, js_Panic panic);
 void js_freestate(js_State *J);
 void js_gc(js_State *J, int report);
@@ -82,6 +84,8 @@
 	JS_DONTENUM = 2,
 	JS_DONTCONF = 4,
 };
+
+void js_report(js_State *J, const char *message);
 
 void js_newerror(js_State *J, const char *message);
 void js_newevalerror(js_State *J, const char *message);
--- a/regexp.c
+++ b/regexp.c
@@ -1003,7 +1003,7 @@
 				continue;
 			case I_SPLIT:
 				if (nready >= MAXTHREAD) {
-					fprintf(stderr, "regexec: backtrack overflow!\n");
+					/* fprintf(stderr, "regexec: backtrack overflow!\n"); */
 					return 0;
 				}
 				spawn(&ready[nready++], pc->y, sp, &sub);