Exemplo n.º 1
0
        /// <summary>
        /// = array[index]
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue GetValueFromIndex(StackValue array, int index, RuntimeCore runtimeCore)
        {
            Validity.Assert(array.IsArray || array.IsString);
            RuntimeMemory rmem = runtimeCore.RuntimeMemory;

            if (array.IsString)
            {
                string str = rmem.Heap.GetString(array);
                if (str == null)
                {
                    return(StackValue.Null);
                }

                if (index < 0)
                {
                    index = index + str.Length;
                }

                if (index >= str.Length || index < 0)
                {
                    runtimeCore.RuntimeStatus.LogWarning(ProtoCore.Runtime.WarningID.kOverIndexing, Resources.kArrayOverIndexed);
                    return(StackValue.Null);
                }

                return(rmem.Heap.AllocateString(str.Substring(index, 1)));
            }
            else
            {
                HeapElement he = GetHeapElement(array, runtimeCore);
                return(StackUtils.GetValue(he, index, runtimeCore));
            }
        }
Exemplo n.º 2
0
        public static StackValue SetDataAtIndices(StackValue array, int[] indices, StackValue data, Core core)
        {
            Validity.Assert(array.optype == AddressType.ArrayPointer || array.optype == AddressType.String);

            StackValue arrayItem = array;

            for (int i = 0; i < indices.Length - 1; ++i)
            {
                HeapElement arrayHeap = core.Heap.Heaplist[(int)arrayItem.opdata];
                int         index     = arrayHeap.ExpandByAcessingAt(indices[i]);
                arrayItem = arrayHeap.Stack[index];

                if (arrayItem.optype == AddressType.String)
                {
                    core.RuntimeStatus.LogWarning(RuntimeData.WarningID.kOverIndexing, ProtoCore.RuntimeData.WarningMessage.kStringOverIndexed);
                    return(StackUtils.BuildNull());
                }
                else if (arrayItem.optype != AddressType.ArrayPointer)
                {
                    StackValue sv = HeapUtils.StoreArray(new StackValue[] { arrayItem }, core);
                    GCUtils.GCRetain(sv, core);
                    arrayHeap.Stack[index] = sv;
                    arrayItem = arrayHeap.Stack[index];
                }
            }

            return(ArrayUtils.SetDataAtIndex(arrayItem, indices[indices.Length - 1], data, core));
        }
Exemplo n.º 3
0
        public override object Execute(ProtoCore.Runtime.Context c, Interpreter dsi)
        {
            Object retVal = base.Execute(c, dsi);

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

            StackValue propValue  = (StackValue)retVal;
            StackValue thisObject = dsi.runtime.rmem.Stack.Last();

            if (StackUtils.IsValidPointer(thisObject) && StackUtils.IsReferenceType(propValue))
            {
                int classIndex = (int)thisObject.metaData.type;
                if (classIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                {
                    var core    = dsi.runtime.Core;
                    int thisptr = (int)thisObject.opdata;

                    int        idx      = core.ClassTable.ClassNodes[classIndex].symbols.IndexOf(PropertyName);
                    StackValue oldValue = core.Heap.Heaplist[(int)thisObject.opdata].GetValue(idx, core);
                    if (!StackUtils.Equals(oldValue, propValue))
                    {
                        GCUtils.GCRetain(propValue, core);
                        core.Heap.Heaplist[thisptr].SetValue(idx, propValue);
                    }
                }
            }

            return(retVal);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get all keys from an array
        /// </summary>
        /// <param name="array"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue[] GetKeys(StackValue array, Core core)
        {
            Validity.Assert(StackUtils.IsArray(array));
            if (!StackUtils.IsArray(array))
            {
                return(null);
            }

            HeapElement       he   = GetHeapElement(array, core);
            List <StackValue> keys = new List <StackValue>();

            for (int i = 0; i < he.VisibleSize; ++i)
            {
                keys.Add(StackUtils.BuildInt(i));
            }

            if (he.Dict != null)
            {
                foreach (var key in he.Dict.Keys)
                {
                    keys.Add(key);
                }
            }

            return(keys.ToArray());
        }
Exemplo n.º 5
0
            public void Update()
            {
                var currentBuffCount = body.GetBuffCount(buffIndex);

                if (timeUntilCombatTimeout > 0)
                {
                    timeUntilCombatTimeout--;
                    timeInCombat++;
                    if (timeInCombat >= TICKS_PER_STACK)
                    {
                        timeInCombat = 0;
                        int maxStackQty = StackUtils.LinearStack(stack, MAX_BASE_DMG_BONUS, MAX_STACK_DMG_BONUS);
                        int addStackQty = Math.Min(StackUtils.LinearStack(stack, BASE_DMG_BONUS, STACK_DMG_BONUS), maxStackQty - currentBuffCount);
                        for (; addStackQty > 0; addStackQty--)
                        {
                            body.AddBuff(buffIndex);
                        }
                    }
                }
                else
                {
                    timeInCombat           = 0;
                    timeUntilCombatTimeout = 0;
                    while (currentBuffCount-- > 0)
                    {
                        body.RemoveBuff(buffIndex);
                    }
                }
            }
Exemplo n.º 6
0
        public static Dictionary <int, StackValue> GetTypeExamplesForLayer(StackValue array, Core core)
        {
            if (!StackUtils.IsArray(array))
            {
                Dictionary <int, StackValue> ret = new Dictionary <int, StackValue>();
                ret.Add((int)array.metaData.type, array);
                return(ret);
            }

            Dictionary <int, StackValue> usageFreq = new Dictionary <int, StackValue>();

            //This is the element on the heap that manages the data structure
            HeapElement heapElement = GetHeapElement(array, core);

            for (int i = 0; i < heapElement.VisibleSize; ++i)
            {
                StackValue sv = heapElement.Stack[i];
                if (!usageFreq.ContainsKey((int)sv.metaData.type))
                {
                    usageFreq.Add((int)sv.metaData.type, sv);
                }
            }

            return(usageFreq);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Generate type statistics for given layer of an array
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static Dictionary <ClassNode, int> GetTypeStatisticsForLayer(StackValue array, Core core)
        {
            if (!StackUtils.IsArray(array))
            {
                Dictionary <ClassNode, int> ret = new Dictionary <ClassNode, int>();
                ret.Add(core.DSExecutable.classTable.ClassNodes[(int)array.metaData.type], 1);
                return(ret);
            }

            Dictionary <ClassNode, int> usageFreq = new Dictionary <ClassNode, int>();

            //This is the element on the heap that manages the data structure
            HeapElement heapElement = GetHeapElement(array, core);

            for (int i = 0; i < heapElement.VisibleSize; ++i)
            {
                StackValue sv = heapElement.Stack[i];
                ClassNode  cn = core.DSExecutable.classTable.ClassNodes[(int)sv.metaData.type];
                if (!usageFreq.ContainsKey(cn))
                {
                    usageFreq.Add(cn, 0);
                }

                usageFreq[cn] = usageFreq[cn] + 1;
            }

            return(usageFreq);
        }
Exemplo n.º 8
0
        public override StackValue Marshal(object obj, ProtoCore.Runtime.Context context, Interpreter dsi, ProtoCore.Type type)
        {
            string     str     = (string)obj;
            StackValue dsarray = PrimitiveMarshler.ConvertCSArrayToDSArray(kCharMarshaler, str.ToCharArray(), context, dsi, type);

            return(StackUtils.BuildString(dsarray.opdata));
        }
Exemplo n.º 9
0
        /*
         * [Obsolete]
         * public static StackValue CoerceArray(StackValue array, Type typ, Core core)
         * {
         *  //@TODO(Luke) handle array rank coersions
         *
         *  Validity.Assert(IsArray(array), "Argument needs to be an array {99FB71A6-72AD-4C93-8F1E-0B1F419C1A6D}");
         *
         *  //This is the element on the heap that manages the data structure
         *  HeapElement heapElement = GetHeapElement(array, core);
         *  StackValue[] newSVs = new StackValue[heapElement.VisibleSize];
         *
         *  for (int i = 0; i < heapElement.VisibleSize; ++i)
         *  {
         *      StackValue sv = heapElement.Stack[i];
         *      StackValue coercedValue;
         *
         *      if (IsArray(sv))
         *      {
         *          Type typ2 = new Type();
         *          typ2.UID = typ.UID;
         *          typ2.rank = typ.rank - 1;
         *          typ2.IsIndexable = (typ2.rank == -1 || typ2.rank > 0);
         *
         *          coercedValue = CoerceArray(sv, typ2, core);
         *      }
         *      else
         *      {
         *          coercedValue = TypeSystem.Coerce(sv, typ, core);
         *      }
         *
         *      GCUtils.GCRetain(coercedValue, core);
         *      newSVs[i] = coercedValue;
         *  }
         *
         *  return HeapUtils.StoreArray(newSVs, core);
         * }
         */


        /// <summary>
        /// Retrieve the first non-array element in an array
        /// </summary>
        /// <param name="svArray"></param>
        /// <param name="sv"></param>
        /// <param name="core"></param>
        /// <returns> true if the element was found </returns>
        public static bool GetFirstNonArrayStackValue(StackValue svArray, ref StackValue sv, Core core)
        {
            if (AddressType.ArrayPointer != svArray.optype)
            {
                return(false);
            }

            int ptr = (int)svArray.opdata;


            if (null == core.Rmem.Heap.Heaplist[ptr].Stack || core.Rmem.Heap.Heaplist[ptr].Stack.Length == 0)
            {
                return(false);
            }

            while (StackUtils.IsArray(core.Rmem.Heap.Heaplist[ptr].Stack[0]))
            {
                ptr = (int)core.Rmem.Heap.Heaplist[ptr].Stack[0].opdata;

                // Handle the case where the array is valid but empty
                if (core.Rmem.Heap.Heaplist[ptr].Stack.Length == 0)
                {
                    return(false);
                }
            }

            sv.optype   = core.Rmem.Heap.Heaplist[ptr].Stack[0].optype;
            sv.opdata   = core.Rmem.Heap.Heaplist[ptr].Stack[0].opdata;
            sv.metaData = core.Rmem.Heap.Heaplist[ptr].Stack[0].metaData;
            return(true);
        }
Exemplo n.º 10
0
 public override void RegisterHooks(ItemIndex itemIndex)
 {
     On.RoR2.HealthComponent.TakeDamage += (orig, self, damageInfo) =>
     {
         if ((self.body.isElite || self.body.isBoss) && damageInfo.attacker)
         {
             var attackerBody = damageInfo.attacker.GetComponent <CharacterBody>();
             if (attackerBody != null && attackerBody && attackerBody.master && attackerBody.master.inventory)
             {
                 int itemCount = attackerBody.master.inventory.GetItemCount(itemIndex);
                 if (itemCount > 0)
                 {
                     if (self.body.isElite)
                     {
                         damageInfo.damage *= 1 + StackUtils.LinearStack(itemCount, ELITE_DMG_BASE, ELITE_DMG_STACK);
                     }
                     else if (self.body.isBoss)
                     {
                         damageInfo.damage *= StackUtils.ExponentialStack(itemCount, BOSS_DMG_BASE, BOSS_DMG_STACK);
                     }
                 }
             }
         }
         orig(self, damageInfo);
     };
 }
Exemplo n.º 11
0
 public override void RegisterHooks(ItemIndex itemIndex)
 {
     On.RoR2.HealthComponent.TakeDamage += (orig, self, damageInfo) =>
     {
         if (damageInfo.attacker)
         {
             var attackerBody = damageInfo.attacker.GetComponent <CharacterBody>();
             if (attackerBody != null && attackerBody && attackerBody.master && attackerBody.master.inventory)
             {
                 int itemCount = attackerBody.master.inventory.GetItemCount(itemIndex);
                 if (itemCount > 0)
                 {
                     float minDamageForProc = StackUtils.ExponentialStack(itemCount, HP_LIMIT, HP_LIMIT_ADJ) * self.body.maxHealth;
                     float instakillChance  = StackUtils.HyperbolicStack(itemCount, BASE_PERC, ADD_PERC);
                     float roll             = UnityEngine.Random.Range(0f, 1f);
                     if (roll < instakillChance && damageInfo.damage >= minDamageForProc)
                     {
                         self.body.master.TrueKill();
                         return;
                     }
                 }
             }
         }
         orig(self, damageInfo);
     };
 }
Exemplo n.º 12
0
        /// <summary>
        /// = array[index1][index2][...][indexN], and
        /// indices = {index1, index2, ..., indexN}
        ///
        /// Note this function doesn't support the replication of array indexing.
        /// </summary>
        /// <param name="array"></param>
        /// <param name="indices"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue GetValueFromIndices(StackValue array, StackValue[] indices, Core core)
        {
            Validity.Assert(StackUtils.IsArray(array) || StackUtils.IsString(array));
            for (int i = 0; i < indices.Length - 1; ++i)
            {
                if (!StackUtils.IsArray(array) && !StackUtils.IsString(array))
                {
                    core.RuntimeStatus.LogWarning(WarningID.kOverIndexing, WarningMessage.kArrayOverIndexed);
                    return(StackUtils.BuildNull());
                }

                StackValue index = indices[i];
                if (StackUtils.IsNumeric(index))
                {
                    index = index.AsInt();
                    array = GetValueFromIndex(array, (int)index.opdata, core);
                }
                else
                {
                    if (array.optype != AddressType.ArrayPointer)
                    {
                        core.RuntimeStatus.LogWarning(WarningID.kOverIndexing, WarningMessage.kArrayOverIndexed);
                        return(StackUtils.BuildNull());
                    }
                    array = GetValueFromIndex(array, index, core);
                }
            }

            return(GetValueFromIndex(array, indices[indices.Length - 1], core));
        }
Exemplo n.º 13
0
        /// <summary>
        /// array[index] = value. Here index can be any type.
        ///
        /// Note this function doesn't support the replication of array indexing.
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        /// <param name="value"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue SetValueForIndex(StackValue array, StackValue index, StackValue value, Core core)
        {
            Validity.Assert(StackUtils.IsArray(array) || StackUtils.IsString(array));

            if (StackUtils.IsNumeric(index))
            {
                index = index.AsInt();
                return(SetValueForIndex(array, (int)index.opdata, value, core));
            }
            else
            {
                HeapElement he = GetHeapElement(array, core);
                if (he.Dict == null)
                {
                    he.Dict = new Dictionary <StackValue, StackValue>(new StackValueComparer(core));
                }

                StackValue oldValue;
                if (!he.Dict.TryGetValue(index, out oldValue))
                {
                    oldValue = StackUtils.BuildNull();
                }

                GCUtils.GCRetain(index, core);
                GCUtils.GCRetain(value, core);
                he.Dict[index] = value;

                return(oldValue);
            }
        }
Exemplo n.º 14
0
        public override object Execute(ProtoCore.Runtime.Context c, Interpreter dsi, List <StackValue> s)
        {
            Object retVal = base.Execute(c, dsi, s);

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

            StackValue propValue  = (StackValue)retVal;
            var        thisObject = s?.Last() ?? dsi.runtime.rmem.Stack.Last();

            bool isValidPointer = thisObject.IsPointer && thisObject.Pointer != Constants.kInvalidIndex;

            if (isValidPointer && propValue.IsReferenceType)
            {
                int classIndex = thisObject.metaData.type;
                if (classIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                {
                    var runtimeCore = dsi.runtime.RuntimeCore;
                    int idx         = runtimeCore.DSExecutable.classTable.ClassNodes[classIndex].Symbols.IndexOf(PropertyName);

                    var        obj      = runtimeCore.Heap.ToHeapObject <DSObject>(thisObject);
                    StackValue oldValue = obj.GetValueFromIndex(idx, runtimeCore);
                    if (!StackUtils.Equals(oldValue, propValue))
                    {
                        obj.SetValueAtIndex(idx, propValue, runtimeCore);
                    }
                }
            }

            return(retVal);
        }
Exemplo n.º 15
0
        public override object Execute(ProtoCore.Runtime.Context c, Interpreter dsi)
        {
            Object retVal = base.Execute(c, dsi);

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

            StackValue propValue  = (StackValue)retVal;
            StackValue thisObject = dsi.runtime.rmem.Stack.Last();

            bool isValidPointer = thisObject.IsPointer && thisObject.opdata != Constants.kInvalidIndex;

            if (isValidPointer && propValue.IsReferenceType)
            {
                int classIndex = thisObject.metaData.type;
                if (classIndex != ProtoCore.DSASM.Constants.kInvalidIndex)
                {
                    var        runtimeCore = dsi.runtime.RuntimeCore;
                    int        idx         = runtimeCore.DSExecutable.classTable.ClassNodes[classIndex].symbols.IndexOf(PropertyName);
                    StackValue oldValue    = dsi.runtime.rmem.Heap.GetHeapElement(thisObject).GetValue(idx, runtimeCore);
                    if (!StackUtils.Equals(oldValue, propValue))
                    {
                        dsi.runtime.rmem.Heap.GetHeapElement(thisObject).SetValue(idx, propValue);
                    }
                }
            }

            return(retVal);
        }
Exemplo n.º 16
0
            public StackValue GetData(int blockId, int symbolindex, int scope)
            {
                MemoryRegion region = DSASM.MemoryRegion.kInvalidRegion;

                if (Constants.kGlobalScope == scope)
                {
                    region = Executable.runtimeSymbols[blockId].symbolList[symbolindex].memregion;
                }
                else
                {
                    region = ClassTable.ClassNodes[scope].symbols.symbolList[symbolindex].memregion;
                }

                if (MemoryRegion.kMemStack == region)
                {
                    return(GetStackData(blockId, symbolindex, scope));
                }
                else if (MemoryRegion.kMemHeap == region)
                {
                    //return GetHeapData(symbolindex);
                    throw new NotImplementedException("{69604961-DE03-440A-97EB-0390B1B0E510}");
                }
                else if (MemoryRegion.kMemStatic == region)
                {
                    Validity.Assert(false, "static region not yet supported, {63EA5434-D2E2-40B6-A816-0046F573236F}");
                }

                Validity.Assert(false, "unsupported memory region, {DCA48F13-EEE1-4374-B301-C96870D44C6B}");
                return(StackUtils.BuildInt(0));
            }
Exemplo n.º 17
0
 private void PushFrame(int size)
 {
     for (int n = 0; n < size; ++n)
     {
         Stack.Add(StackUtils.BuildNull());
     }
 }
Exemplo n.º 18
0
 public void PushGlobFrame(int globsize)
 {
     for (int n = 0; n < globsize; ++n)
     {
         Stack.Add(StackUtils.BuildNull());
     }
 }
Exemplo n.º 19
0
        private static int GetMaxRankForArray(StackValue array, Core core, int tracer)
        {
            if (tracer > RECURSION_LIMIT)
            {
                throw new CompilerInternalException("Internal Recursion limit exceeded in Rank Check - Possible heap corruption {3317D4F6-4758-4C19-9680-75B68DA0436D}");
            }

            if (!StackUtils.IsArray(array))
            {
                return(0);
            }
            //throw new ArgumentException("The stack value provided was not an array");

            int ret = 1;

            //This is the element on the heap that manages the data structure
            HeapElement heapElement = GetHeapElement(array, core);


            int largestSub = 0;

            for (int i = 0; i < heapElement.VisibleSize; ++i)
            {
                StackValue sv = heapElement.Stack[i];

                if (sv.optype == AddressType.ArrayPointer)
                {
                    int subArrayRank = GetMaxRankForArray(sv, core, tracer + 1);

                    largestSub = Math.Max(subArrayRank, largestSub);
                }
            }

            return(largestSub + ret);
        }
Exemplo n.º 20
0
        /// <summary>
        /// Gets all array elements in a List of given type using the given converter to
        /// convert the stackValue.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="array"></param>
        /// <param name="core"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        public static List <T> GetValues <T>(StackValue array, Core core, Func <StackValue, T> converter)
        {
            Validity.Assert(StackUtils.IsArray(array));
            if (!StackUtils.IsArray(array))
            {
                return(null);
            }

            HeapElement he     = GetHeapElement(array, core);
            List <T>    values = new List <T>();

            foreach (var sv in he.Stack)
            {
                values.Add(converter(sv));
            }

            if (he.Dict != null)
            {
                foreach (var sv in he.Dict.Values)
                {
                    values.Add(converter(sv));
                }
            }

            return(values);
        }
Exemplo n.º 21
0
        /// <summary>
        /// array[index1][index2][...][indexN] = value, and
        /// indices = {index1, index2, ..., indexN}
        ///
        /// Note this function doesn't support the replication of array indexing.
        /// </summary>
        /// <param name="array"></param>
        /// <param name="indices"></param>
        /// <param name="value"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue SetValueForIndices(StackValue array, StackValue[] indices, StackValue value, Core core)
        {
            Validity.Assert(StackUtils.IsArray(array) || StackUtils.IsString(array));

            for (int i = 0; i < indices.Length - 1; ++i)
            {
                StackValue  index = indices[i];
                HeapElement he    = GetHeapElement(array, core);

                StackValue subArray;

                if (StackUtils.IsNumeric(index))
                {
                    index = index.AsInt();
                    int absIndex = he.ExpandByAcessingAt((int)index.opdata);
                    subArray = he.Stack[absIndex];
                }
                else
                {
                    subArray = GetValueFromIndex(array, index, core);
                }

                // auto-promotion
                if (!StackUtils.IsArray(subArray))
                {
                    subArray = HeapUtils.StoreArray(new StackValue[] { subArray }, null, core);
                    GCUtils.GCRetain(subArray, core);
                    SetValueForIndex(array, index, subArray, core);
                }

                array = subArray;
            }

            return(SetValueForIndex(array, indices[indices.Length - 1], value, core));
        }
Exemplo n.º 22
0
        public static StackValue GetNextKey(StackValue key, Core core)
        {
            if (StackUtils.IsNull(key) ||
                key.optype != AddressType.ArrayKey ||
                key.opdata < 0 ||
                key.metaData.type < 0)
            {
                return(StackUtils.BuildNull());
            }

            int ptr   = key.metaData.type;
            int index = (int)key.opdata;

            HeapElement he = core.Heap.Heaplist[ptr];

            if ((he.VisibleSize > index + 1) ||
                (he.Dict != null && he.Dict.Count + he.VisibleSize > index + 1))
            {
                StackValue newKey = key;
                newKey.opdata = newKey.opdata + 1;
                return(newKey);
            }

            return(StackUtils.BuildNull());
        }
Exemplo n.º 23
0
        private void GetStackWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string reqJson = ExceptionUtils.parseException(e.Argument.ToString());
            string resp    = ExceptionUtils.sendPost(Properties.Resources.CONTACT_SERVER, reqJson);

            StackUtils.getStackItems(resp);
            e.Result = reqJson;
        }
Exemplo n.º 24
0
        public static StackValue ConcatString(StackValue op1, StackValue op2, ProtoCore.Runtime.RuntimeMemory rmem)
        {
            StackValue[] v1  = (AddressType.String == op1.optype) ? rmem.GetArrayElements(op1) : new StackValue[] { op1 };
            StackValue[] v2  = (AddressType.String == op2.optype) ? rmem.GetArrayElements(op2) : new StackValue[] { op2 };
            StackValue   tmp = rmem.BuildArray(v1.Concat(v2).ToArray());

            return(StackUtils.BuildString(tmp.opdata));
        }
Exemplo n.º 25
0
        internal int ComputeCastDistance(List <StackValue> args, ClassTable classTable, Core core)
        {
            //Compute the cost to migrate a class calls argument types to the coresponding base types
            //This cannot be used to determine whether a function can be called as it will ignore anything that doesn't
            //it should only be used to determine which class is closer

            if (args.Count != FormalParams.Length)
            {
                return(int.MaxValue);
            }

            int distance = 0;

            if (0 == args.Count)
            {
                return(distance);
            }
            else
            {
                // Check if all the types match the current function at 'n'
                for (int i = 0; i < args.Count; ++i)
                {
                    int rcvdType = (int)args[i].metaData.type;

                    // If its a default argumnet, then it wasnt provided by the caller
                    // The rcvdType is the type of the argument signature
                    if (args[i].optype == AddressType.DefaultArg)
                    {
                        rcvdType = FormalParams[i].UID;
                    }

                    int expectedType = FormalParams[i].UID;

                    int currentCost = 0;

                    if (FormalParams[i].IsIndexable != StackUtils.IsArray(args[i])) //Replication code will take care of this
                    {
                        continue;
                    }
                    else if (FormalParams[i].IsIndexable && (FormalParams[i].IsIndexable == StackUtils.IsArray(args[i])))
                    {
                        continue;
                    }
                    else if (expectedType == rcvdType && (FormalParams[i].IsIndexable == StackUtils.IsArray(args[i])))
                    {
                        continue;
                    }
                    else if (rcvdType != ProtoCore.DSASM.Constants.kInvalidIndex &&
                             expectedType != ProtoCore.DSASM.Constants.kInvalidIndex)
                    {
                        currentCost += ClassUtils.GetUpcastCountTo(classTable.ClassNodes[rcvdType],
                                                                   classTable.ClassNodes[expectedType], core);
                    }
                    distance += currentCost;
                }
                return(distance);
            }
        }
Exemplo n.º 26
0
            public bool IsHeapActive(StackValue sv)
            {
                if (!StackUtils.IsReferenceType(sv))
                {
                    return(false);
                }

                return(Heap.Heaplist[(int)sv.opdata].Active);
            }
Exemplo n.º 27
0
        internal static void CompareCores(Core c1, Core c2)
        {
            Assert.AreEqual(c1.DSExecutable.runtimeSymbols.Length, c2.DSExecutable.runtimeSymbols.Length);


            for (int symTableIndex = 0; symTableIndex < c1.DSExecutable.runtimeSymbols.Length; symTableIndex++)
            {
                foreach (SymbolNode symNode in c1.DSExecutable.runtimeSymbols[symTableIndex].symbolList.Values)
                {
                    ExecutionMirror runExecMirror = new ExecutionMirror(c1.CurrentExecutive.CurrentDSASMExec,
                                                                        c1);
                    ExecutionMirror debugExecMirror =
                        new ExecutionMirror(c2.CurrentExecutive.CurrentDSASMExec, c2);

                    bool       lookupOk = false;
                    StackValue runValue = new StackValue();

                    if (symNode.name.StartsWith("%") || symNode.functionIndex != Constants.kInvalidIndex)
                    {
                        continue; //Don't care about internal variables
                    }
                    try
                    {
                        runValue = runExecMirror.GetGlobalValue(symNode.name);
                        Console.WriteLine(symNode.name + ": " + runValue);
                        lookupOk = true;
                    }
                    catch (NotImplementedException)
                    {
                    }
                    catch (Exception ex)
                    {
                        if ((ex is ArgumentOutOfRangeException || ex is IndexOutOfRangeException) &&
                            (c1.RunningBlock != symNode.runtimeTableIndex))
                        {
                            // Quite possible that variables defined in the inner
                            // language block have been garbage collected and
                            // stack frame pointer has been adjusted when return
                            // to the outer block.
                        }
                        else
                        {
                            throw ex;
                        }
                    }

                    if (lookupOk)
                    {
                        StackValue debugValue = debugExecMirror.GetGlobalValue(symNode.name);
                        if (!StackUtils.CompareStackValues(debugValue, runValue, c2, c1))
                        {
                            Assert.Fail(string.Format("\tThe value of variable \"{0}\" doesn't match in run mode and in debug mode.\n", symNode.name));
                        }
                    }
                }
            }
        }
Exemplo n.º 28
0
        /// <summary>
        /// array[index1][index2][...][indexN] = value, and
        /// indices = {index1, index2, ..., indexN}
        /// </summary>
        /// <param name="array"></param>
        /// <param name="indices"></param>
        /// <param name="value"></param>
        /// <param name="t"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue SetValueForIndices(StackValue array, List <StackValue> indices, StackValue value, Type t, Core core)
        {
            StackValue[][] zippedIndices = ArrayUtils.GetZippedIndices(indices, core);
            if (zippedIndices == null || zippedIndices.Length == 0)
            {
                return(StackUtils.BuildNull());
            }

            if (zippedIndices.Length == 1)
            {
                StackValue coercedData = TypeSystem.Coerce(value, t, core);
                GCUtils.GCRetain(coercedData, core);
                return(ArrayUtils.SetValueForIndices(array, zippedIndices[0], coercedData, core));
            }
            else if (value.optype == AddressType.ArrayPointer)
            {
                // Replication happens on both side.
                if (t.rank > 0)
                {
                    t.rank = t.rank - 1;
                    if (t.rank == 0)
                    {
                        t.IsIndexable = false;
                    }
                }

                HeapElement dataHeapElement = GetHeapElement(value, core);
                int         length          = Math.Min(zippedIndices.Length, dataHeapElement.VisibleSize);

                StackValue[] oldValues = new StackValue[length];
                for (int i = 0; i < length; ++i)
                {
                    StackValue coercedData = TypeSystem.Coerce(dataHeapElement.Stack[i], t, core);
                    GCUtils.GCRetain(coercedData, core);
                    oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, core);
                }

                // The returned old values shouldn't have any key-value pairs
                return(HeapUtils.StoreArray(oldValues, null, core));
            }
            else
            {
                // Replication is only on the LHS, so collect all old values
                // and return them in an array.
                StackValue coercedData = TypeSystem.Coerce(value, t, core);
                GCUtils.GCRetain(coercedData, core);

                StackValue[] oldValues = new StackValue[zippedIndices.Length];
                for (int i = 0; i < zippedIndices.Length; ++i)
                {
                    oldValues[i] = SetValueForIndices(array, zippedIndices[i], coercedData, core);
                }

                // The returned old values shouldn't have any key-value pairs
                return(HeapUtils.StoreArray(oldValues, null, core));
            }
        }
Exemplo n.º 29
0
        /// <summary>
        /// = array[index].
        ///
        /// Note this function doesn't support the replication of array indexing.
        /// </summary>
        /// <param name="array"></param>
        /// <param name="index"></param>
        /// <param name="core"></param>
        /// <returns></returns>
        public static StackValue GetValueFromIndex(StackValue array, StackValue index, Core core)
        {
            Validity.Assert(StackUtils.IsArray(array) || StackUtils.IsString(array));
            if (!StackUtils.IsArray(array) && !StackUtils.IsString(array))
            {
                return(StackUtils.BuildNull());
            }

            if (StackUtils.IsNumeric(index))
            {
                index = index.AsInt();
                return(GetValueFromIndex(array, (int)index.opdata, core));
            }
            else if (index.optype == AddressType.ArrayKey)
            {
                int         fullIndex = (int)index.opdata;
                HeapElement he        = GetHeapElement(array, core);

                if (he.VisibleSize > fullIndex)
                {
                    return(GetValueFromIndex(array, fullIndex, core));
                }
                else
                {
                    fullIndex = fullIndex - he.VisibleSize;
                    if (he.Dict != null && he.Dict.Count > fullIndex)
                    {
                        int count = 0;
                        foreach (var key in he.Dict.Keys)
                        {
                            if (count == fullIndex)
                            {
                                return(he.Dict[key]);
                            }
                            count = count + 1;
                        }
                    }
                }

                return(StackUtils.BuildNull());
            }
            else
            {
                HeapElement he    = GetHeapElement(array, core);
                StackValue  value = StackUtils.BuildNull();

                if (he.Dict != null && he.Dict.TryGetValue(index, out value))
                {
                    return(value);
                }
                else
                {
                    return(StackUtils.BuildNull());
                }
            }
        }
Exemplo n.º 30
0
        public static int GetFullSize(StackValue array, Core core)
        {
            Validity.Assert(StackUtils.IsArray(array));
            if (!StackUtils.IsArray(array))
            {
                return(Constants.kInvalidIndex);
            }

            return(GetElementSize(array, core) + GetValueSize(array, core));
        }