Access the V8 Engine Abstract Syntax Tree

Can I access AST for v8 engine for a given JavaScript code? I am working on a static JavaScript parser using the V8 engine.

+8
javascript v8 interpreter abstract-syntax-tree
source share
3 answers

This is pretty old, but maybe the answer helps someone stumble upon this. The answer is yes, assuming you're ready to change V8 and compile your own version.

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

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

Since V8 compiles functions just in time when they are actually called, there is another place in CompileLazy where you will need to do the same to get your ACTs during call scripts.

Due to lazy compilation, this probably will not allow you to perform static analysis, because the execution is already in progress before you access the AST for lazily compiled materials. But here's how to get AST.

+10
source share

Use --print-ast via SetFlagsFromString

+1
source share

Well, I don’t know what you want to achieve, but it looks like you want to change the AST from within your C ++ code (or perhaps write shell classes for the JavaScript context for them?).

I suggest looking at the header file, which pretty much explains what will be used on the V8 AST:

http://v8.googlecode.com/svn/trunk/src/ast.h

~ Greetings

0
source share

All Articles