Skip to content

Commit fe7f2f1

Browse files
committed
Attempt to get psci working agian
1 parent 2b71c78 commit fe7f2f1

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

psci/Make.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import Control.Monad.Error.Class (MonadError(..))
3030
import Control.Monad.IO.Class (MonadIO, liftIO)
3131
import Control.Monad.Reader (MonadReader, ReaderT, runReaderT)
3232
import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
33-
import Control.Monad.Writer (MonadWriter, WriterT, runWriterT)
33+
import Control.Monad.Writer (MonadWriter, WriterT, runWriterT, tell)
3434

3535
import System.Directory (getModificationTime, doesFileExist)
3636
import System.FilePath ((</>), pathSeparator)
@@ -90,10 +90,13 @@ buildMakeActions filePathMap foreigns =
9090

9191
codegen :: CF.Module CF.Ann -> P.Environment -> P.SupplyVar -> P.Externs -> Make ()
9292
codegen m _ nextVar exts = do
93+
let mn = CF.moduleName m
9394
foreignInclude <- case CF.moduleName m `M.lookup` foreigns of
94-
Just _ | not $ requiresForeign m -> error "Found unnecessary foreign module"
95-
| otherwise -> return $ Just $ J.JSApp (J.JSVar "require") [J.JSStringLiteral "./foreign"]
96-
Nothing | requiresForeign m -> error "Foreign module missing"
95+
Just path
96+
| not $ requiresForeign m -> do tell $ P.errorMessage $ P.UnnecessaryFFIModule mn path
97+
return Nothing
98+
| otherwise -> return $ Just $ J.JSApp (J.JSVar "require") [J.JSStringLiteral "./foreign"]
99+
Nothing | requiresForeign m -> throwError . P.errorMessage $ P.MissingFFIModule mn
97100
| otherwise -> return Nothing
98101
pjs <- P.evalSupplyT nextVar $ P.prettyPrintJS <$> J.moduleToJs m foreignInclude
99102
let filePath = P.runModuleName $ CF.moduleName m

psci/PSCi.hs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
{-# LANGUAGE DoAndIfThenElse #-}
1717
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1818
{-# LANGUAGE RecordWildCards #-}
19+
{-# LANGUAGE TupleSections #-}
1920

2021
module PSCi where
2122

@@ -491,14 +492,20 @@ loop PSCiOptions{..} = do
491492
Right modules -> do
492493
historyFilename <- getHistoryFilename
493494
let settings = defaultSettings { historyFile = Just historyFilename }
494-
flip evalStateT (PSCiState psciInputFile defaultImports modules [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
495-
outputStrLn prologueMessage
496-
traverse_ (mapM_ (runPSCI . handleCommand)) config
497-
unless (consoleIsDefined (map snd modules)) . outputStrLn $ unlines
498-
[ "PSCi requires the purescript-console module to be installed."
499-
, "For help getting started, visit http://wiki.purescript.org/PSCi"
500-
]
501-
go
495+
foreignsOrError <- runMake $ do
496+
foreignFiles <- forM psciForeignInputFiles (\inFile -> (inFile,) <$> makeIO (const (P.SimpleErrorWrapper $ P.CannotReadFile inFile)) (readFile inFile))
497+
P.parseForeignModulesFromFiles foreignFiles
498+
case foreignsOrError of
499+
Left errs -> putStrLn (P.prettyPrintMultipleErrors False errs) >> exitFailure
500+
Right foreigns ->
501+
flip evalStateT (PSCiState psciInputFile defaultImports modules foreigns [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
502+
outputStrLn prologueMessage
503+
traverse_ (mapM_ (runPSCI . handleCommand)) config
504+
unless (consoleIsDefined (map snd modules)) . outputStrLn $ unlines
505+
[ "PSCi requires the purescript-console module to be installed."
506+
, "For help getting started, visit http://wiki.purescript.org/PSCi"
507+
]
508+
go
502509
where
503510
go :: InputT (StateT PSCiState IO) ()
504511
go = do
@@ -520,6 +527,12 @@ inputFile = strArgument $
520527
metavar "FILE"
521528
<> Opts.help "Optional .purs files to load on start"
522529

530+
inputForeignFile :: Parser FilePath
531+
inputForeignFile = strOption $
532+
short 'f'
533+
<> long "ffi"
534+
<> help "The input .js file(s) providing foreign import implementations"
535+
523536
nodeFlagsFlag :: Parser [String]
524537
nodeFlagsFlag = option parser $
525538
long "node-opts"
@@ -532,6 +545,7 @@ nodeFlagsFlag = option parser $
532545
psciOptions :: Parser PSCiOptions
533546
psciOptions = PSCiOptions <$> multiLineMode
534547
<*> many inputFile
548+
<*> many inputForeignFile
535549
<*> nodeFlagsFlag
536550

537551
runPSCi :: IO ()

psci/Types.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import qualified Data.Map as M
1919
import qualified Language.PureScript as P
2020

2121
data PSCiOptions = PSCiOptions
22-
{ psciMultiLineMode :: Bool
23-
, psciInputFile :: [FilePath]
24-
, psciInputNodeFlags :: [String]
22+
{ psciMultiLineMode :: Bool
23+
, psciInputFile :: [FilePath]
24+
, psciForeignInputFiles :: [FilePath]
25+
, psciInputNodeFlags :: [String]
2526
}
2627

2728
-- |
@@ -34,7 +35,7 @@ data PSCiState = PSCiState
3435
{ psciImportedFilenames :: [FilePath]
3536
, psciImportedModules :: [ImportedModule]
3637
, psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
37-
--, psciForeignFiles :: M.Map P.ModuleName P.ForeignJS
38+
, psciForeignFiles :: M.Map P.ModuleName (FilePath, P.ForeignJS)
3839
, psciLetBindings :: [P.Declaration]
3940
, psciNodeFlags :: [String]
4041
}

0 commit comments

Comments
 (0)