From 0439990ed6398c76cb4a50287f0bbf80328baadb Mon Sep 17 00:00:00 2001 From: Mariano Guerra Date: Fri, 8 Feb 2013 13:45:53 +0100 Subject: [PATCH] allow call to to-js without ctx --- src/clj/clj_rhino.clj | 34 +++++++++++++++++----------------- test/clj_rhino/core_test.clj | 28 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/clj/clj_rhino.clj b/src/clj/clj_rhino.clj index 769bf41..5ae0add 100644 --- a/src/clj/clj_rhino.clj +++ b/src/clj/clj_rhino.clj @@ -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 diff --git a/test/clj_rhino/core_test.clj b/test/clj_rhino/core_test.clj index bfd1693..6894c67 100644 --- a/test/clj_rhino/core_test.clj +++ b/test/clj_rhino/core_test.clj @@ -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]