Skip to content

Commit 8acd04a

Browse files
committed
:require-global
1 parent 04f158d commit 8acd04a

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ SCI is used in [babashka](https://github.com/babashka/babashka),
2020
record basis field.
2121
- Fix [#1011](https://github.com/babashka/sci/issues/1011): reset ns metadata when evaluating ns form multiple times
2222
- Fix for https://github.com/babashka/babashka/issues/1899
23-
- Add support for `:refer-global`
23+
- Add support for `:refer-global` and `:require-global`
2424

2525
## 0.10.49 (2025-08-22)
2626

src/sci/impl/namespaces.cljc

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -616,14 +616,23 @@
616616
(apply load/eval-use (store/get-ctx) args))
617617

618618
#?(:cljs
619-
(defn refer-global* [_ _ & args]
620-
(let [args (map (fn [x]
621-
(if (and (seq? x)
622-
(= 'quote (first x)))
623-
(second x)
624-
x))
625-
args)]
626-
(apply load/eval-refer-global (store/get-ctx) args))))
619+
(do (defn refer-global* [_ _ & args]
620+
(let [args (map (fn [x]
621+
(if (and (seq? x)
622+
(= 'quote (first x)))
623+
(second x)
624+
x))
625+
args)]
626+
(apply load/eval-refer-global (store/get-ctx) args)))
627+
628+
(defn require-global* [_ _ & args]
629+
(let [args (map (fn [x]
630+
(if (and (seq? x)
631+
(= 'quote (first x)))
632+
(second x)
633+
x))
634+
args)]
635+
(apply load/eval-require-global (store/get-ctx) args)))))
627636

628637
(defn sci-resolve*
629638
([sci-ctx sym]
@@ -1564,7 +1573,9 @@
15641573
'refer (copy-var sci-refer clojure-core-ns {:name 'refer})
15651574
'refer-clojure (macrofy 'refer-clojure sci-refer-clojure)
15661575
#?@(:cljs ['refer-global (copy-var refer-global* clojure-core-ns {:macro true
1567-
:copy-meta-from 'cljs.core/refer-global})])
1576+
:copy-meta-from 'cljs.core/refer-global})
1577+
'require-global (copy-var require-global* clojure-core-ns {:macro true
1578+
:copy-meta-from 'cljs.core/require-global})])
15681579
're-find (copy-core-var re-find)
15691580
#?@(:clj ['re-groups (copy-core-var re-groups)])
15701581
're-pattern (copy-core-var re-pattern)

test/sci/global_test.cljs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
(ns sci.global-test
2-
(:require [clojure.test :as t :refer [deftest is testing]]
2+
(:require [clojure.test :as t :refer [deftest is]]
33
[sci.core :as sci]))
44

55
(deftest refer-global-test
6-
(testing "init"
7-
(let [ctx #(sci/init {:classes {'js js/globalThis}})]
8-
(sci/binding [sci/ns sci/ns]
9-
(is (true?
10-
(sci/eval-form (ctx) '(do (refer-global :only '[String])
11-
(instance? String (new String "foo"))))))
12-
(is (true?
13-
(sci/eval-form (ctx) '(do (ns foo (:refer-global :only [String]))
14-
(instance? String (new String "foo"))))))
15-
(is (true?
16-
(sci/eval-form (ctx) '(do (ns foo (:refer-global :only [String] :rename {String Str}))
17-
(instance? Str (new Str "foo") ))))))
18-
(is (thrown? js/Error
19-
(sci/eval-string* (ctx)
20-
"(ns foo (:refer-global :only [String] :rename {String Str}))
21-
(instance? String (new String \"foo\"))"))))))
6+
(let [ctx #(sci/init {:classes {'js js/globalThis}})]
7+
(sci/binding [sci/ns sci/ns]
8+
(is (true?
9+
(sci/eval-form (ctx) '(do (refer-global :only '[String])
10+
(instance? String (new String "foo"))))))
11+
(is (true?
12+
(sci/eval-form (ctx) '(do (ns foo (:refer-global :only [String]))
13+
(instance? String (new String "foo"))))))
14+
(is (true?
15+
(sci/eval-form (ctx) '(do (ns foo (:refer-global :only [String] :rename {String Str}))
16+
(instance? Str (new Str "foo") ))))))
17+
(is (thrown? js/Error
18+
(sci/eval-string* (ctx)
19+
"(ns foo (:refer-global :only [String] :rename {String Str}))
20+
(instance? String (new String \"foo\"))")))))
21+
22+
(deftest require-global-test
23+
(let [ctx #(sci/init {:classes {'js js/globalThis}})]
24+
(sci/binding [sci/ns sci/ns]
25+
(is (true?
26+
(sci/eval-form (ctx) '(do (require-global '[String :as Str])
27+
(instance? Str (new Str "foo"))))))
28+
(is (true?
29+
(sci/eval-string* (ctx) "(ns foo (:require-global [String :as Str]))
30+
(instance? Str (new Str \"foo\"))")))
31+
(is (true?
32+
(sci/eval-string* (ctx) "(ns foo (:require-global [\"String\" :as Str]))
33+
(instance? Str (new Str \"foo\"))")))
34+
(is (true?
35+
(sci/eval-string* (ctx) "(ns foo (:require-global [String]))
36+
(instance? String (new String \"foo\"))")))
37+
(is (true?
38+
(sci/eval-string* (ctx) "(ns foo (:require-global [\"String\"]))
39+
(instance? String (new String \"foo\"))"))))))

0 commit comments

Comments
 (0)