Пример #1
0
        static int _m_emitPropertyWrap(RealStatePtr L)
        {
            try {
                ObjectTranslator translator = ObjectTranslatorPool.Instance.Find(L);


                XLua.CodeEmit gen_to_be_invoked = (XLua.CodeEmit)translator.FastGetCSObj(L, 1);



                {
                    System.Reflection.Emit.TypeBuilder _typeBuilder = (System.Reflection.Emit.TypeBuilder)translator.GetObject(L, 2, typeof(System.Reflection.Emit.TypeBuilder));
                    System.Reflection.PropertyInfo     _prop        = (System.Reflection.PropertyInfo)translator.GetObject(L, 3, typeof(System.Reflection.PropertyInfo));
                    System.Reflection.MethodInfo       _op          = (System.Reflection.MethodInfo)translator.GetObject(L, 4, typeof(System.Reflection.MethodInfo));
                    bool _genGetter = LuaAPI.lua_toboolean(L, 5);

                    System.Reflection.Emit.MethodBuilder gen_ret = gen_to_be_invoked.emitPropertyWrap(_typeBuilder, _prop, _op, _genGetter);
                    translator.Push(L, gen_ret);



                    return(1);
                }
            } catch (System.Exception gen_e) {
                return(LuaAPI.luaL_error(L, "c# exception:" + gen_e));
            }
        }
Пример #2
0
 /// <summary>
 /// Calls a method on this variable
 /// </summary>
 /// <param name="Method">Method</param>
 /// <param name="Parameters">Parameters sent in</param>
 /// <returns>Variable returned by the function (if one exists, null otherwise)</returns>
 public virtual VariableBase Call(System.Reflection.Emit.MethodBuilder Method, object[] Parameters = null)
 {
     if (Method == null)
     {
         throw new ArgumentNullException("Method");
     }
     return(MethodBase.CurrentMethod.Call(this, Method, Parameters));
 }
        private void CreateProperty(Generators.TypeGenerator generator, System.Type returnType, ParameterInfo[] parameters)
        {
            var parameterTypes = parameters.Map(p => p.Type);

            Generators.PropertyGenerator.PropertyHolder accessor = null;
            System.Type type    = null;
            var         name    = string.Concat(char.ToUpper(Name.First()), Name.Substring(1));
            var         builder = generator.Builder;

            System.Reflection.MethodAttributes attributes = GetAttributes();
            if (IsGetter)
            {
                type = returnType;
                string hiddenName = string.Concat("get_", name);
                if ((attributes & System.Reflection.MethodAttributes.Virtual) == System.Reflection.MethodAttributes.Virtual)
                {
                    generator.CheckImplementMethod(Name, parameterTypes, ref hiddenName, ref returnType, ref attributes);
                }
                System.Reflection.Emit.MethodBuilder getBul = builder.DefineMethod(hiddenName, attributes, returnType, parameterTypes);
                accessor = new Generators.PropertyGenerator.PropertyHolder(Generators.PropertyType.Get,
                                                                           new Generators.MethodGenerator(getBul, parameters, generator)
                {
                    SyntaxBody = Body
                });
            }
            if (IsSetter)
            {
                type     = parameterTypes.FirstOrDefault();
                accessor = new Generators.PropertyGenerator.PropertyHolder(Generators.PropertyType.Set,
                                                                           new Generators.MethodGenerator(builder.DefineMethod(string.Concat("set_", name), attributes, returnType, parameterTypes), parameters, generator)
                {
                    SyntaxBody = Body
                });
            }
            if (generator.TryGetProperty(Name, out Generators.PropertyGenerator property) == false)
            {
                var pb = generator.Builder.DefineProperty(name, System.Reflection.PropertyAttributes.None, type, null);
                property = new Generators.PropertyGenerator(generator, pb);
                property.SetCustomAttribute(typeof(Runtime.RegisterAttribute), Utils.ReflectionHelpers.Register_Attr_Ctor, new[] { Name });
                generator.Add(property);
            }

            System.Reflection.Emit.PropertyBuilder propertyBuilder = property.GetBuilder();
            if (IsGetter)
            {
                propertyBuilder.SetGetMethod(accessor.Method.GetBuilder());
            }
            else if (IsSetter)
            {
                propertyBuilder.SetSetMethod(accessor.Method.GetBuilder());
            }
            else
            {
                throw new System.Exception("Accessor not found");
            }
            property.Accessors.Add(accessor);
        }
Пример #4
0
 /// <summary>
 /// Creates a new ReflectionEmitILGenerator instance from a MethodBuilder.
 /// </summary>
 /// <param name="methodBuilder"> The MethodBuilder to emit IL for. </param>
 /// <param name="emitDebugInfo"> Indicates whether to emit debugging information, like symbol names. </param>
 public ReflectionEmitILGenerator(System.Reflection.Emit.MethodBuilder methodBuilder, bool emitDebugInfo)
 {
     if (methodBuilder == null)
     {
         throw new ArgumentNullException(nameof(methodBuilder));
     }
     this.method        = methodBuilder;
     this.generator     = methodBuilder.GetILGenerator();
     this.emitDebugInfo = emitDebugInfo;
 }
Пример #5
0
        /// <summary>
        /// Compiles the lambda into a method definition.
        /// </summary>
        /// <param name="method">A <see cref="Emit.MethodBuilder"/> which will be used to hold the lambda's IL.</param>
        public void CompileToMethod(System.Reflection.Emit.MethodBuilder method)
        {
            ContractUtils.RequiresNotNull(method, nameof(method));
            ContractUtils.Requires(method.IsStatic, nameof(method));
            var type = method.DeclaringType.GetTypeInfo() as System.Reflection.Emit.TypeBuilder;

            if (type == null)
            {
                throw Error.MethodBuilderDoesNotHaveTypeBuilder();
            }

            Compiler.LambdaCompiler.Compile(this, method);
        }
Пример #6
0
        bool EmitDedicatedMethod(Compiler.CompilerContext ctx, Compiler.Local valueFrom, bool read)
        {
            System.Reflection.Emit.MethodBuilder method = ctx.GetDedicatedMethod(key, read);
            if (method == null)
            {
                return(false);
            }

            using (Compiler.Local token = new ProtoBuf.Compiler.Local(ctx, typeof(SubItemToken)))
            {
                Type rwType = read ? typeof(ProtoReader) : typeof(ProtoWriter);
                ctx.LoadValue(valueFrom);
                if (!read) // write requires the object for StartSubItem; read doesn't
                {          // (if recursion-check is disabled [subtypes] then null is fine too)
                    if (type.IsValueType || !recursionCheck)
                    {
                        ctx.LoadNullRef();
                    }
                    else
                    {
                        ctx.CopyValue();
                    }
                }
                ctx.LoadReaderWriter();
                ctx.EmitCall(rwType.GetMethod("StartSubItem"));
                ctx.StoreValue(token);

                // note: value already on the stack
                ctx.LoadReaderWriter();
                ctx.EmitCall(method);
                // handle inheritance (we will be calling the *base* version of things,
                // but we expect Read to return the "type" type)
                if (read && type != method.ReturnType)
                {
                    ctx.Cast(this.type);
                }
                ctx.LoadValue(token);

                ctx.LoadReaderWriter();
                ctx.EmitCall(rwType.GetMethod("EndSubItem"));
            }
            return(true);
        }
Пример #7
0
        static void Main(string[] args)
        {
            System.Reflection.Emit.AssemblyBuilder assemblyBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(new System.Reflection.AssemblyName("Patch"), System.Reflection.Emit.AssemblyBuilderAccess.Run, (string)null);
            System.Reflection.Emit.ModuleBuilder   moduleBuilder   = assemblyBuilder.DefineDynamicModule("module");
            System.Reflection.Emit.TypeBuilder     typeBuilder     = moduleBuilder.DefineType("program", System.Reflection.TypeAttributes.Public);
            System.Reflection.Emit.MethodBuilder   methodBuilder   = typeBuilder.DefineMethod("simpleSub", System.Reflection.MethodAttributes.Static | System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig, typeof(int), new Type[] { typeof(int), typeof(int) });

            List <MSIL.Instruction> patchedMethod = new List <MSIL.Instruction>();

            foreach (MSIL.Instruction instruction in MSIL.ReadMethod(typeof(Program).GetMethod("simpleAdd")))
            {
                if (instruction.OpCode == System.Reflection.Emit.OpCodes.Add)
                {
                    instruction.OpCode = System.Reflection.Emit.OpCodes.Sub;
                }
                patchedMethod.Add(instruction);
            }
            MSIL.EmitMethod(patchedMethod, methodBuilder);

            Type type = typeBuilder.CreateType();

            System.Reflection.MethodInfo pathedMethod = type.GetMethod("simpleSub");
            System.Console.WriteLine("result: " + (int)pathedMethod.Invoke(null, new object[] { 3, 1 }));
        }
Пример #8
0
 public void AddOtherMethod(System.Reflection.Emit.MethodBuilder mdBuilder)
 {
     throw new PlatformNotSupportedException();
 }
Пример #9
0
 /// <summary>
 /// Creates a new ReflectionEmitILGenerator instance.
 /// </summary>
 /// <param name="generator"> The ILGenerator that is used to output the IL. </param>
 public ReflectionEmitILGenerator(ScriptEngine engine, System.Reflection.Emit.MethodBuilder builder)
 {
     this.generator = builder.GetILGenerator();
     this.builder   = builder;
     Engine         = engine;
 }
Пример #10
0
 public MethodGenerator(System.Reflection.Emit.MethodBuilder builder, ParameterInfo[] parameters, TypeGenerator declaring) : base(builder, parameters, declaring)
 {
     _builder = builder;
     Context  = new TypeContext(declaring.Context);
 }
Пример #11
0
 public void AddOtherMethod(System.Reflection.Emit.MethodBuilder mdBuilder)
 {
 }
Пример #12
0
 public MethodBuilder(System.Reflection.Emit.MethodBuilder methodBuilder)
 {
     _methodBuilder = methodBuilder;
     _il = new ILExpressed(_methodBuilder.GetILGenerator());
 }
 public void CompileToMethod(System.Reflection.Emit.MethodBuilder method, System.Runtime.CompilerServices.DebugInfoGenerator debugInfoGenerator)
 {
     Contract.Requires(method != null);
     Contract.Ensures(0 <= this.Parameters.Count);
 }
 public void CompileToMethod(System.Reflection.Emit.MethodBuilder method)
 {
     Contract.Requires(method != null);
     Contract.Ensures(0 <= this.Parameters.Count);
 }
 /// <summary>
 /// Calls a method on this variable
 /// </summary>
 /// <param name="Method">Method</param>
 /// <param name="Parameters">Parameters sent in</param>
 /// <returns>Variable returned by the function (if one exists, null otherwise)</returns>
 public virtual VariableBase Call(System.Reflection.Emit.MethodBuilder Method, object[] Parameters = null)
 {
     Contract.Requires<ArgumentNullException>(Method!=null,"Method");
     return MethodBase.CurrentMethod.Call(this, Method, Parameters);
 }
Пример #16
0
        static System.Type GenerateFunctionType(System.Type functionType)
        {
            SQLiteFunctionAttribute attribute = AttributeExtensions.GetCustomAttribute <SQLiteFunctionAttribute>(functionType);

            if (attribute == null)
            {
                return(null);
            }
            bool        ex       = TypeExtensions.IsInheritFrom(functionType, typeof(SQLiteFunctionEx)) || attribute.Type == FunctionTypes.Collation;
            FastWrapper baseType = GetType(ex ? "System.Data.SQLite.SQLiteFunctionEx" : "System.Data.SQLite.SQLiteFunction");


            System.Reflection.AssemblyName assemblyName = new System.Reflection.AssemblyName(functionType.Namespace + ".DynamicClass_" + functionType.Name);
            System.Reflection.Emit.AssemblyBuilderAccess accemblyBuilderAccess =
#if netcore
                System.Reflection.Emit.AssemblyBuilderAccess.Run;
#else
                IsDebug
                    ? System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave
                    : System.Reflection.Emit.AssemblyBuilderAccess.Run;
#endif
            System.Reflection.Emit.AssemblyBuilder assembly =
#if netcore
                System.Reflection.Emit.AssemblyBuilder.DefineDynamicAssembly(assemblyName, accemblyBuilderAccess);
#else
                System.AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, accemblyBuilderAccess);
#endif
#if !netcore
            bool canSave = (accemblyBuilderAccess == System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave || accemblyBuilderAccess == System.Reflection.Emit.AssemblyBuilderAccess.Save);
#endif
            System.Reflection.Emit.ModuleBuilder module =
#if netcore
                assembly.DefineDynamicModule(assemblyName.Name);
#else
                canSave
                    ? assembly.DefineDynamicModule(assemblyName.Name, assemblyName.Name + ".dll")
                    : assembly.DefineDynamicModule(assemblyName.Name);//, n.Name + ".dll");
#endif
            System.Reflection.Emit.TypeBuilder type = module.DefineType(
                assemblyName.Name + ".DynamicClass",
                System.Reflection.TypeAttributes.Public | System.Reflection.TypeAttributes.Sealed | System.Reflection.TypeAttributes.AutoClass,
                baseType.Type,
                System.Type.EmptyTypes);

            {
                FastWrapper wrapper = GetType("System.Data.SQLite.SQLiteFunctionAttribute");
                System.Reflection.PropertyInfo[] properties = new System.Reflection.PropertyInfo[] {
                    wrapper.Type.GetProperty("Name"),
                    wrapper.Type.GetProperty("Arguments"),
                    wrapper.Type.GetProperty("FuncType"),
                };
                System.Reflection.Emit.CustomAttributeBuilder attributeBuilder = new System.Reflection.Emit.CustomAttributeBuilder(wrapper.Type.GetConstructor(System.Type.EmptyTypes), new object[0],
                                                                                                                                   properties, new object[] {
                    attribute.Name,
                    attribute.Arguments,
                    TypeExtensions.Convert(attribute.Type, GetType("System.Data.SQLite.FunctionType").Type),
                });
                type.SetCustomAttribute(attributeBuilder);
            }
            System.Reflection.Emit.FieldBuilder _o = type.DefineField("_o", functionType, FieldAttributes.Private);

            {
                System.Reflection.Emit.ConstructorBuilder ctor = type.DefineConstructor(
                    System.Reflection.MethodAttributes.Public | System.Reflection.MethodAttributes.HideBySig | System.Reflection.MethodAttributes.SpecialName | System.Reflection.MethodAttributes.RTSpecialName,
                    System.Reflection.CallingConventions.HasThis,
                    System.Type.EmptyTypes);
                System.Reflection.Emit.ILGenerator il = ctor.GetILGenerator();
                il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                il.Emit(System.Reflection.Emit.OpCodes.Call, baseType.Type.GetConstructor(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.CreateInstance | BindingFlags.Instance, null, System.Type.EmptyTypes, new System.Reflection.ParameterModifier[0]));
                il.Emit(System.Reflection.Emit.OpCodes.Nop);
                il.Emit(System.Reflection.Emit.OpCodes.Nop);
                il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                il.Emit(System.Reflection.Emit.OpCodes.Newobj, functionType.GetConstructor(System.Type.EmptyTypes));
                il.Emit(System.Reflection.Emit.OpCodes.Stfld, _o);
                il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                il.Emit(System.Reflection.Emit.OpCodes.Ldfld, _o);
                il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                if (attribute.Type == FunctionTypes.Collation)
                {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1);
                }
                else
                {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
                }
                il.Emit(System.Reflection.Emit.OpCodes.Callvirt, functionType.GetMethod("Init", BindingFlags.Public | BindingFlags.Instance | BindingFlags.InvokeMethod, null, new System.Type[] {
                    typeof(object), typeof(bool)
                }, null));
                il.Emit(System.Reflection.Emit.OpCodes.Nop);
                il.Emit(System.Reflection.Emit.OpCodes.Ret);
            }
            CreateMethodDelegate createMethod = (methodInfo, action) => {
                System.Reflection.ParameterInfo[] parameters = methodInfo.GetParameters();
                System.Type[] parameterTypes = new System.Type[parameters.Length];
                for (int i = 0; i < parameters.Length; i++)
                {
                    parameterTypes[i] = parameters[i].ParameterType;
                }
                System.Reflection.Emit.MethodBuilder method = type.DefineMethod(methodInfo.Name, (methodInfo.Attributes | MethodAttributes.NewSlot) ^ MethodAttributes.NewSlot, methodInfo.CallingConvention, methodInfo.ReturnType, parameterTypes);
                for (int i = 0; i < parameters.Length; i++)
                {
                    System.Reflection.Emit.ParameterBuilder parameter = method.DefineParameter(i + 1, parameters[i].Attributes, parameters[i].Name);
                    if (parameters[i].IsOptional)
                    {
                        if (parameters[i].ParameterType.IsValueType && parameters[i].DefaultValue == null)
                        {
                            continue;
                        }
                        parameter.SetConstant(parameters[i].DefaultValue);
                    }
                }
                System.Reflection.Emit.ILGenerator il = method.GetILGenerator();
                bool hasReturn = (methodInfo.ReturnType != typeof(void));
                System.Reflection.Emit.LocalBuilder @return = null;
                if (hasReturn)
                {
                    @return = il.DeclareLocal(methodInfo.ReturnType);
                }
                il.Emit(System.Reflection.Emit.OpCodes.Nop);
                il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                il.Emit(System.Reflection.Emit.OpCodes.Ldfld, _o);
                action(functionType.GetMethod(methodInfo.Name, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance), method, il);
                il.Emit(System.Reflection.Emit.OpCodes.Ret);
            };
            if (attribute.Type == FunctionTypes.Scalar)
            {
                createMethod(baseType.Type.GetMethod("Invoke"), (methodInfo, method, il) => {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
                    il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo);
                    il.Emit(System.Reflection.Emit.OpCodes.Stloc_0);
                    System.Reflection.Emit.Label label = il.DefineLabel();
                    il.Emit(System.Reflection.Emit.OpCodes.Br_S, label);
                    il.MarkLabel(label);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldloc_0);
                });
            }
            else if (attribute.Type == FunctionTypes.Collation)
            {
                createMethod(baseType.Type.GetMethod("Compare"), (methodInfo, method, il) => {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2);
                    il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo);
                    il.Emit(System.Reflection.Emit.OpCodes.Stloc_0);
                    System.Reflection.Emit.Label label = il.DefineLabel();
                    il.Emit(System.Reflection.Emit.OpCodes.Br_S, label);
                    il.MarkLabel(label);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldloc_0);
                });
            }
            else
            {
                createMethod(baseType.Type.GetMethod("Final"), (methodInfo, method, il) => {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
                    il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo);
                    il.Emit(System.Reflection.Emit.OpCodes.Stloc_0);
                    System.Reflection.Emit.Label label = il.DefineLabel();
                    il.Emit(System.Reflection.Emit.OpCodes.Br_S, label);
                    il.MarkLabel(label);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldloc_0);
                });
                createMethod(baseType.Type.GetMethod("Step"), (methodInfo, method, il) => {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3);
                    il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo);
                    il.Emit(System.Reflection.Emit.OpCodes.Nop);
                });
            }
            {
                System.Reflection.MethodInfo methodInfo_base = baseType.Type.GetMethod("Dispose", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, new System.Type[] { typeof(bool) }, null);
                createMethod(methodInfo_base, (methodInfo, method, il) => {
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
                    il.Emit(System.Reflection.Emit.OpCodes.Callvirt, methodInfo);
                    il.Emit(System.Reflection.Emit.OpCodes.Nop);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0);
                    il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1);
                    il.Emit(System.Reflection.Emit.OpCodes.Call, methodInfo_base);
                    il.Emit(System.Reflection.Emit.OpCodes.Nop);
                });
            }

#if netcore20
            var result = type.CreateTypeInfo();
#else
            var result = type.CreateType();
#endif
#if !netcore
            if (canSave)
            {
                assembly.Save(assemblyName.Name + ".dll");
            }
#endif
            return(result);
        }
Пример #17
0
 public void SetRemoveOnMethod(System.Reflection.Emit.MethodBuilder mdBuilder)
 {
 }
Пример #18
0
 /// <summary>
 /// Write the specified instruction list into the method builder. There is no need to manage labels or locals they will be created by this method.
 /// </summary>
 /// <param name="Instructions">The list of instruction to emit</param>
 /// <param name="MethodBuilder">The Method builder recieving the instructions</param>
 static public void EmitMethod(List <Instruction> Instructions, System.Reflection.Emit.MethodBuilder MethodBuilder)
 {
     MSIL_emit.emitMethod(Instructions, MethodBuilder.GetILGenerator());
 }
Пример #19
0
 public void SetSetMethod(System.Reflection.Emit.MethodBuilder mdBuilder)
 {
 }
Пример #20
0
 public MethodGenerator(System.Reflection.Emit.MethodBuilder builder, ParameterInfo[] parameters, Type declaring) : base(builder, parameters, declaring)
 {
     _builder = builder;
 }