-
Notifications
You must be signed in to change notification settings - Fork 0
JavaScript Shells
Oh My Goodness!!
Another concept in the making?
Another new thing to learn?
Adding fatigue to my already existing fatigue?
What is going on with these people on the internet?
People are crazy!!!
These might be the emotions you might experience if you have never heard of a JavaScript Shell.
But, honestly, it is very important to know that these shells exist.
And that, they help us in understanding JavaScript a little better. And add a little more fun.
Let us step away from the world of JavaScript.
And step into the world of Operating Systems.
And we all revere the Shell.
In all its holiness.
A shell is an interaction medium for the user. He/She can interact with the operating system using the shell. And perform operations by issuing commands. It is a textual interface and so people type in the name of the command. A prompt is presented to the user to type in the next commands. Each command can be thought of as a program. Some of these commands can be sitting as an executable file on the disk. Or sometimes, they could be a part of the shell itself. Whatever it is, the command name is sought as an input. A search is made and if such a command exists, the shell tries to execute the command.
Shells have existed in the majority of the operating systems, as a text interface was first conceived and was the only way to interact with the OS and then only we had the notion of a graphical interface.
bash, cmd, terminal, whatever you want to call it, based on the OS you are using, they essentially disembark the same responsibility.
- Present a prompt
- Let user type in a command
- Find the command
- Execute it
- If command not found, throw an error
For example, out of immediate interest a user might want to list out the contents of the present directory he/she is in and might issue a command ls or a dir (on Windows). The necessity of the task at hand dictates the command to be used.
And the command is executed and it responds in whatever way it is supposed to. Such as displaying the contents of the current directory.
sameeri@smarryboyina MINGW32 /c/sameeri/adventures
$ ls
v8/ Also shells are closest to the user in terms of interaction.
Shells also provide certain programming constructs to enable grouping of commands and automating a complex task. It allows for repetition and more.
Thus shells are very powerful tools when it comes to the Operating System.
And it is very essential for any serious developer/engineer to work with a shell on a daily basis.
Certain programming languages, owing to their nature, provide language shells.
These languages shells are programs that act like a shell themselves.
Except for the fact that you can type language commands, instead of program names.
Why is it useful?
Well, you can quickly play with little elements/constructs of the language without writing heavy programs, and focus on something very simple.
For example you can type in a JS code snippet and observe it in action.
Say, you were trying to understand the instanceof operator.
You can type in a constructor function and create instances of the constructor function and ask whether an object is an instanceof the constructor function or not.
function Coffee(roast, cream, sugar){
this.roast = roast;
this.cream = cream;
this.sugar = sugar;
}
var c = new Coffee('Pike', 'Whole Milk', '2 Spoons');
c instanceof Coffee //Should be trueThis code block could be anything.
By anything meaning, pertaining to a certain language.
Here we have used JavaScript as an example.
Languages such as ruby, perl, 'python, elixir` all have similar tools.
They could be simple called shell or an interactive shell or more often a REPL (short for Read-Evaluate-Print-Loop).
You could type in some ruby code in the ruby shell.
Language elements can tested, when in doubt.
It becomes a neat little laboratory.
These tools are possible because the way these languages work is a little different than traditional languages.
You do not need to create a file, type code in, compile it and then run it. To see your code in action.
The language execution methods are different for each language.
Thus enabling such tools to be available for some and absent for others.
Just as other language shells, a JavaScript shell is a program that you can use to type in JavaScript commands.
You can type in JavaScript language constructs and observe the behavior of the language.
Initially, people had been using html and the script tag to include JavaScript inline or as a separate file.
And only so could one observe the nature of the language.
Inside the browser.
Then came along a tool inside Firefox - Firebug.
Firebug was a Firefox extension.
It let people play with JavaScript and observe the environment.
It was a neat thing because nothing like that existed previously.
And it became a super hit among developers.
All vendors started to then implement this idea in their browsers.
And the Developer tools are now taken for granted.
You open up Dev tools, hit the console tab and start playing away.
It is magical and it is an important tool in a web developer's toolbox.
Since browsers are the most common programs that people use (may be the most, too), anyone can type in JavaScript and quickly learn the constructs.
Also cool is that we can work with the objects of the browser such as window, document, the XmlHttpRequest and so on.
That is we have complete access to the environment as well.
Thus really making it an indispensable tool.
We do not need to install any special language tools. Like we need to in other languages.
The word ubiquitous truly stands.
Sometime later, people just built online tools to allow people to quickly build experimental code.
Tools such as www.jsfiddle.net, https://jsbin.com, https://codepen.io/
These essentially let you do the same except that they are online.
And an interesting feature is you could save your experiments online.
Then, node came along!
node as a program has to be downloaded and installed.
It can be run at the Operating System shell, as such
$ node
> 1 + 1
2
> Object.Simply running node, without any arguments, starts the node's repl.
And you can type in JavaScript as you could inside the console of a browser.
node also accepts arguments.
The first argument should be a file that has JavaScript in it.
And any other arguments after the program file are treated as arguments for the program itself.
But the biggest thing is
node acts as a repl when no arguments are provided.
node repl feels more like a traditional shell as it runs at the command line.
The biggest differences are based on the environment itself.
Each environment exposes the details of the environment to the user.
Hence in the browser, window would yield the window object and on node global would
yield the global object.
These are unique to the environment and would not be available on the other.
So a window in node would be a ReferenceError and a global in browser would be too.
So, inside node, you could use the require() function to require modules, as it would be available.
The browsers would also provide auto-completion, something very convenient.
mongodb is an open source document store. Its a NoSql database.
When we download an install mongo, we have a lot of binary execution units that come along.
One of them is the mongo shell.
It lets us connect to mongo databases on the local machine or on a remote server.
But what is neat about the mongo shell is it is also a JavaScript shell under the covers.
You can type in JavaScript and work with code as in any other shell.
Assuming the local mongo server is running,
C:\Program Files (x86)\Microsoft Visual Studio 14.0>mongo
2016-12-13T15:19:23.228-0600 I CONTROL [main] Hotfix KB2731284 or later update
is not installed, will zero-out data files
MongoDB shell version: 3.2.1
connecting to: test
> ['apples', 'grapes', 'bananas'].reverse()
[ "bananas", "grapes", "apples" ]
>A little less known shell is the JavaScript shell or simply JS.
It is provided by Mozilla with its JS Engine implementation, SpiderMonkey.
You can obtain the js shell by downloading a binary from the Nightly builds or by building SpiderMonkey.
And the you can run the program at the command line, interactively.
c:\sameeri\adventures\jsshell-win64>js
js> "Hello World".length
11
js>
d8 on the other hand is provided with Google's JS Engine implementation, V8.
However there is only one way you can get d8. By building the source code.
No binaries here.
If you have successfully built V8, then you can run d8, as you would have js.
c:\sameeri\adventures\v8\build\Debug>d8
V8 version 5.7.0 (candidate)
d8> var o = {'name': 'value'}
undefined
d8> o
{name: "value"}
d8> o.name
"value"
d8>What do the JS shells teach us?
That a JavaScript Engine is acting behind the scenes.
We can access the tool to quickly test out some code.
And that each tool exposes its own environment & associated objects, functions and constructs to the user.
You might have come across something similar with JavaScript or any other progamming language.
The principal idea among all these language shells remains the same.
Since we have a variety of JavaScript Engine implementations and since other tools embed one of these inside them, we are all exposed to the same idea in many different ways.