Skip to content

A peer-to-peer stack for code collaboration

ghcjs: Fix build and repl (#215)

Alexander Simmerl921788353e22
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 1e19ac6..6b4ee82 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -472,6 +472,11 @@ Documentation and testing
Functions for creating and querying documentation of variables in scope,
and testing functions.
+``help``
+~~~~~~~~
+
+Default help text.
+
``doc``
~~~~~~~
diff --git a/exe/Server/GHCJS.hs b/exe/GHCJS.hs
similarity index 74%
rename from exe/Server/GHCJS.hs
rename to exe/GHCJS.hs
index f84f8df..defde04 100644
--- a/exe/Server/GHCJS.hs
+++ b/exe/GHCJS.hs
@@ -4,6 +4,7 @@ module GHCJS where
import Protolude hiding (TypeError)
import API
+import Control.Monad.Catch (MonadThrow)
import Data.IORef
import qualified Data.JSString as JSS
import Data.Scientific (floatingOrInteger)
@@ -15,8 +16,9 @@ import GHCJS.Types
import JavaScript.Object (getProp, setProp)
import JavaScript.Object.Internal (Object(..))
import Radicle
-import Servant.API ((:<|>)(..))
+import Radicle.Internal.PrimFns (allDocs)
import Servant.Client.Ghcjs
+import Servant.Client.Internal.XhrClient (runClientMOrigin)
import System.Console.Haskeline (InputT, defaultSettings, runInputT)
main :: IO ()
@@ -66,35 +68,39 @@ bindings :: Bindings (PrimFns (InputT IO))
bindings = e { bindingsPrimFns = bindingsPrimFns e <> primops }
where
e :: Bindings (PrimFns (InputT IO))
- e = pureEnv
+ e = replBindings
primops :: PrimFns (InputT IO)
-primops = PrimFns (fromList [sendPrimop, receivePrimop]) <> replPrimFns
+primops = fromList $ allDocs [sendPrimop, receivePrimop]
where
sendPrimop =
- ( unsafeToIdent "send!"
+ ( "send!"
+ , "Given a URL (string) and a vector of value, sends the value `v` to the\
+ \ remote chain located at the URL for evaluation."
, \case
- [String name, v] -> do
- res <- liftIO $ runClientM (submit $ List $ [String name, v])
+ [String name, Vec v] -> do
+ res <- liftIO $ runClientM' name (submit $ toList v)
case res of
Left e -> throwErrorHere . OtherError
$ "send!: failed:" <> show e
- Right () -> pure $ List []
+ Right _ -> pure $ List []
[_, _] -> throwErrorHere $ TypeError "send!: first argument should be a string"
xs -> throwErrorHere $ WrongNumberOfArgs "send!" 2 (length xs)
)
receivePrimop =
- ( unsafeToIdent "receive!"
+ ( "receive!"
+ , "Given a URL (string) and a integral number `n`, queries the remote chain\
+ \ for the last `n` inputs that have been evaluated."
, \case
[String name, Number n] -> do
case floatingOrInteger n of
Left (_ :: Float) -> throwErrorHere . OtherError
$ "receive!: expecting int argument"
Right r -> do
- liftIO (runClientM (since name r)) >>= \case
+ liftIO (runClientM' name (since r)) >>= \case
Left err -> throwErrorHere . OtherError
$ "receive!: request failed:" <> show err
- Right v' -> pure $ List v'
+ Right v' -> pure v'
[String _, _] -> throwErrorHere $ TypeError "receive!: expecting number as second arg"
[_, _] -> throwErrorHere $ TypeError "receive!: expecting string as first arg"
xs -> throwErrorHere $ WrongNumberOfArgs "receive!" 2 (length xs)
@@ -107,6 +113,13 @@ identV = Keyword . unsafeToIdent
-- * Client functions
-submit :: Value -> ClientM ()
-since :: Text -> Int -> ClientM [Value]
-submit :<|> since :<|> _ = client api
+submit :: [Value] -> ClientM Value
+submit = client chainSubmitEndpoint . Values
+
+since :: Int -> ClientM Value
+since = client chainSinceEndpoint
+
+runClientM' :: (MonadThrow m, MonadIO m) => Text -> ClientM a -> m (Either ServantError a)
+runClientM' baseUrl endpoint = do
+ url <- parseBaseUrl $ T.unpack baseUrl
+ liftIO $ runClientMOrigin endpoint $ ClientEnv url
diff --git a/exe/ReferenceDoc.hs b/exe/ReferenceDoc.hs
index 8cfa9e8..5603df4 100644
--- a/exe/ReferenceDoc.hs
+++ b/exe/ReferenceDoc.hs
@@ -91,7 +91,7 @@ main = do
, "map-values", "modify-map"]
structs = ["member?"]
refs = ["ref", "read-ref", "write-ref", "modify-ref"]
- docs = ["doc", "doc!", "apropos!", "document", "is-test-env"]
+ docs = ["help", "doc", "doc!", "apropos!", "document", "is-test-env"]
io =
["print!", "get-line!", "load!", "read-file!", "read-code!", "send-code!"
, "send-prelude!", "subscribe-to!", "uuid!", "read-line!", "exit!"]
@@ -111,7 +111,7 @@ main = do
typ s = "Functions for manipulating " <> s <> "."
-- Functions which shouldn't be in the reference docs.
- doNotInclude = ["head-shots", "get-head-shot", "eval__", "kim-trans", "pr-thread", "pr-trans"]
+ doNotInclude = ["head-shots", "get-head-shot", "eval__", "kim-trans", "pr-thread", "pr-trans", "_initial-prompt-text"]
allFns =
basics ++ maths ++ evalFns ++ envStuff ++ seqs ++ lists ++ vecs
diff --git a/package.yaml b/package.yaml
index 44a46c5..560ccd4 100644
--- a/package.yaml
+++ b/package.yaml
@@ -116,7 +116,7 @@ executables:
radicle-client-ghcjs:
main: GHCJS.hs
- source-dirs: exe/Server
+ source-dirs: exe
other-modules: API
dependencies:
- radicle
@@ -132,7 +132,13 @@ executables:
buildable: true
else:
buildable: false
- ghc-options: -main-is GHCJS -threaded
+ # XXX(xla): We need to remove the debug linking as it produces output js of
+ # aroud 27MB.
+ # We force debug linking as without it we encounter a runtime exception
+ # in ghcjs due to the metadata initializer only supporting objects with up
+ # to 100
+ # fields: https://github.com/ghcjs/ghcjs/issues/498#issuecomment-230725647
+ ghc-options: -main-is GHCJS -threaded -debug
radicle-doc:
main: Doc.hs
diff --git a/rad/prelude.rad b/rad/prelude.rad
index ed5a6ba..df1501f 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -54,4 +54,16 @@
{:next-will-be (fn [] (+ 1 (read-ref i)))
:next (fn [] (modify-ref i (fn [x] (+ x 1))))}))
+(def _initial-prompt-text
+ "Text used for greeting in the repl."
+ "Welcome to radicle. Type (help) for help.")
+
+(def help
+ "Default help text."
+ (fn []
+ "radicle is a LISP-like language intended for programming chains, and interacting with those chains.
+
+ Type (doc '<name>) for further documentation of <name>."))
+
+
(load! "rad/chains/issues.rad") ;; send-prelude! must be defined already
diff --git a/rad/repl.rad b/rad/repl.rad
index c08c266..eda4638 100644
--- a/rad/repl.rad
+++ b/rad/repl.rad
@@ -2,15 +2,6 @@
;; The Repl
-
-(def _initial_prompt-text
- "Welcome to radicle. Type (help) for help.")
-
-(def help (fn []
- "radicle is a LISP-like language intended for programming chains, and interacting with those chains.
-
- Type (doc '<name>) for further documentation of <name>."))
-
(def read-line-or-exit! (fn []
(def line (get-line!))
(if (eq? line nil)
diff --git a/src/Radicle/Internal/Effects/Capabilities.hs b/src/Radicle/Internal/Effects/Capabilities.hs
index 238d345..ec495f1 100644
--- a/src/Radicle/Internal/Effects/Capabilities.hs
+++ b/src/Radicle/Internal/Effects/Capabilities.hs
@@ -69,7 +69,7 @@ class (Monad m) => ReadFile m where
instance ReadFile (InputT IO) where
readFileS = lift . requestFile
where
- requestFile :: Text -> IO Text
+ requestFile :: Text -> IO (Either Text Text)
requestFile filename = do
req <- newXMLHttpRequest
openSimple req ("GET" :: Text) filename
diff --git a/stack-ghcjs.yaml b/stack-ghcjs.yaml
index 9c1686e..483d571 100644
--- a/stack-ghcjs.yaml
+++ b/stack-ghcjs.yaml
@@ -26,3 +26,4 @@ extra-deps:
- ghcjs-dom-0.9.2.0
- terminfo-0.4.1.2
- ghcjs-dom-jsffi-0.9.2.0
+- uuid-1.3.13