Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
clavascript
Advanced tools
ClavaScript is an experimental ClojureScript syntax to JavaScript compiler.
ClavaScript is an experimental ClojureScript syntax to JavaScript compiler.
It is not intended as a replacement for ClojureScript but as a tool target JS for anything you would not use ClojureScript proper for.
:warning: This project should be considered experimental and may still undergo breaking changes. It's fine to use it for non-critical projects but don't use it in production yet.
Although it's early days, you're welcome to try out clava
and submit issues.
$ mkdir clava-test && cd clava-test
$ npm init -y
$ npm install clavascript@latest
Create a .cljs
or .clvs
file, e.g. example.cljs
:
(ns example
(:require ["fs" :as fs]
["url" :refer [fileURLToPath]]))
(println (fs/existsSync (fileURLToPath js/import.meta.url)))
(defn foo [{:keys [a b c]}]
(+ a b c))
(println (foo {:a 1 :b 2 :c 3}))
Then compile and run (run
does both):
$ npx clvs run example.cljs
true
6
Run npx clvs --help
to see all command line options.
ClavaScript lets you write CLJS syntax but emits small JS output, while still having parts of the CLJS standard library available (ported to mutable data structures, so with caveats). This may work especially well for projects e.g. that you'd like to deploy on CloudFlare workers, node scripts, Github actions, etc. that need the extra performance, startup time and/or small bundle size.
"clavascript/core.js"
module has similar JS equivalents(def x (js/await y))
. Async functions must be marked
with ^:async
: (defn ^:async foo [])
.assoc!
, dissoc!
, conj!
, etc. perform in place mutation on objectsassoc
, dissoc
, conj
, etc. return a new shallow copy of objectsprintln
is a synonym for console.log
pr-str
and prn
coerce values to a string using JSON.stringify
ClavaScript does not implement Clojure seqs. Instead it uses the JavaScript iteration protocols to work with collections. What this means in practice is the following:
seq
takes a collection and returns an Iterable of that collection, or nil if it's emptyiterable
takes a collection and returns an Iterable of that collection, even if it's emptyseqable?
can be used to check if you can call either oneMost collections are iterable already, so seq
and iterable
will simply
return them; an exception are objects created via {:a 1}
, where seq
and
iterable
will return the result of Object.entries
.
first
, rest
, map
, reduce
et al. call iterable
on the collection before
processing, and functions that typically return seqs instead return an array of
the results.
With respect to memory usage:
(js/global.gc)
(println (js/process.memoryUsage))
(defn doit []
(let [x [(-> (new Array 10000000)
(.fill 0)) :foo :bar]
;; Big array `x` is still being held on to by `y`:
y (rest x)]
(println (js/process.memoryUsage))
(vec y)))
(println (doit))
(js/global.gc)
;; Note that big array is garbage collected now:
(println (js/process.memoryUsage))
Run the above program with node --expose-gc ./node_cli mem.cljs
You can produce JSX syntax using the #jsx
tag:
#jsx [:div "Hello"]
produces:
<div>Hello</div>
and outputs the .jsx
extension automatically.
You can use Clojure expressions within #jsx
expressions:
(let [x 1] #jsx [:div (inc x)])
Note that when using a Clojure expression, you escape the JSX context so when you need to return more JSX, use the #jsx
once again:
(let [x 1]
#jsx [:div
(if (odd? x)
#jsx [:span "Odd"]
#jsx [:span "Even"])])
See an example of an application using JSX here (source).
ClavaScript supports async/await
:
(defn ^:async foo [] (js/Promise.resolve 10))
(def x (js/await (foo)))
(println x) ;;=> 10
In arbitrary order, these features are planned:
The core team consists of:
ClavaScript is licensed under the EPL, the same as Clojure core and Scriptjure. See epl-v10.html in the root directory for more information.
FAQs
ClavaScript is an experimental ClojureScript syntax to JavaScript compiler.
We found that clavascript demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.