/* Performs object creation * (corresponds to CILPE.CFG.CreateObject class) */ public ParameterValues Perform_CreateObject(ConstructorInfo ctor) { Type type = ctor.DeclaringType; int count = ParameterValues.GetParametersNumber(ctor); Value[] values = new Value[count]; for (int i = 0; i < count - 1; i++) { values[count - 1 - i] = Pop(); } object obj; if (type.IsArray) { Type elementType = type.GetElementType(); int rank = type.GetArrayRank(); int[] lengths = new int[rank]; for (int i = 0; i < rank; i++) { lengths[i] = (int)((values[i + 1] as StructValue).Obj); } obj = Array.CreateInstance(elementType, lengths); } else { obj = FormatterServices.GetUninitializedObject(type); } Value resultValue = null; if (type.IsValueType) { resultValue = new StructValue(obj as ValueType); values[0] = new PointerToUnboxedValue(obj); } else { values[0] = resultValue = new ObjectReferenceValue(obj); } push(resultValue); return(new ParameterValues(ctor, values)); }
protected override void VisitNewObject(NewObject downNode, object o) { PointerToNode ptrUpNode = (o as Data).PointerToNode; AnnotatedMethod method = Annotation.GetAnnotatedMethod(downNode); Node upNode; ArrayList list = new ArrayList(); for (int i = method.ParamVals.Count - 1; i > 0; i--) if (Annotation.GetValueBTType(method.ParamVals[i].Val) == BTType.Static) list.Add(this.state.Stack.Pop()); if (Annotation.GetValueBTType(method.ParamVals[0].Val) == BTType.Static) { Type objtype = downNode.Constructor.DeclaringType; ObjectReferenceValue obj = new ObjectReferenceValue(FormatterServices.GetUninitializedObject(objtype)); FieldInfo[] fldInfos = ReflectionUtils.GetAllFields(objtype); BTType[] btTypes = Annotation.GetObjectFieldBTTypes(downNode); for (int i = 0; i < fldInfos.Length; i++) if (btTypes[i] == BTType.Dynamic) { Variable varUp = this.mbbUp.Variables.CreateVar(fldInfos[i].FieldType, VariableKind.Local); this.varsHash[new PointerToObjectFieldValue(obj.Obj, fldInfos[i])] = varUp; ptrUpNode = SpecializingVisitor.initVariable(varUp, ptrUpNode); } list.Add(obj); this.state.Stack.Push(obj); upNode = new CallMethod(downNode.Constructor, false, false); } else upNode = new NewObject(downNode.Constructor); list.Reverse(); Value[] args = list.ToArray(typeof(Value)) as Value[]; this.callMethod(downNode, method, ptrUpNode, upNode, args); }
protected override void VisitNewArray(NewArray downNode, object o) { PointerToNode ptrUpNode = (o as Data).PointerToNode; StructValue idx = this.state.Stack.Pop() as StructValue; Array arr = Array.CreateInstance(downNode.Type, SpecializingVisitor.toInt(idx)); ObjectReferenceValue obj = new ObjectReferenceValue(arr); if (Annotation.GetArrayElementsBTType(downNode) == BTType.Dynamic) for (int i = 0; i < SpecializingVisitor.toInt(idx); i++) { Variable varUp = this.mbbUp.Variables.CreateVar(downNode.Type, VariableKind.Local); this.varsHash[new PointerToElementValue(arr, i)] = varUp; ptrUpNode = SpecializingVisitor.initVariable(varUp, ptrUpNode); } this.state.Stack.Push(obj); this.AddTask(downNode.Next, ptrUpNode); }
/* 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); }