Skip to content

Commit

Permalink
Remove need for NativeAotValueManager.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonpryor committed Nov 9, 2023
1 parent ce850ca commit 6920e6c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions samples/Hello-NativeAOTFromJNI/JavaInteropRuntime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ static void init (IntPtr jnienv, IntPtr klass)
try {
var options = new JreRuntimeOptions {
EnvironmentPointer = jnienv,
TypeManager = new NativeAotTypeManager (),
ValueManager = new NativeAotValueManager (),
TypeManager = new NativeAotTypeManager (),
UseMarshalMemberBuilder = false,
};
runtime = options.CreateJreVM ();
}
Expand Down
26 changes: 0 additions & 26 deletions samples/Hello-NativeAOTFromJNI/NativeAotValueManager.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public JniMarshalMemberBuilder MarshalMemberBuilder {
return marshalMemberBuilder;
}
}
public bool UseMarshalMemberBuilder => marshalMemberBuilder != null;

[System.Diagnostics.CodeAnalysis.SuppressMessage ("Design", "CA1031:Do not catch general exception types", Justification = "the *.Export assemblies are optional, so we don't care when they cannot be loaded (they are presumably missing)")]
#if NET
Expand Down
4 changes: 4 additions & 0 deletions src/Java.Runtime.Environment/Java.Interop/JreTypeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public override void RegisterNativeMembers (JniType nativeClass, Type type, Read
return;
}

if (!Runtime.UseMarshalMemberBuilder) {
throw new NotSupportedException ("JniRuntime.MarshalMemberBuilder is required and not present.");
}

var toRegister = new JniMethodMap ();

AddInterfaceMethods (toRegister, type);
Expand Down
25 changes: 23 additions & 2 deletions src/Java.Runtime.Environment/Java.Interop/ManagedValueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,11 @@ public override void ActivatePeer (IJavaPeerable? self, JniObjectReference refer
var runtime = JniEnvironment.Runtime;

try {
var f = runtime.MarshalMemberBuilder.CreateConstructActivationPeerFunc (cinfo);
f (cinfo, reference, argumentValues);
if (runtime.UseMarshalMemberBuilder) {
ActivateViaMarshalMemberBuilder (runtime.MarshalMemberBuilder, reference, cinfo, argumentValues);
return;
}
ActivateViaReflection (reference, cinfo, argumentValues);
} catch (Exception e) {
var m = string.Format ("Could not activate {{ PeerReference={0} IdentityHashCode=0x{1} Java.Type={2} }} for managed type '{3}'.",
reference,
Expand All @@ -218,6 +221,24 @@ public override void ActivatePeer (IJavaPeerable? self, JniObjectReference refer
}
}

void ActivateViaMarshalMemberBuilder (JniRuntime.JniMarshalMemberBuilder builder, JniObjectReference reference, ConstructorInfo cinfo, object?[]? argumentValues)
{
var f = builder.CreateConstructActivationPeerFunc (cinfo);
f (cinfo, reference, argumentValues);
}

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

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

cinfo.Invoke (self, argumentValues);
}

public override List<JniSurfacedPeerInfo> GetSurfacedPeers ()
{
if (RegisteredInstances == null)
Expand Down

0 comments on commit 6920e6c

Please sign in to comment.