various OOM fixes

This commit is contained in:
dzaima 2023-01-23 23:49:27 +02:00
parent b0dbd5c37b
commit 7768d6475f
5 changed files with 46 additions and 23 deletions

View File

@ -210,7 +210,7 @@ B cell2_empty(B f, B w, B x, ur wr, ur xr) {
if (!isPureFn(f) || !CATCH_ERRORS) { dec(w); dec(x); return emptyHVec(); } if (!isPureFn(f) || !CATCH_ERRORS) { dec(w); dec(x); return emptyHVec(); }
if (wr) w = to_fill_cell_1(w); if (wr) w = to_fill_cell_1(w);
if (xr) x = to_fill_cell_1(x); if (xr) x = to_fill_cell_1(x);
if (CATCH) return emptyHVec(); if (CATCH) { freeThrown(); return emptyHVec(); }
B rc = c2(f, w, x); B rc = c2(f, w, x);
popCatch(); popCatch();
return merge_fill_result_1(rc); return merge_fill_result_1(rc);
@ -346,7 +346,7 @@ B cell_c1(Md1D* d, B x) { B f = d->f;
if (cam==0) { if (cam==0) {
if (!isPureFn(f) || !CATCH_ERRORS) { decG(x); return emptyHVec(); } if (!isPureFn(f) || !CATCH_ERRORS) { decG(x); return emptyHVec(); }
B cf = to_fill_cell_1(x); B cf = to_fill_cell_1(x);
if (CATCH) return emptyHVec(); if (CATCH) { freeThrown(); return emptyHVec(); }
B rc = c1(f, cf); B rc = c1(f, cf);
popCatch(); popCatch();
return merge_fill_result_1(rc); return merge_fill_result_1(rc);

View File

@ -274,6 +274,7 @@ static B rank2_empty(B f, B w, ur wk, B x, ur xk) {
if (wk) w = to_fill_cell(w, wk); if (wk) w = to_fill_cell(w, wk);
if (xk) x = to_fill_cell(x, xk); if (xk) x = to_fill_cell(x, xk);
if (CATCH) { empty: if (CATCH) { empty:
freeThrown();
r = empty_frame(sh, k); r = empty_frame(sh, k);
} else { } else {
B rc = c2(f, w, x); B rc = c2(f, w, x);
@ -314,6 +315,7 @@ B rank_c1(Md2D* d, B x) { B f = d->f; B g = d->g;
B cf = to_fill_cell(x, k); B cf = to_fill_cell(x, k);
B r; B r;
if (CATCH) { empty: if (CATCH) { empty:
freeThrown();
r = empty_frame(xsh, k); r = empty_frame(xsh, k);
} else { } else {
B rc = c1(f, cf); B rc = c1(f, cf);

View File

@ -154,7 +154,9 @@ static bool isCmd(char* s, char** e, const char* cmd) {
return false; return false;
} }
#define CATCH_OOM(X) if (CATCH) { freeThrown(); X; }
void highlighter_replxx(const char* input, ReplxxColor* colors, int size, void* data) { void highlighter_replxx(const char* input, ReplxxColor* colors, int size, void* data) {
CATCH_OOM(return)
B charObj = utf8Decode0(input); B charObj = utf8Decode0(input);
if (IA(charObj) != size) goto end; // don't want to kill the REPL if this happens, but gotta do _something_ if (IA(charObj) != size) goto end; // don't want to kill the REPL if this happens, but gotta do _something_
@ -195,6 +197,7 @@ static bool isCmd(char* s, char** e, const char* cmd) {
} }
end: end:
dec(charObj); dec(charObj);
popCatch();
} }
B allNsFields(void); B allNsFields(void);
B str_norm(u32* chars, usz len, bool* upper) { B str_norm(u32* chars, usz len, bool* upper) {
@ -223,6 +226,7 @@ static bool isCmd(char* s, char** e, const char* cmd) {
return c; return c;
} }
NOINLINE void completion_impl(const char* inp, void* res, bool hint, int* dist) { NOINLINE void completion_impl(const char* inp, void* res, bool hint, int* dist) {
CATCH_OOM(return)
B inpB = toC32Any(utf8Decode0(inp)); B inpB = toC32Any(utf8Decode0(inp));
u32* chars = c32any_ptr(inpB); u32* chars = c32any_ptr(inpB);
u32* we = chars+IA(inpB); u32* we = chars+IA(inpB);
@ -289,6 +293,7 @@ static bool isCmd(char* s, char** e, const char* cmd) {
} }
dec(inpB); dec(inpB);
popCatch();
} }
void complete_replxx(const char* inp, replxx_completions* res, int* dist, void* data) { void complete_replxx(const char* inp, replxx_completions* res, int* dist, void* data) {
completion_impl(inp, res, false, dist); completion_impl(inp, res, false, dist);
@ -337,6 +342,7 @@ static bool isCmd(char* s, char** e, const char* cmd) {
static bool inBackslash() { return b_pv.u!=0; } static bool inBackslash() { return b_pv.u!=0; }
static void stopBackslash() { decG(b_pv); b_pv.u = 0; } static void stopBackslash() { decG(b_pv); b_pv.u = 0; }
ReplxxActionResult backslash_replxx(int code, void* data) { ReplxxActionResult backslash_replxx(int code, void* data) {
CATCH_OOM(goto end)
if (inBackslash()) { if (inBackslash()) {
setState(insertChar('\\', false)); setState(insertChar('\\', false));
stopBackslash(); stopBackslash();
@ -348,6 +354,8 @@ static bool isCmd(char* s, char** e, const char* cmd) {
b_pv = utf8Decode0(st.text); b_pv = utf8Decode0(st.text);
b_pp = st.cursorPosition; b_pp = st.cursorPosition;
} }
popCatch();
end:
return REPLXX_ACTION_RESULT_CONTINUE; return REPLXX_ACTION_RESULT_CONTINUE;
} }
ReplxxActionResult enter_replxx(int code, void* data) { ReplxxActionResult enter_replxx(int code, void* data) {
@ -376,7 +384,7 @@ static bool isCmd(char* s, char** e, const char* cmd) {
void modified_replxx(char** s_res, int* p_res, void* userData) { void modified_replxx(char** s_res, int* p_res, void* userData) {
if (!cfg_enableKeyboard) return; if (!cfg_enableKeyboard) return;
if (!inBackslash()) return; if (!inBackslash()) return;
CATCH_OOM(return)
TmpState t = getState(); TmpState t = getState();
B s = t.s; B s = t.s;
u64 pos = t.pos; u64 pos = t.pos;
@ -392,6 +400,7 @@ static bool isCmd(char* s, char** e, const char* cmd) {
stop: decG(s); stop: decG(s);
stopBackslash(); stopBackslash();
popCatch();
} }

View File

@ -1,5 +1,6 @@
#include "../core.h" #include "../core.h"
#include "utf.h" #include "utf.h"
#include "calls.h"
static i8 utf8lenb(u8 ch) { static i8 utf8lenb(u8 ch) {
if (ch<128) return 1; if (ch<128) return 1;
@ -111,24 +112,33 @@ void fprintsB(FILE* f, B x) {
if (xe==el_c32) { if (xe==el_c32) {
fprintsU32(f, c32any_ptr(x), ia); fprintsU32(f, c32any_ptr(x), ia);
} else { } else {
incG(x); #define BUF_SZ 1024
if (!elChr(xe)) { x=chr_squeeze(x); xe=TI(x,elType); } if (elChr(xe)) {
if (!elChr(xe)) { if (xe==el_c32) {
#if !CATCH_ERRORS fprintsU32(f, c32any_ptr(x), ia);
SGetU(x) } else {
for (usz i = 0; i < ia; i++) { u32 buf[BUF_SZ];
B c = GetU(x, i); usz i = 0;
if (isC32(c)) fprintCodepoint(f, o2cG(c)); while (i < ia) {
else if (c.u==0 || noFill(c)) fprintf(f, " "); usz curr = ia-i;
else thrM("Trying to output non-character"); if (curr>BUF_SZ) curr = BUF_SZ;
COPY_TO(buf, el_c32, 0, x, i, curr);
fprintsU32(f, buf, curr);
i+= curr;
} }
return; }
#endif } else {
thrM("Trying to output non-character"); SGetU(x)
for (usz i = 0; i < ia; i++) {
B c = GetU(x, i);
if (isC32(c)) fprintCodepoint(f, o2cG(c));
#if CATCH_ERRORS
else if (c.u==0 || noFill(c)) fprintf(f, " ");
#endif
else thrM("Trying to output non-character");
}
} }
x = taga(cpyC32Arr(x)); #undef BUF_SZ
fprintsU32(f, c32any_ptr(x), ia);
decG(x);
} }
} }

View File

@ -1344,7 +1344,7 @@ NOINLINE void vm_printPos(Comp* comp, i32 bcPos, i64 pos) {
#if FORCE_NATIVE_ERROR_PRINT #if FORCE_NATIVE_ERROR_PRINT
goto native_print; goto native_print;
#endif #endif
if (CATCH) goto native_print; if (CATCH) { freeThrown(); goto native_print; }
B s = emptyCVec(); B s = emptyCVec();
B msg = vm_fmtPoint(src, s, comp->path, cs, ce); B msg = vm_fmtPoint(src, s, comp->path, cs, ce);
@ -1600,9 +1600,11 @@ void unwindCompiler() {
NOINLINE void printErrMsg(B msg) { NOINLINE void printErrMsg(B msg) {
if (isArr(msg)) { if (isArr(msg)) {
SGetU(msg) if (!elChr(TI(msg,elType))) {
usz msgLen = IA(msg); SGetU(msg)
for (usz i = 0; i < msgLen; i++) if (!isC32(GetU(msg,i))) goto base; usz msgLen = IA(msg);
for (usz i = 0; i < msgLen; i++) if (!isC32(GetU(msg,i))) goto base;
}
fprintsB(stderr, msg); fprintsB(stderr, msg);
return; return;
} }