Exemplo n.º 1
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;
  }
Exemplo n.º 2
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;
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
  @Override
  public Object retrieve(InterpreterContext interp) {
    RubyRegexp reg =
        RubyRegexp.newRegexp(
            interp.getRuntime(), ((RubyString) regexp.retrieve(interp)).getByteList(), options);

    reg.setLiteral();

    return reg;
  }
Exemplo n.º 5
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);
  }
Exemplo n.º 6
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());
    }
Exemplo n.º 7
0
    @Specialization
    public RubyString escape(RubyString pattern) {
      notDesignedForCompilation();

      return getContext()
          .makeString(
              org.jruby.RubyRegexp.quote19(new ByteList(pattern.getBytes()), true).toString());
    }
Exemplo n.º 8
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());
 }
  @Override
  public Object interpret(
      ThreadContext context,
      StaticScope currScope,
      DynamicScope currDynScope,
      IRubyObject self,
      Object[] temp) {
    // FIXME (from RegexpNode.java): 1.9 should care about internal or external encoding and not
    // kcode.
    // If we have a constant regexp string or if the regexp patterns asks for caching, cache the
    // regexp
    if (rubyRegexp == null
        || !options.isOnce()
        || context.runtime.getKCode() != rubyRegexp.getKCode()) {
      RubyString[] pieces = retrievePieces(context, self, currScope, currDynScope, temp);
      RubyString pattern = RubyRegexp.preprocessDRegexp(context.runtime, pieces, options);
      RubyRegexp re = RubyRegexp.newDRegexp(context.runtime, pattern, options);
      re.setLiteral();
      rubyRegexp = re;
    }

    return rubyRegexp;
  }
Exemplo n.º 10
0
 @Override
 public IRubyObject interpret(
     Ruby runtime, ThreadContext context, IRubyObject self, Block aBlock) {
   return RubyRegexp.nth_match(matchNumber, context.getBackRef());
 }
Exemplo n.º 11
0
  private IRubyObject unmarshalObjectDirectly(int type, MarshalState state, boolean callProc)
      throws IOException {
    IRubyObject rubyObj = null;
    switch (type) {
      case 'I':
        MarshalState childState = new MarshalState(true);
        rubyObj = unmarshalObject(childState);
        if (childState.isIvarWaiting()) {
          defaultVariablesUnmarshal(rubyObj);
        }
        return rubyObj;
      case '0':
        rubyObj = runtime.getNil();
        break;
      case 'T':
        rubyObj = runtime.getTrue();
        break;
      case 'F':
        rubyObj = runtime.getFalse();
        break;
      case '"':
        rubyObj = RubyString.unmarshalFrom(this);
        break;
      case 'i':
        rubyObj = RubyFixnum.unmarshalFrom(this);
        break;
      case 'f':
        rubyObj = RubyFloat.unmarshalFrom(this);
        break;
      case '/':
        rubyObj = RubyRegexp.unmarshalFrom(this);
        break;
      case ':':
        rubyObj = RubySymbol.unmarshalFrom(this);
        break;
      case '[':
        rubyObj = RubyArray.unmarshalFrom(this);
        break;
      case '{':
        rubyObj = RubyHash.unmarshalFrom(this, false);
        break;
      case '}':
        // "hashdef" object, a hash with a default
        rubyObj = RubyHash.unmarshalFrom(this, true);
        break;
      case 'c':
        rubyObj = RubyClass.unmarshalFrom(this);
        break;
      case 'm':
        rubyObj = RubyModule.unmarshalFrom(this);
        break;
      case 'e':
        RubySymbol moduleName = (RubySymbol) unmarshalObject();
        RubyModule tp = null;
        try {
          tp = runtime.getClassFromPath(moduleName.asJavaString());
        } catch (RaiseException e) {
          if (runtime.getModule("NameError").isInstance(e.getException())) {
            throw runtime.newArgumentError("undefined class/module " + moduleName.asJavaString());
          }
          throw e;
        }

        rubyObj = unmarshalObject();

        tp.extend_object(rubyObj);
        tp.callMethod(runtime.getCurrentContext(), "extended", rubyObj);
        break;
      case 'l':
        rubyObj = RubyBignum.unmarshalFrom(this);
        break;
      case 'S':
        rubyObj = RubyStruct.unmarshalFrom(this);
        break;
      case 'o':
        rubyObj = defaultObjectUnmarshal();
        break;
      case 'u':
        rubyObj = userUnmarshal(state);
        break;
      case 'U':
        rubyObj = userNewUnmarshal();
        break;
      case 'C':
        rubyObj = uclassUnmarshall();
        break;
      default:
        throw getRuntime().newArgumentError("dump format error(" + (char) type + ")");
    }

    if (callProc) {
      return doCallProcForObj(rubyObj);
    }

    return rubyObj;
  }