Skip to content

Commit 7969ee5

Browse files
committed
WIP
0 parents  commit 7969ee5

File tree

23 files changed

+914
-0
lines changed

23 files changed

+914
-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: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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 >=2.2.3.0,
14+
base,
15+
ghc-experimental,
16+
text,
17+
vector >=0.13.2.0,
18+
vector-stream >=0.1.0.1,
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+
Data.Text.Extra
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+
build-depends:
53+
bytestring,
54+
primitive >=0.9.1.0,
55+
56+
test-suite canary-test
57+
import: deps
58+
type: exitcode-stdio-1.0
59+
hs-source-dirs: test
60+
main-is: Main.hs
61+
build-depends:
62+
async >=2.2.5,
63+
canary,
64+
quickcheck-instances >=0.3.32,
65+
tasty >=1.5.3,
66+
tasty-hunit >=0.10.2,
67+
tasty-quickcheck >=0.11.1,

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/Data/Text/Extra.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{-# LANGUAGE MultiWayIf #-}
2+
3+
module Data.Text.Extra
4+
( unsafeCreate,
5+
unsafeCreateUpToN,
6+
unsafeUse,
7+
)
8+
where
9+
10+
import Data.Functor
11+
import Data.Primitive
12+
import Data.Text.Internal qualified as T
13+
import Foreign
14+
import System.IO.Unsafe
15+
16+
unsafeCreate :: Int -> (Ptr Word8 -> IO ()) -> T.Text
17+
unsafeCreate len k = unsafeCreateUpToN len $ \ptr -> k ptr $> len
18+
19+
unsafeCreateUpToN :: Int -> (Ptr Word8 -> IO Int) -> T.Text
20+
unsafeCreateUpToN max_len k
21+
| max_len <= 0 = T.empty
22+
| otherwise = unsafePerformIO $ do
23+
mba <- newPinnedByteArray max_len
24+
len <- k (mutableByteArrayContents mba)
25+
ba <- unsafeFreezeByteArray mba
26+
pure $ T.Text ba 0 len
27+
28+
unsafeUse :: T.Text -> (Ptr Word8 -> Int -> IO a) -> a
29+
unsafeUse (T.Text ba off len) k =
30+
unsafePerformIO $
31+
if
32+
| len <= 0 -> k nullPtr 0
33+
| isByteArrayPinned ba -> withByteArrayContents ba $
34+
\ptr -> k (ptr `plusPtr` off) len
35+
| otherwise -> do
36+
mba <- newPinnedByteArray len
37+
withMutableByteArrayContents mba $ \ptr -> do
38+
copyByteArray mba 0 ba off len
39+
k ptr len
40+

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

0 commit comments

Comments
 (0)