/// <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> public T CheckUserDataType <T>(string funcName, int argNum = -1, TypeValidationFlags flags = TypeValidationFlags.Default) { var v = this.CheckType(funcName, DataType.UserData, argNum, flags); bool allowNil = ((int)(flags & TypeValidationFlags.AllowNil) != 0); if (v.IsNil()) { return(default);
/// <summary> /// Checks the type of this value corresponds to the desired type. A property 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> /// <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(NewBoolean(this.CastToBool())); } if (desiredType == DataType.Number) { var v = this.CastToNumber(); if (v.HasValue) { return(NewNumber(v.Value)); } } if (desiredType == DataType.String) { string v = this.CastToString(); if (v != null) { return(NewString(v)); } } } if (this.IsVoid()) { throw ScriptRuntimeException.BadArgumentNoValue(argNum, funcName, desiredType); } throw ScriptRuntimeException.BadArgument(argNum, funcName, desiredType, this.Type, allowNil); }
/// <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); }
/// <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); }
public static IEnumerable <T> GetArrayUserData <T>(this Table table, string funcName, TypeValidationFlags flags = TypeValidationFlags.AutoConvert) { return(table.GetArray().Select(x => x.CheckUserDataType <T>(funcName, flags: flags))); }
public static IEnumerable <string> GetArrayString(this Table table, string funcName, TypeValidationFlags flags = TypeValidationFlags.AutoConvert) { return(table.GetArrayTyped(DataType.String, funcName, flags).Select(x => x.String)); }
public static IEnumerable <DynValue> GetArrayTyped(this Table table, DataType type, string funcName, TypeValidationFlags flags = TypeValidationFlags.AutoConvert) { return(table.GetArray().Select(x => x.CheckType(funcName, type, flags: flags))); }