From 4d8375fdfd29999575654d93e71598a3a3c3462e Mon Sep 17 00:00:00 2001 From: dzaima Date: Sun, 30 May 2021 02:20:55 +0300 Subject: [PATCH] =?UTF-8?q?make=20=E2=8A=8F=20safe?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sfns.c | 37 +++++++++++++++++++++++++++++-------- src/h.h | 12 ++++++------ src/vm.c | 2 +- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/src/builtins/sfns.c b/src/builtins/sfns.c index eb1ff3d5..8f222700 100644 --- a/src/builtins/sfns.c +++ b/src/builtins/sfns.c @@ -102,6 +102,15 @@ B select_c2(B t, B w, B x) { } dec(w); dec(x); return r; + } else if (TI(x).elType==el_f64) { + f64* rp; B r = m_f64arrc(&rp, w); + f64* xp = f64any_ptr(x); + for (usz i = 0; i < wia; i++) { + usz c = WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)); + rp[i] = xp[c]; + } + dec(w); dec(x); + return r; } else if (TI(x).elType==el_c32) { u32* rp; B r = m_c32arrc(&rp, w); u32* xp = c32any_ptr(x); @@ -111,26 +120,38 @@ B select_c2(B t, B w, B x) { } dec(w); dec(x); return r; + } else if (v(x)->type==t_harr) { + usz i = 0; + B* xp = harr_ptr(x); + HArr_p r = m_harrs(wia, &i); + for (; i < wia; i++) { + usz c = WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)); + r.a[i] = inc(xp[c]); + } + dec(x); + return harr_fcd(r, w); } else { - HArr_p r = m_harrUc(w); - for (usz i = 0; i < wia; i++) { + usz i = 0; + HArr_p r = m_harrs(wia, &i); + for (; i < wia; i++) { usz c = WRAP(wp[i], xia, thrF("โŠ: Indexing out-of-bounds (%iโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", wp[i], xia)); r.a[i] = xget(x, c); } - dec(w); dec(x); - return withFill(r.b,xf); + dec(x); + return withFill(harr_fcd(r,w),xf); } } else { - HArr_p r = m_harrUc(w); + usz i = 0; + HArr_p r = m_harrs(wia, &i); BS2B wgetU = TI(w).getU; - for (usz i = 0; i < wia; i++) { + for (; i < wia; i++) { B cw = wgetU(w, i); if (!isNum(cw)) { harr_pfree(r.b, i); goto base; } usz c = WRAP(o2i64(cw), xia, thrF("โŠ: Indexing out-of-bounds (%RโˆŠ๐•จ, %sโ‰กโ‰ ๐•ฉ)", cw, xia)); r.a[i] = xget(x, c); } - dec(w); dec(x); - return withFill(r.b,xf); + dec(x); + return withFill(harr_fcd(r,w),xf); } } else { BS2B wgetU = TI(w).getU; diff --git a/src/h.h b/src/h.h index 54a96b46..103ad0d6 100644 --- a/src/h.h +++ b/src/h.h @@ -23,13 +23,13 @@ #define MM 1 // memory manager; 0 - malloc (no GC); 1 - buddy; 2 - 2buddy #define HEAP_MAX ~0ULL // default heap max size -// #define LOG_GC // log GC stats +// #define LOG_GC // log GC stats #define FORMATTER // use self-hosted formatter for output -// #define TIME // output runtime of every expression -// #define RT_PERF // time runtime primitives -// #define RT_VERIFY // compare native and runtime versions of primitives -// #define NO_RT // whether to completely disable self-hosted runtime loading -// #define PRECOMP // execute just precompiled code at src/gen/interp +// #define TIME // output runtime of every expression +// #define RT_PERF // time runtime primitives +// #define RT_VERIFY // compare native and runtime versions of primitives +// #define NO_RT // whether to completely disable self-hosted runtime loading +// #define PRECOMP // execute just precompiled code at src/gen/interp #include diff --git a/src/vm.c b/src/vm.c index 952d7609..93a9f037 100644 --- a/src/vm.c +++ b/src/vm.c @@ -38,7 +38,7 @@ enum { SYSV = 30, // N; get system function N LOCU = 31, // N0,N1; like LOCO but overrides the slot with bi_optOut EXTO, EXTM, EXTU, // alternate versions of LOC_ for extended variables - ADDI, ADDU, // PUSH with required increment & not required increment + ADDI, ADDU, // separate PUSH for refcounting needed/not needed (stores the object inline, instead of reading from `objs`) BC_SIZE };