diff --git a/bin/main.c b/bin/main.c index 02c98d1..238229c 100644 --- a/bin/main.c +++ b/bin/main.c @@ -677,6 +677,7 @@ int main (int argc, char* argv[]) const char* logopt = HCL_NULL; hcl_oow_t heapsize = DEFAULT_HEAPSIZE; + int cflags; int verbose = 0; #if defined(HCL_BUILD_DEBUG) @@ -847,6 +848,8 @@ hcl_logufmt (hcl, HCL_LOG_WARN, fmt, ustr, 0x6789); hcl_setoption (hcl, HCL_TRAIT, &trait); } #endif + cflags = 0; + if (xtn->reader_istty) cflags = HCL_COMPILE_CLEAR_CODE | HCL_COMPILE_CLEAR_FNBLK; while (1) { @@ -882,17 +885,8 @@ count++; goto oops; } - if (xtn->reader_istty) - { - /* clear the byte code buffer */ - /* TODO: create a proper function for this and call it */ - hcl->code.bc.len = 0; - hcl->code.lit.len = 0; - } - if (verbose) hcl_prbfmt (hcl, "\n"); /* flush the output buffer by hcl_print above */ - - n = hcl_compile(hcl, obj); + n = hcl_compile(hcl, obj, cflags); hcl_freecnode (hcl, obj); /* not needed any more */ if (n <= -1) diff --git a/lib/comp.c b/lib/comp.c index 21c1425..6fe5f0a 100644 --- a/lib/comp.c +++ b/lib/comp.c @@ -3685,13 +3685,18 @@ static HCL_INLINE int emit_throw (hcl_t* hcl) /* ========================================================================= */ -int hcl_compile (hcl_t* hcl, hcl_cnode_t* obj) +int hcl_compile (hcl_t* hcl, hcl_cnode_t* obj, int flags) { hcl_oow_t saved_bc_len, saved_lit_len; hcl_bitmask_t log_default_type_mask; hcl_fnblk_info_t top_fnblk_saved; HCL_ASSERT (hcl, GET_TOP_CFRAME_INDEX(hcl) < 0); + if (flags & HCL_COMPILE_CLEAR_CODE) + { + hcl->code.bc.len = 0; + hcl->code.lit.len = 0; + } saved_bc_len = hcl->code.bc.len; saved_lit_len = hcl->code.lit.len; @@ -3930,7 +3935,11 @@ int hcl_compile (hcl_t* hcl, hcl_cnode_t* obj) HCL_ASSERT (hcl, hcl->c->fnblk.depth == 0); /* ensure the virtual function block be the only one left */ hcl->code.ngtmprs = hcl->c->fnblk.info[0].tmprcnt; /* populate the number of global temporary variables */ - /*pop_fnblk (hcl); keep the top-level virtual function block by keeping this line commented out */ + if (flags & HCL_COMPILE_CLEAR_FNBLK) + { + pop_fnblk (hcl); + HCL_ASSERT (hcl, hcl->c->fnblk.depth == -1); + } hcl ->log.default_type_mask = log_default_type_mask; return 0; diff --git a/lib/hcl-s.c b/lib/hcl-s.c index 75ade33..4e987c8 100644 --- a/lib/hcl-s.c +++ b/lib/hcl-s.c @@ -1396,7 +1396,7 @@ int hcl_server_proto_handle_request (hcl_server_proto_t* proto) } proto->worker->opstate = HCL_SERVER_WORKER_OPSTATE_COMPILE; - n = hcl_compile(proto->hcl, obj); + n = hcl_compile(proto->hcl, obj, HCL_COMPILE_CLEAR_CODE | HCL_COMPILE_CLEAR_FNBLK); hcl_freecnode (proto->hcl, obj); if (n <= -1) { diff --git a/lib/hcl.c b/lib/hcl.c index 22142a9..e457256 100644 --- a/lib/hcl.c +++ b/lib/hcl.c @@ -83,9 +83,9 @@ static void fill_bigint_tables (hcl_t* hcl) } } -static void* alloc_heap (hcl_t* hcl, hcl_oow_t size) +static void* alloc_heap (hcl_t* hcl, hcl_oow_t* size) { - return hcl_allocmem(hcl, size); + return hcl_allocmem(hcl, *size); } static void free_heap (hcl_t* hcl, void* ptr) diff --git a/lib/hcl.h b/lib/hcl.h index c6fea5e..e3977dc 100644 --- a/lib/hcl.h +++ b/lib/hcl.h @@ -1344,6 +1344,16 @@ struct hcl_dbgi_t #if defined(HCL_INCLUDE_COMPILER) typedef struct hcl_compiler_t hcl_compiler_t; typedef struct hcl_cnode_t hcl_cnode_t; + +enum hcl_compile_flag_t +{ + /* clear byte codes at the beginnign of hcl_compile() */ + HCL_COMPILE_CLEAR_CODE = (1 << 0), + + /* clear the top-level function block at the end of hcl_compile() */ + HCL_COMPILE_CLEAR_FNBLK = (1 << 1) +}; +typedef enum hcl_compile_flag_t hcl_compile_flag_t; #endif #define HCL_ERRMSG_CAPA (2048) @@ -2029,10 +2039,13 @@ HCL_EXPORT hcl_ooi_t hcl_proutufmt ( ... ); +#if defined(HCL_INCLUDE_COMPILER) HCL_EXPORT int hcl_compile ( hcl_t* hcl, - hcl_cnode_t* obj + hcl_cnode_t* obj, + int flags ); +#endif /** * The hcl_decode() function decodes instructions from the position