Skip to content

Commit cc24d7f

Browse files
committed
opt_aset_with
1 parent 441bc01 commit cc24d7f

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/syntax_tree/compiler.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,24 @@ def visit_array(node)
337337
def visit_assign(node)
338338
case node.target
339339
when ARefField
340+
if !frozen_string_literal && specialized_instruction && (node.target.index.parts.length == 1)
341+
arg = node.target.index.parts.first
342+
343+
if arg.is_a?(StringLiteral) && (arg.parts.length == 1)
344+
string_part = arg.parts.first
345+
346+
if string_part.is_a?(TStringContent)
347+
visit(node.target.collection)
348+
visit(node.value)
349+
iseq.swap
350+
iseq.topn(1)
351+
iseq.opt_aset_with(string_part.value, :[]=, 2)
352+
iseq.pop
353+
return
354+
end
355+
end
356+
end
357+
340358
iseq.putnil
341359
visit(node.target.collection)
342360
visit(node.target.index)

lib/syntax_tree/yarv.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,11 @@ def opt_aref_with(object, method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
532532
push([:opt_aref_with, object, call_data(method_id, argc, flag)])
533533
end
534534

535+
def opt_aset_with(object, method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
536+
stack.change_by(-2 + 1)
537+
push([:opt_aset_with, object, call_data(method_id, argc, flag)])
538+
end
539+
535540
def opt_getconstant_path(names)
536541
if RUBY_VERSION >= "3.2"
537542
stack.change_by(+1)

test/compiler_test.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ class CompilerTest < Minitest::Test
202202
"foo[bar] <<= 1",
203203
"foo[bar] ^= 1",
204204
"foo['true']",
205+
"foo['true'] = 1",
205206
# Constants (single)
206207
"Foo",
207208
"Foo = 1",

0 commit comments

Comments
 (0)