Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #626: add cljs.core/exists? #918

Merged
merged 3 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading