I have a QT application in which I want to embed SpiderMonkey. With it I want to parse javascript segment (not execute it) and retrieve all the variables, functions, objects, arrays, etc. in some kind of a structured tree or just list them... Is that possible?
I've looked through the documentation, but could not find anything.
Or perhaps someone can point me to another parser if I can't do that with SpiderMonkey?
If you must do this with a SpiderMonkey embedding, you can use the
JS_InitReflect
function to create aReflect
object in the current global context (documentation). It would require you to write a script to runRelect.parse(/* your javascript segment */)
, but then you've got the AST in a JS object hierarchy. At that point, it should be easy to export a JSON string or whatever you need back to your native code.It would probably be a lot easier to use a project like Esprima and spawn a Node.js process to print out the AST for the JS you want to analyze instead of dealing with the trouble of a full on SpiderMonkey embedding.