2.2.虚拟机概览

AVM2的计算基于对方法体(method body)代码的执行,其上下文为方法信息(method information)、一个本地数据区域(local data area)、一个常量池(constant pool)、一个运行时创建的非基本类型对象的(heap),以及一个运行时环境(run-time environment)。

Computation in the AVM2 is based on executing the code of a method body in the context of method information, a local data area, a constant pool, a heap for non-primitive data objects created at run-time, and a run-time environment.

  • 方法体代码(the code for a method body)
    指令(intructions)组成。每条指令会改变虚拟机状态,或者通过IO影响外部环境。

  • 方法信息(method information)
    方法信息定义方法如何使用,如方法被调用时,默认参数如何替换缺少的参数。

  • 本地数据区域(local data area)
    本地数据区域由操作数栈(operand stack)、作用域栈(scope stack)、本地寄存器(local registers)组成。

    • 操作数栈(operand stack)
      操作数栈保存指令的操作数,并接收运算结果。参数从栈顶压入或弹出。栈顶元素地址为0,其下元素地址为1,以此类推。栈地址并未使用,除了作为一个规范机制。

    • 作用域栈(scope stack)
      作用域栈是运行时环境的一部分,保存的对象用于名称查询(name lookup)指令执行时供AVM2搜索。指令将元素压入作用域栈是用于实现错误处理(error handling)、闭包创建(closure creation)、AS3 with语句等功能。

    • 本地寄存器(local registers)
      本地寄存器保存参数值、局部变量、临时值

  • 常量池(constant pool)
    常量池保存被指令流(instruction stream)引用的常量值:numbers, strings, 还有其他名称(names)。

  • (heap)
    指令和AVM2可以在运行时创建对象,这些对象就位于堆中。访问堆的唯一方法就是通过位于堆的对象的引用。堆中用不到的对象将会被AVM2回收。

  • 运行时环境(run-time environment)
    逻辑上运行时环境包括对象链,这些对象的命名属性(named properties)是运行时名称查询(name lookup)中得到的位置。名称查询从最内层(最近被压入的)作用域到最外层(全局)作用域进行。

    方法闭包的创建会导致处于创建状态的运行时环境在闭包中被捕获;当闭包被调用时,作用域被切换为当前,并将被方法体的代码扩展。

    The creation of a method closure causes the run-time environment that is current at the time of creation to be captured in the closure; when the closure is later invoked, that scope is made current, and will be extended by the code in the method body.

上一篇/2.1.常量值|下一篇/2.3.名称|上级目录/2.AVM结构