Skip to content

Commit

Permalink
Fix #626: add cljs.core/exists? (#918)
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude authored Mar 4, 2024
1 parent caecc6e commit 5940d69
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ SCI is used in [babashka](https://github.com/babashka/babashka),
[joyride](https://github.com/BetterThanTomorrow/joyride/) and many
[other](https://github.com/babashka/sci#projects-using-sci) projects.

## Unreleased

- Fix [#626](https://github.com/babashka/sci/issues/626): add `cljs.core/exists?`

## 0.8.41 (2023-11-24)

- Bump edamame to 1.3.23
Expand Down
30 changes: 29 additions & 1 deletion src/sci/impl/namespaces.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@
print-dup
#?(:cljs alter-meta!)
memfn
time])
time
exists?])
(:require
#?(:clj [clojure.edn :as edn]
:cljs [cljs.reader :as edn])
#?(:clj [clojure.java.io :as jio])
#?(:clj [sci.impl.proxy :as proxy])
#?(:clj [sci.impl.copy-vars :refer [copy-core-var copy-var macrofy new-var]]
:cljs [sci.impl.copy-vars :refer [new-var]])
#?(:cljs [sci.impl.resolve])
[clojure.set :as set]
[clojure.string :as str]
[clojure.walk :as walk]
Expand Down Expand Up @@ -998,6 +1000,30 @@
" msecs"))
ret#))

#?(:cljs
(defn exists?
"Return true if argument exists, analogous to usage of typeof operator
in JavaScript."
[_ _&env ctx x]
(if (symbol? x)
(if (qualified-symbol? x)
(if (= "js" (namespace x))
(let [splits (str/split (name x) ".")]
(list* 'cljs.core/and
(map (fn [accessor]
(list 'cljs.core/not (list 'cljs.core/undefined? (symbol "js" (str accessor)))))
(reduce (fn [acc split]
(let [new-sym (let [la (last acc)]
(str la (when la ".") split))]
(conj acc new-sym)))
[] splits))))
(boolean (try (sci.impl.resolve/resolve-symbol ctx x nil nil)
(catch :default _ nil))))
(or (boolean (sci-find-ns ctx x))
(boolean (try (sci.impl.resolve/resolve-symbol ctx x nil nil)
(catch :default _ nil)))))
`(some? ~x))))

#?(:clj (defn system-time []
(System/nanoTime)))

Expand Down Expand Up @@ -1281,6 +1307,8 @@
'ex-info (copy-core-var ex-info)
'ex-message (copy-core-var ex-message)
'ex-cause (copy-core-var ex-cause)
#?@(:cljs ['exists? (copy-var exists? clojure-core-ns {:macro true
:ctx true :name 'exists?})])
'find-ns (copy-var sci-find-ns clojure-core-ns {:ctx true :name 'find-ns})
'create-ns (copy-var sci-create-ns clojure-core-ns {:ctx true :name 'create-ns})
'in-ns (copy-var sci-in-ns clojure-core-ns {:ctx true :name 'in-ns})
Expand Down
18 changes: 16 additions & 2 deletions test/sci/core_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -1677,8 +1677,22 @@
(let [output (atom "")
print-fn #(swap! output str %)]
(is (= 1 (sci/binding [sci/print-fn print-fn] (sci/eval-string "(time 1)" {:classes {'js js/globalThis :allow :all}}))))
(is (re-matches #"\"Elapsed time: \d\.\d+ msecs\"\s*" @output))))
)
(is (re-matches #"\"Elapsed time: \d\.\d+ msecs\"\s*" @output)))))

#?(:cljs
(deftest exists?-test
(is (true? (sci/eval-string "(exists? cljs.core.first)")))
(is (true? (sci/eval-string "(exists? cljs.core/first)")))
(is (true? (sci/eval-string "(exists? js/console)" {:classes {'js js/globalThis
:allow :all}})))
(is (true? (sci/eval-string "(exists? js/console.log)" {:classes {'js js/globalThis
:allow :all}})))
(is (false? (sci/eval-string "(exists? js/foo.bar)" {:classes {'js js/globalThis
:allow :all}})))
(is (false? (sci/eval-string "(exists? js/console.log.foobar)" {:classes {'js js/globalThis
:allow :all}})))
(is (false? (sci/eval-string "(exists? console.log)" {:classes {'js js/globalThis
:allow :all}})))))

;;;; Scratch

Expand Down

0 comments on commit 5940d69

Please sign in to comment.