Skip to content

Commit 273f857

Browse files
authored
Merge pull request #1692 from Mr-Rm/v2/native/fix-dynamic
Ошибка в операции с датой
2 parents 4cbc198 + 09a5044 commit 273f857

1 file changed

Lines changed: 59 additions & 94 deletions

File tree

src/OneScript.Native/Runtime/DynamicOperations.cs

Lines changed: 59 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -28,68 +28,39 @@ public static BslValue Add(BslValue left, BslValue right)
2828
if (left is BslStringValue str)
2929
return BslStringValue.Create(str + right);
3030

31-
if (left is BslDateValue bslDate && right is BslNumericValue num)
32-
{
33-
return BslDateValue.Create(bslDate - (decimal) num);
34-
}
31+
if (left is BslDateValue bslDate)
32+
return BslDateValue.Create(bslDate + (decimal)right);
3533

3634
var dLeft = (decimal)left;
3735
var dRight = (decimal)right;
38-
return BslNumericValue.Create(dLeft + dRight);
36+
return BslNumericValue.Create(dLeft + dRight); // or throw ConvertToNumberException();
3937
}
4038

4139
public static BslValue Subtract(BslValue left, BslValue right)
4240
{
4341
if (left is BslNumericValue num)
42+
return BslNumericValue.Create(num - (decimal)right);
43+
44+
if (left is BslDateValue date)
4445
{
45-
var result = num - (decimal)right;
46-
return BslNumericValue.Create(result);
47-
}
48-
else if (left is BslDateValue date)
49-
{
50-
switch (right)
51-
{
52-
case BslNumericValue numRight:
53-
{
54-
var result = date - numRight;
55-
return BslDateValue.Create(result);
56-
}
57-
case BslDateValue dateRight:
58-
{
59-
var result = date - dateRight;
60-
return BslNumericValue.Create(result);
61-
}
62-
}
63-
}
64-
else
65-
{
66-
var dLeft = (decimal)left;
67-
var dRight = (decimal)right;
68-
return BslNumericValue.Create(dLeft - dRight);
46+
if (right is BslDateValue dateRight)
47+
return BslNumericValue.Create(date - dateRight);
48+
49+
return BslDateValue.Create(date - (decimal)right);
6950
}
7051

71-
throw BslExceptions.ConvertToNumberException();
72-
}
73-
74-
public static bool ToBoolean(BslValue value)
75-
{
76-
return (bool)value;
77-
}
78-
79-
public static decimal ToNumber(BslValue value)
80-
{
81-
return (decimal)value;
82-
}
83-
84-
public static DateTime ToDate(BslValue value)
85-
{
86-
return (DateTime)value;
87-
}
88-
89-
public static string ToString(BslValue value)
90-
{
91-
return value.ToString();
92-
}
52+
var dLeft = (decimal)left;
53+
var dRight = (decimal)right;
54+
return BslNumericValue.Create(dLeft - dRight); // or throw ConvertToNumberException();
55+
}
56+
57+
public static bool ToBoolean(BslValue value) => (bool)value;
58+
59+
public static decimal ToNumber(BslValue value) => (decimal)value;
60+
61+
public static DateTime ToDate(BslValue value) => (DateTime)value;
62+
63+
public static string ToString(BslValue value) => value.ToString();
9364

9465
// FIXME: тут не должно быть Null, но из-за несовершенства мира они тут бывают. Когда задолбает - надо починить и убрать отсюда проверки на null
9566
public static bool Equality(BslValue left, BslValue right)
@@ -116,32 +87,32 @@ public static int Comparison(BslValue left, BslValue right)
11687
}
11788

11889
public static BslValue WrapClrObjectToValue(object value)
119-
{
90+
{
12091
return value switch
12192
{
122-
null => BslUndefinedValue.Instance,
123-
string s => BslStringValue.Create(s),
124-
decimal d => BslNumericValue.Create(d),
125-
126-
int n => BslNumericValue.Create(n),
127-
uint n => BslNumericValue.Create(n),
128-
short n => BslNumericValue.Create(n),
129-
ushort n => BslNumericValue.Create(n),
130-
byte n => BslNumericValue.Create(n),
131-
sbyte n => BslNumericValue.Create(n),
132-
long l => BslNumericValue.Create(l),
133-
ulong l => BslNumericValue.Create(l),
134-
135-
double dbl => BslNumericValue.Create((decimal) dbl),
136-
bool boolean => BslBooleanValue.Create(boolean),
137-
DateTime date => BslDateValue.Create(date),
138-
BslValue bslValue => bslValue,
139-
_ => throw new TypeConversionException(new BilingualString(
140-
$"Невозможно преобразовать {value.GetType()} в тип {nameof(BslValue)}",
141-
$"Can't Convert {value.GetType()} to {nameof(BslValue)}"))
142-
};
143-
}
144-
93+
null => BslUndefinedValue.Instance,
94+
string s => BslStringValue.Create(s),
95+
decimal d => BslNumericValue.Create(d),
96+
97+
int n => BslNumericValue.Create(n),
98+
uint n => BslNumericValue.Create(n),
99+
short n => BslNumericValue.Create(n),
100+
ushort n => BslNumericValue.Create(n),
101+
byte n => BslNumericValue.Create(n),
102+
sbyte n => BslNumericValue.Create(n),
103+
long l => BslNumericValue.Create(l),
104+
ulong l => BslNumericValue.Create(l),
105+
106+
double dbl => BslNumericValue.Create((decimal) dbl),
107+
bool boolean => BslBooleanValue.Create(boolean),
108+
DateTime date => BslDateValue.Create(date),
109+
BslValue bslValue => bslValue,
110+
_ => throw new TypeConversionException(new BilingualString(
111+
$"Невозможно преобразовать {value.GetType()} в тип {nameof(BslValue)}",
112+
$"Can't Convert {value.GetType()} to {nameof(BslValue)}"))
113+
};
114+
}
115+
145116
public static BslValue ConstructorCall(ITypeManager typeManager, IServiceContainer services, string typeName, IBslProcess process, BslValue[] args)
146117
{
147118
var type = typeManager.GetTypeByName(typeName);
@@ -155,39 +126,33 @@ public static BslValue ConstructorCall(ITypeManager typeManager, IServiceContain
155126
};
156127

157128
return (BslValue) factory.Activate(context, args.Cast<IValue>().ToArray());
158-
}
159-
160-
// TODO: Сделать прямой маппинг на статические фабрики-методы, а не через Factory.Activate
161-
public static T StrictConstructorCall<T>(ITypeManager typeManager, IServiceContainer services, string typeName, IBslProcess process, BslValue[] args)
129+
}
130+
131+
// TODO: Сделать прямой маппинг на статические фабрики-методы, а не через Factory.Activate
132+
public static T StrictConstructorCall<T>(ITypeManager typeManager, IServiceContainer services,
133+
string typeName, IBslProcess process, BslValue[] args)
162134
where T : BslValue
163-
{
164-
return (T) ConstructorCall(typeManager, services, typeName, process, args);
165-
}
135+
=> (T)ConstructorCall(typeManager, services, typeName, process, args);
166136

167137
public static BslObjectValue GetExceptionInfo(IExceptionInfoFactory factory, Exception e)
168-
{
169-
return factory.GetExceptionInfo(e);
170-
}
138+
=> factory.GetExceptionInfo(e);
171139

172140
public static BslTypeValue GetTypeByName(ITypeManager manager, string name)
173-
{
174-
var foundType = manager.GetTypeByName(name);
175-
return new BslTypeValue(foundType);
176-
}
141+
=> new(manager.GetTypeByName(name));
177142

178143
public static BslValue GetIndexedValue(object target, BslValue index)
179144
{
180-
if (!(target is IRuntimeContextInstance context) || !context.IsIndexed)
145+
if (target is not IRuntimeContextInstance context || !context.IsIndexed)
181146
{
182147
throw RuntimeException.IndexedAccessIsNotSupportedException();
183148
}
184149

185-
return (BslValue)context.GetIndexedValue((IValue)index);
150+
return (BslValue)context.GetIndexedValue(index);
186151
}
187152

188153
public static void SetIndexedValue(object target, BslValue index, BslValue value)
189154
{
190-
if (!(target is IRuntimeContextInstance context) || !context.IsIndexed)
155+
if (target is not IRuntimeContextInstance context || !context.IsIndexed)
191156
{
192157
throw RuntimeException.IndexedAccessIsNotSupportedException();
193158
}
@@ -197,7 +162,7 @@ public static void SetIndexedValue(object target, BslValue index, BslValue value
197162

198163
public static BslValue GetPropertyValue(object target, string propertyName)
199164
{
200-
if (!(target is IRuntimeContextInstance context))
165+
if (target is not IRuntimeContextInstance context)
201166
throw BslExceptions.ValueIsNotObjectException();
202167

203168
var propIndex = context.GetPropertyNumber(propertyName);
@@ -206,7 +171,7 @@ public static BslValue GetPropertyValue(object target, string propertyName)
206171

207172
public static BslValue TryCallContextMethod(BslValue instance, string methodName, IBslProcess process, BslValue[] arguments)
208173
{
209-
if (!(instance is IRuntimeContextInstance context))
174+
if (instance is not IRuntimeContextInstance context)
210175
throw BslExceptions.ValueIsNotObjectException();
211176

212177
return CallContextMethod(context, methodName, process, arguments);

0 commit comments

Comments
 (0)