Thursday, 22 July 2010

Rhino API - Introduction

Use: To execute Javascript from java classes.

How Rhino achieves it -
To execute any code what all needed is the context i.e. call stack, local registers etc and the scope which contains all lookup variables and functions. Source gets compiled to produce object files. And object files are used for execution. Rhino uses the same concept for Javascript execution.

1) Create Context (Context cx = Context.enter())
2) Initialize scope (Scriptable scope = cx.initializeStandardObjects())
3) Populate scope(scope.put(name,value))
4) Compile the script (Script script = cx.compileScript(String scriptSource)
5) Execute the script (script.execute(cx,scope))

Context = It represents runtime context which contains all the information for execution of javascript.
Scope = it contains standard variables, functions, their definitions which are looked up while execution of javascript.
Script = It is an output of compilation of javascript. (Like a .class file for java class)

Check this.

No comments: