View Full Version : Runtime code creation

07-24-2006, 04:46 AM
Existence of the flash.display.Loader.loadBytes() (http://livedocs.macromedia.com/labs/as3preview/langref/flash/display/Loader.html#loadBytes()) method implies that given an AS3 SWF Bytecode library, we can create new classes at runtime (pack them into a byte array and import into the current application domain via Loader.loadBytes()). Does AS3 provide another method for runtime class creation as well?

07-24-2006, 08:56 AM
Take a look at the ObjectEncoding class. It states that AS3 supports AMF0 and AMF3. Now I'm not familiar with AMF at all (in fact, I never heard of it before I started with AS3 a few weeks back), but if you knew the specifications of that format, you could probably make new objects in runtime, or even with server-side scripting. But I don't know if the AMF specifications are open.

07-24-2006, 06:14 PM
I have only a very cursory knowledge of what AMF is, but I don't think that AMF serializes actual code (i.e., methods) - at least that wouldn't make any sense as I understand it. (Someone please correct me if I am wrong! :)) If that is so, then we can't use ByteArray.readObject() to construct a full-fledged new Class on the fly, which we can (potentially) with Loader.loadBytes().

Thanx, Fall_X, it was good to look at that.

07-24-2006, 10:33 PM
Well, the documentation says, for readObject : "Reads an object from the byte array, encoded in AMF serialized format.", so it does use AMF. In AS3, a class is also an object, of type Class, which you can reference and all, so apparantly AMF does serialize actual code.

07-25-2006, 12:46 AM
I still think that only properties would be serialized... what would you do with the AS code on the back-end (e.g., in AMFPHP), where you have to deserialize the AMF Object? The properties you can convert, but the code?

I was also looking at the reverse-engineered AMF3 definition on osflash (http://www.osflash.org/amf3/index), and I think that for an Object only properties are passed and a "class-def", which looks to be a class name only.

07-25-2006, 01:04 AM
i didn't try AS3 till now

but not realy understand what this has to do whit "Runtime code creation"

already in AS1 you can load an SWF and use methodes wich are declared inside this

Does AS3 provide another method for runtime class creation as well?

you can create classes at runtime (not realy but sort of) already whit AS1

Q = random(100);
this[Q] = function () {
this[Q].prototype.SomeMethode = function() {
Z = new this[Q]();

and "Runtime code creation" as i woud understand
you cant make whit AS1 AS2 and aslong i know not whit AS3


but this is a qiestion of definition

"Runtime code creation"
is always happanening as the code is what's in the memory
and this is altered each milisecond

07-25-2006, 02:05 AM
It's true that you could do something similar with prototypes... I was thinking of creating a whole class from scratch, and inserting it into the application domain space for other code to be able to use - by a certain name, e.g.. In AS3, I guess you could take Object.prototype, hang a bunch of methods on it... but then how to insert it into the name space? (In AS1/AS2 you could just add it to _global and voila, you got a class definition :).

In any case, Xeef, probably you are right - for just about any sort of foreseeable functionality there are other ways of "runtime code creation".