JavaScript on the command line with the V8 compiler
A JIT compiler that can run in a browser or independently use JavaScript locally as a scripting language.
This compiler is provided by Google in an open source format, which allowed the development of tools such as the Node.js server application platform.
Execution speed allows you to use JavaScript to create system programs. It does not interpret the code, but compiles it before executing it, although variables are dynamic and therefore can change type during program development. But there are techniques like type inference to deal with this problem.

The V8 has a collection garage (like stop-the-world, it stops execution for a cleaning cycle), an advantage over the C++ language. It uses Smalltalk-based assembler (Strongtalk) to create native code from high-level bytecode.
Implements (in 2012) ECMA-262, the third edition of JavaScript, runs on Windows, MacOS and Linux (ARM or x386).
The V8 is very fast, and one wonders if it needs Asm.js at the sight of the last benchmark (in October 2013) of the Google team that compares the V8 compiler to the Firefox compiler, both with Asm.js code.
Comparative speed of Asm.js code execution in Firefox and Chrome (Google source)
A value of 1 indicates the bit rate
One could believe that Asm.js was implemented in Chrome. This is not the case, the speed of V8 is such that in some cases JavaScript code (not only a subset of Asm.js) works as quickly as with the specialized Asm.js compiler from Firefox!
Use V8 as a scripting tool
Thus, with V8, it is possible to use JavaScript as a scripting language and possibly provide GUI scripts outside the browser by integrating them with C++ code and graphics libraries.
V8 is designed as a bookstore that can be linked to any C++ project and therefore use the compiler as a function of a larger system.
This is what the Chrome browser, for which it was written, does and for which it provides faster execution speed than competitors. However, they tend to use the same techniques and catch up.
It can be integrated into a C++ program as a library or used independently to execute, as the interpreter does, JavaScript programs. In the second case, you need to create a manual function that serves to call the parser, provide it with source code for compilation and display the compilation result.
But the generated code can work in collaboration with the bookstores included in the project.
When composing, to execute the program, you need to create a C++ thrower that will be bound to V8 code before compiling it. This creates an interpreter that can parse and execute JavaScript scripts.
int main(int argc, char* argv[]) { String source = String::New($argv[1]); Script script = Script::Compile(source); Value result = script->Run(); String::AsciiValue ascii(result); printf("%s\n", *ascii); return 0; }
A detailed version and instructions for building a JIT interpreter can be found on the V8 website.
But there is a much simpler solution.
Running JavaScript on the Command Line

Node.js is a server infrastructure written in JavaScript designed for applications that can evolve without time constraints. It is asynchronous and responds, like HTML applications, to events. It is used by Microsoft for the Azure service, eBay, LinkedIn for mobile devices, Yandex search engine, Yahoo for the Manhattan project.
And you can use it to run your own scripts, too.
Suppose your script is in the hello.js file.
Download and install Node.js, go to the directory where the executable node.exe is located, and type:
node hello.js
That's all!
JavaScript Command Line I/O
To communicate with the script, provide it with information, Node has a process object.
The process.argv attribute contains the parameters passed in the command line.
Thus, if the command is node hello.js moi, the content of process.argv is:
- node.exe
- hello.js
- i am a
The length attribute specifies the number of arguments:
process.argv.length
And you can use the console object to view the results.
console.log("Hello toi!");
Full demo scenario:
console.log("Arguments:" +process.argv.length);
var name = process.argv[2];
console.log("Hello " + name + "!");
But with Node.js, you can go far beyond that...
- File system access.
- Use the HTML 5 interface using WebSocket.
- Create Local Server... etc
- .
This JavaScript folder provides demonstrations.
Other implementations for using JavaScript on the command line
Reference