Skip to content

Commit c6471b5

Browse files
Lillian Zhangeregon
authored andcommitted
Add an option —use-truffle-regex-with-warn
To print a warning when falling back to Joni because a regular expression can’t be run as a Truffle regex.
1 parent b166787 commit c6471b5

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

src/main/java/org/truffleruby/options/Options.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ public class Options {
129129
public final boolean WARN_EXPERIMENTAL;
130130
/** --use-truffle-regex=false */
131131
public final boolean USE_TRUFFLE_REGEX;
132+
/** --use-truffle-regex-with-warn=false */
133+
public final boolean USE_TRUFFLE_REGEX_WITH_WARN;
132134
/** --argv-globals=false */
133135
public final boolean ARGV_GLOBALS;
134136
/** --chomp-loop=false */
@@ -246,6 +248,7 @@ public Options(Env env, OptionValues options, LanguageOptions languageOptions) {
246248
WARN_DEPRECATED = options.get(OptionsCatalog.WARN_DEPRECATED_KEY);
247249
WARN_EXPERIMENTAL = options.get(OptionsCatalog.WARN_EXPERIMENTAL_KEY);
248250
USE_TRUFFLE_REGEX = options.get(OptionsCatalog.USE_TRUFFLE_REGEX_KEY);
251+
USE_TRUFFLE_REGEX_WITH_WARN = options.get(OptionsCatalog.USE_TRUFFLE_REGEX_WITH_WARN_KEY);
249252
ARGV_GLOBALS = options.get(OptionsCatalog.ARGV_GLOBALS_KEY);
250253
CHOMP_LOOP = options.get(OptionsCatalog.CHOMP_LOOP_KEY);
251254
GETS_LOOP = options.get(OptionsCatalog.GETS_LOOP_KEY);
@@ -387,6 +390,8 @@ public Object fromDescriptor(OptionDescriptor descriptor) {
387390
return WARN_EXPERIMENTAL;
388391
case "ruby.use-truffle-regex":
389392
return USE_TRUFFLE_REGEX;
393+
case "ruby.use-truffle-regex-with-warn":
394+
return USE_TRUFFLE_REGEX_WITH_WARN;
390395
case "ruby.argv-globals":
391396
return ARGV_GLOBALS;
392397
case "ruby.chomp-loop":

src/main/ruby/truffleruby/core/truffle/regexp_operations.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def self.match_from(re, str, pos)
6464
Truffle::Boot.delay do
6565
COMPARE_ENGINES = Truffle::Boot.get_option('compare-regex-engines')
6666
USE_TRUFFLE_REGEX = Truffle::Boot.get_option('use-truffle-regex')
67+
USE_TRUFFLE_REGEX_WITH_WARN = Truffle::Boot.get_option('use-truffle-regex-with-warn')
6768

6869
if Truffle::Boot.get_option('regexp-instrument-creation') or Truffle::Boot.get_option('regexp-instrument-match')
6970
at_exit do
@@ -75,7 +76,7 @@ def self.match_from(re, str, pos)
7576
def self.match_in_region(re, str, from, to, at_start, start)
7677
if COMPARE_ENGINES
7778
match_in_region_compare_engines(re, str, from, to, at_start, start)
78-
elsif USE_TRUFFLE_REGEX
79+
elsif USE_TRUFFLE_REGEX || USE_TRUFFLE_REGEX_WITH_WARN
7980
match_in_region_tregex(re, str, from, to, at_start, start)
8081
else
8182
Primitive.regexp_match_in_region(re, str, from, to, at_start, start)
@@ -115,6 +116,9 @@ def self.match_in_region_tregex(re, str, from, to, at_start, start)
115116
regex_result = compiled_regex.execBytes(str_bytes, from)
116117
end
117118
if bail_out
119+
if USE_TRUFFLE_REGEX_WITH_WARN
120+
warn "match_in_region_tregex(/#{re}/, \"#{str}\"@#{str.encoding}, #{from}, #{to}, #{at_start}, #{encoding_conversion}, #{start}) can’t be run as a Truffle regexp and fell back to Joni"
121+
end
118122
return Primitive.regexp_match_in_region(re, str, from, to, at_start, start)
119123
elsif regex_result.isMatch
120124
starts = []

src/options.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ EXPERT:
144144

145145
# Controlling the regular expression engines
146146
USE_TRUFFLE_REGEX: [use-truffle-regex, boolean, false, 'Use the Truffle regular expression engine for Regexp objects']
147+
USE_TRUFFLE_REGEX_WITH_WARN: [use-truffle-regex-with-warn, boolean, false, 'Use the Truffle regular expression engine for Regexp objects and warn when falling back to Joni']
147148

148149
INTERNAL: # Options for debugging the TruffleRuby implementation
149150
EXPERIMENTAL:

src/shared/java/org/truffleruby/shared/options/OptionsCatalog.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ public class OptionsCatalog {
8383
public static final OptionKey<Boolean> WARN_DEPRECATED_KEY = new OptionKey<>(false);
8484
public static final OptionKey<Boolean> WARN_EXPERIMENTAL_KEY = new OptionKey<>(true);
8585
public static final OptionKey<Boolean> USE_TRUFFLE_REGEX_KEY = new OptionKey<>(false);
86+
public static final OptionKey<Boolean> USE_TRUFFLE_REGEX_WITH_WARN_KEY = new OptionKey<>(false);
8687
public static final OptionKey<Boolean> ARGV_GLOBALS_KEY = new OptionKey<>(false);
8788
public static final OptionKey<Boolean> CHOMP_LOOP_KEY = new OptionKey<>(false);
8889
public static final OptionKey<Boolean> GETS_LOOP_KEY = new OptionKey<>(false);
@@ -596,6 +597,13 @@ public class OptionsCatalog {
596597
.stability(OptionStability.EXPERIMENTAL)
597598
.build();
598599

600+
public static final OptionDescriptor USE_TRUFFLE_REGEX_WITH_WARN = OptionDescriptor
601+
.newBuilder(USE_TRUFFLE_REGEX_WITH_WARN_KEY, "ruby.use-truffle-regex-with-warn")
602+
.help("Use the Truffle regular expression engine for Regexp objects and warn when falling back to Joni")
603+
.category(OptionCategory.EXPERT)
604+
.stability(OptionStability.EXPERIMENTAL)
605+
.build();
606+
599607
public static final OptionDescriptor ARGV_GLOBALS = OptionDescriptor
600608
.newBuilder(ARGV_GLOBALS_KEY, "ruby.argv-globals")
601609
.help("Parse options in script argv into global variables (configured by the -s Ruby option)")
@@ -1221,6 +1229,8 @@ public static OptionDescriptor fromName(String name) {
12211229
return WARN_EXPERIMENTAL;
12221230
case "ruby.use-truffle-regex":
12231231
return USE_TRUFFLE_REGEX;
1232+
case "ruby.use-truffle-regex-with-warn":
1233+
return USE_TRUFFLE_REGEX_WITH_WARN;
12241234
case "ruby.argv-globals":
12251235
return ARGV_GLOBALS;
12261236
case "ruby.chomp-loop":
@@ -1433,6 +1443,7 @@ public static OptionDescriptor[] allDescriptors() {
14331443
WARN_DEPRECATED,
14341444
WARN_EXPERIMENTAL,
14351445
USE_TRUFFLE_REGEX,
1446+
USE_TRUFFLE_REGEX_WITH_WARN,
14361447
ARGV_GLOBALS,
14371448
CHOMP_LOOP,
14381449
GETS_LOOP,

0 commit comments

Comments
 (0)