How to use V8 as JavaScript parser?

2,909 views
Skip to first unread message

Ivan P.

unread,
Sep 14, 2013, 7:21:57 AM9/14/13
to v8-u...@googlegroups.com
Hello, Does anyone has experience with using V8 as standalone parser? 
Please share any links and/or code if possble.
The idea is to get AST and traverse it. 
I need only traverse AST, not modify it.

Ben Noordhuis

unread,
Sep 14, 2013, 9:55:30 AM9/14/13
to v8-u...@googlegroups.com
V8 doesn't have a public API for its AST. Maybe try uglifyjs or acorn.

Ivan P.

unread,
Sep 16, 2013, 5:47:10 PM9/16/13
to v8-u...@googlegroups.com
Yes, I know, but this is what I'm asking about - does anyone could get it out from the deep inside of v8 and make use of it?
In other worlds, what needs to be done to instantiate v8's parser object, run parsing and achieve AST? Any examples of the code?
I really need that, and I need that exactly in C++, so things like uglifyjs do not match for me. However, although it is possible to run uglifyjs and then using V8 c++ api parse uglifyjs output and build own C++ AST on the top, it seems to me and overcoding when there's good ready parser inside v8, just need to learn how to invoke it.

суббота, 14 сентября 2013 г., 16:55:30 UTC+3 пользователь Ben Noordhuis написал:

Louis Santillan

unread,
Sep 17, 2013, 12:22:27 AM9/17/13
to v8-u...@googlegroups.com
Maybe look at something like tiny-js which has a much simpler codebase (https://code.google.com/p/tiny-js/).

-L
--
--
v8-users mailing list
v8-u...@googlegroups.com
http://groups.google.com/group/v8-users
---
You received this message because you are subscribed to the Google Groups "v8-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-users+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Andreas Rossberg

unread,
Sep 17, 2013, 4:42:17 AM9/17/13
to v8-u...@googlegroups.com
This comes up frequently, but the answer remains the same: scrap the
idea. ;) Neither the V8 parser nor its AST are designed for external
interfacing. In particular (1) V8's AST does not necessarily reflect
JavaScript syntax 1-to-1, (2) we change it all the time, and (3) it
depends on various V8 internals. And since all these points are
important for V8, don't expect the situation to change.

/Andreas

Tobias Mueller

unread,
Sep 24, 2013, 4:19:35 AM9/24/13
to v8-u...@googlegroups.com
Hi.

On Mon, Sep 16, 2013 at 02:47:10PM -0700, Ivan P. wrote:
> Yes, I know, but this is what I'm asking about - does anyone could get it
> out from the deep inside of v8 and make use of it?
I might be doing something related that involves me having to inspect
the AST.

> In other worlds, what needs to be done to instantiate v8's parser object,
> run parsing and achieve AST? Any examples of the code?
What I just did was to hook into MakeCode of compiler.cc.
I put my own AST traversal class in this chain, somewhere before GenerateCode:
return Rewriter::Rewrite(info) && Scope::Analyze(info) && GenerateCode(info);

As there doesn't seem to be a generic AST traversal class, I just copied the
prettyprinter and replaced the printing logic with my own.


HTH,
Tobi
Reply all
Reply to author
Forward
0 new messages