on master help sign in
35 files changed+463−425
since splitsnapshots
unifiedsplit
.travis.yml+40−24 README.md+9−9 cabal.project+3−35 default.nix−8 jsaddle-clib/default.nix−9 jsaddle-clib/jsaddle-clib.cabal+4−3 jsaddle-clib/src-ghc/Language/Javascript/JSaddle/CLib.hs+3−1 jsaddle-warp/default.nix−25 jsaddle-warp/jsaddle-warp.cabal+16−10 jsaddle-warp/src/Language/Javascript/JSaddle/Test.hs+13−12 jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs+78−32 jsaddle-warp/tests/DocTest.hs+20−7 jsaddle-webkit2gtk/default.nix−16 jsaddle-webkit2gtk/jsaddle-webkit2gtk.cabal+4−4 jsaddle-webkit2gtk/src/Language/Javascript/JSaddle/WebKitGTK.hs+16−37 jsaddle-webkitgtk/default.nix−16 jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+57−2 jsaddle-wkwebview/cbits-uikit/AppDelegate.m+2−1 jsaddle-wkwebview/cbits/WKWebView.m+8−10 jsaddle-wkwebview/default.nix+14−21 jsaddle-wkwebview/jsaddle-wkwebview.cabal+18−11 jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+55−18 jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+29−5 jsaddle/default.nix−23 jsaddle/jsaddle.cabal+15−16 jsaddle/src-ghc/Data/JSString/Internal/Type.hs+2−1 jsaddle/src-ghc/JavaScript/Array.hs+2−2 jsaddle/src/Language/Javascript/JSaddle.hs+5−5 jsaddle/src/Language/Javascript/JSaddle/Evaluate.hs+1−1 jsaddle/src/Language/Javascript/JSaddle/Object.hs+5−6 jsaddle/src/Language/Javascript/JSaddle/Run.hs+17−3 jsaddle/src/Language/Javascript/JSaddle/Run/Files.hs+7−38 jsaddle/src/Language/Javascript/JSaddle/Types.hs+10−2 nix/hix.nix+8 travis/build.sh+2−12
.travis.yml+40−24
@@ -1,31 +1,47 @@
1−sudo: required
2−services:
3− - docker
1+language: nix
2+nix: 2.2.2
43
5−cache:
6− directories:
7− - .cabal
8− - .ghc
4+jobs:
5+ include:
6+ - stage: Build
7+ env: OS=linux GHCVER=ghc865
8+ - stage: Build
9+ env: OS=linux GHCVER=ghc881
10+ - env: OS=macos GHCVER=ghc865
11+ os: osx
12+ # Fix OSX, see https://travis-ci.community/t/cannot-use-nix-support-on-osx/2927
13+ language: generic
14+ before_install:
15+ - curl https://nixos.org/nix/install | sh
16+ - source /Users/travis/.nix-profile/etc/profile.d/nix.sh
17+ - env: OS=macos GHCVER=ghc881
18+ os: osx
19+ # Fix OSX, see https://travis-ci.community/t/cannot-use-nix-support-on-osx/2927
20+ language: generic
21+ before_install:
22+ - curl https://nixos.org/nix/install | sh
23+ - source /Users/travis/.nix-profile/etc/profile.d/nix.sh
924
10−env:
11−# - CABALVER=1.24 GHCVER=7.10.3 OSVER=xenial
12− - CABALVER=1.24 GHCVER=8.0.1 OSVER=xenial
13− - CABALVER=1.24 GHCVER=8.0.2 OSVER=xenial
14− - CABALVER=1.24 GHCVER=7.10.3 OSVER=trusty
15− - CABALVER=1.24 GHCVER=8.0.1 OSVER=trusty
16− - CABALVER=1.24 GHCVER=8.0.2 OSVER=trusty
17−
18−before_install:
19− - docker build -t ghc -f travis/$OSVER.Dockerfile travis
25+install:
26+- sudo mkdir -p /etc/nix
27+- echo "substituters = https://cache.nixos.org https://hydra.iohk.io" | sudo tee -a /etc/nix/nix.conf > /dev/null
28+- echo "trusted-public-keys = cache.nixos.org-1:6NCHdD59X431o0gWypbMrAURkbJ16ZPMQFGspcDShjY= hydra.iohk.io:f/Ea+s+dFdN+3Y/G+FDgSq+a5NEWhJGzdjvKNGv0/EQ=" | sudo tee -a /etc/nix/nix.conf > /dev/null
2029
2130script:
22− - docker run -v `pwd`:/build ghc
23− sh -x -c "
24− apt-get install -y cabal-install-$CABALVER ghc-$GHCVER &&
25− export PATH=\$HOME/.cabal/bin:/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:/opt/alex/3.1.7/bin:/opt/happy/1.19.5/bin:\$PATH &&
26− ./travis/build.sh"
31+- nix-build --argstr haskellCompiler $GHCVER -A hsPkgs.jsaddle.components.library
32+- nix-build --argstr haskellCompiler $GHCVER -A hsPkgs.jsaddle-warp.components.library
33+- nix-build --argstr haskellCompiler $GHCVER -A hsPkgs.jsaddle-clib.components.library
34+- |
35+ if [ "$OS" == "macos" ]; then
36+ nix-build --argstr haskellCompiler $GHCVER -A hsPkgs.jsaddle-wkwebview.components.library
37+ fi
38+# TODO try to find how to get working webkitgtk with nix on macOS
39+- |
40+ if [ "$OS" != "macos" ]; then
41+ nix-build --argstr haskellCompiler $GHCVER -A hsPkgs.jsaddle-webkit2gtk.components.library
42+ fi
43+
2744notifications:
2845 irc:
2946 channels: "irc.freenode.net#ghcjs"
30− skip_join: true
31− email: true
47+ email: true
README.md+9−9
@@ -80,7 +80,7 @@ main = run 3708 $ do
8080 appendChild newParagraph text
8181 appendChild body (Just newParagraph)
8282 return ()
83−
83+
8484 -- Make an exit button
8585 exitMVar <- liftIO newEmptyMVar
8686 Just exit <- createElement doc (Just "span")
@@ -111,8 +111,8 @@ There are a number of different JSaddle runners to choose from
111111* [jsaddle-clib](https://hackage.haskell.org/package/jsaddle-clib) - C interface used to run JSaddle on Android using JNI.
112112
113113In all of these cases a web control or browser of some sort is used. An
114−[HTML file](https://github.com/ghcjs/jsaddle/blob/master/data/index.html)
115−and a [small JavaScript command interpreter](https://github.com/ghcjs/jsaddle/blob/master/data/jsaddle.js)
114+[HTML file](./jsaddle/src/Language/Javascript/JSaddle/Run/Files.hs#L24)
115+and a [small JavaScript command interpreter](./jsaddle/src/Language/Javascript/JSaddle/Run/Files.hs#L340)
116116are loaded into it. Then the native GHC compiled JSaddle code runs. In order to interact with the browser
117117it sends commands to the JavaScript command interpreter. The mechanism for sending the commands differs
118118for the different runners, but they are always combined into batches and encoded in JSON.
@@ -123,7 +123,7 @@ JavaScript thread until the Haskell callback completes) or asynchronous.
123123### Why use a JavaScript command interpreter?
124124
125125Older versions of JSaddle relied on the WebKit1 interface to WebKitGTK and JavaScriptCore that is only supported in older versions of
126−WebKitGTK. With the newer WekKit2 interface this level of access is only available to WebKit Extensions.
126+WebKitGTK. With the newer WebKit2 interface this level of access is only available to WebKit Extensions.
127127The general advice for people migrating from WebKit1 to WebKit2 seems to be to use JavaScript.
128128
129129As a bonus we have been able to support a number of different platforms with the JavaScript command interpreter and it should
@@ -137,8 +137,8 @@ The first five keys `[0..4]` are
137137these values. Both the Haskell code and `jsaddle.js` can allocate new `JSVal` keys. The Haskell code allocates negative
138138keys and `jsaddle.js` allocates positive keys to avoid clashing.
139139
140−The haskell code will not know the value of the to go with the key, but it will include it in commands to the server
141−that are sort of "hey call this function and and put the result in the map with this key"). This allows for the
140+The haskell code will not know the value to go with the key, but it will include it in commands to the server
141+that are sort of "hey call this function and put the result in the map with this key"). This allows for the
142142lazy execution of the JSaddle code.
143143
144144A finalizer is added on the Haskell code and an asynchronous `FreeJSVal` command is automatically sent to `jsaddle.js`
@@ -146,7 +146,7 @@ when the JSVal is no longer reachable. The `jsaddle.js` code then deletes the k
146146
147147### JSStrings
148148
149−These are haskell `Text` type and so reside on the native side of the WebSocket. If you want to make avoid transferring large string values over the WebSocket use `toJSVal` to conevt it to a `JSVal`.
149+These are haskell `Text` type and so reside on the native side of the WebSocket. If you want to avoid transferring large string values over the WebSocket use `toJSVal` to convert it to a `JSVal`.
150150
151151### Lazy Execution
152152
@@ -178,7 +178,7 @@ force all the pending asynchronous commands to be executed.
178178It uses a handful of JS FFI calls to execute JavaScript functions indirectly. This indirection will be small compared to the overhead of the WebSockets approach (used when JSaddle is compiled with GHC), but it will be significant compared to hand crafted JS FFI calls.
179179
180180For the best performance you may want to write both JS FFI and JSaddle wrappers
181−For your JavaScript code. This is the approach taken by `ghcjs-dom`.
181+for your JavaScript code. This is the approach taken by `ghcjs-dom`.
182182
183183For instance here is `getElementById` from the `ghcjs-dom-jsffi` (used by `ghcjs-dom` when compiled with GHCJS)
184184
@@ -213,7 +213,7 @@ JSaddle does not support exceptions well. When compiled with GHCJS an exception
213213at the point the exception is thrown.
214214
215215When using GHC the Haskell executions will probably continue for a while before the exception is received at all by the Haskell code
216−(because the lazy execution of the JS code). The exception will be thrown when the the next synchronous command is executed.
216+(because the lazy execution of the JS code). The exception will be thrown when the next synchronous command is executed.
217217When it reaches the synchronous command a haskell type JSException will be thrown (this may not be the thread that initiated the
218218command that caused the exception). You can use `syncPoint` and `syncAfter` to force the exception to be thrown.
219219There are `JSM` versions of `catch` and `bracket` that also include a `syncPoint` call.
cabal.project+3−35
@@ -1,42 +1,10 @@
1−flags: -overloaded-methods -overloaded-signals -overloaded-properties
2−
31packages:
42 jsaddle/
53 jsaddle-warp/
64 jsaddle-wkwebview/
7−-- jsaddle-webkitgtk/
85 jsaddle-webkit2gtk/
96 jsaddle-clib/
107
11−package gi-atk
12− flags: -overloaded-methods -overloaded-signals -overloaded-properties
13−package gi-cairo
14− flags: -overloaded-methods -overloaded-signals -overloaded-properties
15−package gi-gdk
16− flags: -overloaded-methods -overloaded-signals -overloaded-properties
17−package gi-gdkpixbuf
18− flags: -overloaded-methods -overloaded-signals -overloaded-properties
19−package gi-gio
20− flags: -overloaded-methods -overloaded-signals -overloaded-properties
21−package gi-glib
22− flags: -overloaded-methods -overloaded-signals -overloaded-properties
23−package gi-gobject
24− flags: -overloaded-methods -overloaded-signals -overloaded-properties
25−package gi-gtk
26− flags: -overloaded-methods -overloaded-signals -overloaded-properties
27−package gi-gtk-hs
28− flags: -overloaded-methods -overloaded-signals -overloaded-properties
29−package gi-gtkosxapplication
30− flags: -overloaded-methods -overloaded-signals -overloaded-properties
31−package gi-gtksource
32− flags: -overloaded-methods -overloaded-signals -overloaded-properties
33−package gi-javascriptcore
34− flags: -overloaded-methods -overloaded-signals -overloaded-properties
35−package gi-pango
36− flags: -overloaded-methods -overloaded-signals -overloaded-properties
37−package gi-soup
38− flags: -overloaded-methods -overloaded-signals -overloaded-properties
39−package gi-webkit
40− flags: -overloaded-methods -overloaded-signals -overloaded-properties
41−package gi-webkit2
42− flags: -overloaded-methods -overloaded-signals -overloaded-properties
8+constraints: haskell-gi-overloading == 0.0
9+
10+allow-newer: websockets:random, splitmix:time, *:time, *:unix, *:process, stm:base
default.nixdeleted−8
@@ -1,8 +0,0 @@
1−haskellPackages: {
2− jsaddle = haskellPackages.callPackage ./jsaddle {};
3− jsaddle-warp = haskellPackages.callPackage ./jsaddle-warp {};
4− jsaddle-wkwebview = haskellPackages.callPackage ./jsaddle-wkwebview {};
5− jsaddle-webkit2gtk = haskellPackages.callPackage ./jsaddle-webkit2gtk {};
6− jsaddle-webkitgtk = haskellPackages.callPackage ./jsaddle-webkitgtk {};
7− jsaddle-clib = haskellPackages.callPackage ./jsaddle-clib {};
8−}
jsaddle-clib/default.nixdeleted−9
@@ -1,9 +0,0 @@
1−{ mkDerivation, aeson, base, bytestring, data-default, jsaddle, stdenv, text }:
2−mkDerivation {
3− pname = "jsaddle-clib";
4− version = "0.9.0.0";
5− src = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [ ".git" "dist" ])) ./.;
6− libraryHaskellDepends = [ aeson base bytestring data-default jsaddle text ];
7− description = "Interface for JavaScript that works with GHCJS and GHC";
8− license = stdenv.lib.licenses.mit;
9−}
jsaddle-clib/jsaddle-clib.cabal+4−3
@@ -1,5 +1,5 @@
11name: jsaddle-clib
2−version: 0.9.0.0
2+version: 0.9.8.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -21,8 +21,9 @@ library
2121 Language.Javascript.JSaddle.CLib
2222 Language.Javascript.JSaddle.CLib.Internal
2323 build-depends:
24− aeson >=0.8.0.2 && <1.3,
24+ aeson >=0.8.0.2 && <1.6,
2525 base <5,
26+ base-compat >=0.9.0 && <0.12,
2627 bytestring >=0.10.6.0 && <0.11,
2728 jsaddle >= 0.9.0.0 && <0.10,
2829 data-default,
@@ -32,7 +33,7 @@ library
3233 Include-dirs: cbits/include
3334 Includes: jsaddle.h
3435 Install-includes: jsaddle.h
35− ghc-options: -ferror-spans -Wall -Werror
36+ ghc-options: -ferror-spans -Wall
3637 if impl(ghcjs)
3738 hs-source-dirs: src-ghcjs
3839 else
jsaddle-clib/src-ghc/Language/Javascript/JSaddle/CLib.hs+3−1
@@ -8,6 +8,9 @@ module Language.Javascript.JSaddle.CLib
88 , appConfigToAppCallbacks
99 ) where
1010
11+import Prelude ()
12+import Prelude.Compat
13+
1114import Control.Monad (void)
1215import Control.Concurrent (forkIO)
1316
@@ -16,7 +19,6 @@ import Data.ByteString (useAsCString, packCString)
1619import Data.ByteString.Char8 (unpack)
1720import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1821import Data.Default (def, Default)
19−import Data.Monoid ((<>))
2022import Data.Text (Text)
2123import qualified Data.Text.Encoding as T
2224
jsaddle-warp/default.nixdeleted−25
@@ -1,25 +0,0 @@
1−{ mkDerivation, aeson, base, bytestring, containers, deepseq
2−, doctest, filepath, foreign-store, ghc-prim, http-types, jsaddle
3−, lens, network, primitive, process, QuickCheck, ref-tf, stdenv
4−, stm, text, time, transformers, uuid, uuid-types, wai
5−, wai-websockets, warp, webdriver, websockets
6−}:
7−mkDerivation {
8− pname = "jsaddle-warp";
9− version = "0.9.4.0";
10− src = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [ ".git" "dist" ])) ./.;
11− libraryHaskellDepends = [
12− aeson base bytestring containers foreign-store http-types jsaddle
13− stm text time transformers uuid uuid-types wai wai-websockets warp
14− websockets
15− ];
16− testHaskellDepends = [
17− aeson base bytestring containers deepseq doctest filepath ghc-prim
18− http-types jsaddle lens network primitive process QuickCheck ref-tf
19− stm text time transformers wai wai-websockets warp webdriver
20− websockets
21− ];
22− testTarget = "--test-option=${jsaddle.src}";
23− description = "Interface for JavaScript that works with GHCJS and GHC";
24− license = stdenv.lib.licenses.mit;
25−}
jsaddle-warp/jsaddle-warp.cabal+16−10
@@ -1,5 +1,5 @@
11name: jsaddle-warp
2−version: 0.9.5.0
2+version: 0.9.8.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -10,7 +10,7 @@ description:
1010 This package provides an EDSL for calling JavaScript that
1111 can be used both from GHCJS and GHC. When using GHC
1212 the application is run using Warp and WebSockets to
13− drive a small JavaScipt helper.
13+ drive a small JavaScript helper.
1414category: Web, Javascript
1515author: Hamish Mackenzie
1616
@@ -24,20 +24,22 @@ library
2424 if !impl(ghcjs -any)
2525 exposed-modules:
2626 Language.Javascript.JSaddle.WebSockets
27+ other-modules:
28+ Language.Javascript.JSaddle.WebSockets.Compat
2729 build-depends:
28− aeson >=0.8.0.2 && <1.3,
30+ aeson >=0.8.0.2 && <1.6,
2931 bytestring >=0.10.6.0 && <0.11,
30− containers >=0.5.6.2 && <0.6,
32+ containers >=0.5.6.2 && <0.7,
3133 foreign-store >=0.2 && <0.3,
32− http-types >=0.8.6 && <0.12,
34+ http-types >=0.8.6 && <0.13,
3335 jsaddle >=0.9.4.0 && <0.10,
34− stm >=2.4.4 && <2.5,
36+ stm >=2.4.4 && <2.6,
3537 text >=1.2.1.3 && <1.3,
36− time >=1.5.0.1 && <1.9,
38+ time >=1.5.0.1 && <1.11,
3739 transformers >=0.4.2.0 && <0.6,
3840 wai >=3.0.3.0 && <3.3,
3941 wai-websockets >=3.0.0.6 && <3.1,
40− warp >=3.1.2 && <3.3,
42+ warp >=3.1.2 && <3.4,
4143 websockets >=0.9.5.0 && <0.13
4244 exposed-modules:
4345 Language.Javascript.JSaddle.Warp
@@ -57,8 +59,10 @@ test-suite test-tool
5759 bytestring -any,
5860 containers -any,
5961 deepseq -any,
60− doctest >=0.10.1 && <0.12,
62+ doctest >=0.10.1 && <0.19,
63+ exceptions -any,
6164 filepath >=1.4.0.0 && <1.5,
65+ foreign-store -any,
6266 ghc-prim -any,
6367 http-types -any,
6468 jsaddle -any,
@@ -68,14 +72,16 @@ test-suite test-tool
6872 ref-tf -any,
6973 primitive -any,
7074 process >=1.2.3.0 && <1.7,
75+ random -any,
7176 stm -any,
7277 text -any,
7378 time -any,
7479 transformers -any,
80+ unliftio-core -any,
7581 wai -any,
7682 wai-websockets -any,
7783 warp -any,
78− webdriver >=0.8.4 && <0.9,
84+ webdriver >=0.8.4 && <0.10,
7985 websockets -any
8086 type: exitcode-stdio-1.0
8187 main-is: DocTest.hs
jsaddle-warp/src/Language/Javascript/JSaddle/Test.hs+13−12
@@ -17,21 +17,22 @@ module Language.Javascript.JSaddle.Test (
1717 , listWindowProperties
1818) where
1919
20−import Control.Applicative
2120import Prelude hiding((!!), catch)
22−import Control.Monad.Trans.Reader (runReaderT)
23−import Language.Javascript.JSaddle
24−import qualified Data.Text as T
25−import Control.Monad.IO.Class (MonadIO(..))
26−import Control.Monad (forever, forM)
27−import Control.Lens.Getter ((^.))
28−import Data.Monoid ((<>))
21+import Control.Applicative
22+import Control.Concurrent (threadDelay, forkIO, ThreadId)
2923import Control.Concurrent.MVar
3024 (tryTakeMVar, putMVar, takeMVar, newEmptyMVar, MVar)
31−import System.IO.Unsafe (unsafePerformIO)
32−import Control.Concurrent (threadDelay, forkIO, ThreadId)
25+import Control.Lens.Getter ((^.))
26+import Control.Monad (forever, forM)
27+import Control.Monad.IO.Class (MonadIO(..))
28+import Control.Monad.Trans.Reader (runReaderT)
29+import Data.Monoid ((<>))
3330import Data.Text (Text)
31+import qualified Data.Text as T
32+import Language.Javascript.JSaddle
3433import Language.Javascript.JSaddle.Warp (run)
34+import System.IO.Unsafe (unsafePerformIO)
35+import System.Timeout (timeout)
3536
3637context :: MVar JSContextRef
3738context = unsafePerformIO newEmptyMVar
@@ -52,12 +53,12 @@ startServer =
5253
5354-- >>> testJSaddle $ ((global ^. js "console" . js "log") # ["Hello"])
5455testJSaddle :: ToJSVal val => JSM val -> IO ()
55−testJSaddle f = do
56+testJSaddle f = timeout 10000000 (do
5657 startServer
5758 c <- takeMVar context
5859 runReaderT (unJSM $ (f >>= valToText >>= liftIO . putStrLn . T.unpack)
5960 `catch` \ (JSException e) -> valToText e >>= liftIO . putStrLn . T.unpack) c
60− putMVar context c
61+ putMVar context c) >>= maybe (putStrLn "testJSaddle timed out") return
6162
6263debugLog :: String -> IO ()
6364debugLog _ = return ()
jsaddle-warp/src/Language/Javascript/JSaddle/WebSockets.hs+78−32
@@ -19,9 +19,13 @@ module Language.Javascript.JSaddle.WebSockets (
1919 jsaddleOr
2020 , jsaddleApp
2121 , jsaddleWithAppOr
22+ , jsaddleAppWithJs
23+ , jsaddleAppWithJsOr
2224 , jsaddleAppPartial
2325 , jsaddleJs
26+ , jsaddleJs'
2427 , debug
28+ , debugOr
2529 , debugWrapper
2630) where
2731
@@ -56,44 +60,57 @@ import qualified Data.Map as M (empty, insert, lookup)
5660import Data.IORef
5761 (readIORef, newIORef, atomicModifyIORef')
5862import Data.ByteString.Lazy (ByteString)
63+import qualified Data.ByteString.Lazy as LBS
64+ (toStrict, stripPrefix)
5965import Control.Concurrent.MVar
6066 (tryTakeMVar, MVar, tryPutMVar, modifyMVar_, putMVar, takeMVar,
6167 readMVar, newMVar, newEmptyMVar, modifyMVar)
6268import Network.Wai.Handler.Warp
6369 (defaultSettings, setTimeout, setPort, runSettings)
64−import Foreign.Store (newStore, readStore, lookupStore)
70+import Foreign.Store (readStore, lookupStore, writeStore, Store(..))
6571import Language.Javascript.JSaddle (askJSM)
6672import Control.Monad.IO.Class (MonadIO(..))
6773
6874import Language.Javascript.JSaddle.WebSockets.Compat (getTextMessageByteString)
75+import qualified Data.Text.Encoding as T (decodeUtf8)
6976
7077jsaddleOr :: ConnectionOptions -> JSM () -> Application -> IO Application
7178jsaddleOr opts entryPoint otherApp = do
7279 syncHandlers <- newIORef M.empty
80+ asyncHandlers <- newIORef M.empty
7381 let wsApp :: ServerApp
7482 wsApp pending_conn = do
7583 conn <- acceptRequest pending_conn
76− rec (processResult, processSyncResult, start) <- runJavaScript (sendTextData conn . encode) $ do
77− syncKey <- T.pack . show . contextId <$> askJSM
78− liftIO $ atomicModifyIORef' syncHandlers (\m -> (M.insert syncKey processSyncResult m, ()))
79− liftIO $ sendTextData conn (encode syncKey)
80− entryPoint
81− _ <- forkIO . forever $
82− receiveDataMessage conn >>= \msg -> case getTextMessageByteString msg of
83− Just t ->
84− case decode t of
85− Nothing -> error $ "jsaddle Results decode failed : " <> show t
86− Just r -> processResult r
87− _ -> error "jsaddle WebSocket unexpected binary data"
88− start
89− waitTillClosed conn
84+ initMsg <- receiveDataMessage conn
85+ case getTextMessageByteString initMsg of
86+ Just "" -> do
87+ rec (processResult, processSyncResult, start) <- runJavaScript (sendTextData conn . encode) $ do
88+ syncKey <- T.pack . show . contextId <$> askJSM
89+ liftIO $ atomicModifyIORef' syncHandlers (\m -> (M.insert syncKey processSyncResult m, ()))
90+ liftIO $ atomicModifyIORef' asyncHandlers (\m -> (M.insert syncKey processResult m, ()))
91+ liftIO $ sendTextData conn (encode syncKey)
92+ entryPoint
93+ start
94+ waitTillClosed conn
95+ Just syncKey ->
96+ M.lookup (T.decodeUtf8 $ LBS.toStrict syncKey) <$> readIORef syncHandlers >>= \case
97+ Nothing -> error "jsaddle missing sync message handler"
98+ Just processResult ->
99+ forever $
100+ receiveDataMessage conn >>= \msg -> case getTextMessageByteString msg of
101+ Just t ->
102+ case decode t of
103+ Nothing -> error $ "jsaddle Results decode failed : " <> show t
104+ Just r -> processResult r
105+ _ -> error "jsaddle WebSocket unexpected binary data"
106+ _ -> error "jsaddle WebSocket unexpected binary data"
90107
91108 -- Based on Network.WebSocket.forkPingThread
92109 waitTillClosed conn = ignore `handle` go 1
93110 where
94111 go :: Int -> IO ()
95112 go i = do
96− threadDelay (1 * 1000 * 1000)
113+ threadDelay (10 * 1000 * 1000)
97114 sendPing conn (T.pack $ show i)
98115 go (i + 1)
99116
@@ -126,9 +143,14 @@ jsaddleApp = jsaddleAppWithJs $ jsaddleJs False
126143
127144jsaddleAppWithJs :: ByteString -> Application
128145jsaddleAppWithJs js req sendResponse =
129− fromMaybe
130− (sendResponse $ W.responseLBS H.status403 [("Content-Type", "text/plain")] "Forbidden")
131− (jsaddleAppPartialWithJs js req sendResponse)
146+ jsaddleAppWithJsOr js
147+ (\_ _ -> sendResponse $ W.responseLBS H.status403 [("Content-Type", "text/plain")] "Forbidden")
148+ req sendResponse
149+
150+jsaddleAppWithJsOr :: ByteString -> Application -> Application
151+jsaddleAppWithJsOr js otherApp req sendResponse =
152+ fromMaybe (otherApp req sendResponse)
153+ (jsaddleAppPartialWithJs js req sendResponse)
132154
133155jsaddleWithAppOr :: ConnectionOptions -> JSM () -> Application -> IO Application
134156jsaddleWithAppOr opts entryPoint otherApp = jsaddleOr opts entryPoint $ \req sendResponse ->
@@ -147,32 +169,49 @@ jsaddleAppPartialWithJs js req sendResponse = case (W.requestMethod req, W.pathI
147169 ("GET", ["jsaddle.js"]) -> Just $ sendResponse $ W.responseLBS H.status200 [("Content-Type", "application/javascript")] js
148170 _ -> Nothing
149171
172+jsaddleJs :: Bool -> ByteString
173+jsaddleJs = jsaddleJs' Nothing
174+
150175-- Use this to generate this string for embedding
151176-- sed -e 's|\\|\\\\|g' -e 's|^| \\|' -e 's|$|\\n\\|' -e 's|"|\\"|g' data/jsaddle.js | pbcopy
152−jsaddleJs :: Bool -> ByteString
153−jsaddleJs refreshOnLoad = "\
154− \if(typeof global !== \"undefined\") {\n\
177+jsaddleJs' :: Maybe ByteString -> Bool -> ByteString
178+jsaddleJs' jsaddleUri refreshOnLoad = "\
179+ \if(typeof global !== \"undefined\" && typeof require === \"function\") {\n\
155180 \ global.window = global;\n\
156181 \ global.WebSocket = require('ws');\n\
157182 \}\n\
158183 \\n\
159184 \var connect = function() {\n\
160− \ var wsaddress = window.location.protocol.replace('http', 'ws')+\"//\"+window.location.hostname+(window.location.port?(\":\"+window.location.port):\"\");\n\
185+ \ var wsaddress = "
186+ <> maybe "window.location.protocol.replace('http', 'ws')+\"//\"+window.location.hostname+(window.location.port?(\":\"+window.location.port):\"\")"
187+ (\ s -> "\"ws" <> s <> "\"")
188+ (jsaddleUri >>= LBS.stripPrefix "http")
189+ <> ";\n\
161190 \\n\
162− \ var ws = new WebSocket(wsaddress);\n\
191+ \ var ws0 = new WebSocket(wsaddress);\n\
163192 \ var syncKey = \"\";\n\
164193 \\n\
165− \ ws.onopen = function(e) {\n\
194+ \ ws0.onopen = function(e) {\n\
195+ \ ws0.send(\"\");\n\
196+ \ var initialResults = [];\n\
197+ \ var ws = {send: function(m) {initialResults.push(m);}};\n\
166198 \ " <> initState <> "\n\
167199 \\n\
168− \ ws.onmessage = function(e) {\n\
200+ \ ws0.onmessage = function(e) {\n\
169201 \ var batch = JSON.parse(e.data);\n\
170202 \ if(inCallback > 0) {\n\
171203 \ asyncBatch = batch;\n\
172204 \ return;\n\
173205 \ }\n\
174206 \ if(typeof batch === \"string\") {\n\
175− \ syncKey = batch;\n" <>
207+ \ syncKey = batch;\n\
208+ \ var ws1 = new WebSocket(wsaddress);\n\
209+ \ ws1.onopen = function(e) {\n\
210+ \ ws1.send(syncKey);\n\
211+ \ initialResults.forEach(function(m){ ws1.send(m); });\n\
212+ \ initialResults = null;\n\
213+ \ ws = ws1;\n\
214+ \ }\n" <>
176215 (if refreshOnLoad
177216 then " var xhr = new XMLHttpRequest();\n\
178217 \ xhr.open('POST', '/reload/'+syncKey, true);\n\
@@ -188,13 +227,13 @@ jsaddleJs refreshOnLoad = "\
188227 \ " <> runBatch (\a -> "ws.send(JSON.stringify(" <> a <> "));")
189228 (Just (\a -> "(function(){\n\
190229 \ var xhr = new XMLHttpRequest();\n\
191− \ xhr.open('POST', '/sync/'+syncKey, false);\n\
230+ \ xhr.open('POST', '" <> fromMaybe "" jsaddleUri <> "/sync/'+syncKey, false);\n\
192231 \ xhr.setRequestHeader(\"Content-type\", \"application/json\");\n\
193232 \ xhr.send(JSON.stringify(" <> a <> "));\n\
194233 \ return JSON.parse(xhr.response);})()")) <> "\
195234 \ };\n\
196235 \ };\n\
197− \ ws.onerror = function() {\n\
236+ \ ws0.onerror = function() {\n\
198237 \ setTimeout(connect, 1000);\n\
199238 \ };\n\
200239 \}\n\
@@ -213,6 +252,13 @@ debug port f = do
213252 jsaddleOr defaultConnectionOptions (registerContext >> f >> syncPoint) (withRefresh $ jsaddleAppWithJs $ jsaddleJs True)
214253 putStrLn $ "<a href=\"http://localhost:" <> show port <> "\">run</a>"
215254
255+debugOr :: Int -> JSM () -> Application -> IO ()
256+debugOr port f b = do
257+ debugWrapper $ \withRefresh registerContext ->
258+ runSettings (setPort port (setTimeout 3600 defaultSettings)) =<<
259+ jsaddleOr defaultConnectionOptions (registerContext >> f >> syncPoint) (withRefresh $ jsaddleAppWithJsOr (jsaddleJs True) b)
260+ putStrLn $ "<a href=\"http://localhost:" <> show port <> "\">run</a>"
261+
216262refreshMiddleware :: ((Response -> IO ResponseReceived) -> IO ResponseReceived) -> Middleware
217263refreshMiddleware refresh otherApp req sendResponse = case (W.requestMethod req, W.pathInfo req) of
218264 ("POST", ["reload", _syncKey]) -> refresh sendResponse
@@ -261,13 +307,13 @@ debugWrapper run = do
261307 start' <- takeMVar mvar
262308 n <- stop
263309 start' n >>= restarter mvar
264− lookupStore shutdown_0 >>= \case
310+ lookupStore storeId >>= \case
265311 Nothing -> do
266312 restartMVar <- newMVar start
267313 void . forkIO $ restarter restartMVar (return 0)
268− void $ newStore restartMVar
314+ void $ writeStore (Store storeId) restartMVar
269315 Just shutdownStore -> do
270316 restartMVar :: MVar (Int -> IO (IO Int)) <- readStore shutdownStore
271317 void $ tryTakeMVar restartMVar
272318 putMVar restartMVar start
273− where shutdown_0 = 0
319+ where storeId = 354
jsaddle-warp/tests/DocTest.hs+20−7
@@ -11,18 +11,17 @@ import System.IO (stderr, BufferMode(..), stdout, hSetBuffering)
1111import System.FilePath ((</>))
1212import System.Exit (exitFailure, exitWith, ExitCode(..))
1313import System.Process (readProcess, system)
14−import Control.Concurrent (forkIO)
14+import Control.Concurrent
15+ (takeMVar, newEmptyMVar, forkIO, threadDelay, putMVar)
1516import Control.Monad (void)
1617import Control.Monad.IO.Class (MonadIO(..))
1718import Data.Monoid ((<>))
1819import System.Environment (getArgs)
19−import Language.Javascript.JSaddle.Run.Files (jsaddleJs)
20−import qualified Data.ByteString.Lazy.Char8 as BS (unpack)
2120import Test.WebDriver (runSession, defaultConfig, openPage, closeSession)
2221
2322main :: IO ()
2423main = do
25− putStrLn "Hello"
24+ putStrLn "Testing JSaddle"
2625 jsaddlePath <- getArgs >>= \case
2726 [arg] -> return arg
2827 _ -> do
@@ -30,14 +29,18 @@ main = do
3029 exitFailure
3130 hSetBuffering stdout LineBuffering
3231 hSetBuffering stderr LineBuffering
32+ putStrLn "Checking for phantomjs"
3333 node <- system "phantomjs --version" >>= \case
3434 ExitSuccess -> return ()
3535 e -> do
3636 putStrLn "phantomjs not found"
3737 exitWith e
38− forkIO . void $ readProcess "phantomjs" ["--webdriver=4444"] (BS.unpack jsaddleJs) >>= putStr
39− runSession defaultConfig $ do
40− openPage "http://localhost:3709"
38+ putStrLn "Starting phantomjs"
39+ forkIO . void $ readProcess "phantomjs" ["--webdriver=4444"] "" >>= putStr
40+ threadDelay 5000000
41+ putStrLn "Running Tests"
42+ done <- newEmptyMVar
43+ forkIO $ do
4144 liftIO $ doctest [
4245 "-hide-all-packages",
4346 "-package=base-" ++ VERSION_base,
@@ -61,6 +64,10 @@ main = do
6164 "-package=ref-tf-" ++ VERSION_ref_tf,
6265 "-package=deepseq-" ++ VERSION_deepseq,
6366 "-package=ghc-prim-" ++ VERSION_ghc_prim,
67+ "-package=exceptions-" ++ VERSION_exceptions,
68+ "-package=unliftio-core-" ++ VERSION_unliftio_core,
69+ "-package=random-" ++ VERSION_random,
70+ "-package=foreign-store-" ++ VERSION_foreign_store,
6471 "-i" <> "src",
6572 "-i" <> "src-ghc",
6673 "src/Language/Javascript/JSaddle/Test.hs",
@@ -83,6 +90,7 @@ main = do
8390 jsaddlePath </> "src/Language/Javascript/JSaddle/Arguments.hs",
8491 jsaddlePath </> "src/Language/Javascript/JSaddle/Classes.hs",
8592 jsaddlePath </> "src/Language/Javascript/JSaddle/Classes/Internal.hs",
93+ jsaddlePath </> "src/Language/Javascript/JSaddle/Debug.hs",
8694 jsaddlePath </> "src/Language/Javascript/JSaddle/Evaluate.hs",
8795 jsaddlePath </> "src/Language/Javascript/JSaddle/Exception.hs",
8896 jsaddlePath </> "src/Language/Javascript/JSaddle/Monad.hs",
@@ -96,4 +104,9 @@ main = do
96104 jsaddlePath </> "src/Language/Javascript/JSaddle/String.hs",
97105 jsaddlePath </> "src/Language/Javascript/JSaddle/Types.hs",
98106 jsaddlePath </> "src/Language/Javascript/JSaddle/Value.hs" ]
107+ putMVar done ()
108+ threadDelay 5000000
109+ runSession defaultConfig $ do
110+ openPage "http://127.0.0.1:3709"
111+ liftIO $ takeMVar done
99112 closeSession
jsaddle-webkit2gtk/default.nixdeleted−16
@@ -1,16 +0,0 @@
1−{ mkDerivation, aeson, base, bytestring, directory, gi-gio, gi-glib
2−, gi-gtk, gi-javascriptcore, gi-webkit2, haskell-gi-base, jsaddle
3−, stdenv, text, unix, webkit2gtk3-javascriptcore
4−}:
5−mkDerivation {
6− pname = "jsaddle-webkit2gtk";
7− version = "0.9.4.0";
8− src = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [ ".git" "dist" ])) ./.;
9− libraryHaskellDepends = [
10− aeson base bytestring directory gi-gio gi-glib gi-gtk
11− gi-javascriptcore gi-webkit2 haskell-gi-base jsaddle text unix
12− webkit2gtk3-javascriptcore
13− ];
14− description = "Interface for JavaScript that works with GHCJS and GHC";
15− license = stdenv.lib.licenses.mit;
16−}
jsaddle-webkit2gtk/jsaddle-webkit2gtk.cabal+4−4
@@ -1,5 +1,5 @@
11name: jsaddle-webkit2gtk
2−version: 0.9.4.0
2+version: 0.9.8.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -26,7 +26,7 @@ library
2626 base <5
2727 if !impl(ghcjs -any)
2828 build-depends:
29− aeson >=0.8.0.2 && <1.3,
29+ aeson >=0.8.0.2 && <1.6,
3030 base <5,
3131 bytestring >=0.10.6.0 && <0.11,
3232 directory >=1.0.0.2 && <1.4,
@@ -35,8 +35,8 @@ library
3535 gi-gtk >=3.0.17 && <3.1,
3636 gi-webkit2 >=4.0.14 && <4.1,
3737 gi-javascriptcore >=4.0.14 && <4.1,
38− haskell-gi-base >=0.20 && <0.22,
39− haskell-gi-overloading ==0.0.*,
38+ haskell-gi-base >=0.20 && <0.26,
39+ haskell-gi-overloading >=0.0 && < 2.0,
4040 jsaddle >=0.9.4.0 && <0.10,
4141 text >=1.2.1.3 && <1.3,
4242 webkit2gtk3-javascriptcore >=0.14.0.0 && <0.15
jsaddle-webkit2gtk/src/Language/Javascript/JSaddle/WebKitGTK.hs+16−37
@@ -43,29 +43,24 @@ import Data.Monoid ((<>))
4343import Data.ByteString.Lazy (toStrict, fromStrict)
4444import qualified Data.ByteString.Lazy as LB (ByteString)
4545import Data.Aeson (encode, decode)
46−import Data.Text (Text)
4746import qualified Data.Text as T (pack)
48−import Data.Text.Foreign (fromPtr)
4947import Data.Text.Encoding (decodeUtf8, encodeUtf8)
5048
51−import Foreign.Ptr (castPtr, nullPtr)
52−
5349import Data.GI.Base.BasicTypes (GObject)
5450import Data.GI.Base.Signals
5551 (connectSignalFunPtr, SignalConnectMode(..), SignalConnectMode,
5652 SignalHandlerId)
57−import Data.GI.Base.ManagedPtr (withManagedPtr)
5853
5954import GI.GLib (timeoutAdd, idleAdd, pattern PRIORITY_HIGH, pattern PRIORITY_DEFAULT)
6055import qualified GI.Gtk as Gtk (main, init)
6156import GI.Gtk
6257 (windowSetPosition, windowSetDefaultSize, windowNew,
63− scrolledWindowNew, noAdjustment, containerAdd,
58+ scrolledWindowNew, Adjustment, containerAdd,
6459 WindowType(..), WindowPosition(..), widgetDestroy,
6560 widgetGetToplevel, widgetShowAll, onWidgetDestroy,
6661 mainQuit)
67−import GI.Gio (noCancellable)
68−import GI.JavaScriptCore (Value(..), GlobalContext(..))
62+import GI.Gio (Cancellable)
63+import GI.JavaScriptCore (valueToString)
6964import GI.WebKit2
7065 (scriptDialogPromptSetText, scriptDialogPromptGetDefaultText,
7166 scriptDialogGetMessage, scriptDialogGetDialogType,
@@ -74,7 +69,7 @@ import GI.WebKit2
7469 setSettingsEnableJavascript, webViewNewWithUserContentManager,
7570 userContentManagerNew,
7671 userContentManagerRegisterScriptMessageHandler,
77− javascriptResultGetValue, javascriptResultGetGlobalContext,
72+ javascriptResultGetJsValue,
7873 webViewGetUserContentManager,
7974 mk_UserContentManagerScriptMessageReceivedCallback,
8075 wrap_UserContentManagerScriptMessageReceivedCallback,
@@ -83,17 +78,16 @@ import GI.WebKit2
8378 onWebViewLoadChanged, setSettingsEnableDeveloperExtras,
8479 webViewSetSettings, webViewGetSettings, ScriptDialogType(..))
8580
86−import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSBase
87− (JSValueRef, JSContextRef)
88−import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSStringRef
89− (jsstringgetcharactersptr, jsstringgetlength, jsstringrelease)
90−import Graphics.UI.Gtk.WebKit.JavaScriptCore.JSValueRef
91− (jsvaluetostringcopy)
92−
9381import Language.Javascript.JSaddle (JSM, Results, Batch)
9482import Language.Javascript.JSaddle.Run (runJavaScript)
9583import Language.Javascript.JSaddle.Run.Files (initState, runBatch, ghcjsHelpers)
9684
85+noAdjustment :: Maybe Adjustment
86+noAdjustment = Nothing
87+
88+noCancellable :: Maybe Cancellable
89+noCancellable = Nothing
90+
9791quitWebView :: WebView -> IO ()
9892quitWebView wv = postGUIAsync $ do w <- widgetGetToplevel wv --TODO: Shouldn't this be postGUISync?
9993 widgetDestroy w
@@ -109,21 +103,6 @@ postGUIAsync :: IO () -> IO ()
109103postGUIAsync action =
110104 void . idleAdd PRIORITY_DEFAULT $ action >> return False
111105
112−withJSContextRef :: GlobalContext -> (JSContextRef -> IO a) -> IO a
113−withJSContextRef (GlobalContext ctx) f = withManagedPtr ctx (f . castPtr)
114−
115−withJSValueRef :: Value -> (JSValueRef -> IO a) -> IO a
116−withJSValueRef (Value ptr) f = withManagedPtr ptr (f . castPtr)
117−
118−valueToText :: JSContextRef -> JSValueRef -> IO Text
119−valueToText ctxRef s = do
120− jsstring <- jsvaluetostringcopy ctxRef s nullPtr
121− l <- jsstringgetlength jsstring
122− p <- jsstringgetcharactersptr jsstring
123− result <- fromPtr (castPtr p) (fromIntegral l)
124− jsstringrelease jsstring
125− return result
126−
127106run :: JSM () -> IO ()
128107run main = do
129108 _ <- Gtk.init Nothing
@@ -148,7 +127,7 @@ run main = do
148127 void . onWebViewLoadChanged webView $ \case
149128 LoadEventFinished -> runInWebView main webView
150129 _ -> return ()
151− webViewLoadHtml webView "" . Just $ "file://" <> T.pack pwd <> "/"
130+ webViewLoadHtml webView "" . Just $ "file://" <> T.pack pwd <> "/index.html"
152131 installQuitHandler webView
153132 Gtk.main
154133
@@ -172,16 +151,16 @@ connectUserContentManagerScriptMessageReceived obj cb after = liftIO $ do
172151 let cb' = wrap_UserContentManagerScriptMessageReceivedCallback cb
173152 cb'' <- mk_UserContentManagerScriptMessageReceivedCallback cb'
174153 connectSignalFunPtr obj "script-message-received::jsaddle" cb'' after
154+#if MIN_VERSION_haskell_gi_base(0,23,0)
155+ Nothing
156+#endif
175157
176158addJSaddleHandler :: WebView -> (Results -> IO ()) -> (Results -> IO Batch) -> IO ()
177159addJSaddleHandler webView processResult syncResults = do
178160 manager <- webViewGetUserContentManager webView
179161 _ <- onUserContentManagerScriptMessageReceived manager $ \result -> do
180− ctx <- javascriptResultGetGlobalContext result
181− arg <- javascriptResultGetValue result
182− bs <- withJSContextRef ctx $ \ctxRef ->
183− withJSValueRef arg $ \argRef ->
184− encodeUtf8 <$> valueToText ctxRef argRef
162+ arg <- javascriptResultGetJsValue result
163+ bs <- encodeUtf8 <$> valueToString arg
185164 mapM_ processResult (decode (fromStrict bs))
186165 _ <- onWebViewScriptDialog webView $ \dialog ->
187166 scriptDialogGetDialogType dialog >>= \case
jsaddle-webkitgtk/default.nixdeleted−16
@@ -1,16 +0,0 @@
1−{ mkDerivation, aeson, base, bytestring, directory, gi-glib, gi-gtk
2−, gi-javascriptcore, gi-webkit, haskell-gi-base, jsaddle, stdenv
3−, text, unix, webkitgtk3-javascriptcore
4−}:
5−mkDerivation {
6− pname = "jsaddle-webkitgtk";
7− version = "0.9.0.0";
8− src = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [ ".git" "dist" ])) ./.;
9− libraryHaskellDepends = [
10− aeson base bytestring directory gi-glib gi-gtk gi-javascriptcore
11− gi-webkit haskell-gi-base jsaddle text unix
12− webkitgtk3-javascriptcore
13− ];
14− description = "Interface for JavaScript that works with GHCJS and GHC";
15− license = stdenv.lib.licenses.mit;
16−}
jsaddle-wkwebview/cbits-cocoa/AppDelegate.m+57−2
@@ -2,6 +2,9 @@
22#import <Cocoa/Cocoa.h>
33#import <WebKit/WebKit.h>
44
5+extern void callIO(HsStablePtr);
6+extern bool callWithCStringReturningBool(const char * _Nonnull, HsStablePtr);
7+extern void callWithCString(const char * _Nonnull, HsStablePtr);
58extern void callWithWebView(WKWebView *, HsStablePtr);
69
710@interface AppDelegate : NSObject <NSApplicationDelegate>
@@ -11,6 +14,12 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
1114- (instancetype)initApp:(HsStablePtr)handler progName:(NSString *)progName;
1215@end
1316
17+HsStablePtr global_willFinishLaunchingWithOptions = 0;
18+HsStablePtr global_didFinishLaunchingWithOptions = 0;
19+HsStablePtr global_applicationUniversalLink = 0;
20+HsStablePtr global_applicationOpenFile = 0;
21+uint64_t global_developerExtrasEnabled = 1;
22+
1423@implementation AppDelegate
1524
1625- (void)applicationWillTerminate:(NSNotification *)aNotification {
@@ -31,18 +40,39 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
3140 return self;
3241}
3342
43+-(BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
44+ bool b = callWithCStringReturningBool([filename UTF8String], global_applicationOpenFile);
45+ return b ? YES : NO;
46+}
47+
3448-(void)applicationWillFinishLaunching:(NSNotification *)notification {
3549 WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
36− [theConfiguration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
50+ if (global_developerExtrasEnabled) {
51+ [theConfiguration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
52+ }
3753 WKWebView *webView = [[WKWebView alloc] initWithFrame: [_window.contentView frame] configuration:theConfiguration];
3854 [_window setContentView:webView];
3955 callWithWebView(webView, _handler);
56+ callIO(global_willFinishLaunchingWithOptions);
57+ // Listen to URL events
58+ NSAppleEventManager *manager = [NSAppleEventManager sharedAppleEventManager];
59+ [manager
60+ setEventHandler:self
61+ andSelector:@selector(applicationUniversalLink:)
62+ forEventClass:kInternetEventClass
63+ andEventID:kAEGetURL];
64+}
65+
66+- (void)applicationUniversalLink:(NSAppleEventDescriptor *)event {
67+ NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
68+ callWithCString([url UTF8String], global_applicationUniversalLink);
4069}
4170
4271-(void)applicationDidFinishLaunching:(NSNotification *)notification {
4372 [_window orderFrontRegardless];
4473 [_window center];
4574 [NSApp activateIgnoringOtherApps:YES];
75+ callIO(global_didFinishLaunchingWithOptions);
4676}
4777
4878-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app {
@@ -51,8 +81,33 @@ extern void callWithWebView(WKWebView *, HsStablePtr);
5181
5282@end
5383
54−void runInWKWebView(HsStablePtr handler, const char * _Nonnull progName) {
84+void runInWKWebView(HsStablePtr handler,
85+ const char * _Nonnull progName,
86+ HsStablePtr hs_willFinishLaunchingWithOptions,
87+ HsStablePtr hs_didFinishLaunchingWithOptions,
88+ HsStablePtr hs_applicationDidBecomeActive,
89+ HsStablePtr hs_applicationWillResignActive,
90+ HsStablePtr hs_applicationDidEnterBackground,
91+ HsStablePtr hs_applicationWillEnterForeground,
92+ HsStablePtr hs_applicationWillTerminate,
93+ HsStablePtr hs_applicationSignificantTimeChange,
94+ HsStablePtr hs_applicationUniversalLink,
95+ const uint64_t hs_requestAuthorizationWithOptions,
96+ const uint64_t hs_requestAuthorizationOptionBadge,
97+ const uint64_t hs_requestAuthorizationOptionSound,
98+ const uint64_t hs_requestAuthorizationOptionAlert,
99+ const uint64_t hs_requestAuthorizationOptionCarPlay,
100+ const uint64_t hs_registerForRemoteNotifications,
101+ HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
102+ HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError,
103+ const uint64_t hs_developerExtrasEnabled,
104+ HsStablePtr hs_applicationOpenFile) {
55105 @autoreleasepool {
106+ global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
107+ global_didFinishLaunchingWithOptions = hs_didFinishLaunchingWithOptions;
108+ global_applicationUniversalLink = hs_applicationUniversalLink;
109+ global_applicationOpenFile = hs_applicationOpenFile;
110+ global_developerExtrasEnabled = hs_developerExtrasEnabled;
56111 NSApplication *application = [NSApplication sharedApplication];
57112 AppDelegate *appDelegate = [[AppDelegate alloc] initApp:handler progName:[NSString stringWithCString:progName encoding:NSUTF8StringEncoding]];
58113 [application setDelegate:appDelegate];
jsaddle-wkwebview/cbits-uikit/AppDelegate.m+2−1
@@ -170,7 +170,8 @@ void runInWKWebView(HsStablePtr handler,
170170 const uint64_t hs_requestAuthorizationOptionCarPlay,
171171 const uint64_t hs_registerForRemoteNotifications,
172172 HsStablePtr hs_didRegisterForRemoteNotificationsWithDeviceToken,
173− HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError) {
173+ HsStablePtr hs_didFailToRegisterForRemoteNotificationsWithError,
174+ const uint64_t hs_developerExtrasEnabled) {
174175 @autoreleasepool {
175176 globalHandler = handler;
176177 global_willFinishLaunchingWithOptions = hs_willFinishLaunchingWithOptions;
jsaddle-wkwebview/cbits/WKWebView.m+8−10
@@ -61,9 +61,9 @@ extern void jsaddleSyncResult(HsStablePtr, JSaddleHandler *, const char * _Nonnu
6161
6262 if(!navigationAction.targetFrame && ![url.scheme isEqualToString:@"file"]) {
6363 if(openApp(url))
64− decisionHandler(WKNavigationActionPolicyCancel);
65− else
66− decisionHandler(WKNavigationActionPolicyAllow);
64+ decisionHandler(WKNavigationActionPolicyCancel);
65+ else
66+ decisionHandler(WKNavigationActionPolicyAllow);
6767 }
6868 else
6969 decisionHandler(WKNavigationActionPolicyAllow);
@@ -95,10 +95,11 @@ void completeSync(JSaddleHandler *handler, const char * _Nonnull s) {
9595 handler.completionHandler = NULL;
9696}
9797
98−void loadHTMLString(WKWebView *webView, const char * _Nonnull html) {
99− NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
100− dispatch_async(dispatch_get_main_queue(), ^{
101− [webView loadHTMLString:htmlString baseURL:NULL];
98+void loadHTMLStringWithBaseURL(WKWebView *webView, const char * _Nonnull html, const char * _Nonnull url) {
99+ NSString *htmlString = [NSString stringWithCString:html encoding:NSUTF8StringEncoding];
100+ NSURL *baseURL = [NSURL URLWithString:[[NSString alloc] initWithCString:url encoding:NSUTF8StringEncoding]];
101+ dispatch_async(dispatch_get_main_queue(), ^{
102+ [webView loadHTMLString:htmlString baseURL:baseURL];
102103 });
103104}
104105
@@ -121,6 +122,3 @@ void loadBundleFile(WKWebView *webView, const char * _Nonnull file, const char *
121122 }
122123 }
123124}
124−
125−
126−
jsaddle-wkwebview/default.nix+14−21
@@ -1,21 +1,14 @@
1−{ mkDerivation, stdenv, aeson, base, bytestring, jsaddle, data-default
2−, buildPackages, hostPlatform
3−}:
4−
5−mkDerivation {
6− pname = "jsaddle-wkwebview";
7− version = "0.9.4.0";
8− src = ./.;
9− libraryHaskellDepends = [ aeson base bytestring jsaddle data-default ];
10− libraryDarwinFrameworkDepends = with buildPackages; if hostPlatform.useiOSCross or false then [
11− (assert osx_sdk != null; osx_sdk)
12− ] else [
13− darwin.libobjc
14− darwin.apple_sdk.libs.xpc
15− darwin.apple_sdk.frameworks.Foundation
16− darwin.apple_sdk.frameworks.Cocoa
17− darwin.apple_sdk.frameworks.WebKit
18− ];
19− description = "Interface for JavaScript that works with GHCJS and GHC";
20− license = stdenv.lib.licenses.mit;
21−}
1+{ filterSource ? (x: x) }:
2+self: super:
3+let
4+ pkgs = self.callPackage ({pkgs}: pkgs) {};
5+ inherit (pkgs) lib;
6+in
7+with pkgs.haskell.lib;
8+overrideCabal (self.callCabal2nix "jsaddle-wkwebview" (filterSource ./.) {}) (drv: {
9+ libraryFrameworkDepends = (drv.libraryFrameworkDepends or []) ++
10+ (if pkgs.stdenv.hostPlatform.useiOSPrebuilt then [
11+ "${pkgs.buildPackages.darwin.xcode}/Contents/Developer/Platforms/${pkgs.stdenv.hostPlatform.xcodePlatform}.platform/Developer/SDKs/${pkgs.stdenv.hostPlatform.xcodePlatform}.sdk/System"
12+ ] else (with pkgs.buildPackages.darwin.apple_sdk.frameworks; [ Cocoa WebKit ]));
13+ buildDepends = lib.optional (!pkgs.stdenv.hostPlatform.useiOSPrebuilt) [ pkgs.buildPackages.darwin.cf-private ];
14+})
jsaddle-wkwebview/jsaddle-wkwebview.cabal+18−11
@@ -1,5 +1,5 @@
11name: jsaddle-wkwebview
2−version: 0.9.4.0
2+version: 0.9.8.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -25,24 +25,31 @@ source-repository head
2525library
2626 exposed-modules:
2727 Language.Javascript.JSaddle.WKWebView
28− Language.Javascript.JSaddle.WKWebView.Internal
2928 build-depends:
30− aeson >=0.8.0.2 && <1.3,
31− base <5,
32− bytestring >=0.10.6.0 && <0.11,
33− jsaddle >= 0.9.4.0 && <0.10,
34− data-default,
35− containers
29+ base <5
3630 default-language: Haskell2010
3731 hs-source-dirs: src
38− frameworks: Foundation, WebKit
3932 ghc-options: -ferror-spans -Wall
40− c-sources:
41− cbits/WKWebView.m
33+ if os(linux)
34+ buildable: False
4235 if impl(ghcjs)
4336 hs-source-dirs: src-ghcjs
4437 else
38+ frameworks: Foundation, WebKit
39+ build-depends:
40+ aeson >=0.8.0.2 && <1.6,
41+ bytestring >=0.10.6.0 && <0.11,
42+ directory,
43+ jsaddle >= 0.9.4.0 && <0.10,
44+ text,
45+ data-default,
46+ containers
47+ exposed-modules:
48+ Language.Javascript.JSaddle.WKWebView.Internal
4549 hs-source-dirs: src-ghc
50+ c-sources:
51+ cbits/WKWebView.m
52+ cc-options: -Wno-everything
4653 if os(ios)
4754 frameworks: UIKit, UserNotifications
4855 if flag(include-app-delegate)
jsaddle-wkwebview/src-ghc/Language/Javascript/JSaddle/WKWebView.hs+55−18
@@ -1,9 +1,12 @@
11module Language.Javascript.JSaddle.WKWebView
22 ( jsaddleMain
3+ , jsaddleMainHTMLWithBaseURL
34 , jsaddleMainFile
45 , WKWebView(..)
56 , run
7+ , run'
68 , runWithAppConfig
9+ , runHTMLWithBaseURL
710 , runFile
811 , mainBundleResourcePath
912 , ApplicationState (..)
@@ -18,10 +21,11 @@ import Data.Set (Set)
1821import qualified Data.Set as Set
1922import Data.Word
2023import Foreign.Marshal.Utils (fromBool)
21−import Language.Javascript.JSaddle.WKWebView.Internal (jsaddleMain, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
24+import Language.Javascript.JSaddle.WKWebView.Internal
25+ (jsaddleMain, jsaddleMainHTMLWithBaseURL, jsaddleMainFile, WKWebView(..), mainBundleResourcePath)
2226import System.Environment (getProgName)
2327import Foreign.C.String (CString, withCString)
24−import Foreign.C.Types (CInt)
28+import Foreign.C.Types (CBool, CInt)
2529import Foreign.StablePtr (StablePtr, newStablePtr)
2630import Language.Javascript.JSaddle (JSM)
2731
@@ -46,6 +50,8 @@ foreign import ccall runInWKWebView
4650 -> Word64 -- registerForRemoteNotifications
4751 -> StablePtr (CString -> IO ()) -- didRegisterForRemoteNotificationsWithDeviceToken
4852 -> StablePtr (CString -> IO ()) -- didFailToRegisterForRemoteNotificationsWithError
53+ -> Word64 -- developerExtrasEnabled
54+ -> StablePtr (CString -> IO CBool) -- applicationOpenFile
4955 -> IO ()
5056
5157data AppDelegateConfig = AppDelegateConfig
@@ -58,8 +64,25 @@ data AppDelegateConfig = AppDelegateConfig
5864 , _appDelegateConfig_applicationWillTerminate :: IO ()
5965 , _appDelegateConfig_applicationSignificantTimeChange :: IO ()
6066 , _appDelegateConfig_applicationUniversalLink :: CString -> IO ()
67+ -- ^ Called when the app is launched from a URL. 'CString' contains the URL.
68+ --
69+ -- The Core Foundation plist key CFBundleURLTypes controls which URL schemes
70+ -- the app should respond to.
6171 , _appDelegateConfig_applicationDidReceiveRemoteNotification :: ApplicationState -> CString -> IO ()
72+ -- ^ Called when the application receives a remote notification.
73+ -- 'ApplicationState' is the current state of the app.
74+ -- The 'CString' JSON-encoded user info contained in the notification.
75+ -- For more information see https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623117-application?language=objc
6276 , _appDelegateConfig_appDelegateNotificationConfig :: AppDelegateNotificationConfig
77+ , _appDelegateConfig_developerExtrasEnabled :: Bool
78+ -- ^ Allow devtools in the app. Defaults to 'True'
79+ , _appDelegateConfig_applicationOpenFile :: CString -> IO Bool
80+ -- ^ Called when the app is launched by opening a file. 'CString' contains
81+ -- the file path. Return value is 'True' if the file was successfully
82+ -- opened. Defaults to ignoring the file and returning 'False'.
83+ --
84+ -- The Core Foundation plist key CFBundleDocumentTypes controls which file
85+ -- types should be associated with the app.
6386 }
6487
6588instance Default AppDelegateConfig where
@@ -75,6 +98,8 @@ instance Default AppDelegateConfig where
7598 , _appDelegateConfig_applicationUniversalLink = \_ -> return ()
7699 , _appDelegateConfig_applicationDidReceiveRemoteNotification = \_ _ -> return ()
77100 , _appDelegateConfig_appDelegateNotificationConfig = def
101+ , _appDelegateConfig_developerExtrasEnabled = True
102+ , _appDelegateConfig_applicationOpenFile = \_ -> return False
78103 }
79104
80105data AuthorizationOption = AuthorizationOption_Badge
@@ -107,29 +132,38 @@ instance Default AppDelegateNotificationConfig where
107132
108133-- | Run JSaddle in a WKWebView
109134run :: JSM () -> IO ()
110−run = run' Nothing def
135+run f = run' def (jsaddleMain f)
111136
112137-- | Run JSaddle in a WKWebView
113138runWithAppConfig :: AppDelegateConfig -> JSM () -> IO ()
114−runWithAppConfig = run' Nothing
139+runWithAppConfig cfg = run' cfg . jsaddleMain
115140
116141-- | Run JSaddle in a WKWebView first loading the specified file
117142-- from the mainBundle (relative to the resourcePath).
118−runFile :: ByteString -- ^ The file to navigate to.
119− -> ByteString -- ^ The path to allow read access to.
120− -> AppDelegateConfig
121− -> JSM ()
122− -> IO ()
123−runFile url allowing = run' $ Just (url, allowing)
124−
125−run' :: Maybe (ByteString, ByteString)
126− -> AppDelegateConfig
127− -> JSM ()
143+runFile
144+ :: ByteString -- ^ The file to navigate to.
145+ -> ByteString -- ^ The path to allow read access to.
146+ -> AppDelegateConfig
147+ -> JSM ()
148+ -> IO ()
149+runFile url allowing cfg = run' cfg . jsaddleMainFile url allowing
150+
151+-- | Run JSaddle in a WKWebView first loading the specified html
152+-- as though it came from the specified URL
153+-- (calls WKWebKit function loadHTMLString).
154+runHTMLWithBaseURL
155+ :: ByteString -- ^ HTML to load.
156+ -> ByteString -- ^ pretend it came from this URL.
157+ -> AppDelegateConfig
158+ -> JSM ()
159+ -> IO ()
160+runHTMLWithBaseURL url allowing cfg = run' cfg . jsaddleMainHTMLWithBaseURL url allowing
161+
162+run' :: AppDelegateConfig
163+ -> (WKWebView -> IO ())
128164 -> IO ()
129−run' mUrl cfg f = do
130− handler <- case mUrl of
131− Just (url, allowing) -> newStablePtr (jsaddleMainFile url allowing f)
132− Nothing -> newStablePtr (jsaddleMain f)
165+run' cfg main = do
166+ handler <- newStablePtr main
133167 progName <- getProgName
134168
135169 -- AppDelegate callbacks
@@ -149,6 +183,7 @@ run' mUrl cfg f = do
149183 2 -> ApplicationState_Background
150184 _ -> ApplicationState_Unknown n
151185 _appDelegateConfig_applicationDidReceiveRemoteNotification cfg appState s
186+ applicationOpenFile <- newStablePtr $ fmap fromBool . _appDelegateConfig_applicationOpenFile cfg
152187
153188 -- AppDelegate notification configuration
154189 let ncfg = _appDelegateConfig_appDelegateNotificationConfig cfg
@@ -180,3 +215,5 @@ run' mUrl cfg f = do
180215 (fromBool registerForRemoteNotifications)
181216 didRegisterForRemoteNotificationsWithDeviceToken
182217 didFailToRegisterForRemoteNotificationsWithError
218+ (fromBool $ _appDelegateConfig_developerExtrasEnabled cfg)
219+ applicationOpenFile
jsaddle-wkwebview/src/Language/Javascript/JSaddle/WKWebView/Internal.hs+29−5
@@ -1,6 +1,7 @@
11{-# LANGUAGE ForeignFunctionInterface, OverloadedStrings #-}
22module Language.Javascript.JSaddle.WKWebView.Internal
33 ( jsaddleMain
4+ , jsaddleMainHTMLWithBaseURL
45 , jsaddleMainFile
56 , WKWebView(..)
67 , mainBundleResourcePath
@@ -15,9 +16,11 @@ import Data.ByteString (useAsCString, packCString)
1516import qualified Data.ByteString as BS (ByteString)
1617import Data.ByteString.Lazy (ByteString, toStrict, fromStrict)
1718import Data.Aeson (encode, decode)
19+import qualified Data.Text as T (pack)
20+import Data.Text.Encoding (encodeUtf8)
1821
1922import Foreign.C.String (CString)
20−import Foreign.C.Types (CInt(..))
23+import Foreign.C.Types (CBool(..), CInt(..))
2124import Foreign.Ptr (Ptr, nullPtr)
2225import Foreign.StablePtr (StablePtr, newStablePtr, deRefStablePtr)
2326
@@ -25,6 +28,8 @@ import Language.Javascript.JSaddle (Results, Batch, JSM)
2528import Language.Javascript.JSaddle.Run (runJavaScript)
2629import Language.Javascript.JSaddle.Run.Files (initState, runBatch, ghcjsHelpers)
2730
31+import System.Directory (getCurrentDirectory)
32+
2833newtype WKWebView = WKWebView (Ptr WKWebView)
2934newtype JSaddleHandler = JSaddleHandler (Ptr JSaddleHandler)
3035
@@ -34,9 +39,10 @@ foreign export ccall jsaddleSyncResult :: StablePtr (Results -> IO Batch) -> JSa
3439foreign export ccall callWithWebView :: WKWebView -> StablePtr (WKWebView -> IO ()) -> IO ()
3540foreign export ccall callWithCIntCString :: CInt -> CString -> StablePtr (CInt -> CString -> IO ()) -> IO ()
3641foreign export ccall callWithCString :: CString -> StablePtr (CString -> IO ()) -> IO ()
42+foreign export ccall callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
3743foreign export ccall callIO :: StablePtr (IO ()) -> IO ()
3844foreign import ccall addJSaddleHandler :: WKWebView -> StablePtr (IO ()) -> StablePtr (Results -> IO ()) -> StablePtr (Results -> IO Batch) -> IO ()
39−foreign import ccall loadHTMLString :: WKWebView -> CString -> IO ()
45+foreign import ccall loadHTMLStringWithBaseURL :: WKWebView -> CString -> CString -> IO ()
4046foreign import ccall loadBundleFile :: WKWebView -> CString -> CString -> IO ()
4147foreign import ccall evaluateJavaScript :: WKWebView -> CString -> IO ()
4248foreign import ccall completeSync :: JSaddleHandler -> CString -> IO ()
@@ -44,9 +50,23 @@ foreign import ccall mainBundleResourcePathC :: IO CString
4450
4551-- | Run JSaddle in WKWebView
4652jsaddleMain :: JSM () -> WKWebView -> IO ()
47−jsaddleMain f webView =
53+jsaddleMain f webView = do
54+ pwd <- getCurrentDirectory
55+ let baseURL = encodeUtf8 $ "file://" <> T.pack pwd <> "/index.html"
56+ jsaddleMainHTMLWithBaseURL (toStrict indexHtml) baseURL f webView
57+
58+-- | Run JSaddle in WKWebView with initial html and base url
59+jsaddleMainHTMLWithBaseURL
60+ :: BS.ByteString -- ^ HTML
61+ -> BS.ByteString -- ^ Base URL
62+ -> JSM ()
63+ -> WKWebView
64+ -> IO ()
65+jsaddleMainHTMLWithBaseURL initialHTML baseURL f webView =
4866 jsaddleMain' f webView $
49− useAsCString (toStrict indexHtml) $ loadHTMLString webView
67+ useAsCString baseURL $ \url ->
68+ useAsCString initialHTML $ \html ->
69+ loadHTMLStringWithBaseURL webView html url
5070
5171-- | Run JSaddle in a WKWebView first loading the specified file
5272-- from the mainBundle (relative to the resourcePath).
@@ -114,6 +134,11 @@ callWithCIntCString n s fptr = do
114134 f <- deRefStablePtr fptr
115135 f n s
116136
137+callWithCStringReturningBool :: CString -> StablePtr (CString -> IO CBool) -> IO CBool
138+callWithCStringReturningBool c fptr = do
139+ f <- deRefStablePtr fptr
140+ f c
141+
117142callIO :: StablePtr (IO ()) -> IO ()
118143callIO ptr = do
119144 f <- deRefStablePtr ptr
@@ -147,4 +172,3 @@ mainBundleResourcePath = do
147172 if bs == nullPtr
148173 then return Nothing
149174 else Just <$> packCString bs
150−
jsaddle/default.nixdeleted−23
@@ -1,23 +0,0 @@
1−{ mkDerivation, aeson, attoparsec, base, base64-bytestring
2−, bytestring, containers, deepseq, filepath, ghc-prim, http-types
3−, lens, primitive, process, random, ref-tf, scientific, stdenv, stm
4−, text, time, transformers, unordered-containers, unliftio-core
5−, vector, exceptions, ghcjs-base, ghcjs-prim, ghc
6−}:
7−mkDerivation {
8− pname = "jsaddle";
9− version = "0.9.4.0";
10− src = builtins.filterSource (path: type: !(builtins.elem (baseNameOf path) [ ".git" "dist" ])) ./.;
11− libraryHaskellDepends = [
12− aeson base base64-bytestring bytestring exceptions lens primitive
13− text transformers
14− ] ++ (if ghc.isGhcjs or false then [
15− ghcjs-base ghcjs-prim
16− ] else [
17− attoparsec containers deepseq filepath ghc-prim http-types process
18− random ref-tf scientific stm time unordered-containers vector
19− unliftio-core
20− ]);
21− description = "Interface for JavaScript that works with GHCJS and GHC";
22− license = stdenv.lib.licenses.mit;
23−}
jsaddle/jsaddle.cabal+15−16
@@ -1,5 +1,5 @@
11name: jsaddle
2−version: 0.9.4.0
2+version: 0.9.8.0
33cabal-version: >=1.10
44build-type: Simple
55license: MIT
@@ -10,7 +10,7 @@ description:
1010 This package provides an EDSL for calling JavaScript that
1111 can be used both from GHCJS and GHC. When using GHC
1212 the application is run using Warp and WebSockets to
13− drive a small JavaScipt helper.
13+ drive a small JavaScript helper.
1414category: Web, Javascript
1515author: Hamish Mackenzie
1616
@@ -36,20 +36,20 @@ library
3636 else
3737 build-depends:
3838 attoparsec >=0.11 && <0.14,
39− containers >=0.5.6.2 && <0.6,
39+ containers >=0.5.6.2 && <0.7,
4040 deepseq >=1.3 && < 1.5,
4141 filepath >=1.4.0.0 && <1.5,
4242 ghc-prim,
43− http-types >=0.8.6 && <0.12,
43+ http-types >=0.8.6 && <0.13,
4444 process >=1.2.3.0 && <1.7,
45− random >= 1.1 && < 1.2,
45+ random >= 1.1 && < 1.3,
4646 ref-tf >=0.4.0.1 && <0.5,
4747 scientific >=0.3 && <0.4,
48− stm >=2.4.4 && <2.5,
49− time >=1.5.0.1 && <1.9,
48+ stm >=2.4.4 && <2.6,
49+ time >=1.5.0.1 && <1.11,
5050 unordered-containers >=0.2 && <0.3,
5151 vector >=0.10 && <0.13,
52− unliftio-core >=0.1 && < 0.2
52+ unliftio-core >=0.1 && < 0.3
5353 exposed-modules:
5454 Data.JSString
5555 Data.JSString.Internal
@@ -107,13 +107,14 @@ library
107107 Language.Javascript.JSaddle.Value
108108 Language.Javascript.JSaddle.Types
109109 build-depends:
110− aeson >=0.8.0.2 && <1.3,
111− base <5,
112− base64-bytestring >=1.0.0.1 && <1.1,
110+ aeson >=0.11.3.0 && <1.6,
111+ base >=4.9 && <5,
112+ base-compat >=0.9.0 && <0.12,
113+ base64-bytestring >=1.0.0.1 && <1.2,
113114 bytestring >=0.10.6.0 && <0.11,
114− exceptions >=0.8 && <0.9,
115− lens >=3.8.5 && <4.16,
116− primitive >=0.6.1.0 && <0.7,
115+ exceptions >=0.8 && <0.11,
116+ lens >=3.8.5 && <5.1,
117+ primitive >=0.6.1.0 && <0.8,
117118 text >=1.2.1.3 && <1.3,
118119 transformers >=0.4.2.0 && <0.6
119120 default-language: Haskell2010
@@ -123,5 +124,3 @@ library
123124 cpp-options: -DCHECK_UNCHECKED
124125 if flag(call-stacks) || flag(check-unchecked)
125126 cpp-options: -DJSADDLE_HAS_CALL_STACK
126−
127−
jsaddle/src-ghc/Data/JSString/Internal/Type.hs+2−1
@@ -38,10 +38,11 @@ import Data.Aeson (ToJSON(..), FromJSON(..))
3838import Data.Data (Data)
3939-- import Data.Text.Internal.Unsafe.Char (ord)
4040import Data.Typeable (Typeable)
41+import Data.Semigroup (Semigroup)
4142import GHC.Exts (Char(..), ord#, andI#, (/=#), isTrue#)
4243
4344-- | A wrapper around a JavaScript string
44−newtype JSString = JSString Text deriving(Show, Read, IsString, Monoid, Ord, Eq, Data, ToJSON, FromJSON, Typeable)
45+newtype JSString = JSString Text deriving(Show, Read, IsString, Semigroup, Monoid, Ord, Eq, Data, ToJSON, FromJSON, Typeable)
4546
4647instance NFData JSString where rnf !_ = ()
4748
jsaddle/src-ghc/JavaScript/Array.hs+2−2
@@ -48,7 +48,7 @@ import Language.Javascript.JSaddle.Types
4848 (JSM, JSVal, SomeJSArray(..), JSArray,
4949 MutableJSArray, GHCJSPure(..))
5050import Control.Lens.Operators ((^.))
51−import Language.Javascript.JSaddle.Object (js2, js0, (##), js1, js)
51+import Language.Javascript.JSaddle.Object (js2, js0, (<##), js1, js)
5252import Language.Javascript.JSaddle.Value (valToNumber)
5353import JavaScript.Array.Internal (create, fromList, fromListIO, toList, toListIO, index, read, push)
5454
@@ -69,7 +69,7 @@ append (SomeJSArray x) (SomeJSArray y) = SomeJSArray <$> x ^. js1 "concat" y
6969{-# INLINE append #-}
7070
7171write :: Int -> JSVal -> MutableJSArray -> JSM ()
72−write n e (SomeJSArray x) = void $ (x ## n) e
72+write n e (SomeJSArray x) = void $ (x <## n) e
7373{-# INLINE write #-}
7474
7575pop :: MutableJSArray -> JSM JSVal
jsaddle/src/Language/Javascript/JSaddle.hs+5−5
@@ -9,7 +9,7 @@
99-- | This package provides an EDSL for calling JavaScript that
1010-- can be used both from GHCJS and GHC. When using GHC
1111-- the application is run using Warp and WebSockets to
12−-- drive a small JavaScipt helper.
12+-- drive a small JavaScript helper.
1313-----------------------------------------------------------------------------
1414
1515module Language.Javascript.JSaddle (
@@ -35,10 +35,10 @@ module Language.Javascript.JSaddle (
3535 --
3636 -- Where it makes sense code examples are given in two forms. One
3737 -- that uses 'eval' to run a purely JavaScript version and one that
38− -- uses more of the JSaddle EDSL feature being demonstated.
38+ -- uses more of the JSaddle EDSL feature being demonstrated.
3939
4040 -- * Calling Haskell from JavaScript
41− -- | You can call back into haskell from JavaScript using 'fun' to
41+ -- | You can call back into Haskell from JavaScript using 'fun' to
4242 -- convert a Haskell function in the JSM monad into a javascript
4343 -- value.
4444
@@ -52,8 +52,8 @@ module Language.Javascript.JSaddle (
5252 -- and the server will run the Haskell code. The JSaddle parts will
5353 -- be executed by sending commands back to the browser.
5454
55− -- Although the code JavaScript code is executed in the the strict order
56− -- set out by the EDSL it done asynchonously to the Haskell code.
55+ -- Although the JavaScript code is executed in the strict order
56+ -- set out by the EDSL it is done asynchronously to the Haskell code.
5757 -- This improves the performance by reducing the number of round trips
5858 -- needed between the Haskell and JavaScript code.
5959
jsaddle/src/Language/Javascript/JSaddle/Evaluate.hs+1−1
@@ -50,7 +50,7 @@ eval :: (ToJSString script)
5050 -> JSM JSVal
5151#ifdef ghcjs_HOST_OS
5252eval script = liftIO $ js_eval (toJSString script)
53−foreign import javascript unsafe "$r = eval($1);"
53+foreign import javascript safe "$r = eval($1);"
5454 js_eval :: JSString -> IO JSVal
5555#else
5656eval = evaluateScript . toJSString
jsaddle/src/Language/Javascript/JSaddle/Object.hs+5−6
@@ -526,12 +526,11 @@ array args = do
526526 Object . coerce <$> fromListIO rargs
527527
528528-- | JavaScript's global object
529−global :: Object
530529#ifdef ghcjs_HOST_OS
531−global = js_window
532−foreign import javascript unsafe "$r = window"
533− js_window :: Object
530+foreign import javascript unsafe "$r = globalThis"
531+ global :: Object
534532#else
533+global :: Object
535534global = Object . JSVal . unsafePerformIO $ newIORef 4
536535#endif
537536
@@ -557,7 +556,7 @@ objCallAsFunction :: MakeArgs args
557556objCallAsFunction f this args = do
558557 rargs <- makeArgs args >>= liftIO . Array.fromListIO
559558 liftIO $ js_apply f this rargs
560−foreign import javascript unsafe "$r = $1.apply($2, $3)"
559+foreign import javascript safe "$r = $1.apply($2, $3)"
561560 js_apply :: Object -> Object -> MutableJSArray -> IO JSVal
562561#else
563562objCallAsFunction f this args = do
@@ -577,7 +576,7 @@ objCallAsConstructor :: MakeArgs args
577576objCallAsConstructor f args = do
578577 rargs <- makeArgs args >>= liftIO . Array.fromListIO
579578 liftIO $ js_new f rargs
580−foreign import javascript unsafe "\
579+foreign import javascript safe "\
581580 switch($2.length) {\
582581 case 0 : $r = new $1(); break;\
583582 case 1 : $r = new $1($2[0]); break;\
jsaddle/src/Language/Javascript/JSaddle/Run.hs+17−3
@@ -73,10 +73,23 @@ import Language.Javascript.JSaddle.Types
7373 Object(..), JSValueReceived(..), JSM(..), Batch(..), JSValueForSend(..), syncPoint, syncAfter, sendCommand)
7474import Language.Javascript.JSaddle.Exception (JSException(..))
7575import Control.DeepSeq (force, deepseq)
76+#if MIN_VERSION_base(4,11,0)
77+import GHC.Stats (getRTSStatsEnabled, getRTSStats, RTSStats(..), gcdetails_live_bytes, gc)
78+#else
7679import GHC.Stats (getGCStatsEnabled, getGCStats, GCStats(..))
80+#endif
7781import Data.Foldable (forM_)
7882#endif
7983
84+#ifndef ghcjs_HOST_OS
85+#if MIN_VERSION_base(4,11,0)
86+currentBytesUsed = gcdetails_live_bytes . gc
87+#else
88+getRTSStatsEnabled = getGCStatsEnabled
89+getRTSStats = getGCStats
90+#endif
91+#endif
92+
8093-- | Enable (or disable) JSaddle logging
8194enableLogging :: Bool -> JSM ()
8295#ifdef ghcjs_HOST_OS
@@ -193,8 +206,8 @@ runJavaScript sendBatch entryPoint = do
193206 logInfo s =
194207 readIORef loggingEnabled >>= \case
195208 True -> do
196− currentBytesUsedStr <- getGCStatsEnabled >>= \case
197− True -> show . currentBytesUsed <$> getGCStats
209+ currentBytesUsedStr <- getRTSStatsEnabled >>= \case
210+ True -> show . currentBytesUsed <$> getRTSStats
198211 False -> return "??"
199212 cbCount <- M.size <$> readTVarIO callbacks
200213 putStrLn . s $ "M " <> currentBytesUsedStr <> " CB " <> show cbCount <> " "
@@ -214,10 +227,11 @@ runJavaScript sendBatch entryPoint = do
214227 zipWithM_ putMVar resultMVars results
215228 forM_ callbacksToFree $ \(JSValueReceived val) ->
216229 atomically (modifyTVar' callbacks (M.delete val))
217− (_, Failure callbacksToFree results exception) -> do
230+ (_, Failure callbacksToFree results exception err) -> do
218231 -- The exception will only be rethrown in Haskell if/when one of the
219232 -- missing results (if any) is evaluated.
220233 putStrLn "A JavaScript exception was thrown! (may not reach Haskell code)"
234+ putStrLn err
221235 zipWithM_ putMVar resultMVars $ results <> repeat (ThrowJSValue exception)
222236 forM_ callbacksToFree $ \(JSValueReceived val) ->
223237 atomically (modifyTVar' callbacks (M.delete val))
jsaddle/src/Language/Javascript/JSaddle/Run/Files.hs+7−38
@@ -13,7 +13,6 @@
1313
1414module Language.Javascript.JSaddle.Run.Files (
1515 indexHtml
16− , jsaddleJs
1716 , initState
1817 , runBatch
1918 , ghcjsHelpers
@@ -42,7 +41,7 @@ initState = "\
4241 \ jsaddle_values.set(1, undefined);\n\
4342 \ jsaddle_values.set(2, false);\n\
4443 \ jsaddle_values.set(3, true);\n\
45− \ jsaddle_values.set(4, window);\n\
44+ \ jsaddle_values.set(4, globalThis);\n\
4645 \ var jsaddle_index = 100;\n\
4746 \ var expectedBatch = 1;\n\
4847 \ var lastResults = [0, {\"tag\": \"Success\", \"contents\": [[], []]}];\n\
@@ -94,6 +93,10 @@ runBatch send sendSync = "\
9493 \ var n = d.contents[1];\n\
9594 \ jsaddle_values.set(n, d.contents[0]);\n\
9695 \ break;\n\
96+ \ case \"JSONValueToValue\":\n\
97+ \ var n = d.contents[1];\n\
98+ \ jsaddle_values.set(n, d.contents[0]);\n\
99+ \ break;\n\
97100 \ case \"GetPropertyByName\":\n\
98101 \ var n = d.contents[2];\n\
99102 \ jsaddle_values.set(n, jsaddle_values.get(d.contents[0])[d.contents[1]]);\n\
@@ -312,7 +315,8 @@ runBatch send sendSync = "\
312315 \ catch (err) {\n\
313316 \ var n = ++jsaddle_index;\n\
314317 \ jsaddle_values.set(n, err);\n\
315− \ " <> send "{\"tag\": \"BatchResults\", \"contents\": [batch[2], {\"tag\": \"Failure\", \"contents\": [callbacksToFree, results, n]}]}" <> "\n\
318+ \ console.log(err);\n\
319+ \ " <> send "{\"tag\": \"BatchResults\", \"contents\": [batch[2], {\"tag\": \"Failure\", \"contents\": [callbacksToFree, results, n, String(err)]}]}" <> "\n\
316320 \ }\n\
317321 \ if(inCallback == 1) {\n\
318322 \ while(asyncBatch !== null) {\n\
@@ -333,41 +337,6 @@ runBatch send sendSync = "\
333337 \ runBatch(batch);\n\
334338 \"
335339
336−-- Use this to generate this string for embedding
337−-- sed -e 's|\\|\\\\|g' -e 's|^| \\|' -e 's|$|\\n\\|' -e 's|"|\\"|g' data/jsaddle.js | pbcopy
338−jsaddleJs :: ByteString
339−jsaddleJs = "\
340− \if(typeof global !== \"undefined\") {\n\
341− \ global.window = global;\n\
342− \ global.WebSocket = require('ws');\n\
343− \}\n\
344− \\n\
345− \var connect = function() {\n\
346− \ var wsaddress =\n\
347− \ typeof window.location === \"undefined\"\n\
348− \ ? \"ws://localhost:3709/\"\n\
349− \ : window.location.protocol.replace('http', 'ws')+\"//\"+window.location.hostname+(window.location.port?(\":\"+window.location.port):\"\");\n\
350− \\n\
351− \ var ws = new WebSocket(wsaddress);\n\
352− \\n\
353− \ ws.onopen = function(e) {\n\
354− \ " <> initState <> "\
355− \\n\
356− \ ws.onmessage = function(e) {\n\
357− \ var batch = JSON.parse(e.data);\n\
358− \\n\
359− \ " <> runBatch (\a -> "ws.send(JSON.stringify(" <> a <> "));") Nothing <> "\
360− \ };\n\
361− \ };\n\
362− \ ws.onerror = function() {\n\
363− \ setTimeout(connect, 1000);\n\
364− \ };\n\
365− \}\n\
366− \\n\
367− \ " <> ghcjsHelpers <> "\
368− \connect();\n\
369− \"
370−
371340ghcjsHelpers :: ByteString
372341ghcjsHelpers = "\
373342 \function h$isNumber(o) {\
jsaddle/src/Language/Javascript/JSaddle/Types.hs+10−2
@@ -124,6 +124,7 @@ import Data.Aeson
124124 (defaultOptions, genericToEncoding, ToJSON(..), FromJSON(..), Value)
125125import GHC.Generics (Generic)
126126import Control.Monad.IO.Unlift (MonadUnliftIO)
127+import qualified Control.Monad.Fail as Fail
127128#endif
128129
129130#if MIN_VERSION_base(4,9,0) && defined(CHECK_UNCHECKED)
@@ -165,7 +166,7 @@ data JSContextRef = JSContextRef {
165166type JSM = IO
166167#else
167168newtype JSM a = JSM { unJSM :: ReaderT JSContextRef IO a }
168− deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadThrow, MonadUnliftIO)
169+ deriving (Functor, Applicative, Monad, MonadIO, MonadFix, MonadThrow, MonadUnliftIO, Fail.MonadFail)
169170
170171instance MonadCatch JSM where
171172 t `catch` c = JSM (unJSM (syncAfter t) `catch` \e -> unJSM (c e))
@@ -178,6 +179,13 @@ instance MonadMask JSM where
178179 JSM $ uninterruptibleMask $ \unmask -> unJSM (a $ q unmask)
179180 where q :: (ReaderT JSContextRef IO a -> ReaderT JSContextRef IO a) -> JSM a -> JSM a
180181 q unmask (JSM b) = syncAfter . JSM $ unmask b
182+#if MIN_VERSION_exceptions(0,9,0)
183+ generalBracket acquire release use =
184+ JSM $ generalBracket
185+ (unJSM acquire)
186+ (\resource exitCase -> unJSM $ release resource exitCase)
187+ (unJSM . syncAfter . use)
188+#endif
181189
182190#endif
183191
@@ -496,7 +504,7 @@ instance ToJSON Result where
496504instance FromJSON Result
497505
498506data BatchResults = Success [JSValueReceived] [Result]
499− | Failure [JSValueReceived] [Result] JSValueReceived
507+ | Failure [JSValueReceived] [Result] JSValueReceived String
500508 deriving (Show, Generic)
501509
502510instance ToJSON BatchResults where
nix/hix.nixnew file+8
@@ -0,0 +1,8 @@
1+{pkgs, ...}:
2+{ modules = [({pkgs, ...}: {
3+ packages.webkit2gtk3-javascriptcore.components.library.doHaddock = false;
4+ })];
5+ shell.buildInputs = [ pkgs.nodejs ];
6+ shell.tools.cabal = {};
7+ shell.crossPlatforms = p: [ p.ghcjs ];
8+}
travis/build.sh+2−12
@@ -6,18 +6,8 @@ GHCVER=`ghc --numeric-version`
66
77JSADDLE_WARP_VERSION=`head -n2 jsaddle-warp/jsaddle-warp.cabal | tail -n1 | sed 's/[^0-9.]//g'`
88
9−if [[ -d .cabal && -d .ghc ]]; then
10− cp -a .cabal .ghc /root
11−fi
12−
139cabal update
14−cabal new-build --constraint='Cabal>=1.24.2.0' 'jsaddle-warp:lib:jsaddle-warp' 'jsaddle-warp:test:test-tool'
10+cabal new-build 'jsaddle-warp:lib:jsaddle-warp' 'jsaddle-warp:test:test-tool'
1511cd jsaddle-warp
16−GHC_PACKAGE_PATH=/opt/ghc/$GHCVER/lib/ghc-$GHCVER/package.conf.d:~/.cabal/store/ghc-$GHCVER/package.db:./dist-newstyle/packagedb/ghc-$GHCVER jsaddle_datadir=`pwd` ../dist-newstyle/build/jsaddle-warp-$JSADDLE_WARP_VERSION/build/test-tool/test-tool ../jsaddle-warp
17−
18−# update the cache
19−rm -rf .cabal
20−cp -a /root/.cabal ./
21−rm -rf .ghc
22−cp -a /root/.ghc ./
12+GHC_PACKAGE_PATH=/opt/ghc/$GHCVER/lib/ghc-$GHCVER/package.conf.d:~/.cabal/store/ghc-$GHCVER/package.db:../dist-newstyle/packagedb/ghc-$GHCVER ../dist-newstyle/build/x86_64-linux/ghc-$GHCVER/jsaddle-warp-$JSADDLE_WARP_VERSION/t/test-tool/build/test-tool/test-tool ../jsaddle
2313