<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 DBus
{
	using Protocol;

	class TypeImplementer
	{
		public static readonly TypeImplementer Root = new TypeImplementer ("DBus.Proxies", false);
		AssemblyBuilder asmB;
		ModuleBuilder modB;
		static readonly object getImplLock = new Object ();

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

		static MethodInfo getTypeFromHandleMethod = typeof (Type).GetMethod ("GetTypeFromHandle", new Type[] {typeof (RuntimeTypeHandle)});
		static ConstructorInfo argumentNullExceptionConstructor = typeof (ArgumentNullException).GetConstructor (new Type[] {typeof (string)});
		static ConstructorInfo messageWriterConstructor = typeof (MessageWriter).GetConstructor (Type.EmptyTypes);
		static MethodInfo messageWriterWriteArray = typeof (MessageWriter).GetMethod ("WriteArray");
		static MethodInfo messageWriterWriteDict = typeof (MessageWriter).GetMethod ("WriteFromDict");
		static MethodInfo messageWriterWriteStruct = typeof (MessageWriter).GetMethod ("WriteStructure");
		static MethodInfo messageReaderReadValue = typeof (MessageReader).GetMethod ("ReadValue", new Type[] { typeof (System.Type) });
		static MethodInfo messageReaderReadArray = typeof (MessageReader).GetMethod ("ReadArray", Type.EmptyTypes);
		static MethodInfo messageReaderReadDictionary = typeof (MessageReader).GetMethod ("ReadDictionary", Type.EmptyTypes);
		static MethodInfo messageReaderReadStruct = typeof (MessageReader).GetMethod ("ReadStruct", Type.EmptyTypes);

		static Dictionary<Type,MethodInfo> writeMethods = new Dictionary<Type,MethodInfo> ();
		static Dictionary<Type,object> typeWriters = new Dictionary<Type,object> ();

		static MethodInfo sendMethodCallMethod = typeof (BusObject).GetMethod ("SendMethodCall");
		static MethodInfo sendSignalMethod = typeof (BusObject).GetMethod ("SendSignal");
		static MethodInfo toggleSignalMethod = typeof (BusObject).GetMethod ("ToggleSignal");

		static Dictionary<EventInfo,DynamicMethod> hookup_methods = new Dictionary<EventInfo,DynamicMethod> ();
		static Dictionary<Type,MethodInfo> readMethods = new Dictionary<Type,MethodInfo> ();

		public TypeImplementer (string name, bool canSave)
		{
			asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName (name),
			                                                      canSave ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run);
			modB = asmB.DefineDynamicModule (name);
		}

		public Type GetImplementation (Type declType)
		{
			Type retT;

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

			string proxyName = declType.FullName + "Proxy";

			Type parentType;

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

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

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

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

			retT = typeB.CreateType ();

			lock (getImplLock)
				map[declType] = retT;

			return retT;
		}

		static void Implement (TypeBuilder typeB, Type iface)
		{
			typeB.AddInterfaceImplementation (iface);

			Dictionary<string,MethodBuilder> builders = new Dictionary<string,MethodBuilder> ();

			foreach (MethodInfo declMethod in iface.GetMethods ()) {
				ParameterInfo[] parms = declMethod.GetParameters ();

				Type[] parmTypes = new Type[parms.Length];
				for (int i = 0 ; i < parms.Length ; i++)
					parmTypes[i] = parms[i].ParameterType;

				MethodAttributes attrs = declMethod.Attributes ^ MethodAttributes.Abstract;
				attrs ^= MethodAttributes.NewSlot;
				attrs |= MethodAttributes.Final;
				MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, attrs, declMethod.ReturnType, parmTypes);
				typeB.DefineMethodOverride (method_builder, declMethod);

				//define in/out/ref/name for each of the parameters
				for (int i = 0; i < parms.Length ; i++)
					method_builder.DefineParameter (i + 1, parms[i].Attributes, parms[i].Name);

				ILGenerator ilg = method_builder.GetILGenerator ();
				GenHookupMethod (ilg, declMethod, sendMethodCallMethod, Mapper.GetInterfaceName (iface), declMethod.Name);

				if (declMethod.IsSpecialName)
					builders[declMethod.Name] = method_builder;<commit_msg>Specify the parent interface before trying to fallback to Mapper<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 DBus
{
	using Protocol;

	class TypeImplementer
	{
		public static readonly TypeImplementer Root = new TypeImplementer ("DBus.Proxies", false);
		AssemblyBuilder asmB;
		ModuleBuilder modB;
		static readonly object getImplLock = new Object ();

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

		static MethodInfo getTypeFromHandleMethod = typeof (Type).GetMethod ("GetTypeFromHandle", new Type[] {typeof (RuntimeTypeHandle)});
		static ConstructorInfo argumentNullExceptionConstructor = typeof (ArgumentNullException).GetConstructor (new Type[] {typeof (string)});
		static ConstructorInfo messageWriterConstructor = typeof (MessageWriter).GetConstructor (Type.EmptyTypes);
		static MethodInfo messageWriterWriteArray = typeof (MessageWriter).GetMethod ("WriteArray");
		static MethodInfo messageWriterWriteDict = typeof (MessageWriter).GetMethod ("WriteFromDict");
		static MethodInfo messageWriterWriteStruct = typeof (MessageWriter).GetMethod ("WriteStructure");
		static MethodInfo messageReaderReadValue = typeof (MessageReader).GetMethod ("ReadValue", new Type[] { typeof (System.Type) });
		static MethodInfo messageReaderReadArray = typeof (MessageReader).GetMethod ("ReadArray", Type.EmptyTypes);
		static MethodInfo messageReaderReadDictionary = typeof (MessageReader).GetMethod ("ReadDictionary", Type.EmptyTypes);
		static MethodInfo messageReaderReadStruct = typeof (MessageReader).GetMethod ("ReadStruct", Type.EmptyTypes);

		static Dictionary<Type,MethodInfo> writeMethods = new Dictionary<Type,MethodInfo> ();
		static Dictionary<Type,object> typeWriters = new Dictionary<Type,object> ();

		static MethodInfo sendMethodCallMethod = typeof (BusObject).GetMethod ("SendMethodCall");
		static MethodInfo sendSignalMethod = typeof (BusObject).GetMethod ("SendSignal");
		static MethodInfo toggleSignalMethod = typeof (BusObject).GetMethod ("ToggleSignal");

		static Dictionary<EventInfo,DynamicMethod> hookup_methods = new Dictionary<EventInfo,DynamicMethod> ();
		static Dictionary<Type,MethodInfo> readMethods = new Dictionary<Type,MethodInfo> ();

		public TypeImplementer (string name, bool canSave)
		{
			asmB = AppDomain.CurrentDomain.DefineDynamicAssembly (new AssemblyName (name),
			                                                      canSave ? AssemblyBuilderAccess.RunAndSave : AssemblyBuilderAccess.Run);
			modB = asmB.DefineDynamicModule (name);
		}

		public Type GetImplementation (Type declType)
		{
			Type retT;

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

			string proxyName = declType.FullName + "Proxy";

			Type parentType;

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

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

			string interfaceName = null;
			if (declType.IsInterface)
				Implement (typeB, declType, interfaceName = Mapper.GetInterfaceName (declType));

			foreach (Type iface in declType.GetInterfaces ())
				Implement (typeB, iface, interfaceName == null ? Mapper.GetInterfaceName (iface) : interfaceName);

			retT = typeB.CreateType ();

			lock (getImplLock)
				map[declType] = retT;

			return retT;
		}

		static void Implement (TypeBuilder typeB, Type iface, string interfaceName)
		{
			typeB.AddInterfaceImplementation (iface);

			Dictionary<string,MethodBuilder> builders = new Dictionary<string,MethodBuilder> ();

			foreach (MethodInfo declMethod in iface.GetMethods ()) {
				ParameterInfo[] parms = declMethod.GetParameters ();

				Type[] parmTypes = new Type[parms.Length];
				for (int i = 0 ; i < parms.Length ; i++)
					parmTypes[i] = parms[i].ParameterType;

				MethodAttributes attrs = declMethod.Attributes ^ MethodAttributes.Abstract;
				attrs ^= MethodAttributes.NewSlot;
				attrs |= MethodAttributes.Final;
				MethodBuilder method_builder = typeB.DefineMethod (declMethod.Name, attrs, declMethod.ReturnType, parmTypes);
				typeB.DefineMethodOverride (method_builder, declMethod);

				//define in/out/ref/name for each of the parameters
				for (int i = 0; i < parms.Length ; i++)
					method_builder.DefineParameter (i + 1, parms[i].Attributes, parms[i].Name);

				ILGenerator ilg = method_builder.GetILGenerator ();
				GenHookupMethod (ilg, declMethod, sendMethodCallMethod, interfaceName, declMethod.Name);

				if (declMethod.IsSpecialName)
					builders[declMethod.Name] = method_builder;
{'fraction_non_alphanumeric': 0.0991213030432919, 'fraction_numerical': 0.0015002143163309043, 'mean_word_length': 4.955966815571155, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1422670', 'n_tokens_mistral': 2826, 'n_tokens_neox': 2486, 'n_words': 674}