source: trunk/abcl/examples/jsr-223/JSR223Example.java

Last change on this file was 11856, checked in by astalla, 15 years ago
  • loading:

added a new primitive sys::load-returning-last-result which behaves like
load but returns the last value produced instead of T

  • JSR-223:
    • used the new load-returning-last-result to evaluate both interpreted and compiled code for consistency (with a caveat, see the wiki page on JSR-223)
    • bindings established through ScriptContext? are now declared special
    • compilation using the runtime compiler has been removed due to inconsistencies with evaluation and file-based compilation
    • updated the example as suggested on the ML to show both modes of getting the AbclScriptEngine?


File size: 2.5 KB
Line 
1import javax.script.*;
2
3public class JSR223Example {
4
5  public static void main(String[] args) {
6      //Script Engine instantiation using ServiceProvider - this will
7      //look in the classpath for a file
8      //  /META-INF/services/javax.script.ScriptEngineFactory
9      //where the AbclScriptEngineFactory is registered
10      ScriptEngine lispEngine = new ScriptEngineManager().getEngineByExtension("lisp");
11
12      //Alternatively, you can directly instantiate the script engine:
13     
14      //ScriptEngineManager scriptManager = new ScriptEngineManager();
15      //scriptManager.registerEngineExtension("lisp", new AbclScriptEngineFactory());
16      //ScriptEngine lispEngine = scriptManager.getEngineByExtension("lisp");
17
18      //(thanks to Peter Tsenter for suggesting this)
19       
20    //Accessing variables
21    System.out.println();
22    System.out.println("*package* = " + lispEngine.get("*package*"));
23    Object someValue = new Object();
24    lispEngine.put("someVariable", someValue);
25    System.out.println("someVariable = " + lispEngine.get("someVariable"));
26    try {
27      //Interpretation (also from streams)
28      lispEngine.eval("(defun hello (arg) (print (list arg someVariable)) (terpri))");
29     
30      //Direct function invocation
31      ((Invocable) lispEngine).invokeFunction("hello", "world");
32     
33      //Implementing a Java interface in Lisp
34      lispEngine.eval("(defun compare-to (&rest args) 42)");
35      Comparable c = ((Invocable) lispEngine).getInterface(java.lang.Comparable.class);
36      System.out.println("compareTo: " + c.compareTo(null));
37     
38      //Compilation!
39      lispEngine.eval("(defmacro slow-compiling-macro (arg) (dotimes (i 1000000) (incf i)) `(print ,arg))");
40     
41      long millis = System.currentTimeMillis();
42      lispEngine.eval("(slow-compiling-macro 42)");
43      millis = System.currentTimeMillis() - millis;
44      System.out.println("interpretation took " + millis);
45     
46      millis = System.currentTimeMillis();
47      CompiledScript cs = ((Compilable) lispEngine).compile("(slow-compiling-macro 42)");
48      millis = System.currentTimeMillis() - millis;
49      System.out.println("compilation took " + millis);
50     
51      millis = System.currentTimeMillis();
52      cs.eval();
53      millis = System.currentTimeMillis() - millis;
54      System.out.println("evaluation took " + millis);
55
56      millis = System.currentTimeMillis();
57      cs.eval();
58      millis = System.currentTimeMillis() - millis;
59      System.out.println("evaluation took " + millis);
60
61      //Ecc. ecc.
62    } catch (NoSuchMethodException e) {
63      e.printStackTrace();
64    } catch (ScriptException e) {
65      e.printStackTrace();
66    }
67  }
68 
69}
Note: See TracBrowser for help on using the repository browser.