示例#1
0
        public object __nonzero__(CodeContext context)
        {
            object value;

            if (TryGetBoundCustomMember(context, "__nonzero__", out value))
            {
                return(PythonOps.CallWithContext(context, value));
            }

            if (TryGetBoundCustomMember(context, "__len__", out value))
            {
                value = PythonOps.CallWithContext(context, value);
                // Convert resulting object to the desired type
                if (value is Int32 || value is BigInteger)
                {
                    return(ScriptingRuntimeHelpers.BooleanToObject(Converter.ConvertToBoolean(value)));
                }
                throw PythonOps.TypeError("an integer is required, got {0}", PythonTypeOps.GetName(value));
            }

            return(ScriptingRuntimeHelpers.True);
        }
        internal override ConstantExpression ConstantFold()
        {
            Expression left  = _left.ConstantFold();
            Expression right = _right.ConstantFold();

            var constLeft  = left as ConstantExpression;
            var constRight = right as ConstantExpression;

            try {
                if (constLeft != null && constRight != null &&
                    constLeft.Value != null && constRight.Value != null &&
                    constLeft.Value.GetType() == constRight.Value.GetType())
                {
                    #region Generated Python Constant Folding

                    // *** BEGIN GENERATED CODE ***
                    // generated by function: gen_constant_folding from: generate_ops.py

                    if (constLeft.Value.GetType() == typeof(Int32))
                    {
                        switch (_op)
                        {
                        case PythonOperator.Add: return(new ConstantExpression(Int32Ops.Add((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.Subtract: return(new ConstantExpression(Int32Ops.Subtract((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.Power: return(new ConstantExpression(Int32Ops.Power((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.Multiply: return(new ConstantExpression(Int32Ops.Multiply((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.FloorDivide: return(new ConstantExpression(Int32Ops.FloorDivide((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.Divide: return(new ConstantExpression(Int32Ops.Divide((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.TrueDivide: return(new ConstantExpression(Int32Ops.TrueDivide((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.Mod: return(new ConstantExpression(Int32Ops.Mod((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.LeftShift: return(new ConstantExpression(Int32Ops.LeftShift((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.RightShift: return(new ConstantExpression(Int32Ops.RightShift((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.BitwiseAnd: return(new ConstantExpression(Int32Ops.BitwiseAnd((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.BitwiseOr: return(new ConstantExpression(Int32Ops.BitwiseOr((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.ExclusiveOr: return(new ConstantExpression(Int32Ops.ExclusiveOr((Int32)constLeft.Value, (Int32)constRight.Value)));

                        case PythonOperator.LessThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) < 0)));

                        case PythonOperator.GreaterThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) > 0)));

                        case PythonOperator.LessThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) <= 0)));

                        case PythonOperator.GreaterThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) >= 0)));

                        case PythonOperator.Equals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) == 0)));

                        case PythonOperator.NotEquals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(Int32Ops.Compare((Int32)constLeft.Value, (Int32)constRight.Value) != 0)));
                        }
                    }
                    if (constLeft.Value.GetType() == typeof(Double))
                    {
                        switch (_op)
                        {
                        case PythonOperator.Add: return(new ConstantExpression(DoubleOps.Add((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.Subtract: return(new ConstantExpression(DoubleOps.Subtract((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.Power: return(new ConstantExpression(DoubleOps.Power((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.Multiply: return(new ConstantExpression(DoubleOps.Multiply((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.FloorDivide: return(new ConstantExpression(DoubleOps.FloorDivide((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.Divide: return(new ConstantExpression(DoubleOps.Divide((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.TrueDivide: return(new ConstantExpression(DoubleOps.TrueDivide((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.Mod: return(new ConstantExpression(DoubleOps.Mod((Double)constLeft.Value, (Double)constRight.Value)));

                        case PythonOperator.LessThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) < 0)));

                        case PythonOperator.GreaterThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) > 0)));

                        case PythonOperator.LessThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) <= 0)));

                        case PythonOperator.GreaterThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) >= 0)));

                        case PythonOperator.Equals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) == 0)));

                        case PythonOperator.NotEquals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(DoubleOps.Compare((Double)constLeft.Value, (Double)constRight.Value) != 0)));
                        }
                    }
                    if (constLeft.Value.GetType() == typeof(BigInteger))
                    {
                        switch (_op)
                        {
                        case PythonOperator.Add: return(new ConstantExpression(BigIntegerOps.Add((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.Subtract: return(new ConstantExpression(BigIntegerOps.Subtract((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.Power: return(new ConstantExpression(BigIntegerOps.Power((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.Multiply: return(new ConstantExpression(BigIntegerOps.Multiply((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.FloorDivide: return(new ConstantExpression(BigIntegerOps.FloorDivide((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.Divide: return(new ConstantExpression(BigIntegerOps.Divide((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.TrueDivide: return(new ConstantExpression(BigIntegerOps.TrueDivide((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.Mod: return(new ConstantExpression(BigIntegerOps.Mod((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.LeftShift: return(new ConstantExpression(BigIntegerOps.LeftShift((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.RightShift: return(new ConstantExpression(BigIntegerOps.RightShift((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.BitwiseAnd: return(new ConstantExpression(BigIntegerOps.BitwiseAnd((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.BitwiseOr: return(new ConstantExpression(BigIntegerOps.BitwiseOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.ExclusiveOr: return(new ConstantExpression(BigIntegerOps.ExclusiveOr((BigInteger)constLeft.Value, (BigInteger)constRight.Value)));

                        case PythonOperator.LessThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) < 0)));

                        case PythonOperator.GreaterThan: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) > 0)));

                        case PythonOperator.LessThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) <= 0)));

                        case PythonOperator.GreaterThanOrEqual: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) >= 0)));

                        case PythonOperator.Equals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) == 0)));

                        case PythonOperator.NotEquals: return(new ConstantExpression(ScriptingRuntimeHelpers.BooleanToObject(BigIntegerOps.Compare((BigInteger)constLeft.Value, (BigInteger)constRight.Value) != 0)));
                        }
                    }
                    if (constLeft.Value.GetType() == typeof(Complex))
                    {
                        switch (_op)
                        {
                        case PythonOperator.Add: return(new ConstantExpression(ComplexOps.Add((Complex)constLeft.Value, (Complex)constRight.Value)));

                        case PythonOperator.Subtract: return(new ConstantExpression(ComplexOps.Subtract((Complex)constLeft.Value, (Complex)constRight.Value)));

                        case PythonOperator.Power: return(new ConstantExpression(ComplexOps.Power((Complex)constLeft.Value, (Complex)constRight.Value)));

                        case PythonOperator.Multiply: return(new ConstantExpression(ComplexOps.Multiply((Complex)constLeft.Value, (Complex)constRight.Value)));

                        case PythonOperator.Divide: return(new ConstantExpression(ComplexOps.Divide((Complex)constLeft.Value, (Complex)constRight.Value)));

                        case PythonOperator.TrueDivide: return(new ConstantExpression(ComplexOps.TrueDivide((Complex)constLeft.Value, (Complex)constRight.Value)));
                        }
                    }

                    // *** END GENERATED CODE ***

                    #endregion

                    if (constLeft.Value.GetType() == typeof(string) && _op == PythonOperator.Add)
                    {
                        return(new ConstantExpression((string)constLeft.Value + (string)constRight.Value));
                    }
                }
                else if (_op == PythonOperator.Multiply && constLeft != null && constRight != null)
                {
                    // 10000 check is to avoid creating massive strings in IL - there's a limit to the number of
                    // bytes of strings we get to have so we don't want to use them all up.
                    if (constLeft.Value.GetType() == typeof(string) && constRight.Value.GetType() == typeof(int))
                    {
                        var res = StringOps.Multiply((string)constLeft.Value, (int)constRight.Value);
                        if (res.Length < MaximumInlineStringLength)
                        {
                            return(new ConstantExpression(res));
                        }
                    }
                    else if (constLeft.Value.GetType() == typeof(int) && constRight.Value.GetType() == typeof(string))
                    {
                        var res = StringOps.Multiply((string)constRight.Value, (int)constLeft.Value);
                        if (res.Length < MaximumInlineStringLength)
                        {
                            return(new ConstantExpression(res));
                        }
                    }
                }
            } catch (ArithmeticException) {
            }

            return(null);
        }
示例#3
0
 public override int Run(InterpretedFrame frame)
 {
     frame.Push(ScriptingRuntimeHelpers.BooleanToObject(LightExceptions.IsLightException(frame.Pop())));
     return(+1);
 }
示例#4
0
 private static object LessThanOrEqualResult(int?comparisonResult)
 {
     return((comparisonResult.HasValue) ? ScriptingRuntimeHelpers.BooleanToObject(comparisonResult.Value <= 0) : null);
 }
示例#5
0
 private static object GreaterThenResult(int?comparisonResult)
 {
     return((comparisonResult.HasValue) ? ScriptingRuntimeHelpers.BooleanToObject(comparisonResult.Value > 0) : null);
 }
 public override int Run(InterpretedFrame frame)
 {
     // unfortunately Type.IsInstanceOfType() is 35-times slower than "is T" so we use generic code:
     frame.Push(ScriptingRuntimeHelpers.BooleanToObject(frame.Pop() is T));
     return(+1);
 }
示例#7
0
            /// <summary>
            /// Called for fields which have been limited to a range of bits.  Given the
            /// value for the full type this extracts the individual bits.
            /// </summary>
            private object ExtractBits(CData instance)
            {
                Debug.Assert(_bits > 0);
                Debug.Assert(_bits < sizeof(ulong) * 8); // Avoid unspecified shifts: ECMA 335: III.3.58-59
                Debug.Assert(instance != null);

                object value;

                if (IsBoolType)
                {
                    int valueBits = instance.MemHolder.ReadByte(offset);
                    int ibVal     = ExtractBitsFromInt(valueBits);
                    value = ScriptingRuntimeHelpers.BooleanToObject(ibVal != 0);
                }
                else
                {
                    value = _fieldType.GetValue(instance.MemHolder, instance, _offset, false);
                    if (value is int iVal)
                    {
                        iVal  = ExtractBitsFromInt(iVal);
                        value = ScriptingRuntimeHelpers.Int32ToObject(iVal);
                    }
                    else if (value is BigInteger biVal)
                    {
                        ulong validBits = (1UL << _bits) - 1;

                        ulong bits;
                        if (IsSignedType)
                        {
                            bits = (ulong)(long)biVal;
                        }
                        else
                        {
                            bits = (ulong)biVal;
                        }

                        bits = (bits >> _bitsOffset) & validBits;

                        if (IsSignedType)
                        {
                            // need to sign extend if high bit is set
                            if ((bits & (1UL << (_bits - 1))) != 0)
                            {
                                bits |= ulong.MaxValue ^ validBits;
                            }
                            value = (BigInteger)(long)bits;
                        }
                        else
                        {
                            value = (BigInteger)bits;
                        }
                    }
                    else
                    {
                        throw new InvalidOperationException("we only return int, bigint from GetValue");
                    }
                }
                return(value);

                int ExtractBitsFromInt(int iVal)
                {
                    int validBits = ((1 << _bits) - 1);

                    iVal = (iVal >> _bitsOffset) & validBits;
                    if (IsSignedType)
                    {
                        // need to sign extend if high bit is set
                        if ((iVal & (1 << (_bits - 1))) != 0)
                        {
                            iVal |= (-1) ^ validBits;
                        }
                    }

                    return(iVal);
                }
            }