public override StackValue Marshal(object obj, ProtoCore.Runtime.Context context, Interpreter dsi, ProtoCore.Type type) { string str = (string)obj; StackValue dsarray = PrimitiveMarshler.ConvertCSArrayToDSArray(kCharMarshaler, str.ToCharArray(), context, dsi, type); return(StackUtils.BuildString(dsarray.opdata)); }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <param name="protoCoreType"></param> private static void GetProtoCoreType(Type type, ref ProtoCore.Type protoCoreType) { FFIObjectMarshler marshaler; if (type.IsArray) { Type elemType = type.GetElementType(); GetProtoCoreType(elemType, ref protoCoreType); protoCoreType.rank += type.GetArrayRank(); //set the rank. protoCoreType.IsIndexable = true; } else if (type.IsInterface && (typeof(ICollection).IsAssignableFrom(type) || typeof(IEnumerable).IsAssignableFrom(type))) { protoCoreType.rank += 1; protoCoreType.IsIndexable = true; } else if (type.IsGenericType && (typeof(ICollection).IsAssignableFrom(type) || typeof(IEnumerable).IsAssignableFrom(type))) { Type[] args = type.GetGenericArguments(); int nArgs = args.Length; if (nArgs != 1) { protoCoreType.Name = GetTypeName(type); protoCoreType.UID = (int)ProtoCore.PrimitiveType.kTypePointer; return; } Type elemType = args[0]; //TODO: Ideally we shouldn't be calling this method on CLRModuleType, //but we want to import this elemType, hence we do this. protoCoreType = CLRModuleType.GetProtoCoreType(elemType, null); protoCoreType.rank += 1; protoCoreType.IsIndexable = true; } else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable <>)) { protoCoreType = CLRModuleType.GetProtoCoreType(Nullable.GetUnderlyingType(type), null); } else if (type == typeof(object)) { protoCoreType = PrimitiveMarshler.CreateType(ProtoCore.PrimitiveType.kTypeVar); } else if (type == typeof(void)) { protoCoreType = PrimitiveMarshler.CreateType(ProtoCore.PrimitiveType.kTypeVoid); } else if (protoCoreType.UID == (int)ProtoCore.PrimitiveType.kTypePointer) { protoCoreType.Name = GetTypeName(type); } else if (mPrimitiveMarshalers.TryGetValue(type, out marshaler)) { protoCoreType = marshaler.GetMarshaledType(type); } else { protoCoreType.Name = GetTypeName(type); protoCoreType.UID = (int)ProtoCore.PrimitiveType.kTypePointer; } }
/// <summary> /// /// </summary> /// <param name="obj"></param> /// <param name="context"></param> /// <param name="dsi"></param> /// <param name="type"></param> /// <returns></returns> public override StackValue Marshal(object obj, ProtoCore.Runtime.Context context, Interpreter dsi, ProtoCore.Type type) { if (obj == null) { return(StackUtils.BuildNull()); } FFIObjectMarshler marshaler = null; StackValue retVal; Type objType = obj.GetType(); if (type.IsIndexable) { if (obj is System.Collections.ICollection) { System.Collections.ICollection collection = obj as System.Collections.ICollection; object[] array = new object[collection.Count]; collection.CopyTo(array, 0); return(PrimitiveMarshler.ConvertCSArrayToDSArray(this, array, context, dsi, type)); } else if (obj is System.Collections.IEnumerable) { System.Collections.IEnumerable enumerable = obj as System.Collections.IEnumerable; return(PrimitiveMarshler.ConvertCSArrayToDSArray(this, enumerable, context, dsi, type)); } } Array arr = obj as Array; if (null != arr) { return(PrimitiveMarshler.ConvertCSArrayToDSArray(this, arr, context, dsi, type)); } else if ((PrimitiveMarshler.IsPrimitiveDSType(type) || PrimitiveMarshler.IsPrimitiveObjectType(obj, type)) && mPrimitiveMarshalers.TryGetValue(objType, out marshaler)) { return(marshaler.Marshal(obj, context, dsi, type)); } else if (CLRObjectMap.TryGetValue(obj, out retVal)) { return(retVal); } return(CreateDSObject(obj, context, dsi)); }
/// <summary> /// /// </summary> /// <param name="dsObject"></param> /// <param name="context"></param> /// <param name="dsi"></param> /// <param name="arrayType"></param> /// <returns></returns> private object ConvertDSArrayToCSArray(StackValue dsObject, ProtoCore.Runtime.Context context, Interpreter dsi, System.Type arrayType) { if (arrayType.IsArray) { // processing only for the primitive types // anything else will be dealt with as it was earlier // if (arrayType.UnderlyingSystemType == typeof(int[])) { return(PrimitiveMarshler.ConvertDSArrayToCSArray <int>(this, dsObject, context, dsi)); } else if (arrayType.UnderlyingSystemType == typeof(double[])) { return(PrimitiveMarshler.ConvertDSArrayToCSArray <double>(this, dsObject, context, dsi)); } else if (arrayType.UnderlyingSystemType == typeof(bool[])) { return(PrimitiveMarshler.ConvertDSArrayToCSArray <bool>(this, dsObject, context, dsi)); } } int ptr = (int)dsObject.opdata; HeapElement hs = dsi.runtime.rmem.Heap.Heaplist[ptr]; int count = hs.VisibleSize; // use arraylist instead of object[], this allows us to correctly capture // the type of objects being passed // ArrayList arrList = new ArrayList(); var elementType = arrayType.GetElementType(); if (elementType == null) { elementType = typeof(object); } for (int idx = 0; idx < count; ++idx) { arrList.Add(UnMarshal(hs.Stack[idx], context, dsi, elementType)); } return(arrList.ToArray(elementType)); }
/// <summary> /// /// </summary> /// <param name="type"></param> /// <returns></returns> public static ProtoCore.Type GetUserDefinedType(Type type) { ProtoCore.Type retype = PrimitiveMarshler.CreateType(ProtoCore.PrimitiveType.kTypePointer); GetProtoCoreType(type, ref retype); return(retype); }
public override object UnMarshal(StackValue dsObject, ProtoCore.Runtime.Context context, Interpreter dsi, Type type) { char[] array = PrimitiveMarshler.ConvertDSArrayToCSArray <char>(kCharMarshaler, dsObject, context, dsi); return(new string(array)); }