4

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.

cdlane
  • 40,441
  • 5
  • 32
  • 81
Ska
  • 6,658
  • 14
  • 53
  • 74

1 Answers1

4

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.
Sean Fujiwara
  • 4,506
  • 22
  • 34
  • 1
    Ok, so maybe I should first ask what an "activation" is. Because in the description of NewActivation they say "Since the local registers of an activation...". So what is an Activation in ABC terminology? – Ska Mar 23 '11 at 13:18
  • 1
    @ska As far as I can tell, it's basically an object that contains the local variables as properties. – Sean Fujiwara Mar 23 '11 at 19:39
  • 1
    Object? Isn't AVM working more with stacks and registers rather than objects? I thought that's the whole idea. It might be like in Unix NASM that registers have to be pushed onto a (some) stack before the Unix gives control to Machine Instruction Program. But in Unix this is nicely explained and logical. Thanks anyway for the post, I'll go and dig for more. – Ska Mar 24 '11 at 12:45
  • 1
    There's not really such thing as "more with stacks" or "more with objects", but are you looking for a technical description, or trying to get an idea of what it is? If you want the technical description, it's in the AVM 2 overview. If you want an idea of how to think about it, think of it as an object containing locals. – Sean Fujiwara Mar 24 '11 at 19:48