Skip to content

Commit 273fcbc

Browse files
ops.h: update OP_ARYDUP instruction and rename to OP_ARYSPLAT.
Transforms the value of a splat inside a return statement (similar to an array). For example, `return *nil` should return `nil.to_a`, while `return *1` should return `[1]`
1 parent 6ee4f84 commit 273fcbc

5 files changed

Lines changed: 8 additions & 13 deletions

File tree

doc/internal/opcode.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ sign) of operands.
9595
| `OP_ARRAY2` | `BBB` | `R(a) = ary_new(R(b),R(b+1)..R(b+c))` |
9696
| `OP_ARYCAT` | `B` | `ary_cat(R(a),R(a+1))` |
9797
| `OP_ARYPUSH` | `BB` | `ary_push(R(a),R(a+1)..R(a+b))` |
98-
| `OP_ARYDUP` | `B` | `R(a) = ary_dup(R(a))` |
98+
| `OP_ARYSPLAT` | `B` | `R(a) = ary_splat(R(a))` |
9999
| `OP_AREF` | `BBB` | `R(a) = R(b)[c]` |
100100
| `OP_ASET` | `BBB` | `R(b)[c] = R(a)` |
101101
| `OP_APOST` | `BBB` | `*R(a),R(a+1)..R(a+c) = R(a)[b..]` |

include/mruby/ops.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ OPCODE(ARRAY, BB) /* R[a] = ary_new(R[a],R[a+1]..R[a+b]) */
8787
OPCODE(ARRAY2, BBB) /* R[a] = ary_new(R[b],R[b+1]..R[b+c]) */
8888
OPCODE(ARYCAT, B) /* ary_cat(R[a],R[a+1]) */
8989
OPCODE(ARYPUSH, BB) /* ary_push(R[a],R[a+1]..R[a+b]) */
90-
OPCODE(ARYDUP, B) /* R[a] = ary_dup(R[a]) */
90+
OPCODE(ARYSPLAT, B) /* R[a] = ary_splat(R[a]) */
9191
OPCODE(AREF, BBB) /* R[a] = R[b][c] */
9292
OPCODE(ASET, BBB) /* R[b][c] = R[a] */
9393
OPCODE(APOST, BBB) /* *R[a],R[a+1]..R[a+c] = R[a][b..] */

mrbgems/mruby-compiler/core/codegen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ gen_retval(codegen_scope *s, node *tree)
22022202
if (nint(tree->car) == NODE_SPLAT) {
22032203
codegen(s, tree, VAL);
22042204
pop();
2205-
genop_1(s, OP_ARYDUP, cursp());
2205+
genop_1(s, OP_ARYSPLAT, cursp());
22062206
}
22072207
else {
22082208
codegen(s, tree, VAL);

src/codedump.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ codedump(mrb_state *mrb, const mrb_irep *irep)
476476
printf("ARYPUSH\tR%d\t%d\t", a, b);
477477
print_lv_a(mrb, irep, a);
478478
break;
479-
CASE(OP_ARYDUP, B):
480-
printf("ARYDUP\tR%d\t", a);
479+
CASE(OP_ARYSPLAT, B):
480+
printf("ARYSPLAT\tR%d\t", a);
481481
print_lv_a(mrb, irep, a);
482482
break;
483483
CASE(OP_AREF, BBB):

src/vm.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2748,15 +2748,10 @@ mrb_vm_exec(mrb_state *mrb, const struct RProc *proc, const mrb_code *pc)
27482748
NEXT;
27492749
}
27502750

2751-
CASE(OP_ARYDUP, B) {
2752-
mrb_value ary = regs[a];
2753-
if (mrb_array_p(ary)) {
2754-
ary = mrb_ary_new_from_values(mrb, RARRAY_LEN(ary), RARRAY_PTR(ary));
2755-
}
2756-
else {
2757-
ary = mrb_ary_new_from_values(mrb, 1, &ary);
2758-
}
2751+
CASE(OP_ARYSPLAT, B) {
2752+
mrb_value ary = mrb_ary_splat(mrb, regs[a]);
27592753
regs[a] = ary;
2754+
mrb_gc_arena_restore(mrb, ai);
27602755
NEXT;
27612756
}
27622757

0 commit comments

Comments
 (0)