actionscript-3bytecodeavm2

In ActionScript Bytecode, what does NewActivation mean?


Some methods use it some don't, obviously that's specified by needsActivation flag, nut what does it do and when to use it and when don't?

The information on AVM docs is somewhat ungenerous:

Creates a new activation object, newactivation, and pushes it onto the stack. Can only be used in methods that have the NEED_ACTIVATION flag set in their MethodInfo entry.


Solution

  • There's a good description in section 6.3 of the AVM 2 overview:

    Since the local registers of an activation are not captured when the newfunction instruction is executed, the environment for non-leaf functions must be stored in activation objects that can be captured properly. The newactivation instruction creates such an activation.

    It's used in a method when it has a local function defined inside it, for example:

    public function QuickTest()
    {
        startTimer(1);
        startTimer(2);
    }
    
    public function startTimer(id:int):void
    {
        var timer:Timer = new Timer(1000, 1);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, function(ev:TimerEvent):void
        {
            trace('Timer #'+id+' done.');
        });
        timer.start();        
    }       
    

    Which results in:

    Timer #1 done.
    Timer #2 done.
    

    You can see that the local variable and argument were "locked" when the method was called. That's because the startTimer method has creates an activation every time it is run, which is where those variables are locked. If local variables weren't locked, the result of this code would be:

    Timer #2 done.
    Timer #2 done.