Skip to content

Commit

Permalink
Merge pull request #1421 from Mr-Rm/v2/fix-1419
Browse files Browse the repository at this point in the history
 fix #1419: проверка типов перечислений при передаче параметров и присваивании
  • Loading branch information
EvilBeaver authored Jul 4, 2024
2 parents 0b0a505 + f04ef24 commit 9ce002f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 40 deletions.
7 changes: 7 additions & 0 deletions src/OneScript.Core/Exceptions/TypeConversionException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ public class TypeConversionException : RuntimeException
public TypeConversionException(BilingualString message) : base(message)
{
}

public TypeConversionException()
: base(new BilingualString(
"Несоответствие типов",
"Type mismatch"))
{
}
}
}
52 changes: 12 additions & 40 deletions src/OneScript.StandardLibrary/Text/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,20 @@ namespace OneScript.StandardLibrary.Text
public class ConsoleContext : AutoContext<ConsoleContext>
{
[ContextProperty("НажатаКлавиша", "KeyPressed")]
public bool HasKey
{
get
{
return Console.KeyAvailable;
}
}
public bool HasKey => Console.KeyAvailable;

[ContextProperty("КурсорЛево", "CursorLeft")]
public int XPos
{
get
{
return Console.CursorLeft;
}
set
{
Console.CursorLeft = Math.Min(value, Console.WindowWidth-1);
}
get => Console.CursorLeft;
set => Console.CursorLeft = Math.Min(value, Console.WindowWidth-1);
}

[ContextProperty("КурсорВерх", "CursorTop")]
public int YPos
{
get
{
return Console.CursorTop;
}
set
{
Console.CursorTop = Math.Min(value, Console.WindowHeight-1);
}
get => Console.CursorTop;
set => Console.CursorTop = Math.Min(value, Console.WindowHeight-1);
}

[ContextMethod("ПрочитатьСтроку", "ReadLine")]
Expand Down Expand Up @@ -89,22 +71,10 @@ public void Write(string text)
}

[ContextProperty("Ширина", "Width")]
public int Width
{
get
{
return Console.WindowWidth;
}
}
public int Width => Console.WindowWidth;

[ContextProperty("Высота", "Height")]
public int Высота
{
get
{
return Console.WindowHeight;
}
}
public int Height => Console.WindowHeight;

[ContextMethod("ВидимостьКурсора", "CursorVisible")]
public bool CursorVisible(bool visible)
Expand Down Expand Up @@ -134,6 +104,8 @@ public IValue TextColor
{
Console.ForegroundColor = typed.UnderlyingValue;
}
else
throw new TypeConversionException();
}
}

Expand All @@ -144,7 +116,7 @@ public IValue BackgroundColor
{
try
{
return (ClrEnumValueWrapper<ConsoleColor>)GlobalsHelper.GetEnum<ConsoleColorEnum>().FromNativeValue(Console.BackgroundColor);
return GlobalsHelper.GetEnum<ConsoleColorEnum>().FromNativeValue(Console.BackgroundColor);
}
catch (InvalidOperationException)
{
Expand All @@ -157,6 +129,8 @@ public IValue BackgroundColor
{
Console.BackgroundColor = typed.UnderlyingValue;
}
else
throw new TypeConversionException();
}
}

Expand Down Expand Up @@ -270,6 +244,4 @@ public void SetError(IValue target)
Console.SetError(writer);
}
}


}
11 changes: 11 additions & 0 deletions src/ScriptEngine/Machine/Contexts/ContextValuesMarshaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ private static object ConvertValueType(IValue value, Type type)
}
else
{
var rawValue = value.GetRawValue();
if (rawValue is IObjectWrapper wrapped)
{
if (!type.IsInstanceOfType(wrapped.UnderlyingObject))
throw new InvalidCastException();
}
else if (!type.IsInstanceOfType(rawValue))
{
throw new InvalidCastException();
}

valueObj = CastToClrObject(value);
}

Expand Down

0 comments on commit 9ce002f

Please sign in to comment.