mirror of
https://github.com/ccxvii/mujs.git
synced 2026-02-06 01:41:37 +08:00
Add a js_setreport callback function to report warnings.
Don't print error and warning messages directly to stderr.
This commit is contained in:
@@ -255,7 +255,7 @@ int js_dostring(js_State *J, const char *source);
|
||||
|
||||
<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 @@ int js_dofile(js_State *J, const char *filename);
|
||||
|
||||
<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>
|
||||
|
||||
7
jsgc.c
7
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)
|
||||
|
||||
1
jsi.h
1
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;
|
||||
|
||||
10
jsparse.c
10
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)
|
||||
|
||||
24
jsstate.c
24
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);
|
||||
|
||||
4
mujs.h
4
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);
|
||||
|
||||
2
regexp.c
2
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);
|
||||
|
||||
Reference in New Issue
Block a user