How MonoTouch works?

2019-01-22 20:38发布

问题:

I would really appreciate it anybody could briefly explain me, what's the general approach to implementing things like MonoTouch? I'm really amazed because it's not the first example I see when people get some platform like Java and make it translate into something like C/Objective-C. I can't really imagine how things like Garbage collector and stuff are being translated.

Thanks in advance.

EDIT: I understand theoretical possibility of translating one language into another. My question is a bit more technical: do they implement a complete runtime in ObjC and bundle it? (I doubt it...) Or they just translate C# code into ObjC/binary/etc?

回答1:

Monotouch uses Ahead-Of-Time compilation to produce a single statically compiled file - so it does not implement the .Net runtime on the iPhone. Monotouch uses binding to the iPhone libraries exposed to C#. There is no translation to Objective-C.

Basically, it gives you the familiarity of working in C# and its associated toolchain while exposing the library calls of the underlying iPhone OS.

Miguel di Icaza explained this a bit on the Stackoverflow podcast where he was a guest. link



回答2:

MonoTouch isn't a translator. It's simply the Mono runtime running on iPhone, with some fancy trickery to make it compatible with the iPhone's restrictions.

The conventional Mono "runtime" isn't just the JIT, it also handles GC, reflection, I/O layer, threading, exceptions etc. There's also the class libraries. In order for your app to run on the iPhone, much of this must be included with the app. Slimming it down presents some challenges. In addition, the iPhone prevents JIT runtime code generation, only runs signed code, and doesn't allow dynamic libraries.

What MonoTouch does is to use an IL linker to combine just the parts of the class libraries that your code uses into a single IL binary. It then uses AOT compilation to pre-generate all the native code that the JIT would normally generate from the IL, then links this together with the JIT-less runtime into a single native binary that can be signed. Finally, the IL is stripped from the managed binary. leaving only metadata.



回答3:

Monotouch is presumably a port of Mono to the iPod.

Mono itself is an open source implementation of the .NET runtime (and some dev tools) in various *nix environments.

This was a massive labour led by Miguel de Icaza who realised early the importance of .NET and set about porting it to open platforms. Incidently he receieved a great deal of support from MicroSoft itself as MONO gave ineteroparability between native .NET and mono.

So the first answer is you need to be a well organised, hard working genius.

The second answer is more Comp Sciency. "Turing Complete" is a the minimum set of functions that a system needs to be considered a programable computer. Its actualy a very small set -- subtract, compare, branch and read and store.

The theory is that any "Turing Complete" system can do anything any other "Turing Complete" system can do. So given any reasonably complete programming language environment you should be able to emulate it using another program environment.

There are numerous examples of this around. Jpython runs the emulates C python in a Java JVM. My two personal favourites are which runs original zX spectrum games in your browser and Knuth MIX assembeler machine both of which are pure javascript.