Without knowing of Niels Mayer's work (until just now) I wrote an
implementation of the Java Scripting API for Clojure --and even named
it the same. Indeed great minds think alike. Though I didn't think of
isolating the runtime in different instances of the engine.

The distribution ZIP file has the clojure-jsr223.jar that goes alog
with clojure.jar, the javadoc, sources and samples. You may get it
here:
http://code.google.com/p/clojure-jsr223/downloads/list

You can run code, set bindings, call functions, get interface
implementations, compile libs, and get some info from the factory. The
engine will redirect in/out/err according to what's in the
SimpleScriptContext argument, whose defaults have no effect on the RT.

I hope this will be useful. I'll check-in the source in google code as
soon as they resolve some issue with the repository.

Will appreciate suggestions on how to better do the work of wrapping
the RT. For example, to get an implementation of a Java interface, the
library requires that something like this has bee previously
evaluated:
...
(def ActionListenerImpl
  (proxy [ActionListener] []
    (actionPerformed [evt]
      (println \"button pushed\"))))

and then you do:
...
button.addActionListener(invoke.getInterface(ActionListener.class));

So with this convention, the library looks for a var named after the
required interface plus the Impl suffix. (Or invoke.getInterface
("other.ns", ActionListener.class) if it's in a namespace other than
user.) Why a var and not a function? I'm not so sure why now; it may
was well be a function. I guess I thought it'd help to make it a bit
different than your other functions.

Here's some sample from the JDK but changed for Clojure:

ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("Clojure");

engine.eval("(println \"Hello, World\")");
engine.eval(new java.io.FileReader("code.clj");

String script = "(defn hello [name] (println \"Hello\" name))";
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction("hello", "Scripting!!" );

File f = new File("samples/jsr223/ScriptVars.java");
engine.put("file", f);
engine.eval("(println (.getAbsolutePath file))");

engine.eval(
"(import java.awt.event.ActionListener)" +
"  (def ActionListenerImpl             " +
"    (proxy [ActionListener] []        " +
"      (actionPerformed [evt]          " +
"        (println \"button pushed\"))))");

Invocable invoke = (Invocable) engine;

JFrame frame = new JFrame();
JButton button = new JButton("Push");
button.addActionListener(invoke.getInterface(ActionListener.class));
frame.getContentPane().add(button);
frame.pack();
frame.setVisible(true);

Compilable compile = (Compilable) engine;
compile.compile("reader.snake");

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to