How to traverse the AST generated by spidermonkey (Javascript file) - javascript

I am very very new to Spidermonkey Parser and I have two questions. The first one is to have a good documentation (for beginners) about how to generate the AST of a Javascript file with spidermonkeyParser. And the second one is to know how to traverse the AST. My goal is to use the information in the AST to do some static analysis of the JS files (Type analysis, String analysis).
Thank you

The AST of Spidermonkey is exposed as Javascript objects. That means you should write a small Javascript script, use that script to read and parse the actual Javascript source file your want to parse and obtain the AST as Javascript objects.
This feature is available in the standalone SpiderMonkey shell (probably not in the version that comes with Firefox). You need to download the full Spidermonkey source and build it using the bundled python scripts. A Shell will be built along with your standalone Spidermonkey Javascript engine. This shell is just a small console program accepting user commands. The shell can read and execute standalone Javascript scripts. In particular, those Javascript scripts executed by this Shell have access to an extra global object called Reflect which has a method called parse(). The Shell also support extra file I/O functions which is the way you read in the target Javascript source you want to parse.
The description of the full AST is here:
https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API

Related

What happens to the Javascript "wrapper" code when the NodeJs source code gets compiled to binary?

I just recently started a Course about Nodejs, where its internals are covered just to get a good mental model about how it is built and how it works. Yet, something that I can't find information about is what happens to the "wrapper Js code" that is bound (through the process.binding('<CppObjName>')) to the compiled C++ Objects when NodeJs Itself gets compiled for a specific platform:
Is it stored somewhere in the host machine as plain text and then fed (say, "loaded") as javascript to the V8 when the NodeJs executable (NodeJs Itself) is run?
Or is it "compiled" to a specific binary format and bundled together with the rest of the compiled C++ Code and V8 (so when when the NodeJs executable is run, such binary is the one loaded into the V8 in order to provide such Objects to the Node API)?

XML to/from Java/Kotlin, cross-platform

I have an XML format specified with XSD and I want to generate the corresponding Java (or Kotlin) sources including (de)serialization. I know JAXB, but as far as I understand, it heavily relies on reflection. This is no option for me, because I want the generated sources to be ready for transpilation to JavaScript (by GWT or Kotlin/JavaScript for example). Because of this, these are the requirements:
Using an XSD schema, generate the corresponding Java or Kotlin source code
Do not use reflection, but plain old Java classes which include serialize/deserialize methods
Inject an XML IO interface (like StaX) with the required methods (which I can instatiate depending on the target: JVM, JavaScript, Android, ...)
Do you know a tool for this task or do I have to create my own solution?

Embed Haskell in Node.js

I am writing a Node.js service that uses various utilities from the Node ecosystem, but the core part of the logic needs to generate a JavaScript AST (ESTree) from some given JSON configuration. It will also include reading various JS files and combining them if required. I am designing the code generation part by defining a domain specific grammar.
The Haskell code will return a ESTree AST which Node.js will convert to JS code using some existing utility.
I am looking for a good way to call Haskell code from Node.js to achieve this.

Kotlin: What is a kjsm file?

I have been trying to use the Kotlin -> js compiler by following this tutorial.
When I run kotlinc-js --help, then the help text mentions the following:
-kjsm Generate kjsm-files (for creating libraries)
What is a kjsm file?
A kjsm-file is a Kotlin JavaScript Meta file (see KotlinJavaScriptMetaFileType).
Such a file appears to be used to provide meta data for native JavaScript objects so that the Kotlin compiler can type-check things and so that an IDE can provide code completion, etc. e.g. If you look in kotlin-js-library-1.0.6.jar you will find, among other kjsm-files, a Window.kjsm file which defines the Window Web API available in web browsers.
You would want to generate your own kjsm-files whenever you are creating a library so that your interfaces can be used by the compiler/IDE in modules which depend on your Kotlin JavaScript library.

Is there any javascript engine for doing file operations

Like to know is there any javascript engine for doing file operations such as create, reads, write , and parse file in file system.
You can use the File System API, but this is for a virtual sandboxed filesystem you need to create yourself (by making your own files or having user select them from their filesystem). Obviously you can't just access the real file system directly in a browser.
For node.js, use the fs module.
Javascript has no such thing as file operations natively.
The word engine is a bit vague here. There are a couple of different javascript interpreters. V8, Spidermonkey, JavaScriptCore, etc. These implmement only Javascript.
What you need is an extension - a library (typically written in C) which exposes this functionality through the interpreter into the script scope as javascript objects and functions.
node.js is a good example of a suite of javascript extensions with support for this. It is based on the V8 javascript interpreter.

Categories

Resources