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();
}
}