コード例 #1
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]);
                }
            }
        }
コード例 #2
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);
                }
            }
        }
コード例 #3
0
ファイル: Script.cs プロジェクト: nicklausd/moonsharp
        /// <summary>
        /// Initializes a new instance of the <see cref="Script"/> class.
        /// </summary>
        /// <param name="coreModules">The core modules to be pre-registered in the default global table.</param>
        public Script(CoreModules coreModules)
        {
            Options          = new ScriptOptions(DefaultOptions);
            PerformanceStats = new PerformanceStatistics();
            Registry         = new Table(this);

            m_ByteCode      = new ByteCode(this);
            m_MainProcessor = new Processor(this, m_GlobalTable, m_ByteCode);
            m_GlobalTable   = new Table(this).RegisterCoreModules(coreModules);
            foreach (var t in TypeDescriptorRegistry.RegisteredTypes)
            {
                Type valType = t.Value.Type;
                if (valType.GetTypeInfo().Assembly.FullName != typeof(Script).GetTypeInfo().Assembly.FullName)
                {
                    // m_GlobalTable.RegisterModuleType(t.Value.Type);
                    if (t.Value is StandardUserDataDescriptor)
                    {
                        StandardUserDataDescriptor desc = (StandardUserDataDescriptor)t.Value;
                        foreach (var member in desc.Members)
                        {
                            if (member.Value is MethodMemberDescriptor)
                            {
                                MethodMemberDescriptor methDesc = (MethodMemberDescriptor)member.Value;
                                if (methDesc.IsConstructor)
                                {
                                    m_GlobalTable.Set(methDesc.Name, methDesc.GetCallbackAsDynValue(this));
                                }
                            }
                            else if (member.Value is OverloadedMethodMemberDescriptor)
                            {
                                OverloadedMethodMemberDescriptor methDesc = (OverloadedMethodMemberDescriptor)member.Value;
                                foreach (var overloadDesc in methDesc.m_Overloads)
                                {
                                    if (overloadDesc is MethodMemberDescriptor)
                                    {
                                        MethodMemberDescriptor actualDesc = (MethodMemberDescriptor)overloadDesc;
                                        if (actualDesc.IsConstructor)
                                        {
                                            //m_GlobalTable.Set(desc.FriendlyName, actualDesc.GetCallbackAsDynValue(this));
                                            m_GlobalTable.Set(desc.FriendlyName, DynValue.NewCallback(methDesc.GetCallbackFunction(this)));
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
コード例 #4
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;
				}
			}
		}
コード例 #5
0
        private List <string> Proposal(UserData userdata)
        {
            List <string> result = new List <string>();

            if (userdata.Descriptor is StandardUserDataDescriptor)
            {
                StandardUserDataDescriptor dscr = (StandardUserDataDescriptor)userdata.Descriptor;


                if (userdata.Object == null)
                {
                    string st = dscr.MemberNames.First();
                    if (dscr.Members.Count() > 0 && dscr.MemberNames.First() == "__new")
                    {
                        result.Add("__new()");
                    }
                    return(result);
                }
                System.Reflection.MethodInfo[] methods = userdata.Object.GetType().GetMethods();
                foreach (System.Reflection.MethodInfo m in methods)
                {
                    string memberResult = m.Name;
                    System.Reflection.ParameterInfo[] methodparams = m.GetParameters();

                    memberResult += "(";
                    foreach (System.Reflection.ParameterInfo param in methodparams)
                    {
                        if (memberResult[memberResult.Length - 1] != '(')
                        {
                            memberResult += ",";
                        }
                        memberResult += param.Name;
                    }
                    memberResult += ")";

                    result.Add(memberResult);
                }
                System.Reflection.FieldInfo[] fields = userdata.Object.GetType().GetFields();
                foreach (System.Reflection.FieldInfo f in fields)
                {
                    result.Add(f.Name);
                }

                /*
                 * //NOTE: This code results in AmbiguousMatchException
                 * foreach (var member in dscr.Members) {
                 *  var memberResult = member.Key;
                 *
                 *  // is Method?
                 *  if (member.Value is OverloadedMethodMemberDescriptor) {
                 *      var declaringType = (member.Value as OverloadedMethodMemberDescriptor).DeclaringType;
                 *
                 *      try {
                 *          var m1 = declaringType.GetMethod(member.Key); //NOTE: Will trigger AmbiguousMatchException if two functions of same name but with different parameters are found
                 *          //var m1 = declaringType.GetMethod(member.Key);
                 *
                 *          if (m1 == null) {
                 *              result.Add(memberResult);
                 *              continue;
                 *          }
                 *          var methodparams = m1.GetParameters();
                 *
                 *          memberResult += "(";
                 *          foreach (var param in methodparams) {
                 *              if (memberResult[memberResult.Length - 1] != '(') {
                 *                  memberResult += ",";
                 *              }
                 *              memberResult += param.Name;
                 *          }
                 *          memberResult += ")";
                 *      }
                 *      catch (Exception e) {
                 *          Debug.LogWarning("Problem is processing userdata/members with memberName:" + member.Key);
                 *          Debug.LogException(e);
                 *      }
                 *  }
                 *  result.Add(memberResult);
                 * }
                 */
            }
            else if (userdata.Descriptor is StandardEnumUserDataDescriptor)
            {
                StandardEnumUserDataDescriptor dscr = (StandardEnumUserDataDescriptor)userdata.Descriptor;
                foreach (string ev in dscr.MemberNames)
                {
                    result.Add(ev.ToString());
                }
            }
            else
            {
                Debug.LogWarning("Proposal from Userdata cannot process UserData-Descriptor-Type:" + userdata.Descriptor.GetType().ToString());
            }

            return(result);
        }
コード例 #6
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];
            }
        }
コード例 #7
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]);
                }
            }
        }