lua_tostring() public static méthode

public static lua_tostring ( IntPtr luaState, int index ) : string
luaState IntPtr
index int
Résultat string
Exemple #1
0
        private static int Panic(IntPtr L)
        {
            string text = string.Format("PANIC: unprotected error in call to Lua API ({0})", LuaDLL.lua_tostring(L, -1));

            throw new Exception(text);
        }
Exemple #2
0
        static int PanicCallback(IntPtr luaState)
        {
            string reason = String.Format("unprotected error in call to Lua API ({0})", LuaDLL.lua_tostring(luaState, -1));

            //        lua_tostring(L, -1);

            throw new LuaException(reason);
        }
Exemple #3
0
        /*
         * Calls the object as a function with the provided arguments and
         * casting returned values to the types in returnTypes before returning
         * them in an array
         */
        internal object[] callFunction(object function, object[] args, Type[] returnTypes)
        {
            //int nArgs=0;
            //int oldTop=LuaDLL.lua_gettop(L);
            //if(!LuaDLL.lua_checkstack(L,args.Length+6))
            //    throw new LuaException("Lua stack overflow");
            //translator.push(L,function);
            //if(args!=null)
            //{
            //    nArgs=args.Length;
            //    for(int i=0;i<args.Length;i++)
            //    {
            //        translator.push(L,args[i]);
            //    }
            //}
            //int error = LuaDLL.lua_pcall(L, nArgs, -1, 0);
            //if (error != 0)
            //    ThrowExceptionFromError(oldTop);

            //if(returnTypes != null)
            //    return translator.popValues(L,oldTop,returnTypes);
            //else
            //    return translator.popValues(L, oldTop);

            int nArgs = 0;

            LuaDLL.lua_getglobal(L, "traceback");
            int oldTop = LuaDLL.lua_gettop(L);

            if (!LuaDLL.lua_checkstack(L, args.Length + 6))
            {
                LuaDLL.lua_pop(L, 1);
                throw new LuaException("Lua stack overflow");
            }

            translator.push(L, function);

            if (args != null)
            {
                nArgs = args.Length;
                for (int i = 0; i < args.Length; i++)
                {
                    translator.push(L, args[i]);
                }
            }

            int error = LuaDLL.lua_pcall(L, nArgs, -1, -nArgs - 2);

            if (error != 0)
            {
                string err = LuaDLL.lua_tostring(L, -1);
                LuaDLL.lua_settop(L, oldTop);
                //LuaTypes luatype = LuaDLL.lua_type(L, -1);
                LuaDLL.lua_pop(L, 1);
                if (err == null)
                {
                    err = "Unknown Lua Error";
                }
                throw new LuaScriptException(err.ToString(), "");
            }

            object[] ret = returnTypes != null?translator.popValues(L, oldTop, returnTypes) : translator.popValues(L, oldTop);

            LuaDLL.lua_pop(L, 1);
            return(ret);
        }
Exemple #4
0
        public static int panic(IntPtr L)
        {
            string reason = String.Format("unprotected error in call to Lua API ({0})", LuaDLL.lua_tostring(L, -1));

            LuaDLL.lua_pop(L, 1);
            throw new LuaException(reason);
        }
 public string LuaToString(int index)
 {
     return(LuaDLL.lua_tostring(L, index));
 }
Exemple #6
0
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }

            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;     // Silently convert nullable types to their non null requics
            }

            long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
            {
                return(extractValues[runtimeHandleValue]);
            }

            //CP: Added support for generic parameters
            if (paramType.IsGenericParameter)
            {
                if (luatype == LuaTypes.LUA_TBOOLEAN)
                {
                    return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TSTRING)
                {
                    return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TUSERDATA)
                {
                    return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                else if (luatype == LuaTypes.LUA_TNUMBER)
                {
                    return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
                //else // suppress CS0642
                ;    //an unsupported type was encountered
            }

            if (paramType.IsValueType && luatype == LuaTypes.LUA_TTABLE)
            {
                int          oldTop = LuaDLL.lua_gettop(luaState);
                ExtractValue ret    = null;
                LuaDLL.lua_pushvalue(luaState, stackPos);
                LuaDLL.lua_pushstring(luaState, "class");
                LuaDLL.lua_gettable(luaState, -2);

                if (!LuaDLL.lua_isnil(luaState, -1))
                {
                    string cls = LuaDLL.lua_tostring(luaState, -1);

                    if (cls == "Vector3" && paramType == typeof(Vector3))
                    {
                        ret = extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Vector2" && paramType == typeof(Vector2))
                    {
                        ret = extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Quaternion" && paramType == typeof(Quaternion))
                    {
                        ret = extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Color" && paramType == typeof(Color))
                    {
                        ret = extractValues[typeof(Color).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Vector4" && paramType == typeof(Vector4))
                    {
                        ret = extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()];
                    }
                    else if (cls == "Ray" && paramType == typeof(Ray))
                    {
                        ret = extractValues[typeof(Ray).TypeHandle.Value.ToInt64()];
                    }
                    else
                    {
                        ret = null;
                    }
                }

                LuaDLL.lua_settop(luaState, oldTop);

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

            if (LuaDLL.lua_isnumber(luaState, stackPos))
            {
                return(extractValues[runtimeHandleValue]);
            }

            if (paramType == typeof(bool))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    return(extractValues[runtimeHandleValue]);
                }
                else if (luatype == LuaTypes.LUA_TNIL)
                {
                    return(extractNetObject); // kevinh - silently convert nil to a null string pointer
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luatype == LuaTypes.LUA_TTABLE)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luatype == LuaTypes.LUA_TFUNCTION)
                {
                    return(extractValues[runtimeHandleValue]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION)
            {
#if __NOGEN__
                translator.throwError(luaState, "Delegates not implemnented");
#else
                return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated));
#endif
            }
            else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE)
            {
#if __NOGEN__
                translator.throwError(luaState, "Interfaces not implemnented");
#else
                return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated));
#endif
            }
            else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL)
            {
                // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found
                return(extractNetObject);
            }
            else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
            {
                if (LuaTypes.LUA_TNIL != LuaDLL.luaL_getmetafield(luaState, stackPos, "__index"))
                {
                    object obj = translator.getNetObject(luaState, -1);
                    LuaDLL.lua_settop(luaState, -2);
                    if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                    {
                        return(extractNetObject);
                    }
                }
            }
            else
            {
                //object obj = translator.getNetObject(luaState, stackPos);  //topameng 修改这里使支持注册到c#的lua类
                object obj = translator.getRawNetObject(luaState, stackPos);
                if (obj != null && paramType.IsAssignableFrom(obj.GetType()))
                {
                    return(extractNetObject);
                }
            }

            return(null);
        }
Exemple #7
0
        /// <summary>
        /// Tries to set a named property or field
        /// </summary>
        /// <param name="luaState"></param>
        /// <param name="targetType"></param>
        /// <param name="target"></param>
        /// <param name="bindingType"></param>
        /// <returns>false if unable to find the named member, true for success</returns>
        private bool trySetMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage)
        {
            detailMessage = null;   // No error yet

            // If not already a string just return - we don't want to call tostring - which has the side effect of
            // changing the lua typecode to string
            // Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to
            // be true for isstring.
            if (LuaDLL.lua_type(luaState, 2) != LuaTypes.LUA_TSTRING)
            {
                detailMessage = "property names must be strings";
                return(false);
            }

            // We only look up property names by string
            string fieldName = LuaDLL.lua_tostring(luaState, 2);

            if (fieldName == null || fieldName.Length < 1 || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_'))
            {
                detailMessage = "invalid property name";
                return(false);
            }

            // Find our member via reflection or the cache
            MemberInfo member = (MemberInfo)checkMemberCache(memberCache, targetType, fieldName);

            if (member == null)
            {
                //CP: Removed NonPublic binding search and made case insensitive
                MemberInfo[] members = targetType.GetMember(fieldName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/);
                if (members.Length > 0)
                {
                    member = members[0];
                    setMemberCache(memberCache, targetType, fieldName, member);
                }
                else
                {
                    detailMessage = "field or property '" + fieldName + "' does not exist";
                    return(false);
                }
            }

            if (member.MemberType == MemberTypes.Field)
            {
                FieldInfo field = (FieldInfo)member;
                object    val   = translator.getAsType(luaState, 3, field.FieldType);
                try
                {
                    field.SetValue(target, val);
                }
                catch (Exception e)
                {
                    ThrowError(luaState, e);
                }
                // We did a call
                return(true);
            }
            else if (member.MemberType == MemberTypes.Property)
            {
                PropertyInfo property = (PropertyInfo)member;
                object       val      = translator.getAsType(luaState, 3, property.PropertyType);
                try
                {
                    property.SetValue(target, val, null);
                }
                catch (Exception e)
                {
                    ThrowError(luaState, e);
                }
                // We did a call
                return(true);
            }

            detailMessage = "'" + fieldName + "' is not a .net field or property";
            return(false);
        }
Exemple #8
0
        /// <summary>
        /// TODO:打印的简易版本,还需要lua的文件名位置等信息
        /// </summary>
        /// <param name="L"></param>
        public static int Print(IntPtr L)
        {
            try
            {
                int n = LuaDLL.lua_gettop(L);
                // CString可以喝String互转
                using (CString.Block())
                {
                    CString sb = CString.Alloc(256);
#if UNITY_EDITOR
                    int    line     = LuaDLL.tolua_where(L, 1);
                    string filename = LuaDLL.lua_tostring(L, -1);
                    LuaDLL.lua_settop(L, n);
                    int offset = filename[0] == '@' ? 1 : 0;

                    if (!filename.Contains("."))
                    {
                        sb.Append('[').Append(filename, offset, filename.Length - offset).Append(".lua:").Append(line).Append("]:");
                    }
                    else
                    {
                        sb.Append('[').Append(filename, offset, filename.Length - offset).Append(':').Append(line).Append("]:");
                    }
#endif

                    for (int i = 1; i <= n; i++)
                    {
                        if (i > 1)
                        {
                            sb.Append("    ");
                        }

                        if (LuaDLL.lua_isstring(L, i) == 1)
                        {
                            sb.Append(LuaDLL.lua_tostring(L, i));
                        }
                        else if (LuaDLL.lua_isnil(L, i))
                        {
                            sb.Append("nil");
                        }
                        else if (LuaDLL.lua_isboolean(L, i))
                        {
                            sb.Append(LuaDLL.lua_toboolean(L, i) ? "true" : "false");
                        }
                        else
                        {
                            IntPtr p = LuaDLL.lua_topointer(L, i);

                            if (p == IntPtr.Zero)
                            {
                                sb.Append("nil");
                            }
                            else
                            {
                                sb.Append(LuaDLL.luaL_typename(L, i)).Append(":0x").Append(p.ToString("X"));
                            }
                        }
                    }

                    Debugger.Log(sb.ToString());            //203行与_line一致
                }
                return(0);
            }
            catch (Exception e)
            {
                return(LuaDLL.toluaL_exception(L, e));
            }
        }
Exemple #9
0
        static int Panic(IntPtr L)
        {
            string reason = String.Format("PANIC: unprotected error in call to Lua API ({0})", LuaDLL.lua_tostring(L, -1));

            throw new Exception(reason);
        }
Exemple #10
0
        static int PanicCallback(KopiLua.Lua.lua_State luaState)
        {
            // string desc = LuaDLL.lua_tostring(luaState, 1);

            // string desc = LuaDLL.lua_tostring(luaState, 1);
            string reason = String.Format("unprotected error in call to Lua API ({0})", LuaDLL.lua_tostring(luaState, -1));

            //        lua_tostring(L, -1);

            throw new LuaException(reason);
        }
        internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType)
        {
            LuaTypes luaTypes = LuaDLL.lua_type(luaState, stackPos);

            if (paramType.IsByRef)
            {
                paramType = paramType.GetElementType();
            }
            Type underlyingType = Nullable.GetUnderlyingType(paramType);

            if (underlyingType != null)
            {
                paramType = underlyingType;
            }
            long key = paramType.TypeHandle.Value.ToInt64();

            if (paramType.Equals(typeof(object)))
            {
                return(this.extractValues[key]);
            }
            if (paramType.IsGenericParameter)
            {
                if (luaTypes == LuaTypes.LUA_TBOOLEAN)
                {
                    return(this.extractValues[typeof(bool).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TSTRING)
                {
                    return(this.extractValues[typeof(string).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TTABLE)
                {
                    return(this.extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TUSERDATA)
                {
                    return(this.extractValues[typeof(object).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TFUNCTION)
                {
                    return(this.extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]);
                }
                if (luaTypes == LuaTypes.LUA_TNUMBER)
                {
                    return(this.extractValues[typeof(double).TypeHandle.Value.ToInt64()]);
                }
            }
            if (paramType.IsValueType && luaTypes == LuaTypes.LUA_TTABLE)
            {
                int          newTop       = LuaDLL.lua_gettop(luaState);
                ExtractValue extractValue = null;
                LuaDLL.lua_pushvalue(luaState, stackPos);
                LuaDLL.lua_pushstring(luaState, "class");
                LuaDLL.lua_gettable(luaState, -2);
                if (!LuaDLL.lua_isnil(luaState, -1))
                {
                    string a = LuaDLL.lua_tostring(luaState, -1);
                    if (a == "Vector3" && paramType == typeof(Vector3))
                    {
                        extractValue = this.extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Vector2" && paramType == typeof(Vector2))
                    {
                        extractValue = this.extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Quaternion" && paramType == typeof(Quaternion))
                    {
                        extractValue = this.extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Color" && paramType == typeof(Color))
                    {
                        extractValue = this.extractValues[typeof(Color).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Vector4" && paramType == typeof(Vector4))
                    {
                        extractValue = this.extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()];
                    }
                    else if (a == "Ray" && paramType == typeof(Ray))
                    {
                        extractValue = this.extractValues[typeof(Ray).TypeHandle.Value.ToInt64()];
                    }
                    else
                    {
                        extractValue = null;
                    }
                }
                LuaDLL.lua_settop(luaState, newTop);
                if (extractValue != null)
                {
                    return(extractValue);
                }
            }
            if (LuaDLL.lua_isnumber(luaState, stackPos))
            {
                return(this.extractValues[key]);
            }
            if (paramType == typeof(bool))
            {
                if (LuaDLL.lua_isboolean(luaState, stackPos))
                {
                    return(this.extractValues[key]);
                }
            }
            else if (paramType == typeof(string))
            {
                if (LuaDLL.lua_isstring(luaState, stackPos))
                {
                    return(this.extractValues[key]);
                }
                if (luaTypes == LuaTypes.LUA_TNIL)
                {
                    return(this.extractNetObject);
                }
            }
            else if (paramType == typeof(LuaTable))
            {
                if (luaTypes == LuaTypes.LUA_TTABLE)
                {
                    return(this.extractValues[key]);
                }
            }
            else if (paramType == typeof(LuaFunction))
            {
                if (luaTypes == LuaTypes.LUA_TFUNCTION)
                {
                    return(this.extractValues[key]);
                }
            }
            else if (typeof(Delegate).IsAssignableFrom(paramType) && luaTypes == LuaTypes.LUA_TFUNCTION)
            {
                this.translator.throwError(luaState, "Delegates not implemnented");
            }
            else if (paramType.IsInterface && luaTypes == LuaTypes.LUA_TTABLE)
            {
                this.translator.throwError(luaState, "Interfaces not implemnented");
            }
            else
            {
                if ((paramType.IsInterface || paramType.IsClass) && luaTypes == LuaTypes.LUA_TNIL)
                {
                    return(this.extractNetObject);
                }
                if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE)
                {
                    if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index") != LuaTypes.LUA_TNIL)
                    {
                        object netObject = this.translator.getNetObject(luaState, -1);
                        LuaDLL.lua_settop(luaState, -2);
                        if (netObject != null && paramType.IsAssignableFrom(netObject.GetType()))
                        {
                            return(this.extractNetObject);
                        }
                    }
                }
                else
                {
                    object rawNetObject = this.translator.getRawNetObject(luaState, stackPos);
                    if (rawNetObject != null && paramType.IsAssignableFrom(rawNetObject.GetType()))
                    {
                        return(this.extractNetObject);
                    }
                }
            }
            return(null);
        }
Exemple #12
0
        static int Print_Warning(IntPtr L)
        {
            try
            {
                int           n  = LuaDLL.lua_gettop(L);
                StringBuilder sb = StringBuilderCache.Acquire();
#if UNITY_EDITOR2
                int    line     = LuaDLL.tolua_where(L, 1);
                string filename = LuaDLL.lua_tostring(L, -1);
                LuaDLL.lua_settop(L, n);

                if (!filename.Contains("."))
                {
                    sb.AppendFormat("[{0}.lua:{1}]:", filename, line);
                }
                else
                {
                    sb.AppendFormat("[{0}:{1}]:", filename, line);
                }
#endif

                sb.Append("[");
                sb.Append(DateTime.Now.ToString("HH:mm:ss"));
                sb.Append("] ");

                for (int i = 1; i <= n; i++)
                {
                    if (i > 1)
                    {
                        sb.Append("    ");
                    }

                    if (LuaDLL.lua_isstring(L, i) == 1)
                    {
                        sb.Append(LuaDLL.lua_tostring(L, i));
                    }
                    else if (LuaDLL.lua_isnil(L, i))
                    {
                        sb.Append("nil");
                    }
                    else if (LuaDLL.lua_isboolean(L, i))
                    {
                        sb.Append(LuaDLL.lua_toboolean(L, i) ? "true" : "false");
                    }
                    else
                    {
                        IntPtr p = LuaDLL.lua_topointer(L, i);

                        if (p == IntPtr.Zero)
                        {
                            sb.Append("nil");
                        }
                        else
                        {
                            sb.AppendFormat("{0}:0x{1}", LuaDLL.luaL_typename(L, i), p.ToString("X"));
                        }
                    }
                }

                GameBase.Debugger.LogWarning_Fixed(StringBuilderCache.GetStringAndRelease(sb));
                return(0);
            }
            catch (Exception e)
            {
                return(LuaDLL.toluaL_exception(L, e));
            }
        }