Using code in ActionScript3 and Javascript

Here's an interesting architectural query. I have a piece of code that needs to run on the server (under Node.js) and on the client (in a Flash 10 application written with ActionScript 3). The code is mostly pretty complex object manipulation, it doesn't make any API calls, and works great in both contexts.

The project is just a demo so far, so I was happy to copy and paste the code in both places. But it would be very interesting to move forward with that.

So how would you go about it?

  • I guess there is no easy way to get the Flash SDK (needs to be built without an IDE) to read and do something useful with the .js file.

  • My only thought is that I could write a code generator that takes a .js file and wraps it in ActionScript.

Are there any obvious approaches I've missed?


Just to anticipate the obvious answer, I am aware of cross-platform languages ​​like HaXe.

+2


a source to share


2 answers


A possible way is to use include in your ActionScript code. Just a quick and very simple test:

package {
    import flash.display.Sprite;
    import flash.text.TextField;

    public class Main extends Sprite {


        private var _alertTxt:TextField;

        include "some.js"

        public function Main() {
            _alertTxt = new TextField();
            _alertTxt.multiline = true;
            _alertTxt.height = 400;
            _alertTxt.width = 400;
            addChild(_alertTxt);
            run();
        }

        public function alert(msg) {
            _alertTxt.text += msg + "\n";
        }
    }
}

      

some.js

function run() {
    alert("run");
    var obj = {
        a : 'hello',
        b : 4.5,
        c : false
    };
    loop(obj);
}

function loop(obj) {
    for (var field in obj) {
        alert(obj[field]);
    }    
}

      



To compile from the command line (you can add other options):

mxmlc -strict=false Main.as

      

If you don't set it to strictly false, it won't compile due to missing type declarations.

+3


a source


why not ExternalApi?

I have a piece of code that should run on the server



what code? if js can handle than ExternaAPI, fcommand or javascript: "alert (''); can handle it ...

how to add javascript code to url to embed iframe in existing page can help with js url econding

0


a source







All Articles