コード例 #1
0
 static DynValue()
 {
     Nil = new DynValue()
     {
         m_Type = DataType.Nil
     }.AsReadOnly();
     Void = new DynValue()
     {
         m_Type = DataType.Void
     }.AsReadOnly();
     True  = DynValue.NewBoolean(true).AsReadOnly();
     False = DynValue.NewBoolean(false).AsReadOnly();
 }
コード例 #2
0
 static DynValue()
 {
     Nil = new DynValue()
     {
         m_Type = DataType.Nil
     };
     Void = new DynValue()
     {
         m_Type = DataType.Void
     };
     True  = DynValue.NewBoolean(true);
     False = DynValue.NewBoolean(false);
 }
コード例 #3
0
        /// <summary>
        /// Checks the type of this value corresponds to the desired type. A propert ScriptRuntimeException is thrown
        /// if the value is not of the specified type or - considering the TypeValidationFlags - is not convertible
        /// to the specified type.
        /// </summary>
        /// <param name="funcName">Name of the function requesting the value, for error message purposes.</param>
        /// <param name="desiredType">The desired data type.</param>
        /// <param name="argNum">The argument number, for error message purposes.</param>
        /// <param name="flags">The TypeValidationFlags.</param>
        /// <returns></returns>
        /// <exception cref="ScriptRuntimeException">Thrown
        /// if the value is not of the specified type or - considering the TypeValidationFlags - is not convertible
        /// to the specified type.</exception>
        public DynValue CheckType(string funcName, DataType desiredType, int argNum = -1, TypeValidationFlags flags = TypeValidationFlags.Default)
        {
            if (this.Type == desiredType)
            {
                return(this);
            }

            bool allowNil = ((int)(flags & TypeValidationFlags.AllowNil) != 0);

            if (allowNil && this.IsNil())
            {
                return(this);
            }

            bool autoConvert = ((int)(flags & TypeValidationFlags.AutoConvert) != 0);

            if (autoConvert)
            {
                if (desiredType == DataType.Boolean)
                {
                    return(DynValue.NewBoolean(this.CastToBool()));
                }

                if (desiredType == DataType.Number)
                {
                    double?v = this.CastToNumber();
                    if (v.HasValue)
                    {
                        return(DynValue.NewNumber(v.Value));
                    }
                }

                if (desiredType == DataType.String)
                {
                    string v = this.CastToString();
                    if (v != null)
                    {
                        return(DynValue.NewString(v));
                    }
                }
            }

            if (this.IsVoid())
            {
                throw ScriptRuntimeException.BadArgumentNoValue(argNum, funcName, desiredType);
            }

            throw ScriptRuntimeException.BadArgument(argNum, funcName, desiredType, this.Type, allowNil);
        }
コード例 #4
0
        /// <summary>
        /// Registers the standard constants (_G, _VERSION, _MOONSHARP) to a table
        /// </summary>
        /// <param name="table">The table.</param>
        public static Table RegisterConstants(this Table table)
        {
            var moonsharpTable = DynValue.NewTable(table.OwnerScript);
            var m = moonsharpTable.Table;

            table.Set("_G", DynValue.NewTable(table));
            table.Set("_VERSION", DynValue.NewString($"MoonSharp {Script.VERSION}"));
            table.Set("_MOONSHARP", moonsharpTable);

            m.Set("version", DynValue.NewString(Script.VERSION));
            m.Set("luacompat", DynValue.NewString(Script.LUA_VERSION));
            m.Set("is_aot", DynValue.NewBoolean(Script.GlobalOptions.Platform.IsRunningOnAOT()));
            m.Set("is_mono", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnMono));

            return(table);
        }
コード例 #5
0
ファイル: ModuleRegister.cs プロジェクト: xiexin36/moonsharp
        /// <summary>
        /// Registers the standard constants (_G, _VERSION, _MOONSHARP) to a table
        /// </summary>
        /// <param name="table">The table.</param>
        /// <returns></returns>
        public static Table RegisterConstants(this Table table)
        {
            DynValue moonsharp_table = DynValue.NewTable(table.OwnerScript);
            Table    m = moonsharp_table.Table;

            table.Set("_G", DynValue.NewTable(table));
            table.Set("_VERSION", DynValue.NewString(string.Format("MoonSharp {0}", Script.VERSION)));
            table.Set("_MOONSHARP", moonsharp_table);

            m.Set("version", DynValue.NewString(Script.VERSION));
            m.Set("luacompat", DynValue.NewString(Script.LUA_VERSION));
            m.Set("platform", DynValue.NewString(Script.GlobalOptions.Platform.GetPlatformName()));
            m.Set("is_aot", DynValue.NewBoolean(Script.GlobalOptions.Platform.IsRunningOnAOT()));
            m.Set("is_unity", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnUnity));
            m.Set("is_mono", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnMono));
            m.Set("is_clr4", DynValue.NewBoolean(PlatformAutoDetector.IsRunningOnClr4));
            m.Set("is_pcl", DynValue.NewBoolean(PlatformAutoDetector.IsPortableFramework));

            return(table);
        }
コード例 #6
0
            public static DynValue ClassToLuaTable(Script script, object obj)
            {
                MoonSharp.Interpreter.Table table = new MoonSharp.Interpreter.Table(script);
                var properties = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly);

                for (int i = 0; i < properties.Length; i++)
                {
                    var property = properties[i];

                    var value = property.GetValue(obj, null);
                    if (value == null)
                    {
                        continue;
                    }

                    if (value.GetType() == typeof(string))
                    {
                        table.Set(property.Name, DynValue.NewString((string)value));
                    }
                    else if (value.GetType() == typeof(int))
                    {
                        table.Set(property.Name, DynValue.NewNumber((int)value));
                    }
                    else if (value.GetType() == typeof(float))
                    {
                        table.Set(property.Name, DynValue.NewNumber((float)value));
                    }
                    else if (value.GetType() == typeof(bool))
                    {
                        table.Set(property.Name, DynValue.NewBoolean((bool)value));
                    }
                    else if (value is ProtoBuf.IExtensible)
                    {
                        table.Set(property.Name, ClassToLuaTable(script, value));
                    }
                    else if (value is IList)
                    {
                        IList list = value as IList;
                        MoonSharp.Interpreter.Table value_table = new MoonSharp.Interpreter.Table(script);
                        for (int j = 0; j < list.Count; ++j)
                        {
                            DynValue item = null;
                            if (list[j] is ProtoBuf.IExtensible)
                            {
                                item = ClassToLuaTable(script, list[j]);
                            }
                            else if (list[j] is int)
                            {
                                item = DynValue.NewNumber((int)list[j]);
                            }
                            else if (list[j] is float)
                            {
                                item = DynValue.NewNumber((float)list[j]);
                            }
                            else if (list[j] is string)
                            {
                                item = DynValue.NewString((string)list[j]);
                            }
                            else if (list[j] is bool)
                            {
                                item = DynValue.NewBoolean((bool)list[j]);
                            }
                            else
                            {
                                Debug.LogError("Convert List to lua table error: unsupported types " + list[j].GetType());
                            }
                            value_table.Append(item);
                        }
                        table.Set(property.Name, DynValue.NewTable(value_table));
                    }
                    else
                    {
                        Debug.LogError("Convert class to lua table error: unsupported types " + value);
                    }
                }

                return(DynValue.NewTable(table));
            }