Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 441bc01

Browse files
committed
opt_aref_with
1 parent d8815de commit 441bc01

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

lib/syntax_tree/compiler.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,21 @@ def visit_tstring_content(node)
158158
node.value
159159
end
160160

161+
def visit_var_ref(node)
162+
raise CompilationError unless node.value.is_a?(Kw)
163+
164+
case node.value.value
165+
when "nil"
166+
nil
167+
when "true"
168+
true
169+
when "false"
170+
false
171+
else
172+
raise CompilationError
173+
end
174+
end
175+
161176
def visit_word(node)
162177
if node.parts.length == 1 && node.parts.first.is_a?(TStringContent)
163178
node.parts.first.value
@@ -258,6 +273,20 @@ def visit_alias(node)
258273

259274
def visit_aref(node)
260275
visit(node.collection)
276+
277+
if !frozen_string_literal && specialized_instruction && (node.index.parts.length == 1)
278+
arg = node.index.parts.first
279+
280+
if arg.is_a?(StringLiteral) && (arg.parts.length == 1)
281+
string_part = arg.parts.first
282+
283+
if string_part.is_a?(TStringContent)
284+
iseq.opt_aref_with(string_part.value, :[], 1)
285+
return
286+
end
287+
end
288+
end
289+
261290
visit(node.index)
262291
iseq.send(:[], 1)
263292
end

lib/syntax_tree/yarv.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,11 @@ def once(postexe_iseq, inline_storage)
527527
push([:once, postexe_iseq, inline_storage])
528528
end
529529

530+
def opt_aref_with(object, method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
531+
stack.change_by(-1 + 1)
532+
push([:opt_aref_with, object, call_data(method_id, argc, flag)])
533+
end
534+
530535
def opt_getconstant_path(names)
531536
if RUBY_VERSION >= "3.2"
532537
stack.change_by(+1)

test/compiler_test.rb

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

0 commit comments

Comments
 (0)