public static void InitializeFields(Type type, bool reusable) { if (type == null) { return; } const string slotStorageName = "#Constant"; foreach (FieldInfo fi in type.GetFields()) { if (fi.Name.StartsWith(slotStorageName)) { object value; if (reusable) { value = GlobalStaticFieldRewriter.GetConstantDataReusable(Int32.Parse(fi.Name.Substring(slotStorageName.Length))); } else { value = GlobalStaticFieldRewriter.GetConstantData(Int32.Parse(fi.Name.Substring(slotStorageName.Length))); } Debug.Assert(value != null); fi.SetValue(null, value); } } }
private void CompileWithStaticGlobals(out DlrMainCallTarget target, out IAttributesCollection globals) { // Create typegen TypeGen typeGen = Snippets.Shared.DefineType(MakeDebugName(), typeof(CustomSymbolDictionary), false, SourceUnit.EmitDebugSymbols); typeGen.TypeBuilder.DefineDefaultConstructor(MethodAttributes.Public); // Create rewriter GlobalStaticFieldRewriter rewriter = new GlobalStaticFieldRewriter(typeGen); // Compile lambda LambdaExpression lambda = rewriter.RewriteLambda(Code, "Initialize"); lambda.CompileToMethod(typeGen.TypeBuilder, CompilerHelpers.PublicStatic, SourceUnit.EmitDebugSymbols); // Create globals dictionary, finish type rewriter.EmitDictionary(); Type type = typeGen.FinishType(); globals = (IAttributesCollection)Activator.CreateInstance(type); // Create target target = (DlrMainCallTarget)Delegate.CreateDelegate(typeof(DlrMainCallTarget), type.GetMethod("Initialize")); // TODO: clean this up after clarifying dynamic site initialization logic InitializeFields(type); }
private void CompileWithStaticGlobals(out DlrMainCallTarget target, out IAttributesCollection globals) { // Create typegen TypeGen typeGen = Snippets.Shared.DefineType(MakeDebugName(), typeof(CustomSymbolDictionary), false, SourceUnit.EmitDebugSymbols); typeGen.TypeBuilder.DefineDefaultConstructor(MethodAttributes.Public); // Create rewriter GlobalStaticFieldRewriter rewriter = new GlobalStaticFieldRewriter(typeGen); // Compile lambda LambdaExpression lambda = rewriter.RewriteLambda(Code, "Initialize"); MethodBuilder mb = typeGen.TypeBuilder.DefineMethod(lambda.Name, CompilerHelpers.PublicStatic); lambda.CompileToMethod(mb, SourceUnit.EmitDebugSymbols); // Create globals dictionary, finish type rewriter.EmitDictionary(); Type type = typeGen.FinishType(); globals = (IAttributesCollection)Activator.CreateInstance(type); // Create target target = (DlrMainCallTarget)Delegate.CreateDelegate(typeof(DlrMainCallTarget), type.GetMethod("Initialize")); // TODO: clean this up after clarifying dynamic site initialization logic InitializeFields(type); }