Obtaning the AST of JavaScript code from Spidermonkey

762 views Asked by At

I need to obtain the AST of a JS code from C++. I know that Spidermonkey's (unlike V8's generated) ASTs follow a standard that I could use.

Can I pass it a code (string or JS file) from a C++ program and get the AST? If so how?

1

There are 1 answers

0
jwilm On

I just answered a similar question here. The short version is:

  1. Build a runtime, context, and global object as outlined here
  2. JS_InitReflect with your global to instantiate the Reflect object in the global scope (documentation)
  3. Write a bit of JS to parse the code you wish to analyze. Depending on your needs, you may just want to do the analysis in JavaScript or hand a JSON encoded string back to your C++.