예제 #1
0
        public CodeGen DefineDynamicMethod(string methodName, Type returnType, Type[] paramTypes)
        {
            CodeGen cg;

            if (Options.GenerateDynamicMethods)
            {
                DynamicMethod target = new DynamicMethod(methodName + "##" + index++, returnType, paramTypes, myModule);
                cg = new CodeGen(this, null, target, target.GetILGenerator(), paramTypes);
                //Console.WriteLine("----> {0} DynamicMethod", target.Name);
            }
            else
            {
                TypeGen tg = DefinePublicType("Type" + methodName + index++, typeof(object));
                cg = tg.DefineUserHiddenMethod(MethodAttributes.Public | MethodAttributes.Static,
                                               "Handle", returnType, paramTypes);
            }
            return(cg);
        }
예제 #2
0
        internal static CodeGen GenerateModuleInitialize(CompilerContext context, GlobalSuite gs, TypeGen tg, CustomModuleInit customInit)
        {
            CodeGen ncg = tg.DefineUserHiddenMethod(MethodAttributes.Public, "Initialize", typeof(void), Type.EmptyTypes);

            ncg.Context = context;

            if (Options.StaticModules)
            {
                ncg.Names = CodeGen.CreateStaticFieldNamespace(tg);
            }
            else
            {
                throw new NotImplementedException(); //.names = new FieldNamespace(null, tg, new ModuleSlot(tg.myType));
            }

            if (context.TrueDivision)
            {
                ncg.ModuleSlot.EmitGet(ncg);
                ncg.EmitInt(1);
                ncg.EmitCall(typeof(ICallerContext), "set_TrueDivision");
            }

            // Add __doc__ and __name__
            ncg.Names.CreateGlobalSlot(Name.Make("__doc__"));
            ncg.Names.CreateGlobalSlot(Name.Make("__name__"));

            string doc = gs.GetDocString();

            ncg.EmitStringOrNull(doc);
            ncg.EmitSet(Name.Make("__doc__"));

            if (customInit != null)
            {
                customInit(ncg);
            }

            gs.Emit(ncg);
            ncg.EmitPosition(Location.None, Location.None);
            ncg.EmitReturn();

            FinishCustomDict(tg, ncg.Names);

            return(ncg);
        }