Add special error handling for safe 'protected' functions.

This commit is contained in:
Tor Andersson
2021-07-23 16:23:08 +02:00
parent 822061539a
commit 9dd965b679
2 changed files with 41 additions and 1 deletions

View File

@@ -16,7 +16,7 @@ static void jsR_run(js_State *J, js_Function *F);
static void js_trystackoverflow(js_State *J)
{
STACK[TOP].type = JS_TLITSTR;
STACK[TOP].u.litstr = "try stack overflow";
STACK[TOP].u.litstr = "exception stack overflow";
++TOP;
js_throw(J);
}

View File

@@ -8,6 +8,16 @@
#include <assert.h>
#include <errno.h>
static int js_ptry(js_State *J) {
if (J->trytop == JS_TRYLIMIT) {
STACK[TOP].type = JS_TLITSTR;
STACK[TOP].u.litstr = "exception stack overflow";
++TOP;
return 1;
}
return 0;
}
static void *js_defaultalloc(void *actx, void *ptr, int size)
{
if (size == 0) {
@@ -31,6 +41,8 @@ static void js_defaultpanic(js_State *J)
int js_ploadstring(js_State *J, const char *filename, const char *source)
{
if (js_ptry(J))
return 1;
if (js_try(J))
return 1;
js_loadstring(J, filename, source);
@@ -40,6 +52,8 @@ int js_ploadstring(js_State *J, const char *filename, const char *source)
int js_ploadfile(js_State *J, const char *filename)
{
if (js_ptry(J))
return 1;
if (js_try(J))
return 1;
js_loadfile(J, filename);
@@ -50,6 +64,10 @@ int js_ploadfile(js_State *J, const char *filename)
const char *js_trystring(js_State *J, int idx, const char *error)
{
const char *s;
if (js_ptry(J)) {
js_pop(J, 1);
return error;
}
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -62,6 +80,10 @@ const char *js_trystring(js_State *J, int idx, const char *error)
double js_trynumber(js_State *J, int idx, double error)
{
double v;
if (js_ptry(J)) {
js_pop(J, 1);
return error;
}
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -74,6 +96,10 @@ double js_trynumber(js_State *J, int idx, double error)
int js_tryinteger(js_State *J, int idx, int error)
{
int v;
if (js_ptry(J)) {
js_pop(J, 1);
return error;
}
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -86,6 +112,10 @@ int js_tryinteger(js_State *J, int idx, int error)
int js_tryboolean(js_State *J, int idx, int error)
{
int v;
if (js_ptry(J)) {
js_pop(J, 1);
return error;
}
if (js_try(J)) {
js_pop(J, 1);
return error;
@@ -189,6 +219,11 @@ void js_loadfile(js_State *J, const char *filename)
int js_dostring(js_State *J, const char *source)
{
if (js_ptry(J)) {
js_report(J, "exception stack overflow");
js_pop(J, 1);
return 1;
}
if (js_try(J)) {
js_report(J, js_trystring(J, -1, "Error"));
js_pop(J, 1);
@@ -204,6 +239,11 @@ int js_dostring(js_State *J, const char *source)
int js_dofile(js_State *J, const char *filename)
{
if (js_ptry(J)) {
js_report(J, "exception stack overflow");
js_pop(J, 1);
return 1;
}
if (js_try(J)) {
js_report(J, js_trystring(J, -1, "Error"));
js_pop(J, 1);