操纵V8 ast

时间:2013-04-03 11:01:35

标签: javascript compilation v8 abstract-syntax-tree

我打算直接在v8代码中实现js代码覆盖。 我的初始目标是为抽象语法树中的每个语句添加一个简单的打印。 我看到有一个AstVisitor类,它允许你遍历AST。 所以我的问题是如何在访问者目前访问的语句后向AST添加语句?

1 个答案:

答案 0 :(得分:5)

好的,我会总结一下我的实验。首先,我写的内容适用于V8,因为它在Chromium版本r157275中使用,所以事情可能不再起作用了 - 但我会链接到当前版本中的位置。

如上所述,您需要自己的AST访问者,比如MyAstVisior,它继承自AstVisitor并且必须从那里实现一堆VisitXYZ方法。检测/检查执行代码所需的唯一一个是VisitFunctionLiteral。执行的代码是源(文件)中的一个函数或一组松散语句,V8将其包装在一个函数中,然后执行该函数。

然后,在解析的AST转换为代码之前,here(从松散语句编译的函数的编译)和there(在运行时编译,当为执行预定义的函数执行时)第一次),您将访问者传递给函数文字,该文字将在访问者上调用VisitFunctionLiteral

MyAstVisitor myAV(info);
info->function()->Accept(&myAV);
// next line is the V8 compile call
if (!MakeCode(info)) {

我将CompilationInfo指针info传递给自定义访问者,因为需要修改AST。构造函数如下所示:

MyAstVisitor(CompilationInfo* compInfo) :
    _ci(compInfo), _nf(compInfo->isolate(), compInfo->zone()), _z(compInfo->zone()){};

_ci,_nf和_z是指向CompilationInfoAstNodeFactory<AstNullVisitor>Zone的指针。

现在在VisitFunctionLiteral中,您可以遍历函数体并根据需要插入语句。

void MyAstVisitor::VisitFunctionLiteral(FunctionLiteral* funLit){
    // fetch the function body
    ZoneList<Statement*>* body = funLit->body();
    // create a statement list used to collect the instrumented statements
    ZoneList<Statement*>* _stmts = new (_z) ZoneList<Statement*>(body->length(), _z);
    // iterate over the function body and rewrite each statement
    for (int i = 0; i < body->length(); i++) {
       // the rewritten statements are put into the collector
       rewriteStatement(body->at(i), _stmts);
    }
    // replace the original function body with the instrumented one
    body->Clear();
    body->AddAll(_stmts->ToVector(), _z);
}

rewriteStatement方法中,您现在可以检查语句。 _stmts指针包含一个语句列表,最终将替换原始函数体。因此,要在每个语句后添加print语句,首先添加原始语句,然后添加自己的print语句:

void MyAstVisitor::rewriteStatement(Statement* stmt, ZoneList<Statement*>* collector){
    // add original statement
    collector->Add(stmt, _z);

    // create and add print statement, assuming you define print somewhere in JS:

    // 1) create handle (VariableProxy) for print function
    Vector<const char> fName("print", 5);
    Handle<String> fNameStr = Isolate::Current()->factory()->NewStringFromAscii(fName, TENURED);
    fNameStr = Isolate::Current()->factory()->SymbolFromString(fNameStr);
    // create the proxy - (it is vital to use _ci->function()->scope(), _ci->scope() crashes)
    VariableProxy* _printVP = _ci->function()->scope()->NewUnresolved(&_nf, fNameStr, Interface::NewUnknown(_z), 0);

    // 2) create message
    Vector<const char> tmp("Hello World!", 12);
    Handle<String> v8String = Isolate::Current()->factory()->NewStringFromAscii(tmp, TENURED);
    Literal* msg = _nf.NewLiteral(v8String);

    // 3) create argument list, call expression, expression statement and add the latter to the collector
    ZoneList<Expression*>* args = new (_z) ZoneList<Expression*>(1, _z);
    args->Add(msg);
    Call* printCall = _nf.NewCall(_printVP, args, 0);
    ExpressionStatement* printStmt = _nf.NewExpressionStatement(printCall);
    collector->Add(printStmt, _z);   
}

NewCallNewUnresolved的最后一个参数是一个数字,用于指定脚本中的位置。我假设这用于调试/错误消息,以告知错误发生的位置。我至少从未遇到过将其设置为0的问题(kNoPosition也有一个常量)。

最后一句话:在每个语句之后实际上不会添加一个print语句,因为Blocks(例如循环体)是表示语句列表的语句,而循环是具有条件表达式和正文的语句块。因此,您需要检查当前处理的语句类型并以递归方式查看它。重写块与重写函数体几乎相同。

但是当您开始替换或修改现有语句时,您将遇到问题,因为AST还包含有关分支的信息。因此,如果您为某些条件替换跳转目标,则会破坏您的代码。如果直接将重写功能添加到单个表达式和语句类型而不是创建新的替换它们来替换它们,我想这可以被涵盖。

到目前为止,我希望它有所帮助。