removed hawk::array_size(), hawk::array_tally().

enhanced hawk::array() and hawk::map() to accept values
This commit is contained in:
hyung-hwan 2020-05-03 15:49:31 +00:00
parent 84d869606f
commit 8ad37dabf0
4 changed files with 69 additions and 73 deletions

View File

@ -97,6 +97,21 @@ typedef struct hawk_tree_t hawk_tree_t;
# define HAWK_MAX_RTX_STACK_LIMIT ((hawk_oow_t)1 << (HAWK_SIZEOF_VOID_P * 4))
#endif
/* Don't forget to grow HAWK_IDX_BUF_SIZE if hawk_int_t is very large */
#if (HAWK_SIZEOF_INT_T <= 16) /* 128 bits */
# define HAWK_IDX_BUF_SIZE 64
#elif (HAWK_SIZEOF_INT_T <= 32) /* 256 bits */
# define HAWK_IDX_BUF_SIZE 128
#elif (HAWK_SIZEOF_INT_T <= 64) /* 512 bits */
# define HAWK_IDX_BUF_SIZE 192
#elif (HAWK_SIZEOF_INT_T <= 128) /* 1024 bits */
# define HAWK_IDX_BUF_SIZE 384
#elif (HAWK_SIZEOF_INT_T <= 256) /* 2048 bits */
# define HAWK_IDX_BUF_SIZE 640
#else
# error unsupported. hawk_int_t too big
#endif
/* ------------------------------------------------------------------------ */

View File

@ -224,61 +224,69 @@ static int fnc_gcrefs (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
static int fnc_array (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{
hawk_val_t* tmp;
hawk_oow_t nargs, i;
tmp = hawk_rtx_makearrval(rtx, -1);
nargs = hawk_rtx_getnargs(rtx);
tmp = hawk_rtx_makearrval(rtx, ((nargs > 0)? nargs: -1));
if (HAWK_UNLIKELY(!tmp)) return -1; /* hard failure */
/* TODO: take arguments and put them to the array*/
for (i = 0; i < nargs; i++)
{
if (HAWK_UNLIKELY(hawk_rtx_setarrvalfld(rtx, tmp, i + 1, hawk_rtx_getarg(rtx, i)) == HAWK_NULL))
{
hawk_rtx_freeval (rtx, tmp, 0);
return -1;
}
}
hawk_rtx_setretval (rtx, tmp);
return 0;
}
static int fnc_array_size (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{
hawk_val_t* a0;
hawk_oow_t iv;
a0 = hawk_rtx_getarg(rtx, 0);
if (HAWK_RTX_GETVALTYPE(rtx, a0) != HAWK_VAL_ARR)
{
hawk_rtx_seterrnum (rtx, HAWK_NULL, HAWK_ENOTARR);
return -1;
}
iv = HAWK_ARR_SIZE(((hawk_val_arr_t*)a0)->arr);
HAWK_ASSERT (iv >= 0 && iv <= HAWK_QUICKINT_MAX);
hawk_rtx_setretval (rtx, hawk_rtx_makeintval(rtx, iv));
return 0;
}
static int fnc_array_tally (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{
hawk_val_t* a0;
hawk_oow_t iv;
a0 = hawk_rtx_getarg(rtx, 0);
if (HAWK_RTX_GETVALTYPE(rtx, a0) != HAWK_VAL_ARR)
{
hawk_rtx_seterrnum (rtx, HAWK_NULL, HAWK_ENOTARR);
return -1;
}
iv = HAWK_ARR_TALLY(((hawk_val_arr_t*)a0)->arr);
HAWK_ASSERT (iv >= 0 && iv <= HAWK_QUICKINT_MAX);
hawk_rtx_setretval (rtx, hawk_rtx_makeintval(rtx, iv));
return 0;
}
static int fnc_map (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{
hawk_val_t* tmp;
hawk_oow_t nargs, i;
hawk_ooch_t idxbuf[HAWK_IDX_BUF_SIZE];
tmp = hawk_rtx_makemapval(rtx);
if (HAWK_UNLIKELY(!tmp)) return -1; /* hard failure */
/* TODO: take arguments and put them to the map */
nargs = hawk_rtx_getnargs(rtx);
for (i = 0; i < nargs; i++)
{
hawk_rtx_valtostr_out_t out;
hawk_val_t* v;
v = hawk_rtx_getarg(rtx, i);
out.type = HAWK_RTX_VALTOSTR_CPLCPY;
out.u.cplcpy.ptr = idxbuf;
out.u.cplcpy.len = HAWK_COUNTOF(idxbuf);
if (hawk_rtx_valtostr(rtx, v, &out) <= -1)
{
out.type = HAWK_RTX_VALTOSTR_CPLDUP;
if (hawk_rtx_valtostr(rtx, v, &out) <= -1)
{
hawk_rtx_freeval (rtx, tmp, 0);
return -1;
}
}
v = (++i >= nargs)? hawk_val_nil: hawk_rtx_getarg(rtx, i);
v = hawk_rtx_setmapvalfld(rtx, tmp, out.u.cpldup.ptr, out.u.cpldup.len, v);
if (out.u.cpldup.ptr != idxbuf) hawk_rtx_freemem (rtx, out.u.cpldup.ptr);
if (HAWK_UNLIKELY(!v))
{
hawk_rtx_freeval (rtx, tmp, 0);
return -1;
}
/* if (i >= nargs) break; this check is probably not needed. another i++ in the 3rd segment of the for statement should be mostly harmless. potential overflow issue is not a real issue as the number of arguments can be so high. */
}
hawk_rtx_setretval (rtx, tmp);
return 0;
}
@ -365,9 +373,6 @@ static fnctab_t fnctab[] =
{
/* keep this table sorted for binary search in query(). */
{ HAWK_T("array"), { { 0, A_MAX }, fnc_array, 0 } },
{ HAWK_T("array_size"), { { 1, 1 }, fnc_array_size, 0 } },
{ HAWK_T("array_tally"), { { 1, 1 }, fnc_array_tally, 0 } },
{ HAWK_T("call"), { { 1, A_MAX, HAWK_T("vR") }, fnc_call, 0 } },
{ HAWK_T("function_exists"), { { 1, 1, HAWK_NULL }, fnc_function_exists, 0 } },
{ HAWK_T("gc"), { { 0, 1, HAWK_NULL }, fnc_gc, 0 } },

View File

@ -32,21 +32,6 @@
#define DEF_BUF_CAPA 256
#define HAWK_RTX_STACK_INCREMENT 512
/* Don't forget to grow IDXBUFSIZE if hawk_int_t is very large */
#if (HAWK_SIZEOF_INT_T <= 16) /* 128 bits */
# define IDXBUFSIZE 64
#elif (HAWK_SIZEOF_INT_T <= 32) /* 256 bits */
# define IDXBUFSIZE 128
#elif (HAWK_SIZEOF_INT_T <= 64) /* 512 bits */
# define IDXBUFSIZE 192
#elif (HAWK_SIZEOF_INT_T <= 128) /* 1024 bits */
# define IDXBUFSIZE 384
#elif (HAWK_SIZEOF_INT_T <= 256) /* 2048 bits */
# define IDXBUFSIZE 640
#else
# error unsupported. hawk_int_t too big
#endif
enum exit_level_t
{
EXIT_NONE,
@ -2997,7 +2982,7 @@ static HAWK_INLINE int delete_indexed (hawk_rtx_t* rtx, hawk_val_t* vv, hawk_nde
hawk_map_t* map;
hawk_ooch_t* str = HAWK_NULL;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
hawk_ooch_t idxbuf[HAWK_IDX_BUF_SIZE];
hawk_arr_t* arr;
hawk_ooi_t idx;
@ -3984,7 +3969,7 @@ static hawk_val_t* do_assignment_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* var,
hawk_map_t* map;
hawk_ooch_t* str = HAWK_NULL;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
hawk_ooch_t idxbuf[HAWK_IDX_BUF_SIZE];
hawk_ooi_t idx;
hawk_arr_t* arr;
@ -4415,7 +4400,7 @@ static hawk_val_t* eval_binop_in (hawk_rtx_t* rtx, hawk_nde_t* left, hawk_nde_t*
hawk_val_type_t ropvtype;
hawk_ooch_t* str;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
hawk_ooch_t idxbuf[HAWK_IDX_BUF_SIZE];
hawk_nde_t* remidx;
hawk_int_t idxint;
@ -6764,7 +6749,7 @@ static hawk_val_t** get_reference_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* var)
hawk_map_t* map;
hawk_ooch_t* str = HAWK_NULL;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
hawk_ooch_t idxbuf[HAWK_IDX_BUF_SIZE];
hawk_arr_t* arr;
hawk_ooi_t idx;
@ -7000,7 +6985,7 @@ static hawk_val_t* eval_indexed (hawk_rtx_t* rtx, hawk_nde_var_t* var)
hawk_map_t* map; /* containing map */
hawk_ooch_t* str = HAWK_NULL;
hawk_oow_t len;
hawk_ooch_t idxbuf[IDXBUFSIZE];
hawk_ooch_t idxbuf[HAWK_IDX_BUF_SIZE];
hawk_arr_t* arr; /* containing array */
hawk_ooi_t idx;

View File

@ -95,10 +95,8 @@ function main()
call_by_ref_3(a[0][40]);
ensure (a[0][40], "hello world", SCRIPTNAME);
ensure (length(a[0]), 2, SCRIPTNAME);
ensure (hawk::array_size(a[0]), 41, SCRIPTNAME);
delete a[0][40];
ensure (length(a[0]), 1, SCRIPTNAME);
ensure (hawk::array_size(a[0]), 41, SCRIPTNAME);
ensure (a[0][0], "farewell", SCRIPTNAME);
ensure (a[0][40], nil, SCRIPTNAME);
#hawk::splice (a[0], 40, 1, SCRIPTNAME);
@ -111,31 +109,24 @@ function main()
a = hawk::array();
ensure (length(a), 0, SCRIPTNAME);
ensure (hawk::array_size(a), 0, SCRIPTNAME);
for (i = 0; i < 10; i++) a[i*i]=i;
ensure (length(a), 10, SCRIPTNAME);
ensure (hawk::array_size(a), 82, SCRIPTNAME);
delete a[0];
ensure (length(a), 9, SCRIPTNAME);
ensure (hawk::array_size(a), 82, SCRIPTNAME);
delete a[81];
ensure (length(a), 8, SCRIPTNAME);
ensure (hawk::array_size(a), 82, SCRIPTNAME);
delete a[36];
ensure (length(a), 7, SCRIPTNAME);
ensure (hawk::array_size(a), 82, SCRIPTNAME);
for (i = 0; i < 10; i++) delete a[i*i];
ensure (length(a), 0, SCRIPTNAME);
ensure (hawk::array_size(a), 82, SCRIPTNAME);
delete a;
ensure (length(a), 0, SCRIPTNAME);
ensure (hawk::array_size(a), 0, SCRIPTNAME);
}
{