The Mozilla
Organization
At A Glance
Feedback
Get Involved
Newsgroups
License Terms
Newsbot
Developer Docs
Roadmap
Projects
Ports
Module Owners
Hacking
Get the Source
Build It
Testing
Download
Bugzilla
Bug Writing
Tools
View Source
Tree Status
New Checkins
Submit A Bug
FAQ
Search
Tutorial: Embedding Rhino

Embedding Rhino can be done simply with good results. With more effort on the part of the embedder, the objects exposed to scripts can be customized further.

This tutorial leads you through the steps from a simple embedding to more customized, complex embeddings. Fully compilable examples are provided along the way.

The examples live in the rhino/examples directory in the distribution and in mozilla/js/rhino/examples in cvs. This document will link to them using lxr.

In this document, JavaScript code will be in green, Java code will be in green, and shell logs will be in purple.

Contents



RunScript: A simple embedding

About the simplest embedding of Rhino possible is the RunScript example. All it does it read a script from the command line, execute it, and print a result.

Here's an example use of RunScript from a shell command line:

$ java RunScript 'Math.cos(Math.PI)'
-1
$ java RunScript 'function f(x){return x+1} f(7)'
8
Note that you'll have to have both the Rhino classes and the RunScript example class file in the classpath. Let's step through the body of main one line at time.

Entering a Context

The code

Context cx = Context.enter();
Creates and enters a Context. A Context stores information about the execution environment of a script.
 

Initializing standard objects

The code

Scriptable scope = cx.initStandardObjects(null);
Initializes the standard objects (Object, Function, etc.) This must be done before scripts can be executed. The null parameter tells initStandardObjects to create and return a scope object that we use in later calls.

Collecting the arguments

This code is standard Java and not specific to Rhino. It just collects all the arguments and concatenates them together.

String s = "";
for (int i=0; i < args.length; i++)
    s += args[i];


Evaluating a script

The code

Object result = cx.evaluateString(scope, s, "<cmd>", 1, null);
uses the Context cx to evaluate a string. Evaluation of the script looks up variables in scope, and errors will be reported with the filename <cmd> and line number 1.
 

Print the result

The code

System.out.println(cx.toString(result));
prints the result of evaluating the script (contained in the variable result). result could be a string, JavaScript object, or other values..The toString method converts any JavaScript value to a string.
 

Exit the Context

The code

Context.exit();
exits the Context. This removes the association between the Context and the current thread and is an essential cleanup action. There should be a call to exit for every call to enter.
    Expose Java APIs

Use Java APIs

No additional code in the embedding needed! The JavaScript feature called LiveConnect allows JavaScript programs to interact with Java objects:

$ java RunScript 'java.lang.System.out.println(3)'
3.0
undefined
Implementing interfaces

Using Rhino, JavaScript objects can implement arbitrary Java interfaces. There's no Java code to write--it's part of Rhino's LiveConnect implementation. For example, we can see how to implement java.lang.Runnable in a Rhino shell session:

js> obj = { run: function() { print('hi'); } }
[object Object]
js> obj.run()
hi
js> r = new java.lang.Runnable(obj);
[object Object]
js> t = new java.lang.Thread(r)
Thread[Thread-0,5,main]
js> t.start()
hi
Add Java objects

The next example is RunScript2. This is the same as RunScript, but with the addition of two extra lines of code:

Scriptable jsArgs = Context.toObject(System.out, scope);
scope.put("out", scope, jsArgs);
These lines add a global variable out that is a JavaScript reflection of the System.out variable: $ java RunScript2 'out.println(42)'
42.0
undefined
JavaScript host objects

Defining Host Objects

Custom host objects can implement special JavaScript features like dynamic properties.

Counter example

The Counter example is a simple host object. We'll go through it method by method below.

It's easy to try out new host object classes in the shell using its built-in defineClass function. We'll see how to add it to RunScript later. (Note that because the java -jar option preempts the rest of the classpath, we can't use that and access the Counter class.) 

$ java -cp 'js.jar;examples' org.mozilla.javascript.tools.shell.Main
js> defineClass("Counter")
js> c = new Counter(7)
[object Counter]
js> c.count
7
js> c.count
8
js> c.count
9
js> c.resetCount()
js> c.count
0
Counter's constructors

The zero-argument constructor is used by Rhino runtime to create instances. For the counter example, no initialization work is needed, so the implementation is empty.

public Counter () { } The method jsConstructor defines the JavaScript constructor that was called with the expression new Counter(7) in the JavaScript code above. public void jsConstructor(int a) { count = a; } Class name

The class name is defined by the getClassName method. This is used to determine the name of the constructor.

public String getClassName() { return "Counter"; } Dynamic properties

Dynamic properties are defined by methods beginning with jsGet_ or jsSet_. The method jsGet_count defines the count property.

public int jsGet_count() { return count++; } The expression c.count in the JavaScript code above results in a call to this method.

Defining JavaScript "methods"

Methods can be defined using the jsFunction_ prefix. Here we define resetCount for JavaScript.

public void jsFunction_resetCount() { count = 0; } The call c.resetCount() above calls this method.

Adding Counter to RunScript

Now take a look at the RunScript3 example. It's the same as RunScript except that the method ScriptableObject.defineClass uses a Java class to define the Counter "class" in the top-level scope:

ScriptableObject.defineClass(scope, Counter.class); Now we can reference the Counter object from our script: $ java RunScript3 'c = new Counter(3); c.count; c.count;'
4

Copyright © 1998-2000 The Mozilla Organization.
Last modified December 16, 1999.