Skip to content

Commit 678ddc8

Browse files
committedApr 6, 2021
Add example namespace
1 parent e0b09e2 commit 678ddc8

File tree

5 files changed

+140
-3
lines changed

5 files changed

+140
-3
lines changed
 

‎bin/test/node/shadow

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
command="$1"
55
shift
6-
clj -M:cljs:test "$command" test-node "$@" && node --max_old_space_size=6000 ./compiled/node/test.js
6+
clj -M:cljs:test "$command" test-node "$@" && node --max_old_space_size=6500 ./compiled/node/test.js

‎deps.edn

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
:dev
1313
{:extra-paths ["src/dev"
14+
"src/example"
1415
"src/test"]}
1516

1617
:jar

‎shadow-cljs.edn

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{:builds
22
{:dev
33
{:asset-path "/js"
4-
:modules {:main {:entries [helins.wasmeta.dev]}}
4+
:modules {:main {:entries [helins.wasm.dev]}}
55
:output-dir "cljs/js"
66
:target :browser}
77

@@ -14,7 +14,8 @@
1414
{:alias [:cljs]}
1515

1616
:dev-http
17-
{8000 "cljs"}
17+
{8000 ["cljs"
18+
"src/wasm"]}
1819

1920
:nrepl
2021
{:port 14563}}

‎src/dev/helins/wasm/dev.cljc

+11
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
[helins.wasm :as wasm]
2121
[helins.wasm.bin :as wasm.bin]
2222
[helins.wasm.count :as wasm.count]
23+
[helins.wasm.example]
2324
[helins.wasm.ir :as wasm.ir]
2425
[helins.wasm.read :as wasm.read]
2526
[helins.wasm.schema :as wasm.schema]
@@ -61,6 +62,16 @@
6162
;clojure.pprint/pprint
6263
)
6364

65+
66+
(-> (js/fetch "test.wasm")
67+
(.then (fn [resp]
68+
(.arrayBuffer resp)))
69+
(.then (fn [array-buffer]
70+
(-> array-buffer
71+
binf/view
72+
wasm/prepare-view
73+
wasm/decompile
74+
clojure.pprint/pprint))))
6475

6576
)
6677

‎src/example/helins/wasm/example.cljc

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
;; This Source Code Form is subject to the terms of the Mozilla Public
2+
;; License, v. 2.0. If a copy of the MPL was not distributed with this
3+
;; file, You can obtain one at https://mozilla.org/MPL/2.0/.
4+
5+
6+
(ns helins.wasm.example
7+
8+
"Simple examples of compilation / decompilation and Malli schemas."
9+
10+
{:author "Adam Helinski"}
11+
12+
(:require [clojure.pprint]
13+
[helins.binf :as binf]
14+
[helins.wasm :as wasm]
15+
[helins.wasm.schema :as wasm.schema]
16+
[malli.core :as malli]
17+
[malli.generator :as malli.gen]
18+
[malli.util]))
19+
20+
21+
;;;;;;;;;; Malli schemas
22+
23+
24+
(def registry
25+
(-> (merge (malli/default-schemas)
26+
(malli.util/schemas))
27+
wasm.schema/registry))
28+
29+
30+
31+
(comment
32+
33+
34+
;; Generating a single WASM instruction at random
35+
;;
36+
(clojure.pprint/pprint (malli.gen/generate :wasm/instr
37+
{:registry registry})))
38+
39+
40+
;;;;;;;;;; Compilation / Decompilation - Files (JVM)
41+
42+
43+
(comment
44+
45+
46+
(def decompiled
47+
(wasm/decompile-file "src/wasm/test.wasm"))
48+
49+
50+
(clojure.pprint/pprint decompiled)
51+
52+
53+
(def compiled
54+
(wasm/compile-file decompiled
55+
"/tmp/test2.wasm"))
56+
57+
58+
;; Of course, it is valid
59+
;;
60+
(malli/validate :wasm/module
61+
decompiled
62+
{:registry registry}))
63+
64+
65+
;;;;;;;;;; Compilation / Decompilation - BinF views (JVM + JS)
66+
67+
68+
(comment
69+
70+
;; Anything that can be represented as a BinF view can be decompiled
71+
;;
72+
;; Eg. JVM - Wrapping a byte array in a view
73+
;; JS - Wrapping a ArrayBuffer in a view
74+
;;
75+
;; See https://github.com/helins/binf.cljc
76+
77+
;; This is a realistic Clojurescript example using `js/fetch`.
78+
;; Starting the :dev Shadow-CLJS profile serves the follow test files.
79+
;; See "Development" section in README
80+
81+
82+
(def *decompiled
83+
(atom nil))
84+
85+
86+
;; Fetching the WASM source and decompiling into above atom.
87+
;;
88+
(-> (js/fetch "test.wasm")
89+
(.then (fn [resp]
90+
(.arrayBuffer resp)))
91+
(.then (fn [array-buffer]
92+
(reset! *decompiled
93+
(-> array-buffer
94+
;; Converting to BinF view and preparing it (will set the right endianess)
95+
binf/view
96+
wasm/prepare-view
97+
;; Decompiling
98+
wasm/decompile)))))
99+
100+
101+
;; Pretty printing
102+
;;
103+
(clojure.pprint/pprint @*decompiled)
104+
105+
106+
(malli/validate :wasm/module
107+
@*decompiled
108+
{:registry registry})
109+
110+
111+
;; Recompiling
112+
;;
113+
(def compiled
114+
(wasm/compile @*decompiled))
115+
116+
117+
;; Of course, you can decompile again for fun
118+
;;
119+
(-> compiled
120+
(binf/seek 0)
121+
wasm/decompile
122+
clojure.pprint/pprint)
123+
124+
)

0 commit comments

Comments
 (0)
Please sign in to comment.