Guard state initialization with try to avoid panic in initialization.

This commit is contained in:
Tor Andersson
2022-06-09 15:53:51 +02:00
parent 88f6d86b6c
commit 3451b6ca96
2 changed files with 10 additions and 0 deletions
+6
View File
@@ -322,6 +322,11 @@ js_State *js_newstate(js_Alloc alloc, void *actx, int flags)
J->nextref = 0;
J->gcthresh = 0; /* reaches stability within ~ 2-5 GC cycles */
if (js_try(J)) {
js_freestate(J);
return NULL;
}
J->R = jsV_newobject(J, JS_COBJECT, NULL);
J->G = jsV_newobject(J, JS_COBJECT, NULL);
J->E = jsR_newenvironment(J, J->G, NULL);
@@ -329,5 +334,6 @@ js_State *js_newstate(js_Alloc alloc, void *actx, int flags)
jsB_init(J);
js_endtry(J);
return J;
}
+4
View File
@@ -311,6 +311,10 @@ main(int argc, char **argv)
}
J = js_newstate(NULL, NULL, strict ? JS_STRICT : 0);
if (!J) {
fprintf(stderr, "Could not initialize MuJS.\n");
exit(1);
}
js_newcfunction(J, jsB_gc, "gc", 0);
js_setglobal(J, "gc");