Exemple #1
0
        public override object Visit(MethodNode obj)
        {
            object instance;

            if (obj.Variable != null)
            {
                instance = obj.Variable.Accept(this);
            }
            else
            {
                instance = _ctx.Resolve("this");//Note:treat it as part of host class
            }
            string methodName = (obj.Method as SimpleNode).Token.TokenValue;

            object[] paramArray = (object[])obj.Args.Accept(this);

            if (instance == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            ISmartInvoker   invoker;
            ClassDefination cdef = instance as ClassDefination;

            if (cdef != null)
            {
                invoker = EvalSmartInvoker.CreateInstance(cdef.ObjType, false);
            }
            else
            {
                invoker = EvalSmartInvoker.CreateInstance(instance, true);
            }
            return(invoker.Invoke(methodName, paramArray));
        }
Exemple #2
0
        public override object Visit(IndexerNode obj)
        {
            object instance = obj.Variable.Accept(this);

            object[] paramArray = (object[])obj.Args.Accept(this);

            if (instance == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            ISmartInvoker   invoker;
            ClassDefination cdef = instance as ClassDefination;

            if (cdef != null)
            {
                invoker = EvalSmartInvoker.CreateInstance(cdef.ObjType, false);
            }
            else
            {
                invoker = EvalSmartInvoker.CreateInstance(instance, true);
            }

            if (instance.GetType().IsArray)//Note:invoke "Get" on array
            {
                return(invoker.Invoke("Get", paramArray));
            }
            else//Note:invoke indexer on instance
            {
                try//Note:Try indexer first
                {
                    return(invoker.Invoke("get_Item", paramArray));
                }
                catch//Note:then try property
                {
                    if (paramArray.Length == 1 && paramArray[0] != null)
                    {
                        string     propertyName = paramArray[0].ToString();
                        IReflector r            = Reflector.Bind(instance);
                        if (r.ExistProperty(propertyName))
                        {
                            return(r.GetPropertyValue(propertyName));
                        }
                        else if (r.ExistField(propertyName))
                        {
                            return(r.GetFieldValue(propertyName));
                        }
                        else
                        {
                            ExceptionHelper.ThrowIndexOrPropertyNotExist(instance.GetType(), propertyName);
                        }
                    }
                    //Note:Can not convert to property, throw
                    throw;
                }
            }
        }
Exemple #3
0
        public override object Visit(PropertyNode obj)
        {
            object instance = obj.Variable.Accept(this);

            if (instance == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            string          propertyName = (obj.Property as SimpleNode).Token.TokenValue;
            IReflector      r;
            ClassDefination cdef = instance as ClassDefination;

            if (cdef != null)
            {
                r = Reflector.Bind(cdef.ObjType, ReflectorPolicy.CreateInstance(false, true, false, false));
            }
            else
            {
                r = Reflector.Bind(instance, ReflectorPolicy.CreateInstance(false, true, false, true));
            }

            if (r.ExistProperty(propertyName))//Note:Property first
            {
                return(r.GetPropertyValue(propertyName));
            }
            else if (r.ExistField(propertyName))//Note:Field 2nd
            {
                return(r.GetFieldValue(propertyName));
            }
            else//Note:Then try indexer
            {
                try
                {
                    return(r.Invoke("get_Item", new object[] { propertyName }));
                }
                catch
                {
                    //Note:Try indexer failed.
                    ExceptionHelper.ThrowIndexOrPropertyNotExist(instance.GetType(), propertyName);
                    return(null);
                }
            }
        }
Exemple #4
0
        public override object Visit(UnaryNode obj)
        {
            object val = obj.OperandNode.Accept(this);

            if (obj.Token.TokenValue == "!")
            {
                bool b = ScriptTypeUtil.EvalToBoolean(val);
                return(!b);
            }

            if (val == null)
            {
                ExceptionHelper.ThrowEvalNull();
            }

            if (obj.Token.TokenValue == "+")
            {
                return(val);
            }

            if (obj.Token.TokenValue == "-")
            {
                if (ScriptTypeUtil.IsDecimal(val))
                {
                    decimal result = ScriptTypeUtil.ConvertToDecimal(val);
                    return(-result);
                }

                if (ScriptTypeUtil.IsLong(val))
                {
                    long result = ScriptTypeUtil.ConvertToLong(val);
                    return(-result);
                }
            }

            ExceptionHelper.ThrowUnaryOperatorInvalid(obj.Token.TokenValue, val);
            return(null);
        }