Exemplo n.º 1
0
        internal void WriteObjectToLS(uint objectindex, object data)
        {
            Type        eltype = data.GetType();
            ISerializer s;

            Serializers.TryGetValue(eltype, out s);
            if (s == null && eltype.IsArray)
            {
                Serializers.TryGetValue(typeof(Array), out s);
            }
            if (s == null)
            {
                throw new Exception("Unable to serialize element of type: " + eltype.FullName);
            }

            uint size;

            byte[] buffer = s.Serialize(data, out size);

            ObjectTableEntry e = ObjectTable[objectindex];

            if (size != e.Size)
            {
                throw new Exception("Bad stuff happened");
            }

            if (e.AlignedSize != buffer.Length)
            {
                byte[] tmp = new byte[e.AlignedSize];
                Array.Copy(buffer, tmp, buffer.Length);
                buffer = tmp;
            }

            Converter.WriteBytes(e.Offset, buffer);
        }
Exemplo n.º 2
0
        public object ReadObjectFromLS(uint objectIndex, object storage)
        {
            if (objectIndex == 0)
            {
                return(null);
            }

            if (!KnownObjectsById.ContainsKey(objectIndex))
            {
                ObjectTableEntry e      = ObjectTable[objectIndex];
                Type             eltype = FindElementType(objectIndex);
                ISerializer      s;
                Serializers.TryGetValue(eltype, out s);
                if (s == null && eltype.IsArray)
                {
                    Serializers.TryGetValue(typeof(Array), out s);
                }
                if (s == null)
                {
                    throw new Exception("Unable to deserialize element of type: " + eltype.FullName);
                }

                SPEEmulator.EndianBitConverter c = new SPEEmulator.EndianBitConverter(new byte[e.AlignedSize]);
                Converter.ReadBytes(e.Offset, c.Data);

                object el = s.Deserialize(c, e, storage);

                KnownObjectsById[objectIndex] = el;
                KnownObjectsByObj[el]         = objectIndex;
            }

            return(KnownObjectsById[objectIndex]);
        }
Exemplo n.º 3
0
        public uint CreateObjectOnLS(object data)
        {
            if (data == null)
            {
                return(0);
            }

            if (!KnownObjectsByObj.ContainsKey(data))
            {
                Type        eltype = data.GetType();
                ISerializer s;
                Serializers.TryGetValue(eltype, out s);
                if (s == null && eltype.IsArray)
                {
                    Serializers.TryGetValue(typeof(Array), out s);
                }
                if (s == null)
                {
                    throw new Exception("Unable to serialize element of type: " + eltype.FullName);
                }

                uint   size;
                byte[] buffer = s.Serialize(data, out size);

                uint objindex;

                if (eltype.IsArray && eltype.GetElementType().IsPrimitive)
                {
                    objindex = ObjectTable.AddObject(AccCIL.AccCIL.GetObjType(eltype.GetElementType()), size, null);
                }
                else if (eltype == typeof(string))
                {
                    objindex = ObjectTable.AddObject(AccCIL.KnownObjectTypes.String, size, null);
                }
                else
                {
                    objindex = ObjectTable.AddObject(AccCIL.KnownObjectTypes.Object, size, eltype.FullName);
                }

                ObjectTableEntry e = ObjectTable[objindex];
                if (e.AlignedSize != buffer.Length)
                {
                    byte[] tmp = new byte[e.AlignedSize];
                    Array.Copy(buffer, tmp, buffer.Length);
                    buffer = tmp;
                }

                Converter.WriteBytes(e.Offset, buffer);

                KnownObjectsById[objindex] = data;
                KnownObjectsByObj[data]    = objindex;
            }

            return(KnownObjectsByObj[data]);
        }
Exemplo n.º 4
0
        private Type FindElementType(uint objectIndex)
        {
            ObjectTableEntry e = ObjectTable[objectIndex];

            if (e.KnownType == AccCIL.KnownObjectTypes.String)
            {
                return(typeof(string));
            }
            if (e.Type != 0)
            {
                return(Type.GetType((string)ReadObjectFromLS(e.Type)));
            }
            else
            {
                return(AccCIL.AccCIL.GetObjType(e.KnownType).MakeArrayType());
            }
        }
Exemplo n.º 5
0
        private static void WriteObjectTable(SPEEmulator.IEndianBitConverter conv, ObjectTableWrapper objtable)
        {
            SPEEmulator.EndianBitConverter obj_tb_tmp = new SPEEmulator.EndianBitConverter(new byte[objtable.Data.Length * 4]);
            foreach (uint u in objtable.Data)
            {
                obj_tb_tmp.WriteUInt(u);
            }

            conv.WriteBytes(SPEJITCompiler.OBJECT_TABLE_OFFSET, obj_tb_tmp.Data);

            foreach (KeyValuePair <string, uint> k in objtable.Strings)
            {
                ObjectTableEntry e = objtable[k.Value];

                System.Diagnostics.Debug.Assert(e.KnownType == AccCIL.KnownObjectTypes.String);
                System.Diagnostics.Debug.Assert(e.Size == k.Key.Length);

                byte[] localdata = new byte[e.AlignedSize];
                System.Text.Encoding.UTF8.GetBytes(k.Key, 0, k.Key.Length, localdata, 0);

                conv.WriteBytes(e.Offset, localdata);
            }
        }
Exemplo n.º 6
0
            public object Deserialize(SPEEmulator.IEndianBitConverter conv, ObjectTableEntry e, object storage)
            {
                Type arrtype;
                uint elsize;

                if (e.KnownType == AccCIL.KnownObjectTypes.String)
                {
                    throw new InvalidProgramException("Something is wrong here");
                }

                if (e.Type == 0)
                {
                    arrtype = AccCIL.AccCIL.GetObjType(e.KnownType).MakeArrayType();
                    elsize  = 1u << (int)BuiltInSPEMethods.get_array_elem_len_mult((uint)e.KnownType);
                }
                else
                {
                    arrtype = Type.GetType((string)m_parent.ReadObjectFromLS(e.Type));
                    elsize  = 4;
                }

                Type eltype   = arrtype.GetElementType();
                uint arraylen = e.Size / elsize;

                Array arr;

                if (storage == null)
                {
                    arr = Array.CreateInstance(eltype, arraylen);
                }
                else
                {
                    arr = (Array)storage;
                    if (storage.GetType().GetElementType() != eltype || arr.Length != arraylen)
                    {
                        throw new Exception("Unexpected difference in storage object and actual object");
                    }
                }

                if (eltype.IsPrimitive)
                {
                    ISerializer elserializer;
                    m_parent.Serializers.TryGetValue(eltype, out elserializer);
                    if (elserializer == null)
                    {
                        throw new Exception("Unsupported inner type: " + eltype.FullName);
                    }

                    for (int i = 0; i < arr.Length; i++)
                    {
                        arr.SetValue(elserializer.Deserialize(conv, null, arr.GetValue(i)), i);
                    }
                }
                else
                {
                    //In this case elements may have a different type than what the array states,
                    //because the array elements can be interface or object type, and the actual
                    //instance type is unknown
                    ISerializer uintd = m_parent.Serializers[typeof(uint)];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        arr.SetValue(m_parent.ReadObjectFromLS((uint)uintd.Deserialize(conv, null, null), arr.GetValue(i)), i);
                    }
                }

                return(arr);
            }
Exemplo n.º 7
0
 public object Deserialize(SPEEmulator.IEndianBitConverter conv, ObjectTableEntry e, object storage)
 {
     byte[] data = new byte[e.AlignedSize];
     conv.ReadBytes(data);
     return(System.Text.Encoding.UTF8.GetString(data, 0, (int)e.Size));
 }
Exemplo n.º 8
0
 public object Deserialize(SPEEmulator.IEndianBitConverter conv, ObjectTableEntry e, object storage)
 {
     return(conv.ReadDouble());
 }
Exemplo n.º 9
0
 public object Deserialize(SPEEmulator.IEndianBitConverter conv, ObjectTableEntry e, object storage)
 {
     return(conv.ReadByte() == 0 ? false : true);
 }