Skip to content

Commit 00d6f8a

Browse files
committed
WIP
0 parents  commit 00d6f8a

File tree

21 files changed

+835
-0
lines changed

21 files changed

+835
-0
lines changed

.ghcid

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--command "wasm32-wasi-cabal repl --repl-options=-fno-code"
2+
--restart cabal.project
3+
--restart canary.cabal

.github/workflows/ci.yml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: ci
2+
3+
on:
4+
merge_group:
5+
pull_request:
6+
push:
7+
branches:
8+
- master
9+
workflow_dispatch:
10+
11+
jobs:
12+
ci:
13+
name: ci-${{ matrix.flavour }}
14+
runs-on: ubuntu-24.04
15+
continue-on-error: ${{ matrix.experimental }}
16+
strategy:
17+
fail-fast: false
18+
matrix:
19+
flavour:
20+
- "9.12"
21+
- "9.10"
22+
experimental:
23+
- false
24+
include:
25+
- flavour: "gmp"
26+
experimental: true
27+
steps:
28+
29+
- name: checkout
30+
uses: actions/checkout@v4
31+
32+
- name: ghc-wasm-meta
33+
run: |
34+
pushd "$(mktemp -d)"
35+
curl -f -L https://gitlab.haskell.org/haskell-wasm/ghc-wasm-meta/-/archive/master/ghc-wasm-meta-master.tar.gz | tar xz --strip-components=1
36+
PLAYWRIGHT=1 ./setup.sh
37+
~/.ghc-wasm/add_to_github_path.sh
38+
cp cabal.project.local ${{ github.workspace }}
39+
popd
40+
env:
41+
FLAVOUR: ${{ matrix.flavour }}
42+
43+
- name: gen-plan-json
44+
run: |
45+
wasm32-wasi-cabal build --dry-run
46+
47+
- name: cabal-cache
48+
uses: actions/cache@v4
49+
with:
50+
key: ${{ matrix.flavour }}-${{ hashFiles('dist-newstyle/cache/plan.json') }}
51+
restore-keys: ${{ matrix.flavour }}-
52+
path: |
53+
~/.ghc-wasm/.cabal/store
54+
dist-newstyle
55+
56+
- name: ci
57+
run: |
58+
./util/ci

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist-newstyle

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Revision history for canary
2+
3+
## 0.1.0.0 -- YYYY-mm-dd
4+
5+
* First version. Released on an unsuspecting world.

LICENSE

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Copyright (c) 2025, Cheng Shao
2+
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
* Redistributions of source code must retain the above copyright
8+
notice, this list of conditions and the following disclaimer.
9+
10+
* Redistributions in binary form must reproduce the above
11+
copyright notice, this list of conditions and the following
12+
disclaimer in the documentation and/or other materials provided
13+
with the distribution.
14+
15+
* Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived
17+
from this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

cabal.project

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
packages: .
2+
3+
tests: True
4+
5+
multi-repl: True
6+
7+
source-repository-package
8+
type: git
9+
location: https://github.com/amesgen/splitmix.git
10+
tag: cea9e31bdd849eb0c17611bb99e33d590e126164
11+
12+
package *
13+
optimization: 2
14+
15+
package aeson
16+
flags: -ordered-keymap
17+
18+
package tasty
19+
flags: -unix
20+
21+
package vector
22+
flags: -boundschecks

canary.cabal

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
cabal-version: 3.14
2+
name: canary
3+
version: 0.1.0.0
4+
license: BSD-3-Clause
5+
license-file: LICENSE
6+
author: Cheng Shao
7+
maintainer: [email protected]
8+
build-type: Simple
9+
extra-doc-files: CHANGELOG.md
10+
11+
common deps
12+
build-depends:
13+
aeson,
14+
async,
15+
base,
16+
bytestring,
17+
ghc-experimental,
18+
primitive,
19+
servant-client-core,
20+
text,
21+
22+
default-language: GHC2024
23+
ghc-options:
24+
-Weverything
25+
-Wno-all-missed-specialisations
26+
-Wno-implicit-prelude
27+
-Wno-missing-export-lists
28+
-Wno-missing-import-lists
29+
-Wno-missing-kind-signatures
30+
-Wno-missing-local-signatures
31+
-Wno-missing-poly-kind-signatures
32+
-Wno-missing-role-annotations
33+
-Wno-missing-safe-haskell-mode
34+
-Wno-prepositive-qualified-module
35+
-Wno-unsafe
36+
37+
library
38+
import: deps
39+
exposed-modules:
40+
GHC.Wasm.JS.Array
41+
GHC.Wasm.JS.AsyncIterator
42+
GHC.Wasm.JS.ESM
43+
GHC.Wasm.JS.Free
44+
GHC.Wasm.JS.JSON
45+
GHC.Wasm.JS.Promise
46+
GHC.Wasm.JS.ReadableStream
47+
GHC.Wasm.JS.RuntimeFFI
48+
GHC.Wasm.JS.String
49+
GHC.Wasm.JS.Uint8Array
50+
51+
hs-source-dirs: src
52+
53+
test-suite canary-test
54+
import: deps
55+
type: exitcode-stdio-1.0
56+
hs-source-dirs: test
57+
main-is: Main.hs
58+
build-depends:
59+
canary,
60+
quickcheck-instances,
61+
tasty,
62+
tasty-bench,
63+
tasty-hunit,
64+
tasty-quickcheck,

src/GHC/Wasm/JS/Array.hs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module GHC.Wasm.JS.Array (
2+
JSArray (..),
3+
fromJSVals,
4+
toJSVals,
5+
) where
6+
7+
import Data.Foldable
8+
import GHC.Wasm.Prim
9+
import System.IO.Unsafe
10+
11+
newtype JSArray = JSArray { unJSArray :: JSVal }
12+
13+
fromJSVals :: [JSVal] -> JSArray
14+
fromJSVals vs = unsafePerformIO $ do
15+
arr <- arrayNew
16+
for_ vs $ arrayPush arr
17+
pure arr
18+
19+
toJSVals :: JSArray -> [JSVal]
20+
toJSVals arr = [arrayIndex arr i | i <- [0 .. arrayLength arr - 1]]
21+
22+
foreign import javascript unsafe
23+
"[]"
24+
arrayNew :: IO JSArray
25+
26+
foreign import javascript unsafe
27+
"$1.push($2)"
28+
arrayPush :: JSArray -> JSVal -> IO ()
29+
30+
foreign import javascript unsafe
31+
"$1.length"
32+
arrayLength :: JSArray -> Int
33+
34+
foreign import javascript unsafe
35+
"$1[$2]"
36+
arrayIndex :: JSArray -> Int -> JSVal

src/GHC/Wasm/JS/AsyncIterator.hs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
module GHC.Wasm.JS.AsyncIterator (
2+
JSAsyncIterable (..),
3+
JSAsyncIterator (..),
4+
fromJSAsyncIterable,
5+
toLazyList,
6+
toLazyByteString,
7+
) where
8+
9+
import Control.Exception
10+
import qualified Data.ByteString.Lazy.Internal as LBS
11+
import Data.Coerce
12+
import GHC.Wasm.JS.Free
13+
import GHC.Wasm.JS.Uint8Array
14+
import GHC.Wasm.Prim
15+
import System.IO.Unsafe
16+
17+
newtype JSAsyncIterable = JSAsyncIterable { unJSAsyncIterable :: JSVal }
18+
19+
newtype JSAsyncIterator = JSAsyncIterator { unJSAsyncIterator :: JSVal }
20+
21+
foreign import javascript unsafe
22+
"$1[Symbol.asyncIterator] ? $1[Symbol.asyncIterator]() : $1[Symbol.iterator]()"
23+
fromJSAsyncIterable :: JSAsyncIterable -> IO JSAsyncIterator
24+
25+
newtype JSIteratorResult = IteratorResult JSVal
26+
27+
toLazyList :: JSAsyncIterator -> IO [JSVal]
28+
toLazyList iter = w
29+
where
30+
w = unsafeInterleaveIO $ do
31+
r <- jsAsyncIteratorNext iter
32+
if jsIteratorResultDone r
33+
then do
34+
free r
35+
pure []
36+
else do
37+
v <- jsIteratorResultValue r
38+
free r
39+
vs <- w
40+
pure $ v : vs
41+
42+
toLazyByteString :: JSAsyncIterator -> IO LBS.ByteString
43+
toLazyByteString iter = w
44+
where
45+
w = unsafeInterleaveIO $ do
46+
r <- jsAsyncIteratorNext iter
47+
if jsIteratorResultDone r
48+
then do
49+
free r
50+
pure LBS.Empty
51+
else do
52+
v <- coerce <$> jsIteratorResultValue r
53+
free r
54+
c <- evaluate $ toStrictByteString v
55+
free v
56+
LBS.chunk c <$> w
57+
58+
foreign import javascript safe
59+
"$1.next()"
60+
jsAsyncIteratorNext :: JSAsyncIterator -> IO JSIteratorResult
61+
62+
foreign import javascript unsafe
63+
"$1.done"
64+
jsIteratorResultDone :: JSIteratorResult -> Bool
65+
66+
foreign import javascript unsafe
67+
"$1.value"
68+
jsIteratorResultValue :: JSIteratorResult -> IO JSVal

src/GHC/Wasm/JS/ESM.hs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
module GHC.Wasm.JS.ESM
2+
( PkgSpec (..),
3+
JSModule (..),
4+
jsImport,
5+
JSElement (..),
6+
cssImport,
7+
elementRemove,
8+
npmESM,
9+
npmFile,
10+
jsrESM,
11+
)
12+
where
13+
14+
import Control.Exception
15+
import Data.String
16+
import GHC.Wasm.JS.Free
17+
import GHC.Wasm.Prim
18+
19+
newtype PkgSpec = PkgSpec { unPkgSpec :: String }
20+
deriving newtype (IsString)
21+
22+
newtype JSModule = JSModule { unJSModule :: JSVal }
23+
24+
jsImport :: String -> IO JSModule
25+
jsImport url = do
26+
let url' = toJSString url
27+
m <- jsLazyImport url'
28+
free url'
29+
evaluate m
30+
31+
newtype JSElement = JSElement { unJSElement :: JSVal }
32+
33+
cssImport :: String -> IO JSElement
34+
cssImport url = do
35+
let url' = toJSString url
36+
n <- cssLazyImport url'
37+
free url'
38+
evaluate n
39+
40+
foreign import javascript unsafe
41+
"$1.remove()"
42+
elementRemove :: JSElement -> IO ()
43+
44+
npmESM :: PkgSpec -> FilePath -> String
45+
npmESM pkg rest = "https://cdn.jsdelivr.net/npm/" <> unPkgSpec pkg <> rest <> "/+esm"
46+
47+
npmFile :: PkgSpec -> FilePath -> String
48+
npmFile pkg f =
49+
"https://cdn.jsdelivr.net/npm/" <> unPkgSpec pkg <> "/" <> f
50+
51+
jsrESM :: PkgSpec -> String
52+
jsrESM pkg = "https://esm.sh/jsr/" <> unPkgSpec pkg
53+
54+
foreign import javascript safe
55+
"import($1)"
56+
jsLazyImport :: JSString -> IO JSModule
57+
58+
foreign import javascript safe
59+
"new Promise((res, rej) => { const l = document.createElement('link'); l.rel = 'stylesheet'; l.href = $1; l.addEventListener('load', () => res(l)); l.addEventListener('error', rej); document.head.append(l); })"
60+
cssLazyImport :: JSString -> IO JSElement

0 commit comments

Comments
 (0)