Skip to content

Commit 5a45a40

Browse files
authored
Merge pull request EvilBeaver#1480 from Mr-Rm/v2/native-enums
Переделаны перечисления для работы в native
2 parents 72229c7 + bb550fa commit 5a45a40

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+279
-655
lines changed

src/OneScript.Core/TypeUtils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public static bool IsNumeric(this Type type)
1818
switch (Type.GetTypeCode(type))
1919
{
2020
case TypeCode.Int32:
21+
return !type.IsEnum;
2122
case TypeCode.Decimal:
2223
case TypeCode.UInt32:
2324
case TypeCode.Int64:

src/ScriptEngine/Machine/Contexts/ClrEnumValueWrapper.cs renamed to src/OneScript.Core/Values/ClrEnumValueWrapper.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,25 @@ This Source Code Form is subject to the terms of the
66
----------------------------------------------------------*/
77

88
using OneScript.Commons;
9+
using OneScript.Types;
910

10-
namespace ScriptEngine.Machine.Contexts
11+
namespace OneScript.Values
1112
{
1213
public class ClrEnumValueWrapper<T> : EnumerationValue, IObjectWrapper where T :struct
1314
{
1415
private readonly T _realValue;
1516

16-
public ClrEnumValueWrapper(EnumerationContext owner, T realValue):base(owner)
17-
{
17+
public ClrEnumValueWrapper(TypeDescriptor systemType, T realValue, string name, string alias)
18+
: base (systemType, name, alias)
19+
{
1820
_realValue = realValue;
1921
}
2022

21-
public object UnderlyingObject
22-
{
23-
get
24-
{
25-
return _realValue;
26-
}
27-
}
23+
public object UnderlyingObject => _realValue;
2824

29-
public T UnderlyingValue
30-
{
31-
get
32-
{
33-
return _realValue;
34-
}
35-
}
25+
public T UnderlyingValue => _realValue;
3626

37-
public override bool Equals(IValue other)
27+
public override bool Equals(BslValue other)
3828
{
3929
if (!(other?.GetRawValue() is ClrEnumValueWrapper<T> otherWrapper))
4030
return false;
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
using OneScript.Commons;
9+
using OneScript.Exceptions;
10+
using OneScript.Localization;
11+
using OneScript.Types;
12+
using System;
13+
using ScriptEngine.Machine;
14+
15+
namespace OneScript.Values
16+
{
17+
public abstract class EnumerationValue : BslValue
18+
{
19+
readonly TypeDescriptor _systemType;
20+
readonly string _name, _alias;
21+
22+
public EnumerationValue(TypeDescriptor systemType, string name, string alias)
23+
{
24+
if (!Utils.IsValidIdentifier(name))
25+
throw new ArgumentException("Name must be a valid identifier", "name");
26+
27+
if(alias != null && !Utils.IsValidIdentifier(alias))
28+
throw new ArgumentException("Name must be a valid identifier", "alias");
29+
30+
_systemType = systemType;
31+
_name = name;
32+
_alias = alias;
33+
}
34+
35+
public string Name => _name;
36+
public string Alias => _alias;
37+
38+
public bool IsFilled() => true;
39+
40+
public override TypeDescriptor SystemType => _systemType;
41+
42+
public override string ToString()
43+
{
44+
return BilingualString.Localize(_name, _alias);
45+
}
46+
47+
public override IValue GetRawValue() => this;
48+
49+
public override int CompareTo(BslValue other)
50+
{
51+
throw RuntimeException.ComparisonNotSupportedException();
52+
}
53+
54+
public override bool Equals(BslValue other)
55+
{
56+
return ReferenceEquals(other?.GetRawValue(), this);
57+
}
58+
}
59+
}

src/OneScript.Native/Compiler/ExpressionHelpers.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,24 @@ private static Expression TryFindConversionOp(Expression value, Type targetType)
267267

268268
return DowncastDecimal(decimalNum, targetType);
269269
}
270+
else if (targetType.IsEnum)
271+
{
272+
Type generic = typeof(ClrEnumValueWrapper<>);
273+
var wrapperType = generic.MakeGenericType(new[]{targetType});
274+
try
275+
{
276+
var wrapper = Expression.Convert(value, wrapperType);
277+
return Expression.Property(wrapper,"UnderlyingValue");
278+
}
279+
catch (InvalidOperationException)
280+
{
281+
throw new NativeCompilerException(
282+
BilingualString.Localize(
283+
$"Преобразование {value.Type} в тип {targetType} недоступно",
284+
$"Conversion from {value.Type} to {targetType} is unavailable")
285+
);
286+
}
287+
}
270288
else
271289
{
272290
var conversion = TryConvertBslValueToPrimitiveType(value, targetType);

src/OneScript.StandardLibrary/Binary/FileStreamContext.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ This Source Code Form is subject to the terms of the
88
using System;
99
using System.IO;
1010
using OneScript.Contexts;
11+
using OneScript.Exceptions;
1112
using OneScript.Types;
13+
using OneScript.Values;
1214
using ScriptEngine.Machine;
1315
using ScriptEngine.Machine.Contexts;
1416

@@ -301,30 +303,34 @@ public Stream GetUnderlyingStream()
301303
}
302304

303305
[ScriptConstructor(Name = "С указанием режима открытия")]
304-
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue bufferSize = null)
306+
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue param3 = null)
305307
{
306-
if (bufferSize == null || bufferSize.SystemType == BasicTypes.Number)
308+
if (param3 == null || param3.SystemType == BasicTypes.Number)
307309
{
308310
return new FileStreamContext(
309311
filename.AsString(),
310312
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
311313
FileAccessEnum.ReadAndWrite,
312-
ContextValuesMarshaller.ConvertParam<int>(bufferSize));
314+
ContextValuesMarshaller.ConvertParam<int>(param3));
313315
}
314316
else
315317
{
316-
// перегрузка методов не позволяет вызвать второй конструктор без доуточнения реальных типов
317-
return Constructor(
318-
filename,
319-
openMode,
320-
new ClrEnumValueWrapper<FileAccessEnum>(null, FileAccessEnum.ReadAndWrite),
321-
bufferSize);
318+
if (param3 is ClrEnumValueWrapper<FileAccessEnum> access)
319+
return new FileStreamContext(
320+
filename.AsString(),
321+
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
322+
ContextValuesMarshaller.ConvertParam<FileAccessEnum>(access));
323+
else
324+
throw RuntimeException.InvalidNthArgumentType(3);
322325
}
323326
}
324327

325328
[ScriptConstructor(Name = "С указанием режима открытия и уровня доступа")]
326329
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue access, IValue bufferSize = null)
327330
{
331+
if ( bufferSize != null && bufferSize.SystemType != BasicTypes.Number)
332+
throw RuntimeException.InvalidNthArgumentType(4);
333+
328334
return new FileStreamContext(
329335
filename.AsString(),
330336
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),

src/OneScript.StandardLibrary/DriveInfo/DriveTypeEnum.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ This Source Code Form is subject to the terms of the
77

88
using OneScript.Contexts.Enums;
99
using OneScript.Types;
10+
using OneScript.Values;
1011
using ScriptEngine.Machine.Contexts;
1112

1213
namespace OneScript.StandardLibrary.DriveInfo
@@ -24,31 +25,22 @@ namespace OneScript.StandardLibrary.DriveInfo
2425
[SystemEnum("ТипДиска", "DriveType")]
2526
public class DriveTypeEnum : EnumerationContext
2627
{
27-
2828
private DriveTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
2929
: base(typeRepresentation, valuesType)
3030
{
31-
31+
this.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
32+
this.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
33+
this.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
34+
this.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
35+
this.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
36+
this.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
37+
this.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);
3238
}
3339

3440
public static DriveTypeEnum CreateInstance(ITypeManager typeManager)
3541
{
36-
37-
var instance = EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
38-
typeManager,
39-
(t, v) => new DriveTypeEnum(t, v));
40-
41-
instance.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
42-
instance.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
43-
instance.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
44-
instance.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
45-
instance.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
46-
instance.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
47-
instance.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);
48-
49-
return instance;
42+
return EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
43+
typeManager, (t, v) => new DriveTypeEnum(t, v));
5044
}
51-
5245
}
53-
5446
}

src/OneScript.StandardLibrary/Json/JSONWriterSettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This Source Code Form is subject to the terms of the
66
----------------------------------------------------------*/
77

88
using OneScript.Contexts;
9+
using OneScript.Values;
910
using ScriptEngine.Machine;
1011
using ScriptEngine.Machine.Contexts;
1112

src/OneScript.StandardLibrary/SystemEnvironmentContext.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This Source Code Form is subject to the terms of the
1212
using OneScript.Contexts;
1313
using OneScript.Exceptions;
1414
using OneScript.StandardLibrary.Collections;
15+
using OneScript.Values;
1516
using ScriptEngine;
1617
using ScriptEngine.HostedScript.Library;
1718
using ScriptEngine.Machine;

src/OneScript.StandardLibrary/Tasks/BackgroundTasksManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ This Source Code Form is subject to the terms of the
1515
using OneScript.Exceptions;
1616
using OneScript.StandardLibrary.Collections;
1717
using OneScript.Types;
18+
using OneScript.Values;
1819
using ScriptEngine.Machine;
1920
using ScriptEngine.Machine.Contexts;
2021
using ExecutionContext = ScriptEngine.Machine.ExecutionContext;

src/OneScript.StandardLibrary/Text/ConsoleColorEnum.cs

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This Source Code Form is subject to the terms of the
88
using System;
99
using OneScript.Contexts.Enums;
1010
using OneScript.Types;
11+
using OneScript.Values;
1112
using ScriptEngine.Machine.Contexts;
1213

1314
namespace OneScript.StandardLibrary.Text
@@ -18,35 +19,28 @@ public class ConsoleColorEnum : ClrEnumWrapper<ConsoleColor>
1819
private ConsoleColorEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
1920
: base(typeRepresentation, valuesType)
2021
{
22+
this.WrapClrValue("Черный", "Black", ConsoleColor.Black);
23+
this.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
24+
this.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
25+
this.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
26+
this.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
27+
this.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
28+
this.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
29+
this.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);
30+
31+
this.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
32+
this.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
33+
this.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
34+
this.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
35+
this.WrapClrValue("Красный", "Red", ConsoleColor.Red);
36+
this.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
37+
this.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
38+
this.WrapClrValue("Белый", "White", ConsoleColor.White);
2139
}
2240

2341
public static ConsoleColorEnum CreateInstance(ITypeManager typeManager)
2442
{
25-
var instance = EnumContextHelper.CreateClrEnumInstance<ConsoleColorEnum, ConsoleColor>(
26-
typeManager,
27-
(t,v) => new ConsoleColorEnum(t,v));
28-
29-
instance.WrapClrValue("Черный", "Black", ConsoleColor.Black);
30-
instance.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
31-
instance.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
32-
instance.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
33-
instance.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
34-
instance.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
35-
instance.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
36-
instance.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);
37-
38-
instance.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
39-
instance.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
40-
instance.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
41-
instance.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
42-
instance.WrapClrValue("Красный", "Red", ConsoleColor.Red);
43-
instance.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
44-
instance.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
45-
instance.WrapClrValue("Белый", "White", ConsoleColor.White);
46-
47-
OnInstanceCreation(instance);
48-
49-
return instance;
43+
return CreateInstance(typeManager, (t, v) => new ConsoleColorEnum(t, v));
5044
}
5145
}
5246
}

0 commit comments

Comments
 (0)