added System<<getBytes, System<<putBytes.

also added primitive functions for the above
This commit is contained in:
hyunghwan.chung 2017-12-09 15:47:43 +00:00
parent 471d8ad797
commit 541ab59028
5 changed files with 232 additions and 85 deletions

View File

@ -314,6 +314,9 @@ TODO: how to pass all variadic arguments to another variadic methods???
method(#class,#primitive) putUint16 (rawptr, offset, value). method(#class,#primitive) putUint16 (rawptr, offset, value).
method(#class,#primitive) putUint32 (rawptr, offset, value). method(#class,#primitive) putUint32 (rawptr, offset, value).
method(#class,#primitive) putUint64 (rawptr, offset, value). method(#class,#primitive) putUint64 (rawptr, offset, value).
method(#class,#primitive) getBytes (rawptr, offset, byte_array, offset_in_buffer, len_in_buffer).
method(#class,#primitive) putBytes (rawptr, offset, byte_array, offset_in_buffer, len_in_buffer).
} }

View File

@ -3277,6 +3277,7 @@ static pf_t pftab[] =
{ "System_calloc:", { moo_pf_system_calloc, 1, 1 } }, { "System_calloc:", { moo_pf_system_calloc, 1, 1 } },
{ "System_free", { moo_pf_system_free, 1, 1 } }, { "System_free", { moo_pf_system_free, 1, 1 } },
{ "System_free:", { moo_pf_system_free, 1, 1 } }, { "System_free:", { moo_pf_system_free, 1, 1 } },
{ "System_getBytes", { moo_pf_system_get_bytes, 5, 5 } },
{ "System_getInt16", { moo_pf_system_get_int16, 2, 2 } }, { "System_getInt16", { moo_pf_system_get_int16, 2, 2 } },
{ "System_getInt32", { moo_pf_system_get_int32, 2, 2 } }, { "System_getInt32", { moo_pf_system_get_int32, 2, 2 } },
{ "System_getInt64", { moo_pf_system_get_int64, 2, 2 } }, { "System_getInt64", { moo_pf_system_get_int64, 2, 2 } },
@ -3288,6 +3289,7 @@ static pf_t pftab[] =
{ "System_malloc", { moo_pf_system_malloc, 1, 1 } }, { "System_malloc", { moo_pf_system_malloc, 1, 1 } },
{ "System_malloc:", { moo_pf_system_malloc, 1, 1 } }, { "System_malloc:", { moo_pf_system_malloc, 1, 1 } },
{ "System_popCollectable", { pf_system_pop_collectable, 0, 0 } }, { "System_popCollectable", { pf_system_pop_collectable, 0, 0 } },
{ "System_putBytes", { moo_pf_system_put_bytes, 5, 5 } },
{ "System_putInt8", { moo_pf_system_put_int8, 3, 3 } }, { "System_putInt8", { moo_pf_system_put_int8, 3, 3 } },
{ "System_putInt16", { moo_pf_system_put_int16, 3, 3 } }, { "System_putInt16", { moo_pf_system_put_int16, 3, 3 } },
{ "System_putInt32", { moo_pf_system_put_int32, 3, 3 } }, { "System_putInt32", { moo_pf_system_put_int32, 3, 3 } },

View File

@ -1331,6 +1331,9 @@ moo_pfrc_t moo_pf_system_calloc (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_system_free (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_system_free (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_smptr_free (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_smptr_free (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_system_get_bytes (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_system_put_bytes (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_system_get_int8 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_system_get_int8 (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_system_get_int16 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_system_get_int16 (moo_t* moo, moo_ooi_t nargs);
moo_pfrc_t moo_pf_system_get_int32 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_system_get_int32 (moo_t* moo, moo_ooi_t nargs);

View File

@ -470,7 +470,7 @@ moo_pfrc_t moo_pf_responds_to (moo_t* moo, moo_ooi_t nargs)
selector = MOO_STACK_GETARG(moo, nargs, 0); selector = MOO_STACK_GETARG(moo, nargs, 0);
/*if (MOO_CLASSOF(moo,selector) != moo->_symbol)*/ /*if (MOO_CLASSOF(moo,selector) != moo->_symbol)*/
if (!MOO_OOP_IS_POINTER(selector) || !MOO_OBJ_IS_CHAR_POINTER(selector)) if (!MOO_OBJ_IS_CHAR_POINTER(selector))
{ {
moo_seterrbfmt (moo, MOO_EINVAL, "invalid argument - %O", selector); moo_seterrbfmt (moo, MOO_EINVAL, "invalid argument - %O", selector);
return MOO_PF_FAILURE; return MOO_PF_FAILURE;

View File

@ -35,9 +35,9 @@ static MOO_INLINE moo_pfrc_t _system_alloc (moo_t* moo, moo_ooi_t nargs, int cle
MOO_ASSERT (moo, nargs == 1); MOO_ASSERT (moo, nargs == 1);
tmp = MOO_STACK_GETARG(moo, nargs, 0); tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (!MOO_OOP_IS_SMOOI(tmp)) if (!MOO_OOP_IS_SMOOI(tmp) || MOO_OOP_TO_SMOOI(tmp) < 0)
{ {
moo_seterrbfmt (moo, MOO_EINVAL, "invalid size - %O", tmp); moo_seterrbfmt (moo, MOO_EINVAL, "invalid size %O for raw memory allocation", tmp);
return MOO_PF_FAILURE; return MOO_PF_FAILURE;
} }
@ -47,7 +47,7 @@ static MOO_INLINE moo_pfrc_t _system_alloc (moo_t* moo, moo_ooi_t nargs, int cle
if (!MOO_IN_SMPTR_RANGE(ptr)) if (!MOO_IN_SMPTR_RANGE(ptr))
{ {
moo_seterrbfmt (moo, MOO_ERANGE, "%p not in smptr range", ptr); moo_seterrbfmt (moo, MOO_ERANGE, "%p not in smptr range for raw memory allocation", ptr);
moo_freemem (moo, ptr); moo_freemem (moo, ptr);
return MOO_PF_FAILURE; return MOO_PF_FAILURE;
} }
@ -159,7 +159,7 @@ static MOO_INLINE moo_oop_t _fetch_raw_int (moo_t* moo, moo_int8_t* rawptr, moo_
#endif #endif
default: default:
moo_seterrnum (moo, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw signed memory fetch", size);
return MOO_NULL; return MOO_NULL;
} }
@ -198,18 +198,24 @@ static MOO_INLINE moo_oop_t _fetch_raw_uint (moo_t* moo, moo_uint8_t* rawptr, mo
#endif #endif
default: default:
moo_seterrnum (moo, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw unsigned memory fetch", size);
return MOO_NULL; return MOO_NULL;
} }
return moo_oowtoint (moo, v); return moo_oowtoint (moo, v);
} }
static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t offset, int size, moo_oop_t voop) static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t offset, int size, moo_oop_t voop)
{ {
moo_ooi_t w, max, min; moo_ooi_t w, max, min;
if (moo_inttoooi (moo, voop, &w) == 0) return -1; if (moo_inttoooi(moo, voop, &w) == 0)
{
moo_seterrbfmt (moo, moo_geterrnum(moo), "invalid value %O for raw signed memory store", voop);
return -1;
}
/* assume 2's complement */ /* assume 2's complement */
max = (moo_ooi_t)(~(moo_oow_t)0 >> ((MOO_SIZEOF_OOW_T - size) * 8 + 1)); max = (moo_ooi_t)(~(moo_oow_t)0 >> ((MOO_SIZEOF_OOW_T - size) * 8 + 1));
@ -217,7 +223,7 @@ static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t
if (w > max || w < min) if (w > max || w < min)
{ {
moo_seterrnum (moo, MOO_ERANGE); moo_seterrbfmt (moo, MOO_ERANGE, "value %jd out of supported range for raw signed memory store", w);
return -1; return -1;
} }
@ -248,7 +254,7 @@ static MOO_INLINE int _store_raw_int (moo_t* moo, moo_uint8_t* rawptr, moo_oow_t
#endif #endif
} }
moo_seterrnum (moo, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw signed memory store", size);
return -1; return -1;
} }
@ -259,14 +265,21 @@ static MOO_INLINE int _store_raw_uint (moo_t* moo, moo_uint8_t* rawptr, moo_oow_
if ((n = moo_inttooow (moo, voop, &w)) <= 0) if ((n = moo_inttooow (moo, voop, &w)) <= 0)
{ {
if (n <= -1) moo_seterrnum (moo, MOO_ERANGE); /* negative number */ if (n <= -1)
{
moo_seterrbfmt (moo, MOO_ERANGE, "negative value %O for raw unsigned memory store", voop);
}
else
{
moo_seterrbfmt (moo, moo_geterrnum(moo), "invalid value %O for raw unsigned memory store", voop);
}
return -1; return -1;
} }
max = (~(moo_oow_t)0 >> ((MOO_SIZEOF_OOW_T - size) * 8)); max = (~(moo_oow_t)0 >> ((MOO_SIZEOF_OOW_T - size) * 8));
if (w > max) if (w > max)
{ {
moo_seterrnum (moo, MOO_ERANGE); moo_seterrbfmt (moo, MOO_ERANGE, "value %ju out of supported range for raw unsigned memory store", w);
return -1; return -1;
} }
@ -297,10 +310,11 @@ static MOO_INLINE int _store_raw_uint (moo_t* moo, moo_uint8_t* rawptr, moo_oow_
#endif #endif
} }
moo_seterrnum (moo, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "unsupported size %d for raw unsigned memory store", size);
return -1; return -1;
} }
/* ------------------------------------------------------------------------------------- */
static moo_pfrc_t _get_system_int (moo_t* moo, moo_ooi_t nargs, int size) static moo_pfrc_t _get_system_int (moo_t* moo, moo_ooi_t nargs, int size)
{ {
@ -311,23 +325,24 @@ static moo_pfrc_t _get_system_int (moo_t* moo, moo_ooi_t nargs, int size)
/* MOO_PF_CHECK_RCV (moo, MOO_STACK_GETRCV(moo, nargs) == (moo_oop_t)moo->_system); */ /* MOO_PF_CHECK_RCV (moo, MOO_STACK_GETRCV(moo, nargs) == (moo_oop_t)moo->_system); */
MOO_ASSERT (moo, nargs == 2); MOO_ASSERT (moo, nargs == 2);
tmp = MOO_STACK_GETARG(moo, nargs, 0); tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp); if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp);
else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0) goto einval; else if (moo_inttooow(moo, tmp, (moo_oow_t*)&rawptr) <= 0)
if (moo_inttooow (moo, MOO_STACK_GETARG(moo, nargs, 1), &offset) <= 0)
{ {
einval: moo_seterrbfmt (moo, MOO_EINVAL, "invalid pointer %O for raw signed memory fetch", tmp);
MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL); return MOO_PF_FAILURE;
return MOO_PF_SUCCESS; }
tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw signed memory fetch", tmp);
return MOO_PF_FAILURE;
} }
tmp = _fetch_raw_int(moo, rawptr, offset, size); tmp = _fetch_raw_int(moo, rawptr, offset, size);
if (!tmp) if (!tmp) return MOO_PF_FAILURE;
{
MOO_STACK_SETRETTOERRNUM (moo, nargs);
return MOO_PF_SUCCESS;
}
MOO_STACK_SETRET (moo, nargs, tmp); MOO_STACK_SETRET (moo, nargs, tmp);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
@ -344,28 +359,27 @@ static moo_pfrc_t _get_system_uint (moo_t* moo, moo_ooi_t nargs, int size)
MOO_ASSERT (moo, nargs == 2); MOO_ASSERT (moo, nargs == 2);
tmp = MOO_STACK_GETARG(moo, nargs, 0); tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp); if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp);
else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0) goto einval; else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0)
if (moo_inttooow (moo, MOO_STACK_GETARG(moo, nargs, 1), &offset) <= 0)
{ {
einval: moo_seterrbfmt (moo, MOO_EINVAL, "invalid pointer %O for raw unsigned memory fetch", tmp);
MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL); return MOO_PF_FAILURE;
return MOO_PF_SUCCESS; }
tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow (moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw unsigned memory fetch", tmp);
return MOO_PF_FAILURE;
} }
tmp = _fetch_raw_uint (moo, rawptr, offset, size); tmp = _fetch_raw_uint (moo, rawptr, offset, size);
if (!tmp) if (!tmp) return MOO_PF_FAILURE;
{
MOO_STACK_SETRETTOERRNUM (moo, nargs);
return MOO_PF_SUCCESS;
}
MOO_STACK_SETRET (moo, nargs, tmp); MOO_STACK_SETRET (moo, nargs, tmp);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
} }
moo_pfrc_t moo_pf_system_get_int8 (moo_t* moo, moo_ooi_t nargs) moo_pfrc_t moo_pf_system_get_int8 (moo_t* moo, moo_ooi_t nargs)
{ {
return _get_system_int (moo, nargs, 1); return _get_system_int (moo, nargs, 1);
@ -417,22 +431,26 @@ static moo_pfrc_t _put_system_int (moo_t* moo, moo_ooi_t nargs, int size)
MOO_ASSERT (moo, nargs == 3); MOO_ASSERT (moo, nargs == 3);
tmp = MOO_STACK_GETARG(moo, nargs, 0); tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp); if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp);
else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0) goto einval; else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid pointer %O for raw signed memory store", tmp);
return MOO_PF_FAILURE;
}
if (moo_inttooow(moo, MOO_STACK_GETARG(moo, nargs, 1), &offset) <= 0) goto einval; tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw signed memory store", tmp);
return MOO_PF_FAILURE;
}
if (_store_raw_int (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 2)) <= -1) if (_store_raw_int (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 2)) <= -1)
{ {
MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_FAILURE;
return MOO_PF_SUCCESS;
} }
MOO_STACK_SETRETTORCV (moo, nargs); MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
einval:
MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL);
return MOO_PF_SUCCESS;
} }
static moo_pfrc_t _put_system_uint (moo_t* moo, moo_ooi_t nargs, int size) static moo_pfrc_t _put_system_uint (moo_t* moo, moo_ooi_t nargs, int size)
@ -446,22 +464,26 @@ static moo_pfrc_t _put_system_uint (moo_t* moo, moo_ooi_t nargs, int size)
MOO_ASSERT (moo, nargs == 3); MOO_ASSERT (moo, nargs == 3);
tmp = MOO_STACK_GETARG(moo, nargs, 0); tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp); if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp);
else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0) goto einval; else if (moo_inttooow (moo, tmp, (moo_oow_t*)&rawptr) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid pointer %O for raw unsigned memory store", tmp);
return MOO_PF_FAILURE;
}
if (moo_inttooow(moo, MOO_STACK_GETARG(moo, nargs, 1), &offset) <= 0) goto einval; tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw unsigned memory store", tmp);
return MOO_PF_FAILURE;
}
if (_store_raw_uint (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 2)) <= -1) if (_store_raw_uint (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 2)) <= -1)
{ {
MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_FAILURE;
return MOO_PF_SUCCESS;
} }
MOO_STACK_SETRETTORCV (moo, nargs); MOO_STACK_SETRETTORCV (moo, nargs);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
einval:
MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL);
return MOO_PF_SUCCESS;
} }
moo_pfrc_t moo_pf_system_put_int8 (moo_t* moo, moo_ooi_t nargs) moo_pfrc_t moo_pf_system_put_int8 (moo_t* moo, moo_ooi_t nargs)
@ -504,16 +526,139 @@ moo_pfrc_t moo_pf_system_put_uint64 (moo_t* moo, moo_ooi_t nargs)
return _put_system_uint (moo, nargs, 8); return _put_system_uint (moo, nargs, 8);
} }
/* ------------------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------------------- */
moo_pfrc_t moo_pf_system_get_bytes (moo_t* moo, moo_ooi_t nargs)
{
moo_uint8_t* rawptr;
moo_oow_t offset, offset_in_buffer, len_in_buffer;
moo_oop_t tmp;
/* MOO_PF_CHECK_RCV (moo, MOO_STACK_GETRCV(moo, nargs) == (moo_oop_t)moo->_system); */
MOO_ASSERT (moo, nargs == 5);
tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp);
else if (moo_inttooow(moo, tmp, (moo_oow_t*)&rawptr) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid pointer %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 3);
if (moo_inttooow(moo, tmp, &offset_in_buffer) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid buffer offset %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 4);
if (moo_inttooow(moo, tmp, &len_in_buffer) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid buffer length %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 2);
if (!MOO_OBJ_IS_BYTE_POINTER(tmp))
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid buffer %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
if (offset_in_buffer < MOO_OBJ_GET_SIZE(tmp))
{
moo_oow_t max_len = MOO_OBJ_GET_SIZE(tmp) - offset_in_buffer;
if (len_in_buffer > max_len) len_in_buffer = max_len;
if (len_in_buffer > MOO_SMOOI_MAX) len_in_buffer = MOO_SMOOI_MAX;
}
else len_in_buffer = 0;
if (len_in_buffer > 0)
{
MOO_MEMCPY (&((moo_oop_byte_t)tmp)->slot[offset_in_buffer], &rawptr[offset], len_in_buffer);
}
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(len_in_buffer));
return MOO_PF_SUCCESS;
}
moo_pfrc_t moo_pf_system_put_bytes (moo_t* moo, moo_ooi_t nargs)
{
moo_uint8_t* rawptr;
moo_oow_t offset, offset_in_buffer, len_in_buffer;
moo_oop_t tmp;
/* MOO_PF_CHECK_RCV (moo, MOO_STACK_GETRCV(moo, nargs) == (moo_oop_t)moo->_system); */
MOO_ASSERT (moo, nargs == 5);
tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (MOO_OOP_IS_SMPTR(tmp)) rawptr = MOO_OOP_TO_SMPTR(tmp);
else if (moo_inttooow(moo, tmp, (moo_oow_t*)&rawptr) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid pointer %O for raw memory store", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 1);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw memory store", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 3);
if (moo_inttooow(moo, tmp, &offset_in_buffer) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid buffer offset %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 4);
if (moo_inttooow(moo, tmp, &len_in_buffer) <= 0)
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid buffer length %O for raw memory fetch", tmp);
return MOO_PF_FAILURE;
}
tmp = MOO_STACK_GETARG(moo, nargs, 2);
if (!MOO_OBJ_IS_BYTE_POINTER(tmp))
{
moo_seterrbfmt (moo, MOO_EINVAL, "invalid buffer %O for raw memory store", tmp);
return MOO_PF_FAILURE;
}
if (offset_in_buffer < MOO_OBJ_GET_SIZE(tmp))
{
moo_oow_t max_len = MOO_OBJ_GET_SIZE(tmp) - offset_in_buffer;
if (len_in_buffer > max_len) len_in_buffer = max_len;
if (len_in_buffer > MOO_SMOOI_MAX) len_in_buffer = MOO_SMOOI_MAX;
}
else len_in_buffer = 0;
if (len_in_buffer > 0)
{
MOO_MEMCPY (&rawptr[offset], &((moo_oop_byte_t)tmp)->slot[offset_in_buffer], len_in_buffer);
}
MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(len_in_buffer));
return MOO_PF_SUCCESS;
}
/* ------------------------------------------------------------------------------------- */
static moo_pfrc_t _get_smptr_int (moo_t* moo, moo_ooi_t nargs, int size) static moo_pfrc_t _get_smptr_int (moo_t* moo, moo_ooi_t nargs, int size)
{ {
moo_oop_t rcv; moo_oop_t rcv, tmp;
moo_int8_t* rawptr; moo_int8_t* rawptr;
moo_oow_t offset; moo_oow_t offset;
moo_oop_t result; moo_oop_t result;
@ -523,20 +668,17 @@ static moo_pfrc_t _get_smptr_int (moo_t* moo, moo_ooi_t nargs, int size)
rcv = MOO_STACK_GETRCV(moo, nargs); rcv = MOO_STACK_GETRCV(moo, nargs);
MOO_PF_CHECK_RCV (moo, MOO_OOP_IS_SMPTR(rcv)); MOO_PF_CHECK_RCV (moo, MOO_OOP_IS_SMPTR(rcv));
if (moo_inttooow (moo, MOO_STACK_GETARG(moo, nargs, 0), &offset) <= 0) tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (moo_inttooow (moo, tmp, &offset) <= 0)
{ {
MOO_STACK_SETRETTOERROR(moo, nargs, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw signed memory fetch", tmp);
return MOO_PF_SUCCESS; return MOO_PF_FAILURE;
} }
rawptr = MOO_OOP_TO_SMPTR(rcv); rawptr = MOO_OOP_TO_SMPTR(rcv);
result = _fetch_raw_int (moo, rawptr, offset, size); result = _fetch_raw_int (moo, rawptr, offset, size);
if (!result) if (!result) return MOO_PF_FAILURE;
{
MOO_STACK_SETRETTOERRNUM (moo, nargs);
return MOO_PF_SUCCESS;
}
MOO_STACK_SETRET (moo, nargs, result); MOO_STACK_SETRET (moo, nargs, result);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
@ -544,7 +686,7 @@ static moo_pfrc_t _get_smptr_int (moo_t* moo, moo_ooi_t nargs, int size)
static moo_pfrc_t _get_smptr_uint (moo_t* moo, moo_ooi_t nargs, int size) static moo_pfrc_t _get_smptr_uint (moo_t* moo, moo_ooi_t nargs, int size)
{ {
moo_oop_t rcv; moo_oop_t rcv, tmp;
moo_uint8_t* rawptr; moo_uint8_t* rawptr;
moo_oow_t offset; moo_oow_t offset;
moo_oop_t result; moo_oop_t result;
@ -554,20 +696,17 @@ static moo_pfrc_t _get_smptr_uint (moo_t* moo, moo_ooi_t nargs, int size)
rcv = MOO_STACK_GETRCV(moo, nargs); rcv = MOO_STACK_GETRCV(moo, nargs);
MOO_PF_CHECK_RCV (moo, MOO_OOP_IS_SMPTR(rcv)); MOO_PF_CHECK_RCV (moo, MOO_OOP_IS_SMPTR(rcv));
if (moo_inttooow (moo, MOO_STACK_GETARG(moo, nargs, 0), &offset) <= 0) tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (moo_inttooow (moo, tmp, &offset) <= 0)
{ {
MOO_STACK_SETRETTOERROR(moo, nargs, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw unsigned memory fetch", tmp);
return MOO_PF_SUCCESS; return MOO_PF_FAILURE;
} }
rawptr = MOO_OOP_TO_SMPTR(rcv); rawptr = MOO_OOP_TO_SMPTR(rcv);
result = _fetch_raw_uint (moo, rawptr, offset, size); result = _fetch_raw_uint (moo, rawptr, offset, size);
if (!result) if (!result) return MOO_PF_FAILURE;
{
MOO_STACK_SETRETTOERRNUM(moo, nargs);
return MOO_PF_SUCCESS;
}
MOO_STACK_SETRET (moo, nargs, result); MOO_STACK_SETRET (moo, nargs, result);
return MOO_PF_SUCCESS; return MOO_PF_SUCCESS;
@ -626,16 +765,16 @@ static moo_pfrc_t _put_smptr_int (moo_t* moo, moo_ooi_t nargs, int size)
rawptr = MOO_OOP_TO_SMPTR(tmp); rawptr = MOO_OOP_TO_SMPTR(tmp);
if (moo_inttooow(moo, MOO_STACK_GETARG(moo, nargs, 0), &offset) <= 0) tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{ {
MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw signed memory store", tmp);
return MOO_PF_SUCCESS; return MOO_PF_FAILURE;
} }
if (_store_raw_int (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 1)) <= -1) if (_store_raw_int (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 1)) <= -1)
{ {
MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_FAILURE;
return MOO_PF_SUCCESS;
} }
MOO_STACK_SETRETTORCV (moo, nargs); MOO_STACK_SETRETTORCV (moo, nargs);
@ -655,16 +794,16 @@ static moo_pfrc_t _put_smptr_uint (moo_t* moo, moo_ooi_t nargs, int size)
rawptr = MOO_OOP_TO_SMPTR(tmp); rawptr = MOO_OOP_TO_SMPTR(tmp);
if (moo_inttooow(moo, MOO_STACK_GETARG(moo, nargs, 0), &offset) <= 0) tmp = MOO_STACK_GETARG(moo, nargs, 0);
if (moo_inttooow(moo, tmp, &offset) <= 0)
{ {
MOO_STACK_SETRETTOERROR (moo, nargs, MOO_EINVAL); moo_seterrbfmt (moo, MOO_EINVAL, "invalid offset %O for raw unsigned memory store", tmp);
return MOO_PF_SUCCESS; return MOO_PF_FAILURE;
} }
if (_store_raw_uint (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 1)) <= -1) if (_store_raw_uint (moo, rawptr, offset, size, MOO_STACK_GETARG(moo, nargs, 1)) <= -1)
{ {
MOO_STACK_SETRETTOERRNUM (moo, nargs); return MOO_PF_FAILURE;
return MOO_PF_SUCCESS;
} }
MOO_STACK_SETRETTORCV (moo, nargs); MOO_STACK_SETRETTORCV (moo, nargs);