Exemplo n.º 1
0
        /// <summary>
        /// Gets a see MemberAccess object that represents the instance, member name and other information on the member access expression.
        /// </summary>
        /// <param name="node">The Ast node associated with the member access operation</param>
        /// <param name="methods">The collection of registered methods on various types</param>
        /// <param name="obj">The object on which the member access is being performed.</param>
        /// <param name="memberName">The name of the member to get.</param>
        /// <returns></returns>
        public static MemberAccess GetLangBasicTypeMember(AstNode node, RegisteredMethods methods, LObject obj, string memberName)
        {
            var type = obj.Type;
            
            // Get the methods implementation LTypeMethods for this basic type 
            // e.g. string,  date,  time,  array , map
            // e.g. LStringType  LDateType, LTimeType, LArrayType, LMapType
            var typeMethods = methods.Get(type);
            var memberExists = typeMethods.HasMember(obj, memberName);

            // 1. Can set non-existing map properties
            if (type == LTypes.Map && !memberExists)
            {
                var nonExistMAccess = new MemberAccess(MemberMode.PropertyMember);
                nonExistMAccess.Name = type.Name;
                nonExistMAccess.Instance = obj;
                nonExistMAccess.MemberName = memberName;
                nonExistMAccess.Type = type;
                nonExistMAccess.MemberMissing = true;
                return nonExistMAccess;
            }

            // 2. Check that the member exists.
            if (!memberExists)
                throw ExceptionHelper.BuildRunTimeException(node, "Property or Member : " + memberName + " does not exist");

            // 3. It's either a Property or method
            var isProp = typeMethods.HasProperty(obj, memberName);
            var mode = isProp ? MemberMode.PropertyMember : MemberMode.MethodMember;
            var maccess = new MemberAccess(mode);
            maccess.Name = type.Name;
            maccess.Instance = obj;
            maccess.MemberName = memberName;
            maccess.Type = type;
            return maccess;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Evaluate the result of indexing an object e.g. users[0] or users["admins"]
        /// </summary>
        /// <param name="regmethods"></param>
        /// <param name="node"></param>
        /// <param name="target"></param>
        /// <param name="ndxObj"></param>
        /// <returns></returns>
        public static LObject AccessIndex(RegisteredMethods regmethods, AstNode node, LObject target, LObject ndxObj)
        {
            object result = LObjects.Null;
            // Case 1: Array access users[0];
            if (target.Type == LTypes.Array || target.Type.TypeVal == LTypes.Table.TypeVal)
            {
                var ndx = ((LNumber)ndxObj).Value;
                var isArray = target.Type == LTypes.Array;
                var methods = isArray ? regmethods.Get(LTypes.Array) : regmethods.Get(LTypes.Table);

                // TODO: Make this generic.
                var length = Convert.ToInt32(methods.ExecuteMethod(target, "length", null));
                if(ndx >= length)
                    throw ExceptionHelper.BuildRunTimeException(node, "Index out of bounds : '" + ndx + "'");

                result = methods.GetByNumericIndex(target, (int)ndx);
            }
            // Case 2: Map access. users["kishore"];
            else if (target.Type == LTypes.Map)
            {
                var memberName = ((LString)ndxObj).Value;
                var methods = regmethods.Get(LTypes.Map);
                if (!methods.HasProperty(target, memberName))
                    throw ExceptionHelper.BuildRunTimeException(node, "Property does not exist : '" + memberName + "'");

                result = methods.GetByStringMember(target, memberName);
            }
            // Conver to lang type.
            if(result != LObjects.Null && !(result is LObject))
            {
                result = LangTypeHelper.ConvertToLangValue(result);
            }
            return (LObject)result;
        }