From 2bd510181c067e9744e2769d032ee1cbecee614c Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Sun, 25 Feb 2024 13:55:03 +0900 Subject: [PATCH] enhanced error handling in some object allocation functions --- bin/main.c | 7 +++++++ lib/hcl.h | 4 ++-- lib/obj.c | 44 +++++++++++++++++++++++++++++++++----------- lib/xchg.c | 2 ++ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/bin/main.c b/bin/main.c index 63eef93..b393242 100644 --- a/bin/main.c +++ b/bin/main.c @@ -512,6 +512,8 @@ static int on_fed_cnode_in_interactive_mode (hcl_t* hcl, hcl_cnode_t* obj) if (!xtn->feed.ongoing) { + /* the first expression in the current user input line. + * arrange to clear byte-codes before compiling the expression. */ flags = HCL_COMPILE_CLEAR_CODE | HCL_COMPILE_CLEAR_FNBLK; xtn->feed.ongoing = 1; } @@ -525,8 +527,13 @@ static int on_fed_cnode_in_interactive_mode (hcl_t* hcl, hcl_cnode_t* obj) else { hcl_oow_t i; + for (i = xtn->feed.pos; i < xtn->feed.len; i++) { + /* this loop is kind of weird. it is to check the current feed buffer is left with + * spaces only and to execute the compiled bytecodes so far if the check is true. + * the check is performed because a single line of the user input can have multiple + * expressions joined with a semicolon or contains trailing spaces. */ if (!hcl_is_bch_space(xtn->feed.buf[i])) break; } diff --git a/lib/hcl.h b/lib/hcl.h index 8e6fe94..2210e0d 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -2404,7 +2404,7 @@ HCL_EXPORT int hcl_addliteraltocode ( ); /** - * The hcl_brewcode() initializes the structure pointed to by \a code.partially or entirely. + * The hcl_brewcode() initializes the structure pointed to by \a code partially or entirely. * The part already initialized is not destroyed and/or reinitialized. */ HCL_EXPORT int hcl_brewcode ( @@ -2413,7 +2413,7 @@ HCL_EXPORT int hcl_brewcode ( ); /** - * The hcl_purgecode() function cleans up the data held in memory + * The hcl_purgecode() function cleans up the data held in code space memory * pointed to by \a code. */ HCL_EXPORT void hcl_purgecode ( diff --git a/lib/obj.c b/lib/obj.c index 5933b70..baaee31 100644 --- a/lib/obj.c +++ b/lib/obj.c @@ -24,7 +24,6 @@ #include "hcl-prv.h" - #if defined(HCL_PROFILE_VM) #include #include /* getrusage */ @@ -339,13 +338,19 @@ hcl_oop_t hcl_makebytestring (hcl_t* hcl, const hcl_ooch_t* ptr, hcl_oow_t len, hcl_oow_t i; hcl_oob_t v; - b = alloc_numeric_array(hcl, HCL_BRAND_BYTE_ARRAY, HCL_NULL, len, HCL_OBJ_TYPE_BYTE, HCL_SIZEOF(hcl_oob_t), 1, ngc); - if (HCL_UNLIKELY(!b)) return HCL_NULL; - - for (i = 0; i < len; i++) + b = (hcl_oop_byte_t)alloc_numeric_array(hcl, HCL_BRAND_BYTE_ARRAY, HCL_NULL, len, HCL_OBJ_TYPE_BYTE, HCL_SIZEOF(hcl_oob_t), 1, ngc); + if (HCL_UNLIKELY(!b)) { - v = ptr[i] & 0xFF; - HCL_OBJ_SET_BYTE_VAL(b, i, v); + const hcl_ooch_t* orgmsg = hcl_backuperrmsg(hcl); + hcl_seterrbfmt (hcl, HCL_ERRNUM(hcl), "unable to make bytestring - %js", orgmsg); + } + else + { + for (i = 0; i < len; i++) + { + v = ptr[i] & 0xFF; + HCL_OBJ_SET_BYTE_VAL(b, i, v); + } } return (hcl_oop_t)b; @@ -353,8 +358,15 @@ hcl_oop_t hcl_makebytestring (hcl_t* hcl, const hcl_ooch_t* ptr, hcl_oow_t len, hcl_oop_t hcl_makestring (hcl_t* hcl, const hcl_ooch_t* ptr, hcl_oow_t len, int ngc) { - /*return hcl_alloccharobj(hcl, HCL_BRAND_STRING, ptr, len);*/ - return alloc_numeric_array(hcl, HCL_BRAND_STRING, ptr, len, HCL_OBJ_TYPE_CHAR, HCL_SIZEOF(hcl_ooch_t), 1, ngc); + hcl_oop_char_t c; + /*c = hcl_alloccharobj(hcl, HCL_BRAND_STRING, ptr, len);*/ + c = (hcl_oop_char_t)alloc_numeric_array(hcl, HCL_BRAND_STRING, ptr, len, HCL_OBJ_TYPE_CHAR, HCL_SIZEOF(hcl_ooch_t), 1, ngc); + if (HCL_UNLIKELY(!c)) + { + const hcl_ooch_t* orgmsg = hcl_backuperrmsg(hcl); + hcl_seterrbfmt (hcl, HCL_ERRNUM(hcl), "unable to make string - %js", orgmsg); + } + return (hcl_oop_t)c; } hcl_oop_t hcl_makefpdec (hcl_t* hcl, hcl_oop_t value, hcl_ooi_t scale) @@ -375,7 +387,12 @@ hcl_oop_t hcl_makefpdec (hcl_t* hcl, hcl_oop_t value, hcl_ooi_t scale) f = (hcl_oop_fpdec_t)hcl_allocoopobj(hcl, HCL_BRAND_FPDEC, HCL_FPDEC_NAMED_INSTVARS); hcl_popvolat (hcl); - if (HCL_UNLIKELY(!f)) return HCL_NULL; + if (HCL_UNLIKELY(!f)) + { + const hcl_ooch_t* orgmsg = hcl_backuperrmsg(hcl); + hcl_seterrbfmt (hcl, HCL_ERRNUM(hcl), "unable to make fpdec - %js", orgmsg); + return HCL_NULL; + } f->value = value; f->scale = HCL_SMOOI_TO_OOP(scale); @@ -392,7 +409,12 @@ hcl_oop_t hcl_makeclass (hcl_t* hcl, hcl_oop_t superclass, hcl_ooi_t nivars, hcl hcl_pushvolat (hcl, &cvars_str); c = (hcl_oop_class_t)hcl_allocoopobj(hcl, HCL_BRAND_CLASS, HCL_CLASS_NAMED_INSTVARS + ncvars); hcl_popvolats (hcl, 3); - if (HCL_UNLIKELY(!c)) return HCL_NULL; + if (HCL_UNLIKELY(!c)) + { + const hcl_ooch_t* orgmsg = hcl_backuperrmsg(hcl); + hcl_seterrbfmt (hcl, HCL_ERRNUM(hcl), "unable to make class - %js", orgmsg); + return HCL_NULL; + } c->superclass = superclass; c->nivars = HCL_SMOOI_TO_OOP(nivars); diff --git a/lib/xchg.c b/lib/xchg.c index fd86d68..c6d1721 100644 --- a/lib/xchg.c +++ b/lib/xchg.c @@ -604,6 +604,8 @@ int hcl_unmarshalcodefrommem (hcl_t* hcl, hcl_code_t* code, const hcl_ptl_t* src int hcl_brewcode (hcl_t* hcl, hcl_code_t* code) { + /* create space to hold byte code and debug information */ + if (!code->bc.ptr) { code->bc.ptr = (hcl_oob_t*)hcl_allocmem(hcl, HCL_SIZEOF(*code->bc.ptr) * HCL_BC_BUFFER_INIT); /* TODO: set a proper intial size */