Esempio n. 1
0
        /// <summary>
        /// Checks if the type is a specific userdata type, and returns it or throws.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="funcName">Name of the function.</param>
        /// <param name="argNum">The argument number.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        public T CheckUserDataType <T>(string funcName, int argNum = -1, TypeValidationFlags flags = TypeValidationFlags.Default)
        {
            DynValue v        = this.CheckType(funcName, DataType.UserData, argNum, flags);
            bool     allowNil = ((int)(flags & TypeValidationFlags.AllowNil) != 0);

            if (v.IsNil())
            {
                return(default(T));
            }

            T t;

            if (v.UserData.TryGet <T>(out t))
            {
                return(t);
            }

            throw ScriptRuntimeException.BadArgumentUserData(argNum, funcName, typeof(T), t, allowNil);
        }
Esempio n. 2
0
        /// <summary>
        /// Checks if the type is a specific userdata type, and returns it or throws.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="funcName">Name of the function.</param>
        /// <param name="argNum">The argument number.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        public T CheckUserDataType <T>(string funcName, int argNum = -1, TypeValidationFlags flags = TypeValidationFlags.Default)
        {
            DynValue v        = this.CheckType(funcName, DataType.UserData, argNum, flags);
            bool     allowNil = ((int)(flags & TypeValidationFlags.AllowNil) != 0);

            if (v.IsNil())
            {
                return(default(T));
            }

            object o = v.UserData.Object;

            if (o != null && o is T)
            {
                return((T)o);
            }

            throw ScriptRuntimeException.BadArgumentUserData(argNum, funcName, typeof(T), o, allowNil);
        }