Beispiel #1
0
        private object FuncInvoker <Arg1T, Arg2T, ResultT>(Closure closure, ExecutionNode[] argumentFns)
        {
            var arg1 = closure.Unbox <Arg1T>(argumentFns[0].Run(closure));
            var arg2 = closure.Unbox <Arg2T>(argumentFns[1].Run(closure));

            return(((Func <Arg1T, Arg2T, ResultT>) this.fn).Invoke(arg1, arg2));
        }
Beispiel #2
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var instance = closure.Unbox <object>(closure.Locals[LOCAL_OPERAND1]);

            if (this.bindingsByMember.Count == 0)
            {
                return(instance);
            }

            foreach (var bindings in this.bindingsByMember)
            {
                var member       = bindings.Key;
                var addTarget    = default(object);
                var fieldInfo    = member as FieldInfo;
                var propertyInfo = member as PropertyInfo;

                if (fieldInfo != null)
                {
                    if (fieldInfo.IsStatic == false && instance == null)
                    {
                        throw new NullReferenceException();
                    }
                    addTarget = fieldInfo.GetValue(instance);
                }
                else if (propertyInfo != null)
                {
                    var getMethod = propertyInfo.GetGetMethod(nonPublic: true);
                    if (getMethod.IsStatic == false && instance == null)
                    {
                        throw new NullReferenceException();
                    }

                    addTarget = propertyInfo.GetValue(instance, null);
                }
                else
                {
                    throw new InvalidOperationException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_INVALIDMEMBERFOREXPRESSION, member));
                }

                foreach (var bindGroup in bindings)
                {
                    var addMethod        = bindGroup.AddMethod;
                    var addArgumentNodes = bindGroup.AddMethodArguments;
                    var addArguments     = new object[addArgumentNodes.Length];
                    for (var i = 0; i < addArgumentNodes.Length; i++)
                    {
                        addArguments[i] = closure.Unbox <object>(addArgumentNodes[i].Run(closure));
                    }

                    addMethod.Invoke(addTarget, addArguments);
                }
            }
            return(instance);
        }
Beispiel #3
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var left = this.leftNode.Run(closure);

            // shortcut for and-also(&&) and or-else(||)
            if (this.shortcutLeftValue != null && Equals(this.shortcutLeftValue, closure.Unbox <object>(left)))
            {
                return(this.shortcutLeftValue);
            }

            var right = this.rightNode.Run(closure);

            if (this.isNullable && (left == null || right == null))
            {
                left  = closure.Unbox <object>(left);
                right = closure.Unbox <object>(right);

                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (this.binaryExpression.NodeType)
                {
                case ExpressionType.Equal: return(ReferenceEquals(left, right) ? Constants.TrueObject : Constants.FalseObject);

                case ExpressionType.NotEqual: return(ReferenceEquals(left, right) ? Constants.FalseObject : Constants.TrueObject);

                case ExpressionType.GreaterThan:
                case ExpressionType.GreaterThanOrEqual:
                case ExpressionType.LessThan:
                case ExpressionType.LessThanOrEqual: return(Constants.FalseObject);

                // C# Specs -> 7.11.4 Nullable boolean logical operators
                case ExpressionType.And:
                    if (Equals(left, Constants.FalseObject) || Equals(right, Constants.FalseObject))
                    {
                        return(Constants.FalseObject);
                    }
                    goto default;

                case ExpressionType.Or:
                    if (Equals(left, Constants.TrueObject) || Equals(right, Constants.TrueObject))
                    {
                        return(Constants.TrueObject);
                    }
                    goto default;

                default:
                    return(null);
                }
            }

            return(Intrinsic.InvokeBinaryOperation(closure, left, right, this.binaryExpression.NodeType, this.operation));
        }
        public static Func <Arg1T, Arg2T, Arg3T, ResultT> PrepareFunc <Arg1T, Arg2T, Arg3T, ResultT>(Expression body, ReadOnlyCollection <ParameterExpression> parameters)
        {
            if (body == null)
            {
                throw new ArgumentNullException("body");
            }
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }

            var collector = new ConstantsCollector();

            collector.Visit(body);

            var constExpressions     = collector.Constants.ToArray();
            var parameterExpressions = parameters.ToArray();
            var compiledBody         = Compile(body, constExpressions, parameterExpressions);
            var constants            = ArrayUtils.ConvertAll(constExpressions, c => c.Value);

            return((arg1, arg2, arg3) =>
            {
                var locals = new object[] { null, null, null, arg1, arg2, arg3 };
                var closure = new Closure(constants, locals);

                var result = closure.Unbox <ResultT>(compiledBody.Run(closure));
                Array.Clear(locals, 0, locals.Length);
                return result;
            });
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var instance = closure.Unbox <object>(this.newNode.Run(closure));

            if (ReferenceEquals(this.memberAssignmentNode, MemberAssignmentsNode.Empty) == false)
            {
                closure.Locals[LOCAL_OPERAND1] = instance;
                this.memberAssignmentNode.Run(closure);
            }

            if (ReferenceEquals(this.listBindingNode, MemberListBindingsNode.Empty) == false)
            {
                closure.Locals[LOCAL_OPERAND1] = instance;
                this.listBindingNode.Run(closure);
            }

            if (ReferenceEquals(this.memberMemberBindingNode, MemberMemberBindingsNode.Empty) == false)
            {
                closure.Locals[LOCAL_OPERAND1] = instance;
                this.memberMemberBindingNode.Run(closure);
            }

            closure.Locals[LOCAL_OPERAND1] = null;

            return(instance);
        }
Beispiel #6
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var testValue = closure.Unbox <bool>(this.conditionTestNode.Run(closure));
            var value     = testValue ? this.trueBranchNode.Run(closure) : this.falseBranchNode.Run(closure);

            return(value);
        }
Beispiel #7
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var initializationValues = new object[this.initializationValueNodes.Length];

            for (var i = 0; i < initializationValues.Length; i++)
            {
                initializationValues[i] = closure.Unbox <object>(this.initializationValueNodes[i].Run(closure));
            }

            var constructorArguments = EmptyArguments;

            if (this.constructorParametersCount > 0)
            {
                constructorArguments = new object[this.constructorParametersCount];
                Array.Copy(initializationValues, constructorArguments, this.constructorParametersCount);
            }

            var newInstance = this.isNullableType ? null : Activator.CreateInstance(this.newExpression.Type, constructorArguments);

            if (newInstance == null)
            {
                throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.newExpression));
            }

            return(newInstance);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var targetDelegate  = closure.Unbox <Delegate>(this.target.Run(closure));
            var invokeArguments = new object[this.argumentNodes.Length];

            for (var i = 0; i < invokeArguments.Length; i++)
            {
                invokeArguments[i] = closure.Unbox <object>(this.argumentNodes[i].Run(closure));
            }

            if (targetDelegate == null)
            {
                throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.invocationExpression.Expression));
            }

            return(targetDelegate.DynamicInvoke(invokeArguments));
        }
Beispiel #9
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var target = closure.Unbox <object>(this.targetNode.Run(closure));

            if (target == null)
            {
                return(Constants.FalseObject);
            }

            return(this.typeBinaryExpression.TypeOperand.IsInstanceOfType(target) ? Constants.TrueObject : Constants.FalseObject);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var target = closure.Unbox <Array>(this.targetNode.Run(closure));

            if (target == null)
            {
                throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.unaryExpression.Operand));
            }

            return(closure.Box(target.Length));
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var target = closure.Unbox <object>(this.targetNode.Run(closure));

            if (target == null)
            {
                return(Constants.FalseObject);
            }

            return(this.targetType.IsAssignableFrom(target.GetType()) ? Constants.TrueObject : Constants.FalseObject);
        }
Beispiel #12
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            if (this.methodCallNode != null)
            {
                return(this.methodCallNode.Run(closure));
            }
            else
            {
                var target = closure.Unbox <Array>(this.targetNode.Run(closure));

                if (target == null)
                {
                    throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.expression));
                }

                var index = this.indexNode.Run(closure);
                return(closure.Is <int[]>(index)
                                        ? target.GetValue(closure.Unbox <int[]>(index))
                                        : target.GetValue(closure.Unbox <int>(index)));
            }
        }
Beispiel #13
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var initializationValues = new object[this.initializationValueNodes.Length];

            for (var i = 0; i < initializationValues.Length; i++)
            {
                initializationValues[i] = closure.Unbox <object>(this.initializationValueNodes[i].Run(closure));
            }

            var constructorArguments = EmptyArguments;

            if (this.constructorParametersCount > 0)
            {
                constructorArguments = new object[this.constructorParametersCount];
                Array.Copy(initializationValues, constructorArguments, this.constructorParametersCount);
            }

            var newInstance = this.isNullableType ? null : Activator.CreateInstance(this.newExpression.Type, constructorArguments);

            if (this.newExpression.Members == null)
            {
                return(newInstance);
            }

            if (newInstance == null)
            {
                throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.newExpression));
            }

            for (var j = 0; j < this.newExpression.Members.Count; j++)
            {
                var member       = this.newExpression.Members[j];
                var fieldInfo    = member as FieldInfo;
                var propertyInfo = member as PropertyInfo;
                var value        = initializationValues[constructorArguments.Length + j];

                if (fieldInfo != null)
                {
                    fieldInfo.SetValue(newInstance, value);
                }
                else if (propertyInfo != null)
                {
                    propertyInfo.SetValue(newInstance, value, null);
                }
                else
                {
                    throw new InvalidOperationException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_INVALIDMEMBERFOREXPRESSION, member));
                }
            }

            return(newInstance);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            // ReSharper disable once AssignNullToNotNullAttribute
            var array = Array.CreateInstance(this.newArrayExpression.Type.GetElementType(), this.initializationValueNodes.Length);

            for (var i = 0; i < this.initializationValueNodes.Length; i++)
            {
                var initializationValue = this.initializationValueNodes[i].Run(closure);
                array.SetValue(closure.Unbox <object>(initializationValue), i);
            }

            return(array);
        }
Beispiel #15
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var left = closure.Unbox <object>(this.leftNode.Run(closure));

            if (left != null)
            {
                return(left);
            }
            else
            {
                return(this.rightNode.Run(closure));
            }
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var instance = closure.Unbox <object>(closure.Locals[LOCAL_OPERAND1]);

            if (this.memberAssignments.Length == 0)
            {
                return(instance);
            }

            foreach (var assignFn in this.memberAssignments)
            {
                var member       = assignFn.Member;
                var valueFn      = assignFn.ValueNode;
                var value        = closure.Unbox <object>(valueFn.Run(closure));
                var fieldInfo    = member as FieldInfo;
                var propertyInfo = member as PropertyInfo;

                if (instance == null)
                {
                    throw new NullReferenceException();
                }

                if (fieldInfo != null)
                {
                    fieldInfo.SetValue(instance, value);
                }
                else if (propertyInfo != null)
                {
                    propertyInfo.SetValue(instance, value, null);
                }
                else
                {
                    throw new InvalidOperationException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_INVALIDMEMBERFOREXPRESSION, member));
                }
            }

            return(instance);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var ranks = new int[this.rankNodes.Length];

            for (var i = 0; i < this.rankNodes.Length; i++)
            {
                ranks[i] = closure.Unbox <int>(this.rankNodes[i].Run(closure));
            }

            // ReSharper disable once AssignNullToNotNullAttribute
            var array = Array.CreateInstance(this.newArrayExpression.Type.GetElementType(), ranks);

            return(array);
        }
Beispiel #18
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var list = closure.Unbox <object>(this.newNode.Run(closure));

            if (this.initializationNodes.Length == 0)
            {
                return(list);
            }

            foreach (var listInit in this.initializationNodes)
            {
                var addMethod     = listInit.Key;
                var argumentNodes = listInit.Value;
                var addArguments  = new object[argumentNodes.Length];

                for (var i = 0; i < argumentNodes.Length; i++)
                {
                    addArguments[i] = closure.Unbox <object>(argumentNodes[i].Run(closure));
                }

                addMethod.Invoke(list, addArguments);
            }
            return(list);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            if (this.convertNode != null)
            {
                return(this.convertNode.Run(closure));
            }

            var target = closure.Unbox <object>(this.targetNode.Run(closure));

            if (target == null)
            {
                return(null);
            }

            if (this.typeAsExpression.Type.IsInstanceOfType(target) == false)
            {
                return(null);
            }

            return(target);
        }
Beispiel #20
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            if (this.convertNode != null)
            {
                return(this.convertNode.Run(closure));
            }

            var target = closure.Unbox <object>(this.targetNode.Run(closure));

            if (target == null)
            {
                return(null);
            }

            if (this.targetType.IsAssignableFrom(target.GetType()) == false)
            {
                return(null);
            }

            return(target);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var target = closure.Unbox <object>(this.targetNode.Run(closure));

            if (this.isStatic == false && target == null)
            {
                throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.memberExpression.Expression));
            }

            if (this.fieldInfo != null)
            {
                return(this.fieldInfo.GetValue(target));
            }
            else if (this.propertyGetter != null)
            {
                return(this.propertyGetter.Invoke(target, null));
            }
            else
            {
                throw new InvalidOperationException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_INVALIDMEMBERFOREXPRESSION, this.memberExpression.Member));
            }
        }
Beispiel #22
0
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            if (this.fastCallInvoker != null)
            {
                return(this.fastCallInvoker(closure, this.argumentNodes));
            }
            else
            {
                var target = this.targetNode.Run(closure);

                if (this.isStatic == false && target == null)
                {
                    throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.methodCallExpression.Object));
                }

                var arguments = new object[this.argumentNodes.Length];
                for (var i = 0; i < arguments.Length; i++)
                {
                    arguments[i] = closure.Unbox <object>(this.argumentNodes[i].Run(closure));
                }

                return(this.methodCallExpression.Method.Invoke(target, arguments));
            }
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var target = closure.Unbox <object>(closure.Locals[LOCAL_OPERAND1]);

            if (this.bindingsByMember.Count == 0)
            {
                return(target);
            }

            foreach (var bindings in this.bindingsByMember)
            {
                var member       = bindings.Key;
                var fieldInfo    = member as FieldInfo;
                var propertyInfo = member as PropertyInfo;
                var bindTarget   = default(object);

                if (fieldInfo != null)
                {
                    if (fieldInfo.IsStatic == false && target == null)
                    {
                        throw new NullReferenceException();
                    }
                    bindTarget = fieldInfo.GetValue(target);
                }
                else if (propertyInfo != null)
                {
                    var getMethod = propertyInfo.GetGetMethod(nonPublic: true);
                    if (getMethod.IsStatic == false && target == null)
                    {
                        throw new NullReferenceException();
                    }

                    bindTarget = propertyInfo.GetValue(target, null);
                }
                else
                {
                    throw new InvalidOperationException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_INVALIDMEMBERFOREXPRESSION, member));
                }

                foreach (var bind in bindings)
                {
                    if (ReferenceEquals(bind.MemberAssignments, MemberAssignmentsNode.Empty) == false)
                    {
                        closure.Locals[LOCAL_OPERAND1] = bindTarget;
                        bind.MemberAssignments.Run(closure);
                    }

                    if (ReferenceEquals(bind.MemberListBindings, MemberListBindingsNode.Empty) == false)
                    {
                        closure.Locals[LOCAL_OPERAND1] = bindTarget;
                        bind.MemberListBindings.Run(closure);
                    }

                    if (ReferenceEquals(bind.MemberMemberBindings, Empty) == false)
                    {
                        closure.Locals[LOCAL_OPERAND1] = bindTarget;
                        bind.MemberMemberBindings.Run(closure);
                    }
                }
            }
            return(target);
        }
        /// <inheritdoc />
        public override object Run(Closure closure)
        {
            var operand = closure.Unbox <object>(this.operandNode.Run(closure));

            if (operand == null && (this.convertExpression.Type.IsValueType == false || this.isTargetTypeNullable))
            {
                return(null);
            }

            var operandType = closure.GetType(operand);
            var convertType = this.convertExpression.NodeType;

            if (convertType != ExpressionType.Convert)
            {
                convertType = ExpressionType.ConvertChecked;
            }

            // un-box
            if ((this.sourceType == typeof(object) || this.sourceType == typeof(ValueType) || this.sourceType.IsInterface) && this.targetType.IsValueType)
            {
                // null un-box
                if (operand == null)
                {
                    throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.convertExpression.Operand));
                }
                // type check for un-box
                if (operandType == this.targetType)
                {
                    return(operand);
                }
                throw new InvalidCastException();
            }
            // box
            else if (this.sourceType.IsValueType && (this.targetType == typeof(object) || this.targetType == typeof(ValueType) || this.targetType.IsInterface))
            {
                // type check for box
                return(this.targetType.IsAssignableFrom(operandType) ? operand : null);
            }
            // to enum
            else if (this.targetType.IsEnum && (this.sourceType == typeof(byte) ||
                                                this.sourceType == typeof(sbyte) ||
                                                this.sourceType == typeof(short) ||
                                                this.sourceType == typeof(ushort) ||
                                                this.sourceType == typeof(int) ||
                                                this.sourceType == typeof(uint) ||
                                                this.sourceType == typeof(long) ||
                                                this.sourceType == typeof(ulong)))
            {
                if (operand == null)
                {
                    throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.convertExpression.Operand));
                }

                operand = Intrinsic.InvokeConversion(closure, operand, Enum.GetUnderlyingType(this.targetType), this.convertExpression.NodeType, null);
                return(Enum.ToObject(this.targetType, closure.Unbox <object>(operand)));
            }
            // from enum
            else if (this.sourceType.IsEnum && (this.targetType == typeof(byte) ||
                                                this.targetType == typeof(sbyte) ||
                                                this.targetType == typeof(short) ||
                                                this.targetType == typeof(ushort) ||
                                                this.targetType == typeof(int) ||
                                                this.targetType == typeof(uint) ||
                                                this.targetType == typeof(long) ||
                                                this.targetType == typeof(ulong)))
            {
                if (operand == null)
                {
                    throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.convertExpression.Operand));
                }

                operand = Convert.ChangeType(closure.Unbox <object>(operand), Enum.GetUnderlyingType(this.sourceType));
                operand = Intrinsic.InvokeConversion(closure, operand, this.targetType, this.convertExpression.NodeType, null);
                return(operand);
            }
            // from nullable
            if (this.targetType.IsValueType && this.isSourceTypeNullable)
            {
                if (operand == null)
                {
                    throw new NullReferenceException(string.Format(Properties.Resources.EXCEPTION_EXECUTION_EXPRESSIONGIVESNULLRESULT, this.convertExpression.Operand));
                }

                operand = Intrinsic.InvokeConversion(closure, operand, this.targetType, this.convertExpression.NodeType, null);
            }
            else if (this.targetType.IsAssignableFrom(operandType))
            {
                return(operand);
            }

            return(Intrinsic.InvokeConversion(closure, operand, this.targetType, convertType, this.operation));
        }