public override void SetReferencedValue(Value val) { Type t = obj.GetType(); StructValue structVal = val.FromStack(t) as StructValue; if (structVal.IsPrimitive) { DataModelUtils.StoreToBox(obj, (int)(StructValue.getTypeIndex(t)), structVal.Obj); } else { FieldInfo[] fields = obj.GetType().GetFields((BindingFlags) (BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic) ); foreach (FieldInfo field in fields) { field.SetValue(obj, field.GetValue(structVal.Obj)); } } }
/* Performs conversion of primitive value on top of the stack * (corresponds to CILPE.CFG.ConvertValue class) */ public void Perform_ConvertValue(Type type, bool overflow, bool unsigned, out Exception exc) { exc = null; Value val = Pop(); if (!(val is StructValue && (val as StructValue).IsPrimitive)) { throw new InvalidOperandException(); } StructValue primVal = val as StructValue; StructValue.TypeIndex typeIndex = StructValue.getTypeIndex(type), valTypeIndex = primVal.getTypeIndex(); if (!overflow && unsigned && typeIndex != StructValue.TypeIndex.FLOAT64 || overflow && typeIndex == StructValue.TypeIndex.FLOAT32 || overflow && typeIndex == StructValue.TypeIndex.FLOAT64) { throw new InvalidConvertOpException(); } object res; bool success = DataModelUtils.Convert( (int)typeIndex, overflow, unsigned, primVal.Obj, (int)valTypeIndex, out res ); if (!success) { exc = res as Exception; } else { push(new StructValue(res as ValueType)); } }
/* Performs check for a finite real number * (corresponds to CILPE.CFG.CheckFinite class) */ public void Perform_CheckFinite(out Exception exc) { exc = null; Value val = this[0]; if (!(val is StructValue && (val as StructValue).IsPrimitive)) { throw new InvalidOperandException(); } StructValue primVal = val as StructValue; if (!(primVal.Obj is double)) { throw new InvalidOperandException(); } double number = (double)(primVal.Obj); if (Double.IsInfinity(number) || Double.IsNaN(number)) { exc = new ArithmeticException(); } }
/* Performs unary operation on the value on top of the stack * (corresponds to CILPE.CFG.UnaryOp class) */ public void Perform_UnaryOp(UnaryOp.ArithOp op) { Value val = Pop(); ValueType res = null; if (val is StructValue && (val as StructValue).IsPrimitive) { StructValue strVal = val as StructValue; ValueType obj = strVal.Obj; StructValue.TypeIndex typeIndex = strVal.getTypeIndex(); if (!DataModelUtils.Unary((int)op, obj, (int)typeIndex, out res)) { throw new InvalidOperandException(); } } else { throw new InvalidOperandException(); } push(new StructValue(res)); }
public override Value ToStack() { Value result = null; if (isPrimitive) { object res = null; DataModelUtils.ToStack(obj, (int)getTypeIndex(), out res); result = new StructValue(res as ValueType); } else { if (obj.GetType().IsEnum) { result = new StructValue(getEnumValue(obj)); } else { result = MakeCopy(); } } return(result); }
private static int toInt(StructValue val) { if (val.Type == typeof(int)) return (int) val.Obj; else if (val.Type == typeof(IntPtr)) return (int) (IntPtr) val.Obj; else throw new InternalException(); }
public override Value ToStack() { Value result = null; if (isPrimitive) { object res = null; DataModelUtils.ToStack(obj,(int)getTypeIndex(),out res); result = new StructValue(res as ValueType); } else { if (obj.GetType().IsEnum) result = new StructValue(getEnumValue(obj)); else result = MakeCopy(); } return result; }
/* Invokes method */ public Value Invoke(out Exception exc) { exc = null; if (method.IsConstructor && method.DeclaringType.IsArray) { return(parms[0].Val); } object obj = null; int pos = 0; if (!Method.IsStatic) { pos = 1; Value objVal = parms[0].Val; if (objVal is ObjectReferenceValue) { obj = (objVal as ObjectReferenceValue).Obj; } else if (objVal is PointerValue) { obj = (objVal as PointerValue).GetReferencedObject(); } else if (objVal is NullValue) { obj = null; } else { throw new InvalidOperandException(); } } else { pos = 0; obj = null; } object[] parameters = new object[Count - pos]; for (int i = pos; i < Count; i++) { object p = this[i].Val.ToParameter(); if (types[i - pos] == typeof(int) && p.GetType() == typeof(IntPtr)) { parameters[i - pos] = (int)(IntPtr)p; } else { parameters[i - pos] = p; } } object retVal = null; try { retVal = method.Invoke(obj, parameters); } catch (Exception e) { exc = e.InnerException; } Value result = null; if (exc == null && !Method.IsConstructor) { Type retType = (Method as MethodInfo).ReturnType; if (retType != typeof(void)) { if (retType.IsValueType) { result = new StructValue(retVal as ValueType); } else if (retVal != null) { result = new ObjectReferenceValue(retVal); } else { result = new NullValue(); } } } return(result); }