Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit dfe8f72

Browse files
committed
Merge pull request #2081 from khdang/merge_with_netnative_dcjs
Update NetCore DCJS to use the same code with NetNative DCJS
2 parents ef2db56 + 082f3aa commit dfe8f72

File tree

55 files changed

+2764
-549
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+2764
-549
lines changed

src/System.Private.DataContractSerialization/src/Resources/Strings.resx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1083,5 +1083,10 @@
10831083
<data name="EnumTypeNotSupportedByDataContractJsonSerializer" xml:space="preserve">
10841084
<value>DataContractJsonSerializer does not support data members of type '{0}'. Consider using int, System.Object, or a concrete enum definition instead.</value>
10851085
</data>
1086-
1086+
<data name="KeyTypeCannotBeParsedInSimpleDictionary" xml:space="preserve">
1087+
<value>The dictionary of type '{0}' cannot be deserialized as a simple dictionary because its key type '{1}' does not have a public static Parse method.</value>
1088+
</data>
1089+
<data name="JsonDuplicateMemberInInput" xml:space="preserve">
1090+
<value>The data contract type '{0}' cannot be deserialized because the data member '{1}' was found more than once in the input.</value>
1091+
</data>
10871092
</root>

src/System.Private.DataContractSerialization/src/System.Private.DataContractSerialization.csproj

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
<FileAlignment>512</FileAlignment>
1515
<DebugSymbols>true</DebugSymbols>
1616
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DefineConstants>$(DefineConstants);MERGE_DCJS</DefineConstants>
1718
</PropertyGroup>
1819
<PropertyGroup>
1920
<RuntimeSerializationSources>System\Runtime\Serialization</RuntimeSerializationSources>
2021
<JsonSources>System\Runtime\Serialization\Json</JsonSources>
2122
<XmlSources>System\Xml</XmlSources>
2223
<TextSources>System\Text</TextSources>
24+
<NetNative>$(DefineConstants.Contains('NET_NATIVE'))</NetNative>
25+
<MergeDcjs>$(DefineConstants.Contains('MERGE_DCJS'))</MergeDcjs>
2326
</PropertyGroup>
2427
<ItemGroup>
2528
<Compile Include="$(CommonPath)\System\__HResults.cs" />
@@ -38,12 +41,12 @@
3841
<Compile Include="$(RuntimeSerializationSources)\DataContractSerializerSettings.cs" />
3942
<Compile Include="$(RuntimeSerializationSources)\DataMember.cs" />
4043
<Compile Include="$(RuntimeSerializationSources)\EnumDataContract.cs" />
41-
<Compile Include="$(RuntimeSerializationSources)\ExtensionDataObject.cs" />
42-
<Compile Include="$(RuntimeSerializationSources)\ExtensionDataReader.cs" />
44+
<Compile Include="$(RuntimeSerializationSources)\ExtensionDataObject.cs" Condition="$(NetNative)" />
45+
<Compile Include="$(RuntimeSerializationSources)\ExtensionDataReader.cs" Condition="$(NetNative)" />
4346
<Compile Include="$(RuntimeSerializationSources)\Globals.cs" />
44-
<Compile Include="$(RuntimeSerializationSources)\GenericParameterDataContract.cs" />
47+
<Compile Include="$(RuntimeSerializationSources)\GenericParameterDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
4548
<Compile Include="$(RuntimeSerializationSources)\HybridObjectCache.cs" />
46-
<Compile Include="$(RuntimeSerializationSources)\InvalidDataContract.cs" />
49+
<Compile Include="$(RuntimeSerializationSources)\InvalidDataContract.cs" Condition="$(NetNative)" />
4750
<Compile Include="$(RuntimeSerializationSources)\ObjectToIdCache.cs" />
4851
<Compile Include="$(RuntimeSerializationSources)\ObjectReferenceStack.cs" />
4952
<Compile Include="$(RuntimeSerializationSources)\PrimitiveDataContract.cs" />
@@ -66,8 +69,9 @@
6669
<Compile Include="$(RuntimeSerializationSources)\TypeCode.cs" />
6770
<Compile Include="$(RuntimeSerializationSources)\ExceptionDataContract.cs" />
6871
<Compile Include="$(RuntimeSerializationSources)\KnownTypeDataContractResolver.cs" />
69-
<Compile Include="$(RuntimeSerializationSources)\XmlObjectSerializerReadContextComplex.cs" />
70-
<Compile Include="$(RuntimeSerializationSources)\XmlObjectSerializerWriteContextComplex.cs" />
72+
<Compile Include="$(RuntimeSerializationSources)\XmlObjectSerializerReadContextComplex.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
73+
<Compile Include="$(RuntimeSerializationSources)\XmlObjectSerializerWriteContextComplex.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
74+
<Compile Include="$(RuntimeSerializationSources)\BitFlagsGenerator.cs" Condition="!$(NetNative) And $(MergeDcjs)" />
7175
<Compile Include="$(XmlSources)\ArrayHelper.cs" />
7276
<Compile Include="$(XmlSources)\IStreamProvider.cs" />
7377
<Compile Include="$(XmlSources)\IXmlDictionary.cs" />
@@ -90,31 +94,31 @@
9094
<Compile Include="$(XmlSources)\XmlDictionaryReaderQuotas.cs" />
9195
<Compile Include="$(XmlSources)\XmlDictionaryString.cs" />
9296
<Compile Include="$(XmlSources)\XmlExceptionHelper.cs" />
93-
<Compile Include="$(XmlSources)\XmlNodeReader.cs" />
97+
<Compile Include="$(XmlSources)\XmlNodeReader.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
9498
<Compile Include="$(XmlSources)\XmlNodeWriter.cs" />
9599
<Compile Include="$(XmlSources)\XmlStreamNodeWriter.cs" />
96100
<Compile Include="$(XmlSources)\XmlSchemaSet.cs" />
97101
<Compile Include="$(XmlSources)\XmlUTF8TextReader.cs" />
98-
<Compile Include="$(XmlSources)\XmlUTF8TextWriter.cs" />
102+
<Compile Include="$(XmlSources)\XmlUTF8TextWriter.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
99103
<Compile Include="$(XmlSources)\EncodingStreamWrapper.cs" />
100104
<Compile Include="$(TextSources)\Base64Encoding.cs" />
101105
<Compile Include="$(TextSources)\SurrogateChar.cs" />
102-
<Compile Include="$(TextSources)\BinHexEncoding.cs" />
106+
<Compile Include="$(TextSources)\BinHexEncoding.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
103107
<Compile Include="$(JsonSources)\DataContractJsonSerializer.cs" />
104-
<Compile Include="$(JsonSources)\JsonByteArrayDataContract.cs" />
105-
<Compile Include="$(JsonSources)\JsonClassDataContract.cs" />
106-
<Compile Include="$(JsonSources)\JsonCollectionDataContract.cs" />
107-
<Compile Include="$(JsonSources)\JsonDataContract.cs" />
108-
<Compile Include="$(JsonSources)\JsonEnumDataContract.cs" />
108+
<Compile Include="$(JsonSources)\JsonByteArrayDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
109+
<Compile Include="$(JsonSources)\JsonClassDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
110+
<Compile Include="$(JsonSources)\JsonCollectionDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
111+
<Compile Include="$(JsonSources)\JsonDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
112+
<Compile Include="$(JsonSources)\JsonEnumDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
109113
<Compile Include="$(JsonSources)\JsonFormatWriterGenerator.cs" />
110114
<Compile Include="$(JsonSources)\JsonFormatReaderGenerator.cs" />
111-
<Compile Include="$(JsonSources)\JsonObjectDataContract.cs" />
112-
<Compile Include="$(JsonSources)\JsonQNameDataContract.cs" />
113-
<Compile Include="$(JsonSources)\JsonReaderDelegator.cs" />
114-
<Compile Include="$(JsonSources)\JsonWriterDelegator.cs" />
115-
<Compile Include="$(JsonSources)\JsonStringDataContract.cs" />
116-
<Compile Include="$(JsonSources)\JsonUriDataContract.cs" />
117-
<Compile Include="$(JsonSources)\JsonXmlDataContract.cs" />
115+
<Compile Include="$(JsonSources)\JsonObjectDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
116+
<Compile Include="$(JsonSources)\JsonQNameDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
117+
<Compile Include="$(JsonSources)\JsonReaderDelegator.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
118+
<Compile Include="$(JsonSources)\JsonWriterDelegator.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
119+
<Compile Include="$(JsonSources)\JsonStringDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
120+
<Compile Include="$(JsonSources)\JsonUriDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
121+
<Compile Include="$(JsonSources)\JsonXmlDataContract.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
118122
<Compile Include="$(JsonSources)\JsonGlobals.cs" />
119123
<Compile Include="$(JsonSources)\XmlObjectSerializerReadContextComplexJson.cs" />
120124
<Compile Include="$(JsonSources)\XmlObjectSerializerWriteContextComplexJson.cs" />
@@ -127,14 +131,15 @@
127131
<Compile Include="$(JsonSources)\DateTimeFormat.cs" />
128132
<Compile Include="$(JsonSources)\EmitTypeInformation.cs" />
129133
<Compile Include="$(JsonSources)\DataContractJsonSerializerSettings.cs" />
130-
<Compile Include="$(JsonSources)\XmlJsonReader.cs" />
131-
<Compile Include="$(JsonSources)\XmlJsonWriter.cs" />
132-
<Compile Include="$(JsonSources)\JsonReaderWriterFactory.cs" />
133-
<Compile Include="$(JsonSources)\JsonNodeType.cs" />
134-
<Compile Include="$(JsonSources)\IXmlJsonReaderInitializer.cs" />
135-
<Compile Include="$(JsonSources)\IXmlJsonWriterInitializer.cs" />
136-
<Compile Include="$(JsonSources)\ByteArrayHelperWithString.cs" />
137-
<Compile Include="$(JsonSources)\JsonEncodingStreamWrapper.cs" />
134+
<Compile Include="$(JsonSources)\XmlJsonReader.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
135+
<Compile Include="$(JsonSources)\XmlJsonWriter.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
136+
<Compile Include="$(JsonSources)\JsonReaderWriterFactory.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
137+
<Compile Include="$(JsonSources)\JsonNodeType.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
138+
<Compile Include="$(JsonSources)\IXmlJsonReaderInitializer.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
139+
<Compile Include="$(JsonSources)\IXmlJsonWriterInitializer.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
140+
<Compile Include="$(JsonSources)\ByteArrayHelperWithString.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
141+
<Compile Include="$(JsonSources)\JsonEncodingStreamWrapper.cs" Condition="$(NetNative) Or $(MergeDcjs)" />
142+
<Compile Include="$(JsonSources)\JsonFormatGeneratorStatics.cs" Condition="!$(NetNative) And $(MergeDcjs)" />
138143
</ItemGroup>
139144
<ItemGroup>
140145
<ProjectReference Include="..\..\System.Runtime.Serialization.Primitives\src\System.Runtime.Serialization.Primitives.csproj">
@@ -146,4 +151,4 @@
146151
<None Include="project.json" />
147152
</ItemGroup>
148153
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
149-
</Project>
154+
</Project>
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace System.Runtime.Serialization
5+
{
6+
using System;
7+
using System.Reflection;
8+
using System.Reflection.Emit;
9+
using System.Security;
10+
11+
internal class BitFlagsGenerator
12+
{
13+
private int _bitCount;
14+
private CodeGenerator _ilg;
15+
private LocalBuilder[] _locals;
16+
17+
public BitFlagsGenerator(int bitCount, CodeGenerator ilg, string localName)
18+
{
19+
_ilg = ilg;
20+
_bitCount = bitCount;
21+
int localCount = (bitCount + 7) / 8;
22+
_locals = new LocalBuilder[localCount];
23+
for (int i = 0; i < _locals.Length; i++)
24+
{
25+
_locals[i] = ilg.DeclareLocal(typeof(byte), localName + i, (byte)0);
26+
}
27+
}
28+
29+
public static bool IsBitSet(byte[] bytes, int bitIndex)
30+
{
31+
int byteIndex = GetByteIndex(bitIndex);
32+
byte bitValue = GetBitValue(bitIndex);
33+
return (bytes[byteIndex] & bitValue) == bitValue;
34+
}
35+
36+
public static void SetBit(byte[] bytes, int bitIndex)
37+
{
38+
int byteIndex = GetByteIndex(bitIndex);
39+
byte bitValue = GetBitValue(bitIndex);
40+
bytes[byteIndex] |= bitValue;
41+
}
42+
43+
public int GetBitCount()
44+
{
45+
return _bitCount;
46+
}
47+
48+
public LocalBuilder GetLocal(int i)
49+
{
50+
return _locals[i];
51+
}
52+
53+
public int GetLocalCount()
54+
{
55+
return _locals.Length;
56+
}
57+
58+
public void Load(int bitIndex)
59+
{
60+
LocalBuilder local = _locals[GetByteIndex(bitIndex)];
61+
byte bitValue = GetBitValue(bitIndex);
62+
_ilg.Load(local);
63+
_ilg.Load(bitValue);
64+
_ilg.And();
65+
_ilg.Load(bitValue);
66+
_ilg.Ceq();
67+
}
68+
69+
public void LoadArray()
70+
{
71+
LocalBuilder localArray = _ilg.DeclareLocal(Globals.TypeOfByteArray, "localArray");
72+
_ilg.NewArray(typeof(byte), _locals.Length);
73+
_ilg.Store(localArray);
74+
for (int i = 0; i < _locals.Length; i++)
75+
{
76+
_ilg.StoreArrayElement(localArray, i, _locals[i]);
77+
}
78+
_ilg.Load(localArray);
79+
}
80+
81+
public void Store(int bitIndex, bool value)
82+
{
83+
LocalBuilder local = _locals[GetByteIndex(bitIndex)];
84+
byte bitValue = GetBitValue(bitIndex);
85+
if (value)
86+
{
87+
_ilg.Load(local);
88+
_ilg.Load(bitValue);
89+
_ilg.Or();
90+
_ilg.Stloc(local);
91+
}
92+
else
93+
{
94+
_ilg.Load(local);
95+
_ilg.Load(bitValue);
96+
_ilg.Not();
97+
_ilg.And();
98+
_ilg.Stloc(local);
99+
}
100+
}
101+
102+
private static byte GetBitValue(int bitIndex)
103+
{
104+
return (byte)(1 << (bitIndex & 7));
105+
}
106+
107+
private static int GetByteIndex(int bitIndex)
108+
{
109+
return bitIndex >> 3;
110+
}
111+
}
112+
}

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/ClassDataContract.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,23 @@ public int Compare(DataMember x, DataMember y)
14471447

14481448
internal static DataMemberComparer Singleton = new DataMemberComparer();
14491449
}
1450+
1451+
#if !NET_NATIVE && MERGE_DCJS
1452+
/// <summary>
1453+
/// Get object type for Xml/JsonFormmatReaderGenerator
1454+
/// </summary>
1455+
internal Type ObjectType
1456+
{
1457+
get
1458+
{
1459+
Type type = UnderlyingType;
1460+
if (type.GetTypeInfo().IsValueType && !IsNonAttributedType)
1461+
{
1462+
type = Globals.TypeOfValueType;
1463+
}
1464+
return type;
1465+
}
1466+
}
1467+
#endif
14501468
}
14511469
}
1452-

src/System.Private.DataContractSerialization/src/System/Runtime/Serialization/CodeGenerator.cs

Lines changed: 80 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,33 @@ private static MethodInfo ArraySetValue
7979
}
8080
}
8181

82+
#if !NET_NATIVE && MERGE_DCJS
83+
[SecurityCritical]
84+
private static MethodInfo s_objectToString;
85+
private static MethodInfo ObjectToString
86+
{
87+
[SecuritySafeCritical]
88+
get
89+
{
90+
if (s_objectToString == null)
91+
s_objectToString = typeof(object).GetMethod("ToString", Array.Empty<Type>());
92+
return s_objectToString;
93+
}
94+
}
95+
96+
private static MethodInfo s_stringFormat;
97+
private static MethodInfo StringFormat
98+
{
99+
[SecuritySafeCritical]
100+
get
101+
{
102+
if (s_stringFormat == null)
103+
s_stringFormat = typeof(string).GetMethod("Format", new Type[] { typeof(string), typeof(object[]) });
104+
return s_stringFormat;
105+
}
106+
}
107+
#endif
108+
82109
private Type _delegateType;
83110

84111
#if USE_REFEMIT
@@ -123,6 +150,10 @@ private static Module SerializationModule
123150
private enum CodeGenTrace { None, Save, Tron };
124151
private CodeGenTrace _codeGenTrace;
125152

153+
#if !NET_NATIVE && MERGE_DCJS
154+
private LocalBuilder _stringFormatArray;
155+
#endif
156+
126157
internal CodeGenerator()
127158
{
128159
//Defaulting to None as thats the default value in WCF
@@ -974,7 +1005,6 @@ internal void Ldc(object o)
9741005
case TypeCode.Decimal:
9751006
case TypeCode.DateTime:
9761007
case TypeCode.Empty:
977-
case TypeCode.DBNull:
9781008
default:
9791009
throw System.Runtime.Serialization.DiagnosticUtility.ExceptionUtility.ThrowHelperError(XmlObjectSerializer.CreateSerializationException(SR.Format(SR.UnknownConstantType, DataContract.GetClrTypeFullName(valueType))));
9801010
}
@@ -1185,7 +1215,6 @@ private OpCode GetLdelemOpCode(TypeCode typeCode)
11851215
switch (typeCode)
11861216
{
11871217
case TypeCode.Object:
1188-
case TypeCode.DBNull:
11891218
return OpCodes.Ldelem_Ref;// TypeCode.Object:
11901219
case TypeCode.Boolean:
11911220
return OpCodes.Ldelem_I1;// TypeCode.Boolean:
@@ -1250,7 +1279,6 @@ private OpCode GetStelemOpCode(TypeCode typeCode)
12501279
switch (typeCode)
12511280
{
12521281
case TypeCode.Object:
1253-
case TypeCode.DBNull:
12541282
return OpCodes.Stelem_Ref;// TypeCode.Object:
12551283
case TypeCode.Boolean:
12561284
return OpCodes.Stelem_I1;// TypeCode.Boolean:
@@ -1619,6 +1647,55 @@ internal void IfNotIsEmptyString(LocalBuilder strLocal)
16191647
Load(0);
16201648
If(Cmp.NotEqualTo);
16211649
}
1650+
1651+
#if !NET_NATIVE && MERGE_DCJS
1652+
internal void BeginWhileCondition()
1653+
{
1654+
Label startWhile = DefineLabel();
1655+
MarkLabel(startWhile);
1656+
_blockStack.Push(startWhile);
1657+
}
1658+
1659+
internal void BeginWhileBody(Cmp cmpOp)
1660+
{
1661+
Label startWhile = (Label)_blockStack.Pop();
1662+
If(cmpOp);
1663+
_blockStack.Push(startWhile);
1664+
}
1665+
1666+
internal void EndWhile()
1667+
{
1668+
Label startWhile = (Label)_blockStack.Pop();
1669+
Br(startWhile);
1670+
EndIf();
1671+
}
1672+
1673+
internal void CallStringFormat(string msg, params object[] values)
1674+
{
1675+
NewArray(typeof(object), values.Length);
1676+
if (_stringFormatArray == null)
1677+
_stringFormatArray = DeclareLocal(typeof(object[]), "stringFormatArray");
1678+
Stloc(_stringFormatArray);
1679+
for (int i = 0; i < values.Length; i++)
1680+
StoreArrayElement(_stringFormatArray, i, values[i]);
1681+
1682+
Load(msg);
1683+
Load(_stringFormatArray);
1684+
Call(StringFormat);
1685+
}
1686+
1687+
internal void ToString(Type type)
1688+
{
1689+
if (type != Globals.TypeOfString)
1690+
{
1691+
if (type.GetTypeInfo().IsValueType)
1692+
{
1693+
Box(type);
1694+
}
1695+
Call(ObjectToString);
1696+
}
1697+
}
1698+
#endif
16221699
}
16231700

16241701

0 commit comments

Comments
 (0)