diff --git a/moo/kernel/System.moo b/moo/kernel/System.moo index 643baa0..612fbff 100644 --- a/moo/kernel/System.moo +++ b/moo/kernel/System.moo @@ -72,7 +72,7 @@ class System(Apex) if (gc) { System logNl: 'Exiting the GC finalization process ' & (thisProcess id) asString. - break + break. }. System logNl: 'Forcing garbage collection before termination in ' & (thisProcess id) asString. @@ -342,5 +342,8 @@ class SmallPointer(Object) method(#primitive) putUint32 (offset, value). method(#primitive) putUint64 (offset, value). + method(#primitive) getBytes (offset, byte_array, offset_in_buffer, len_in_buffer). + method(#primitive) putBytes (offset, byte_array, offset_in_buffer, len_in_buffer). + method(#primitive) free. } diff --git a/moo/lib/exec.c b/moo/lib/exec.c index c481ab9..22b7d64 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -3254,6 +3254,7 @@ static pf_t pftab[] = { "SmallPointer_asString", { pf_smptr_as_string, 0, 0 } }, { "SmallPointer_free", { moo_pf_smptr_free, 0, 0 } }, + { "SmallPointer_getBytes", { moo_pf_smptr_get_bytes, 4, 4 } }, { "SmallPointer_getInt16", { moo_pf_smptr_get_int16, 1, 1 } }, { "SmallPointer_getInt32", { moo_pf_smptr_get_int32, 1, 1 } }, { "SmallPointer_getInt64", { moo_pf_smptr_get_int64, 1, 1 } }, @@ -3262,6 +3263,7 @@ static pf_t pftab[] = { "SmallPointer_getUint32", { moo_pf_smptr_get_uint32, 1, 1 } }, { "SmallPointer_getUint64", { moo_pf_smptr_get_uint64, 1, 1 } }, { "SmallPointer_getUint8", { moo_pf_smptr_get_uint8, 1, 1 } }, + { "SmallPointer_putBytes", { moo_pf_smptr_put_bytes, 4, 4 } }, { "SmallPointer_putInt8", { moo_pf_smptr_put_int8, 2, 2 } }, { "SmallPointer_putInt16", { moo_pf_smptr_put_int16, 2, 2 } }, { "SmallPointer_putInt32", { moo_pf_smptr_put_int32, 2, 2 } }, @@ -3275,6 +3277,7 @@ static pf_t pftab[] = { "System_calloc", { moo_pf_system_calloc, 1, 1 } }, { "System_calloc:", { moo_pf_system_calloc, 1, 1 } }, + { "System_collectGarbage", { pf_system_collect_garbage, 0, 0 } }, { "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 } }, @@ -3286,6 +3289,7 @@ static pf_t pftab[] = { "System_getUint32", { moo_pf_system_get_uint32, 2, 2 } }, { "System_getUint64", { moo_pf_system_get_uint64, 2, 2 } }, { "System_getUint8", { moo_pf_system_get_uint8, 2, 2 } }, + { "System_log", { pf_system_log, 2, MA } }, { "System_malloc", { moo_pf_system_malloc, 1, 1 } }, { "System_malloc:", { moo_pf_system_malloc, 1, 1 } }, { "System_popCollectable", { pf_system_pop_collectable, 0, 0 } }, @@ -3298,18 +3302,14 @@ static pf_t pftab[] = { "System_putUint16", { moo_pf_system_put_uint16, 3, 3 } }, { "System_putUint32", { moo_pf_system_put_uint32, 3, 3 } }, { "System_putUint64", { moo_pf_system_put_uint64, 3, 3 } }, - + { "System_return:to:", { pf_system_return_value_to_context, 2, 2 } }, { "System_signal:afterSecs:", { pf_system_add_timed_semaphore, 2, 2 } }, { "System_signal:afterSecs:nanosecs:", { pf_system_add_timed_semaphore, 3, 3 } }, { "System_signal:onInput:", { pf_system_add_input_semaphore, 2, 2 } }, { "System_signal:onInOutput:", { pf_system_add_inoutput_semaphore, 2, 2 } }, { "System_signal:onOutput:", { pf_system_add_output_semaphore, 2, 2 } }, { "System_signalOnGCFin:", { pf_system_add_gcfin_semaphore, 1, 1 } }, - { "System_unsignal:", { pf_system_remove_semaphore, 1, 1 } }, - - { "System_collectGarbage", { pf_system_collect_garbage, 0, 0 } }, - { "System_log", { pf_system_log, 2, MA } }, - { "System_return:to:", { pf_system_return_value_to_context, 2, 2 } } + { "System_unsignal:", { pf_system_remove_semaphore, 1, 1 } } }; moo_pfbase_t* moo_getpfnum (moo_t* moo, const moo_ooch_t* ptr, moo_oow_t len, moo_ooi_t* pfnum) diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index f74b1f7..bbb9c3b 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -1331,8 +1331,6 @@ 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_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_int16 (moo_t* moo, moo_ooi_t nargs); @@ -1354,6 +1352,9 @@ moo_pfrc_t moo_pf_system_put_uint16 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_system_put_uint32 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_system_put_uint64 (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_smptr_get_int8 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_smptr_get_int16 (moo_t* moo, moo_ooi_t nargs); @@ -1375,6 +1376,9 @@ moo_pfrc_t moo_pf_smptr_put_uint16 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_smptr_put_uint32 (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_smptr_put_uint64 (moo_t* moo, moo_ooi_t nargs); +moo_pfrc_t moo_pf_smptr_get_bytes (moo_t* moo, moo_ooi_t nargs); +moo_pfrc_t moo_pf_smptr_put_bytes (moo_t* moo, moo_ooi_t nargs); + /* TODO: remove the following debugging functions */ /* ========================================================================= */ /* debug.c */ diff --git a/moo/lib/pf-sys.c b/moo/lib/pf-sys.c index 5cbe219..a372530 100644 --- a/moo/lib/pf-sys.c +++ b/moo/lib/pf-sys.c @@ -853,3 +853,117 @@ moo_pfrc_t moo_pf_smptr_put_uint64 (moo_t* moo, moo_ooi_t nargs) /* ------------------------------------------------------------------------------------- */ +moo_pfrc_t moo_pf_smptr_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 rcv, tmp; + + MOO_ASSERT (moo, nargs == 4); + + rcv = MOO_STACK_GETRCV(moo, nargs); + MOO_PF_CHECK_RCV (moo, MOO_OOP_IS_SMPTR(rcv)); + rawptr = MOO_OOP_TO_SMPTR(rcv); + + tmp = MOO_STACK_GETARG(moo, nargs, 0); + 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, 2); + 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, 3); + 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, 1); + 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_smptr_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 rcv, tmp; + + MOO_ASSERT (moo, nargs == 4); + + rcv = MOO_STACK_GETRCV(moo, nargs); + MOO_PF_CHECK_RCV (moo, MOO_OOP_IS_SMPTR(rcv)); + rawptr = MOO_OOP_TO_SMPTR(rcv); + + tmp = MOO_STACK_GETARG(moo, nargs, 0); + 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, 2); + 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, 3); + 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, 1); + 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; +}