diff --git a/moo/kernel/Apex.moo b/moo/kernel/Apex.moo index aef1bc3..93b30c2 100644 --- a/moo/kernel/Apex.moo +++ b/moo/kernel/Apex.moo @@ -100,9 +100,9 @@ extend Apex method(#dual,#primitive) basicAt: index. method(#dual,#primitive) basicAt: index put: value. - - ##method(#dual,#primitive) basicMoveFrom: sindex length: length to: dindex. - ##method(#dual,#primitive) basicCopyFrom: sindex length: length to: dindex. + + method(#dual,#primitive) basicFillFrom: sindex with: value count: count. + method(#dual,#primitive) basicShiftFrom: sindex to: dindex count: count. (* ------------------------------------------------------------------ * FINALIZATION SUPPORT diff --git a/moo/kernel/Socket.moo b/moo/kernel/Socket.moo index 5556cf8..fbd962b 100644 --- a/moo/kernel/Socket.moo +++ b/moo/kernel/Socket.moo @@ -183,14 +183,15 @@ class(#byte) IP6Address(IPAddress) if (colonpos >= 0) { ## double colon position - self basicMoveFrom: colonpos length: 5 to: ((self basicSize) - colonpos). + self basicShiftFrom: colonpos to: (colonpos + (self basicSize - tgpos)) count: (tgpos - colonpos). + ##tgpos := tgpos + (self basicSize - tgpos). + } + elsif (tgpos ~~ (self basicSize)) + { + ^Error.Code.EINVAL }. - -tgpos dump. -self basicSize dump. - if (tgpos ~~ (self basicSize)) { 'DDDDDDDDDDDd' dump. ^Error.Code.EINVAL }. } - + method fromString: str { if ((self __fromString: str) isError) @@ -328,7 +329,7 @@ extend Socket { self.insem := Semaphore new. self.insem signalAction: [:sem | self.inputAction value: self value: true]. - System addAsyncSemaphore: self.insem; + System addAsyncSemaphore: self.insem. }. System signal: self.insem onInput: self.handle. @@ -357,7 +358,6 @@ extend Socket } } - class MyObject(Object) { method(#class) main @@ -365,6 +365,13 @@ class MyObject(Object) | s conact inact outact | +s := ByteArray new: 100. +s basicFillFrom: 0 with: ($a asInteger) count: 100. +s basicFillFrom: 50 with: ($b asInteger) count: 50. +(s basicShiftFrom: 50 to: 94 count: 10) dump. +s dump. +##thisProcess terminate. + s := IP4Address fromString: '192.168.123.232'. s dump. s basicSize dump. diff --git a/moo/lib/exec.c b/moo/lib/exec.c index 5eb7155..aa42890 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -3343,8 +3343,10 @@ static pf_t pftab[] = { "Apex_addToBeFinalized", { pf_add_to_be_finalized, 0, 0 } }, { "Apex_basicAt:", { moo_pf_basic_at, 1, 1 } }, { "Apex_basicAt:put:", { moo_pf_basic_at_put, 2, 2 } }, + { "Apex_basicFillFrom:with:count:", { moo_pf_basic_fill, 3, 3 } }, { "Apex_basicNew", { moo_pf_basic_new, 0, 0 } }, { "Apex_basicNew:", { moo_pf_basic_new, 1, 1 } }, + { "Apex_basicShiftFrom:to:count:", { moo_pf_basic_shift, 3, 3 } }, { "Apex_basicSize", { moo_pf_basic_size, 0, 0 } }, { "Apex_class", { moo_pf_class, 0, 0 } }, { "Apex_hash", { pf_hash, 0, 0 } }, diff --git a/moo/lib/moo-prv.h b/moo/lib/moo-prv.h index 638371d..94845ec 100644 --- a/moo/lib/moo-prv.h +++ b/moo/lib/moo-prv.h @@ -1324,6 +1324,8 @@ moo_pfrc_t moo_pf_class (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_size (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_at (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_basic_at_put (moo_t* moo, moo_ooi_t nargs); +moo_pfrc_t moo_pf_basic_fill (moo_t* moo, moo_ooi_t nargs); +moo_pfrc_t moo_pf_basic_shift (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_is_kind_of (moo_t* moo, moo_ooi_t nargs); moo_pfrc_t moo_pf_responds_to (moo_t* moo, moo_ooi_t nargs); diff --git a/moo/lib/pf-basic.c b/moo/lib/pf-basic.c index 4ea260e..e75b331 100644 --- a/moo/lib/pf-basic.c +++ b/moo/lib/pf-basic.c @@ -434,9 +434,118 @@ moo_pfrc_t moo_pf_basic_at_put (moo_t* moo, moo_ooi_t nargs) return MOO_PF_SUCCESS; } -moo_pfrc_t moo_pf_basic_move (moo_t* moo, moo_ooi_t nargs) +moo_pfrc_t moo_pf_basic_fill (moo_t* moo, moo_ooi_t nargs) { - moo_oop_t rcv, spos, slen, dpos; + moo_oop_t rcv, spos, dval, slen; + moo_oow_t sidx, ssz, maxlen, end, i; + + MOO_ASSERT (moo, nargs == 3); + + rcv = MOO_STACK_GETRCV(moo, nargs); + if (!MOO_OOP_IS_POINTER(rcv)) + { + /* the receiver is a special numeric object, not a normal pointer */ + moo_seterrbfmt (moo, MOO_EMSGRCV, "receiver not indexable - %O", rcv); + return MOO_PF_FAILURE; + } + + if (MOO_OBJ_GET_FLAGS_RDONLY(rcv)) + { + moo_seterrbfmt (moo, MOO_EPERM, "now allowed to change a read-only object - %O", rcv); + return MOO_PF_FAILURE; + } + + spos = MOO_STACK_GETARG(moo, nargs, 0); + dval = MOO_STACK_GETARG(moo, nargs, 1); + slen = MOO_STACK_GETARG(moo, nargs, 2); + + if (moo_inttooow(moo, spos, &sidx) <= 0) + { + /* negative integer or not integer */ + moo_seterrbfmt (moo, MOO_EINVAL, "invalid source position - %O", spos); + return MOO_PF_FAILURE; + } + if (sidx >= MOO_OBJ_GET_SIZE(rcv)) + { + /* index out of range */ + moo_seterrbfmt (moo, MOO_ERANGE, "source position out of bound - %zu", sidx); + return MOO_PF_FAILURE; + } + + if (moo_inttooow(moo, slen, &ssz) <= 0) + { + /* negative integer or not integer */ + moo_seterrbfmt (moo, MOO_EINVAL, "invalid fill count - %O", slen); + return MOO_PF_FAILURE; + } + + maxlen = MOO_OBJ_GET_SIZE(rcv) - sidx; + if (ssz > maxlen) ssz = maxlen; + end = sidx + ssz; + + /* TODO: do i need to perform a range check? */ + switch (MOO_OBJ_GET_FLAGS_TYPE(rcv)) + { + case MOO_OBJ_TYPE_BYTE: + if (!MOO_OOP_IS_SMOOI(dval)) goto invalid_fill_value; + for (i = sidx; i < end; i++) ((moo_oop_byte_t)rcv)->slot[i] = MOO_OOP_TO_SMOOI(dval); + break; + + case MOO_OBJ_TYPE_CHAR: + if (!MOO_OOP_IS_CHAR(dval)) goto invalid_fill_value; + for (i = sidx; i < end; i++) ((moo_oop_char_t)rcv)->slot[i] = MOO_OOP_TO_CHAR(dval); + break; + + case MOO_OBJ_TYPE_HALFWORD: + if (!MOO_OOP_IS_SMOOI(dval)) goto invalid_fill_value; + for (i = sidx; i < end; i++) ((moo_oop_halfword_t)rcv)->slot[i] = MOO_OOP_TO_SMOOI(dval); + break; + + case MOO_OBJ_TYPE_WORD: + { + moo_oow_t dw; + if (moo_inttooow(moo, dval, &dw) <= 0) goto invalid_fill_value; + for (i = sidx; i < end; i++) ((moo_oop_word_t)rcv)->slot[i] = dw; + break; + } + + case MOO_OBJ_TYPE_OOP: + for (i = sidx; i < end; i++) ((moo_oop_oop_t)rcv)->slot[i] = dval; + break; + + default: + moo_seterrnum (moo, MOO_EINTERN); + return MOO_PF_HARD_FAILURE; + } + + +#if defined(MOO_LIMIT_OBJ_SIZE) + MOO_ASSERT (moo, ssz <= MOO_SMOOI_MAX); + MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ssz)); +#else + if (ssz <= MOO_SMOOI_MAX) + { + MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ssz)); + } + else + { + moo_oop_t v; + v = moo_oowtoint (moo, ssz); + if (!v) MOO_PF_FAILURE; + MOO_STACK_SETRET (moo, nargs, v); + } +#endif + + return MOO_PF_SUCCESS; + +invalid_fill_value: + moo_seterrbfmt (moo, MOO_EINVAL, "invalid fill value - %O", dval); + return MOO_PF_FAILURE; +} + +moo_pfrc_t moo_pf_basic_shift (moo_t* moo, moo_ooi_t nargs) +{ + moo_oop_t rcv, spos, dpos, slen; moo_oow_t sidx, ssz, didx; MOO_ASSERT (moo, nargs == 3); @@ -456,8 +565,8 @@ moo_pfrc_t moo_pf_basic_move (moo_t* moo, moo_ooi_t nargs) } spos = MOO_STACK_GETARG(moo, nargs, 0); - slen = MOO_STACK_GETARG(moo, nargs, 1); - dpos = MOO_STACK_GETARG(moo, nargs, 2); + dpos = MOO_STACK_GETARG(moo, nargs, 1); + slen = MOO_STACK_GETARG(moo, nargs, 2); if (moo_inttooow(moo, spos, &sidx) <= 0) { @@ -472,15 +581,6 @@ moo_pfrc_t moo_pf_basic_move (moo_t* moo, moo_ooi_t nargs) return MOO_PF_FAILURE; } - if (moo_inttooow(moo, slen, &ssz) <= 0) - { - /* negative integer or not integer */ - moo_seterrbfmt (moo, MOO_EINVAL, "invalid source length - %O", slen); - return MOO_PF_FAILURE; - } - - /* TODO: adjust ssz */ - if (moo_inttooow(moo, dpos, &didx) <= 0) { /* negative integer or not integer */ @@ -494,44 +594,127 @@ moo_pfrc_t moo_pf_basic_move (moo_t* moo, moo_ooi_t nargs) return MOO_PF_FAILURE; } - switch (MOO_OBJ_GET_FLAGS_TYPE(rcv)) + if (moo_inttooow(moo, slen, &ssz) <= 0) { - case MOO_OBJ_TYPE_BYTE: - MOO_MEMMOVE (&((moo_oop_byte_t)rcv)->slot[didx], - &((moo_oop_byte_t)rcv)->slot[sidx], - ssz * MOO_SIZEOF(((moo_oop_byte_t)rcv)->slot[0])); - break; - - case MOO_OBJ_TYPE_CHAR: - MOO_MEMMOVE (&((moo_oop_char_t)rcv)->slot[didx], - &((moo_oop_char_t)rcv)->slot[sidx], - ssz * MOO_SIZEOF(((moo_oop_char_t)rcv)->slot[0])); - break; - - case MOO_OBJ_TYPE_HALFWORD: - MOO_MEMMOVE (&((moo_oop_halfword_t)rcv)->slot[didx], - &((moo_oop_halfword_t)rcv)->slot[sidx], - ssz * MOO_SIZEOF(((moo_oop_halfword_t)rcv)->slot[0])); - break; - - case MOO_OBJ_TYPE_WORD: - MOO_MEMMOVE (&((moo_oop_word_t)rcv)->slot[didx], - &((moo_oop_word_t)rcv)->slot[sidx], - ssz * MOO_SIZEOF(((moo_oop_word_t)rcv)->slot[0])); - break; - - case MOO_OBJ_TYPE_OOP: - MOO_MEMMOVE (&((moo_oop_oop_t)rcv)->slot[didx], - &((moo_oop_oop_t)rcv)->slot[sidx], - ssz * MOO_SIZEOF(((moo_oop_oop_t)rcv)->slot[0])); - break; - - default: - moo_seterrnum (moo, MOO_EINTERN); - return MOO_PF_HARD_FAILURE; + /* negative integer or not integer */ + moo_seterrbfmt (moo, MOO_EINVAL, "invalid shift count - %O", slen); + return MOO_PF_FAILURE; } - MOO_STACK_SETRETTORCV (moo, nargs); + if (sidx != didx && ssz > 0) + { + moo_oow_t maxlen; + + maxlen = MOO_OBJ_GET_SIZE(rcv) - didx; + if (ssz > maxlen) ssz = maxlen; + + switch (MOO_OBJ_GET_FLAGS_TYPE(rcv)) + { + case MOO_OBJ_TYPE_BYTE: + MOO_MEMMOVE (&((moo_oop_byte_t)rcv)->slot[didx], + &((moo_oop_byte_t)rcv)->slot[sidx], + ssz * MOO_SIZEOF(((moo_oop_byte_t)rcv)->slot[0])); + + if (didx > sidx) + { + MOO_MEMSET (&((moo_oop_byte_t)rcv)->slot[sidx], 0, + (didx - sidx) * MOO_SIZEOF(((moo_oop_byte_t)rcv)->slot[0])); + } + else + { + MOO_MEMSET (&((moo_oop_byte_t)rcv)->slot[didx + ssz - 1], 0, + (sidx - didx) * MOO_SIZEOF(((moo_oop_byte_t)rcv)->slot[0])); + } + break; + + case MOO_OBJ_TYPE_CHAR: + MOO_MEMMOVE (&((moo_oop_char_t)rcv)->slot[didx], + &((moo_oop_char_t)rcv)->slot[sidx], + ssz * MOO_SIZEOF(((moo_oop_char_t)rcv)->slot[0])); + + if (didx > sidx) + { + MOO_MEMSET (&((moo_oop_char_t)rcv)->slot[sidx], 0, + (didx - sidx) * MOO_SIZEOF(((moo_oop_char_t)rcv)->slot[0])); + } + else + { + MOO_MEMSET (&((moo_oop_char_t)rcv)->slot[didx + ssz - 1], 0, + (sidx - didx) * MOO_SIZEOF(((moo_oop_char_t)rcv)->slot[0])); + } + break; + + case MOO_OBJ_TYPE_HALFWORD: + MOO_MEMMOVE (&((moo_oop_halfword_t)rcv)->slot[didx], + &((moo_oop_halfword_t)rcv)->slot[sidx], + ssz * MOO_SIZEOF(((moo_oop_halfword_t)rcv)->slot[0])); + + if (didx > sidx) + { + MOO_MEMSET (&((moo_oop_halfword_t)rcv)->slot[sidx], 0, + (didx - sidx) * MOO_SIZEOF(((moo_oop_halfword_t)rcv)->slot[0])); + } + else + { + MOO_MEMSET (&((moo_oop_halfword_t)rcv)->slot[didx + ssz - 1], 0, + (sidx - didx) * MOO_SIZEOF(((moo_oop_halfword_t)rcv)->slot[0])); + } + break; + + case MOO_OBJ_TYPE_WORD: + MOO_MEMMOVE (&((moo_oop_word_t)rcv)->slot[didx], + &((moo_oop_word_t)rcv)->slot[sidx], + ssz * MOO_SIZEOF(((moo_oop_word_t)rcv)->slot[0])); + + if (didx > sidx) + { + MOO_MEMSET (&((moo_oop_word_t)rcv)->slot[sidx], 0, + (didx - sidx) * MOO_SIZEOF(((moo_oop_word_t)rcv)->slot[0])); + } + else + { + MOO_MEMSET (&((moo_oop_word_t)rcv)->slot[didx + ssz - 1], 0, + (sidx - didx) * MOO_SIZEOF(((moo_oop_word_t)rcv)->slot[0])); + } + break; + + case MOO_OBJ_TYPE_OOP: + MOO_MEMMOVE (&((moo_oop_oop_t)rcv)->slot[didx], + &((moo_oop_oop_t)rcv)->slot[sidx], + ssz * MOO_SIZEOF(((moo_oop_oop_t)rcv)->slot[0])); + + if (didx > sidx) + { + while (sidx < didx) ((moo_oop_oop_t)rcv)->slot[sidx++] = moo->_nil; + } + else + { + while (didx > sidx) ((moo_oop_oop_t)rcv)->slot[(didx++) + ssz] = moo->_nil; + } + break; + + default: + moo_seterrnum (moo, MOO_EINTERN); + return MOO_PF_HARD_FAILURE; + } + } + +#if defined(MOO_LIMIT_OBJ_SIZE) + MOO_ASSERT (moo, ssz <= MOO_SMOOI_MAX); + MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ssz)); +#else + if (ssz <= MOO_SMOOI_MAX) + { + MOO_STACK_SETRET (moo, nargs, MOO_SMOOI_TO_OOP(ssz)); + } + else + { + moo_oop_t v; + v = moo_oowtoint (moo, ssz); + if (!v) MOO_PF_FAILURE; + MOO_STACK_SETRET (moo, nargs, v); + } +#endif return MOO_PF_SUCCESS; }