/// <summary> /// Applies given operator on coerced values and converts its result to Ruby truth (using Protocols.IsTrue). /// </summary> /// <exception cref="ArgumentError"> /// "coerce" method is not defined, throws a subclass of SystemException, or returns something other than a pair of objects. /// </exception> public static bool CoerceAndRelate(BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ comparisonStorage, string /*!*/ relationalOp, object self, object other) { object result; if (TryCoerceAndApply(coercionStorage, comparisonStorage, relationalOp, self, other, out result)) { return(RubyOps.IsTrue(result)); } throw RubyExceptions.MakeComparisonError(coercionStorage.Context, self, other); }
bool IEqualityComparer <object> .Equals(object x, object y) { if (x == y) { return(true); } if (x is int) { return(y is int && (int)x == (int)y); } return(RubyOps.IsTrue(_eqlSite.Target(_eqlSite, x, y))); }
/// <summary> /// Compare the values of self and other (coerce using other as the object then call the passed in comparison operator). /// </summary> /// <remarks> /// This method is used for <, <=, > and >= operators. If we can't coerce then throw a specific comparison exception. /// </remarks> /// <exception cref="ArgumentException">If self and other cannot be coerced to the same type.</exception> public static bool CoerceAndCallRelationOperator(RubyContext /*!*/ context, object self, object other, DynamicInvocation invoke) { try { // Swap self and other around to do the coercion. RubyArray coercedValues = RubySites.Coerce(context, other, self); return(RubyOps.IsTrue(invoke(context, coercedValues[0], coercedValues[1]))); } catch (MemberAccessException x) { throw RubyExceptions.MakeComparisonError(context, self, other, x); } catch (ArgumentException x) { throw RubyExceptions.MakeComparisonError(context, self, other, x); } catch (NullReferenceException x) { throw RubyExceptions.MakeComparisonError(context, self, other, x); } }
public static int ConvertCompareResult(RubyContext /*!*/ context, object result) { // Should throw if a call to <=> returns null Debug.Assert(result != null); if (RubyOps.IsTrue(_GreaterSharedSite.Target(_GreaterSharedSite, context, result, 0))) { return(1); } else if (RubyOps.IsTrue(_LessSharedSite.Target(_LessSharedSite, context, result, 0))) { return(-1); } return(0); }
public static int ConvertCompareResult(ComparisonStorage /*!*/ comparisonStorage, object /*!*/ result) { Debug.Assert(result != null); var greaterThanSite = comparisonStorage.GreaterThanSite; if (RubyOps.IsTrue(greaterThanSite.Target(greaterThanSite, result, 0))) { return(1); } var lessThanSite = comparisonStorage.LessThanSite; if (RubyOps.IsTrue(lessThanSite.Target(lessThanSite, result, 0))) { return(-1); } return(0); }
/// <summary> /// Applies given operator on coerced values and returns the result. /// </summary> /// <exception cref="TypeError"> /// "coerce" method is not defined, throws a subclass of SystemException, or returns something other than a pair of objects. /// </exception> public static object TryCoerceAndApply( BinaryOpStorage /*!*/ coercionStorage, BinaryOpStorage /*!*/ binaryOpStorage, string /*!*/ binaryOp, object self, object other) { if (other == null) { return(null); } object result; if (TryCoerceAndApply(coercionStorage, binaryOpStorage, binaryOp, self, other, out result)) { if (result != null) { return(RubyOps.IsTrue(result)); } } return(null); }
public static int ConvertCompareResult( BinaryOpStorage /*!*/ lessThanStorage, BinaryOpStorage /*!*/ greaterThanStorage, object /*!*/ result) { Debug.Assert(result != null); var greaterThanSite = greaterThanStorage.GetCallSite(">"); if (RubyOps.IsTrue(greaterThanSite.Target(greaterThanSite, result, 0))) { return(1); } var lessThanSite = lessThanStorage.GetCallSite("<"); if (RubyOps.IsTrue(lessThanSite.Target(lessThanSite, result, 0))) { return(-1); } return(0); }