Coder Social home page Coder Social logo

cherry-wasm-demo's Introduction

#+title Compile ClojureScript to WebAssmebly using cherry and javy #+author Eugen Stan

Compile ClojureScript to WebAssembly

This is a demo repo to try and compile ClojureScript to WASM using javy.

Once we have the wasm file we can try to run it using one of the existing runtimes. I imagine we will need a runtime that supports GC - such as Chrome / Firefox or Node.js . See https://webassembly.org/features/ for un up to date list.

What we need

You will need node.js for cherry and javy binary locally.

You can follow the guide for cherry here: https://github.com/squint-cljs/cherry and for javy here: https://github.com/bytecodealliance/javy .

I have created the example files in this repo to use as a boilerplate.

What the code looks like

The code is very bare-bones - just the example from cherry site.

It works. I did not try anything more than this.

The final wasm is quite big at ~6.8MB . Removing cherry require’s (which are needed only for evaluating clojurescript in WebAssmebly) gets the generated wasm to ~2MB. Still large but I’m happy with this.

(ns my.cherry
  (:require [cherry.embed :as cherry]))

(cherry/preserve-ns 'cljs.core)
(cherry/preserve-ns 'clojure.string)

(defn add [a b]
  (+ a b))

(defn init []
  (println "Hello from init!" (add 1 3)))

(println "Hello world!" (add 1 4))

How to run

# compile ClojureScript via shadowclj to embed cherry.core and produce a single js file
clojure -M:cherry:shadow release cherry-wasm

# compile js to wasm
javy compile out/js/cherry-wasm.js -o cherry.wasm

# Run my-cherry.wasm using wasmtime
wasmtime cherry.wasm

Hello world! 5
Hello from init!

# Run it with node.js (output redacted for brevity)
node --experimental-wasi-unstable-preview1 node-run-cherry-wasm.cjs
Hello world! 5
Hello from init! 4

Current limitations

Javy currently exposes a limited API for interacting with the outside world. Only stdin and stdout are allowed. See Invoking Javy-generated modules programatically I created an issue to see if we can also get command line arguments and environment variables.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.