Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(set! *warn-on-reflection* true)
(import (org.mozilla.javascript Context NativeObject NativeArray))
(defn js-eval [string]
(let [cx (Context/enter)
scope (.initStandardObjects cx)]
(.evaluateString cx scope string "<js-eval>" 1 nil)))
(defprotocol Clojurify (clojurify [obj]))
(extend-protocol Clojurify
NativeArray
(clojurify [obj]
(into [] (map #(clojurify (.get obj (int %) obj))
(range (.jsGet_length obj)))))
NativeObject
(clojurify [obj]
(zipmap (.getIds obj)
(map #(clojurify (.get obj ^String % obj))
(.getIds obj))))
Object
(clojurify [obj] obj))
(clojurify (js-eval "({foo: 'bar', 'ding dong': [1, 2, 'foo', 'bar']})"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.