Skip to content

Commit

Permalink
Drop explicit dependency on ClojureScript (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
noprompt committed Mar 19, 2020
1 parent bc1826a commit 5096ff2
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 43 deletions.
5 changes: 3 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{:paths ["src"]
:deps {org.clojure/clojurescript {:mvn/version "1.10.439"}}
:deps {}
:aliases {:dev {:extra-deps {fipp/fipp {:mvn/version "0.6.21"}
org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.439"}
org.clojure/test.check {:mvn/version "0.10.0-alpha3"}}}
:test {:extra-paths ["test"]
:extra-deps {org.clojure/clojure {:mvn/version "1.10.0"}
org.clojure/clojurescript {:mvn/version "1.10.439"}
;;org.clojure/clojurescript {:mvn/version "1.10.439"}
org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"
:sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}
Expand All @@ -16,6 +16,7 @@
#_["-m" "cognitect.test-runner"]}
:cljs-test {:extra-paths ["test"]
:extra-deps {org.clojure/test.check {:mvn/version "0.10.0-alpha3"}
org.clojure/clojurescript {:mvn/version "1.10.439"}
olical/cljs-test-runner {:mvn/version "3.4.0"}}
:main-opts ["-m" "cljs-test-runner.main"]}
:make-defproject {:extra-paths ["bin"]
Expand Down
13 changes: 6 additions & 7 deletions src/meander/match/epsilon.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
[meander.match.syntax.epsilon :as r.match.syntax]
[meander.match.runtime.epsilon :as r.match.runtime]
[meander.util.epsilon :as r.util]
#?(:cljs [goog.object :as gobj]))
#?(:clj
(:import (cljs.tagged_literals JSValue))))
#?(:cljs [goog.object :as gobj])))

(def
^{:dynamic true
Expand Down Expand Up @@ -187,10 +185,11 @@
(map compile-ground (:elements node))

:jsa
#?(:clj
(JSValue. (vec (compile-ground (:prt node))))
:cljs
(into-array (compile-ground (:prt node))))
(let [v (vec (compile-ground (:prt node)))]
#?(:clj
(r.util/make-js-value v)
:cljs
(into-array v)))

:lit
(r.syntax/unparse node)
Expand Down
68 changes: 34 additions & 34 deletions src/meander/syntax/epsilon.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
[clojure.set :as set]
[clojure.spec.alpha :as s]
[clojure.string :as string]
[cljs.tagged-literals]
[meander.syntax.specs.epsilon :as m.syntax.specs]
[meander.util.epsilon :as r.util])
:cljs
Expand All @@ -16,9 +15,7 @@
[meander.util.epsilon :as r.util]
[goog.object]))
#?(:cljs
(:require-macros [meander.syntax.epsilon]))
#?(:clj
(:import (cljs.tagged_literals JSValue))))
(:require-macros [meander.syntax.epsilon])))

#?(:clj (set! *warn-on-reflection* true))

Expand Down Expand Up @@ -727,24 +724,24 @@
:value sym}))))))

(defn parse-js-value
{:private true}
[^JSValue js-value env]
(let [x (.val js-value)]
(cond
(vector? x)
{:tag :jsa
:prt (expand-prt (parse-all x env))}

(map? x)
{:tag :jso
:object (into {}
(map
(fn [[k v]]
(let [k* (if (keyword? k)
(subs (str k) 1)
k)]
[(parse k* env) (parse v env)])))
x)})))
{:arglists '([val-of-js-value env])
:private true}
[x env]
(cond
(vector? x)
{:tag :jsa
:prt (expand-prt (parse-all x env))}

(map? x)
{:tag :jso
:object (into {}
(map
(fn [[k v]]
(let [k* (if (keyword? k)
(subs (str k) 1)
k)]
[(parse k* env) (parse v env)])))
x)}))

(defn parse-vector
{:private true}
Expand Down Expand Up @@ -852,8 +849,8 @@
(symbol? form)
(parse-symbol form)

#?@(:clj [(instance? JSValue form)
(parse-js-value form env)])
#?@(:clj [(r.util/js-value? form)
(parse-js-value (r.util/val-of-js-value form) env)])

:else
{:tag :lit
Expand Down Expand Up @@ -948,10 +945,11 @@
(max-length (:prt node)))

(defmethod unparse :jsa [node]
#?(:clj
(JSValue. (vec (unparse (:prt node))))
:cljs
(into-array (unparse (:prt node)))))
(let [p (unparse (:prt node))]
#?(:clj
(r.util/make-js-value (vec p))
:cljs
(into-array p))))

(defmethod search? :jsa [node]
(search? (:prt node)))
Expand All @@ -970,11 +968,12 @@

(defmethod unparse :jso [node]
#?(:clj
(JSValue. (reduce-kv
(let [m (reduce-kv
(fn [m k v]
(assoc m (unparse k) (unparse v)))
{}
(:object node)))
(:object node))]
(r.util/make-js-value m))
:cljs
(reduce-kv
(fn [obj [k v]]
Expand Down Expand Up @@ -1824,10 +1823,11 @@
(map lit-form (:elements node))

:jsa
#?(:clj
(JSValue. (vec (lit-form (:prt node))))
:cljs
(into-array (lit-form (:prt node))))
(let [form (lit-form (:prt node))]
#?(:clj
(r.util/make-js-value (vec form))
:cljs
(into-array form)))

:lit
(:value node)
Expand Down
27 changes: 27 additions & 0 deletions src/meander/util/epsilon.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,30 @@
sym))
(symbol (name (:name cljs-ns)) (name sym)))
sym)))

#?(:clj
(try
(let [c (Class/forName "cljs.tagged_literals.JSValue")]
(defn js-value? [x]
(instance? c x)))
(catch ClassNotFoundException _
(defn js-value? [x]
false)))
:cljs
(defn js-value? [x]
false))

#?(:clj
(try
(let [c (Class/forName "cljs.tagged_literals.JSValue")
s 'cljs.tagged_literals.JSValue]
(defn make-js-value [x]
(eval `(new ~s ~x))))
(catch ClassNotFoundException _
(defn make-js-value [x]
x)))
:cljs
(defn make-js-value [x] x))

(defn val-of-js-value [x]
(if (js-value? x) (.val x) x))

0 comments on commit 5096ff2

Please sign in to comment.