[Mono-dev] NObjective. Fast .NET <==> objc bridge

Eugeny Grishul eugeny.grishul at gmail.com
Sun Nov 9 11:19:20 EST 2008


Hi guys.

Recently I started project called NObjective which main goal is create
high-performance and flexible library with lowest memory/cpu overheads which
provides ability to bridge runtimes in efficient ways. Currently it supports
codegeneration so hundreds of objc classes can be easily exported to .NET
runtime via transparent proxies. Also you can export your own .NET classes
to objc runtime.

See results of performance test ( you can find it in sources ):
http://code.google.com/p/objcmapper/

Also see usage:
http://code.google.com/p/objcmapper/wiki/Usage

But I have some issues with mono JIT:
1) My libary uses cecil assebly instrumentation to create structs that
inherited from non-System.ValueType class. Mono JIT is crashed on boxing of
such structs.
// it will be normally executed
Console.WriteLine( NSNumber.numberWithInt_( 10 ).ToString() );
// crash
Console.WriteLine( NSNumber.numberWithInt_( 10 ) );

Stacktrace:

  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke
(object,object[],System.Exception&) <0x00004>
  at (wrapper managed-to-native) System.Reflection.MonoMethod.InternalInvoke
(object,object[],System.Exception&) <0xffffffff>
  at System.Reflection.MonoMethod.Invoke
(object,System.Reflection.BindingFlags,System.Reflection.Binder,object[],System.Globalization.CultureInfo)
[0x00057] in /private/tmp/monobuild/build/BUILD/mono
-2.0.1/mcs/class/corlib/System.Reflection/MonoMethod.cs:157

At first see implementation of NObjective.Proxies ( Proxies/*.cs ). I need
some value type that contains IntPtr to represent unmanaged handle. Other
types that inherit from it should not contain fields ( after cecil
instrumentation NObjective.Proxies.dll contains NSObject with one IntPtr
field. other value types inherit from it and have no fields but have
additional methods ).  May be it will be better to reserve some type for JIT
runtime ( something like Mono.NativeHandle ) that allows to inherit from.
* Note: SafeHandle is class but I need a value type to simplify interop and
do it without memory overheads.
* Note: runtime already have types not directly inherited from ValueType -
enums
2) Marshal.GetFunctionPointerForDelegate fails on method that return "char".
Open Runtime.cs line 167. You will see:
public static CharacterAtIndex CharacterAtIndexTrampoline =
CharacterAtIndexImpl;
public delegate short CharacterAtIndex( MonoStringTransparentProxy* @this,
Selector selector, int index );

private static short CharacterAtIndexImpl( MonoStringTransparentProxy*
@this, Selector selector, int index )
{
	Tracer.Information( "NSString proxy responded to " + selector );

	return ( short ) @this->ManagedString[index];
}

Replace it with:

public static CharacterAtIndex CharacterAtIndexTrampoline =
CharacterAtIndexImpl;
public delegate char CharacterAtIndex( MonoStringTransparentProxy* @this,
Selector selector, int index );

private static char CharacterAtIndexImpl( MonoStringTransparentProxy* @this,
Selector selector, int index )
{
	Tracer.Information( "NSString proxy responded to " + selector );

	return ( char ) @this->ManagedString[index];
}

You will get something like:

ERROR:marshal.c:8908:mono_marshal_emit_managed_wrapper: code should not be
reached
Stacktrace:

  at (wrapper managed-to-native)
System.Object.__icall_wrapper_mono_delegate_to_ftnptr (object) <0xffffffff>
  at (wrapper managed-to-native)
System.Object.__icall_wrapper_mono_delegate_to_ftnptr (object) <0x00090>
  at (wrapper managed-to-native)
System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegateInternal
(System.Delegate) <0x000ac>
  at System.Runtime.InteropServices.Marshal.GetFunctionPointerForDelegate
(System.Delegate) [0x00000] in
/private/tmp/monobuild/build/BUILD/mono-2.0.1/mcs/class/corlib/Assembly/AssemblyInfo.cs:592
-- 
View this message in context: http://www.nabble.com/NObjective.-Fast-.NET-%3C%3D%3D%3E-objc-bridge-tp20407885p20407885.html
Sent from the Mono - Dev mailing list archive at Nabble.com.



More information about the Mono-devel-list mailing list