Skip to content

Commit

Permalink
separate gen from invoke; 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
alandipert committed Jun 18, 2013
1 parent 29c00a8 commit 4197e59
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -40,7 +40,7 @@ Requires Java 1.6 JDK or higher.
## Dependency [![Build Status](https://travis-ci.org/tailrecursion/javastar.png?branch=master)](https://travis-ci.org/tailrecursion/javastar)

```clojure
[tailrecursion/javastar "1.1.1"]
[tailrecursion/javastar "1.1.2"]
```

## License
Expand Down
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject tailrecursion/javastar "1.1.1"
(defproject tailrecursion/javastar "1.1.2"
:description "Write Java inside Clojure"
:url "https://github.com/tailrecursion/javastar"
:license {:name "Eclipse Public License"
Expand Down
30 changes: 14 additions & 16 deletions src/tailrecursion/javastar.clj
Expand Up @@ -74,7 +74,7 @@
[s pattern substitutions]
(reduce #(.replaceFirst ^String %1 pattern %2) s substitutions))

(defn generate-class
(defn ^Class generate-class
"Generates, compiles, and loads a class with a single method, m,
containing the code string. Defines m's signature using
return-type, arg-types, and ~{} occurrences in source as with js*.
Expand Down Expand Up @@ -140,16 +140,14 @@

(def method-cache (atom (cache/lu-cache-factory {} :threshold 1024)))

(defn maybegen-and-invoke
[imports return-type arg-strs arg-classes code args]
(defn ^java.lang.reflect.Method generate-method
[imports return-type arg-strs arg-classes code]
(let [k [return-type arg-classes code]]
(if-let [cached (get @method-cache k)]
(do (swap! method-cache cache/hit k)
(.invoke ^java.lang.reflect.Method cached nil (into-array Object args)))
(if-let [meth (get @method-cache k)]
(do (swap! method-cache cache/hit k) meth)
(let [klass (generate-class imports return-type arg-strs code)
meth (.getMethod ^Class klass "m" (into-array Class arg-classes))]
(swap! method-cache cache/miss k meth)
(.invoke ^java.lang.reflect.Method meth nil (into-array Object args))))))
meth (.getMethod klass "m" (into-array Class arg-classes))]
(do (swap! method-cache cache/miss k meth) meth)))))

(defmacro java*
"Similar to ClojureScript's js*. Compiles a Java code string into a
Expand All @@ -166,13 +164,13 @@
(java-add 1 2) ;=> 3"
[imports return-type arg-types code & args]
{:pre [(= (count arg-types) (count args))]}
`(maybegen-and-invoke
(mapv #(.getName ^Class %) ~imports)
~(or (prim-strings return-type) `(.getName ^Class ~return-type))
~(mapv #(or (prim-strings %) `(.getName ^Class ~%)) arg-types)
~(mapv #(or (prim-classes %) %) arg-types)
~code
[~@args]))
`(let [meth# (generate-method
(mapv #(.getName ^Class %) ~imports)
~(or (prim-strings return-type) `(.getName ^Class ~return-type))
~(mapv #(or (prim-strings %) `(.getName ^Class ~%)) arg-types)
~(mapv #(or (prim-classes %) %) arg-types)
~code)]
(.invoke meth# nil (into-array Object [~@args]))))

(comment

Expand Down

0 comments on commit 4197e59

Please sign in to comment.