Skip to content

Commit f5df788

Browse files
committed
FIX: TAKE/DEEP does not copies series values within the block
fixes: Oldes/Rebol-issues#171
1 parent 567f53f commit f5df788

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

src/core/t-block.c

+22-9
Original file line numberDiff line numberDiff line change
@@ -703,8 +703,8 @@ static struct {
703703

704704
case A_TAKE:
705705
// take/part:
706-
if (D_REF(2)) {
707-
len = Partial1(value, D_ARG(3));
706+
if (D_REF(ARG_TAKE_PART)) {
707+
len = Partial1(value, D_ARG(ARG_TAKE_LENGTH));
708708
if (len == 0) {
709709
zero_blk:
710710
Set_Block(D_RET, Make_Block(0));
@@ -715,22 +715,35 @@ static struct {
715715

716716
index = VAL_INDEX(value); // /part can change index
717717
// take/last:
718-
if (D_REF(5)) index = tail - len;
718+
if (D_REF(ARG_TAKE_LAST)) index = tail - len;
719719
if (index < 0 || index >= tail) {
720-
if (!D_REF(2)) goto is_none;
720+
if (!D_REF(ARG_TAKE_PART)) goto is_none;
721721
goto zero_blk;
722722
}
723723

724724
// if no /part, just return value, else return block:
725-
if (!D_REF(2)) *D_RET = BLK_HEAD(ser)[index];
726-
else Set_Series(VAL_TYPE(value), D_RET, Copy_Block_Len(ser, index, len)); // no more /DEEP
727-
// else Set_Block(D_RET, Copy_Block_Deep(ser, index, len, D_REF(4) ? COPY_DEEP: 0));
725+
if (!D_REF(ARG_TAKE_PART)) {
726+
*D_RET = BLK_HEAD(ser)[index];
727+
if (D_REF(ARG_TAKE_DEEP) && ANY_SERIES(D_RET)) {
728+
VAL_SERIES(D_RET) = ANY_BLOCK(D_RET)
729+
? Clone_Block(VAL_SERIES(D_RET))
730+
: Copy_Series(VAL_SERIES(D_RET));
731+
}
732+
}
733+
else {
734+
Set_Series(
735+
VAL_TYPE(value), D_RET,
736+
D_REF(ARG_TAKE_DEEP)
737+
? Copy_Block_Values(ser, 0, len, CP_DEEP | TS_STD_SERIES)
738+
: Copy_Block_Len(ser, index, len)
739+
);
740+
}
728741
Remove_Series(ser, index, len);
729742
return R_RET;
730743

731744
case A_PUT:
732-
arg2 = D_ARG(3);
733-
args = D_REF(4) ? AM_FIND_CASE : 0;
745+
arg2 = D_ARG(ARG_PUT_VALUE);
746+
args = D_REF(ARG_PUT_CASE) ? AM_FIND_CASE : 0;
734747
ret = Find_Block(ser, index, tail, arg, len, args, 1);
735748
if(ret != NOT_FOUND) {
736749
ret++;

src/tests/units/series-test.r3

+46
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,52 @@ Rebol [
195195
--assert 3 = take/last blk: [1 2 3]
196196
--assert [1 2] = blk
197197

198+
;@@ https://github.com/Oldes/Rebol-issues/issues/171
199+
--test-- "take/deep block!"
200+
a: [1 [2] "3"] b: reduce [a] c: take b
201+
--assert same? a c
202+
a: [1 [2] "3"] b: reduce [a] c: take/deep b
203+
--assert not same? a c
204+
--assert not same? a/2 c/2
205+
--assert not same? a/3 c/3
206+
--assert [2 3] = append c/2 3
207+
--assert [2] = a/2
208+
--test-- "take/deep block with string!"
209+
a: "1" b: reduce [a 2] c: take b
210+
--assert same? a c
211+
a: "1" b: reduce [a 2] c: take/deep b
212+
--assert "1" = a
213+
--assert "1" = c
214+
--assert not same? a c
215+
--assert "12" = append c 2
216+
--assert "1" = a
217+
--test-- "take/deep block with object!"
218+
a: object [] b: reduce [a 2] c: take b
219+
--assert same? a c
220+
a: object [] b: reduce [a 2] c: take/deep b
221+
--assert same? a c ; object are not copied
222+
--test-- "take/deep/part block!"
223+
a: [1 [2] "3"] b: reduce [a] c: take/part b 1
224+
--assert same? a c/1
225+
--assert [] = b
226+
a: [1 [2] "3"] b: reduce [a] c: take/deep/part b 1
227+
--assert not same? a c/1
228+
--assert not same? a/2 c/1/2
229+
--assert not same? a/3 c/1/3
230+
--assert [2 3] = append c/1/2 3
231+
--assert "34" = append c/1/3 4
232+
--assert "3" = a/3
233+
--test-- "take/deep/part block with string!"
234+
a: "1" b: reduce [a 2] c: take/part b 1
235+
--assert same? a c/1
236+
--assert [2] = b
237+
a: "1" b: reduce [a 2] c: take/deep/part b 1
238+
--assert [2] = b
239+
--assert "1" = a
240+
--assert "1" = c/1
241+
--assert not same? a c/1
242+
--assert "12" = append c/1 2
243+
--assert "1" = a
198244

199245
--test-- "take block!"
200246
s: [1]

src/tools/make-headers.r

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ foreach word [
339339
read
340340
write
341341
query
342+
take
342343
] [make-arg-enums word]
343344

344345
acts: load %../boot/natives.r

0 commit comments

Comments
 (0)