Example #1
0
  public void load(Ruby runtime, boolean wrap) throws IOException {
    RubyModule timeout = runtime.defineModule("Timeout");
    RubyClass superclass = runtime.getRuntimeError();
    RubyClass timeoutError =
        runtime.defineClassUnder("Error", superclass, superclass.getAllocator(), timeout);
    runtime.defineClassUnder(
        "ExitException", runtime.getException(), runtime.getException().getAllocator(), timeout);

    // Here we create an "anonymous" exception type used for unrolling the stack.
    // MRI creates a new one for *every call* to timeout, which can be costly.
    // We opt to use a single exception type for all cases to avoid this overhead.
    RubyClass anonEx =
        runtime.defineClassUnder(
            "AnonymousException",
            runtime.getException(),
            runtime.getException().getAllocator(),
            timeout);
    anonEx.setBaseName(null); // clear basename so it's anonymous when raising

    // These are not really used by timeout, but exposed for compatibility
    timeout.defineConstant(
        "THIS_FILE", RubyRegexp.newRegexp(runtime, "timeout\\.rb", new RegexpOptions()));
    timeout.defineConstant("CALLER_OFFSET", RubyFixnum.newFixnum(runtime, 0));

    // Timeout module methods
    timeout.defineAnnotatedMethods(Timeout.class);

    // Toplevel defines
    runtime.getObject().defineConstant("TimeoutError", timeoutError);
    runtime.getObject().defineAnnotatedMethods(TimeoutToplevel.class);
  }
Example #2
0
    @Specialization
    public boolean equal(RubyRegexp a, RubyRegexp b) {
      notDesignedForCompilation();

      return ((org.jruby.RubyString)
              org.jruby.RubyRegexp.newRegexp(
                      getContext().getRuntime(), a.getSource(), a.getRegex().getOptions())
                  .to_s())
          .getByteList()
          .equals(
              ((org.jruby.RubyString)
                      org.jruby.RubyRegexp.newRegexp(
                              getContext().getRuntime(), b.getSource(), b.getRegex().getOptions())
                          .to_s())
                  .getByteList());
    }
Example #3
0
  public RubyRegexp loadPattern(Ruby runtime) {
    // FIXME: 1.9 should care about internal or external encoding and not kcode.
    if (pattern == null || runtime.getKCode() != pattern.getKCode()) {
      setPattern(RubyRegexp.newRegexp(runtime, value, options));
    }

    return pattern;
  }
Example #4
0
 public final RubyRegexp cacheRegexp(int index, RubyString pattern, int options) {
   RubyRegexp regexp = regexps[index];
   Ruby runtime = pattern.getRuntime();
   if (regexp == null || runtime.getKCode() != regexp.getKCode()) {
     regexp = RubyRegexp.newRegexp(runtime, pattern.getByteList(), options);
     regexps[index] = regexp;
   }
   return regexp;
 }
Example #5
0
 public final RubyRegexp getRegexp(Ruby runtime, int index, String pattern, int options) {
   RubyRegexp regexp = regexps[index];
   if (regexp == null || runtime.getKCode() != regexp.getKCode()) {
     regexp = RubyRegexp.newRegexp(runtime, pattern, options);
     regexp.setLiteral();
     regexps[index] = regexp;
   }
   return regexp;
 }
Example #6
0
  @Override
  public Object retrieve(InterpreterContext interp) {
    RubyRegexp reg =
        RubyRegexp.newRegexp(
            interp.getRuntime(), ((RubyString) regexp.retrieve(interp)).getByteList(), options);

    reg.setLiteral();

    return reg;
  }
Example #7
0
 @Specialization
 public RubyString to_s(RubyRegexp regexp) {
   return new RubyString(
       getContext().getCoreLibrary().getStringClass(),
       ((org.jruby.RubyString)
               org.jruby.RubyRegexp.newRegexp(
                       getContext().getRuntime(),
                       regexp.getSource(),
                       regexp.getRegex().getOptions())
                   .to_s())
           .getByteList());
 }