diff --git a/docs/reference.html b/docs/reference.html index e5655a0..341a536 100644 --- a/docs/reference.html +++ b/docs/reference.html @@ -255,7 +255,7 @@ int js_dostring(js_State *J, const char *source);

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.

@@ -264,7 +264,7 @@ int js_dofile(js_State *J, const char *filename);
 
 

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.

Protected environments

diff --git a/jsgc.c b/jsgc.c index 62b5821..0d6df86 100644 --- a/jsgc.c +++ b/jsgc.c @@ -210,9 +210,12 @@ void js_gc(js_State *J, int report) ++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) diff --git a/jsi.h b/jsi.h index 0686a78..2b25f2e 100644 --- a/jsi.h +++ b/jsi.h @@ -147,6 +147,7 @@ struct js_State void *actx; void *uctx; js_Alloc alloc; + js_Report report; js_Panic panic; js_StringNode *strings; diff --git a/jsparse.c b/jsparse.c index 0d45c8d..4e1f26c 100644 --- a/jsparse.c +++ b/jsparse.c @@ -43,11 +43,15 @@ static void jsP_error(js_State *J, const char *fmt, ...) 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) diff --git a/jsstate.c b/jsstate.c index ae23f05..9d2cea2 100644 --- a/jsstate.c +++ b/jsstate.c @@ -16,9 +16,15 @@ static void *js_defaultalloc(void *actx, void *ptr, int size) 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 @@ void js_loadfile(js_State *J, const char *filename) 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_dostring(js_State *J, const char *source) 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 @@ js_Panic js_atpanic(js_State *J, js_Panic panic) 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 @@ js_State *js_newstate(js_Alloc alloc, void *actx, int flags) 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); diff --git a/mujs.h b/mujs.h index 58087c2..a4b5d0d 100644 --- a/mujs.h +++ b/mujs.h @@ -39,11 +39,13 @@ typedef void (*js_Finalize)(js_State *J, void *p); 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); @@ -83,6 +85,8 @@ enum { 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); void js_newrangeerror(js_State *J, const char *message); diff --git a/regexp.c b/regexp.c index 0357580..9cad0c9 100644 --- a/regexp.c +++ b/regexp.c @@ -1003,7 +1003,7 @@ static int match(Reinst *pc, const char *sp, const char *bol, int flags, Resub * 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);