Scripting API (JSR 223) allows you to write Java applications that embed a scripting engine and execute user's scripts through a standardized API (javax.script package).
Pnuts engine for JSR223 is ready for use when pnuts.jar and pnuts-jsr223.jar is in your CLASSPATH.
import javax.script.*;
public class HelloWorld {
public static void main(String[] args) throws Exception {
//Initiate ScriptEngineManager
ScriptEngineManager manager = new ScriptEngineManager();
//Return Pnuts engine by name
ScriptEngine jsengine = manager.getEngineByName("pnuts");
//Execute the HelloWorld script
jsengine.eval(new java.io.FileReader("HelloWorld.pnut"));
}
}
import javax.script.*;
public class CompilableInterface {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine pnutsEngine = manager.getEngineByName("pnuts");
Compilable eng = (Compilable)pnutsEngine;
CompiledScript scr = eng.compile(new java.io.FileReader("CompilableInterface.pnut"));
ScriptEngine e = (ScriptEngine)eng;
//set engine scope namespace
Bindings n = new SimpleBindings();
e.setBindings(n, ScriptContext.ENGINE_SCOPE);
//evaluate compiled script
for (int i=0; i<3; i++) {
n.put("count", new Integer(i));
n.put("currentTime", new Long(System.currentTimeMillis()));
scr.eval();
}
}
}
counter = 0
switch (count) {
case 0: counter = "first time"; break;
case 1: counter = "second time"; break;
case 2: counter = "third time";
}
println("\nRun compiled script, the ", counter)
time = date(currentTime)
println("Current time is ", time)
To configure a Pnuts scripting engine, call Pnuts.setDefault(Properties) method with any of these properties before Pnuts API is loaded.
Property Description Default pnuts.modules Specifies the modules to be used when the engine is initialized. It should be a comma-separated list. pnuts.tools pnuts.lang.defaultPnutsImpl Specifies the class of the default Pnuts Implementation object. See 'The pnuts.lang.defaultPnutsImplProperty' for more details.pnuts.compiler.CompilerPnutsImpl pnuts.lang.defaultConfiguration If a vaild pnuts.lang.Configurationsubclass is specified, an instanceof the class is created with the default constructor and it becomes the default Configuration.The system's default pnuts.compiler.optimize If true is specified, Pnuts compiler generates a little faster code. Instead, line number information is not printed when an error occurs. false pnuts.compiler.useDynamicProxy If true is specified, method/constructor call is implemented by dynamic proxy class, rather than reflection API. Method/constructor call could be a little faster using with JIT compiler. true pnuts.package.factory If this property is specified at start up time, the class becomes the default package factory. See pnuts.lang.PackageFactory. The system's default
C:\> jrunscript -l pnuts
Alternatively, if the JAR files are located as follows,
C:\> jrunscript -J-Djava.ext.dirs=%PNUTS_HOME%\lib;%PNUTS_HOME%\modules -l pnutsIf you are using bash, the command can be shortened by defining an alias as follows.
alias jrunscript='jrunscript "-J-Djava.ext.dirs=d:/pnuts/lib;d:/pnuts/modules"'Then, Pnuts engine can be used via jrunscript as if the JAR files are in ${JRE}/lib/ext/.
bash$ jrunscript -q Language ECMAScript 0 implemention "Mozilla Rhino" @IMPLEMENTATION.VERSION@ Language Pnuts 1.1 implemention "Pnuts" 1.0 bash$ jrunscript -l pnuts pnuts>