Skip to content

Commit 42755a9

Browse files
authored
Merge pull request #202 from jhawthorn/expandarray_nil
Allow special case of expandarray with nil
2 parents a83668b + aeaf0a8 commit 42755a9

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

test/ruby/test_yjit.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,19 @@ def foo(str)
136136
RUBY
137137
end
138138

139+
def test_expandarray
140+
assert_compiles(<<~'RUBY', insns: %i[expandarray], result: [1, 2])
141+
a, b = [1, 2]
142+
RUBY
143+
end
144+
145+
def test_expandarray_nil
146+
assert_compiles(<<~'RUBY', insns: %i[expandarray], result: [nil, nil])
147+
a, b = nil
148+
[a, b]
149+
RUBY
150+
end
151+
139152
def test_compile_opt_getinlinecache
140153
assert_compiles(<<~RUBY, insns: %i[opt_getinlinecache], result: 123, min_calls: 2)
141154
def get_foo

yjit_codegen.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,8 +986,19 @@ gen_expandarray(jitstate_t* jit, ctx_t* ctx)
986986
// num is the number of requested values. If there aren't enough in the
987987
// array then we're going to push on nils.
988988
rb_num_t num = (rb_num_t) jit_get_arg(jit, 0);
989+
val_type_t array_type = ctx_get_opnd_type(ctx, OPND_STACK(0));
989990
x86opnd_t array_opnd = ctx_stack_pop(ctx, 1);
990991

992+
if (array_type.type == ETYPE_NIL) {
993+
// special case for a, b = nil pattern
994+
// push N nils onto the stack
995+
for (int i = 0; i < num; i++) {
996+
x86opnd_t push = ctx_stack_push(ctx, TYPE_NIL);
997+
mov(cb, push, imm_opnd(Qnil));
998+
}
999+
return YJIT_KEEP_COMPILING;
1000+
}
1001+
9911002
// Move the array from the stack into REG0 and check that it's an array.
9921003
mov(cb, REG0, array_opnd);
9931004
guard_object_is_heap(cb, REG0, ctx, COUNTED_EXIT(side_exit, expandarray_not_array));

0 commit comments

Comments
 (0)