Solibulo

Uninteresting things
2008-10-04 00:28
by softlion

Have you ever dreamed to write javascript code in C# (or VB#) ?
Have you ever dreamed to write a Flash application in C# instead of ActionScript ?
It is now possible with either Script# or the JSC projects, both having a different approach to this same starting idea.

 

As was said, the idea is to write C# code which gets transformed somehow into javascript files. All of this using the powerful Visual Studio code editor with all its goodies like intellisense, snippets, refactoring, and the power of the C# language with support for LINQ in JSC !
How is it done behind the scene ?

 

In Script#, the C# code is parsed and compiled by the Script# compiler which outputs javascript code. Standard assemblies like system.dll must be replaced by specific Script# assemblies - this is done automatically using the correct project template in VS.

 

In JSC,  which stands for "Javascript Compiler" (which is now more than that as it generates also ActionScript Java and PHP !), the C# code is compiled by the regular C# compiler into assemblies (in MSIL), which in turn are parsed and "recompiled" by the jsc compiler into the output language (js files for javascript, as files for actionscript, ...).That means LINQ (.NET 3.5) is fully supported as it is only a syntax evolution which is completely transformed into "MSIL 2.0" (.NET 2.0) by the 3.5 compiler. You can reference any .NET assembly you want, but many built-in functions won't work as they depend on low-level system functions. JSC provides assemblies declaring specificities of each target language in its own namespace, as well as extension methods to help working with the target language objects.

Example:

    [Script, ScriptApplicationEntryPoint]
    [SWF(backgroundColor = 0xcccccc)]
    public class MyFlashMainClass : Sprite
    {
        Sprite mc;
        Sprite currentDraggedSprite;
        TextField t4;

        public MyFlashMainClass ()
        {
            var c = new Sprite();
            c.graphics.beginFill(0xf8f8f8);
            c.graphics.drawRect(1, 1, stage.stageWidth-2, stage.stageHeight-2);
            c.graphics.endFill();
            c.AttachTo(this);
            c.doubleClickEnabled = true;

            stage.frameRate = 30;
            stage.focus = this;
.....

 

Which one is better ? My heart goes to JSC as I'm fond of LINQ, it's anonymous delegates syntax and extension methods, even if Script# seems better documented.
I've successfully created my first Flash application using JSC and I'm very proud ! So it really works ! Even if there are syntax tricks and the needs to know basic flash programming to make it work.

 

References

JSC: http://jsc.sourceforge.net/

Script#: http://projects.nikhilk.net/ScriptSharp/

 

Comments

Comments are closed