Skip to content

Commit 654485d

Browse files
committed
Change the way the special js namespace is implemented
1 parent 3ff7aba commit 654485d

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

src/net/coruscation/js4clj/api/polyglot.clj

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns net.coruscation.js4clj.api.polyglot
22
(:require
3-
[clojure.string :as string]))
3+
[clojure.string :as string]
4+
[net.coruscation.js4clj.context :refer [*context*]]))
45

56
(defn with-raw-clojure-value [polyglot-value clojure-value]
67
(with-meta polyglot-value
@@ -14,9 +15,20 @@
1415
{::raw-polyglot-value polyglot-value}))
1516

1617
(defn polyglot-value [obj]
17-
(or (::raw-polyglot-value (meta obj))
18-
(and (instance? org.graalvm.polyglot.Value obj)
19-
obj)))
18+
(cond (::raw-polyglot-value (meta obj))
19+
(::raw-polyglot-value (meta obj))
20+
21+
(and (symbol? obj)
22+
(= (namespace obj)
23+
(name 'net.coruscation.js4clj.js)))
24+
(.getMember (.getBindings *context* "js")
25+
(name obj))
26+
27+
(instance? org.graalvm.polyglot.Value obj)
28+
obj
29+
30+
:else
31+
nil))
2032

2133
(defn- to-camel-style [s]
2234
(string/replace s #"-([a-z])" (fn [g]

src/net/coruscation/js4clj/js.clj

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,28 @@
77
[org.graalvm.polyglot Context Source Value]))
88

99
(def ^{:dynamic true :private true} *no-clojurify* false)
10+
1011
(def ^:private nullprint (java.io.PrintWriter. (proxy [java.io.OutputStream] []
1112
(write [_ _]))))
12-
13-
(defn- define-builtin [ns primitive & [alias]]
13+
(defn- define-builtin [ns primitive]
1414
(binding [*err* nullprint]
15-
(intern ns (if alias (symbol alias) (symbol primitive))
16-
((if *no-clojurify* identity clojurify-value)
17-
(.getMember (.getBindings *context* "js") primitive)))))
15+
(intern ns (with-meta primitive
16+
{:doc (str "A symbol stands for js `" (name primitive) "`")})
17+
(symbol (name ns) (name primitive)))))
1818

1919
(defmacro ^:private define-builtins
2020
{:clj-kondo/lint-as 'clojure.core/declare}
2121
[ns & primitives]
2222
(create-ns ns)
2323
`(doseq [primitive# '~primitives]
24-
(define-builtin '~ns (str primitive#))))
25-
26-
;; In cljs there is also a js/undefined
27-
;; in which (= nil js/undefined) but we can't mimic it.
28-
;; Still, we need a js/undefined in case we need to do some
29-
;; very specific interop.
30-
(def undefined
31-
"Internal representation of `undefined` in JavaScript.
32-
33-
Only use this when you want to pass a undefined value to a JavaScript function.
34-
35-
Unlike ClojureScript, this value does not equal to `nil`"
36-
(.getMember (.getBindings *context* "js") "undefined"))
24+
(define-builtin '~ns primitive#)))
3725

3826
#_{:clojure-lsp/ignore [:clojure-lsp/unused-public-var]}
3927
(define-builtins net.coruscation.js4clj.js
4028
globalThis
4129
Infinity
4230
NaN
31+
undefined
4332
Object
4433
Function
4534
Boolean

test/net/coruscation/js4clj/js4clj_test.clj

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656

5757
(deftest wrapper-method-test
5858
(testing ""
59-
(is (function? js/Array))
59+
;; (is (function? js/Array))
6060
(is (= (polyglot/get-meta-qualified-name (polyglot/get-meta-object js/Array))
6161
"Function"))
6262
(is (= (polyglot/get-meta-qualified-name (polyglot/get-meta-object (polyglot/get-member js/Array "from")))
@@ -70,10 +70,22 @@
7070

7171
(deftest polyglot-value?-test
7272
(testing ""
73-
(is (true? (fn? js/Array)) "js/Array is a clojure fn")
74-
(is (boolean (polyglot/polyglot-value js/Array)) "js/Array is also a polyglot value")
75-
(is (isa? (class (polyglot/polyglot-value js/Array))
76-
org.graalvm.polyglot.Value) "It returns the wrapped Value")))
73+
(is (true? (symbol? js/Array)) "js/Array is just a symbol now")
74+
(is (instance? org.graalvm.polyglot.Value (polyglot/polyglot-value js/Array) ) "We turn it into a polyglot value using polyglot-value")
75+
(is (true? (fn? (converting/clojurify-value (polyglot/polyglot-value js/Array)))) "We can't use it as a function now, maybe we need to solve this problem in the future")))
76+
77+
(deftest special-js-namespace-test
78+
(testing ""
79+
(is (symbol? js/Array)
80+
"It is just a symbol")
81+
(is (= (js->clj (core/js-new js/Array 1 2 3))
82+
[1 2 3])
83+
"We can use it as the class")
84+
(is (= (js->clj (core/js-new js/Array js/undefined js/undefined))
85+
[nil nil])
86+
"We can pass it as arguments")
87+
(is (true? (core/js-fn? js/Array))
88+
"functions starting with js- will treat these symbols as js values")))
7789

7890
(deftest js-array?-test
7991
(testing ""
@@ -82,9 +94,6 @@
8294
(is (false? (core/js-array? js/undefined)) "returns false, no error threw")))
8395

8496
(deftest js->clj-test
85-
(testing "Default testing"
86-
(is (nil? (js->clj js/undefined)))
87-
(is (fn? (js->clj js/Array))))
8897

8998
(testing "Testing builitn types, they should be returned as is"
9099
(is (nil? (js->clj nil)))
@@ -216,7 +225,7 @@
216225

217226
(deftest executable-javascript-object-test
218227
(testing ""
219-
(is (function? js/Array) "It is a clojure function")
228+
(is (not (function? js/Array)) "We can not use it as a Clojure function now")
220229
(is (core/js-fn? js/Array) "It is also a javascript function")
221230
(is (polyglot/can-instantiate js/Array) "We can also instantiate it")
222231
(is (polyglot/get-member js/Array "from") "We can get its static method")

0 commit comments

Comments
 (0)