git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #1133 from purescript/external-ffi-2
32cb3b4Phil Freeman
psci/Make.hs+7−4
@@ -30,7 +30,7 @@ import Control.Monad.Error.Class (MonadError(..))
30import Control.Monad.IO.Class (MonadIO, liftIO)
30import Control.Monad.IO.Class (MonadIO, liftIO)
31import Control.Monad.Reader (MonadReader, ReaderT, runReaderT)
31import Control.Monad.Reader (MonadReader, ReaderT, runReaderT)
32import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
32import Control.Monad.Trans.Except (ExceptT(..), runExceptT)
33−import Control.Monad.Writer (MonadWriter, WriterT, runWriterT)
33+import Control.Monad.Writer (MonadWriter, WriterT, runWriterT, tell)
34
34
35import System.Directory (getModificationTime, doesFileExist)
35import System.Directory (getModificationTime, doesFileExist)
36import System.FilePath ((</>), pathSeparator)
36import System.FilePath ((</>), pathSeparator)
@@ -90,10 +90,13 @@ buildMakeActions filePathMap foreigns =
90
90
91 codegen :: CF.Module CF.Ann -> P.Environment -> P.SupplyVar -> P.Externs -> Make ()
91 codegen :: CF.Module CF.Ann -> P.Environment -> P.SupplyVar -> P.Externs -> Make ()
92 codegen m _ nextVar exts = do
92 codegen m _ nextVar exts = do
93+ let mn = CF.moduleName m
93 foreignInclude <- case CF.moduleName m `M.lookup` foreigns of
94 foreignInclude <- case CF.moduleName m `M.lookup` foreigns of
94− Just _ | not $ requiresForeign m -> error "Found unnecessary foreign module"
95+ Just path
96+ | not $ requiresForeign m -> do tell $ P.errorMessage $ P.UnnecessaryFFIModule mn path
97+ return Nothing
95− | otherwise -> return $ Just $ J.JSApp (J.JSVar "require") [J.JSStringLiteral "./foreign"]
98+ | otherwise -> return $ Just $ J.JSApp (J.JSVar "require") [J.JSStringLiteral "./foreign"]
96− Nothing | requiresForeign m -> error "Foreign module missing"
99+ Nothing | requiresForeign m -> throwError . P.errorMessage $ P.MissingFFIModule mn
97 | otherwise -> return Nothing
100 | otherwise -> return Nothing
98 pjs <- P.evalSupplyT nextVar $ P.prettyPrintJS <$> J.moduleToJs m foreignInclude
101 pjs <- P.evalSupplyT nextVar $ P.prettyPrintJS <$> J.moduleToJs m foreignInclude
99 let filePath = P.runModuleName $ CF.moduleName m
102 let filePath = P.runModuleName $ CF.moduleName m
psci/PSCi.hs+22−8
@@ -16,6 +16,7 @@
16{-# LANGUAGE DoAndIfThenElse #-}
16{-# LANGUAGE DoAndIfThenElse #-}
17{-# LANGUAGE GeneralizedNewtypeDeriving #-}
17{-# LANGUAGE GeneralizedNewtypeDeriving #-}
18{-# LANGUAGE RecordWildCards #-}
18{-# LANGUAGE RecordWildCards #-}
19+{-# LANGUAGE TupleSections #-}
19
20
20module PSCi where
21module PSCi where
21
22
@@ -491,14 +492,20 @@ loop PSCiOptions{..} = do
491 Right modules -> do
492 Right modules -> do
492 historyFilename <- getHistoryFilename
493 historyFilename <- getHistoryFilename
493 let settings = defaultSettings { historyFile = Just historyFilename }
494 let settings = defaultSettings { historyFile = Just historyFilename }
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 ->
494− flip evalStateT (PSCiState psciInputFile defaultImports modules [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
501+ flip evalStateT (PSCiState psciInputFile defaultImports modules foreigns [] psciInputNodeFlags) . runInputT (setComplete completion settings) $ do
495− outputStrLn prologueMessage
502+ outputStrLn prologueMessage
496− traverse_ (mapM_ (runPSCI . handleCommand)) config
503+ traverse_ (mapM_ (runPSCI . handleCommand)) config
497− unless (consoleIsDefined (map snd modules)) . outputStrLn $ unlines
504+ unless (consoleIsDefined (map snd modules)) . outputStrLn $ unlines
498− [ "PSCi requires the purescript-console module to be installed."
505+ [ "PSCi requires the purescript-console module to be installed."
499− , "For help getting started, visit http://wiki.purescript.org/PSCi"
506+ , "For help getting started, visit http://wiki.purescript.org/PSCi"
500− ]
507+ ]
501− go
508+ go
502 where
509 where
503 go :: InputT (StateT PSCiState IO) ()
510 go :: InputT (StateT PSCiState IO) ()
504 go = do
511 go = do
@@ -520,6 +527,12 @@ inputFile = strArgument $
520 metavar "FILE"
527 metavar "FILE"
521 <> Opts.help "Optional .purs files to load on start"
528 <> Opts.help "Optional .purs files to load on start"
522
529
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+
523nodeFlagsFlag :: Parser [String]
536nodeFlagsFlag :: Parser [String]
524nodeFlagsFlag = option parser $
537nodeFlagsFlag = option parser $
525 long "node-opts"
538 long "node-opts"
@@ -532,6 +545,7 @@ nodeFlagsFlag = option parser $
532psciOptions :: Parser PSCiOptions
545psciOptions :: Parser PSCiOptions
533psciOptions = PSCiOptions <$> multiLineMode
546psciOptions = PSCiOptions <$> multiLineMode
534 <*> many inputFile
547 <*> many inputFile
548+ <*> many inputForeignFile
535 <*> nodeFlagsFlag
549 <*> nodeFlagsFlag
536
550
537runPSCi :: IO ()
551runPSCi :: IO ()
psci/Types.hs+5−4
@@ -19,9 +19,10 @@ import qualified Data.Map as M
19import qualified Language.PureScript as P
19import qualified Language.PureScript as P
20
20
21data PSCiOptions = PSCiOptions
21data PSCiOptions = PSCiOptions
22− { psciMultiLineMode :: Bool
22+ { psciMultiLineMode :: Bool
23− , psciInputFile :: [FilePath]
23+ , psciInputFile :: [FilePath]
24+ , psciForeignInputFiles :: [FilePath]
24− , psciInputNodeFlags :: [String]
25+ , psciInputNodeFlags :: [String]
25 }
26 }
26
27
27-- |
28-- |
@@ -34,7 +35,7 @@ data PSCiState = PSCiState
34 { psciImportedFilenames :: [FilePath]
35 { psciImportedFilenames :: [FilePath]
35 , psciImportedModules :: [ImportedModule]
36 , psciImportedModules :: [ImportedModule]
36 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
37 , psciLoadedModules :: [(Either P.RebuildPolicy FilePath, P.Module)]
37− --, psciForeignFiles :: M.Map P.ModuleName P.ForeignJS
38+ , psciForeignFiles :: M.Map P.ModuleName (FilePath, P.ForeignJS)
38 , psciLetBindings :: [P.Declaration]
39 , psciLetBindings :: [P.Declaration]
39 , psciNodeFlags :: [String]
40 , psciNodeFlags :: [String]
40 }
41 }