Skip to content

Add RegisterNatives overload for raw function pointers#1391

Merged
jonathanpeppers merged 4 commits intomainfrom
dev/simonrozsival/register-natives-intptr
Mar 19, 2026
Merged

Add RegisterNatives overload for raw function pointers#1391
jonathanpeppers merged 4 commits intomainfrom
dev/simonrozsival/register-natives-intptr

Conversation

@simonrozsival
Copy link
Member

Add JniNativeMethod struct matching JNI's JNINativeMethod layout (byte* Name, byte* Signature, IntPtr FunctionPointer) and a new RegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>) overload.

Calls the JNI RegisterNatives function pointer directly via the function table, bypassing delegate marshaling entirely. Zero allocations in the entire path.

Add end-to-end test with a dedicated Java class (RegisterNativesTestType) and an [UnmanagedCallersOnly] native callback that is registered via the new API, then called from Java to verify the result.

Add JniNativeMethod struct matching JNI's JNINativeMethod layout
(byte* Name, byte* Signature, IntPtr FunctionPointer) and a new
RegisterNatives(JniObjectReference, ReadOnlySpan<JniNativeMethod>)
overload.

Calls the JNI RegisterNatives function pointer directly via the
function table, bypassing delegate marshaling entirely. Zero
allocations in the entire path.

Add end-to-end test with a dedicated Java class (RegisterNativesTestType)
and an [UnmanagedCallersOnly] native callback that is registered via
the new API, then called from Java to verify the result.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 18, 2026 12:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link
Member

@jonathanpeppers jonathanpeppers left a comment

Choose a reason for hiding this comment

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

##[error]src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): Error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?)
D:\a\1\s\src\Java.Interop\Java.Interop\JniEnvironment.Types.cs(288,99): error CS1061: 'JniEnvironmentInfo' does not contain a definition for 'Invoker' and no accessible extension method 'Invoker' accepting a first argument of type 'JniEnvironmentInfo' could be found (are you missing a using directive or an assembly reference?) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): Error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(30,23): error RS0016: Symbol 'implicit constructor for 'JniNativeMethod'' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): Error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\Java.Interop\JniNativeMethodRegistration.cs(36,10): error RS0016: Symbol 'JniNativeMethod' is not part of the declared public API (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]
##[error]src\Java.Interop\PublicAPI.Unshipped.txt(16,1): Error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md)
D:\a\1\s\src\Java.Interop\PublicAPI.Unshipped.txt(16,1): error RS0017: Symbol 'Java.Interop.JniNativeMethod.JniNativeMethod(byte* name, byte* signature, System.IntPtr functionPointer) -> void' is part of the declared API, but is either not public or could not be found (https://github.com/dotnet/roslyn-analyzers/blob/main/src/PublicApiAnalyzers/PublicApiAnalyzers.Help.md) [D:\a\1\s\src\Java.Interop\Java.Interop.csproj]

simonrozsival and others added 3 commits March 19, 2026 09:09
- Fix CS1061: Use JNIEnv function table directly via (*((JNIEnv**)env))
  instead of info.Invoker which doesn't exist in the function pointers
  compilation path
- Fix RS0016: Add implicit parameterless constructor to PublicAPI
- Fix RS0016/RS0017: Use nint instead of System.IntPtr in PublicAPI
  (matches .NET 10 representation)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The Java class was not listed in the JavaInteropTestJar item group,
causing ClassNotFoundException at runtime.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@jonathanpeppers jonathanpeppers merged commit 6363926 into main Mar 19, 2026
2 checks passed
@jonathanpeppers jonathanpeppers deleted the dev/simonrozsival/register-natives-intptr branch March 19, 2026 12:33
@simonrozsival simonrozsival added the copilot `copilot-cli` or other AIs were used to author this label Mar 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

copilot `copilot-cli` or other AIs were used to author this

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants