Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ static void init (IntPtr jnienv, IntPtr klass)
var options = new NativeAotRuntimeOptions {
EnvironmentPointer = jnienv,
TypeManager = typeManager,
ValueManager = new NativeAotValueManager (typeManager),
ValueManager = new ManagedValueManager (),
UseMarshalMemberBuilder = false,
JniGlobalReferenceLogWriter = settings.GrefLog,
JniLocalReferenceLogWriter = settings.LrefLog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static NativeAotRuntimeOptions CreateJreVM (NativeAotRuntimeOptions builder)
builder.TypeManager ??= new NativeAotTypeManager ();
#endif // NET

builder.ValueManager ??= new NativeAotValueManager (builder.TypeManager);
builder.ValueManager ??= new ManagedValueManager ();
builder.ObjectReferenceManager ??= new ManagedObjectReferenceManager (builder.JniGlobalReferenceLogWriter, builder.JniLocalReferenceLogWriter);

if (builder.InvocationPointer != IntPtr.Zero || builder.EnvironmentPointer != IntPtr.Zero)
Expand Down
14 changes: 8 additions & 6 deletions src/Mono.Android/Android.Runtime/AndroidRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ class AndroidRuntime : JniRuntime {
internal AndroidRuntime (IntPtr jnienv,
IntPtr vm,
IntPtr classLoader,
IntPtr classLoader_loadClass,
JniRuntime.JniTypeManager? typeManager,
JniRuntime.JniValueManager? valueManager,
bool jniAddNativeMethodRegistrationAttributePresent)
: base (new AndroidRuntimeOptions (jnienv,
vm,
classLoader,
classLoader_loadClass,
typeManager,
valueManager,
jniAddNativeMethodRegistrationAttributePresent))
{
// This is not ideal, but we need to set this while the runtime is initializing but we can't do it directly from the `JNIEnvInit.Initialize` method, since
Expand Down Expand Up @@ -93,16 +95,16 @@ class AndroidRuntimeOptions : JniRuntime.CreationOptions {
public AndroidRuntimeOptions (IntPtr jnienv,
IntPtr vm,
IntPtr classLoader,
IntPtr classLoader_loadClass,
JniRuntime.JniTypeManager? typeManager,
JniRuntime.JniValueManager? valueManager,
bool jniAddNativeMethodRegistrationAttributePresent)
{
EnvironmentPointer = jnienv;
ClassLoader = new JniObjectReference (classLoader, JniObjectReferenceType.Global);
ClassLoader_LoadClass_id= classLoader_loadClass;
InvocationPointer = vm;
ObjectReferenceManager = new AndroidObjectReferenceManager ();
TypeManager = new AndroidTypeManager (jniAddNativeMethodRegistrationAttributePresent);
ValueManager = new AndroidValueManager ();
TypeManager = typeManager ?? new AndroidTypeManager (jniAddNativeMethodRegistrationAttributePresent);
ValueManager = valueManager ?? new AndroidValueManager ();
UseMarshalMemberBuilder = false;
JniAddNativeMethodRegistrationAttributePresent = jniAddNativeMethodRegistrationAttributePresent;
}
Expand Down
11 changes: 10 additions & 1 deletion src/Mono.Android/Android.Runtime/JNIEnvInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Java.Interop;
using Java.Interop.Tools.TypeNameMappings;

using Microsoft.Android.Runtime;

namespace Android.Runtime
{
static internal class JNIEnvInit
Expand Down Expand Up @@ -108,7 +110,14 @@ internal static unsafe void Initialize (JnienvInitializeArgs* args)
java_class_loader = args->grefLoader;

BoundExceptionType = (BoundExceptionType)args->ioExceptionType;
androidRuntime = new AndroidRuntime (args->env, args->javaVm, args->grefLoader, args->Loader_loadClass, args->jniAddNativeMethodRegistrationAttributePresent != 0);
androidRuntime = new AndroidRuntime (
args->env,
args->javaVm,
args->grefLoader,
null,
RuntimeType != DotNetRuntimeType.MonoVM ? new ManagedValueManager () : null,
args->jniAddNativeMethodRegistrationAttributePresent != 0
);
ValueManager = androidRuntime.ValueManager;

IsRunningOnDesktop = args->isRunningOnDesktop == 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@

namespace Microsoft.Android.Runtime;

class NativeAotValueManager : JniRuntime.JniValueManager
class ManagedValueManager : JniRuntime.JniValueManager
{
const DynamicallyAccessedMemberTypes Constructors = DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors;

readonly JniRuntime.JniTypeManager TypeManager;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weird, I guess this is unused now.

Dictionary<int, List<IJavaPeerable>>? RegisteredInstances = new Dictionary<int, List<IJavaPeerable>>();

public NativeAotValueManager(JniRuntime.JniTypeManager typeManager) =>
TypeManager = typeManager;
internal ManagedValueManager ()
{
}

public override void WaitForGCBridgeProcessing ()
{
Expand All @@ -33,7 +33,7 @@ public override void WaitForGCBridgeProcessing ()
public override void CollectPeers ()
{
if (RegisteredInstances == null)
throw new ObjectDisposedException (nameof (NativeAotValueManager));
throw new ObjectDisposedException (nameof (ManagedValueManager));

var peers = new List<IJavaPeerable> ();

Expand Down Expand Up @@ -62,7 +62,7 @@ public override void CollectPeers ()
public override void AddPeer (IJavaPeerable value)
{
if (RegisteredInstances == null)
throw new ObjectDisposedException (nameof (NativeAotValueManager));
throw new ObjectDisposedException (nameof (ManagedValueManager));

var r = value.PeerReference;
if (!r.IsValid)
Expand Down Expand Up @@ -127,7 +127,7 @@ void WarnNotReplacing (int key, IJavaPeerable ignoreValue, IJavaPeerable keepVal
public override IJavaPeerable? PeekPeer (JniObjectReference reference)
{
if (RegisteredInstances == null)
throw new ObjectDisposedException (nameof (NativeAotValueManager));
throw new ObjectDisposedException (nameof (ManagedValueManager));

if (!reference.IsValid)
return null;
Expand All @@ -153,7 +153,7 @@ void WarnNotReplacing (int key, IJavaPeerable ignoreValue, IJavaPeerable keepVal
public override void RemovePeer (IJavaPeerable value)
{
if (RegisteredInstances == null)
throw new ObjectDisposedException (nameof (NativeAotValueManager));
throw new ObjectDisposedException (nameof (ManagedValueManager));

if (value == null)
throw new ArgumentNullException (nameof (value));
Expand Down Expand Up @@ -230,20 +230,25 @@ public override void ActivatePeer (IJavaPeerable? self, JniObjectReference refer

void ActivateViaReflection (JniObjectReference reference, ConstructorInfo cinfo, object?[]? argumentValues)
{
var declType = cinfo.DeclaringType ?? throw new NotSupportedException ("Do not know the type to create!");
var declType = GetDeclaringType (cinfo);

#pragma warning disable IL2072
var self = (IJavaPeerable) System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject (declType);
#pragma warning restore IL2072
self.SetPeerReference (reference);

cinfo.Invoke (self, argumentValues);

[UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "🤷‍♂️")]
[return: DynamicallyAccessedMembers (Constructors)]
Type GetDeclaringType (ConstructorInfo cinfo) =>
cinfo.DeclaringType ?? throw new NotSupportedException ("Do not know the type to create!");
}

public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()
{
if (RegisteredInstances == null)
throw new ObjectDisposedException (nameof (NativeAotValueManager));
throw new ObjectDisposedException (nameof (ManagedValueManager));

lock (RegisteredInstances) {
var peers = new List<JniSurfacedPeerInfo> (RegisteredInstances.Count);
Expand Down
1 change: 1 addition & 0 deletions src/Mono.Android/Mono.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@
<Compile Include="Java.Util.Concurrent.Atomic\AtomicInteger.cs" />
<Compile Include="Java.Util.Concurrent.Atomic\AtomicLong.cs" />
<Compile Include="Javax.Microedition.Khronos.Egl\EGLContext.cs" />
<Compile Include="Microsoft.Android.Runtime\ManagedValueManager.cs" />
<Compile Include="Org.Apache.Http.Impl.Conn\DefaultClientConnection.cs" />
<Compile Include="Org.Apache.Http.Impl.Cookie\BasicClientCookie.cs" />
<Compile Include="System.Drawing/PointConverter.cs" />
Expand Down
Loading