Skip to content

Commit b91a93c

Browse files
committed
fix #1115: беззнаковые типы для ЧтениеДанных/ЗаписьДанных
1 parent 58aea76 commit b91a93c

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/ScriptEngine.HostedScript/Library/Binary/DataReader.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,10 +472,10 @@ private T FromBytes<T>(byte[] bytes, Func<byte[], int, T> leConverter, Func<byte
472472
/// <returns name="Number"/>
473473
///
474474
[ContextMethod("ПрочитатьЦелое16", "ReadInt16")]
475-
public int ReadInt16(IValue byteOrder = null)
475+
public uint ReadInt16(IValue byteOrder = null)
476476
{
477-
var bytes = _reader.ReadBytes(sizeof(short));
478-
return FromBytes(bytes, BitConversionFacility.LittleEndian.ToInt16, BitConversionFacility.BigEndian.ToInt16, byteOrder);
477+
var bytes = _reader.ReadBytes(sizeof(ushort));
478+
return FromBytes(bytes, BitConversionFacility.LittleEndian.ToUInt16, BitConversionFacility.BigEndian.ToUInt16, byteOrder);
479479
}
480480

481481

src/ScriptEngine.HostedScript/Library/Binary/DataWriter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private byte[] GetBytes<T>(T value, Converter<T, byte[]> leConverter, Converter<
379379
/// Значение по умолчанию: Неопределено. </param>
380380
///
381381
[ContextMethod("ЗаписатьЦелое16", "WriteInt16")]
382-
public void WriteInt16(short number, IValue byteOrder = null)
382+
public void WriteInt16(ushort number, IValue byteOrder = null)
383383
{
384384
var buffer = GetBytes(number, BitConversionFacility.LittleEndian.GetBytes, BitConversionFacility.BigEndian.GetBytes, byteOrder);
385385
_binaryWriter.Write(buffer, 0, buffer.Length);
@@ -397,7 +397,7 @@ public void WriteInt16(short number, IValue byteOrder = null)
397397
/// Значение по умолчанию: Неопределено. </param>
398398
///
399399
[ContextMethod("ЗаписатьЦелое32", "WriteInt32")]
400-
public void WriteInt32(int number, IValue byteOrder = null)
400+
public void WriteInt32(uint number, IValue byteOrder = null)
401401
{
402402
var buffer = GetBytes(number, BitConversionFacility.LittleEndian.GetBytes, BitConversionFacility.BigEndian.GetBytes, byteOrder);
403403
_binaryWriter.Write(buffer, 0, buffer.Length);
@@ -406,7 +406,7 @@ public void WriteInt32(int number, IValue byteOrder = null)
406406

407407
/// <summary>
408408
///
409-
/// Записывает целое 16-битное число в целевой поток.
409+
/// Записывает целое 64-битное число в целевой поток.
410410
/// </summary>
411411
///
412412
/// <param name="number">
@@ -416,7 +416,7 @@ public void WriteInt32(int number, IValue byteOrder = null)
416416
/// Значение по умолчанию: Неопределено. </param>
417417
///
418418
[ContextMethod("ЗаписатьЦелое64", "WriteInt64")]
419-
public void WriteInt64(long number, IValue byteOrder = null)
419+
public void WriteInt64(ulong number, IValue byteOrder = null)
420420
{
421421
var buffer = GetBytes(number, BitConversionFacility.LittleEndian.GetBytes, BitConversionFacility.BigEndian.GetBytes, byteOrder);
422422
_binaryWriter.Write(buffer, 0, buffer.Length);

src/ScriptEngine/Machine/Contexts/ContextValuesMarshaller.cs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,36 @@ public static class ContextValuesMarshaller
1414
{
1515
public static T ConvertParam<T>(IValue value)
1616
{
17-
var type = typeof(T);
18-
object valueObj = ConvertParam(value, type);
19-
if (valueObj == null)
20-
{
21-
return default(T);
22-
}
23-
2417
try
2518
{
26-
return (T)valueObj;
19+
object valueObj = ConvertParam(value, typeof(T));
20+
return valueObj != null ? (T)valueObj : default;
2721
}
2822
catch (InvalidCastException)
2923
{
3024
throw RuntimeException.InvalidArgumentType();
3125
}
32-
26+
catch (OverflowException)
27+
{
28+
throw RuntimeException.InvalidArgumentValue();
29+
}
3330
}
3431

3532
public static T ConvertParam<T>(IValue value, T defaultValue)
3633
{
37-
var type = typeof(T);
38-
object valueObj = ConvertParam(value, type);
39-
if (valueObj == null)
40-
{
41-
return defaultValue;
42-
}
43-
4434
try
4535
{
46-
return (T)valueObj;
36+
object valueObj = ConvertParam(value, typeof(T));
37+
return valueObj != null ? (T)valueObj : defaultValue;
4738
}
4839
catch (InvalidCastException)
4940
{
5041
throw RuntimeException.InvalidArgumentType();
5142
}
52-
43+
catch (OverflowException)
44+
{
45+
throw RuntimeException.InvalidArgumentValue();
46+
}
5347
}
5448

5549
public static object ConvertParam(IValue value, Type type)

0 commit comments

Comments
 (0)