Skip to content

Commit

Permalink
Merge pull request MessagePack-CSharp#1461 from LineSmarts/master
Browse files Browse the repository at this point in the history
Fix for "System.NotImplementedException: byref delegate" (second attempt)
  • Loading branch information
AArnott authored Aug 2, 2022
2 parents 1092f85 + d75c56a commit 541ad7c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ public static object Deserialize(Type type, ReadOnlySequence<byte> bytes, Messag
return GetOrAdd(type).Deserialize_ReadOnlySequence_Options_CancellationToken.Invoke(bytes, options, cancellationToken);
}

/// <summary>
/// Helper method used by reflection.
/// </summary>
private static void SerializeSemiGeneric<T>(ref MessagePackWriter writer, object valueObject, MessagePackSerializerOptions options = null)
{
Serialize(ref writer, (T)valueObject, options);
}

/// <summary>
/// Helper method used by reflection.
/// </summary>
private static object DeserializeSemiGeneric<T>(ref MessagePackReader reader, MessagePackSerializerOptions options = null)
{
return Deserialize<T>(ref reader, options);
}

private static async ValueTask<object> DeserializeObjectAsync<T>(Stream stream, MessagePackSerializerOptions options, CancellationToken cancellationToken) => await DeserializeAsync<T>(stream, options, cancellationToken).ConfigureAwait(false);

private static CompiledMethods GetOrAdd(Type type)
Expand Down Expand Up @@ -219,39 +235,22 @@ internal CompiledMethods(Type type)
}

{
// public static void Serialize<T>(ref MessagePackWriter writer, T obj, MessagePackSerializerOptions options)
MethodInfo serialize = GetMethod(nameof(Serialize), type, new Type[] { typeof(MessagePackWriter).MakeByRefType(), null, typeof(MessagePackSerializerOptions) });
// private static void SerializeSemiGeneric<T>(ref MessagePackWriter writer, object obj, MessagePackSerializerOptions options)
MethodInfo serialize = GetMethod(nameof(SerializeSemiGeneric), type, new Type[] { typeof(MessagePackWriter).MakeByRefType(), typeof(object), typeof(MessagePackSerializerOptions) });
#if ENABLE_IL2CPP
this.Serialize_MessagePackWriter_T_Options = (ref MessagePackWriter x, object y, MessagePackSerializerOptions z) => ThrowRefStructNotSupported();
#else
ParameterExpression param1 = Expression.Parameter(typeof(MessagePackWriter).MakeByRefType(), "writer");
ParameterExpression param2 = Expression.Parameter(typeof(object), "obj");
ParameterExpression param3 = Expression.Parameter(typeof(MessagePackSerializerOptions), "options");

MethodCallExpression body = Expression.Call(
null,
serialize,
param1,
ti.IsValueType ? Expression.Unbox(param2, type) : Expression.Convert(param2, type),
param3);
MessagePackWriterSerialize lambda = Expression.Lambda<MessagePackWriterSerialize>(body, param1, param2, param3).Compile(PreferInterpretation);

this.Serialize_MessagePackWriter_T_Options = lambda;
this.Serialize_MessagePackWriter_T_Options = (MessagePackWriterSerialize)serialize.CreateDelegate(typeof(MessagePackWriterSerialize));
#endif
}

{
// public static T Deserialize<T>(ref MessagePackReader reader, MessagePackSerializerOptions options)
MethodInfo deserialize = GetMethod(nameof(Deserialize), type, new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(MessagePackSerializerOptions) });
// private static object DeserializeSemiGeneric<T>(ref MessagePackReader reader, MessagePackSerializerOptions options)
MethodInfo deserialize = GetMethod(nameof(DeserializeSemiGeneric), type, new Type[] { typeof(MessagePackReader).MakeByRefType(), typeof(MessagePackSerializerOptions) });
#if ENABLE_IL2CPP
this.Deserialize_MessagePackReader_Options = (ref MessagePackReader reader, MessagePackSerializerOptions options) => { ThrowRefStructNotSupported(); return null; };
#else
ParameterExpression param1 = Expression.Parameter(typeof(MessagePackReader).MakeByRefType(), "reader");
ParameterExpression param2 = Expression.Parameter(typeof(MessagePackSerializerOptions), "options");
UnaryExpression body = Expression.Convert(Expression.Call(null, deserialize, param1, param2), typeof(object));
MessagePackReaderDeserialize lambda = Expression.Lambda<MessagePackReaderDeserialize>(body, param1, param2).Compile();

this.Deserialize_MessagePackReader_Options = lambda;
this.Deserialize_MessagePackReader_Options = (MessagePackReaderDeserialize)deserialize.CreateDelegate(typeof(MessagePackReaderDeserialize));
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1264,6 +1264,9 @@ void OnNotFound()
il.Emit(OpCodes.Br, readNext);
}

#if NET_STANDARD_2_0
throw new NotImplementedException("NET_STANDARD_2_0 directive was used");
#else
if (canOverwrite)
{
automata.EmitMatch(il, buffer, longKey, OnFoundAssignDirect, OnNotFound);
Expand All @@ -1272,6 +1275,7 @@ void OnNotFound()
{
automata.EmitMatch(il, buffer, longKey, OnFoundAssignLocalVariable, OnNotFound);
}
#endif

il.MarkLabel(readNext);
reader.EmitLdarg();
Expand Down

0 comments on commit 541ad7c

Please sign in to comment.