Skip to content

Commit cc3cf6f

Browse files
committed
Add api documents
1 parent d59c896 commit cc3cf6f

File tree

22 files changed

+1087
-3
lines changed

22 files changed

+1087
-3
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:lint-as {net.coruscation.js4clj.api.polyglot/define-unwrap-executable-alias clojure.core/declare}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:lint-as {net.coruscation.js4clj.js/define-builtins clojure.core/declare}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:lint-as {net.coruscation.js4clj.require/require-js clojure.core/require}}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{:config-in-call {net.coruscation.js4clj.utils/js. {:ignore [:unresolved-symbol :type-mismatch]}, net.coruscation.js4clj.utils/js.- {:ignore [:unresolved-symbol :type-mismatch]}, net.coruscation.js4clj.utils/js.. {:ignore [:unresolved-symbol :type-mismatch]}}}

deps.edn

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@
2323
:extra-deps {hiccup/hiccup {:mvn/version "2.0.0"}
2424
ring/ring-core {:mvn/version "1.15.3"}
2525
ring-cors/ring-cors {:mvn/version "0.1.13"}
26-
ring/ring-jetty-adapter {:mvn/version "1.15.3"}}}}}
26+
ring/ring-jetty-adapter {:mvn/version "1.15.3"}}}
27+
:codox {:extra-deps {codox/codox {:mvn/version "0.10.8"}}
28+
:exec-fn codox.main/generate-docs
29+
:exec-args {:name "net.coruscation.js4clj"
30+
:description "Use JavaScript in JVM with Clojure by GraalJS"
31+
:namespaces [net.coruscation.js4clj.require
32+
net.coruscation.js4clj.core
33+
net.coruscation.js4clj.utils
34+
net.coruscation.js4clj.js
35+
net.coruscation.js4clj.api.polyglot]
36+
:output-path "docs"}}}}

doc/intro.md

Lines changed: 127 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,128 @@
1-
# Introduction to javascript-clj/javascript-clj
1+
# Usage
22

3-
TODO: write [great documentation](http://jacobian.org/writing/what-to-write/)
3+
# Prerequisite & Installation
4+
5+
TODO
6+
7+
8+
<a id="org71358e7"></a>
9+
10+
# Usage
11+
12+
13+
<a id="org0757d24"></a>
14+
15+
## Requiring Namespaces
16+
17+
```clojure
18+
(ns net.coruscation.js4clj.example
19+
(:require
20+
[net.coruscation.js4clj.require :refer [require-js]]
21+
[net.coruscation.js4clj.utils :refer [js. js.- js.. clj->js js->clj js-set!]]
22+
[net.coruscation.js4clj.core :refer [js-new]]
23+
[net.coruscation.js4clj.js :as js]))
24+
```
25+
26+
27+
<a id="orgea97a50"></a>
28+
29+
## Using JavaScript Libraries
30+
31+
Install JavaScript libraries in the project root using npm like usual.
32+
33+
```bash
34+
npm init
35+
npm i luxon # luxon as an example
36+
```
37+
38+
```clojure
39+
;; we can use luxon as a namespace now, for example: luxon/DateTime
40+
(require-js '["luxon" :as luxon])
41+
```
42+
43+
44+
<a id="orgf88f3b7"></a>
45+
46+
## Converting data between Clojure and JavaScript
47+
48+
This library provides two functions similar to those of ClojureScript: `js->clj` and `clj->js`.
49+
50+
```clojure
51+
;; result: #object[org.graalvm.polyglot.Value 0x22b60c71 "{key: \"value\"}"]
52+
(def js-obj (clj->js {:key "value"}))
53+
;; result: {:key "value"}
54+
(js->clj js-obj :keywordize-keys true)
55+
```
56+
57+
58+
<a id="orgbbec24a"></a>
59+
60+
## Calling JavaScript functions, get/set JavaScript properties
61+
62+
Similar to ClojureScript, this libraries provides four macros: `js.` `js.-` `js..` and `js-set!`
63+
64+
We can use `js.` to call a method of an Object.
65+
66+
```clojure
67+
(def current-time (js. luxon/DateTime now))
68+
;; result: "2025-11-27T23:27:34.853+08:00"
69+
(js. current-time toString)
70+
```
71+
72+
We can use `js.-` to access properties of an object.
73+
74+
```clojure
75+
;; result: "value"
76+
(js.- (clj->js {:key "value"})
77+
key)
78+
```
79+
80+
We can also use `js..` to chain the calling
81+
82+
```clojure
83+
(js.. luxon/DateTime now toString)
84+
```
85+
86+
We can also use `js..` to access property by using `-` as a prefix:
87+
88+
```clojure
89+
(js.. luxon/DateTime
90+
(fromObject (clj->js {:day 22}))
91+
(plus (clj->js {:day 1}))
92+
;; results: 23
93+
-day)
94+
```
95+
96+
The library also provides a `js-set!`, it must be used along with `js.-` or `js..`.
97+
98+
```clojure
99+
(let [obj (clj->js {})]
100+
(js-set! (js.- obj key)
101+
"value")
102+
103+
(js.- obj key) ;; "value"
104+
105+
(js-set! (js.. obj -key)
106+
"new value")
107+
(js.- obj key) ;; "new value"
108+
)
109+
```
110+
111+
112+
<a id="orgd8c1da3"></a>
113+
114+
# Examples
115+
116+
You can check this [minimal Server Side Rendering with Client Side Rehydration example using react](https://github.com/imakira/js4clj/tree/master/example/js4clj).
117+
118+
119+
<a id="orgdada725"></a>
120+
121+
# Links
122+
123+
This project is inspired by:
124+
125+
- [libpython-clj](https://github.com/clj-python/libpython-clj)
126+
- [py4clj](https://github.com/plandes/clj-py4j)
127+
128+
Check this blog about roughly how it is done [Experiment with GraalVM, Clojure, and JavaScript.](https://coruscation.net/blogs/experiment-with-graalvm--clojure--and-javascript.html)

0 commit comments

Comments
 (0)