actionscript-3 - 在 ActionScript Bytecode 中,NewActivation 是什么意思?

标签 actionscript-3 bytecode avm2

有些方法使用它有些不使用,显然这是由needsActivation 标志指定的,它有什么作用,何时使用,何时不使用?

AVM 文档上的信息有点不慷慨:

创建一个新的激活对象 newactivation 并将其压入堆栈。只能用于在 MethodInfo 条目中设置了 NEED_ACTIVATION 标志的方法中。

最佳答案

AVM 2 概述的第 6.3 节中有一个很好的描述:

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.



当它内部定义了局部函数时,它会在方法中使用,例如:
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();        
}       

结果是:
Timer #1 done.
Timer #2 done.

您可以看到调用方法时局部变量和参数被“锁定”。这是因为 startTimer 方法在每次运行时都会创建一个激活,这是这些变量被锁定的地方。如果未锁定局部变量,则此代码的结果将是:
Timer #2 done.
Timer #2 done.

关于actionscript-3 - 在 ActionScript Bytecode 中,NewActivation 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5399792/

相关文章:

actionscript-3 - 使用自定义 LoaderInfo 类的自定义 AS3 Loader 类?

android - Dalvik 字节码跟踪器

java - 为什么使用invokedynamic 调用Java 8 lambda?

java - 如何在 Byte Buddy 中将 Advice 添加到 "goto"字节代码?

c# - 翻译我写到 C# 的 ActionScript 3 代码

arrays - 在 AS3 中清空向量或数组的最佳方法是什么?

actionscript-3 - AVM2 支持哪些优化?

flash - 如何在 AVM2 字节码中找到方法?

actionscript-3 - 从 as3 中的输入文本中删除逗号