allow call to to-js without ctx

This commit is contained in:
Mariano Guerra 2013-02-08 13:45:53 +01:00
parent 462ac5530b
commit 0439990ed6
2 changed files with 45 additions and 17 deletions

View file

@ -11,9 +11,24 @@
(-from-rhino [object]
"convert a value from rhino to a more clojure friendly representation"))
(defn to-js [obj scope ctx]
(defn with-context [fun]
"create a context call fun with it and safelly exit the context"
(let [ctx (Context/enter)]
(try
(fun ctx)
(finally (Context/exit)))))
(defn with-context-if-nil [ctx fun]
"create a context if ctx is nil, otherwise use ctx and call fun with it,
exit safelly after if ctx was created here, otherwise is up to the caller
(which should be inside a with-context somewhere up the call stack)"
(if ctx
(fun ctx)
(with-context fun)))
(defn to-js [obj scope & [ctx]]
"convert obj to a rhino compatible object"
(-to-rhino obj scope ctx))
(with-context-if-nil ctx #(-to-rhino obj scope %)))
(defn from-js [obj]
"convert obj from rhino into a clojure friendly representation"
@ -115,21 +130,6 @@
"getClass" "JavaAdapter" "JavaImporter" "Continuation"
"XML" "XMLList" "Namespace" "QName"])
(defn with-context [fun]
"create a context call fun with it and safelly exit the context"
(let [ctx (Context/enter)]
(try
(fun ctx)
(finally (Context/exit)))))
(defn with-context-if-nil [ctx fun]
"create a context if ctx is nil, otherwise use ctx and call fun with it,
exit safelly after if ctx was created here, otherwise is up to the caller
(which should be inside a with-context somewhere up the call stack)"
(if ctx
(fun ctx)
(with-context fun)))
(defn eval [scope code & {:keys [ctx filename line-number sec-domain]}]
(with-context-if-nil ctx (fn [ctx1]
(.evaluateString ctx1 scope code

View file

@ -16,6 +16,14 @@
(is (= (js/to-js item scope ctx) js-item)))
(map vector arr js-arr)))))
(defn- check-array-to-js-no-ctx [arr scope]
(let [js-arr (js/to-js arr scope)]
(is (= (count js-arr) (count arr)))
(is (= (class js-arr) NativeArray))
(dorun (map (fn [[item js-item]]
(is (= (js/to-js item scope) js-item)))
(map vector arr js-arr)))))
(defn- is-identity [value]
(is (= (js/from-js value) value)))
@ -101,6 +109,26 @@
(is (= (get (js/to-js {:name "mariano"} scope ctx) "name") "mariano"))
(is (= (get (js/to-js {:name "mariano" :age 27} scope ctx) "age") 27))))))
(testing "to-js works without ctx"
(let [scope (js/new-safe-scope)]
(is (= (js/to-js nil scope) nil))
(is (= (js/to-js 1 scope) 1))
(is (= (js/to-js 1/2 scope) 0.5))
(is (= (js/to-js true scope) true))
(is (= (js/to-js "foo" scope) "foo"))
(is (= (js/to-js :foo scope) "foo"))
(check-array-to-js-no-ctx [] scope)
(check-array-to-js-no-ctx [nil 1 1/2 true "foo" :foo] scope)
(check-array-to-js-no-ctx '(nil 1 1/2 true "foo" :foo) scope)
; check to-js-generic
(check-array-to-js-no-ctx (to-array [nil 1 1/2 true "foo" :foo]) scope)
(is (= (get (js/to-js {:name "mariano"} scope) "name") "mariano"))
(is (= (get (js/to-js {:name "mariano" :age 27} scope) "age") 27))))
(testing "to-js works on complex nested structures"
(js/with-context
(fn [ctx]