Ejemplo n.º 1
0
		/// <summary>
		/// Called to handle the registration or deregistration of a type descriptor. Must return the type descriptor to be registered, or null to remove the registration.
		/// </summary>
		/// <param name="newDescriptor">The new descriptor, or null if this is a deregistration.</param>
		/// <param name="oldDescriptor">The old descriptor, or null if no descriptor was previously registered for this type.</param>
		/// <returns></returns>
		public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
		{
			if (newDescriptor == null)
				return null;
			else
				return oldDescriptor ?? newDescriptor;
		}
Ejemplo n.º 2
0
		/// <summary>
		/// Called to handle the registration or deregistration of a type descriptor. Must return the type descriptor to be registered, or null to remove the registration.
		/// </summary>
		/// <param name="newDescriptor">The new descriptor, or null if this is a deregistration.</param>
		/// <param name="oldDescriptor">The old descriptor, or null if no descriptor was previously registered for this type.</param>
		/// <returns></returns>
		public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
		{
			if (newDescriptor == null)
				return null;
			else
				return oldDescriptor ?? newDescriptor;
		}
 internal ProxyUserDataDescriptor(IProxyFactory proxyFactory, IUserDataDescriptor proxyDescriptor,
                                  string friendlyName = null)
 {
     m_ProxyFactory       = proxyFactory;
     this.Name            = friendlyName ?? (proxyFactory.TargetType.Name + "::proxy");
     this.InnerDescriptor = proxyDescriptor;
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Creates a userdata DynValue from the specified object, using a specific descriptor
 /// </summary>
 /// <param name="o">The object</param>
 /// <param name="descr">The descriptor.</param>
 /// <returns></returns>
 public static DynValue Create(object o, IUserDataDescriptor descr)
 {
     return(DynValue.NewUserData(new UserData()
     {
         Descriptor = descr,
         Object = o
     }));
 }
Ejemplo n.º 5
0
        public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
        {
            if (oldDescriptor == null && newDescriptor != null)
            {
                return newDescriptor;
            }

            return oldDescriptor;
        }
Ejemplo n.º 6
0
        public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
        {
            if (oldDescriptor == null && newDescriptor != null)
            {
                return(newDescriptor);
            }

            return(oldDescriptor);
        }
Ejemplo n.º 7
0
		/// <summary>
		/// Creates a static userdata DynValue from the specified IUserDataDescriptor
		/// </summary>
		/// <param name="descr">The IUserDataDescriptor</param>
		/// <returns></returns>
		public static DynValue CreateStatic(IUserDataDescriptor descr)
		{
			if (descr == null) return null;

			return DynValue.NewUserData(new UserData()
			{
				Descriptor = descr,
				Object = null
			});
		}
Ejemplo n.º 8
0
 private void Start()
 {
     script        = new Script();
     robot_context = new RobotContext(this);
     foreach (var type in registered_types)
     {
         UserData.RegisterType(type);
     }
     robot_context_desc = UserData.GetDescriptorForObject(robot_context);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Called to handle the registration or deregistration of a type descriptor. Must return the type descriptor to be registered, or null to remove the registration.
 /// </summary>
 /// <param name="newDescriptor">The new descriptor, or null if this is a deregistration.</param>
 /// <param name="oldDescriptor">The old descriptor, or null if no descriptor was previously registered for this type.</param>
 /// <returns></returns>
 public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
 {
     if (newDescriptor == null)
     {
         return(null);
     }
     else
     {
         return(oldDescriptor ?? newDescriptor);
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Registers a type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
        /// <param name="friendlyName">Friendly name of the descriptor.</param>
        /// <param name="descriptor">The descriptor, or null to use a default one.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
        {
            accessMode = ResolveDefaultAccessModeForType(accessMode, type);

            lock (s_Lock)
            {
                IUserDataDescriptor oldDescriptor = null;
                s_TypeRegistry.TryGetValue(type, out oldDescriptor);

                if (descriptor == null)
                {
                    if (IsTypeBlacklisted(type))
                    {
                        return(null);
                    }

                    if (Framework.Do.GetInterfaces(type).Any(ii => ii == typeof(IUserDataType)))
                    {
                        AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                        return(PerformRegistration(type, audd, oldDescriptor));
                    }
                    else if (Framework.Do.IsGenericTypeDefinition(type))
                    {
                        StandardGenericsUserDataDescriptor typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
                        return(PerformRegistration(type, typeGen, oldDescriptor));
                    }
                    else if (Framework.Do.IsEnum(type))
                    {
                        var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
                        return(PerformRegistration(type, enumDescr, oldDescriptor));
                    }
                    else
                    {
                        StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);

                        if (accessMode == InteropAccessMode.BackgroundOptimized)
                        {
#if NETFX_CORE
                            System.Threading.Tasks.Task.Run(() => ((IOptimizableDescriptor)udd).Optimize());
#else
                            ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor)udd).Optimize());
#endif
                        }

                        return(PerformRegistration(type, udd, oldDescriptor));
                    }
                }
                else
                {
                    PerformRegistration(type, descriptor, oldDescriptor);
                    return(descriptor);
                }
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Creates a static userdata DynValue from the specified IUserDataDescriptor
        /// </summary>
        /// <param name="descr">The IUserDataDescriptor</param>
        /// <returns></returns>
        public static DynValue CreateStatic(IUserDataDescriptor descr)
        {
            if (descr == null)
            {
                return(DynValue.Invalid);
            }

            var userData = UserDataRef.Request();

            userData.Descriptor = descr;
            return(DynValue.NewUserData(userData));
        }
Ejemplo n.º 12
0
		public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
		{
			//if (oldDescriptor != null && oldDescriptor.Type.IsGenericType)
			//	return newDescriptor;

			if (oldDescriptor == null && newDescriptor != null)
			{
				var backupColor = Console.ForegroundColor;
				Console.ForegroundColor = ConsoleColor.Magenta;
				Console.WriteLine("Registering type {0} with {1}", newDescriptor.Type.Name, newDescriptor.GetType().Name);
				Console.ForegroundColor = backupColor;
				return newDescriptor;
			}

			return oldDescriptor;
		}
Ejemplo n.º 13
0
        public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
        {
            //if (oldDescriptor != null && oldDescriptor.Type.IsGenericType)
            //	return newDescriptor;

            if (oldDescriptor == null && newDescriptor != null)
            {
                var backupColor = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Magenta;
                Console.WriteLine("Registering type {0} with {1}", newDescriptor.Type.Name, newDescriptor.GetType().Name);
                Console.ForegroundColor = backupColor;
                return(newDescriptor);
            }

            return(oldDescriptor);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Creates a userdata DynValue from the specified object, using a specific descriptor
        /// </summary>
        /// <param name="o">The object</param>
        /// <param name="descr">The descriptor.</param>
        /// <returns></returns>
        public static DynValue CreateWithDescriptor <T>(T o, IUserDataDescriptor descr)
        {
            IUserData userData;

            if (typeof(T).IsValueType)
            {
                userData = UserDataStruct <T> .Request();
            }
            else
            {
                userData = UserDataRef.Request();
            }
            userData.Descriptor = descr;
            userData.TrySet(o);

            return(DynValue.NewUserData(userData));
        }
Ejemplo n.º 15
0
        private static IUserDataDescriptor PerformRegistration(Type type, IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
        {
            IUserDataDescriptor result = RegistrationPolicy.HandleRegistration(newDescriptor, oldDescriptor);

            if (result != oldDescriptor)
            {
                if (result == null)
                {
                    s_TypeRegistry.Remove(type);
                }
                else
                {
                    s_TypeRegistry[type]        = result;
                    s_TypeRegistryHistory[type] = result;
                }
            }

            return(result);
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Registers a type with a custom userdata descriptor
 /// </summary>
 /// <param name="customDescriptor">The custom descriptor.</param>
 public static IUserDataDescriptor RegisterType(IUserDataDescriptor customDescriptor)
 {
     return(TypeDescriptorRegistry.RegisterType_Impl(customDescriptor.Type, InteropAccessMode.Default, null, customDescriptor));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Called to handle the registration or deregistration of a type descriptor. Must return the type descriptor to be registered, or null to remove the registration.
 /// </summary>
 /// <param name="newDescriptor">The new descriptor, or null if this is a deregistration.</param>
 /// <param name="oldDescriptor">The old descriptor, or null if no descriptor was previously registered for this type.</param>
 /// <returns></returns>
 public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
 {
     return(oldDescriptor ?? newDescriptor);
 }
Ejemplo n.º 18
0
		/// <summary>
		/// Registers a type with a custom userdata descriptor
		/// </summary>
		/// <typeparam name="T">The type to be registered</typeparam>
		/// <param name="customDescriptor">The custom descriptor.</param>
		public static IUserDataDescriptor RegisterType<T>(IUserDataDescriptor customDescriptor)
		{
			return TypeDescriptorRegistry.RegisterType_Impl(typeof(T), InteropAccessMode.Default, null, customDescriptor);
		}
Ejemplo n.º 19
0
		/// <summary>
		/// Creates a ScriptRuntimeException with a predefined error message specifying that
		/// an attempt to access a non-static member from a static userdata was made
		/// </summary>
		/// <param name="typeDescr">The type descriptor.</param>
		/// <param name="desc">The member descriptor.</param>
		/// <returns></returns>
		public static ScriptRuntimeException AccessInstanceMemberOnStatics(IUserDataDescriptor typeDescr, IMemberDescriptor desc)
		{
			return new ScriptRuntimeException("attempt to access instance member {0}.{1} from a static userdata", typeDescr.Name, desc.Name);
		}
Ejemplo n.º 20
0
        /// <summary>
        /// Registers a type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
        /// <param name="friendlyName">Friendly name of the descriptor.</param>
        /// <param name="descriptor">The descriptor, or null to use a default one.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                MoonSharpUserDataAttribute attr = type.GetCustomAttributes(true).OfType <MoonSharpUserDataAttribute>()
                                                  .SingleOrDefault();

                if (attr != null)
                {
                    accessMode = attr.AccessMode;
                }
            }


            if (accessMode == InteropAccessMode.Default)
            {
                accessMode = s_DefaultAccessMode;
            }

            lock (s_Lock)
            {
                if (!s_Registry.ContainsKey(type))
                {
                    if (descriptor == null)
                    {
                        if (IsTypeBlacklisted(type))
                        {
                            return(null);
                        }

                        if (type.GetInterfaces().Any(ii => ii == typeof(IUserDataType)))
                        {
                            AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, audd);
                            return(audd);
                        }
                        else if (type.IsGenericTypeDefinition)
                        {
                            StandardGenericsUserDataDescriptor typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
                            s_Registry.Add(type, typeGen);
                            return(typeGen);
                        }
                        else if (type.IsEnum)
                        {
                            var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, enumDescr);
                            return(enumDescr);
                        }
                        else
                        {
                            StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
                            s_Registry.Add(type, udd);

                            if (accessMode == InteropAccessMode.BackgroundOptimized)
                            {
                                ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor)udd).Optimize());
                            }

                            return(udd);
                        }
                    }
                    else
                    {
                        s_Registry.Add(type, descriptor);
                        return(descriptor);
                    }
                }
                else
                {
                    return(s_Registry[type]);
                }
            }
        }
		/// <summary>
		/// Registers a type
		/// </summary>
		/// <param name="type">The type.</param>
		/// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
		/// <param name="friendlyName">Friendly name of the descriptor.</param>
		/// <param name="descriptor">The descriptor, or null to use a default one.</param>
		/// <returns></returns>
		internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
		{
			accessMode = ResolveDefaultAccessModeForType(accessMode, type);

			lock (s_Lock)
			{
				IUserDataDescriptor oldDescriptor = null;
				s_TypeRegistry.TryGetValue(type, out oldDescriptor);

				if (descriptor == null)
				{
					if (IsTypeBlacklisted(type))
						return null;

					if (Framework.Do.GetInterfaces(type).Any(ii => ii == typeof(IUserDataType)))
					{
						AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
						return PerformRegistration(type, audd, oldDescriptor);
					}
					else if (Framework.Do.IsGenericTypeDefinition(type))
					{
						StandardGenericsUserDataDescriptor typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
						return PerformRegistration(type, typeGen, oldDescriptor);
					}
					else if (Framework.Do.IsEnum(type))
					{
						var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
						return PerformRegistration(type, enumDescr, oldDescriptor);
					}
					else
					{
						StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);

						if (accessMode == InteropAccessMode.BackgroundOptimized)
						{
#if NETFX_CORE
							System.Threading.Tasks.Task.Run(() => ((IOptimizableDescriptor)udd).Optimize());
#else
							ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor)udd).Optimize());
#endif
						}

						return PerformRegistration(type, udd, oldDescriptor);
					}
				}
				else
				{
					PerformRegistration(type, descriptor, oldDescriptor);
					return descriptor;
				}
			}
		}
Ejemplo n.º 22
0
        private static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                MoonSharpUserDataAttribute attr = type.GetCustomAttributes(true).OfType <MoonSharpUserDataAttribute>()
                                                  .SingleOrDefault();

                if (attr != null)
                {
                    accessMode = attr.AccessMode;
                }
            }


            if (accessMode == InteropAccessMode.Default)
            {
                accessMode = s_DefaultAccessMode;
            }

            lock (s_Lock)
            {
                if (!s_Registry.ContainsKey(type))
                {
                    if (descriptor == null)
                    {
                        if (type.GetInterfaces().Any(ii => ii == typeof(IUserDataType)))
                        {
                            AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, audd);
                            return(audd);
                        }
                        else
                        {
                            StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
                            s_Registry.Add(type, udd);

                            if (accessMode == InteropAccessMode.BackgroundOptimized)
                            {
                                ThreadPool.QueueUserWorkItem(o => udd.Optimize());
                            }

                            return(udd);
                        }
                    }
                    else
                    {
                        s_Registry.Add(type, descriptor);
                        return(descriptor);
                    }
                }
                else
                {
                    return(s_Registry[type]);
                }
            }
        }
Ejemplo n.º 23
0
        /// <summary>
        /// Gets the best possible type descriptor for a specified CLR type.
        /// </summary>
        /// <param name="type">The CLR type for which the descriptor is desired.</param>
        /// <param name="searchInterfaces">if set to <c>true</c> interfaces are used in the search.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor GetDescriptorForType(Type type, bool searchInterfaces)
        {
            lock (s_Lock)
            {
                IUserDataDescriptor typeDescriptor = null;

                // if the type has been explicitly registered, return its descriptor as it's complete
                if (type.IsGenericType)
                {
                    IUserDataDescriptor iudd = null;
                    if (s_TypeRegistry.TryGetValue(type.BaseType, out iudd))
                    {
                        return(iudd);
                    }
                }
                else
                {
                    IUserDataDescriptor iudd = null;
                    if (s_TypeRegistry.TryGetValue(type, out iudd))
                    {
                        return(iudd);
                    }
                }

                if (RegistrationPolicy.AllowTypeAutoRegistration(type))
                {
                    // no autoreg of delegates
                    if (!Framework.Do.IsAssignableFrom((typeof(Delegate)), type))
                    {
                        return(RegisterType_Impl(type, DefaultAccessMode, type.FullName, null));
                    }
                }

                // search for the base object descriptors
                for (Type t = type; t != null; t = Framework.Do.GetBaseType(t))
                {
                    IUserDataDescriptor u;

                    if (s_TypeRegistry.TryGetValue(t, out u))
                    {
                        typeDescriptor = u;
                        break;
                    }
                    else if (Framework.Do.IsGenericType(t))
                    {
                        if (s_TypeRegistry.TryGetValue(t.GetGenericTypeDefinition(), out u))
                        {
                            typeDescriptor = u;
                            break;
                        }
                    }
                }

                if (typeDescriptor is IGeneratorUserDataDescriptor)
                {
                    typeDescriptor = ((IGeneratorUserDataDescriptor)typeDescriptor).Generate(type);
                }


                // we should not search interfaces (for example, it's just for statics..), no need to look further
                if (!searchInterfaces || typeDescriptor != null)
                {
                    return(typeDescriptor);
                }

                if (searchInterfaces)
                {
                    foreach (Type interfaceType in Framework.Do.GetInterfaces(type))
                    {
                        IUserDataDescriptor interfaceDescriptor;

                        if (s_TypeRegistry.TryGetValue(interfaceType, out interfaceDescriptor))
                        {
                            if (interfaceDescriptor is IGeneratorUserDataDescriptor)
                            {
                                interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type);
                            }

                            if (interfaceDescriptor != null)
                            {
                                return(interfaceDescriptor);
                            }
                        }
                        else if (Framework.Do.IsGenericType(interfaceType))
                        {
                            if (s_TypeRegistry.TryGetValue(interfaceType.GetGenericTypeDefinition(), out interfaceDescriptor))
                            {
                                if (interfaceDescriptor is IGeneratorUserDataDescriptor)
                                {
                                    interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type);
                                }

                                if (interfaceDescriptor != null)
                                {
                                    return(interfaceDescriptor);
                                }
                            }
                        }
                    }
                }

                return(null);
            }
        }
Ejemplo n.º 24
0
        /// <summary>
        /// Registers a proxy type.
        /// </summary>
        /// <param name="proxyFactory">The proxy factory.</param>
        /// <param name="accessMode">The access mode.</param>
        /// <param name="friendlyName">Name of the friendly.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterProxyType_Impl(IProxyFactory proxyFactory, InteropAccessMode accessMode, string friendlyName)
        {
            IUserDataDescriptor proxyDescriptor = RegisterType_Impl(proxyFactory.ProxyType, accessMode, friendlyName, null);

            return(RegisterType_Impl(proxyFactory.TargetType, accessMode, friendlyName, new ProxyUserDataDescriptor(proxyFactory, proxyDescriptor, friendlyName)));
        }
Ejemplo n.º 25
0
        /// <summary>
        /// Gets the best possible type descriptor for a specified CLR type.
        /// </summary>
        /// <param name="type">The CLR type for which the descriptor is desired.</param>
        /// <param name="searchInterfaces">if set to <c>true</c> interfaces are used in the search.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor GetDescriptorForType(Type type, bool searchInterfaces)
        {
            lock (s_Lock)
            {
                IUserDataDescriptor typeDescriptor = null;

                // if the type has been explicitly registered, return its descriptor as it's complete
                if (s_TypeRegistry.ContainsKey(type))
                {
                    return(s_TypeRegistry[type]);
                }

                if (RegistrationPolicy.AllowTypeAutoRegistration(type))
                {
                    // no autoreg of delegates
                    if (!typeof(Delegate).IsAssignableFrom(type))
                    {
                        return(RegisterType_Impl(type, DefaultAccessMode, type.FullName, null));
                    }
                }

                // search for the base object descriptors
                for (Type t = type; t != null; t = t.BaseType)
                {
                    IUserDataDescriptor u;

                    if (s_TypeRegistry.TryGetValue(t, out u))
                    {
                        typeDescriptor = u;
                        break;
                    }
                    else if (t.IsGenericType)
                    {
                        if (s_TypeRegistry.TryGetValue(t.GetGenericTypeDefinition(), out u))
                        {
                            typeDescriptor = u;
                            break;
                        }
                    }
                }

                if (typeDescriptor is IGeneratorUserDataDescriptor)
                {
                    typeDescriptor = ((IGeneratorUserDataDescriptor)typeDescriptor).Generate(type);
                }


                // we should not search interfaces (for example, it's just for statics..), no need to look further
                if (!searchInterfaces)
                {
                    return(typeDescriptor);
                }

                List <IUserDataDescriptor> descriptors = new List <IUserDataDescriptor>();

                if (typeDescriptor != null)
                {
                    descriptors.Add(typeDescriptor);
                }


                if (searchInterfaces)
                {
                    foreach (Type interfaceType in type.GetInterfaces())
                    {
                        IUserDataDescriptor interfaceDescriptor;

                        if (s_TypeRegistry.TryGetValue(interfaceType, out interfaceDescriptor))
                        {
                            if (interfaceDescriptor is IGeneratorUserDataDescriptor)
                            {
                                interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type);
                            }

                            if (interfaceDescriptor != null)
                            {
                                descriptors.Add(interfaceDescriptor);
                            }
                        }
                        else if (interfaceType.IsGenericType)
                        {
                            if (s_TypeRegistry.TryGetValue(interfaceType.GetGenericTypeDefinition(), out interfaceDescriptor))
                            {
                                if (interfaceDescriptor is IGeneratorUserDataDescriptor)
                                {
                                    interfaceDescriptor = ((IGeneratorUserDataDescriptor)interfaceDescriptor).Generate(type);
                                }

                                if (interfaceDescriptor != null)
                                {
                                    descriptors.Add(interfaceDescriptor);
                                }
                            }
                        }
                    }
                }

                if (descriptors.Count == 1)
                {
                    return(descriptors[0]);
                }
                else if (descriptors.Count == 0)
                {
                    return(null);
                }
                else
                {
                    return(new CompositeUserDataDescriptor(descriptors, type));
                }
            }
        }
		internal ProxyUserDataDescriptor(IProxyFactory proxyFactory, IUserDataDescriptor proxyDescriptor, string friendlyName = null)
		{
			m_ProxyFactory = proxyFactory;
			Name = friendlyName ?? (proxyFactory.TargetType.Name + "::proxy");
			m_ProxyDescriptor = proxyDescriptor;
		}
Ejemplo n.º 27
0
        private static IUserDataDescriptor GetDescriptorForType(Type type, bool searchInterfaces)
        {
            lock (s_Lock)
            {
                IUserDataDescriptor typeDescriptor = null;

                // if the type has been explicitly registered, return its descriptor as it's complete
                if (s_Registry.ContainsKey(type))
                {
                    return(s_Registry[type]);
                }

                if (RegistrationPolicy == InteropRegistrationPolicy.Automatic)
                {
                    return(RegisterType_Impl(type, DefaultAccessMode, type.FullName, null));
                }

                // search for the base object descriptors
                for (Type t = type; t != null; t = t.BaseType)
                {
                    IUserDataDescriptor u;

                    if (s_Registry.TryGetValue(t, out u))
                    {
                        typeDescriptor = u;
                        break;
                    }
                }

                // we should not search interfaces (for example, it's just for statics..), no need to look further
                if (!searchInterfaces)
                {
                    return(typeDescriptor);
                }

                List <IUserDataDescriptor> descriptors = new List <IUserDataDescriptor>();

                if (typeDescriptor != null)
                {
                    descriptors.Add(typeDescriptor);
                }


                if (searchInterfaces)
                {
                    foreach (Type t in type.GetInterfaces())
                    {
                        IUserDataDescriptor u;

                        if (s_Registry.TryGetValue(t, out u))
                        {
                            descriptors.Add(u);
                        }
                    }
                }

                if (descriptors.Count == 1)
                {
                    return(descriptors[0]);
                }
                else if (descriptors.Count == 0)
                {
                    return(null);
                }
                else
                {
                    return(new CompositeUserDataDescriptor(descriptors, type));
                }
            }
        }
		private static IUserDataDescriptor PerformRegistration(Type type, IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
		{
			IUserDataDescriptor result = RegistrationPolicy.HandleRegistration(newDescriptor, oldDescriptor);

			if (result != oldDescriptor)
			{
				if (result == null)
				{
					s_TypeRegistry.Remove(type);
				}
				else
				{
					s_TypeRegistry[type] = result;
					s_TypeRegistryHistory[type] = result;
				}
			}

			return result;
		}
Ejemplo n.º 29
0
 public static void RegisterType(Type type, IUserDataDescriptor customDescriptor)
 {
     RegisterType_Impl(type, InteropAccessMode.Default, null, customDescriptor);
 }
Ejemplo n.º 30
0
 /// <summary>
 /// Creates a ScriptRuntimeException with a predefined error message specifying that
 /// an attempt to access a non-static member from a static userdata was made
 /// </summary>
 /// <param name="typeDescr">The type descriptor.</param>
 /// <param name="desc">The member descriptor.</param>
 /// <returns></returns>
 public static ScriptRuntimeException AccessInstanceMemberOnStatics(IUserDataDescriptor typeDescr, IMemberDescriptor desc)
 {
     return(new ScriptRuntimeException("attempt to access instance member {0}.{1} from a static userdata", typeDescr.Name, desc.Name));
 }
Ejemplo n.º 31
0
        /// <summary>
        ///     Registers a type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
        /// <param name="friendlyName">Friendly name of the descriptor.</param>
        /// <param name="descriptor">The descriptor, or null to use a default one.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode,
            string friendlyName, IUserDataDescriptor descriptor)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                var attr = type.GetCustomAttributes(true).OfType<MoonSharpUserDataAttribute>()
                    .SingleOrDefault();

                if (attr != null)
                    accessMode = attr.AccessMode;
            }


            if (accessMode == InteropAccessMode.Default)
                accessMode = s_DefaultAccessMode;

            lock (s_Lock)
            {
                if (!s_Registry.ContainsKey(type))
                {
                    if (descriptor == null)
                    {
                        if (IsTypeBlacklisted(type))
                            return null;

                        if (type.GetInterfaces().Any(ii => ii == typeof (IUserDataType)))
                        {
                            var audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, audd);
                            return audd;
                        }
                        if (type.IsGenericTypeDefinition)
                        {
                            var typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
                            s_Registry.Add(type, typeGen);
                            return typeGen;
                        }
                        if (type.IsEnum)
                        {
                            var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, enumDescr);
                            return enumDescr;
                        }
                        var udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
                        s_Registry.Add(type, udd);

                        if (accessMode == InteropAccessMode.BackgroundOptimized)
                        {
                            ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor) udd).Optimize());
                        }

                        return udd;
                    }
                    s_Registry.Add(type, descriptor);
                    return descriptor;
                }
                return s_Registry[type];
            }
        }
		/// <summary>
		/// Called to handle the registration or deregistration of a type descriptor. Must return the type descriptor to be registered, or null to remove the registration.
		/// </summary>
		/// <param name="newDescriptor">The new descriptor, or null if this is a deregistration.</param>
		/// <param name="oldDescriptor">The old descriptor, or null if no descriptor was previously registered for this type.</param>
		/// <returns></returns>
		public IUserDataDescriptor HandleRegistration(IUserDataDescriptor newDescriptor, IUserDataDescriptor oldDescriptor)
		{
			return oldDescriptor ?? newDescriptor;
		}
Ejemplo n.º 33
0
 public static void RegisterUserDataType(Type t, IUserDataDescriptor descriptor)
 {
     UserData.RegisterType(t, descriptor);
 }