Inazuma.PetitClr.Core.PetitClrRuntime.AllocateObject C# (CSharp) Method

AllocateObject() public static method

public static AllocateObject ( MethodTable methodTable, CorInfoType type = CorInfoType.Class ) : ObjectInstance
methodTable Inazuma.PetitClr.Core.Structure.MethodTable
type CorInfoType
return Inazuma.PetitClr.Core.Structure.ObjectInstance
        public static ObjectInstance AllocateObject(MethodTable methodTable, CorInfoType type = CorInfoType.Class)
        {
            var newObject = PetitClrRuntime.Current.GCHeap.Alloc();
            newObject.MethodTable = methodTable;
            newObject.Type = type;

            newObject.FieldInstances = new ObjectInstance[methodTable.EEClass.NumInstanceFields];
            for (var i = 0; i < newObject.FieldInstances.Length; i++)
            {
                newObject.FieldInstances[i] = ObjectInstance.Null;
            }

            // TODO: implements type assertion
            //Debug.Assert(methodTable.EEClass.IsValueType == (newObject.Type == CorInfoType.ValueClass));

            return newObject;
        }
    }

Usage Example

Ejemplo n.º 1
0
        private void NewObj()
        {
            var inst         = _instructions[_instructionPtr];
            var memberRef    = ((MethodReference)inst.Operand);
            var type         = _classLoader.LoadTypeFromTypeRef(memberRef.DeclaringType);
            var resolvedCtor = memberRef.Resolve();

            if (resolvedCtor.IsStatic || resolvedCtor.IsAbstract)
            {
                ThrowHelper.VerificationError("newobj on static or abstract method");
            }

            // There are four cases:
            // 1) Value types (ordinary constructor, resulting VALUECLASS pushed)
            // 2) String (var-args constructor, result automatically pushed)
            // 3) MDArray (var-args constructor, resulting OBJECTREF pushed)
            // 4) Reference types (ordinary constructor, resulting OBJECTREF pushed)
            var classInfo = memberRef.DeclaringType;

            if (classInfo.IsValueType)
            {
                // Value type
                throw ThrowHelper.NotImplementedYet;
            }
            else if (classInfo.Namespace == "System" && classInfo.Name == "String") // TODO: move to method table
            {
                // For a VAROBJSIZE class (currently == String), pass NULL as this to "pseudo-constructor."
                throw ThrowHelper.NotImplementedYet;
            }
            else
            {
                ObjectInstance thisArgObj = null;
                if (classInfo.IsArray)
                {
                    // Array
                    throw ThrowHelper.NotImplementedYet;
                }
                else
                {
                    // Reference
                    thisArgObj = PetitClrRuntime.AllocateObject(type);
                    DoCallWork(false, thisArgObj, resolvedCtor);
                }

                _opStack.Push(thisArgObj);
            }
        }