-
-
Save michalmarczyk/386569 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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