Exemplo n.º 1
0
        private DynamicMetaObject BindConvert(DynamicMetaObject self)
        {
            PerfTrack.NoteEvent(PerfTrack.Categories.Binding, "Convert " + Type.FullName + " " + self.LimitType);
            PerfTrack.NoteEvent(PerfTrack.Categories.BindingTarget, "Conversion");

            DynamicMetaObject res;

#if !SILVERLIGHT
            DynamicMetaObject comConvert;
            if (Microsoft.Scripting.ComInterop.ComBinder.TryConvert(CompatBinder, self, out comConvert))
            {
                res = comConvert;
            }
            else
#endif
            {
                res = self.BindConvert(CompatBinder);
            }

            // if we return object and the interop binder had to put on an extra conversion
            // to the strong type go ahead and remove it now.
            if (ReturnType == typeof(object) &&
                res.Expression.Type != typeof(object) &&
                res.Expression.NodeType == ExpressionType.Convert)
            {
                res = new DynamicMetaObject(
                    ((UnaryExpression)res.Expression).Operand,
                    res.Restrictions
                    );
            }

            return(res);
        }
            public static void TestMetaObject(DynamicMetaObject target, Type type, bool isValid = true)
            {
                ConvertBinder     binder = new TestConvertBinder(type);
                DynamicMetaObject result = target.BindConvert(binder);

                Assert.IsNotNull(result);

                // Convert expression
                UnaryExpression expression = result.Expression as UnaryExpression;

                Assert.IsNotNull(expression);
                Assert.AreEqual <Type>(binder.Type, expression.Type);

                if (isValid)
                {
                    MethodCallExpression methodCallExp = expression.Operand as MethodCallExpression;

                    if (methodCallExp != null)
                    {
                        Assert.IsTrue(methodCallExp.Method.ToString().Contains("CastValue"));
                    }
                    else
                    {
                        ParameterExpression paramExpression = expression.Operand as ParameterExpression;
                        Assert.IsNotNull(paramExpression);
                    }
                }
                else
                {
                    Expression <Action> throwExp = Expression.Lambda <Action>(Expression.Block(expression), new ParameterExpression[] { });
                    ExceptionTestHelper.ExpectException <InvalidCastException>(() => throwExp.Compile().Invoke());
                }
            }
Exemplo n.º 3
0
        /// <summary>
        /// Performs the binding of the dynamic convert operation.
        /// </summary>
        /// <param name="target">The target of the dynamic convert operation.</param>
        /// <param name="args">An array of arguments of the dynamic convert operation.</param>
        /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
        public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            ContractUtils.RequiresNotNull(target, "target");
            ContractUtils.Requires(args == null || args.Length == 0, "args");

            return(target.BindConvert(this));
        }
Exemplo n.º 4
0
 public override DynamicMetaObject BindConvert(ConvertBinder binder)
 {
     return(AddRestrictions(_metaForwardee.BindConvert(binder)));
 }
        private static DynamicMetaObject MakeEnumeratorOperation(PythonOperationBinder operation, DynamicMetaObject self) {
            if (self.GetLimitType() == typeof(string)) {
                self = self.Restrict(self.GetLimitType());

                return new DynamicMetaObject(
                    Expression.Call(
                        typeof(PythonOps).GetMethod("StringEnumerator"),
                        self.Expression
                    ),
                    self.Restrictions
                );
            } else if (self.GetLimitType() == typeof(PythonDictionary)) {
                self = self.Restrict(self.GetLimitType());

                return new DynamicMetaObject(
                    Expression.Call(
                        typeof(PythonOps).GetMethod("MakeDictionaryKeyEnumerator"),
                        self.Expression
                    ),
                    self.Restrictions
                );
            } else if (self.Value is IEnumerable ||
                       typeof(IEnumerable).IsAssignableFrom(self.GetLimitType())) {
                self = self.Restrict(self.GetLimitType());

                return new DynamicMetaObject(
                    Expression.Call(
                        Expression.Convert(
                            self.Expression,
                            typeof(IEnumerable)
                        ),
                        typeof(IEnumerable).GetMethod("GetEnumerator")
                    ),
                    self.Restrictions
                );

            } else if (self.Value is IEnumerator ||                                 // check for COM object (and fast check when we have values)
                       typeof(IEnumerator).IsAssignableFrom(self.GetLimitType())) { // check if we don't have a value
                DynamicMetaObject ieres = self.Restrict(self.GetLimitType());

#if !SILVERLIGHT
                if (ComOps.IsComObject(self.Value)) {
                    ieres = new DynamicMetaObject(
                         self.Expression,
                         ieres.Restrictions.Merge(
                            BindingRestrictions.GetExpressionRestriction(
                                Ast.TypeIs(self.Expression, typeof(IEnumerator))
                            )
                        )
                    );
                }
#endif

                return ieres;
            }

            ParameterExpression tmp = Ast.Parameter(typeof(IEnumerator), "enum");
            DynamicMetaObject res = self.BindConvert(new ConversionBinder(BinderState.GetBinderState(operation), typeof(IEnumerator), ConversionResultKind.ExplicitTry));
            return new DynamicMetaObject(                
                Expression.Block(
                    new[] { tmp },
                    Ast.Condition(
                        Ast.NotEqual(
                            Ast.Assign(tmp, res.Expression),
                            AstUtils.Constant(null)
                        ),
                        tmp,
                        Ast.Call(
                            typeof(PythonOps).GetMethod("ThrowTypeErrorForBadIteration"),
                            BinderState.GetCodeContext(operation),
                            self.Expression
                        )
                    )
                ),
                res.Restrictions
            );
        }
Exemplo n.º 6
0
 public override DynamicMetaObject BindConvert(ConvertBinder binder)
 {
     return(scriptItem.PostProcessBindResult(metaDynamic.BindConvert(binder)));
 }
Exemplo n.º 7
0
 public override DynamicMetaObject BindConvert(ConvertBinder binder) => _innerMetaObject.BindConvert(binder);
Exemplo n.º 8
0
 public override DynamicMetaObject BindConvert(ConvertBinder binder)
 {
     return(_innerMetaObject.BindConvert(binder));
 }
            public static void TestBindParams(DynamicMetaObject target)
            {
                ConvertBinder binder = new TestConvertBinder(typeof(int));

                ExceptionTestHelper.ExpectException <ArgumentNullException>(() => { var result = target.BindConvert(null); });
            }
Exemplo n.º 10
0
                public override DynamicMetaObject BindConvert(ConvertBinder binder)
                {
                    var result = _default.BindConvert(binder);

                    return(result);
                }
Exemplo n.º 11
0
        /// <summary>
        /// Performs the binding of the dynamic convert operation.
        /// </summary>
        /// <param name="target">The target of the dynamic convert operation.</param>
        /// <param name="args">An array of arguments of the dynamic convert operation.</param>
        /// <returns>The <see cref="DynamicMetaObject"/> representing the result of the binding.</returns>
        public sealed override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args) {
            ContractUtils.RequiresNotNull(target, "target");
            ContractUtils.Requires(args == null || args.Length == 0, "args");

            return target.BindConvert(this);
        }
Exemplo n.º 12
0
 public override DynamicMetaObject BindConvert(ConvertBinder binder)
 {
     return(binder.FallbackConvert(_baseMetaObject, AddTypeRestrictions(_metaObject.BindConvert(binder))));
 }