Ejemplo n.º 1
0
        public static T RequiresNotNull <T>(object obj)
        {
            if (obj == null)
            {
                return((T)Builtins.AssertionViolation(GetCaller(), "argument cannot be null"));
            }

            if (!(obj is T))
            {
                // no way pass a box op :(
                object o = ConvertFromSchemeObject <T>(obj);
                if (o != null)
                {
                    return((T)o);
                }
                if (typeof(T) == typeof(Callable))
                {
                    return((T)Builtins.AssertionViolation(GetCaller(), "expected procedure", obj));
                }
                else
                {
                    return((T)Builtins.AssertionViolation(GetCaller(), "expected type: " + typeof(T).Name, obj));
                }
            }

            return((T)obj);
        }
Ejemplo n.º 2
0
        public static T[] RequiresArray <T>(object obj)
        {
            if (obj is byte[] && typeof(T) == typeof(byte))
            {
                return(obj as T[]);
            }

            if (obj is T[])
            {
                return(obj as T[]);
            }


            object[] arr = obj as object[];

            if (arr == null)
            {
                Builtins.AssertionViolation(GetCaller(), "expected type: " + typeof(T).Name, obj);
            }

            if (typeof(T) == typeof(object))
            {
                return((T[])obj);
            }

            T[] ret = new T[arr.Length];

            for (int i = 0; i < arr.Length; i++)
            {
                ret[i] = Requires <T>(arr[i]);
            }

            return(ret);
        }
Ejemplo n.º 3
0
        internal static object SymbolToEnum(Type type, object symbol)
        {
            if (symbol is SymbolId)
            {
                string name = SymbolTable.IdToString(RequiresNotNull <SymbolId>(symbol));
                try
                {
                    return(Enum.Parse(type, name, true));
                }
                catch (Exception ex)
                {
                    return(Builtins.AssertionViolation("symbol-to-enum", ex.Message, symbol, type));
                }
            }
            else if (symbol is Cons)
            {
                int v = 0;
                foreach (object n in symbol as Cons)
                {
                    v |= (int)SymbolToEnum(type, n);
                }

                return(v);
            }
            else
            {
                return(Builtins.AssertionViolation("symbol-to-enum", "Not a valid type", symbol, type));
            }
        }
Ejemplo n.º 4
0
        public static T SymbolToEnum <T>(object symbol)
        {
            if (symbol is SymbolId)
            {
                string name = SymbolTable.IdToString(RequiresNotNull <SymbolId>(symbol));
                try
                {
                    return((T)Enum.Parse(typeof(T), name, true));
                }
                catch (Exception ex)
                {
                    Builtins.AssertionViolation("symbol-to-enum", ex.Message, symbol, typeof(T));
                    return(default(T));
                }
            }
            else if (symbol is Cons)
            {
                int v = 0;
                foreach (object n in symbol as Cons)
                {
                    v |= (int)(object)SymbolToEnum <T>(n);
                }

                return((T)(object)v);
            }
            else
            {
                Builtins.AssertionViolation("symbol-to-enum", "Not a valid type", symbol, typeof(T));
                return(default(T));
            }
        }
Ejemplo n.º 5
0
        public static object Disassemble(object proc, object argcount)
        {
            if (proc is Closure)
            {
                var c  = RequiresNotNull <Closure>(proc);
                var st = c.Targets;
                var vt = c.VarargTargets;
                var tc = st.Length + vt.Length;
                // implies case closure
                if (tc > 1)
                {
                    // check for valid arg count
                    if (argcount != FALSE)
                    {
                        int ac = Requires <int>(argcount);
                        // now figure out what can be used...

                        foreach (var m in st)
                        {
                            if (m.GetParameters().Length == ac)
                            {
                                return(DisassembleMethod(m));
                            }
                        }

                        foreach (var m in vt)
                        {
                            if (m.GetParameters().Length <= ac - 1)
                            {
                                return(DisassembleMethod(m));
                            }
                        }

                        return(Builtins.AssertionViolation("disassemble", "procedure ambiguation failed", proc, argcount));
                    }
                    else
                    {
                        return(Builtins.AssertionViolation("disassemble", "procedure ambiguation requires an argument count parameter", proc));
                    }
                }
                else if (tc == 0)
                {
                    return(Builtins.AssertionViolation("disassemble", "not possible on procedure", proc));
                }

                if (st.Length == 1)
                {
                    return(DisassembleMethod(st[0]));
                }
                else
                {
                    return(DisassembleMethod(vt[0]));
                }
            }
            else
            {
                return(Builtins.AssertionViolation("disassemble", "builtin procedures not supported, consult the source code", proc));
            }
        }
Ejemplo n.º 6
0
        public static T FFIDelegate <T>(IntPtr ptr) where T : class
        {
            if (ptr == IntPtr.Zero)
            {
                Builtins.AssertionViolation("FFIDelegate", "pointer cannot be null", ptr);
            }
            var del = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T));

            return((T)(object)del);
        }
Ejemplo n.º 7
0
        public static T FFIConvertTo <T>(object obj)
        {
            var objtype = obj == null ? typeof(IntPtr) : obj.GetType();

            if (objtype == typeof(T))
            {
                if (obj == null)
                {
                    return((T)(object)IntPtr.Zero);
                }
                return((T)obj);
            }

            var tc = Type.GetTypeCode(typeof(T));

            switch (tc)
            {
            case TypeCode.Int32:
                return((T)(object)Convert.ToInt32(obj));

            case TypeCode.UInt32:
                return((T)(object)Convert.ToUInt32(obj));

            case TypeCode.Int64:
                return((T)(object)Convert.ToInt64(obj));

            case TypeCode.UInt64:
                return((T)(object)Convert.ToUInt64(obj));

            case TypeCode.Int16:
                return((T)(object)Convert.ToInt16(obj));

            case TypeCode.UInt16:
                return((T)(object)Convert.ToUInt16(obj));

            case TypeCode.Byte:
                return((T)(object)Convert.ToByte(obj));

            case TypeCode.SByte:
                return((T)(object)Convert.ToSByte(obj));

            case TypeCode.Single:
                return((T)(object)Convert.ToSingle(obj));

            case TypeCode.Double:
                return((T)(object)Convert.ToDouble(obj));

            default:
                if (typeof(T) == typeof(IntPtr))
                {
                    return((T)(object)new IntPtr(Convert.ToInt32(obj)));
                }
                return((T)Builtins.AssertionViolation("FFIConvertTo", "not a known ffi type", obj, objtype));
            }
        }
Ejemplo n.º 8
0
 public static T Requires <T>(object obj)
 {
     if (obj != null && !(obj is T))
     {
         // no way pass a box op :(
         object o = ConvertFromSchemeObject <T>(obj);
         if (o != null)
         {
             return((T)o);
         }
         Builtins.AssertionViolation(GetCaller(), "expected type: " + typeof(T).Name, obj);
     }
     if (obj == null && typeof(T).IsValueType)
     {
         Builtins.AssertionViolation(GetCaller(), "value type cannot be null", typeof(T).Name);
     }
     return((T)obj);
 }
Ejemplo n.º 9
0
        public static T ConvertToDelegate <T>(object proc)
        {
            if (!(proc is Callable))
            {
                Builtins.AssertionViolation("ConvertToDelegate", "not a procedure", proc);
            }
            MethodInfo meth = typeof(T).GetMethod("Invoke");

            ParameterInfo[] pars = meth.GetParameters();
            if (meth.ReturnType == typeof(void))
            {
                Delegate d = MakeVoidTypedCallable(Array.ConvertAll(pars, x => x.ParameterType), proc as Callable);
                return((T)(object)Delegate.CreateDelegate(typeof(T), d.Target, d.Method));
            }
            else
            {
                Delegate d = MakeTypedCallable(meth.ReturnType, Array.ConvertAll(pars, x => x.ParameterType), proc as Callable);
                return((T)(object)Delegate.CreateDelegate(typeof(T), d.Target, d.Method));
            }
        }
Ejemplo n.º 10
0
        public static object FFIConvertFrom <T>(T value)
        {
            var tc = Type.GetTypeCode(typeof(T));

            switch (tc)
            {
            case TypeCode.Int32:
            case TypeCode.Double:
                return(value);

            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                return(Builtins.ToIntegerIfPossible(GetValue(value)));

            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Byte:
            case TypeCode.SByte:
                return(Convert.ToInt32(value));

            case TypeCode.Single:
                return(Convert.ToDouble(value));

            case TypeCode.String:
                return(Convert.ToString(value));

            default:
                if (typeof(T) == typeof(IntPtr) ||
                    typeof(T) == typeof(UIntPtr))
                {
                    return(value);
                }
                return(Builtins.AssertionViolation("FFIConvertTo", "not a known ffi type", value, typeof(T)));
            }
        }