views:

61

answers:

2

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 app written with Actionscript 3). The code is mostly fairly intricate object manipulation, it doesn't make any API calls, and works fine in both contexts.

So far the project is just a demo, so I've been happy to copy and paste the code into both places. But it might be quite interesting to move forward with this.

So how would you do it?

  • I assume there is no easy way to get the Flash SDK (has to build without an IDE) to read and do something useful with a .js file.

  • My only thought is that I could write a code-generator that takes the .js file and places it in an ActionScript wrapper.

Are there any obvious approaches that I've missed?


Just to pre-empt an obvious answer, I know about cross-platform languages like HaXe.

A: 

why not ExternalApi?

I have a piece of code that needs to run on the server

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

http://stackoverflow.com/questions/2653255/how-to-inject-javascript-code-in-url-to-insert-iframe-in-existing-page can help with js url econding

dnkira
Its a good suggestion, thanks, but unfortunately it wouldn't work in my use case (the SWF isn't always running in a browser).
Ian
+3  A: 

A possible way is using include in your wrapper 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 command-line (you might want to add other options):

mxmlc -strict=false Main.as

If you don't set strict to false, it won't compile because of the lack of type declarations.

Juan Pablo Califano
Gadzooks, Juan. Didn't know that even existed.I arrived in ActionScript at v.3 when it became a 'serious' development language. And I guess because of that I never found out about include. Perfect solution. Thanks. I'm very pleased it was so simple.
Ian
And 30 seconds later, it is working like a charm... Just a FYI in case anyone else happens across this: I don't want to switch off strict for the bulk of my compile. So I'm compiling this wrapped up component into a SWC file for later linking to the final compile. That way the lax checking doesn't allow me to get away with anything I shouldn't elsewhere.
Ian
Cool. Yeah, include is not a secret feature but it's not all that publicized either, as its use is generally frowned upon. But I'm glad they didn't remove it from AS 3.0, since it can be quite useful in some scenarios.
Juan Pablo Califano