如何从AS3代码调用Tamarin ESC编译器?

时间:2011-05-01 00:03:30

标签: actionscript-3 compiler-construction eval ecmascript-5

我正试图从AS3代码中调用Tamarin的ESC编译器。我已经在Flash Player中加载了ESC编译器字节代码,但是当我调用它时,无论我提供什么源代码,ESC编译器总是返回相同的字节代码。人类可读的ESC代码如下所示:

function compileStringToBytes(input, context="(string)", start_line=1) {
    let [_,_,res] = compile( (function () input),
                             (function (abc) abc.getBytes()),
                             context,
                             start_line );
    return res;
}

我使用以下AS3代码调用它:

var compile:Function = getDefinitionByName("ESC::compileStringToBytes") as Function;
var array:ByteArray = compile( function():String { return "trace(\"hi\");" },
                "test" );

但无论我提供什么源代码,它总是返回两个函数的字节代码,一个调用另一个函数,两个函数都没有做任何事情。

这是它返回的字节代码(元数据被剥离,转换为人类可读):

script0
const <#internal test>::internal:Namespace = <#internal test>   /* slot_id 0 */
const <#internal test>::public:Namespace =  /* slot_id 0 */
var Function:*  /* slot_id 0 */

function script0$init():*   /* disp_id 0*/
{
  // local_count=2 max_scope=1 max_stack=2 code_len=11
  0         getlocal0       
  1         pushscope       
  2         findpropstrict  Function
  4         newfunction     function ():*   /* disp_id 0*/
  6         setproperty     Function
  8         getlocal1       
  9         returnvalue     
  10        returnvoid      
}


function ():*   /* disp_id 0*/
{
  // local_count=1 max_scope=0 max_stack=0 code_len=1
  0         returnvoid      
}

提前致谢!我知道这是一个铁杆问题,但我希望一些核心编码员可以对此有所了解!

1 个答案:

答案 0 :(得分:4)

找到了我自己的问题的答案。

以下站点使用最新的Tamarin ESC编译器ABC位通过Flash动态运行代码(左侧面板显示运行编译器的源代码):

http://wonderfl.net/c/2pBs/read

它实际上使用Mercurial http接口下载程序抓取Tamarin Redux位:http://hg.mozilla.org/tamarin-redux/raw-file/db3ebe261f68/esc/bin/

奇妙的代码依次使用Claus Wahlers的codeazur as3swf来包装ABC位以加载到Flash中(编译器本身和编译的位):

https://github.com/claus/as3swf

谢谢克劳斯和wonderfl!开源代码。