Example #1
0
        public static unsafe object BoxFromStackItem(Type t, int typeID, StackItem *item)
        {
            if (t.IsByRef) {
                t = t.GetElementType();
            }

            if (typeID == 0) {
                if (t.IsEnum) {
                    return Enum.ToObject(t, SizeOfNativeLong <= sizeof(long) ? item->s_int : item->s_long);
                }
                if (t == typeof(int)) {
                    return item->s_int;
                }
                if (t == typeof(bool)) {
                    return item->s_bool;
                }
                if (t == typeof(short)) {
                    return item->s_short;
                }
                if (t == typeof(float)) {
                    return item->s_float;
                }
                if (t == typeof(double)) {
                    return item->s_double;
                }
                if (t == typeof(NativeLong)) {
                    return new NativeLong((SizeOfNativeLong <= sizeof(long))? item->s_int : item->s_long);
                }
                if (t == typeof(ushort)) {
                    return item->s_ushort;
                }
                if (t == typeof(uint)) {
                    return item->s_uint;
                }
                if (t == typeof(NativeULong)) {
                    return new NativeULong((SizeOfNativeLong <= sizeof(long))? item->s_uint : item->s_ulong);
                }
                if (t == typeof(long)) {
                    return item->s_long;
                }
                if (t == typeof(ulong)) {
                    return item->s_ulong;
                }
                if (t == typeof(sbyte)) {
                    return item->s_char;
                }
                if (t == typeof(byte)) {
                    return item->s_uchar;
                }
                if (t == typeof(char)) {
                    return (char) item->s_char;
                }
                if (item->s_class != IntPtr.Zero) {
                    if (typeof(Delegate).IsAssignableFrom(t)) {
                        return Marshal.GetDelegateForFunctionPointer(item->s_class, t);
                    }
                    if (t == typeof(string)) {
                        string value = Marshal.PtrToStringUni(item->s_class);
                        Marshal.FreeHGlobal(item->s_class);
                        return value;
                    }
                    // the StackItem contains a GCHandle to an object
                    GCHandle handle = (GCHandle) item->s_class;
                    object ret = handle.Target;
            #if DEBUG
                    DebugGCHandle.Free(handle);
            #else
                    handle.Free();
            #endif
                    return ret;
                }
                return null;
            }

            return GetByTypeID(typeID, item);
        }
Example #2
0
 private static unsafe object GetByTypeID(int typeID, StackItem *item)
 {
     switch ((TypeId) typeID) {
         case TypeId.t_bool:
             return item->s_bool;
         case TypeId.t_char:
             return (char) item->s_char;
         case TypeId.t_uchar:
             return item->s_uchar;
         case TypeId.t_short:
             return item->s_short;
         case TypeId.t_ushort:
             return item->s_ushort;
         case TypeId.t_enum:
         case TypeId.t_int:
             return item->s_int;
         case TypeId.t_uint:
             return item->s_uint;
         case TypeId.t_long:
             return item->s_long;
         case TypeId.t_ulong:
             return item->s_ulong;
         case TypeId.t_float:
             return item->s_float;
         case TypeId.t_double:
             return item->s_double;
         case TypeId.t_string:
             string value = Marshal.PtrToStringUni(item->s_class);
             Marshal.FreeHGlobal(item->s_class);
             return value;
         default:
             GCHandle handle = (GCHandle) item->s_class;
             object ret = handle.Target;
     #if DEBUG
             DebugGCHandle.Free(handle);
     #else
             handle.Free();
     #endif
             return ret;
     }
 }
Example #3
0
        public static unsafe TypeId UnboxToStackItem(object o, StackItem *item)
        {
            if (o == null) {
                item->s_class = IntPtr.Zero;
                return TypeId.t_class;
            }
            Type t = o.GetType();

            if (t.IsEnum) {
                if (SizeOfNativeLong <= sizeof(long)) {
                    int value = Enum.GetUnderlyingType(t) == typeof(long) ? (int) (long) o : (int) o;
                    item->s_int = value;
                    return TypeId.t_int;
                } else {
                    long value = Enum.GetUnderlyingType(t) == typeof(long) ? (long) o : (int) o;
                    item->s_long = value;
                    return TypeId.t_long;
                }
            }
            if (t == typeof(int)) {
                item->s_int = (int) o;
                return TypeId.t_int;
            }
            if (t == typeof(bool)) {
                item->s_bool = (bool) o;
                return TypeId.t_bool;
            }
            if (t == typeof(short)) {
                item->s_short = (short) o;
                return TypeId.t_short;
            }
            if (t == typeof(float)) {
                item->s_float = (float) o;
                return TypeId.t_float;
            }
            if (t == typeof(double)) {
                item->s_double = (double) o;
                return TypeId.t_double;
            }
            if (t == typeof(NativeLong)) {
                if (SizeOfNativeLong <= sizeof(long)) {
                    item->s_int = (int) (NativeLong) o;
                    return TypeId.t_int;
                }
                item->s_long = (NativeLong) o;
                return TypeId.t_long;
            }
            if (t == typeof(ushort)) {
                item->s_ushort = (ushort) o;
                return TypeId.t_ushort;
            }
            if (t == typeof(uint)) {
                item->s_uint = (uint) o;
                return TypeId.t_uint;
            }
            if (t == typeof(NativeULong)) {
                if (SizeOfNativeLong <= sizeof(long)) {
                    item->s_uint = (uint) (NativeULong) o;
                    return TypeId.t_uint;
                }
                item->s_ulong = (NativeULong) o;
                return TypeId.t_ulong;
            }
            if (t == typeof(long)) {
                item->s_long = (long) o;
                return TypeId.t_long;
            }
            if (t == typeof(ulong)) {
                item->s_ulong = (ulong) o;
                return TypeId.t_ulong;
            }
            if (t == typeof(sbyte)) {
                item->s_char = (sbyte) o;
                return TypeId.t_uchar;
            }
            if (t == typeof(byte)) {
                item->s_uchar = (byte) o;
                return TypeId.t_uchar;
            }
            if (t == typeof(char)) {
                item->s_char = (sbyte) (char) o;
                return TypeId.t_char;
            }
            string text = o as string;
            if (text != null) {
                item->s_class = Marshal.StringToHGlobalUni(text);
                return TypeId.t_string;
            }
            if (o is Delegate) {
                item->s_class = Marshal.GetFunctionPointerForDelegate((Delegate) o);
            } else {
            #if DEBUG
                item->s_class = (IntPtr) DebugGCHandle.Alloc(o);
            #else
                item->s_class = (IntPtr) GCHandle.Alloc(o);
            #endif
            }
            return t == typeof(string) ? TypeId.t_string : TypeId.t_class;
        }
Example #4
0
		public override IMessage Invoke(IMessage message) {
			IMethodCallMessage callMessage = (IMethodCallMessage) message;
			StackItem[] stack = new StackItem[callMessage.ArgCount+1];

#if DEBUG
			if ((QDebug.DebugChannel() & QtDebugChannel.QTDB_TRANSPARENT_PROXY) != 0) {
				Console.WriteLine(	"ENTER SignalInvocation.Invoke() MethodName: {0}.{1} Type: {2} ArgCount: {3}", 
									instance,
									callMessage.MethodName, 
									callMessage.TypeName, 
									callMessage.ArgCount.ToString() );
			}
#endif

			unsafe {
				fixed(StackItem * stackPtr = stack) {
					for (int i = 0; i < callMessage.ArgCount; i++) {
						SmokeMarshallers.UnboxToStackItem(callMessage.Args[i], stackPtr + i + 1);
					}

					IMethodReturnMessage returnMessage = new ReturnMessage(null, callMessage); /*(IMethodReturnMessage) message;*/
					MethodReturnMessageWrapper returnValue = new MethodReturnMessageWrapper((IMethodReturnMessage) returnMessage);

#if DEBUG
					GCHandle instanceHandle = DebugGCHandle.Alloc(instance);
#else
					GCHandle instanceHandle = GCHandle.Alloc(instance);
#endif

					Qyoto.CPPMethod signalEntry = Qyoto.GetSignalSignature(signalsInterface, (MethodInfo) callMessage.MethodBase);

					Type returnType = ((MethodInfo) returnMessage.MethodBase).ReturnType;
					SignalEmit(signalEntry.signature, signalEntry.type, (IntPtr) instanceHandle, (IntPtr) stackPtr, callMessage.ArgCount);

					if (returnType != typeof(void)) {
						returnValue.ReturnValue = SmokeMarshallers.BoxFromStackItem(returnType, 0, stackPtr);
					}

					returnMessage = returnValue;
					return returnMessage;
				}
			}
		}
Example #5
0
		public object Invoke(string mungedName, string signature, Type returnType, bool refArgs, params object[] args) {
#if DEBUG
			if ((QDebug.DebugChannel() & QtDebugChannel.QTDB_TRANSPARENT_PROXY) != 0) {
				Console.WriteLine(	"ENTER SmokeInvocation.Invoke() MethodName: {0}.{1} Type: {2} ArgCount: {3}", 
									className,
									signature, 
									returnType, 
									args.Length / 2 );
			}
#endif
			ModuleIndex methodId;
			methodId.smoke = IntPtr.Zero;
			methodId.index = -1;
			if (!methodIdCache.TryGetValue(signature, out methodId)) {
				methodId = FindMethodId(className, mungedName, signature);

				if (methodId.index == -1) {
					Console.Error.WriteLine(	"LEAVE Invoke() ** Missing method ** {0}.{1}", 
												className,
												signature );
					return null;
				}

				methodIdCache[signature] = methodId;
			}

			StackItem[] stack = new StackItem[(args.Length / 2) + 1];
			int[] typeIDs = new int[(args.Length / 2) + 1];

			unsafe {
				fixed(StackItem* stackPtr = stack) {
					fixed (int* typeIDsPtr = typeIDs)
					{
						typeIDs[0] = 0;
						for (int i = 1, k = 1; i < args.Length; i += 2, k++) {
							typeIDs[k] = (int) SmokeMarshallers.UnboxToStackItem(args[i], stackPtr + k);
						}

						object returnValue = null;
						if (instance == null) {
							CallSmokeMethod(methodId.smoke, methodId.index, (IntPtr) 0, (IntPtr) stackPtr, args.Length / 2, (IntPtr) typeIDsPtr);
						} else {
#if DEBUG
							GCHandle instanceHandle = DebugGCHandle.Alloc(instance);
#else
							GCHandle instanceHandle = GCHandle.Alloc(instance);
#endif
							CallSmokeMethod(methodId.smoke, methodId.index, (IntPtr) instanceHandle, (IntPtr) stackPtr, args.Length / 2, (IntPtr) typeIDsPtr);
#if DEBUG
							DebugGCHandle.Free(instanceHandle);
#else
							instanceHandle.Free();
#endif
						}
						if (returnType != typeof(void)) {
							returnValue = SmokeMarshallers.BoxFromStackItem(returnType, typeIDs[0], stackPtr);
						}

						if (refArgs) {
							for (int i = 1, k = 1; i < args.Length; i += 2, k++) {
								Type t = args[i].GetType();
								if (t.IsPrimitive || t == typeof(NativeLong) || t == typeof(NativeULong)) {
									args[i] = SmokeMarshallers.BoxFromStackItem(t, typeIDs[k], stackPtr + k);
								}
							}
						}

						return returnValue;
					}
				}
			}
		}
Example #6
0
 public static unsafe TypeId UnboxToStackItem(object o, StackItem* item)
 {
     if (o == null)
     {
         item->s_class = IntPtr.Zero;
         return TypeId.t_class;
     }
     if (o is Enum)
     {
         if (SizeOfNativeLong <= sizeof(long))
         {
             int value = Enum.GetUnderlyingType(o.GetType()) == typeof(long) ? (int) (long) o : (int) o;
             item->s_int = value;
             return TypeId.t_int;
         }
         long val = Enum.GetUnderlyingType(o.GetType()) == typeof(long) ? (long) o : (int) o;
         item->s_long = val;
         return TypeId.t_long;
     }
     if (o is int)
     {
         item->s_int = (int) o;
         return TypeId.t_int;
     }
     if (o is bool)
     {
         item->s_bool = (bool) o;
         return TypeId.t_bool;
     }
     if (o is short)
     {
         item->s_short = (short) o;
         return TypeId.t_short;
     }
     if (o is float)
     {
         item->s_float = (float) o;
         return TypeId.t_float;
     }
     if (o is double)
     {
         item->s_double = (double) o;
         return TypeId.t_double;
     }
     if (o is NativeLong)
     {
         if (SizeOfNativeLong <= sizeof (long))
         {
             item->s_int = (int) (NativeLong) o;
             return TypeId.t_int;
         }
         item->s_long = (NativeLong) o;
         return TypeId.t_long;
     }
     if (o is ushort)
     {
         item->s_ushort = (ushort) o;
         return TypeId.t_ushort;
     }
     if (o is uint)
     {
         item->s_uint = (uint) o;
         return TypeId.t_uint;
     }
     if (o is NativeULong)
     {
         if (SizeOfNativeLong <= sizeof (long))
         {
             item->s_uint = (uint) (NativeULong) o;
             return TypeId.t_uint;
         }
         item->s_ulong = (NativeULong) o;
         return TypeId.t_ulong;
     }
     if (o is long)
     {
         item->s_long = (long) o;
         return TypeId.t_long;
     }
     if (o is ulong)
     {
         item->s_ulong = (ulong) o;
         return TypeId.t_ulong;
     }
     if (o is char)
     {
         item->s_char = (char) o;
         return TypeId.t_uchar;
     }
     if (o is byte)
     {
         item->s_uchar = (byte) o;
         return TypeId.t_uchar;
     }
     string text = o as string;
     if (text != null)
     {
         item->s_class = Marshal.StringToHGlobalUni(text);
         return TypeId.t_string;
     }
     Delegate @delegate = o as Delegate;
     if (@delegate != null)
     {
         item->s_class = Marshal.GetFunctionPointerForDelegate(@delegate);
         return TypeId.t_class;
     }
     #if DEBUG
     item->s_class = (IntPtr) DebugGCHandle.Alloc(o);
     #else
     item->s_class = (IntPtr) GCHandle.Alloc(o);
     #endif
     if (o is QBitArray)
     {
         return TypeId.t_bitArray;
     }
     if (o is QByteArray)
     {
         return TypeId.t_byteArray;
     }
     if (o is QDate)
     {
         return TypeId.t_date;
     }
     if (o is IList<QVariant>)
     {
         return TypeId.t_list;
     }
     if (o is QLocale)
     {
         return TypeId.t_locale;
     }
     if (o is IDictionary<string, QVariant>)
     {
         return TypeId.t_map;
     }
     if (o is QPoint)
     {
         return TypeId.t_point;
     }
     if (o is QPointF)
     {
         return TypeId.t_pointF;
     }
     if (o is QRect)
     {
         return TypeId.t_rect;
     }
     if (o is QRectF)
     {
         return TypeId.t_rectF;
     }
     if (o is QRegExp)
     {
         return TypeId.t_regExp;
     }
     if (o is QSize)
     {
         return TypeId.t_size;
     }
     if (o is QSizeF)
     {
         return TypeId.t_sizeF;
     }
     if (o is IList<string>)
     {
         return TypeId.t_stringList;
     }
     if (o is QTime)
     {
         return TypeId.t_time;
     }
     if (o is QUrl)
     {
         return TypeId.t_url;
     }
     return TypeId.t_class;
 }