Exemplo n.º 1
0
        public void Load(DarksVMContext ctx, out ExecutionState state)
        {
            uint        sp       = ctx.Registers[DarksVMConstants.REG_SP].U4;
            DarksVMSlot typeSlot = ctx.Stack[sp];

            MemberInfo reference = ctx.Instance.Data.LookupReference(typeSlot.U4);

            switch (reference)
            {
            case Type _:
                typeSlot.O = ValueTypeBox.Box(((Type)reference).TypeHandle, typeof(RuntimeTypeHandle));
                break;

            case MethodBase _:
                typeSlot.O = ValueTypeBox.Box(((MethodBase)reference).MethodHandle, typeof(RuntimeMethodHandle));
                break;

            case FieldInfo _:
                typeSlot.O = ValueTypeBox.Box(((FieldInfo)reference).FieldHandle, typeof(RuntimeFieldHandle));
                break;
            }
            ctx.Stack[sp] = typeSlot;

            state = ExecutionState.Next;
        }
Exemplo n.º 2
0
        public void Run(VMContext ctx, out ExecutionState state)
        {
            var sp       = ctx.Registers[Constants.REG_SP].U4;
            var typeSlot = ctx.Stack[sp];

            var reference = ctx.Instance.Data.LookupReference(typeSlot.U4);

            if (reference is Type)
            {
                typeSlot.O = ValueTypeBox.Box(((Type)reference).TypeHandle, typeof(RuntimeTypeHandle));
            }
            else if (reference is MethodBase)
            {
                typeSlot.O = ValueTypeBox.Box(((MethodBase)reference).MethodHandle, typeof(RuntimeMethodHandle));
            }
            else if (reference is FieldInfo)
            {
                typeSlot.O = ValueTypeBox.Box(((FieldInfo)reference).FieldHandle, typeof(RuntimeFieldHandle));
            }
            ctx.Stack[sp] = typeSlot;

            state = ExecutionState.Next;
        }
Exemplo n.º 3
0
        public void Load(DarksVMContext ctx, out ExecutionState state)
        {
            var sp       = ctx.Registers[DarksVMConstants.REG_SP].U4;
            var typeSlot = ctx.Stack[sp--];
            var addrSlot = ctx.Stack[sp--];

            var type = (Type)ctx.Instance.Data.LookupReference(typeSlot.U4);

            if (addrSlot.O is IReference)
            {
                var reference = (IReference)addrSlot.O;
                var slot      = new DarksVMSlot();
                if (type.IsValueType)
                {
                    object def = null;
                    if (Nullable.GetUnderlyingType(type) == null)
                    {
                        def = FormatterServices.GetUninitializedObject(type);
                    }
                    slot.O = ValueTypeBox.Box(def, type);
                }
                else
                {
                    slot.O = null;
                }
                reference.SetValue(ctx, slot, PointerType.OBJECT);
            }
            else
            {
                throw new NotSupportedException();
            }

            ctx.Stack.SetTopPosition(sp);
            ctx.Registers[DarksVMConstants.REG_SP].U4 = sp;
            state = ExecutionState.Next;
        }
Exemplo n.º 4
0
        public static unsafe VMSlot FromObject(object obj, Type type)
        {
            if (type.IsEnum)
            {
                var elemType = Enum.GetUnderlyingType(type);
                return(FromObject(Convert.ChangeType(obj, elemType), elemType));
            }

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Byte:
                return(new VMSlot {
                    u1 = (byte)obj
                });

            case TypeCode.SByte:
                return(new VMSlot {
                    u1 = (byte)(sbyte)obj
                });

            case TypeCode.Boolean:
                return(new VMSlot {
                    u1 = (byte)((bool)obj ? 1 : 0)
                });

            case TypeCode.UInt16:
                return(new VMSlot {
                    u2 = (ushort)obj
                });

            case TypeCode.Int16:
                return(new VMSlot {
                    u2 = (ushort)(short)obj
                });

            case TypeCode.Char:
                return(new VMSlot {
                    u2 = (char)obj
                });

            case TypeCode.UInt32:
                return(new VMSlot {
                    u4 = (uint)obj
                });

            case TypeCode.Int32:
                return(new VMSlot {
                    u4 = (uint)(int)obj
                });

            case TypeCode.UInt64:
                return(new VMSlot {
                    u8 = (ulong)obj
                });

            case TypeCode.Int64:
                return(new VMSlot {
                    u8 = (ulong)(long)obj
                });

            case TypeCode.Single:
                return(new VMSlot {
                    r4 = (float)obj
                });

            case TypeCode.Double:
                return(new VMSlot {
                    r8 = (double)obj
                });

            default:
                if (obj is Pointer)
                {
                    return new VMSlot {
                               u8 = (ulong)Pointer.Unbox(obj)
                    }
                }
                ;
                if (obj is IntPtr)
                {
                    return new VMSlot {
                               u8 = (ulong)(IntPtr)obj
                    }
                }
                ;
                if (obj is UIntPtr)
                {
                    return new VMSlot {
                               u8 = (ulong)(UIntPtr)obj
                    }
                }
                ;
                if (type.IsValueType)
                {
                    return new VMSlot {
                               o = ValueTypeBox.Box(obj, type)
                    }
                }
                ;
                return(new VMSlot {
                    o = obj
                });
            }
        }