Rhino (JavaScript engine)
Encyclopedia
Rhino is an open source
Open source
The term open source describes practices in production and development that promote access to the end product's source materials. Some consider open source a philosophy, others consider it a pragmatic methodology...

 JavaScript engine
JavaScript engine
A JavaScript engine is specialized computer software which interprets and executes JavaScript . Although there are several uses for a JavaScript engine, it is most commonly used in web browsers.-History:...

. It is developed entirely in Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...

 and managed by the Mozilla Foundation
Mozilla Foundation
The Mozilla Foundation is a non-profit organization that exists to support and provide leadership for the open source Mozilla project. The organization sets the policies that govern development, operates key infrastructure and controls trademarks and other intellectual property...

. The Foundation also provides another implementation of JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

 engine written in C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....

 known as SpiderMonkey.

Rhino converts JavaScript scripts into classes. Rhino works in both compiled
Compiler
A compiler is a computer program that transforms source code written in a programming language into another computer language...

 as well as interpreted
Interpreter (computing)
In computer science, an interpreter normally means a computer program that executes, i.e. performs, instructions written in a programming language...

 mode. It is intended to be used in server-side
Server-side
Server-side refers to operations that are performed by the server in a client–server relationship in computer networking.Typically, a server is a software program, such as a web server, that runs on a remote server, reachable from a user's local computer or workstation...

 applications, hence there is no built-in support for the browser
Web browser
A web browser is a software application for retrieving, presenting, and traversing information resources on the World Wide Web. An information resource is identified by a Uniform Resource Identifier and may be a web page, image, video, or other piece of content...

 objects that are commonly associated with JavaScript.

Rhino can be used as a debugger
Debugger
A debugger or debugging tool is a computer program that is used to test and debug other programs . The code to be examined might alternatively be running on an instruction set simulator , a technique that allows great power in its ability to halt when specific conditions are encountered but which...

 by making use of the Rhino shell. It can be used in applications by embedding Rhino.

A slightly modified version of Rhino 1.6r2 comes bundled with the Sun Microsystems
Sun Microsystems
Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...

 release of Java SE version 6, which was released in December 2006. This makes it easier to integrate JavaScript as part of Java programs and to access Java resources from JavaScript. Other implementations of Java 6 may differ.

History

The Rhino project was started at Netscape in 1997. At the time, Netscape was planning to produce a version of Navigator
Netscape Navigator
Netscape Navigator was a proprietary web browser that was popular in the 1990s. It was the flagship product of the Netscape Communications Corporation and the dominant web browser in terms of usage share, although by 2002 its usage had almost disappeared...

 written entirely in Java and so it needed an implementation of JavaScript written in Java. When Netscape stopped work on "Javagator", as it was called, the Rhino project was finished as a JavaScript engine. Since then, a couple of major companies (including Sun Microsystems
Sun Microsystems
Sun Microsystems, Inc. was a company that sold :computers, computer components, :computer software, and :information technology services. Sun was founded on February 24, 1982...

) have licensed Rhino for use in their products and paid Netscape to do so, allowing work to continue on it.

Originally, Rhino compiled all JavaScript code to Java bytecode
Java bytecode
Java bytecode is the form of instructions that the Java virtual machine executes. Each bytecode opcode is one byte in length, although some require parameters, resulting in some multi-byte instructions. Not all of the possible 256 opcodes are used. 51 are reserved for future use...

s in generated classfiles. This produced the best performance (often beating the C implementation of JavaScript when run on a JIT
Just-in-time compilation
In computing, just-in-time compilation , also known as dynamic translation, is a method to improve the runtime performance of computer programs. Historically, computer programs had two modes of runtime operation, either interpreted or static compilation...

), but suffered from two faults. First, compilation time was long since generating Java bytecodes and loading the generated classes was a heavyweight (resource-intensive) process. Also, the implementation effectively leaked memory
Memory leak
A memory leak, in computer science , occurs when a computer program consumes memory but is unable to release it back to the operating system. In object-oriented programming, a memory leak happens when an object is stored in memory but cannot be accessed by the running code...

 since most JVMs
Java Virtual Machine
A Java virtual machine is a virtual machine capable of executing Java bytecode. It is the code execution component of the Java software platform. Sun Microsystems stated that there are over 4.5 billion JVM-enabled devices.-Overview:...

 don't collect
Garbage collection (computer science)
In computer science, garbage collection is a form of automatic memory management. The garbage collector, or just collector, attempts to reclaim garbage, or memory occupied by objects that are no longer in use by the program...

 unused classes or the strings
String (computer science)
In formal languages, which are used in mathematical logic and theoretical computer science, a string is a finite sequence of symbols that are chosen from a set or alphabet....

 that are interned as a result of loading a class file.

So in the fall of 1998, Rhino added an interpretive mode. The classfile generation code was moved to an optional, dynamically loaded
Dynamic loading
Dynamic loading is a mechanism by which a computer program can, at run time, load a library into memory, retrieve the addresses of functions and variables contained in the library, execute those functions or access those variables, and unload the library from memory...

 package. Compilation is faster and when scripts are no longer in use they can be collected like any other Java object.

Rhino was released to Mozilla Foundation
Mozilla Foundation
The Mozilla Foundation is a non-profit organization that exists to support and provide leadership for the open source Mozilla project. The organization sets the policies that govern development, operates key infrastructure and controls trademarks and other intellectual property...

 in April 1998. Originally Rhino classfile generation had been held back from release. However the licensees of Rhino have now agreed to release all of Rhino to open source, including class file generation. Since its release to open source, Rhino has found a variety of uses and an increasing number of people have contributed to the code. The project gets its name from the animal on the cover of the JavaScript book from O'Reilly Media
O'Reilly Media
O'Reilly Media is an American media company established by Tim O'Reilly that publishes books and Web sites and produces conferences on computer technology topics...

. Starting with version 1.7R1, Rhino relies on the Java 5
Java version history
The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community Process , which uses Java Specification Requests to propose and...

 platform, and supports version 1.7 of JavaScript
JavaScript
JavaScript is a prototype-based scripting language that is dynamic, weakly typed and has first-class functions. It is a multi-paradigm language, supporting object-oriented, imperative, and functional programming styles....

.

Example

Below is an example of Java code running JavaScript print('Hello, world!')


import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class RhinoEngine {
public static void main(String[] args) {
ScriptEngineManager mgr = new ScriptEngineManager;
// Now we can go and get a script engine we want.
// This can be done either by finding a factory that supports
// our required scripting language
// (engine = factory.getScriptEngine;)
// or by requesting a script engine that supports a
// given language by name from the script engine manager.
ScriptEngine engine = mgr.getEngineByName("JavaScript");

// Now we have a script engine instance that
// can execute some JavaScript
try {
engine.put("name", args[0]);
engine.eval("print('Hello ' + name + '!')");
} catch (ScriptException ex) {
ex.printStackTrace;
}
}
}


Here is a simple example showing a Rhino script that outputs each line in the stdin converted to uppercase.


// Equivalent in effect to the Java declaration import java.io.*;
importPackage(java.io);
importPackage(java.lang);

// "in" is a keyword in JavaScript.
// In JavaScript you could query for an attribute using [] syntax:
var reader = new BufferedReader( new InputStreamReader(System['in']) );

// We use JavaScript's dynamic typing here to let s be a boolean first, and later on a string...
var s = true;

while (s) {
s = reader.readLine;

if (s) {
System.out.println(s.toUpperCase);
}
}

Rhino Shell

The JavaScript shell provides a simple way to run scripts in batch mode or an interactive environment for exploratory programming. Invoking the Rhino Shell with the command below:
java org.mozilla.javascript.tools.shell.Main

The Rhino Shell can also be invoked with this command:
java -jar js.jar
The advantage is that you don't have to configure the classpath to use this form.

See also

  • JavaScript engine
    JavaScript engine
    A JavaScript engine is specialized computer software which interprets and executes JavaScript . Although there are several uses for a JavaScript engine, it is most commonly used in web browsers.-History:...

  • List of JavaScript engines
  • Mozilla Grendel
    Mozilla Grendel
    Grendel is a mail/news reader by the Mozilla project written entirely in the Java programming language. Its goal is to be a true cross-platform application with a feature set that satisfies the power user.- History :...

  • Nashorn
    Nashorn (JavaScript engine)
    Nashorn is an upcoming JavaScript engine, developed entirely in Java by Oracle Corporation. It will be based on JSR 292 and will be available for Java 8 in late 2012. The project has been announced first at the JVM language summit in July 2011., and then confirmed at JavaOne in October 2011-See...

    , an upcoming Java JavaScript engine developed by Oracle Corporation
    Oracle Corporation
    Oracle Corporation is an American multinational computer technology corporation that specializes in developing and marketing hardware systems and enterprise software products – particularly database management systems...


External links

The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK