<commit_before>// Copyright 2007 <NAME> <<EMAIL>>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;

namespace NDesk.DBus
{
	class TypeImplementer
	{
		public static TypeImplementer Root = new TypeImplementer ("NDesk.DBus.Proxies", false);
		AssemblyBuilder asmB;
		ModuleBuilder modB;
		//List<Assembly> cacheAs = new List<Assembly> ();

		public TypeImplementer (string name, bool canSave)
		{
			//asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("NDesk.DBus.Proxies"), AssemblyBuilderAccess.Run);
			asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("NDesk.DBus.Proxies"), canSave ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run);
			modB = asmB.DefineDynamicModule ("NDesk.DBus.Proxies");
			//modB = asmB.DefineDynamicModule ("NDesk.DBus.Proxies.dll", "NDesk.DBus.Proxies.dll");
			//Load ("NDesk.DBus.Proxies");
		}

		/*
		public void Load (string fname)
		{
			try {
				cacheAs.Add (AppDomain.CurrentDomain.Load (fname));
			} catch {
			}
		}

		public void Save ()
		{
			string fname = "NDesk.DBus.Proxies.dll";
			System.IO.File.Delete (fname);
			asmB.Save (fname);
		}
		*/

		Dictionary<Type,Type> map = new Dictionary<Type,Type> ();

		public Type GetImplementation (Type declType)
		{
			Type retT;

			if (map.TryGetValue (declType, out retT))
				return retT;

			string proxyName = declType.FullName + "Proxy";

			/*
			foreach (Assembly cacheA in cacheAs) {
				Type rt = cacheA.GetType (proxyName, false);
				if (rt != null) {
					Console.WriteLine ("HIT " + rt);
					map[declType] = rt;
					return rt;
				}
			}
			*/

			Type parentType;

			if (declType.IsInterface)
				parentType = typeof (BusObject);
			else
				parentType = declType;

			TypeBuilder typeB = modB.DefineType (proxyName, TypeAttributes.Class | TypeAttributes.Public, parentType);

			if (false && !declType.IsInterface) {
				foreach (MethodInfo mi in declType.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
					MethodBody body = mi.GetMethodBody ();
					Console.WriteLine(mi + ":");
					foreach (LocalVariableInfo lvar in body.LocalVariables) {
						Console.WriteLine(lvar.LocalIndex + ": " + lvar.LocalType);
					}
					Console.WriteLine();

					//DynamicMethod method_builder = new DynamicMethod ("Write" + t.Name, typeof (void), new Type[] {typeof (MessageWriter), t}, typeof (MessageWriter));
					//DynamicMethod dm = new DynamicMethod ("RepProx", typeof(void), new Type[] { typeof(object), typeof(int), typeof(string) }, typeof(object));
					//DynamicILInfo dil = dm.GetDynamicILInfo ();
					//dil.SetCode(body.GetILAsByteArray (), body.MaxStackSize);

					//MethodBuilder mb = typeB.DefineMethod ("RepProx", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(object), typeof(int), typeof(string) });
					//MethodBuilder mb = typeB.DefineMethod ("RepProx", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { declType, typeof(int), typeof(string) });
					//

				//MethodAttributes attrs = mi.Attributes;
			 	//^ MethodAttributes.Abstract;
				//attrs ^= MethodAttributes.NewSlot;
				//attrs |= MethodAttributes.Final;

				//MethodAttributes.Public
				MethodAttributes attrs = MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig;
				Console.WriteLine("cwl attrs: " + typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }).Attributes);
					//DynamicILInfo dil = mi.GetDynamicILInfo ();

					ParameterInfo[] parms = mi.GetParameters();
					//MethodBuilder mb = typeB.DefineMethod ("RepProx", attrs, typeof(void), new Type[] { typeof(int), typeof(string) });
					//MethodBuilder mb = typeB.DefineMethod ("RepProx", attrs, typeof(void), new Type[] { declType, typeof(int), typeof(string) });
					MethodBuilder mb = typeB.DefineMethod ("RepProx", attrs, typeof(void), new Type[] { typeof(object), typeof(int), typeof(string) });

					mb.DefineParameter (0, ParameterAttributes.None, "self");
					for (int i = 0; i < parms.Length ; i++) {
						Console.WriteLine("i={0} pos={1} {2} {3}", i, parms[i].Position, parms[i].Attributes, parms[i]);
						mb.DefineParameter (parms[i].Position + 1, parms[i].Attributes, parms[i].Name);
					}

				}
			}


			if (declType.IsInterface)
				Implement (typeB, declType);

			foreach (Type iface in declType.GetInterfaces ())
				Implement (typeB, iface);

			retT = typeB.CreateType ();
			map[declType] = retT;

			return retT;
		}

		static void Implement (TypeBuilder typeB, Type iface)
		{<commit_msg>Quick fix for thread-safety issue in TypeImplementer<commit_after>// Copyright 2007 <NAME> <<EMAIL>>
// This software is made available under the MIT License
// See COPYING for details

using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Collections.Generic;

namespace NDesk.DBus
{
	class TypeImplementer
	{
		public static TypeImplementer Root = new TypeImplementer ("NDesk.DBus.Proxies", false);
		AssemblyBuilder asmB;
		ModuleBuilder modB;
		static object getImplLock = new Object ();
		//List<Assembly> cacheAs = new List<Assembly> ();

		public TypeImplementer (string name, bool canSave)
		{
			//asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("NDesk.DBus.Proxies"), AssemblyBuilderAccess.Run);
			asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName ("NDesk.DBus.Proxies"), canSave ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run);
			modB = asmB.DefineDynamicModule ("NDesk.DBus.Proxies");
			//modB = asmB.DefineDynamicModule ("NDesk.DBus.Proxies.dll", "NDesk.DBus.Proxies.dll");
			//Load ("NDesk.DBus.Proxies");
		}

		/*
		public void Load (string fname)
		{
			try {
				cacheAs.Add (AppDomain.CurrentDomain.Load (fname));
			} catch {
			}
		}

		public void Save ()
		{
			string fname = "NDesk.DBus.Proxies.dll";
			System.IO.File.Delete (fname);
			asmB.Save (fname);
		}
		*/

		Dictionary<Type,Type> map = new Dictionary<Type,Type> ();

		public Type GetImplementation (Type declType)
		{
			lock (getImplLock) {
				Type retT;

				if (map.TryGetValue (declType, out retT))
					return retT;

				string proxyName = declType.FullName + "Proxy";

				/*
				foreach (Assembly cacheA in cacheAs) {
					Type rt = cacheA.GetType (proxyName, false);
					if (rt != null) {
						Console.WriteLine ("HIT " + rt);
						map[declType] = rt;
						return rt;
					}
				}
				*/

				Type parentType;

				if (declType.IsInterface)
					parentType = typeof (BusObject);
				else
					parentType = declType;

				TypeBuilder typeB = modB.DefineType (proxyName, TypeAttributes.Class | TypeAttributes.Public, parentType);

				if (false && !declType.IsInterface) {
					foreach (MethodInfo mi in declType.GetMethods (BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)) {
						MethodBody body = mi.GetMethodBody ();
						Console.WriteLine(mi + ":");
						foreach (LocalVariableInfo lvar in body.LocalVariables) {
							Console.WriteLine(lvar.LocalIndex + ": " + lvar.LocalType);
						}
						Console.WriteLine();

						//DynamicMethod method_builder = new DynamicMethod ("Write" + t.Name, typeof (void), new Type[] {typeof (MessageWriter), t}, typeof (MessageWriter));
						//DynamicMethod dm = new DynamicMethod ("RepProx", typeof(void), new Type[] { typeof(object), typeof(int), typeof(string) }, typeof(object));
						//DynamicILInfo dil = dm.GetDynamicILInfo ();
						//dil.SetCode(body.GetILAsByteArray (), body.MaxStackSize);

						//MethodBuilder mb = typeB.DefineMethod ("RepProx", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(object), typeof(int), typeof(string) });
						//MethodBuilder mb = typeB.DefineMethod ("RepProx", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { declType, typeof(int), typeof(string) });
						//

					//MethodAttributes attrs = mi.Attributes;
				 	//^ MethodAttributes.Abstract;
					//attrs ^= MethodAttributes.NewSlot;
					//attrs |= MethodAttributes.Final;

					//MethodAttributes.Public
					MethodAttributes attrs = MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig;
					Console.WriteLine("cwl attrs: " + typeof(Console).GetMethod("WriteLine", new Type[] { typeof(string) }).Attributes);
						//DynamicILInfo dil = mi.GetDynamicILInfo ();

						ParameterInfo[] parms = mi.GetParameters();
						//MethodBuilder mb = typeB.DefineMethod ("RepProx", attrs, typeof(void), new Type[] { typeof(int), typeof(string) });
						//MethodBuilder mb = typeB.DefineMethod ("RepProx", attrs, typeof(void), new Type[] { declType, typeof(int), typeof(string) });
						MethodBuilder mb = typeB.DefineMethod ("RepProx", attrs, typeof(void), new Type[] { typeof(object), typeof(int), typeof(string) });

						mb.DefineParameter (0, ParameterAttributes.None, "self");
						for (int i = 0; i < parms.Length ; i++) {
							Console.WriteLine("i={0} pos={1} {2} {3}", i, parms[i].Position, parms[i].Attributes, parms[i]);
							mb.DefineParameter (parms[i].Position + 1, parms[i].Attributes, parms[i].Name);
						}

					}
				}


				if (declType.IsInterface)
					Implement (typeB, declType);

				foreach (Type iface in declType.GetInterfaces ())
					Implement (typeB, iface);

				retT = typeB.CreateType ();
				map[declType] = retT;

				return retT;
			}
		}

		static void Implement (TypeBuilder typeB, Type iface)
		{
{'fraction_non_alphanumeric': 0.1378125, 'fraction_numerical': 0.0022916666666666667, 'mean_word_length': 3.738894373149062, 'pattern_counts': {'":': 4, '<': 19, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '762222', 'n_tokens_mistral': 3568, 'n_tokens_neox': 2947, 'n_words': 697}