0x11-dev / pyv8

Automatically exported from code.google.com/p/pyv8
0 stars 0 forks source link

display all the nodes of the generated AST #170

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
import PyV8
with PyV8.JSContext() as c:
  with PyV8.JSEngine() as e:
   s = e.compile("document.write("<h2>Table of Factorials</h2>");
for(i = 1, fact = 1; i < 10; i++, fact *= i) {
    document.write(i + "! = " + fact);
    document.write("<br>");
}
")
   s.visit

It is mentioned a 'callback handler' is required. I am unable to determine what 
exactly is this callback handler and how to use it

visit(<boost...api: object handler)

What is the expected output? What do you see instead?
 tree to be displayed

output: object at address

What version of the product are you using? On what operating system?
O.s ubuntu 11.04
v8 pyv8 python

Please provide any additional information below.

Original issue reported on code.google.com by little.cherub13 on 12 Apr 2013 at 8:43

GoogleCodeExporter commented 9 years ago
Sorry for the poor document, you could dump AST with a visitor

import PyV8
import json

class Visitor(object):
    def onProgram(self, prog):
        self.ast = prog.toAST()

        self.json = json.loads(prog.toJSON())

with PyV8.JSContext() as c:
    with PyV8.JSEngine() as e:
        s = e.compile("""document.write("<h2>Table of Factorials</h2>");
        for(i = 1, fact = 1; i < 10; i++, fact *= i) {
            document.write(i + "! = " + fact);
        document.write("<br>");
        }
        """)

        visitor = Visitor()

        s.visit(visitor)

        print json.dumps(visitor.json, sort_keys=True, indent=4, separators=(',', ': '))

please check TestAST for more detail

https://code.google.com/p/pyv8/source/browse/trunk/PyV8.py#2298

Original comment by flier...@gmail.com on 15 Apr 2013 at 2:09

GoogleCodeExporter commented 9 years ago
Thanks for the help!!! It helped me alot :)..

Original comment by little.cherub13 on 16 Apr 2013 at 8:17

GoogleCodeExporter commented 9 years ago
Is it possible to obtain the call graph from the AST generated using Pyv8. 

Original comment by audiotun...@gmail.com on 17 Apr 2013 at 5:17

GoogleCodeExporter commented 9 years ago
Is it possible to manipulate the abstract syntax tree generated using pyv8. How 
to do it?? Which are the functions to be used? 

Original comment by pooja.ba...@gmail.com on 17 Apr 2013 at 5:41