Archive for the ‘Dot Net Anywhere’ Category

Planned Next Release - 0.2.6 - 6th May

Wednesday, April 22nd, 2009

A minor update is planned for release in the next couple of weeks - hopefully by 6th May.

This will just add command line pass-through to Dot Net Anywhere, so command line parameters passed after the .NET executable name will be passed through to the .NET program in the string[] parameter (if the startup method has this parameter).

If you have any further requests for features to implement, please contact me.

No promises though, and only small features will be considered!

Version 0.2.5 released

Monday, March 30th, 2009

Version 0.2.5 has been released on 29th March 2009.

This release has these major changes:

  • All solutions upgraded to Visual Studio 2008.
  • LINQ to objects has been (almost) completely implemented.
  • Various bugs have been fixed related to the operation of Generic Classes and Methods.
  • All conversion operators are now implemented (e.g. cast an int to a float), and the implementation method has been improved.
  • Dictionary<,> and ArrayList have been re-implemented and improved.
  • Comparer and EqualityComparer have been implemented.

See the release notes for more details, or see the source code if you're really interested.

Beware that there are still some known bugs in this release:

  • There is a memory leak somewhere - I haven't looked into this is any detail yet.
  • Some subtle generic bugs still remain; one of these is why .ThenBy() and .ThenByDescending() have not been implemented.

See the Bugs.txt files in the release for a few more details. If you can find more details in the source code, please let me know!

The Problem with Platform Invoke

Monday, March 23rd, 2009

Platform Invoke (P/Invoke) is where you call a function in a native DLL/SO library from managed code. In C# this appears very simple - you just write something like this:

[DllImport("MyLibrary")]
extern public static int PerformNativeOperation(int x, int y);

Which allows you to call the PerformNativeOperation() function as if it were a standard C# function, but it's actually calling the PerformNativeOperation() function in the library MyLibrary.dll or MyLibrary.so (depending on OS).

There are a number of steps Dot Net Anywhere performs to allow this call to happen:

  1. Find and load the native library, if it isn't loaded already.
  2. Find the requested function within the library.
  3. Marshall the call arguments from .NET in-memory representations to native in-memory representations.
  4. Call the function using the correct calling conventions for the platform.
  5. Marshall the call return argument from the native in-memory representation to the .NET in-memory representation.
  6. Free any temporary memory used during marshalling operations.

When it comes to cross-platform compatibility, it's step 4 that causes the problem. Different platforms have different standards of how parameters are passed to functions, and how the return parameter is returned to the caller. For example:

  • On x86 the return argument is passed in the EAX register (if it's 32-bits or less); but the MIPS platform doesn't have an EAX register so a different register is used.
  • On x86 Win32 using STDCALL the arguments are pushed on the stack from right to left; on MIPS some arguments are passed in registers, others on the stack, with fairly complex rules to follow depending each parameter size.

Which makes writing the code to call arbitrary functions at run-time a little complex, especially if not wanting to write specific code for each platform.

Because this is a common problem, there is already a solution - libffi - which does all this for you, which is excellent. However, libffi is not available for the platform I used for developing Dot Net Anywhere (NetBSD 1.5, x86 and MIPS), so it is not used.

The solution can be seen in PInvoke.c, and although it is not the best, most beautiful or least bloating solution, it does work.

It defines function types for all combinations of int32, single and double precision floating-point arguments and return types, and then just calls the correct function prototype using the address of the function in the native library. Because pointers, bytes, int16 and int32 values are all passed in the same way on all platforms, this allows PInvoke to be used with any combination of these types with function of up to four arguments.

Neat - but not particularly good.

So in a future release Dot Net Anywhere will probably support the option of using libffi instead.

LINQ, Bugs and Dot Net Anywhere

Wednesday, March 18th, 2009

The next version of Dot Net Anywhere, 0.2.5 , is going to support the beginnings of LINQ to Objects. That is, the LINQ extension methods on IEnumerable<T> are all (or at least, mostly) going to be implemented.

You probably realise that LINQ relies heavily on:

  1. Lambda functions - which are transformed into methods and delegates.
  2. Iterators - using yield return.
  3. Generic methods.

Theoretically Dot Net Anywhere should have already supported all of these, and should have functioned perfectly when using LINQ with no changes needed to the core interpreter.

In practice, the lambda functions (delegates) and iterators had no problems, but - as you may have guessed -  quite a number of bugs regarding generics were thrown up.

So, if you've stumbled across any of these bugs yourself:

  • Static fields in generic types didn't work.
  • Overriding virtual methods in generic types failed.
  • Generic methods just didn't really work at all - sorry.
  • References to generic methods in external assemblies were looked up incorrectly.

Then don't panic - version 0.2.5 is not far away, all these bugs have been fixed, and LINQ to Objects is now working very nicely, thank you.

Speedy Exceptions

Monday, March 16th, 2009

Throwing exceptions is sloooooow.

We all learn as novice .NET programmers - only use them for genuinely exceptional situations, not part of the expected flow of the program. Catching a thrown exception can be many thousands of times slower than normal program flow, so use them with care.

All excellent advice.

However, in Dot Net Anywhere, exceptions are fast - see JIT_Execute.c for details.

Of couse, fast is relative. Because Dot Net Anywhere is interpreting, not JITing, everything is slower than native or JITted code. But exception program flow is no slower than normal program flow.

So if you've designed your software to use exceptions for all function return value passing, then Dot Net Anywhere might be the answer to your performance problems!

Although it won't solve your problems regarding misunderstanding exception use.