Skip to content

Commit

Permalink
Fix: use 'light' match-ing (not depending on scope) (#11656)
Browse files Browse the repository at this point in the history
back-port of #11653
  • Loading branch information
kares authored Mar 18, 2020
1 parent f1501b4 commit d106e8b
Showing 1 changed file with 18 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import org.jruby.Ruby;
import org.jruby.RubyRegexp;
import org.jruby.RubyString;
import org.jruby.runtime.builtin.IRubyObject;
import org.jruby.util.ByteList;
Expand Down Expand Up @@ -466,34 +468,40 @@ private static boolean valueIsTruthy(Object object) {
!Boolean.toString(false).equals(other);
}

private static RubyRegexp newRegexp(String pattern) {
final Ruby runtime = RubyUtil.RUBY;
return RubyRegexp.newRegexpFromStr(runtime, runtime.newString(pattern), 0);
}

private static boolean matches(RubyString str, RubyRegexp regexp) {
return regexp.match_p(RubyUtil.RUBY.getCurrentContext(), str).isTrue(); // match? returns true/false
}

private static final class FieldMatches implements EventCondition {

private final FieldReference field;

private final RubyString regex;
private final RubyRegexp regexp;

private FieldMatches(final String field, final String regex) {
private FieldMatches(final String field, final String pattern) {
this.field = FieldReference.from(field);
this.regex = RubyUtil.RUBY.newString(regex);
this.regexp = newRegexp(pattern);
}

@Override
public boolean fulfilled(final JrubyEventExtLibrary.RubyEvent event) {
final Object tomatch = event.getEvent().getUnconvertedField(field);
return tomatch instanceof RubyString &&
!((RubyString) tomatch).match(WorkerLoop.THREAD_CONTEXT.get(), regex).isNil();
final Object toMatch = event.getEvent().getUnconvertedField(field);
return toMatch instanceof RubyString && matches((RubyString) toMatch, regexp);
}
}

private static final class ConstantMatches implements EventCondition {

private final boolean matches;

private ConstantMatches(final Object constant, final String regex) {
private ConstantMatches(final Object constant, final String pattern) {
this.matches = constant instanceof String &&
!(RubyUtil.RUBY.newString((String) constant).match(
WorkerLoop.THREAD_CONTEXT.get(),
RubyUtil.RUBY.newString(regex)).isNil());
matches(RubyUtil.RUBY.newString((String) constant), newRegexp(pattern));
}

@Override
Expand Down

0 comments on commit d106e8b

Please sign in to comment.