Commit 7e3308dc
7e3308dccc78Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
fix(codegen): dispatch regex held in a constant through the engine
Stage 3 of issue #61. RX = /pat/; RX.match?(s) and s =~ RX fell
through every dispatcher and compiled to 0 / (-1):
find_regexp_indexonly resolved directRegularExpressionNodes, notConstantReadNodes pointing at one. Extend it to walk the constant's@const_expr_ids[ci]and recurse when the initializer is itself a regex literal.match?/match/=~with a regex receiver were unhandled incompile_call_expr(the onlymatch?site lived incompile_string_method_expr, which is keyed on a string receiver). Add a regex-receiver dispatch above the no-receiver fork soRX.match?(s)andRX =~ sreachsp_re_match_p/sp_re_match.
Combined with stages 1 (byte-length fix) and 2 (engine UTF-8 char
class support), the issue's reproducer
puts RX.match?("₁₂") now prints true.
test/regex_via_constant.rb covers the four call shapes
(RX.match?, s =~ RX, RX =~ s, RX.match).
Closes #61
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 7e3308dccc78
- Parent
- 65250c0ad119
spinel_codegen.rbmodifiedView file
@@ -910,7 +910,24 @@
910910
end911911
i = i + 1912912
end913+
return -1913914
end915+
# A constant initialized to a regex literal forwards to the916+
# underlying pattern, so `RX = /pat/; RX.match?(s)` and917+
# `s =~ RX` dispatch to the engine instead of falling through918+
# to the literal-`(-1)` / `sp_str_include` fallbacks.919+
if @nd_type[nid] == "ConstantReadNode"920+
cname = resolve_const_ref_name(nid)921+
if cname != ""922+
ci = find_const_idx(cname)923+
if ci >= 0 && ci < @const_expr_ids.length924+
eid = @const_expr_ids[ci]925+
if eid >= 0 && @nd_type[eid] == "RegularExpressionNode"926+
return find_regexp_index(eid)927+
end928+
end929+
end930+
end914931
-1915932
end916933
@@ -13271,6 +13288,27 @@
1327113288
end1327213289
end1327313290
13291+
# regex.match? / regex.match / regex =~ str — receiver is the regex13292+
# (typically a constant referring to a /…/ literal). Dispatched here13293+
# rather than compile_string_method_expr, which wants a string13294+
# receiver.13295+
if recv >= 0 && (mname == "match?" || mname == "=~" || mname == "match")13296+
ridx = find_regexp_index(recv)13297+
if ridx >= 013298+
args_id = @nd_arguments[nid]13299+
if args_id >= 013300+
arg_ids = get_args(args_id)13301+
if arg_ids.length > 013302+
sc = compile_expr(arg_ids[0])13303+
if mname == "match?"13304+
return "sp_re_match_p(sp_re_pat_" + ridx.to_s + ", " + sc + ")"13305+
end13306+
return "sp_re_match(sp_re_pat_" + ridx.to_s + ", " + sc + ")"13307+
end13308+
end13309+
end13310+
end13311+
1327413312
# No receiver1327513313
if recv < 01327613314
return compile_no_recv_call_expr(nid, mname)test/regex_via_constant.rbaddedView file
Loading diff…