- Rhino (JavaScript engine)
-
Rhino Developer(s) Mozilla Foundation / Mozilla Corporation Stable release 1.7R3 / May 9, 2011 Development status Active Written in Java Operating system Cross-platform Platform Java Virtual Machine Type JavaScript engine License MPL 1.1/GPL 2.0 Website http://www.mozilla.org/rhino/ Rhino is an open source JavaScript engine. It is developed entirely in Java and managed by the Mozilla Foundation. The Foundation also provides another implementation of JavaScript engine written in C known as SpiderMonkey.
Rhino converts JavaScript scripts into classes. Rhino works in both compiled as well as interpreted mode. It is intended to be used in server-side applications, hence there is no built-in support for the browser objects that are commonly associated with JavaScript.
Rhino can be used as a debugger 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 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.
Contents
History
The Rhino project was started at Netscape in 1997. At the time, Netscape was planning to produce a version of Navigator 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) 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 bytecodes in generated classfiles. This produced the best performance (often beating the C implementation of JavaScript when run on a JIT), 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 since most JVMs don't collect unused classes or the strings that are interned as a result of loading a class file.[citation needed]
So in the fall of 1998, Rhino added an interpretive mode. The classfile generation code was moved to an optional, dynamically loaded 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 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.[1] The project gets its name from the animal on the cover of the JavaScript book from O'Reilly Media.[2] Starting with version 1.7R1, Rhino relies on the Java 5 platform, and supports version 1.7 of JavaScript.[3]
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().toUpperCase(); if (s) { System.out.println(s); } }
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
- List of JavaScript engines
- Mozilla Grendel
- Nashorn, an upcoming Java JavaScript engine developed by Oracle Corporation
References
- ^ "Rhino History". Mozilla Foundation. http://www.mozilla.org/rhino/history.html. Retrieved 2008-03-20.
- ^ O'Reilly's Javascript book, inspiration for the Rhino name
- ^ "New in Rhino 1.7R1". Mozilla Foundation. https://developer.mozilla.org/en/docs/New_in_Rhino_1.7R1. Retrieved 2008-03-20.
External links
- Rhino - JavaScript for Java
- Download Rhino
- Download Archive (Main download page above doesn't work currently)
- 5 things you didn't know about ... the Java Scripting API by Ted Neward
Mozilla Project Mozilla LabsMozilla ResearchBoot 2 Gecko · RustCamino · Firefox (2 · 3 · 3.5 · 3.6 · 4 · 5 · 6 · 7 · 8 · 9 · 10 · 11 · 12 · Mobile) · SeaMonkey
more- Origin and lineage: Mozilla Application Suite · Netscape Navigator · Netscape Communicator · Netscape Communications · Beonex Communicator
- Forks: BurningDog · Flock · Gnuzilla · Iceape · IceCat · Icedove · Iceowl · Miro · Netscape 9 · Portable Edition · Songbird · Swiftfox · Swiftweasel · TenFourFox · XeroBank
- Framework: add-on · Gecko · Necko · XBL · XPCOM · XPConnect · XPInstall · XUL · XULRunner
- Componentry: Application Object Model · Composer · NSPR · NSS · Rhino · SpiderMonkey · Tamarin · Venkman
- Project: Personas, Snowl, Test Pilot — Chromatabs, Geode, Joey, Operator, The Coop
- Discontinued: Calendar Project · Grendel · Minimo
Organization Former Mozilla Organization · Mozilla Foundation (Subsidiary: Mozilla Corporation · Mozilla Messaging · Mozilla Online)Official affiliateCommunity See also ECMAScript Dialects ActionScript · Caja · JavaScript (engines) · JScript · JScript .NET · Objective-J · QtScript · WMLScriptEngines
(comparison)Carakan · Futhark · InScript · JavaScriptCore (SquirrelFish) · JScript · KJS · Linear B · Narcissus · QtScript · Rhino · SpiderMonkey (TraceMonkey, JägerMonkey) · Tamarin · V8 · ChakraFrameworks,
librariesJavaScript
(comparison)Ample SDK · Clean AJAX · CougarXML · Dojo · Echo · Ext · Google Web Toolkit · jQuery · Lively Kernel · midori · MochiKit · MooTools · OpenLink AJAX · Prototype · Pyjamas · qooxdoo · Rialto · Rico · script.aculo.us · SmartClient · SproutCore · Spry · YUI LibraryLibraries
(list)PDFObject · SWFObject · SWFAddressActionScriptMultiple
implementationsPeople Other Categories:- JVM programming languages
- Mozilla
- JavaScript engines
- Cross-platform software
Wikimedia Foundation. 2010.