-
Notifications
You must be signed in to change notification settings - Fork 568
[Export] Fix dynamic export array callbacks #11428
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jonathanpeppers
merged 2 commits into
main
from
dev/simonrozsival/mono-android-export-array-callbacks
May 22, 2026
+20
−22
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -167,7 +167,7 @@ public CodeExpression PrepareCallback (CodeExpression arg) | |
| return GetCallbackPrep (type, parameter_kind, arg); | ||
| } | ||
|
|
||
| public CodeExpression CleanupCallback (CodeExpression arg, CodeExpression orgArg) | ||
| public CodeItem CleanupCallback (CodeExpression arg, CodeExpression orgArg) | ||
| { | ||
| return GetCallbackCleanup (type, arg, orgArg); | ||
| } | ||
|
|
@@ -190,7 +190,7 @@ public static CodeExpression GetCallbackPrep (Type type, ExportParameterKind pki | |
| // ArraySymbol: | ||
| // return new string[] { String.Format ("{0}[] {1} = ({0}[]) JNIEnv.GetArray ({2}, JniHandleOwnership.DoNotTransfer, typeof ({3}));", ElementType, var_name, SymbolTable.GetNativeName (var_name), sym.FullName) }; | ||
| case SymbolKind.Array: | ||
| return new CodeMethodCall (jnienv_getarray, arg, do_not_transfer_literal, new CodeLiteral (type)).CastTo (type); | ||
| return new CodeMethodCall (jnienv_getarray.MakeGenericMethod (type.GetElementType ()), arg).CastTo (type); | ||
|
|
||
| // CharSequenceSymbol: | ||
| // return new string[] { String.Format ("Java.Lang.ICharSequence {0} = Java.Lang.Object.GetObject<Java.Lang.ICharSequence> ({1}, JniHandleOwnership.DoNotTransfer);", var_name, SymbolTable.GetNativeName (var_name)) }; | ||
|
|
@@ -239,7 +239,7 @@ public static CodeExpression GetCallbackPrep (Type type, ExportParameterKind pki | |
| return arg; | ||
| } | ||
|
|
||
| public static CodeExpression GetCallbackCleanup (Type type, CodeExpression arg, CodeExpression orgArg) | ||
| public static CodeItem GetCallbackCleanup (Type type, CodeExpression arg, CodeExpression orgArg) | ||
| { | ||
| switch (GetKind (type)) { | ||
| // ArraySymbol: | ||
|
|
@@ -249,21 +249,28 @@ public static CodeExpression GetCallbackCleanup (Type type, CodeExpression arg, | |
| // return result; | ||
| case SymbolKind.Array: | ||
| MethodInfo copyArrayMethod; | ||
| switch (Type.GetTypeCode (type)) { | ||
| Type elementType = type.GetElementType (); | ||
| switch (Type.GetTypeCode (elementType)) { | ||
| case TypeCode.Empty: | ||
| case TypeCode.DBNull: | ||
| throw new NotSupportedException ("Only primitive types and IJavaObject is supported in array type in callback method parameter or return value"); | ||
| case TypeCode.Object: | ||
| if (typeof (IJavaObject).IsAssignableFrom (type)) | ||
| copyArrayMethod = typeof (JNIEnv).GetMethod ("CopyArray", new Type [] { typeof (IJavaObject), typeof (IntPtr) }); | ||
| if (typeof (IJavaObject).IsAssignableFrom (elementType)) | ||
| copyArrayMethod = typeof (JNIEnv).GetMethod ("CopyArray", new Type [] { typeof (IJavaObject[]), typeof (IntPtr) }); | ||
| else | ||
| goto case TypeCode.Empty; | ||
| break; | ||
| default: | ||
| copyArrayMethod = typeof (JNIEnv).GetMethod ("CopyArray", new Type [] { type, typeof (IntPtr) }); | ||
| break; | ||
| } | ||
| return new CodeWhen (arg.IsNull, arg, new CodeMethodCall (copyArrayMethod, arg, orgArg)); | ||
| if (copyArrayMethod == null) | ||
| throw new NotSupportedException ($"JNIEnv.CopyArray does not support array type '{type}'."); | ||
| CodeBlock copyArrayBlock = new CodeBlock (); | ||
| copyArrayBlock.Add (new CodeMethodCall (copyArrayMethod, arg, orgArg)); | ||
| return new CodeIf (CodeExpression.Not (arg.IsNull)) { | ||
| TrueBlock = copyArrayBlock, | ||
| }; | ||
|
|
||
| // CharSequenceSymbol: | ||
| // return new string[] { String.Format ("Java.Lang.ICharSequence {0} = Java.Lang.Object.GetObject<Java.Lang.ICharSequence> ({1}, JniHandleOwnership.DoNotTransfer);", var_name, SymbolTable.GetNativeName (var_name)) }; | ||
|
|
@@ -711,10 +718,12 @@ CodeMethod GenerateNativeCallbackDelegate (string generatedMethodName) | |
| // sw.WriteLine ("{0}\t{1} {2};", indent, Parameters.HasCleanup ? RetVal.NativeType + " __ret =" : "return", RetVal.ToNative (opt, call)); | ||
| var callArgs = new List<CodeExpression> (); | ||
| for (int i = 0; i < parameter_type_infos.Count; i++) { | ||
| if (parameter_type_infos [i].NeedsPrep) | ||
| callArgs.Add (parameter_type_infos [i].PrepareCallback (mgen.GetArg (i + 2))); | ||
| else | ||
| if (parameter_type_infos [i].NeedsPrep) { | ||
| var preparedArg = parameter_type_infos [i].PrepareCallback (mgen.GetArg (i + 2)); | ||
| callArgs.Add (builder.DeclareVariable (method.GetParameters () [i].ParameterType, preparedArg)); | ||
| } else { | ||
| callArgs.Add (parameter_type_infos [i].FromNative (mgen.GetArg (i + 2))); | ||
| } | ||
| } | ||
| CodeMethodCall call; | ||
| if (method.IsStatic) | ||
|
|
@@ -740,7 +749,7 @@ CodeMethod GenerateNativeCallbackDelegate (string generatedMethodName) | |
| // sw.WriteLine ("{0}\t{1}", indent, cleanup); | ||
| var callbackCleanup = new List<CodeStatement> (); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 💡 Code organization — Rule: Remove unused code |
||
| for (int i = 0; i < parameter_type_infos.Count; i++) | ||
| builder.CurrentBlock.Add (parameter_type_infos [i].CleanupCallback (callArgs [i], mgen.GetArg (i))); | ||
| builder.CurrentBlock.Add (parameter_type_infos [i].CleanupCallback (callArgs [i], mgen.GetArg (i + 2))); | ||
|
|
||
| //if (!IsVoid && Parameters.HasCleanup) | ||
| // sw.WriteLine ("{0}\treturn __ret;", indent); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.