protected JSInvocationExpression CastToInteger(JSExpression booleanExpression) { return(JSInvocationExpression.InvokeMethod( JS.Number(TypeSystem.SByte), booleanExpression, null, true )); }
public void VisitNode(JSIndexerExpression ie) { var indexType = ie.Index.GetActualType(TypeSystem); if ( !TypeUtil.IsIntegral(indexType) && IsEnumOrNullableEnum(indexType) ) { var cast = JSInvocationExpression.InvokeStatic( JS.Number(TypeSystem.Int32), new[] { ie.Index }, true ); ie.ReplaceChild(ie.Index, cast); } VisitChildren(ie); }
public void VisitNode(JSCastExpression ce) { var currentType = ce.Expression.GetActualType(TypeSystem); var targetType = ce.NewType; JSExpression newExpression = null; if (targetType.MetadataType == MetadataType.Char) { newExpression = JSInvocationExpression.InvokeStatic( JS.fromCharCode, new[] { ce.Expression }, true ); } else if ( (currentType.MetadataType == MetadataType.Char) && TypeUtil.IsIntegral(targetType) ) { newExpression = JSInvocationExpression.InvokeMethod( JS.charCodeAt, ce.Expression, new[] { JSLiteral.New(0) }, true ); } else if ( IntroduceEnumCasts.IsEnumOrNullableEnum(currentType) ) { var enumInfo = TypeInfo.Get(currentType); if (targetType.MetadataType == MetadataType.Boolean) { EnumMemberInfo enumMember; if (enumInfo.ValueToEnumMember.TryGetValue(0, out enumMember)) { newExpression = new JSBinaryOperatorExpression( JSOperator.NotEqual, ce.Expression, new JSEnumLiteral(enumMember.Value, enumMember), TypeSystem.Boolean ); } else if (enumInfo.ValueToEnumMember.TryGetValue(1, out enumMember)) { newExpression = new JSBinaryOperatorExpression( JSOperator.Equal, ce.Expression, new JSEnumLiteral(enumMember.Value, enumMember), TypeSystem.Boolean ); } else { newExpression = new JSUntranslatableExpression(String.Format( "Could not cast enum of type '{0}' to boolean because it has no zero value or one value", currentType.FullName )); } } else if (TypeUtil.IsNumeric(targetType)) { newExpression = JSInvocationExpression.InvokeStatic( JS.Number(targetType), new[] { ce.Expression }, true ); } else if (targetType.FullName == "System.Enum") { newExpression = ce.Expression; } else { // Debugger.Break(); } } else if ( targetType.MetadataType == MetadataType.Boolean ) { newExpression = new JSBinaryOperatorExpression( JSBinaryOperator.NotEqual, ce.Expression, new JSDefaultValueLiteral(currentType), TypeSystem.Boolean ); } else if ( TypeUtil.IsNumeric(targetType) && TypeUtil.IsNumeric(currentType) ) { if ( TypeUtil.IsIntegral(currentType) || !TypeUtil.IsIntegral(targetType) ) { newExpression = ce.Expression; } else { newExpression = JSInvocationExpression.InvokeStatic(JS.floor, new[] { ce.Expression }, true); } } else { newExpression = JSIL.Cast(ce.Expression, targetType); } if (newExpression != null) { ParentNode.ReplaceChild(ce, newExpression); VisitReplacement(newExpression); } else { // Debugger.Break(); VisitChildren(ce); } }