Skip to content

Commit

Permalink
Added function to load many resources into a scope in rhino.
Browse files Browse the repository at this point in the history
  • Loading branch information
tbatchelli committed Nov 27, 2010
1 parent b58d1d5 commit 2265f30
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
24 changes: 9 additions & 15 deletions src/clj_coffeescript/compiler.clj
Original file line number Diff line number Diff line change
@@ -1,26 +1,20 @@
(ns clj-coffeescript.compiler
(:use clj-coffeescript.rhino)
(:import [java.io InputStreamReader]))

(defn get-resource [uri]
(.getResourceAsStream (clojure.lang.RT/baseLoader) uri))

(defn get-resource-as-stream [uri]
(let [resource (get-resource uri)]
(InputStreamReader. resource "UTF-8")))
(:use clj-coffeescript.rhino))

(defn build-compiler []
(with-open [compiler (get-resource-as-stream "coffee-script.js")]
(with-context [ctx]
(set-context-interpreted ctx) ;; avoid 64kb src limit
(let [compiler-scope (build-scope)]
(load-stream compiler "coffee-script.js" compiler-scope ctx)
compiler-scope))))
(with-context [ctx]
(set-context-interpreted ctx) ;; avoid 64kb src limit
(let [compiler-scope (build-scope)]
;; load the coffeescript compiler
(load-resources ["coffee-script.js"] compiler-scope ctx)
compiler-scope)))

(defn compile-string [compiler-scope src & bare?]
(let [scope (build-scope compiler-scope)]
(with-context [ctx]
;; load the script to compile into a variable
(set-named-property "coffeeScriptSource" src compiler-scope scope)
;; compile the contents of the variable
(evaluate-string (format "CoffeeScript.compile(coffeeScriptSource, {bare: %s});" bare?)
"clj-coffeescript"
scope
Expand Down
17 changes: 16 additions & 1 deletion src/clj_coffeescript/rhino.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns clj-coffeescript.rhino
(:import [org.mozilla.javascript Context]))
(:import [org.mozilla.javascript Context]
[java.io InputStreamReader]))

(defmacro with-context [[ctx] & body]
`(try (let [~ctx (Context/enter)]
Expand All @@ -9,6 +10,13 @@
(defn set-context-interpreted [ctx]
(.setOptimizationLevel ctx -1))

(defn get-resource [uri]
(.getResourceAsStream (clojure.lang.RT/baseLoader) uri))

(defn get-resource-as-stream [uri]
(let [resource (get-resource uri)]
(InputStreamReader. resource "UTF-8")))

(defn build-scope
([]
(with-context [ctx]
Expand All @@ -20,11 +28,18 @@
(.setParentScope new-scope parent)
new-scope))))

(defn load-resources [resources scope ctx]
(let [load-resource (fn [uri]
(with-open [stream (get-resource-as-stream uri)]
(load-stream stream uri scope ctx )))]
(doall (map load-resource resources))))

(defn load-stream
([stream file-name scope]
(with-context [ctx]
(load-stream stream file-name scope ctx)))
([stream file-name scope ctx]
(println ctx scope stream file-name)
(.evaluateReader ctx scope stream file-name 0 nil)
(println file-name " loaded.")))

Expand Down

0 comments on commit 2265f30

Please sign in to comment.