访问V8引擎的抽象语法树 [英] Access the Abstract Syntax Tree of V8 Engine

查看:131
本文介绍了访问V8引擎的抽象语法树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于给定的JavaScript代码,是否可以访问v8引擎的AST?我正在使用V8引擎开发JavaScript静态分析器。

Is it possible to access the AST of the v8 engine, for a given JavaScript code? I'm working on a JavaScript Static Analyzer using V8 engine.

推荐答案

这是相当古老但也许答案可以帮助有人绊倒这个。答案是肯定的,假设您愿意修改V8并编译自己的版本。

This is pretty old but maybe the answer helps someone stumbling upon this. The answer is yes, assuming you are willing to modify V8 and compile your own version of it.

如果是,那么在 compiler.cc 你找到了一个在 MakeFunctionInfo <中调用 MakeCode 的地方/ em>它将存储在传递的CompilationInfo对象中的AST转换为本机代码。
您需要编写一个继承自 AstVisitor 然后你可以在调用 MakeCode 之前插入以下行来检查AST:

If so, then in compiler.cc you find a spot where MakeCode is called throughout MakeFunctionInfo which transforms the AST that is stored in the passed in CompilationInfo object into native code. You need to write a class that inherits from AstVisitor then you can inspect the AST by inserting the following lines before the call to MakeCode:

MyAstVisitor mAV;
// this will call VisitFunctionLiteral in your AST visitor
info->function()->Accept(mAV);

由于V8在实际调用时及时编译函数,因此 CompileLazy 你需要做同样的事情才能在执行调用脚本的过程中获得它们。

As V8 compiles functions just-in-time when they are actually called, there is another spot in CompileLazy where you would have to do the same to get their ASTs throughout execution of calling scripts.

由于懒惰的编译事情,这可能会赢得' t使您能够进行静态分析,因为在您访问AST以进行延迟编译之前,执行已经在进行中。但这是如何获得AST。

Because of the lazy compilation thing this probably won't enable you to do static analysis, because the execution already is in progress before you have access to the ASTs for lazily compiled stuff. But this is how to obtain the ASTs.

这篇关于访问V8引擎的抽象语法树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆