public static Delegate System_Reflection_TypeFilter(LuaFunction func)
 {
     System.Reflection.TypeFilter d = (param0, param1) =>
     {
         int    top = func.BeginPCall();
         IntPtr L   = func.GetLuaState();
         LuaScriptMgr.Push(L, param0);
         LuaScriptMgr.PushVarObject(L, param1);
         func.PCall(top, 2);
         object[] objs = func.PopValues(top);
         func.EndPCall(top);
         return((bool)objs[0]);
     };
     return(d);
 }
        static internal int checkDelegate(IntPtr l, int p, out System.Reflection.TypeFilter ua)
        {
            int op = extractFunction(l, p);

            if (LuaDLL.lua_isnil(l, p))
            {
                ua = null;
                return(op);
            }
            else if (LuaDLL.lua_isuserdata(l, p) == 1)
            {
                ua = (System.Reflection.TypeFilter)checkObj(l, p);
                return(op);
            }
            if (LuaDLL.lua_isnil(l, -1))
            {
                ua = null;
                return(op);
            }
            LuaDelegate ld;

            checkType(l, -1, out ld);
            LuaDLL.lua_pop(l, 1);
            if (ld.d != null)
            {
                ua = (System.Reflection.TypeFilter)ld.d;
                return(op);
            }

            l  = LuaState.get(l).L;
            ua = (System.Type a1, System.Object a2) =>
            {
                int error = pushTry(l);

                pushValue(l, a1);
                pushValue(l, a2);
                ld.pcall(2, error);
                bool ret;
                checkType(l, error + 1, out ret);
                LuaDLL.lua_settop(l, error - 1);
                return(ret);
            };
            ld.d = ua;
            return(op);
        }
 public virtual Type[] FindInterfaces(System.Reflection.TypeFilter filter, object filterCriteria)
 {
 }
예제 #4
0
 public virtual System.Type[] FindTypes(System.Reflection.TypeFilter filter, object filterCriteria)
 {
     throw null;
 }
예제 #5
0
 public virtual new System.Type[] FindInterfaces(System.Reflection.TypeFilter filter, Object filterCriteria)
 {
     return(default(System.Type[]));
 }
예제 #6
0
        public unsafe void DeepCopyFromImplementation(IntPtr from, out object to, Type target_type, bool reset = true)
        {
            try
            {
                if (reset && _level == 0)
                {
                    ClearAllocatedObjects();
                }

                _level++;

                Type t_type = target_type;
                Type f_type = CreateImplementationType(t_type);

                if (t_type.FullName.Equals("System.Object"))
                {
                    object o = Marshal.PtrToStructure <System.Object>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.Int16"))
                {
                    object o = Marshal.PtrToStructure <System.Int16>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.Int32"))
                {
                    object o = Marshal.PtrToStructure <System.Int32>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.Int64"))
                {
                    object o = Marshal.PtrToStructure <System.Int64>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.UInt16"))
                {
                    object o = Marshal.PtrToStructure <System.UInt16>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.UInt32"))
                {
                    object o = Marshal.PtrToStructure <System.UInt32>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.UInt64"))
                {
                    object o = Marshal.PtrToStructure <System.UInt64>(from);
                    to = o;
                    return;
                }
                if (t_type.FullName.Equals("System.IntPtr"))
                {
                    object o = Marshal.PtrToStructure <System.IntPtr>(from);
                    to = o;
                    return;
                }

                // Map boolean into byte.
                if (t_type.FullName.Equals("System.Boolean"))
                {
                    byte v = *(byte *)from;
                    to = (System.Boolean)(v == 1 ? true : false);
                    return;
                }

                // Map char into uint16.
                if (t_type.FullName.Equals("System.Char"))
                {
                    to = (System.Char)from;
                    return;
                }

                String name;
                System.Reflection.TypeFilter tf;
                Type bbt = null;

                //// Declare inheritance types.
                //if (declare_parent_chain)
                //{
                //    // First, declare base type
                //    Type bt = hostType.BaseType;
                //    if (bt != null && !bt.FullName.Equals("System.Object"))
                //    {
                //        bbt = CreateImplementationType(bt, declare_parent_chain, declare_flatten_structure);
                //    }
                //}

                name = f_type.FullName;
                name = name.Replace("[", "\\[").Replace("]", "\\]");
                tf   = new System.Reflection.TypeFilter((Type t, object o) =>
                {
                    return(t.FullName == name);
                });


                if (t_type.IsArray)
                {
                    // "from" is assumed to be a unmanaged buffer
                    // with three fields, "ptr", "len", "data".
                    int    len        = *(int *)((long)(byte *)from + Marshal.SizeOf(typeof(IntPtr)));
                    IntPtr intptr_src = *(IntPtr *)(from);
                    byte * ptr        = (byte *)intptr_src;
                    // For now, only one-dimension, given "len".
                    var to_array = Array.CreateInstance(t_type.GetElementType(), new int[1] {
                        len
                    });
                    _allocated_objects[to_array] = (IntPtr)ptr;
                    Cp((void *)ptr, to_array, t_type.GetElementType());
                    to = to_array;
                    return;
                }

                if (IsStruct(t_type) || t_type.IsClass)
                {
                    IntPtr ip = from;
                    if (ip == IntPtr.Zero)
                    {
                        to = null;
                        return;
                    }

                    to = Activator.CreateInstance(t_type);
                    _allocated_objects[to] = ip;

                    System.Reflection.FieldInfo[] ffi = f_type.GetFields();
                    System.Reflection.FieldInfo[] tfi = t_type.GetFields();

                    for (int i = 0; i < ffi.Length; ++i)
                    {
                        var ffield = ffi[i];
                        var tfield = tfi.Where(k => k.Name == ffield.Name).FirstOrDefault();
                        if (tfield == null)
                        {
                            throw new ArgumentException("Field not found.");
                        }
                        // Note, special case all field types.
                        if (tfield.FieldType.IsArray)
                        {
                            int    field_size = Marshal.SizeOf(typeof(IntPtr));
                            IntPtr ipv        = (IntPtr)Marshal.PtrToStructure <IntPtr>(ip);
                            if (_allocated_objects.ContainsValue(ipv))
                            {
                                object tooo = _allocated_objects.Where(p => p.Value == ipv).Select(p => p.Key)
                                              .FirstOrDefault();
                                tfield.SetValue(to, tooo);
                            }
                            else
                            {
                                DeepCopyFromImplementation(ipv, out object tooo, tfield.FieldType);
                                tfield.SetValue(to, tooo);
                            }
                            ip = (IntPtr)((long)ip + field_size);
                        }
                        else if (tfield.FieldType.IsClass)
                        {
                            int    field_size = Marshal.SizeOf(typeof(IntPtr));
                            IntPtr ipv        = (IntPtr)Marshal.PtrToStructure <IntPtr>(ip);
                            if (_allocated_objects.ContainsValue(ipv))
                            {
                                object tooo = _allocated_objects.Where(p => p.Value == ipv).Select(p => p.Key)
                                              .FirstOrDefault();
                                tfield.SetValue(to, tooo);
                            }
                            else
                            {
                                DeepCopyFromImplementation(ipv, out object tooo, tfield.FieldType);
                                tfield.SetValue(to, tooo);
                            }
                            ip = (IntPtr)((long)ip + field_size);
                        }
                        else
                        {
                            int field_size = Marshal.SizeOf(ffield.FieldType);
                            DeepCopyFromImplementation(ip, out object tooo, tfield.FieldType);
                            tfield.SetValue(to, tooo);
                            ip = (IntPtr)((long)ip + field_size);
                        }
                    }

                    return;
                }

                throw new Exception("Unknown type.");
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Exception");
                System.Console.WriteLine(e);
                throw e;
            }
            finally
            {
                _level--;
            }
        }
예제 #7
0
        /// <summary>
        /// This method copies from a managed type into a blittable managed type.
        /// The type is converted from managed into a blittable managed type.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public unsafe void DeepCopyToImplementation(object from, void *to_buffer)
        {
            // Copy object to a buffer.
            try
            {
                _level++;

                {
                    bool is_null = false;
                    try
                    {
                        if (from == null)
                        {
                            is_null = true;
                        }
                        else if (from.Equals(null))
                        {
                            is_null = true;
                        }
                    }
                    catch (Exception e)
                    {
                    }
                    if (is_null)
                    {
                        throw new Exception("Unknown type of object.");
                    }
                }

                Type hostType = from.GetType();

                // Let's start with basic types.
                if (hostType.FullName.Equals("System.Object"))
                {
                    throw new Exception("Type is System.Object, but I don't know what to represent it as.");
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.Int16"))
                {
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.Int32"))
                {
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.Int64"))
                {
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.UInt16"))
                {
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.UInt32"))
                {
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.UInt64"))
                {
                    Cp(to_buffer, from);
                    return;
                }
                if (hostType.FullName.Equals("System.IntPtr"))
                {
                    Cp(to_buffer, from);
                    return;
                }

                // Map boolean into byte.
                if (hostType.FullName.Equals("System.Boolean"))
                {
                    bool        v  = (bool)from;
                    System.Byte v2 = (System.Byte)(v ? 1 : 0);
                    Cp(to_buffer, v2);
                    return;
                }

                // Map char into uint16.
                if (hostType.FullName.Equals("System.Char"))
                {
                    Char          v  = (Char)from;
                    System.UInt16 v2 = (System.UInt16)v;
                    Cp(to_buffer, v2);
                    return;
                }

                //// Declare inheritance types.
                //Type bbt = null;
                //if (declare_parent_chain)
                //{
                //    // First, declare base type
                //    Type bt = hostType.BaseType;
                //    if (bt != null && !bt.FullName.Equals("System.Object"))
                //    {
                //        bbt = CreateImplementationType(bt, declare_parent_chain, declare_flatten_structure);
                //    }
                //}

                String name = hostType.FullName;
                name = name.Replace("[", "\\[").Replace("]", "\\]");
                System.Reflection.TypeFilter tf = new System.Reflection.TypeFilter((Type t, object o) =>
                {
                    return(t.FullName == name);
                });

                // Find blittable type for hostType.
                Type[] types = _asm.mb.FindTypes(tf, null);

                if (types.Length == 0)
                {
                    throw new Exception("Unknown type.");
                }
                Type blittable_type = types[0];

                if (hostType.IsArray)
                {
                    if (_allocated_objects.ContainsKey(from))
                    {
                        // Full object already stuffed into implementation buffer.
                    }
                    else
                    {
                        _allocated_objects[from] = (IntPtr)to_buffer;

                        // An array is represented as a pointer/length struct.
                        // The data in the array is contained in the buffer following the length.
                        // The buffer allocated must be big enough to contain all data. Use
                        // Buffer.SizeOf(array) to get the representation buffer size.
                        // If the element is an array or a class, a buffer is allocated for each
                        // element, and an intptr used in the array.
                        var a = (Array)from;
                        var blittable_element_type = CreateImplementationType(from.GetType().GetElementType());
                        var len = a.Length;
                        int bytes;
                        if (from.GetType().GetElementType().IsClass)
                        {
                            // We create a buffer for the class, and stuff a pointer in the array.
                            bytes = Marshal.SizeOf(typeof(IntPtr))          // Pointer
                                    + Marshal.SizeOf(typeof(Int32))         // length
                                    + Marshal.SizeOf(typeof(IntPtr)) * len; // elements
                        }
                        else
                        {
                            bytes = Marshal.SizeOf(typeof(IntPtr))                  // Pointer
                                    + Marshal.SizeOf(typeof(Int32))                 // length
                                    + Marshal.SizeOf(blittable_element_type) * len; // elements
                        }
                        var   destIntPtr = (byte *)to_buffer;
                        byte *df0        = destIntPtr;
                        byte *df1        = Marshal.SizeOf(typeof(IntPtr)) // Pointer
                                           + destIntPtr;
                        byte *df2 = Marshal.SizeOf(typeof(IntPtr))        // Pointer
                                    + Marshal.SizeOf(typeof(Int32))       // length
                                    + destIntPtr;
                        Cp(df0, (IntPtr)df2);                             // Copy df2 to *df0
                        Cp(df1, len);
                        Cp(df2, a, CreateImplementationType(a.GetType().GetElementType()));
                    }
                    return;
                }

                if (IsStruct(hostType) || hostType.IsClass)
                {
                    if (_allocated_objects.ContainsKey(from))
                    {
                        // Full object already stuffed into implementation buffer.
                    }
                    else
                    {
                        if (from.GetType().IsClass)
                        {
                            _allocated_objects[from] = (IntPtr)to_buffer;
                        }

                        Type  f    = from.GetType();
                        Type  tr   = blittable_type;
                        int   size = Marshal.SizeOf(tr);
                        void *ip   = to_buffer;

                        System.Reflection.FieldInfo[] ffi = f.GetFields();
                        System.Reflection.FieldInfo[] tfi = tr.GetFields();

                        foreach (System.Reflection.FieldInfo fi in ffi)
                        {
                            object field_value = fi.GetValue(from);
                            String na          = fi.Name;
                            var    tfield      = tfi.Where(k => k.Name == fi.Name).FirstOrDefault();
                            if (tfield == null)
                            {
                                throw new ArgumentException("Field not found.");
                            }
                            var field_size = Marshal.SizeOf(tfield.FieldType);
                            if (fi.FieldType.IsArray)
                            {
                                // Allocate a whole new buffer, copy to that, place buffer pointer into field at ip.
                                if (field_value != null)
                                {
                                    Array ff    = (Array)field_value;
                                    var   size2 = Marshal.SizeOf(typeof(IntPtr))               // Pointer
                                                  + Marshal.SizeOf(typeof(Int32))              // length
                                                  + Marshal.SizeOf(typeof(Int32)) * ff.Length; // Array values
                                    if (_allocated_objects.ContainsKey(field_value))
                                    {
                                        IntPtr gp = _allocated_objects[field_value];
                                        DeepCopyToImplementation(gp, ip);
                                    }
                                    else
                                    {
                                        IntPtr gp = New(size2);
                                        DeepCopyToImplementation(field_value, gp);
                                        DeepCopyToImplementation(gp, ip);
                                    }
                                }
                                else
                                {
                                    field_value = IntPtr.Zero;
                                    DeepCopyToImplementation(field_value, ip);
                                }
                                ip = (void *)((long)ip + Marshal.SizeOf(typeof(IntPtr)));
                            }
                            else if (fi.FieldType.IsClass)
                            {
                                // Allocate a whole new buffer, copy to that, place buffer pointer into field at ip.
                                if (field_value != null)
                                {
                                    if (_allocated_objects.ContainsKey(field_value))
                                    {
                                        IntPtr gp = _allocated_objects[field_value];
                                        DeepCopyToImplementation(gp, ip);
                                    }
                                    else
                                    {
                                        var    size2 = Marshal.SizeOf(tfield.FieldType);
                                        IntPtr gp    = New(size2);
                                        DeepCopyToImplementation(field_value, gp);
                                        DeepCopyToImplementation(gp, ip);
                                    }
                                }
                                else
                                {
                                    field_value = IntPtr.Zero;
                                    DeepCopyToImplementation(field_value, ip);
                                }
                                ip = (void *)((long)ip + Marshal.SizeOf(typeof(IntPtr)));
                            }
                            else if (IsStruct(fi.FieldType))
                            {
                                throw new Exception("Whoops.");
                            }
                            else
                            {
                                DeepCopyToImplementation(field_value, ip);
                                ip = (void *)((long)ip + field_size);
                            }
                        }
                    }
                    return;
                }

                throw new Exception("Unknown type.");
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Exception");
                System.Console.WriteLine(e);
                throw e;
            }
            finally
            {
                _level--;
            }
        }
예제 #8
0
        public Type CreateImplementationType(Type hostType, bool declare_parent_chain = true, bool declare_flatten_structure = false)
        {
            try
            {
                // Let's start with basic types.
                if (hostType.FullName.Equals("System.Object"))
                {
                    return(typeof(System.Object));
                }
                if (hostType.FullName.Equals("System.Int16"))
                {
                    return(typeof(System.Int16));
                }
                if (hostType.FullName.Equals("System.Int32"))
                {
                    return(typeof(System.Int32));
                }
                if (hostType.FullName.Equals("System.Int64"))
                {
                    return(typeof(System.Int64));
                }
                if (hostType.FullName.Equals("System.UInt16"))
                {
                    return(typeof(System.UInt16));
                }
                if (hostType.FullName.Equals("System.UInt32"))
                {
                    return(typeof(System.UInt32));
                }
                if (hostType.FullName.Equals("System.UInt64"))
                {
                    return(typeof(System.UInt64));
                }
                if (hostType.FullName.Equals("System.IntPtr"))
                {
                    return(typeof(System.IntPtr));
                }

                // Map boolean into byte.
                if (hostType.FullName.Equals("System.Boolean"))
                {
                    return(typeof(System.Byte));
                }

                // Map char into uint16.
                if (hostType.FullName.Equals("System.Char"))
                {
                    return(typeof(System.UInt16));
                }

                String name;
                System.Reflection.TypeFilter tf;
                Type bbt = null;

                // Declare inheritance types.
                if (declare_parent_chain)
                {
                    // First, declare base type
                    Type bt = hostType.BaseType;
                    bbt = bt;
                    if (bt != null && !bt.FullName.Equals("System.Object") && !bt.FullName.Equals("System.ValueType"))
                    {
                        bbt = CreateImplementationType(bt, declare_parent_chain, declare_flatten_structure);
                    }
                }

                name = hostType.FullName;
                _type_name_map.TryGetValue(name, out string alt);
                tf = new System.Reflection.TypeFilter((Type t, object o) =>
                {
                    return(t.FullName == name || t.FullName == alt);
                });

                // Find if blittable type for hostType was already performed.
                Type[] types = _asm.mb.FindTypes(tf, null);

                // If blittable type was not created, create one with all fields corresponding
                // to that in host, with special attention to arrays.
                if (types.Length != 0)
                {
                    return(types[0]);
                }

                if (hostType.IsArray)
                {
                    Type elementType = CreateImplementationType(hostType.GetElementType(), declare_parent_chain,
                                                                declare_flatten_structure);
                    object array_obj  = Array.CreateInstance(elementType, 0);
                    Type   array_type = array_obj.GetType();

                    // For arrays, convert into a struct with first field being a
                    // pointer, and the second field a length.

                    var tb = _asm.mb.DefineType(
                        array_type.FullName,
                        System.Reflection.TypeAttributes.Public
                        | System.Reflection.TypeAttributes.SequentialLayout
                        | System.Reflection.TypeAttributes.Serializable);
                    _type_name_map[hostType.FullName] = tb.FullName;

                    // Convert byte, int, etc., in host type to pointer in blittable type.
                    // With array, we need to also encode the length.
                    tb.DefineField("ptr", typeof(IntPtr), System.Reflection.FieldAttributes.Public);
                    tb.DefineField("len", typeof(Int32), System.Reflection.FieldAttributes.Public);

                    return(tb.CreateType());
                }
                else if (IsStruct(hostType))
                {
                    TypeBuilder tb = null;
                    if (bbt != null)
                    {
                        tb = _asm.mb.DefineType(
                            name,
                            System.Reflection.TypeAttributes.Public
                            | System.Reflection.TypeAttributes.SequentialLayout
                            | System.Reflection.TypeAttributes.Serializable,
                            bbt);
                    }
                    else
                    {
                        tb = _asm.mb.DefineType(
                            name,
                            System.Reflection.TypeAttributes.Public
                            | System.Reflection.TypeAttributes.SequentialLayout
                            | System.Reflection.TypeAttributes.Serializable);
                    }
                    _type_name_map[name] = tb.FullName;
                    Type ht = hostType;
                    while (ht != null)
                    {
                        var fields = ht.GetFields(
                            System.Reflection.BindingFlags.Instance
                            | System.Reflection.BindingFlags.NonPublic
                            | System.Reflection.BindingFlags.Public
                            | System.Reflection.BindingFlags.Static);
                        var fields2 = ht.GetFields();
                        foreach (var field in fields)
                        {
                            // For non-array type fields, just define the field as is.
                            // Recurse
                            Type elementType = CreateImplementationType(field.FieldType, declare_parent_chain,
                                                                        declare_flatten_structure);
                            // If elementType is a reference or array, then we need to convert it to a IntPtr.
                            if (elementType.IsClass || elementType.IsArray)
                            {
                                elementType = typeof(System.IntPtr);
                            }
                            tb.DefineField(field.Name, elementType, System.Reflection.FieldAttributes.Public);
                        }
                        if (declare_flatten_structure)
                        {
                            ht = ht.BaseType;
                        }
                        else
                        {
                            ht = null;
                        }
                    }
                    // Base type will be used.
                    return(tb.CreateType());
                }
                else if (hostType.IsClass)
                {
                    TypeBuilder tb = null;
                    if (bbt != null)
                    {
                        tb = _asm.mb.DefineType(
                            name,
                            System.Reflection.TypeAttributes.Public
                            | System.Reflection.TypeAttributes.SequentialLayout
                            | System.Reflection.TypeAttributes.Serializable,
                            bbt);
                    }
                    else
                    {
                        tb = _asm.mb.DefineType(
                            name,
                            System.Reflection.TypeAttributes.Public
                            | System.Reflection.TypeAttributes.SequentialLayout
                            | System.Reflection.TypeAttributes.Serializable);
                    }
                    _type_name_map[name] = tb.FullName;
                    Type ht = hostType;
                    while (ht != null)
                    {
                        var fields = ht.GetFields(
                            System.Reflection.BindingFlags.Instance
                            | System.Reflection.BindingFlags.NonPublic
                            | System.Reflection.BindingFlags.Public
                            | System.Reflection.BindingFlags.Static);
                        var fields2 = ht.GetFields();
                        foreach (var field in fields)
                        {
                            // For non-array type fields, just define the field as is.
                            // Recurse
                            Type elementType = CreateImplementationType(field.FieldType, declare_parent_chain,
                                                                        declare_flatten_structure);
                            // If elementType is a reference or array, then we need to convert it to a IntPtr.
                            if (elementType.IsClass || elementType.IsArray)
                            {
                                elementType = typeof(System.IntPtr);
                            }
                            tb.DefineField(field.Name, elementType, System.Reflection.FieldAttributes.Public);
                        }
                        if (declare_flatten_structure)
                        {
                            ht = ht.BaseType;
                        }
                        else
                        {
                            ht = null;
                        }
                    }
                    // Base type will be used.
                    return(tb.CreateType());
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception e)
            {
                System.Console.WriteLine("Exception");
                System.Console.WriteLine(e);
                throw e;
            }
            finally
            {
            }
        }