Exemple #1
0
  @Specialization(guards = "!isRubyString(object)")
  public RubyBasicObject coerceObject(VirtualFrame frame, Object object) {
    final Object coerced;

    try {
      coerced = toStrNode.call(frame, object, "to_str", null);
    } catch (RaiseException e) {
      if (((RubyBasicObject) e.getRubyException()).getLogicalClass()
          == getContext().getCoreLibrary().getNoMethodErrorClass()) {
        CompilerDirectives.transferToInterpreter();
        throw new RaiseException(
            getContext().getCoreLibrary().typeErrorNoImplicitConversion(object, "String", this));
      } else {
        throw e;
      }
    }

    if (RubyGuards.isRubyString(coerced)) {
      return (RubyBasicObject) coerced;
    } else {
      CompilerDirectives.transferToInterpreter();
      throw new RaiseException(
          getContext()
              .getCoreLibrary()
              .typeErrorBadCoercion(object, "String", "to_str", coerced, this));
    }
  }
Exemple #2
0
  @ExplodeLoop
  private Object handleException(VirtualFrame frame, RaiseException exception) {
    CompilerAsserts.neverPartOfCompilation();

    notDesignedForCompilation();
    getContext()
        .getCoreLibrary()
        .getGlobalVariablesObject()
        .getOperations()
        .setInstanceVariable(
            getContext().getCoreLibrary().getGlobalVariablesObject(),
            "$!",
            exception.getRubyException());

    for (RescueNode rescue : rescueParts) {
      if (rescue.canHandle(frame, exception.getRubyException())) {
        return rescue.execute(frame);
      }
    }

    throw exception;
  }
  @Override
  public Object isDefined(VirtualFrame frame) {
    final RubyConstant constant;
    try {
      constant = lookupConstantNode.executeLookupConstant(frame);
    } catch (RaiseException e) {
      if (Layouts.BASIC_OBJECT.getLogicalClass(((DynamicObject) e.getRubyException()))
          == getContext().getCoreLibrary().getNameErrorClass()) {
        // private constant
        return nil();
      }
      throw e;
    }

    if (constant == null) {
      return nil();
    } else {
      return Layouts.STRING.createString(
          getContext().getCoreLibrary().getStringFactory(),
          RubyString.encodeBytelist("constant", UTF8Encoding.INSTANCE),
          StringSupport.CR_7BIT,
          null);
    }
  }