SpiderMonkey

Implementation of programming language ECMAScript

SpiderMonkey is a historically first implementation of JavaScript, which later became ECMAScript. It was developed by Netscape in 1995, later released as free software and is now supported and developed by Mozilla Foundation.

SpiderMonkey contains JavaScript compiler and interpreter along with several service programs. This implementation is written in C and is meant to be embedded in other applications. The most common example of its usage is Mozilla Firefox web browser. Since Firefox 3.5 the engine is called TraceMonkey and uses new optimization technique.

Examples:

Factorial:

Example for versions SpiderMonkey 1.7

This example uses recursive factorial definition and is meant to be executed from the web-browser. To run the example, copy its source text to a file factorial.js and create an HTML file placed in the same directory and containing the following text:

<head><script type="text/javascript" src="factorial.js"></script></head> <body></body>

When you open the HTML file, JavaScript code runs and writes its output to the page shown. document.clear() command clears everything that is written on the currrent web-page.

function factorial(n)
{   if (n == 0)
        return 1;
    else
        return n * factorial(n-1);
}
var i;
document.clear();
for (i = 0; i <= 16; i++)
    document.write(i + "! = " + factorial(i) + "<br />");

Fibonacci numbers:

Example for versions SpiderMonkey 1.7

This example uses recursive definition of Fibonacci numbers and is meant to be executed from web-browser.

function fibonacci(n)
{   if (n<3)
        return 1;
    else
        return fibonacci(n-1) + fibonacci(n-2);
}
var i;
document.clear();
for (i = 1; i <= 16; i++)
    document.write(fibonacci(i) + ", ");
document.write("...<br />");

Quadratic equation:

Example for versions SpiderMonkey 1.7

The example is meant to be executed from the web-browser. To run the example, copy the code to a file quadratic.js and create an HTML file placed in the same directory and containing the following text:

<head>
<script type="text/javascript" src="quadratic.js"></script>
</head>
<body>
<form name="quadratic">
    <input type="number" required="required" name="A">
    <input type="number" required="required" name="B">
    <input type="number" required="required" name="C">
    <input type="button" value="Solve" onClick="solve()">
</form>
<p id="output">
</p>
</body>

This will create a web-page with three input fields and a button. The equation will be solved once the button is pressed, and the roots will be printed beneath the inputs.

JavaScript doesn’t provide complex numbers, so the calculations are done in real numbers with separate check for discriminant sign. This implementation allows to solve quadratic equations not only with integer coefficients but also with floating-point ones.

function print(real, imag) 
{   if (Math.abs(imag)<1E-6)
        return real;
    else
        return '('+real+','+imag+')';
}

function solve() 
{   A = document.quadratic.A.value;
    if (Math.abs(A)<1E-3)
    {   document.getElementById('output').innerHTML = 'Not a quadratic equation';
        return;
    }
    B = document.quadratic.B.value;
    C = document.quadratic.C.value;
    A = 2*A;
    D = B*B-2*A*C;
    if (Math.abs(D)<1E-3)
    {   document.getElementById('output').innerHTML = 'x = '+(-B/A);
        return;
    }
    if (D>0)
        document.getElementById('output').innerHTML = 'x1 = '+print((-B+Math.sqrt(D))/A, 0)+'<br />x2 = '+print((-B-Math.sqrt(D))/A, 0);
    else
        document.getElementById('output').innerHTML = 'x1 = '+print(-B/A,Math.sqrt(-D)/A)+'<br />x2 = '+print(-B/A,-Math.sqrt(-D)/A);
}

Hello, World!:

Example for versions Rhino 1.6, SpiderMonkey 1.7

JavaScript uses different commands to output messages depending on what environment is it used in:

  • print: for interpreters with command-line interface prints the message to standard output stream, but when used in web-browser, calls print dialog instead;
  • document.write: when used in web-browser, prints the message to the current document (web-page);
  • console.log: a command for Firebug plugin which prints the message to debug console of the plugin;
  • alert: when used in web-browser, creates a pop-up information window with the message.

Note that three last commands won’t work in non-browser-based environment, since they use objects (document and console) which are not defined in command-line interfaces.

The example itself works in a smart universal way: since it can be executed in multiple environments with various objects defined, it checks each object in turn, and uses the first available writing method.

if (typeof console === 'object') {
    console.log('Hello, World!');
} else if (typeof document === 'object') {
    document.write('Hello, World!');
} else {
    print('Hello, World!');
}

CamelCase:

Example for versions SpiderMonkey 1.7

This example is meant to be executed from web-browser, same as quadratic equation. Input form should look like this:

<form name="CamelCase">
  <input type="text" required="required" name="txt">
  <input type="button" value="Convert to CamelCase" onClick="convert()">
</form>

The code itself could have been written in one line, but has been broken in several parts for better readability. First line gets the string to process; second line converts it to lower case and replaces all non-letter characters with spaces; third line capitalizes each word; and fourth line removes all spaces. JavaScript has a very strong support of regular expressions, so this is done easily.

function convert() {
  txt = document.CamelCase.txt.value;
  txt = txt.toLowerCase().replace(/[^a-z ]+/g, ' ');
  txt = txt.replace(/^(.)|\s(.)/g, function($1) { return $1.toUpperCase(); });
  txt = txt.replace(/[^a-zA-Z]+/g, '');
  document.getElementById('output').innerHTML = txt;
}