Skip to content

Commit 980b3f7

Browse files
committed
WIP
0 parents  commit 980b3f7

File tree

22 files changed

+901
-0
lines changed

22 files changed

+901
-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 --enable-multi-repl"
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
cabal.project.local
2+
ci.log
3+
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packages: .
2+
3+
tests: True
4+
5+
source-repository-package
6+
type: git
7+
location: https://github.com/amesgen/splitmix.git
8+
tag: cea9e31bdd849eb0c17611bb99e33d590e126164
9+
10+
package *
11+
optimization: 2
12+
ghc-options: -fdicts-cheap -fexpose-all-unfoldings -flate-dmd-anal -flate-specialise -fspecialise-aggressively
13+
14+
package tasty
15+
flags: -unix
16+
17+
package vector
18+
flags: -boundschecks

canary.cabal

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
base,
15+
ghc-experimental,
16+
text,
17+
vector,
18+
vector-stream,
19+
20+
default-language: GHC2024
21+
ghc-options:
22+
-Weverything
23+
-Wno-all-missed-specialisations
24+
-Wno-implicit-prelude
25+
-Wno-missing-export-lists
26+
-Wno-missing-import-lists
27+
-Wno-missing-kind-signatures
28+
-Wno-missing-local-signatures
29+
-Wno-missing-poly-kind-signatures
30+
-Wno-missing-role-annotations
31+
-Wno-missing-safe-haskell-mode
32+
-Wno-prepositive-qualified-module
33+
-Wno-unsafe
34+
35+
library
36+
import: deps
37+
exposed-modules:
38+
Data.Stream.Monadic.Extra
39+
GHC.Wasm.JS.Array
40+
GHC.Wasm.JS.AsyncIterator
41+
GHC.Wasm.JS.ESM
42+
GHC.Wasm.JS.Free
43+
GHC.Wasm.JS.JSON
44+
GHC.Wasm.JS.Promise
45+
GHC.Wasm.JS.ReadableStream
46+
GHC.Wasm.JS.RuntimeFFI
47+
GHC.Wasm.JS.String
48+
GHC.Wasm.JS.Uint8Array
49+
50+
hs-source-dirs: src
51+
build-depends:
52+
bytestring,
53+
primitive,
54+
55+
test-suite canary-test
56+
import: deps
57+
type: exitcode-stdio-1.0
58+
hs-source-dirs: test
59+
main-is: Main.hs
60+
build-depends:
61+
async,
62+
canary,
63+
quickcheck-instances,
64+
tasty,
65+
tasty-hunit,
66+
tasty-quickcheck,

src/Data/Stream/Monadic/Extra.hs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Data.Stream.Monadic.Extra
2+
( toLazyList,
3+
toVector,
4+
)
5+
where
6+
7+
import Control.Monad
8+
import Control.Monad.Primitive
9+
import Data.Stream.Monadic qualified as VS
10+
import Data.Vector.Fusion.Bundle.Monadic qualified as VB
11+
import Data.Vector.Fusion.Bundle.Size qualified as VB
12+
import Data.Vector.Generic qualified as V
13+
import Data.Vector.Generic.Mutable qualified as MV
14+
15+
toLazyList :: (PrimBase m) => VS.Stream m a -> m [a]
16+
toLazyList (VS.Stream step s) = w s
17+
where
18+
w s0 = unsafeInterleave $ do
19+
r <- step s0
20+
case r of
21+
VS.Yield x s1 -> do
22+
xs <- w s1
23+
pure $ x : xs
24+
VS.Skip s1 -> w s1
25+
VS.Done -> pure []
26+
27+
toVector :: (PrimMonad m, V.Vector v a) => VS.Stream m a -> m (v a)
28+
toVector = V.unsafeFreeze <=< MV.munstream . flip VB.fromStream VB.Unknown

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 qualified Data.Vector.Generic as V
8+
import GHC.Wasm.Prim
9+
import System.IO.Unsafe
10+
11+
newtype JSArray = JSArray { unJSArray :: JSVal }
12+
13+
fromJSVals :: (V.Vector v JSVal) => v JSVal -> JSArray
14+
fromJSVals vs = unsafePerformIO $ do
15+
arr <- arrayNew
16+
V.forM_ vs $ arrayPush arr
17+
pure arr
18+
19+
toJSVals :: (V.Vector v JSVal) => JSArray -> v JSVal
20+
toJSVals arr = V.generate (arrayLength arr) (arrayIndex arr)
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: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
module GHC.Wasm.JS.AsyncIterator (
2+
JSAsyncIterable (..),
3+
JSAsyncIterator (..),
4+
fromJSAsyncIterable,
5+
toStream,
6+
toLazyByteString,
7+
) where
8+
9+
import Control.Exception
10+
import qualified Data.ByteString.Lazy as LBS
11+
import qualified Data.Stream.Monadic as VS
12+
import qualified Data.Stream.Monadic.Extra as VS
13+
import GHC.Wasm.JS.Free
14+
import GHC.Wasm.JS.Uint8Array
15+
import GHC.Wasm.Prim
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+
toStream :: JSAsyncIterator -> VS.Stream IO JSVal
28+
toStream iter = flip VS.unfoldrM () $ \_ -> do
29+
r <- jsAsyncIteratorNext iter
30+
if jsIteratorResultDone r
31+
then do
32+
free r
33+
pure Nothing
34+
else do
35+
v <- jsIteratorResultValue r
36+
free r
37+
pure $ Just (v, ())
38+
39+
toLazyByteString :: JSAsyncIterator -> IO LBS.ByteString
40+
toLazyByteString =
41+
fmap LBS.fromChunks
42+
. VS.toLazyList
43+
. VS.mapM
44+
( \v -> do
45+
c <- evaluate $ toStrictByteString $ JSUint8Array v
46+
free v
47+
pure c
48+
)
49+
. toStream
50+
51+
foreign import javascript safe
52+
"$1.next()"
53+
jsAsyncIteratorNext :: JSAsyncIterator -> IO JSIteratorResult
54+
55+
foreign import javascript unsafe
56+
"$1.done"
57+
jsIteratorResultDone :: JSIteratorResult -> Bool
58+
59+
foreign import javascript unsafe
60+
"$1.value"
61+
jsIteratorResultValue :: JSIteratorResult -> IO JSVal

0 commit comments

Comments
 (0)