public Type Compile(Type superType, Type stubType, IPersistentVector interfaces, bool onetimeUse, GenContext context) { if (_compiledType != null) { return(_compiledType); } string publicTypeName = IsDefType || (_isStatic && Compiler.IsCompiling) ? InternalName : InternalName + "__" + RT.nextID(); //Console.WriteLine("DefFn {0}, {1}", publicTypeName, context.AssemblyBuilder.GetName().Name); _typeBuilder = context.AssemblyGen.DefinePublicType(publicTypeName, superType, true); context = context.WithNewDynInitHelper().WithTypeBuilder(_typeBuilder); Var.pushThreadBindings(RT.map(Compiler.CompilerContextVar, context)); try { if (interfaces != null) { for (int i = 0; i < interfaces.count(); i++) { _typeBuilder.AddInterfaceImplementation((Type)interfaces.nth(i)); } } ObjExpr.MarkAsSerializable(_typeBuilder); GenInterface.SetCustomAttributes(_typeBuilder, _classMeta); try { if (IsDefType) { Compiler.RegisterDuplicateType(_typeBuilder); Var.pushThreadBindings(RT.map( Compiler.CompileStubOrigClassVar, stubType )); //, //Compiler.COMPILE_STUB_CLASS, _baseType)); } EmitConstantFieldDefs(_typeBuilder); EmitKeywordCallsiteDefs(_typeBuilder); DefineStaticConstructor(_typeBuilder); if (SupportsMeta) { _metaField = _typeBuilder.DefineField("__meta", typeof(IPersistentMap), FieldAttributes.Public | FieldAttributes.InitOnly); } EmitClosedOverFields(_typeBuilder); EmitProtocolCallsites(_typeBuilder); _ctorInfo = EmitConstructor(_typeBuilder, superType); if (_altCtorDrops > 0) { EmitFieldOnlyConstructor(_typeBuilder, superType); } if (SupportsMeta) { EmitNonMetaConstructor(_typeBuilder, superType); EmitMetaFunctions(_typeBuilder); } EmitStatics(_typeBuilder); EmitMethods(_typeBuilder); //if (KeywordCallsites.count() > 0) // EmitSwapThunk(_typeBuilder); _compiledType = _typeBuilder.CreateType(); if (context.DynInitHelper != null) { context.DynInitHelper.FinalizeType(); } // If we don't pick up the ctor after we finalize the type, // we sometimes get a ctor which is not a RuntimeConstructorInfo // This causes System.DynamicILGenerator.Emit(opcode,ContructorInfo) to blow up. // The error says the ConstructorInfo is null, but there is a second case in the code. // Thank heavens one can run Reflector on mscorlib. ConstructorInfo[] cis = _compiledType.GetConstructors(); foreach (ConstructorInfo ci in cis) { if (ci.GetParameters().Length == CtorTypes().Length) { _ctorInfo = ci; break; } } return(_compiledType); } finally { if (IsDefType) { Var.popThreadBindings(); } } } finally { Var.popThreadBindings(); } }
public Type Compile(Type superType, Type stubType, IPersistentVector interfaces, bool onetimeUse, GenContext context) { if (CompiledType != null) { return(CompiledType); } string publicTypeName = IsDefType /* || (CanBeDirect && Compiler.IsCompiling) */ ? InternalName : InternalName + "__" + RT.nextID(); TypeBuilder = context.AssemblyGen.DefinePublicType(publicTypeName, superType, true); context = context.WithNewDynInitHelper().WithTypeBuilder(TypeBuilder); Var.pushThreadBindings(RT.map(Compiler.CompilerContextVar, context)); try { if (interfaces != null) { for (int i = 0; i < interfaces.count(); i++) { TypeBuilder.AddInterfaceImplementation((Type)interfaces.nth(i)); } } ObjExpr.MarkAsSerializable(TypeBuilder); GenInterface.SetCustomAttributes(TypeBuilder, ClassMeta); try { if (IsDefType) { Compiler.RegisterDuplicateType(TypeBuilder); Var.pushThreadBindings(RT.map( Compiler.CompileStubOrigClassVar, stubType, Compiler.CompilingDefTypeVar, true )); //, //Compiler.COMPILE_STUB_CLASS, _baseType)); } EmitConstantFieldDefs(TypeBuilder); EmitKeywordCallsiteDefs(TypeBuilder); DefineStaticConstructor(TypeBuilder); if (SupportsMeta) { MetaField = TypeBuilder.DefineField("__meta", typeof(IPersistentMap), FieldAttributes.Public | FieldAttributes.InitOnly); } // If this IsDefType, then it has already emitted the closed-over fields on the base class. if (!IsDefType) { EmitClosedOverFields(TypeBuilder); } EmitProtocolCallsites(TypeBuilder); CtorInfo = EmitConstructor(TypeBuilder, superType); if (AltCtorDrops > 0) { EmitFieldOnlyConstructors(TypeBuilder, superType); } if (SupportsMeta) { EmitNonMetaConstructor(TypeBuilder, superType); EmitMetaFunctions(TypeBuilder); } EmitStatics(TypeBuilder); EmitMethods(TypeBuilder); CompiledType = TypeBuilder.CreateType(); if (context.DynInitHelper != null) { context.DynInitHelper.FinalizeType(); } CtorInfo = GetConstructorWithArgCount(CompiledType, CtorTypes().Length); return(CompiledType); } finally { if (IsDefType) { Var.popThreadBindings(); } } } finally { Var.popThreadBindings(); } }