Skip to content

Commit a7f5afc

Browse files
committed
Implement runtime JIT capability detection.
1 parent 03d1316 commit a7f5afc

File tree

3 files changed

+73
-15
lines changed

3 files changed

+73
-15
lines changed

src/MsgPack/Serialization/EmittingSerializers/SerializationMethodGeneratorManager.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static SerializationMethodGeneratorManager Get( SerializationMethodGenera
9494

9595
#if !SILVERLIGHT
9696

97-
private static SerializationMethodGeneratorManager _canCollect = new SerializationMethodGeneratorManager( false, true, null );
97+
private static SerializationMethodGeneratorManager _canCollect = Create( false, true, null );
9898

9999
/// <summary>
100100
/// Get the singleton instance for can-collect mode.
@@ -105,7 +105,7 @@ public static SerializationMethodGeneratorManager CanCollect
105105
}
106106

107107
#if !NETSTANDARD1_1 && !NETSTANDARD1_3
108-
private static SerializationMethodGeneratorManager _canDump = new SerializationMethodGeneratorManager( true, false, null );
108+
private static SerializationMethodGeneratorManager _canDump = Create( true, false, null );
109109

110110
/// <summary>
111111
/// Get the singleton instance for can-dump mode.
@@ -118,7 +118,7 @@ public static SerializationMethodGeneratorManager CanDump
118118
#endif // !NETSTANDARD1_1 && !NETSTANDARD1_3
119119
#endif // !SILVERLIGHT
120120

121-
private static SerializationMethodGeneratorManager _fast = new SerializationMethodGeneratorManager( false, false, null );
121+
private static SerializationMethodGeneratorManager _fast = Create( false, false, null );
122122

123123
/// <summary>
124124
/// Get the singleton instance for fast mode.
@@ -128,15 +128,27 @@ public static SerializationMethodGeneratorManager Fast
128128
get { return _fast; }
129129
}
130130

131+
private static SerializationMethodGeneratorManager Create( bool isDebuggable, bool isCollectable, AssemblyBuilder assemblyBuilder )
132+
{
133+
try
134+
{
135+
return new SerializationMethodGeneratorManager( isDebuggable, isCollectable, assemblyBuilder );
136+
}
137+
catch ( PlatformNotSupportedException )
138+
{
139+
return null;
140+
}
141+
}
142+
131143
internal static void Refresh()
132144
{
133145
#if !SILVERLIGHT
134-
_canCollect = new SerializationMethodGeneratorManager( false, true, null );
146+
_canCollect = Create( false, true, null );
135147
#if !NETSTANDARD1_1 && !NETSTANDARD1_3
136-
_canDump = new SerializationMethodGeneratorManager( true, false, null );
148+
_canDump = Create( true, false, null );
137149
#endif // !NETSTANDARD1_1 && !NETSTANDARD1_3
138150
#endif // !SILVERLIGHT
139-
_fast = new SerializationMethodGeneratorManager( false, false, null );
151+
_fast = Create( false, false, null );
140152
}
141153

142154
// ReSharper disable once PrivateFieldCanBeConvertedToLocalVariable

src/MsgPack/Serialization/SerializationContext.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -655,23 +655,23 @@ public MessagePackSerializer<T> GetSerializer<T>( object providerParameter )
655655

656656
if ( serializer == null )
657657
{
658-
#if !AOT
659-
if ( this._serializerGeneratorOptions.DisableRuntimeCodeGeneration )
658+
#if !UNITY
659+
if ( !this._serializerGeneratorOptions.CanRuntimeCodeGeneration )
660660
{
661-
#endif // AOT
661+
#endif // !UNITY
662662
// On debugging, or AOT only envs, use reflection based aproach.
663663
serializer =
664664
this.GetSerializerWithoutGeneration<T>( schema )
665665
?? this.OnResolveSerializer<T>( schema )
666666
?? MessagePackSerializer.CreateReflectionInternal<T>( this, this.EnsureConcreteTypeRegistered( typeof( T ) ), schema );
667-
#if !AOT
667+
#if !UNITY
668668
}
669669
else
670670
{
671671
// This thread creating new type serializer.
672672
serializer = this.OnResolveSerializer<T>( schema ) ?? MessagePackSerializer.CreateInternal<T>( this, schema );
673673
}
674-
#endif // !AOT
674+
#endif // !UNITY
675675
}
676676
}
677677
else

src/MsgPack/Serialization/SerializerOptions.cs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#region -- License Terms --
1+
#region -- License Terms --
22
//
33
// MessagePack for CLI
44
//
@@ -24,12 +24,20 @@
2424
#endif
2525

2626
using System;
27-
using System.Threading;
2827
#if CORE_CLR || UNITY || NETSTANDARD1_1
2928
using Contract = MsgPack.MPContract;
3029
#else
3130
using System.Diagnostics.Contracts;
3231
#endif // CORE_CLR || UNITY || NETSTANDARD1_1
32+
#if NETSTANDARD1_3 || NETSTANDARD2_0
33+
using System.Reflection;
34+
using System.Reflection.Emit;
35+
#endif // NETSTANDARD1_3 || NETSTANDARD2_0
36+
using System.Runtime.CompilerServices;
37+
using System.Threading;
38+
#if NETSTANDARD1_3 || NETSTANDARD2_0
39+
using MsgPack.Serialization.EmittingSerializers;
40+
#endif // NETSTANDARD1_3 || NETSTANDARD2_0
3341

3442
namespace MsgPack.Serialization
3543
{
@@ -59,7 +67,7 @@ internal EmitterFlavor EmitterFlavor
5967
set { Volatile.Write( ref this._emitterFlavor, ( int )value ); }
6068
}
6169

62-
#if !AOT
70+
#if !UNITY
6371

6472
private int _generatorOption;
6573

@@ -135,7 +143,45 @@ public bool DisableRuntimeCodeGeneration
135143
#endif // !FEATURE_CONCURRENT
136144
}
137145
}
138-
#endif // !AOT
146+
147+
private static bool CanEmit = DetermineCanEmit();
148+
149+
[MethodImpl( MethodImplOptions.NoInlining )]
150+
private static bool DetermineCanEmit()
151+
{
152+
#if NETSTANDARD1_3 || NETSTANDARD2_0
153+
try
154+
{
155+
return DetermineCanEmitCore();
156+
}
157+
catch
158+
{
159+
return false;
160+
}
161+
#elif NETFX_CORE || UNITY
162+
return false;
163+
#else
164+
// Desktop etc.
165+
return true;
166+
#endif
167+
}
168+
169+
#if NETSTANDARD1_3 || NETSTANDARD2_0
170+
171+
[MethodImpl( MethodImplOptions.NoInlining )]
172+
private static bool DetermineCanEmitCore()
173+
{
174+
return SerializationMethodGeneratorManager.Fast != null;
175+
}
176+
177+
#endif // NETSTANDARD1_3 || NETSTANDARD2_0
178+
179+
internal bool CanRuntimeCodeGeneration
180+
{
181+
get { return CanEmit && !this.DisableRuntimeCodeGeneration; }
182+
}
183+
184+
#endif // !UNITY
139185

140186
#if !FEATURE_CONCURRENT
141187
private volatile bool _isNonPublicAccessDisabled;

0 commit comments

Comments
 (0)