Skip to content

Commit bf65908

Browse files
committed
значения перечислений отвязаны от контекста; контроль типов в ФайловыйПоток
1 parent 7d7ff0a commit bf65908

File tree

7 files changed

+59
-83
lines changed

7 files changed

+59
-83
lines changed

src/OneScript.StandardLibrary/Binary/FileStreamContext.cs

Lines changed: 14 additions & 9 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 System.IO;
1010
using OneScript.Contexts;
11+
using OneScript.Exceptions;
1112
using OneScript.Types;
1213
using ScriptEngine.Machine;
1314
using ScriptEngine.Machine.Contexts;
@@ -301,30 +302,34 @@ public Stream GetUnderlyingStream()
301302
}
302303

303304
[ScriptConstructor(Name = "С указанием режима открытия")]
304-
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue bufferSize = null)
305+
public static FileStreamContext Constructor(IValue filename, IValue openMode, IValue param3 = null)
305306
{
306-
if (bufferSize == null || bufferSize.SystemType == BasicTypes.Number)
307+
if (param3 == null || param3.SystemType == BasicTypes.Number)
307308
{
308309
return new FileStreamContext(
309310
filename.AsString(),
310311
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
311312
FileAccessEnum.ReadAndWrite,
312-
ContextValuesMarshaller.ConvertParam<int>(bufferSize));
313+
ContextValuesMarshaller.ConvertParam<int>(param3));
313314
}
314315
else
315316
{
316-
// перегрузка методов не позволяет вызвать второй конструктор без доуточнения реальных типов
317-
return Constructor(
318-
filename,
319-
openMode,
320-
new ClrEnumValueWrapper<FileAccessEnum>(null, FileAccessEnum.ReadAndWrite),
321-
bufferSize);
317+
if (param3 is ClrEnumValueWrapper<FileAccessEnum> access)
318+
return new FileStreamContext(
319+
filename.AsString(),
320+
ContextValuesMarshaller.ConvertParam<FileOpenModeEnum>(openMode),
321+
ContextValuesMarshaller.ConvertParam<FileAccessEnum>(access));
322+
else
323+
throw RuntimeException.InvalidNthArgumentType(3);
322324
}
323325
}
324326

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

src/OneScript.StandardLibrary/Text/TextEncodingEnum.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public static Encoding GetEncoding(IValue encoding, bool addBOM = true)
141141
if (!(encoding.GetRawValue() is ClrEnumValueWrapper<TextEncodingValues> encValue))
142142
throw RuntimeException.InvalidArgumentType();
143143

144-
var encodingEnum = (TextEncodingEnum)encValue.Owner;
144+
var encodingEnum = GlobalsHelper.GetEnum<TextEncodingEnum>();
145145

146146
Encoding enc;
147147
if (encValue == encodingEnum.Ansi)

src/ScriptEngine/Machine/Contexts/ClrEnumValueWrapper.cs

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

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

1011
namespace ScriptEngine.Machine.Contexts
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

3727
public override bool Equals(IValue other)
3828
{

src/ScriptEngine/Machine/Contexts/ClrEnumWrapper.cs

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

88
using System;
99
using System.Collections.Generic;
10+
using System.Runtime.CompilerServices;
11+
using OneScript.Commons;
1012
using OneScript.Contexts.Enums;
1113
using OneScript.Types;
1214

@@ -46,8 +48,8 @@ public virtual ClrEnumValueWrapper<T> FromNativeValue(T native)
4648

4749
throw new InvalidOperationException($"Item '{native}' not found");
4850
}
49-
50-
private void Autoregister()
51+
52+
private void Autoregister(TypeDescriptor valuesType)
5153
{
5254
var attrib = typeof(T).GetCustomAttributes(typeof(EnumerationTypeAttribute), false);
5355
if(attrib.Length == 0)
@@ -60,27 +62,28 @@ private void Autoregister()
6062
foreach (var contextFieldAttribute in field.GetCustomAttributes (typeof (EnumValueAttribute), false))
6163
{
6264
var contextField = (EnumValueAttribute)contextFieldAttribute;
63-
var osValue = new ClrEnumValueWrapper<T>(this, (T)field.GetValue(null));
6465

65-
if (contextField.Alias == null)
66+
string alias = contextField.Alias;
67+
if ( alias == null)
6668
{
6769
if(StringComparer
6870
.InvariantCultureIgnoreCase
6971
.Compare(field.Name, contextField.Name) != 0)
70-
AddValue(contextField.Name, field.Name, osValue);
71-
else
72-
AddValue(contextField.Name, osValue);
72+
alias = field.Name;
7373
}
74-
else
75-
AddValue(contextField.Name, contextField.Alias, osValue);
74+
75+
var osValue = new ClrEnumValueWrapper<T>(valuesType, (T)field.GetValue(null),
76+
contextField.Name, alias);
77+
78+
AddValue(osValue);
7679
}
7780
}
7881
}
79-
82+
8083
public static ClrEnumWrapper<T> CreateInstance(TypeDescriptor typeRepresentation, TypeDescriptor valuesType)
8184
{
8285
var instance = new ClrEnumWrapper<T>(typeRepresentation, valuesType);
83-
instance.Autoregister();
86+
instance.Autoregister(valuesType);
8487
Instance = instance;
8588

8689
return instance;

src/ScriptEngine/Machine/Contexts/EnumContextHelper.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ public static ClrEnumValueWrapper<T> WrapClrValue<T>(
8181
T value)
8282
where T : struct
8383
{
84-
var wrappedValue = new ClrEnumValueWrapper<T>(owner, value);
85-
owner.AddValue(name, alias, wrappedValue);
84+
var wrappedValue = new ClrEnumValueWrapper<T>(owner.ValuesType, value, name, alias);
85+
owner.AddValue(wrappedValue);
8686
return wrappedValue;
8787
}
88-
8988
}
9089

9190
public delegate T EnumCreationDelegate<T>(TypeDescriptor typeRepresentation, TypeDescriptor valuesType) where T : EnumerationContext;

src/ScriptEngine/Machine/Contexts/EnumerationContext.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,15 @@ public EnumerationContext(TypeDescriptor typeRepresentation, TypeDescriptor valu
2525
_valuesType = valuesType;
2626
}
2727

28-
public void AddValue(string name, EnumerationValue val)
28+
public void AddValue(EnumerationValue val)
2929
{
30-
AddValue(name, null, val);
31-
}
32-
33-
public void AddValue(string name, string alias, EnumerationValue val)
34-
{
35-
System.Diagnostics.Debug.Assert(name != null);
3630
System.Diagnostics.Debug.Assert(val != null);
3731

38-
if (!Utils.IsValidIdentifier(name))
39-
throw new ArgumentException("Name must be a valid identifier", "name");
40-
41-
if(alias != null && !Utils.IsValidIdentifier(alias))
42-
throw new ArgumentException("Name must be a valid identifier", "alias");
43-
44-
_nameIds.RegisterName(name, alias);
45-
val.ValuePresentation = name;
32+
_nameIds.RegisterName(val.Name, val.Alias);
4633
_values.Add(val);
47-
4834
}
4935

50-
public TypeDescriptor ValuesType
51-
{
52-
get
53-
{
54-
return _valuesType;
55-
}
56-
}
36+
public TypeDescriptor ValuesType => _valuesType;
5737

5838
public EnumerationValue this[string name]
5939
{

src/ScriptEngine/Machine/Contexts/EnumerationValue.cs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,46 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

8+
using OneScript.Commons;
89
using OneScript.Exceptions;
10+
using OneScript.Localization;
911
using OneScript.Types;
1012
using OneScript.Values;
13+
using System;
1114

1215
namespace ScriptEngine.Machine.Contexts
1316
{
1417
public abstract class EnumerationValue : BslValue
1518
{
16-
readonly EnumerationContext _owner;
19+
readonly TypeDescriptor _systemType;
20+
readonly string _name, _alias;
1721

18-
public EnumerationValue(EnumerationContext owner)
22+
public EnumerationValue(TypeDescriptor systemType, string name, string alias)
1923
{
20-
_owner = owner;
21-
}
24+
if (!Utils.IsValidIdentifier(name))
25+
throw new ArgumentException("Name must be a valid identifier", "name");
2226

23-
public EnumerationContext Owner
24-
{
25-
get
26-
{
27-
return _owner;
28-
}
29-
}
27+
if(alias != null && !Utils.IsValidIdentifier(alias))
28+
throw new ArgumentException("Name must be a valid identifier", "alias");
3029

31-
public string ValuePresentation
32-
{
33-
get;set;
30+
_systemType = systemType;
31+
_name = name;
32+
_alias = alias;
3433
}
3534

35+
public string Name => _name;
36+
public string Alias => _alias;
37+
3638
public bool IsFilled() => true;
3739

38-
public override TypeDescriptor SystemType => _owner.ValuesType;
40+
public override TypeDescriptor SystemType => _systemType;
3941

4042
public override string ToString()
4143
{
42-
return ValuePresentation == null ? SystemType.Name : ValuePresentation;
44+
return BilingualString.Localize(_name, _alias);
4345
}
4446

45-
public override IValue GetRawValue()
46-
{
47-
return this;
48-
}
47+
public override IValue GetRawValue() => this;
4948

5049
public override int CompareTo(BslValue other)
5150
{

0 commit comments

Comments
 (0)