Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1419: проверка типов перечислений при передаче параметров и присваивании #1421

Merged
merged 5 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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