xamarin-macios/runtime/delegates.t4

445 строки
12 KiB
Plaintext
Исходник Обычный вид История

2016-04-21 15:19:32 +03:00
<# // vim: set filetype=cs :
#>
<#@ template language="C#" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#
var delegates = new XDelegates {
new XDelegate ("void", "void", "xamarin_register_nsobject",
"MonoObject *", "IntPtr", "managed_obj",
"id", "IntPtr", "native_obj"
) {
WrappedManagedFunction = "RegisterNSObject",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_register_assembly",
"MonoReflectionAssembly *", "IntPtr", "assembly"
) {
WrappedManagedFunction = "RegisterAssembly",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_throw_ns_exception",
"NSException *", "IntPtr", "exc"
) {
WrappedManagedFunction = "ThrowNSException",
ExceptionHandling = false,
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
Use ExceptionDispatchInfo to keep stack traces when unhandled exceptions are marshaled. Improves bug #45742 a bit. (#1040) Example code: public override void ViewDidLoad () { throw new Exception ("USELESS"); } Current output with unhandled exceptions that are marshaled: Unhandled Exception: System.Exception: USELESS at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 [ERROR] FATAL UNHANDLED EXCEPTION: UselessExceptions.CustomException: USELESS at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 Note how the managed frame where the exception was thrown does not show up. This is because we technically catch exceptions when marshaling them, and then later we call mono_raise_exception to raise the same exception. Unfortunately that leads to overwriting the initial stack trace, and we end up with a stack trace that does not include the location where the original exception was thrown. So instead of calling mono_raise_exception to rethrow the same exception, we use ExceptionDispatchInfo, which is able to capture the stack trace for both the original exception and the new exception, producing the following output: System.Exception: USELESS at UselessExceptions.ViewController.ViewDidLoad () [0x0000c] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/ViewController.cs:27 --- End of stack trace from previous location where exception was thrown --- at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 [ERROR] FATAL UNHANDLED EXCEPTION: UselessExceptions.CustomException: USELESS at UselessExceptions.ViewController.ViewDidLoad () [0x0000c] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/ViewController.cs:27 --- End of stack trace from previous location where exception was thrown --- at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 Incidently this is how Xamarin.Android does it [1]. [1]: https://github.com/xamarin/Java.Interop/commit/9387f2fe16300ea8eb3d5270e50f0513d251bd62 https://bugzilla.xamarin.com/show_bug.cgi?id=45742
2016-10-27 21:03:11 +03:00
new XDelegate ("void", "void", "xamarin_rethrow_managed_exception",
"guint32", "uint", "original_exception_gchandle"
) {
WrappedManagedFunction = "RethrowManagedException",
OnlyDynamicUsage = false,
},
Use ExceptionDispatchInfo to keep stack traces when unhandled exceptions are marshaled. Improves bug #45742 a bit. (#1040) Example code: public override void ViewDidLoad () { throw new Exception ("USELESS"); } Current output with unhandled exceptions that are marshaled: Unhandled Exception: System.Exception: USELESS at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 [ERROR] FATAL UNHANDLED EXCEPTION: UselessExceptions.CustomException: USELESS at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 Note how the managed frame where the exception was thrown does not show up. This is because we technically catch exceptions when marshaling them, and then later we call mono_raise_exception to raise the same exception. Unfortunately that leads to overwriting the initial stack trace, and we end up with a stack trace that does not include the location where the original exception was thrown. So instead of calling mono_raise_exception to rethrow the same exception, we use ExceptionDispatchInfo, which is able to capture the stack trace for both the original exception and the new exception, producing the following output: System.Exception: USELESS at UselessExceptions.ViewController.ViewDidLoad () [0x0000c] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/ViewController.cs:27 --- End of stack trace from previous location where exception was thrown --- at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 [ERROR] FATAL UNHANDLED EXCEPTION: UselessExceptions.CustomException: USELESS at UselessExceptions.ViewController.ViewDidLoad () [0x0000c] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/ViewController.cs:27 --- End of stack trace from previous location where exception was thrown --- at (wrapper managed-to-native) AppKit.NSApplication:NSApplicationMain (int,string[]) at AppKit.NSApplication.Main (System.String[] args) [0x00041] in /work/maccore/master/xamarin-macios/src/AppKit/NSApplication.cs:98 at UselessExceptions.MainClass.Main (System.String[] args) [0x00007] in /Users/rolf/Downloads/filed-bug-test-cases-master/Xamarin/bxc45742/Main.cs:10 Incidently this is how Xamarin.Android does it [1]. [1]: https://github.com/xamarin/Java.Interop/commit/9387f2fe16300ea8eb3d5270e50f0513d251bd62 https://bugzilla.xamarin.com/show_bug.cgi?id=45742
2016-10-27 21:03:11 +03:00
new XDelegate ("int", "int", "xamarin_create_ns_exception",
"NSException *", "IntPtr", "exc"
) {
WrappedManagedFunction = "CreateNSException",
OnlyDynamicUsage = false,
},
new XDelegate ("NSException *", "IntPtr", "xamarin_unwrap_ns_exception",
"uint32_t", "uint", "exc_handle"
) {
WrappedManagedFunction = "UnwrapNSException",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_block_wrapper_creator",
"MonoObject *", "IntPtr", "method",
"unsigned long", "uint", "parameter"
) {
WrappedManagedFunction = "GetBlockWrapperCreator",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_create_block_proxy",
"MonoObject *", "IntPtr", "method",
"void *", "IntPtr", "block"
) {
WrappedManagedFunction = "CreateBlockProxy",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("id", "IntPtr", "xamarin_create_delegate_proxy",
"MonoObject *", "IntPtr", "method",
Optimize calls to BlockLiteral.SetupBlock to inject the block signature. (#3391) * [linker] Optimize calls to BlockLiteral.SetupBlock to inject the block signature. Optimize calls to BlockLiteral.SetupBlock[Unsafe] to calculate the block signature at build time, and inject it into the call site. This makes block invocations 10-15x faster (I've added tests that asserts at least an 8x increase). It's also required in order to be able to remove the dynamic registrar code in the future (since calculating the block signature at runtime requires the dynamic registrar). * [mtouch/mmp] Add support for reporting errors/warnings that point to the code line causing the error/warning. Add support for reporting errors/warnings that point to the code line causing the error/warning by adding ErrorHelper overloads that take the exact instruction to report (previously we defaulted to the first line/instruction in a method). * [tests] Add support for asserting filename/linenumber in warning messages. * Make all methods that manually create BlockLiterals optimizable. * [tests] Create a BaseOptimizeGeneratedCodeTest test that's included in both XI's and XM's link all test. * [tests] Add link all test (for both XI and XM) to test the BlockLiteral.SetupBlock optimization. * [tests] Add mtouch/mmp tests for the BlockLiteral.SetupBlock optimization. * [tests][linker] Make the base test class abstract, so tests in the base class aren't executed twice. * [tests][linker] Don't execute linkall-only tests in linksdk. The optimization tests only apply when the test assembly is linked, and that only happens in linkall, so exclude those tests in linksdk. * [tests][mmptest] Update test according to mmp changes. Fixes these test failures: 1) Failed : Xamarin.MMP.Tests.MMPTests.MM0132("inline-runtime-arch") The warning 'MM0132: Unknown optimization: 'inline-runtime-arch'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size.' was not found in the output: Message #1 did not match: actual: 'Unknown optimization: 'inline-runtime-arch'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size, blockliteral-setupblock.' expected: 'Unknown optimization: 'inline-runtime-arch'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size.' Message #2 did not match: actual: 'Unknown optimization: 'inline-runtime-arch'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size, blockliteral-setupblock.' expected: 'Unknown optimization: 'inline-runtime-arch'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size.' 2) Failed : Xamarin.MMP.Tests.MMPTests.MM0132("foo") The warning 'MM0132: Unknown optimization: 'foo'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size.' was not found in the output: Message #1 did not match: actual: 'Unknown optimization: 'foo'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size, blockliteral-setupblock.' expected: 'Unknown optimization: 'foo'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size.' Message #2 did not match: actual: 'Unknown optimization: 'foo'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size, blockliteral-setupblock.' expected: 'Unknown optimization: 'foo'. Valid optimizations are: remove-uithread-checks, dead-code-elimination, inline-isdirectbinding, inline-intptr-size.' * [tests][linker] Fix typo. Fixes this test failure: 1) SetupBlock_CustomDelegate (Linker.Shared.BaseOptimizeGeneratedCodeTest.SetupBlock_CustomDelegate) Counter Expected: 1 But was: 2 * [registrar] Minor adjustment to error message to match previous (and better) behavior. Fixes this test failure: 1) Failed : Xamarin.Registrar.GenericType_WithInvalidParameterTypes The error 'MT4136: The registrar cannot marshal the parameter type 'System.Collections.Generic.List`1<U>' of the parameter 'arg' in the method 'Open`1.Bar(System.Collections.Generic.List`1<U>)'' was not found in the output: Message #1 did not match: actual: 'The registrar cannot marshal the parameter type 'System.Collections.Generic.List`1<Foundation.NSObject>' of the parameter 'arg' in the method 'Open`1.Bar(System.Collections.Generic.List`1<U>)'' expected: 'The registrar cannot marshal the parameter type 'System.Collections.Generic.List`1<U>' of the parameter 'arg' in the method 'Open`1.Bar(System.Collections.Generic.List`1<U>)'' * [docs] mmp shows MM errors/warnings. * [docs] Improve according to reviews. * [tests] Fix merge failure causing test duplication.
2018-02-06 09:08:15 +03:00
"MonoObject *", "IntPtr", "block",
"const char *", "IntPtr", "signature",
"unsigned int", "uint", "token_ref"
) {
WrappedManagedFunction = "CreateDelegateProxy",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_register_entry_assembly",
"MonoReflectionAssembly *", "IntPtr", "assembly"
) {
WrappedManagedFunction = "RegisterEntryAssembly",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_class",
"Class", "IntPtr", "ptr"
) {
WrappedManagedFunction = "GetClass",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_selector",
"SEL", "IntPtr", "ptr"
) {
WrappedManagedFunction = "GetSelector",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_get_method_for_selector",
2016-04-21 15:19:32 +03:00
"Class", "IntPtr", "cls",
"SEL", "IntPtr", "sel",
"bool", "bool", "is_static",
"MethodDescription *", "IntPtr", "desc"
) {
WrappedManagedFunction = "GetMethodForSelector",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_nsobject",
"id", "IntPtr", "obj"
) {
WrappedManagedFunction = "GetNSObjectWrapped",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("bool", "bool", "xamarin_has_nsobject",
"id", "IntPtr", "obj"
) {
WrappedManagedFunction = "HasNSObject",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("id", "IntPtr", "xamarin_get_handle_for_inativeobject",
"MonoObject *", "IntPtr", "obj"
) {
WrappedManagedFunction = "GetHandleForINativeObject",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_unregister_nsobject",
"id", "IntPtr", "native_obj",
"MonoObject *", "IntPtr", "managed_obj"
) {
WrappedManagedFunction = "UnregisterNSObject",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_try_get_or_construct_nsobject",
"id", "IntPtr", "obj"
) {
WrappedManagedFunction = "TryGetOrConstructNSObjectWrapped",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_inative_object_dynamic",
"id", "IntPtr", "obj",
"bool", "bool", "owns",
"void *", "IntPtr", "type"
) {
WrappedManagedFunction = "GetINativeObject_Dynamic",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
[registrar] Use metadata tokens instead of strings to find types and methods. (#1085) Use metadata tokens instead of strings to find types and methods. This makes the code to find methods more compact (a lot less strings in the executable, and additionally in most cases a compact representation (32-bit integer) of the corresponding metadata token and additional information can be used, which results in less executable code (fewer parameters to methods, etc)), resulting in smaller executables. Size savings are around 200kb for dont link apps, and 20-60kb for linked apps (this obviously varies a lot depending on how much has to registered by the registrar). | | Before | After | Diff | |----------------|--------------:|--------------:|------------------:| | dontlink/32bit | 102.810.144 | 102.609.456 | -200.688 = -0,20% | | dontlink/64bit | 107.420.576 | 107.221.792 | -198.784 = -0,19% | | linksdk/32bit | 40.957.296 | 40.936.864 | -20.432 = -0,05% | | linksdk/64bit | 43.113.136 | 43.093.936 | -19.200 = -0,04% | | linkall/32bit | 38.410.032 | 38.348.288 |  -61.744 = -0,16% | | linkall/64bit | 40.315.200 | 40.267.344 | -47.856 = -0,12% | Additionally I've removed the `lazy_map` dictionary, which we populated at startup and was used to map between Class instances and the corresponding managed type's FullName, and instead iterate over a native array of Class -> metadata token mappings whenever we need to look up the managed type for a certain Class instance. This is slightly slower for each type we need to look up (for a non-linked app there might be a 2000-3000 entries in the native array, which would be iterated instead of using a hashtable lookup), but it's only done once per type and there's a significant startup memory improvement. For a non-linked test app I get the following using the Xamarin profiler: | | Before | After | Diff | |-------------------|--------:|--------:|----------------:| | Memory allocated | 2,8 MB | 2,4 MB | -0,4 MB = -14 % | | Objects allocated | 43678 | 38463 | -5215 = -12 % | | Private bytes | 26,6 MB | 24,4 MB | -2,2 MB = -8,3% | | Working set | 26,6 MB | 24,4 MB | -2,2 MB = -8,3% |
2016-11-01 21:34:56 +03:00
new XDelegate ("MonoReflectionMethod *", "IntPtr", "xamarin_get_method_from_token",
"unsigned int", "uint", "token_ref"
) {
WrappedManagedFunction = "GetMethodFromToken",
OnlyDynamicUsage = false,
},
[registrar] Use metadata tokens instead of strings to find types and methods. (#1085) Use metadata tokens instead of strings to find types and methods. This makes the code to find methods more compact (a lot less strings in the executable, and additionally in most cases a compact representation (32-bit integer) of the corresponding metadata token and additional information can be used, which results in less executable code (fewer parameters to methods, etc)), resulting in smaller executables. Size savings are around 200kb for dont link apps, and 20-60kb for linked apps (this obviously varies a lot depending on how much has to registered by the registrar). | | Before | After | Diff | |----------------|--------------:|--------------:|------------------:| | dontlink/32bit | 102.810.144 | 102.609.456 | -200.688 = -0,20% | | dontlink/64bit | 107.420.576 | 107.221.792 | -198.784 = -0,19% | | linksdk/32bit | 40.957.296 | 40.936.864 | -20.432 = -0,05% | | linksdk/64bit | 43.113.136 | 43.093.936 | -19.200 = -0,04% | | linkall/32bit | 38.410.032 | 38.348.288 |  -61.744 = -0,16% | | linkall/64bit | 40.315.200 | 40.267.344 | -47.856 = -0,12% | Additionally I've removed the `lazy_map` dictionary, which we populated at startup and was used to map between Class instances and the corresponding managed type's FullName, and instead iterate over a native array of Class -> metadata token mappings whenever we need to look up the managed type for a certain Class instance. This is slightly slower for each type we need to look up (for a non-linked app there might be a 2000-3000 entries in the native array, which would be iterated instead of using a hashtable lookup), but it's only done once per type and there's a significant startup memory improvement. For a non-linked test app I get the following using the Xamarin profiler: | | Before | After | Diff | |-------------------|--------:|--------:|----------------:| | Memory allocated | 2,8 MB | 2,4 MB | -0,4 MB = -14 % | | Objects allocated | 43678 | 38463 | -5215 = -12 % | | Private bytes | 26,6 MB | 24,4 MB | -2,2 MB = -8,3% | | Working set | 26,6 MB | 24,4 MB | -2,2 MB = -8,3% |
2016-11-01 21:34:56 +03:00
new XDelegate ("MonoReflectionMethod *", "IntPtr", "xamarin_get_generic_method_from_token",
"MonoObject *", "IntPtr", "obj",
"unsigned int", "uint", "token_ref"
) {
WrappedManagedFunction = "GetGenericMethodFromToken",
OnlyDynamicUsage = false,
},
[registrar] Use metadata tokens instead of strings to find types and methods. (#1085) Use metadata tokens instead of strings to find types and methods. This makes the code to find methods more compact (a lot less strings in the executable, and additionally in most cases a compact representation (32-bit integer) of the corresponding metadata token and additional information can be used, which results in less executable code (fewer parameters to methods, etc)), resulting in smaller executables. Size savings are around 200kb for dont link apps, and 20-60kb for linked apps (this obviously varies a lot depending on how much has to registered by the registrar). | | Before | After | Diff | |----------------|--------------:|--------------:|------------------:| | dontlink/32bit | 102.810.144 | 102.609.456 | -200.688 = -0,20% | | dontlink/64bit | 107.420.576 | 107.221.792 | -198.784 = -0,19% | | linksdk/32bit | 40.957.296 | 40.936.864 | -20.432 = -0,05% | | linksdk/64bit | 43.113.136 | 43.093.936 | -19.200 = -0,04% | | linkall/32bit | 38.410.032 | 38.348.288 |  -61.744 = -0,16% | | linkall/64bit | 40.315.200 | 40.267.344 | -47.856 = -0,12% | Additionally I've removed the `lazy_map` dictionary, which we populated at startup and was used to map between Class instances and the corresponding managed type's FullName, and instead iterate over a native array of Class -> metadata token mappings whenever we need to look up the managed type for a certain Class instance. This is slightly slower for each type we need to look up (for a non-linked app there might be a 2000-3000 entries in the native array, which would be iterated instead of using a hashtable lookup), but it's only done once per type and there's a significant startup memory improvement. For a non-linked test app I get the following using the Xamarin profiler: | | Before | After | Diff | |-------------------|--------:|--------:|----------------:| | Memory allocated | 2,8 MB | 2,4 MB | -0,4 MB = -14 % | | Objects allocated | 43678 | 38463 | -5215 = -12 % | | Private bytes | 26,6 MB | 24,4 MB | -2,2 MB = -8,3% | | Working set | 26,6 MB | 24,4 MB | -2,2 MB = -8,3% |
2016-11-01 21:34:56 +03:00
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_inative_object_static",
"id", "IntPtr", "obj",
"bool", "bool", "owns",
"unsigned int", "uint", "iface_token_ref",
"unsigned int", "uint", "implementation_token_ref"
) {
WrappedManagedFunction = "GetINativeObject_Static",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("MonoObject *", "IntPtr", "xamarin_get_nsobject_with_type",
"id", "IntPtr", "obj",
"void *", "IntPtr", "type",
"int32_t *", "out bool", "created"
) {
WrappedManagedFunction = "GetNSObjectWithType",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_dispose",
"MonoObject *", "IntPtr", "mobj"
) {
WrappedManagedFunction = "Dispose",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("bool", "bool", "xamarin_is_parameter_transient",
"MonoReflectionMethod *", "IntPtr", "method",
"int", "int", "parameter"
) {
WrappedManagedFunction = "IsParameterTransient",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("bool", "bool", "xamarin_is_parameter_out",
"MonoReflectionMethod *", "IntPtr", "method",
"unsigned long", "uint", "parameter"
) {
WrappedManagedFunction = "IsParameterOut",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("void", "void", "xamarin_get_method_and_object_for_selector",
2016-04-21 15:19:32 +03:00
"Class", "IntPtr", "cls",
"SEL", "IntPtr", "sel",
"bool", "bool", "is_static",
2016-04-21 15:19:32 +03:00
"id", "IntPtr", "obj",
"MonoObject **", "ref IntPtr", "mthis",
"MethodDescription *", "IntPtr", "desc"
) {
WrappedManagedFunction = "GetMethodAndObjectForSelector",
OnlyDynamicUsage = true,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("guint32", "int", "xamarin_create_product_exception_for_error",
2016-04-21 15:19:32 +03:00
"int", "int", "code",
"guint32", "uint", "inner_exception_gchandle",
2016-04-21 15:19:32 +03:00
"const char *", "string", "message"
) {
WrappedManagedFunction = "CreateProductException",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("char *", "IntPtr", "xamarin_reflection_type_get_full_name",
"MonoReflectionType *", "IntPtr", "type"
) {
WrappedManagedFunction = "TypeGetFullName",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
new XDelegate ("char *", "IntPtr", "xamarin_lookup_managed_type_name",
"Class", "IntPtr", "klass"
) {
WrappedManagedFunction = "LookupManagedTypeName",
OnlyDynamicUsage = false,
},
new XDelegate ("enum MarshalManagedExceptionMode", "MarshalManagedExceptionMode", "xamarin_on_marshal_managed_exception",
"int", "int", "exception"
) {
WrappedManagedFunction = "OnMarshalManagedException",
OnlyDynamicUsage = false,
},
new XDelegate ("enum MarshalObjectiveCExceptionMode", "MarshalObjectiveCExceptionMode", "xamarin_on_marshal_objectivec_exception",
"id", "IntPtr", "exception",
"bool", "bool", "throwManagedAsDefault"
) {
WrappedManagedFunction = "OnMarshalObjectiveCException",
OnlyDynamicUsage = false,
},
new XDelegate ("NSString *", "IntPtr", "xamarin_convert_smart_enum_to_nsstring",
"void *", "IntPtr", "value"
) {
WrappedManagedFunction = "ConvertSmartEnumToNSString",
OnlyDynamicUsage = true,
},
new XDelegate ("void *", "IntPtr", "xamarin_convert_nsstring_to_smart_enum",
"NSString *", "IntPtr", "value",
"MonoReflectionType *", "IntPtr", "type"
) {
WrappedManagedFunction = "ConvertNSStringToSmartEnum",
OnlyDynamicUsage = true,
},
new XDelegate ("int32_t", "int", "xamarin_create_runtime_exception",
"int32_t", "int", "code",
"const char *", "IntPtr", "message"
) {
WrappedManagedFunction = "CreateRuntimeException",
OnlyDynamicUsage = false,
},
2016-04-21 15:19:32 +03:00
};
delegates.CalculateLengths ();
#><#+
class Arg
{
public string CType;
public string MType;
public string Name;
}
class XDelegates : List<XDelegate>
{
public void CalculateLengths ()
{
foreach (var x in this) {
MaxEntryPointLength = Math.Max (MaxEntryPointLength, x.EntryPoint.Length);
MaxCReturnTypeLength = Math.Max (MaxCReturnTypeLength, x.CReturnType.Length);
x.Delegates = this;
}
}
public int MaxEntryPointLength;
public int MaxCReturnTypeLength;
}
class XDelegate
{
public string CReturnType;
public string MReturnType;
public string EntryPoint;
public List<Arg> Arguments;
public string WrappedManagedFunction;
public bool ExceptionHandling = true;
// Detemines whether the function is only used by the dynamic registrar (in which case we might be able to link the function away if the static registrar is being used)
public bool OnlyDynamicUsage;
2016-04-21 15:19:32 +03:00
public XDelegates Delegates;
public XDelegate (string cReturnType, string mReturnType, string entryPoint, params string [] arguments)
{
CReturnType = cReturnType;
MReturnType = mReturnType;
EntryPoint = entryPoint;
if (arguments == null || arguments.Length == 0)
return;
if (arguments.Length % 3 != 0)
throw new Exception (string.Format ("Export arguments params must be a multiple of 3 to form a set of (c type, managed name, name) triples for {0}", entryPoint));
if (!entryPoint.StartsWith ("xamarin_"))
throw new Exception ("All entry points must start with 'xamarin_'");
Arguments = new List<Arg> ();
for (var i = 0; i < arguments.Length; i += 3)
Arguments.Add (new Arg {
CType = arguments [i],
MType = arguments [i + 1],
Name = arguments [i + 2]
});
}
public string SimpleEntryPoint {
get {
return EntryPoint.Substring ("xamarin_".Length);
}
}
public string AlignEntryPoint {
get {
return new string (' ', Delegates.MaxEntryPointLength - EntryPoint.Length);
}
}
public string AlignCReturnType {
get {
return new string (' ', Delegates.MaxCReturnTypeLength - CReturnType.Length);
}
}
string CFormatArgs (string empty, bool nameOnly)
{
if (Arguments == null || Arguments.Count == 0)
return empty;
var builder = new StringBuilder ();
foreach (var arg in Arguments) {
if (!nameOnly) {
builder.Append (arg.CType);
builder.Append (' ');
}
builder.Append (arg.Name);
builder.Append (", ");
}
builder.Length -= 2;
if (ExceptionHandling) {
if (nameOnly) {
builder.Append (", exception_gchandle");
} else {
builder.Append (", guint32 *exception_gchandle");
}
}
2016-04-21 15:19:32 +03:00
return builder.ToString ();
}
string MFormatArgs (string empty, bool nameOnly)
{
if (Arguments == null || Arguments.Count == 0)
return empty;
var builder = new StringBuilder ();
foreach (var arg in Arguments) {
if (!nameOnly) {
builder.Append (arg.MType);
builder.Append (' ');
} else if (arg.MType.StartsWith ("out ")) {
builder.Append ("out ");
} else if (arg.MType.StartsWith ("ref ")) {
builder.Append ("ref ");
}
builder.Append (arg.Name);
builder.Append (", ");
}
builder.Length -= 2;
if (ExceptionHandling) {
if (nameOnly) {
// nothing to do
} else {
builder.Append (", out int exception_gchandle");
}
}
2016-04-21 15:19:32 +03:00
return builder.ToString ();
}
public string CArgumentSignature {
get { return CFormatArgs ("void", nameOnly: false); }
}
public string CArgumentNames {
get { return CFormatArgs (String.Empty, nameOnly: true); }
}
public string MArgumentSignature {
get { return MFormatArgs ("", nameOnly: false); }
}
public string MArgumentNames {
get { return MFormatArgs (String.Empty, nameOnly: true); }
}
}
#>