Skip to content

Commit

Permalink
Replace usage of FormatterServices.GetUninitializedObject on NET Core…
Browse files Browse the repository at this point in the history
… with RuntimeHelpers. Should remove dependency on System.Runtime.Serialization.Formatters?
  • Loading branch information
wasabii committed Feb 28, 2024
1 parent c6568da commit f43d460
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/IKVM.MSBuild.Tasks/IkvmJsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Text;

Expand Down Expand Up @@ -321,7 +322,11 @@ static Dictionary<string, T> CreateMemberNameDictionary<T>(T[] members) where T

static object ParseObject(Type type, string json)
{
#if NETFRAMEWORK
object instance = FormatterServices.GetUninitializedObject(type);
#else
object instance = RuntimeHelpers.GetUninitializedObject(type);
#endif

//The list is split into key/value pairs only, this means the split must be divisible by 2 to be valid JSON
List<string> elems = Split(json);
Expand Down
8 changes: 7 additions & 1 deletion src/IKVM.Runtime/JNI/JNIEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ Jeroen Frijters
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;

using IKVM.ByteCode.Text;

Expand Down Expand Up @@ -739,7 +741,11 @@ static jobject AllocObjectImpl(JNIEnv* pEnv, RuntimeJavaType wrapper)
return IntPtr.Zero;
}
wrapper.Finish();
return pEnv->MakeLocalRef(System.Runtime.Serialization.FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType));
#if NETFRAMEWORK
return pEnv->MakeLocalRef(FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType));
#else
return pEnv->MakeLocalRef(RuntimeHelpers.GetUninitializedObject(wrapper.TypeAsBaseType));
#endif
}
catch (RetargetableJavaException e)
{
Expand Down
4 changes: 4 additions & 0 deletions src/IKVM.Runtime/Java/Externs/sun/misc/Unsafe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,11 @@ public static object allocateInstance(object self, global::java.lang.Class cls)
throw x.ToJava();
}

#if NETFRAMEWORK
return FormatterServices.GetUninitializedObject(wrapper.TypeAsBaseType);
#else
return RuntimeHelpers.GetUninitializedObject(wrapper.TypeAsBaseType);
#endif
}

/// <summary>
Expand Down
20 changes: 15 additions & 5 deletions src/IKVM.Runtime/Java/Externs/sun/reflect/ReflectionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Jeroen Frijters
using System.Reflection;
#if !NO_REF_EMIT
using System.Reflection.Emit;
using System.Runtime.CompilerServices;

#endif
using System.Runtime.Serialization;
using System.Security;
Expand Down Expand Up @@ -282,7 +284,11 @@ internal SerializationConstructorAccessorImpl(global::java.lang.reflect.Construc
[SecuritySafeCritical]
public object newInstance(object[] args)
{
#if NETFRAMEWORK
var obj = FormatterServices.GetUninitializedObject(type);
#else
var obj = RuntimeHelpers.GetUninitializedObject(type);
#endif
if (mw != null)
mw.Invoke(obj, ConvertArgs(mw.DeclaringType.GetClassLoader(), mw.GetParameters(), args));

Expand Down Expand Up @@ -966,13 +972,17 @@ public object newInstance(object[] args)

}

private sealed class FastSerializationConstructorAccessorImpl : global::sun.reflect.ConstructorAccessor
sealed class FastSerializationConstructorAccessorImpl : global::sun.reflect.ConstructorAccessor
{

private static readonly MethodInfo GetTypeFromHandleMethod = typeof(Type).GetMethod("GetTypeFromHandle", new Type[] { typeof(RuntimeTypeHandle) });
private static readonly MethodInfo GetUninitializedObjectMethod = typeof(FormatterServices).GetMethod("GetUninitializedObject", new Type[] { typeof(Type) });
private delegate object InvokeCtor();
private InvokeCtor invoker;
static readonly MethodInfo GetTypeFromHandleMethod = typeof(Type).GetMethod(nameof(Type.GetTypeFromHandle), new[] { typeof(RuntimeTypeHandle) });
#if NETFRAMEWORK
static readonly MethodInfo GetUninitializedObjectMethod = typeof(FormatterServices).GetMethod(nameof(FormatterServices.GetUninitializedObject), new[] { typeof(Type) });
#else
static readonly MethodInfo GetUninitializedObjectMethod = typeof(RuntimeHelpers).GetMethod(nameof(RuntimeHelpers.GetUninitializedObject), new[] { typeof(Type) });
#endif
delegate object InvokeCtor();
InvokeCtor invoker;

internal FastSerializationConstructorAccessorImpl(global::java.lang.reflect.Constructor constructorToCall, global::java.lang.Class classToInstantiate)
{
Expand Down
7 changes: 7 additions & 0 deletions src/IKVM.Runtime/RuntimeAssemblyClassLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ Jeroen Frijters
using IKVM.Attributes;
using IKVM.Runtime.Syntax;

using System.Runtime.CompilerServices;


#if IMPORTER || EXPORTER
using IKVM.Reflection;

Expand Down Expand Up @@ -1253,7 +1256,11 @@ void InitializeJavaClassLoader(JavaClassLoaderConstructionInProgress jclcip, Typ
[System.Security.SecuritySafeCritical]
static object GetUninitializedObject(Type type)
{
#if NETFRAMEWORK
return FormatterServices.GetUninitializedObject(type);
#else
return RuntimeHelpers.GetUninitializedObject(type);
#endif
}

static void LoadCustomClassLoaderRedirects(RuntimeContext context)
Expand Down
7 changes: 1 addition & 6 deletions src/IKVM.Tools.Exporter/IkvmExporterContext.NetCore.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
#if NETCOREAPP

using System;
using System.IO;
using System.Reflection;
using System.Runtime.Loader;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

using Microsoft.Extensions.DependencyModel.Resolution;

using System.Text.Json;

namespace IKVM.Tools.Exporter
{

Expand Down

0 comments on commit f43d460

Please sign in to comment.