Home > C#, JavaScript, Object Pascal, OP4JS, Smart Mobile Studio > Compiling to .net with Smart Mobile Studio

Compiling to .net with Smart Mobile Studio

February 19, 2015 Leave a comment Go to comments

You are probably thinking this is too good to be true, but it’s not. In fact, I have a working prototype already (although it needs a hell of a lot of work). But since people seem to think this is either a blatant lie, or that I’m doing black magic to get this working, I’m going to explain how I do it!

In fact, I have provided links to all the parts you need on this post, so you can download, compile and play around with the tech yourself. But first, some context 🙂

Smart Mobile Studio has a kick-ass compiler. This compiler takes object pascal, parses, tokenizes it and turns it into an AST (abstract symbol tree, where every command, variable, statement etc. is turned into an object in memory). This AST is then analysed by a code-generator (codegen) which turns the AST into JavaScript.

Since Smart Mobile Studio has been in sale for a couple of years I don’t think I need to explain or prove that point as well.

It's ALIVE! It's ALIVE! MOHAHAHAHA

It’s ALIVE! It’s ALIVE! MOHAHAHAHA

But .net is a completely different beast. But only when you are looking at .net from the outside. I am also a C# developer so I also look at Delphi and Smart from inside C#.

In short: The .net framework has one huge advantage over native languages, and that is something called reflection. Reflection is not just RTTI, like some people have voiced. It’s also the ability to hot-swap code modules in a living program, because bytecodes are just that – byte codes. Since .net is a “virtual language” you can do all sorts of cool stuff with it, like changing a program at runtime.

But the one feature that sets .net apart from Java, which works pretty much along the same lines, is the ability to also emit code. Thats right, you can take a class and emit it to disk from you program. But it doesn’t stop there.

You can also compile new code from within your own program, by building something called expression-trees. This is pretty much what all compilers does (including smart mobile studio), because an expression-tree is a fancy way of saying “AST”.

So you can programmatically build up a full expression tree from within your C# program.

Any bells ring yet?

He he. What you do is of-course to construct an expression-tree and then use the emitter to dump that as an assembly, and voila — you have in fact generated a separate, self-sustaining assembly! It’s a bit more complex than that, but essentially that’s how you do it.

The other kids are doing it

Some guy picked up this years ago and he decided to build a JavaScript compiler. So he built a full JavaScript compiler which parses, tokenizes and does everything a compiler should – and generates an expression-tree — and voila, he emits the code to disk and what you get is a compiled, fully working MS-CIL generated assembly (read: a DLL file that can be executed or used in your .NET projects).

Compiles without much mayhem on my mac

Compiles without much mayhem on my mac

When you load in this assembly into Visual Studio, you will find that all the function names are exported as symbols, so you can happily call your now compiled JavaScript code from C# or Visual Basic.

Pretty cool right? The speed benefits alone are phenomenal, because the assembly is compiled to highly optimized machine code when it’s loaded the first time.

What about Smart Mobile Studio?

Since Smart Mobile Studio generates JavaScript, and Jurassic generates CIL from JavaScript — I guess I don’t need to tell you where this is heading, or that in fact it’s possible to achieve.

In fact, if you don’t believe me, clone out your own GIT version of Jurassic, load up Visual Studio (Visual Studio 2013 Community edition is free) and play around with the code yourself.

Will this become a part of Smart Mobile Studio?

Time will tell. We have more than enough work on our plates to start throwing .net into the mix right now, but I do find the route interesting. It would represent a much better way than the one Borland started ages ago, since the Smart Pascal dialect would be 100% retained. We have already gotten rid of pointers, so Smart Pascal is perfectly suited for the .net framework. You wouldn’t even need to think about .net, just write ordinary Smart Pascal.

Why should this be cool?

It opens up XBOX development first and foremost, followed by native Mono and Desktop development. This means you can write games that targets both HTML5, Mobile and XBOX with the same codebase. That should make developers happy I think.

And then there is ASP.net to think about. Thats probably one of the most important technologies in my view.

Well — fun thing to play with!

Advertisements
  1. abouchez
    February 19, 2015 at 3:16 pm

    Why not just the DWS compiler emit C# or IL?
    I suppose it may be much better, since JavaScript introduces noticeable overhead due to its lack of strong typing, and its use of object prototyping.
    Whereas you may be able to compile directly from DWS types into plain C#, with all potential benefits of high level types and straight OOP.

    • Jon Lennart Aasenden
      February 19, 2015 at 3:40 pm

      True, that is one way of doing it. But why re-invent the wheel? When all we need to do is create a command-line exe, and add a third step to the compiler chain — that is easier.

      But yes, C# codegen has been experimented with. I began that in the QTX-IDE which is on pause at the moment. Because it’s possible to map object pascal 1:1 with C#.

  2. February 24, 2015 at 7:04 pm

    Hi, there is a tool which will convert your C# to JavaScript automatically for you.

    Take a look at CSHTML5 at http://www.cshtml5.com/

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: