Some days ago,
Nicolas Cannasse has released a new library called
hScript, for
haXe.
This lib enables you to "
interpret some code dynamically".
hScript is really
powerful, you can try it with this little
hScript Interpreter.
This was made with haXe and the important code, here, is :
function parseScript( s : String )
{
var parser = new hscript.Parser();
var program = parser.parseString( s );
var interp = new hscript.Interp();
interp.variables.set( "root", flash.Lib.current );
interp.variables.set( "new", createInstance );
interp.variables.set( "class", getClass );
interp.execute( program );
}
function createInstance( s : String, a : Array<Dynamic> )
{
return Type.createInstance( Type.resolveClass( s ), a );
}
function getClass( s : String )
{
return Type.resolveClass( s );
}
"
root" variable is "shared" with the interpreter and set to
flash.Lib.current.
Since there is no "
new" keyword in hScript, in order to simulate a constructor, we can call a method that uses Type.createInstance. Here this method is named "
new" and is still shared with the interpreter.
If you want to access a
static field of a class, you have to use "
class" function that is shared with the interpreter and returns the real class from a string.
With this sample, you can access all the flash player's API basically. If you want to access a specific class you must first import it. In other words,
you can access all the classes built into the SWF or available by the flash player.
Check
http://code.google.com/p/hscript/ to see more informations.