Skip to content

Commit 99d3376

Browse files
authored
Merge pull request #1337 from EvilBeaver/feature/issue-1327-latest
Преобразования типов в составном описании
2 parents 4190ddf + c053e5c commit 99d3376

File tree

5 files changed

+570
-198
lines changed

5 files changed

+570
-198
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 ScriptEngine.HostedScript.Library.Binary;
9+
using ScriptEngine.Machine;
10+
using ScriptEngine.Machine.Values;
11+
12+
namespace ScriptEngine.HostedScript.Library
13+
{
14+
public static class CommonTypes
15+
{
16+
public static readonly TypeDescriptor BinaryData = TypeManager.GetTypeByFrameworkType(typeof(BinaryDataContext));
17+
public static readonly TypeDescriptor Null = TypeManager.GetTypeByFrameworkType(typeof(NullValue));
18+
public static readonly TypeDescriptor Boolean = TypeDescriptor.FromDataType(DataType.Boolean);
19+
public static readonly TypeDescriptor String = TypeDescriptor.FromDataType(DataType.String);
20+
public static readonly TypeDescriptor Date = TypeDescriptor.FromDataType(DataType.Date);
21+
public static readonly TypeDescriptor Number = TypeDescriptor.FromDataType(DataType.Number);
22+
public static readonly TypeDescriptor Type = TypeDescriptor.FromDataType(DataType.Type);
23+
public static readonly TypeDescriptor Undefined = TypeDescriptor.FromDataType(DataType.Undefined);
24+
25+
public static readonly TypeDescriptor[] Primitives = {
26+
CommonTypes.Boolean,
27+
CommonTypes.BinaryData,
28+
CommonTypes.String,
29+
CommonTypes.Date,
30+
CommonTypes.Null,
31+
CommonTypes.Number,
32+
CommonTypes.Type
33+
};
34+
}
35+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
using System.Collections.Generic;
8+
using ScriptEngine.HostedScript.Library.Binary;
9+
using ScriptEngine.Machine;
10+
using ScriptEngine.Machine.Values;
11+
12+
namespace ScriptEngine.HostedScript.Library
13+
{
14+
internal class TypeComparer : IComparer<TypeTypeValue>
15+
{
16+
public int Compare(TypeTypeValue x, TypeTypeValue y)
17+
{
18+
if (x == null)
19+
{
20+
return y == null ? 0 : -1;
21+
}
22+
23+
if (y == null) return 1;
24+
25+
var primitiveX = PrimitiveIndex(x);
26+
var primitiveY = PrimitiveIndex(y);
27+
28+
if (primitiveX != -1)
29+
{
30+
if (primitiveY != -1)
31+
return primitiveX - primitiveY;
32+
33+
return -1;
34+
}
35+
36+
if (primitiveY != -1)
37+
return 1;
38+
39+
return x.Value.ID.CompareTo(y.Value.ID);
40+
}
41+
42+
private static int PrimitiveIndex(TypeTypeValue type)
43+
{
44+
var typeDescriptor = TypeManager.GetTypeDescriptorFor(type);
45+
for (var primitiveIndex = 0; primitiveIndex < CommonTypes.Primitives.Length; primitiveIndex++)
46+
{
47+
if (typeDescriptor.Equals(CommonTypes.Primitives[primitiveIndex]))
48+
{
49+
return primitiveIndex;
50+
}
51+
}
52+
53+
return -1;
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)