git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1133 from purescript/external-ffi-2

Attempt to get psci working agian

commit32cb3b43c13c4270486d63a5715dab79249913cc authorPhil Freeman <paf31@cantab.net> dateFri 29 May 2015, 09:25 (11 years ago) parent2b71c78fe7f2f1
3 files changed+34−16
psci/Make.hs+7−4 psci/PSCi.hs+22−8 psci/Types.hs+5−4
psci/Make.hs+7−4
@@ -30,7 +30,7 @@ import Control.Monad.Error.Class (MonadError(..))
3030import Control.Monad.IO.Class (MonadIO, liftIO)
3131import Control.Monad.Reader (MonadReader, ReaderT, runReaderT)
3232import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
33−import Control.Monad.Writer (MonadWriter, WriterT, runWriterT)
33+import Control.Monad.Writer (MonadWriter, WriterT, runWriterT, tell)
3434
3535import System.Directory (getModificationTime, doesFileExist)
3636import 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+22−8
@@ -16,6 +16,7 @@
1616{-# LANGUAGE DoAndIfThenElse #-}
1717{-# LANGUAGE GeneralizedNewtypeDeriving #-}
1818{-# LANGUAGE RecordWildCards #-}
19+{-# LANGUAGE TupleSections #-}
1920
2021module 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+
523536nodeFlagsFlag :: Parser [String]
524537nodeFlagsFlag = option parser $
525538 long "node-opts"
@@ -532,6 +545,7 @@ nodeFlagsFlag = option parser $
532545psciOptions :: Parser PSCiOptions
533546psciOptions = PSCiOptions <$> multiLineMode
534547 <*> many inputFile
548+ <*> many inputForeignFile
535549 <*> nodeFlagsFlag
536550
537551runPSCi :: IO ()
psci/Types.hs+5−4
@@ -19,9 +19,10 @@ import qualified Data.Map as M
1919import qualified Language.PureScript as P
2020
2121data 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 }