Flash Dynamic Runtime Class'
I’ve been re-looking at how to do runtime class sharing rather then the typical compile-time sharing. This started when i was implementing a proxy for class’ that you want to runtime share. So you can call methods on a class right away, even if it isn’t loaded yet.
There are several hurdles to overcome to make this work, the biggest, for me, was figuring out how to dynamically instance a class, from a string representation, at runtime.
The method that worked for me was to use the following code
After i had figured this out, i was searching for some more info, and found that this is the way most people do it. I tried to use eval, but didn’t have any luck. Also, as2lib has a lot of great reflection functionality, but i was just trying to learn how to do it myself.
Here is the wrapper class, which is a proof of concept example of the runtime proxy.
So, using this, you can do something like x = wrapper("com.loc.myfunc");. Then you can call functions on x as if it were the com.loc.myfunc instance as long as the functions don’t return anything. The wrapper class looks for the com.loc.myfunc runtime file in the com/loc/myfunc.swf file. In real life, the location of your shared assets would be controlled via some kind of config file.
Anyway, it works, so i’ll probably never look at it again. But in case i do, i want to have a copy of it around!
Another way to do this would be to use the Deferred methods which the Python asynchronous network framework Twisted uses. There is an implementation in actionscript (thanks to Bob Ippolito , which came out of the mochikit javascript library (i’m actually not sure which came first). This would allow you to call methods on the wrapper, and register callbacks, which would receive the return values once the class was properly loaded. I leave this as an exercise for the reader. Although i’ll probably try to implement this myself, as it sounds pretty cool.
I should also mention that Ted was the first to really put me on to runtime sharing via his PSDK/FLOW projects.
I do wonder if, now that broadband is so much more prevalent, if runtime sharing still stacks up? I was also thinking about Laszlo and how it could fit in… Creating/Updating the new runtime shared bits as the AS files are changed, but using caching to remove most of the bandwidth usage… I’m sure there is much more to consider… Just not enough time!
UPDATE: I got it working using the aboved mentioned Deferred code. I had to make changes in the __resolve and _applyPickles functions. Basically, adding the deferred to be returned to the caller. This now makes all calls asynchronous, or rather, all calls before the dynamic class is loaded, asyncronous. I’m not sure if its better to have the client code deal with the return type, have it do a instanceof Deferred, and take the appropriate action… Have to think about this some more. This also means that any property access’ that are attempted before the class is loaded are lost, only function calls are saved. Once the class is loaded, property access works as usual. I think.
UPDATE 2: I’ve included the newest version of the wrapper class above. This now lets you set wether you want a function to be asynchronous or not, by appending _deferred to the end of the method name. I’m also including my tester class (org.test.tester), and the code that sits in the .fla to make it all go. Here are the instructions:
- Create
demodirectory. - Add
orgsub-directory. - Add
testsub-directory toorgdirectory. - Copy
wrapper(above) andtester(below) code into .as files with same name ie wrapper.as and tester.as. Keep them in thedemodirectory. - Create a
tester.flain thedemodir and include onlyorg.test.tester;as a script in the first frame (this is to force it to add the code to itself). - Publish
tester.fla. - Put
tester.swfinto thedemo/org/testdirectory. - Create a
example.flafile in thedemodir. - Copy the
example(below tester) code into a script in frame 1 of theexample.flafile. - Publish the
example.flaand look at the cool trace statements!
Here is the tester code:
Here is the code to go into the example.fla:
There is some magic in there now, but i think its pretty cool stuff. Maybe useful to somebody too!

Leave a comment