Skip to content
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
1 change: 1 addition & 0 deletions src/OneScript.Core/TypeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static bool IsNumeric(this Type type)
switch (Type.GetTypeCode(type))
{
case TypeCode.Int32:
return !type.IsEnum;
case TypeCode.Decimal:
case TypeCode.UInt32:
case TypeCode.Int64:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,25 @@ This Source Code Form is subject to the terms of the
----------------------------------------------------------*/

using OneScript.Commons;
using OneScript.Types;

namespace ScriptEngine.Machine.Contexts
namespace OneScript.Values
{
public class ClrEnumValueWrapper<T> : EnumerationValue, IObjectWrapper where T :struct
{
private readonly T _realValue;

public ClrEnumValueWrapper(EnumerationContext owner, T realValue):base(owner)
{
public ClrEnumValueWrapper(TypeDescriptor systemType, T realValue, string name, string alias)
: base (systemType, name, alias)
{
_realValue = realValue;
}

public object UnderlyingObject
{
get
{
return _realValue;
}
}
public object UnderlyingObject => _realValue;

public T UnderlyingValue
{
get
{
return _realValue;
}
}
public T UnderlyingValue => _realValue;

public override bool Equals(IValue other)
public override bool Equals(BslValue other)
{
if (!(other?.GetRawValue() is ClrEnumValueWrapper<T> otherWrapper))
return false;
Expand Down
59 changes: 59 additions & 0 deletions src/OneScript.Core/Values/EnumerationValue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/

using OneScript.Commons;
using OneScript.Exceptions;
using OneScript.Localization;
using OneScript.Types;
using System;
using ScriptEngine.Machine;

namespace OneScript.Values
{
public abstract class EnumerationValue : BslValue
{
readonly TypeDescriptor _systemType;
readonly string _name, _alias;

public EnumerationValue(TypeDescriptor systemType, string name, string alias)
{
if (!Utils.IsValidIdentifier(name))
throw new ArgumentException("Name must be a valid identifier", "name");

if(alias != null && !Utils.IsValidIdentifier(alias))
throw new ArgumentException("Name must be a valid identifier", "alias");

_systemType = systemType;
_name = name;
_alias = alias;
}

public string Name => _name;
public string Alias => _alias;

public bool IsFilled() => true;

public override TypeDescriptor SystemType => _systemType;

public override string ToString()
{
return BilingualString.Localize(_name, _alias);
}

public override IValue GetRawValue() => this;

public override int CompareTo(BslValue other)
{
throw RuntimeException.ComparisonNotSupportedException();
}

public override bool Equals(BslValue other)
{
return ReferenceEquals(other?.GetRawValue(), this);
}
}
}
18 changes: 18 additions & 0 deletions src/OneScript.Native/Compiler/ExpressionHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,24 @@ private static Expression TryFindConversionOp(Expression value, Type targetType)

return DowncastDecimal(decimalNum, targetType);
}
else if (targetType.IsEnum)
{
Type generic = typeof(ClrEnumValueWrapper<>);
var wrapperType = generic.MakeGenericType(new[]{targetType});
try
{
var wrapper = Expression.Convert(value, wrapperType);
return Expression.Property(wrapper,"UnderlyingValue");
}
catch (InvalidOperationException)
{
throw new NativeCompilerException(
BilingualString.Localize(
$"Преобразование {value.Type} в тип {targetType} недоступно",
$"Conversion from {value.Type} to {targetType} is unavailable")
);
}
}
else
{
var conversion = TryConvertBslValueToPrimitiveType(value, targetType);
Expand Down
24 changes: 15 additions & 9 deletions src/OneScript.StandardLibrary/Binary/FileStreamContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ This Source Code Form is subject to the terms of the
using System;
using System.IO;
using OneScript.Contexts;
using OneScript.Exceptions;
using OneScript.Types;
using OneScript.Values;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;

Expand Down Expand Up @@ -301,30 +303,34 @@ public Stream GetUnderlyingStream()
}

[ScriptConstructor(Name = "С указанием режима открытия")]
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue bufferSize = null)
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue param3 = null)
{
if (bufferSize == null || bufferSize.SystemType == BasicTypes.Number)
if (param3 == null || param3.SystemType == BasicTypes.Number)
{
return new FileStreamContext(
filename.AsString(),
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
FileAccessEnum.ReadAndWrite,
ContextValuesMarshaller.ConvertParam<int>(bufferSize));
ContextValuesMarshaller.ConvertParam<int>(param3));
}
else
{
// перегрузка методов не позволяет вызвать второй конструктор без доуточнения реальных типов
return Constructor(
filename,
openMode,
new ClrEnumValueWrapper<FileAccessEnum>(null, FileAccessEnum.ReadAndWrite),
bufferSize);
if (param3 is ClrEnumValueWrapper<FileAccessEnum> access)
return new FileStreamContext(
filename.AsString(),
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
ContextValuesMarshaller.ConvertParam<FileAccessEnum>(access));
else
throw RuntimeException.InvalidNthArgumentType(3);
}
}

[ScriptConstructor(Name = "С указанием режима открытия и уровня доступа")]
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue access, IValue bufferSize = null)
{
if ( bufferSize != null && bufferSize.SystemType != BasicTypes.Number)
throw RuntimeException.InvalidNthArgumentType(4);

return new FileStreamContext(
filename.AsString(),
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
Expand Down
28 changes: 10 additions & 18 deletions src/OneScript.StandardLibrary/DriveInfo/DriveTypeEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ This Source Code Form is subject to the terms of the

using OneScript.Contexts.Enums;
using OneScript.Types;
using OneScript.Values;
using ScriptEngine.Machine.Contexts;

namespace OneScript.StandardLibrary.DriveInfo
Expand All @@ -24,31 +25,22 @@ namespace OneScript.StandardLibrary.DriveInfo
[SystemEnum("ТипДиска", "DriveType")]
public class DriveTypeEnum : EnumerationContext
{

private DriveTypeEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
{

this.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
this.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
this.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
this.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
this.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
this.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
this.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);
}

public static DriveTypeEnum CreateInstance(ITypeManager typeManager)
{

var instance = EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
typeManager,
(t, v) => new DriveTypeEnum(t, v));

instance.WrapClrValue("Неизвестный", "Unknown", System.IO.DriveType.Unknown);
instance.WrapClrValue("НеИмеетКорневойКаталог", "NoRootDirectory", System.IO.DriveType.NoRootDirectory);
instance.WrapClrValue("СъемноеЗапоминающееУстройство", "Removable", System.IO.DriveType.Removable);
instance.WrapClrValue("ЖесткийДиск", "Fixed", System.IO.DriveType.Fixed);
instance.WrapClrValue("СетевойДиск", "Network", System.IO.DriveType.Network);
instance.WrapClrValue("ОптическийДиск", "CDRom", System.IO.DriveType.CDRom);
instance.WrapClrValue("ДискОЗУ", "Ram", System.IO.DriveType.Ram);

return instance;
return EnumContextHelper.CreateClrEnumInstance<DriveTypeEnum, System.IO.DriveType>(
typeManager, (t, v) => new DriveTypeEnum(t, v));
}

}

}
1 change: 1 addition & 0 deletions src/OneScript.StandardLibrary/Json/JSONWriterSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This Source Code Form is subject to the terms of the
----------------------------------------------------------*/

using OneScript.Contexts;
using OneScript.Values;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;

Expand Down
1 change: 1 addition & 0 deletions src/OneScript.StandardLibrary/SystemEnvironmentContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This Source Code Form is subject to the terms of the
using OneScript.Contexts;
using OneScript.Exceptions;
using OneScript.StandardLibrary.Collections;
using OneScript.Values;
using ScriptEngine;
using ScriptEngine.HostedScript.Library;
using ScriptEngine.Machine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This Source Code Form is subject to the terms of the
using OneScript.Exceptions;
using OneScript.StandardLibrary.Collections;
using OneScript.Types;
using OneScript.Values;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using ExecutionContext = ScriptEngine.Machine.ExecutionContext;
Expand Down
44 changes: 19 additions & 25 deletions src/OneScript.StandardLibrary/Text/ConsoleColorEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This Source Code Form is subject to the terms of the
using System;
using OneScript.Contexts.Enums;
using OneScript.Types;
using OneScript.Values;
using ScriptEngine.Machine.Contexts;

namespace OneScript.StandardLibrary.Text
Expand All @@ -18,35 +19,28 @@ public class ConsoleColorEnum : ClrEnumWrapper<ConsoleColor>
private ConsoleColorEnum(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
: base(typeRepresentation, valuesType)
{
this.WrapClrValue("Черный", "Black", ConsoleColor.Black);
this.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
this.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
this.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
this.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
this.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
this.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
this.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);

this.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
this.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
this.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
this.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
this.WrapClrValue("Красный", "Red", ConsoleColor.Red);
this.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
this.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
this.WrapClrValue("Белый", "White", ConsoleColor.White);
}

public static ConsoleColorEnum CreateInstance(ITypeManager typeManager)
{
var instance = EnumContextHelper.CreateClrEnumInstance<ConsoleColorEnum, ConsoleColor>(
typeManager,
(t,v) => new ConsoleColorEnum(t,v));

instance.WrapClrValue("Черный", "Black", ConsoleColor.Black);
instance.WrapClrValue("ТемноСиний", "DarkBlue", ConsoleColor.DarkBlue);
instance.WrapClrValue("ТемноЗеленый", "DarkGreen", ConsoleColor.DarkGreen);
instance.WrapClrValue("ТемноБирюзовый", "DarkCyan", ConsoleColor.DarkCyan);
instance.WrapClrValue("ТемноКрасный", "DarkRed", ConsoleColor.DarkRed);
instance.WrapClrValue("ТемноМалиновый", "DarkMagenta", ConsoleColor.DarkMagenta);
instance.WrapClrValue("ТемноЖелтый", "DarkYellow", ConsoleColor.DarkYellow);
instance.WrapClrValue("Серый", "Gray", ConsoleColor.Gray);

instance.WrapClrValue("ТемноСерый", "DarkGray", ConsoleColor.DarkGray);
instance.WrapClrValue("Синий", "Blue", ConsoleColor.Blue);
instance.WrapClrValue("Зеленый", "Green", ConsoleColor.Green);
instance.WrapClrValue("Бирюза", "Cyan", ConsoleColor.Cyan);
instance.WrapClrValue("Красный", "Red", ConsoleColor.Red);
instance.WrapClrValue("Малиновый", "Magenta", ConsoleColor.Magenta);
instance.WrapClrValue("Желтый", "Yellow", ConsoleColor.Yellow);
instance.WrapClrValue("Белый", "White", ConsoleColor.White);

OnInstanceCreation(instance);

return instance;
return CreateInstance(typeManager, (t, v) => new ConsoleColorEnum(t, v));
}
}
}
1 change: 1 addition & 0 deletions src/OneScript.StandardLibrary/Text/ConsoleContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This Source Code Form is subject to the terms of the
using OneScript.Contexts;
using OneScript.Exceptions;
using OneScript.StandardLibrary.Binary;
using OneScript.Values;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;

Expand Down
3 changes: 2 additions & 1 deletion src/OneScript.StandardLibrary/Text/TextEncodingEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This Source Code Form is subject to the terms of the
using OneScript.Types;
using ScriptEngine.Machine;
using ScriptEngine.Machine.Contexts;
using OneScript.Values;

namespace OneScript.StandardLibrary.Text
{
Expand Down Expand Up @@ -141,7 +142,7 @@ public static Encoding GetEncoding(IValue encoding, bool addBOM = true)
if (!(encoding.GetRawValue() is ClrEnumValueWrapper<TextEncodingValues> encValue))
throw RuntimeException.InvalidArgumentType();

var encodingEnum = (TextEncodingEnum)encValue.Owner;
var encodingEnum = GlobalsHelper.GetEnum<TextEncodingEnum>();

Encoding enc;
if (encValue == encodingEnum.Ansi)
Expand Down
7 changes: 2 additions & 5 deletions src/OneScript.StandardLibrary/XDTO/XDTOSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ namespace OneScript.StandardLibrary.XDTO
public sealed class XDTOSerializer : AutoContext<XDTOSerializer>
{
private readonly XmlGlobalFunctions _xmlGlobalFunctions;
private readonly XmlNodeTypeEnum _xmlNodeEnum;


private XDTOSerializer(IGlobalsManager globalsManager)
{
_xmlGlobalFunctions = globalsManager.GetInstance<XmlGlobalFunctions>();
_xmlNodeEnum = globalsManager.GetInstance<XmlNodeTypeEnum>();
}

private void WriteXMLSimpleData(XmlWriterImpl xmlWriter,
Expand Down Expand Up @@ -190,7 +187,7 @@ public IValue ReadXML(XmlReaderImpl xmlReader, IValue valueType = null)
if (valueType is BslTypeValue typeTypeValue)
typeValue = typeTypeValue;

else if (xmlReader.NodeType.Equals(_xmlNodeEnum.FromNativeValue(XmlNodeType.Element)))
else if (xmlReader.NodeType.Equals(XmlNodeTypeEnum.FromNativeValue(XmlNodeType.Element)))
{
IValue xsiType = xmlReader.GetAttribute(ValueFactory.Create("type"), XmlSchema.InstanceNamespace);
IValue xsiNil = xmlReader.GetAttribute(ValueFactory.Create("nil"), XmlSchema.InstanceNamespace);
Expand Down Expand Up @@ -238,7 +235,7 @@ public IValue ReadXML(XmlReaderImpl xmlReader, IValue valueType = null)
else if (typeof(BslPrimitiveValue).IsAssignableFrom(implType))
{
xmlReader.Read();
if (xmlReader.NodeType.Equals(_xmlNodeEnum.FromNativeValue(XmlNodeType.Text)))
if (xmlReader.NodeType.Equals(XmlNodeTypeEnum.FromNativeValue(XmlNodeType.Text)))
{
result = XMLValue(typeValue, xmlReader.Value);
xmlReader.Read();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class XSComplexFinalUnion : AutoContext<XSComplexFinalUnion>

private bool Contains(XmlSchemaDerivationMethod value)
{
XSComplexFinal enumValue = XSComplexFinal.FromNativeValue(value);
var enumValue = EnumerationXSComplexFinal.FromNativeValue(value);
IValue idx = _values.Find(enumValue);
return idx.SystemType == BasicTypes.Number;
}
Expand Down
Loading