Пример #1
0
        public override Dyn.DynamicMetaObject FallbackConvert(Dyn.DynamicMetaObject target, Dyn.DynamicMetaObject errorSuggestion)
        {
            if (!target.HasValue)
            {
                return Defer(target);
            }

            if (target.LimitType.IsSubclassOf(typeof(Any)))
            {
                return new Dyn.DynamicMetaObject(Expression.Convert(target.Expression, typeof(Any)), target.Restrictions.Merge(Dyn.BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)));
            }

            if (target.Value == null)
            {
                return new Dyn.DynamicMetaObject(Expression.Constant(Any.None, typeof(Any)), Dyn.BindingRestrictions.GetInstanceRestriction(target.Expression, null));
            }

            var targetExpression = target.Expression;
            if (targetExpression.Type != target.LimitType)
            {
                targetExpression = Expression.Convert(targetExpression, target.LimitType);
            }

            var restrictions = target.Restrictions
                .Merge(Dyn.BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType));

            var converter = converters[target.LimitType];

            return new Dyn.DynamicMetaObject(converter(targetExpression), restrictions);
        }
Пример #2
0
        public override DYN.DynamicMetaObject FallbackGetIndex(DYN.DynamicMetaObject target, DYN.DynamicMetaObject[] indexes, DYN.DynamicMetaObject errorSuggestion)
        {
            DLR.Expression expression;
            DYN.BindingRestrictions restriction;

            if (indexes.Length == 1 && indexes[0].HasValue && indexes[0].Value == null)
            {
                restriction = DYN.BindingRestrictions.GetExpressionRestriction(
                        DLR.Expression.Equal(indexes[0].Expression, DLR.Expression.Constant(null))
                );

                expression = target.Expression;
            }
            else
            {
                restriction = DYN.BindingRestrictions.GetTypeRestriction(
                    indexes[0].Expression, typeof(List<Types.AType>)
                );

                var indexer = DLR.Expression.Convert(indexes[0].Expression, typeof(List<Types.AType>));

                var rankCheck = DLR.Expression.IfThen(
                    DLR.Expression.LessThan(
                        DLR.Expression.Property(target.Expression, "Rank"),
                        DLR.Expression.Property(indexer, "Count")
                    ),
                    DLR.Expression.Throw(
                        DLR.Expression.New(typeof(Error.Rank).GetConstructor(new Type[] { typeof(string) }),
                    //DLR.Expression.Constant("[]")
                    // FIXME-LATER: This is just for debug, so remove it later:
                            DLR.Expression.Call(
                                DLR.Expression.Property(indexer, "Count"),
                                typeof(Int32).GetMethod("ToString", new Type[] { })
                            )
                        )
                    )
                );

                expression = DLR.Expression.Block(
                    rankCheck,
                    DLR.Expression.MakeIndex(
                        DLR.Expression.Convert(target.Expression, typeof(Types.AType)),
                        GetIndexBinder.AArrayIndexerProperty,
                        new DLR.Expression[] { indexer }
                    )
                );
            }

            DYN.DynamicMetaObject dynobj = new DYN.DynamicMetaObject(
                expression,
                DYN.BindingRestrictions.GetTypeRestriction(target.Expression, target.RuntimeType).Merge(restriction)
            );

            return dynobj;
        }
Пример #3
0
        /// <summary>
        /// Prints the list of contexts
        /// </summary>
        /// <param name="scope"></param>
        /// <returns></returns>
        public static AType PrintContexts(DYN.IDynamicMetaObjectProvider scope)
        {
            DYN.DynamicMetaObject storage = scope.GetMetaObject(
                DLR.Expression.Property(
                    DLR.Expression.Convert(DLR.Expression.Constant(scope), typeof(Scope)),
                    "Storage"
                )
            );

            Console.WriteLine(String.Join(" ", storage.GetDynamicMemberNames()));

            return Utils.ANull();
        }
Пример #4
0
        public override DYN.DynamicMetaObject FallbackConvert(DYN.DynamicMetaObject target, DYN.DynamicMetaObject errorSuggestion)
        {
            DYN.BindingRestrictions restriction = DYN.BindingRestrictions.GetTypeRestriction(
                target.Expression, target.RuntimeType
            );

            // Found an AType, simply return the expression
            // We check if it implements the AType interface
            if (target.RuntimeType.GetInterface("AType") != null)
            {
                DYN.DynamicMetaObject result = new DYN.DynamicMetaObject(
                    DLR.Expression.Convert(target.Expression, typeof(AType)),
                    restriction
                );

                return result;
            }
            else if(target.RuntimeType.Name == "Int32")
            {
                DYN.DynamicMetaObject result = new DYN.DynamicMetaObject(
                    // Create a new AInteger from the input value (need to convert to int first)
                    DLR.Expression.Call(
                        typeof(AInteger).GetMethod("Create", new Type[] { typeof(int) }),
                        DLR.Expression.Convert(target.Expression, typeof(int))
                    ),
                    restriction
                );

                // .New AplusCore.Types.AInteger((System.Int32)$$arg0)
                return result;
            }
            else if (target.RuntimeType.Name == "String")
            {
                DYN.DynamicMetaObject result = new DYN.DynamicMetaObject(
                    DLR.Expression.Convert(target.Expression, typeof(AType),
                        typeof(Helpers).GetMethod("BuildString")
                    ),
                    restriction
                );

                return result;
            }

            // TODO::
            throw new NotImplementedException();
        }
        public void Get_Dynamic_Parsed()
        {
            dynamic dyn = new
            {
                Property1 = 1M,
                Property2 = "val",
                Property3 = _now
            };

            var outDic = _digger.Get(dyn);
            Assert.That(outDic.Count, Is.EqualTo(3));
            Assert.That(outDic["Property1"].PropertyNameSource, Is.EqualTo(PropertyNameSource.Default));
            Assert.That(outDic["Property1"].Value, Is.EqualTo(1M));
            Assert.That(outDic["Property2"].PropertyNameSource, Is.EqualTo(PropertyNameSource.Default));
            Assert.That(outDic["Property2"].Value, Is.EqualTo("val"));
            Assert.That(outDic["Property3"].PropertyNameSource, Is.EqualTo(PropertyNameSource.Default));
            Assert.That(outDic["Property3"].Value, Is.EqualTo(_now));
        }
Пример #6
0
        public override DYN.DynamicMetaObject FallbackSetIndex(DYN.DynamicMetaObject target, DYN.DynamicMetaObject[] indexes, DYN.DynamicMetaObject value, DYN.DynamicMetaObject errorSuggestion)
        {
            DLR.Expression expression;
            DYN.BindingRestrictions restriction;

            // TODO: Fix this: add restirction to AType interface
            //// check if the target implements the AType interface
            //if (target.RuntimeType.GetInterface("AType") == null)
            //{
            //    dynobj = new DYN.DynamicMetaObject(
            //        DLR.Expression.Block(
            //            DLR.Expression.Throw(
            //                DLR.Expression.New(
            //                    typeof(Error.Mismatch).GetConstructor(new Type[] { typeof(string) }),
            //                    DLR.Expression.Constant(
            //                        String.Format("No indexing defined on '{0}'", target.RuntimeType.ToString())
            //                    )
            //                )
            //            ),
            //            value.Expression
            //        ),
            //        restriction
            //    );

            //    return dynobj;
            //}

            if (indexes.Length == 1 && indexes[0].HasValue && indexes[0].Value == null)
            {
                restriction = DYN.BindingRestrictions.GetExpressionRestriction(
                        DLR.Expression.Equal(indexes[0].Expression, DLR.Expression.Constant(null))
                );

                BindingFlags searchFlags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public;

                expression = DLR.Expression.Block(
                    DLR.Expression.Call(
                        null,
                        typeof(Utils).GetMethod("PerformAssign", searchFlags),
                        DLR.Expression.Convert(target.Expression, typeof(AType)),
                        DLR.Expression.Convert(value.Expression, typeof(AType))
                    ),
                    value.Expression
                );
            }
            else
            {
                // $target[$indexes] = $value;
                restriction = DYN.BindingRestrictions.GetTypeRestriction(
                    indexes[0].Expression, typeof(List<Types.AType>)
                );

                var indexer = DLR.Expression.Convert(indexes[0].Expression, typeof(List<Types.AType>));

                var rankCheck = DLR.Expression.IfThen(
                    DLR.Expression.LessThan(
                        DLR.Expression.Property(target.Expression, "Rank"),
                        DLR.Expression.Property(indexer, "Count")
                    ),
                    DLR.Expression.Throw(
                        DLR.Expression.New(typeof(Error.Rank).GetConstructor(new Type[] { typeof(string) }),
                    //DLR.Expression.Constant("[]")
                    // FIXME-LATER: This is just for debug, so remove it later:
                            DLR.Expression.Call(
                                DLR.Expression.Property(indexer, "Count"),
                                typeof(Int32).GetMethod("ToString", new Type[] { })
                            )
                        )
                    )
                );

                expression = DLR.Expression.Block(
                    typeof(Types.AType),
                    rankCheck,
                    DLR.Expression.Assign(
                        DLR.Expression.MakeIndex(
                            DLR.Expression.Convert(Tools.CloneMemoryMappedFile(target.Expression), typeof(Types.AType)),
                            SetIndexBinder.AArrayIndexerProperty,
                            new DLR.Expression[] { indexer }
                        ),
                        value.Expression
                    ),
                    value.Expression
                );
            }

            DYN.DynamicMetaObject dynobj = new DYN.DynamicMetaObject(
                expression, 
                DYN.BindingRestrictions.GetTypeRestriction(target.Expression, target.RuntimeType).Merge(restriction)
            );

            return dynobj;
        }
Пример #7
0
 public SetIndexBinder(DYN.CallInfo callinfo)
     : base(callinfo)
 {
 }
        public void Get_Dynamic_Parsed()
        {
            dynamic dyn = new
            {
                DecimalProperty = DecimalPropertyValue,
                StringProperty = StringPropertyValue,
                DateTimeProperty = DateTimePropertyValue
            };

            var properties = _digger.Get(dyn);
            CheckProperties(properties);
        }