dhall-haskell icon indicating copy to clipboard operation
dhall-haskell copied to clipboard

Guide to compile dhall to JS backend, wasm backend and GHCJS

Open flip111 opened this issue 1 year ago • 6 comments

Could anyone provide some hints on how to get dhall compiled into a javascript library? And how would one get an API specification for this library? AFAIK there are three possibly targets, the older GHCJS, the new javascript backend 1 and a WASM backend. I would like to try all three

flip111 avatar Jul 13 '24 18:07 flip111

The Dhall JavaScript code that powers https://dhall-lang.org is built using the older GHCJS and that's using Nixpkgs's support for GHCJS. However, I haven't tried either the WASM backend or the new JavaScript backend.

Also, even the older GHCJS build is deprecated (it's currently disabled in master), although I've been working on re-enabling it. The dhall-lang repository is pinned to an older version of this repository for building the JavaScript code.

Gabriella439 avatar Aug 21 '24 03:08 Gabriella439

With the WASM backend, the only problematic dependency now is unix-compat, which is used directly as well as via atomic-write. Comment those two out in the Cabal file, use the following cabal.project, and nix shell gitlab:haskell-wasm/ghc-wasm-meta?rev=56dfe2478cae35ded335261c854bb8b2a5e7f4d2#all_9_10 -c wasm32-wasi-cabal build dhall --only-dependencies works:

packages:
  ./dhall

if arch(wasm32)
  allow-newer:
    cborg:*,
    text-short:*,
    th-abstraction:*,
    th-compat:*,
    th-lift:*,

  constraints:
    -- The `network` library doesn't build, although I believe there's a work-in-progress patch out there somewhere.
    -- Even then, `-use-http-client-tls` would probably be useful for avoiding the whole `crypton`/`basement` ecosystem,
    -- which is unmaintained and includes a lot of C stuff.
    dhall -with-http,
    haskeline -terminfo,

  constraints: time installed
  allow-newer: time

  -- https://github.com/well-typed/cborg/pull/322
  source-repository-package
    type: git
    location: https://github.com/amesgen/cborg
    tag: c3b5c696f62d04c0d87f55250bfc0016ab94d800
    subdir: cborg

georgefst avatar Dec 28 '24 13:12 georgefst

Note that I haven't looked in to unix-compat, what exactly it does, and whether dhall could easily avoid depending on it.

georgefst avatar Dec 28 '24 13:12 georgefst

@georgefst did you get the wasm version running or just compiling?

flip111 avatar Dec 28 '24 15:12 flip111

@georgefst did you get the wasm version running or just compiling?

I didn't even get it compiling! Note that I used the --only-dependencies flag.

georgefst avatar Jan 17 '25 00:01 georgefst

Okay, with #2647 (not strictly necessary, but Wasm development is now primarily happening on the 9.12 branch), and some patches for network and unix-compat I managed to run a basic example!

import Dhall

data Example = Example {foo :: Natural, bar :: Vector Double} deriving (Show, Generic, FromDhall)

main = print =<< input @Example auto "{foo = 1, bar = [3.0, 4.0, 5.0]}"
$ wasmtime ./dhall-test.wasm 
Example {foo = 1, bar = [3.0,4.0,5.0]}

georgefst avatar Mar 07 '25 13:03 georgefst