adding Hawk::setArrayVal()

This commit is contained in:
hyung-hwan 2025-07-05 13:36:13 +09:00
parent 5a860ec805
commit 9a87e03b5e
2 changed files with 155 additions and 86 deletions

View File

@ -850,6 +850,73 @@ int Hawk::Value::setMbs (Run* r, const hawk_bch_t* str)
return n;
}
int Hawk::Value::setArrayVal (hawk_ooi_t idx, hawk_val_t* v)
{
if (HAWK_UNLIKELY(!this->run)) return -1;
return this->setArrayVal(this->run, idx, v);
}
int Hawk::Value::setArrayVal (Run* r, hawk_ooi_t idx, hawk_val_t* v)
{
HAWK_ASSERT (r != HAWK_NULL);
if (HAWK_RTX_GETVALTYPE(r->rtx, this->val) != HAWK_VAL_ARR)
{
// the previous value is not a arr.
// a new arr value needs to be created first.
hawk_val_t* arr = hawk_rtx_makearrval(r->rtx, -1); // TODO: can we change this to accept the initial value in the constructor
if (arr == HAWK_NULL)
{
r->hawk->retrieveError (r);
return -1;
}
hawk_rtx_refupval(r->rtx, arr);
// update the arr with a given value
if (hawk_rtx_setarrvalfld(r->rtx, arr, idx, v) == HAWK_NULL)
{
hawk_rtx_refdownval(r->rtx, arr);
r->hawk->retrieveError (r);
return -1;
}
// free the previous value
if (this->run)
{
// if val is not nil, this->run can't be NULL
hawk_rtx_refdownval(this->run->rtx, this->val);
}
this->run = r;
this->val = arr;
}
else
{
HAWK_ASSERT (run != HAWK_NULL);
// if the previous value is a arr, things are a bit simpler
// however it needs to check if the runtime context matches
// with the previous one.
if (this->run != r)
{
// it can't span across multiple runtime contexts
this->run->setError(HAWK_EINVAL);
this->run->hawk->retrieveError (run);
return -1;
}
// update the arr with a given value
if (hawk_rtx_setarrvalfld(r->rtx, val, idx, v) == HAWK_NULL)
{
r->hawk->retrieveError (r);
return -1;
}
}
return 0;
}
int Hawk::Value::setIndexedVal (const Index& idx, hawk_val_t* v)
{
if (HAWK_UNLIKELY(!this->run)) return -1;
@ -2174,8 +2241,7 @@ void Hawk::xstrs_t::clear (hawk_t* hawk)
{
if (this->ptr != HAWK_NULL)
{
while (this->len > 0)
hawk_freemem(hawk, this->ptr[--this->len].ptr);
while (this->len > 0) hawk_freemem(hawk, this->ptr[--this->len].ptr);
hawk_freemem(hawk, this->ptr);
this->ptr = HAWK_NULL;

View File

@ -1076,6 +1076,9 @@ public:
int setMbs (const hawk_bch_t* str);
int setMbs (Run* r, const hawk_bch_t* str);
int setArrayVal (hawk_ooi_t idx, hawk_val_t* v);
int setArrayVal (Run* r, hawk_ooi_t idx, hawk_val_t* v);
int setIndexedVal (const Index& idx, hawk_val_t* v);
int setIndexedVal (Run* r, const Index& idx, hawk_val_t* v);
int setIndexedInt (const Index& idx, hawk_int_t v);