I'm calling Clojure from Java and calling eval on a string passed in. The Java code will hold the objects, and the client code can specify strings of Clojure code to run on an object. I know how to call the Clojure code from Java, but how do I pass a variable in?
Here's what I have. First, a simple object to work on:
public class Helloer {
public String getGreeting() { return "Hello"; }
}
Then some boilerplate code to call a Clojure method.
public static String call(Helloer helloer, String expression) throws Exception {
RT.loadResourceScript("EvalObject.clj");
final Var schrodEval = RT.var("eval-object", "eval-string");
final String result = (String) schrodEval.invoke(expression, helloer);
return result;
}
But then I get stuck on the Clojure code. The object is passed in fine, but how do I pass the value into the eval?
Here's what I've tried:
(ns eval-object)
(defn eval-string [string this]
(eval (read-string string)))
(defn eval-string2 [string value]
(def this)
(binding [this value]
(eval (read-string string))))
(defn eval-string3 [string value]
(def this)
(eval (list 'binding (vector 'this 5) (read-string string))))
These give:
java.lang.Exception: Unable to resolve symbol: this in this context (NO_SOURCE_FILE:0)
So then I tried constructing a binding clause that defines this:
(defn eval-string4 [string value]
(def this)
(eval (list 'do (list 'defonce 'this nil)
(list 'binding (vector 'this value) (read-string string)))))
But now I get:
java.lang.RuntimeException: Can't embed object in code, maybe print-dup not defined: Helloer@638bd7f1 (NO_SOURCE_FILE:0)
Am I missing something? Is it possible to pass objects from Java, to Clojure, and into an eval?