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(); }
/// <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); }