Skip to content

Commit f822ca8

Browse files
committed
FFI exts - previous tests pass
1 parent 314961e commit f822ca8

9 files changed

Lines changed: 46 additions & 31 deletions

File tree

app/Command/Compile.hs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ compile PSCMakeOptions{..} = do
7373
(makeErrors, makeWarnings) <- runMake pscmOpts $ do
7474
ms <- CST.parseModulesFromFiles id moduleFiles
7575
let filePathMap = M.fromList $ map (\(fp, pm) -> (P.getModuleName $ CST.resPartial pm, Right fp)) ms
76-
foreigns <- inferForeignModules filePathMap
76+
foreigns <- inferForeignModules (P.optionsFFIExts pscmOpts) filePathMap
7777
let makeActions = buildMakeActions pscmOutputDir filePathMap foreigns pscmUsePrefix
7878
P.make_ makeActions (map snd ms)
7979
printWarningsAndErrors (P.optionsVerboseErrors pscmOpts) pscmJSONErrors moduleFiles makeWarnings makeErrors
@@ -133,9 +133,14 @@ targetParser =
133133
. T.unpack
134134
. T.strip
135135

136+
ffiExtParser :: Opts.ReadM [String]
137+
ffiExtParser =
138+
Opts.str >>= \s ->
139+
for (T.split (== ',') s)
140+
$ pure . T.unpack . T.strip
141+
136142
ffiExtensions :: Opts.Parser [String]
137-
ffiExtensions = Opts.option targetParser $
138-
Opts.long "ffi-exts"
143+
ffiExtensions = Opts.option ffiExtParser $ Opts.long "ffi-exts"
139144
<> Opts.value ["js"]
140145
<> Opts.help
141146
( "Specifies comma-separated file extensions to consider for foriegn module implementations. "

src/Language/PureScript/Docs/Collect.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ compileForDocs outputDir inputFiles = do
9595
fmap fst $ P.runMake testOptions $ do
9696
ms <- P.parseModulesFromFiles identity moduleFiles
9797
let filePathMap = Map.fromList $ map (\(fp, pm) -> (P.getModuleName $ P.resPartial pm, Right fp)) ms
98-
foreigns <- P.inferForeignModules filePathMap
98+
ffiExts <- asks P.optionsFFIExts
99+
foreigns <- P.inferForeignModules ffiExts filePathMap
99100
let makeActions =
100101
(P.buildMakeActions outputDir filePathMap foreigns False)
101102
{ P.progress = liftIO . TIO.hPutStr stdout . (<> "\n") . P.renderProgressMessage "documentation for "

src/Language/PureScript/Ide/Rebuild.hs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,14 @@ rebuildFile file actualFile codegenTargets runOpenBuild = do
8787
let filePathMap = M.singleton moduleName (Left P.RebuildAlways)
8888
let pureRebuild = fp == ""
8989
let modulePath = if pureRebuild then fp' else file
90-
foreigns <- P.inferForeignModules (M.singleton moduleName (Right modulePath))
90+
let opts = P.defaultOptions { P.optionsCodegenTargets = codegenTargets }
91+
foreigns <- P.inferForeignModules (P.optionsFFIExts opts) (M.singleton moduleName (Right modulePath))
9192
let makeEnv = P.buildMakeActions outputDirectory filePathMap foreigns False
9293
& (if pureRebuild then enableForeignCheck foreigns codegenTargets . shushCodegen else identity)
9394
& shushProgress
9495
-- Rebuild the single module using the cached externs
9596
(result, warnings) <- logPerf (labelTimespec "Rebuilding Module") $
96-
liftIO $ P.runMake (P.defaultOptions { P.optionsCodegenTargets = codegenTargets }) do
97+
liftIO $ P.runMake opts do
9798
newExterns <- P.rebuildModule makeEnv externs m
9899
unless pureRebuild
99100
$ updateCacheDb codegenTargets outputDirectory file actualFile moduleName
@@ -137,7 +138,8 @@ updateCacheDb codegenTargets outputDirectory file actualFile moduleName = do
137138

138139
foreignCacheInfo <-
139140
if S.member P.JS codegenTargets then do
140-
foreigns' <- P.inferForeignModules (M.singleton moduleName (Right (fromMaybe file actualFile)))
141+
let opts = P.defaultOptions { P.optionsCodegenTargets = codegenTargets }
142+
foreigns' <- P.inferForeignModules (P.optionsFFIExts opts) (M.singleton moduleName (Right (fromMaybe file actualFile)))
141143
for (M.lookup moduleName foreigns') \foreignPath -> do
142144
foreignHash <- P.hashFile foreignPath
143145
pure (normaliseForCache cwd foreignPath, (dayZero, foreignHash))

src/Language/PureScript/Interactive.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,13 @@ make
8181
:: [(FilePath, CST.PartialResult P.Module)]
8282
-> P.Make ([P.ExternsFile], P.Environment)
8383
make ms = do
84-
foreignFiles <- P.inferForeignModules filePathMap
85-
externs <- P.make (buildActions foreignFiles) (map snd ms)
84+
ffiExts <- asks P.optionsFFIExts
85+
foreignFiles <- P.inferForeignModules ffiExts filePathMap
86+
externs <- P.make (buildActions ffiExts foreignFiles) (map snd ms)
8687
return (externs, foldl' (flip P.applyExternsFileToEnvironment) P.initEnvironment externs)
8788
where
88-
buildActions :: M.Map P.ModuleName FilePath -> P.MakeActions P.Make
89-
buildActions foreignFiles =
89+
buildActions :: S.Set String -> M.Map P.ModuleName FilePath -> P.MakeActions P.Make
90+
buildActions _ffiExts foreignFiles =
9091
P.buildMakeActions modulesDir
9192
filePathMap
9293
foreignFiles

src/Language/PureScript/Make.hs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -367,28 +367,30 @@ make' MakeOptions{..} ma@MakeActions{..} ms = do
367367
BuildPlan.markComplete buildPlan moduleName result
368368

369369
-- | Infer the module name for a module by looking for the same filename with
370-
-- a .js or .ts extension.
370+
-- an FFI extension (e.g., .js, .ts, or other configured extensions).
371371
inferForeignModules
372372
:: forall m
373373
. MonadIO m
374-
=> M.Map ModuleName (Either RebuildPolicy FilePath)
374+
=> S.Set String
375+
-- ^ Set of FFI extensions to check (e.g., {"js", "ts"})
376+
-> M.Map ModuleName (Either RebuildPolicy FilePath)
375377
-> m (M.Map ModuleName FilePath)
376-
inferForeignModules =
378+
inferForeignModules exts =
377379
fmap (M.mapMaybe id) . traverse inferForeignModule
378380
where
379381
inferForeignModule :: Either RebuildPolicy FilePath -> m (Maybe FilePath)
380382
inferForeignModule (Left _) = return Nothing
381383
inferForeignModule (Right path) = do
382-
let
383-
jsFile = replaceExtension path "js"
384-
tsFile = replaceExtension path "ts"
385-
existsJs <- liftIO $ doesFileExist jsFile
386-
387-
if existsJs
388-
then return (Just jsFile)
389-
else do
390-
existsTs <- liftIO $ doesFileExist tsFile
391-
if existsTs
392-
then return (Just tsFile)
393-
else return Nothing
384+
-- Try each extension in order
385+
let extList = S.toList exts
386+
candidates = map (replaceExtension path) extList
387+
findFirst candidates
388+
389+
findFirst :: [FilePath] -> m (Maybe FilePath)
390+
findFirst [] = return Nothing
391+
findFirst (fp:fps) = do
392+
exists <- liftIO $ doesFileExist fp
393+
if exists
394+
then return (Just fp)
395+
else findFirst fps
394396

src/Language/PureScript/Make/Actions.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
304304
| not $ requiresForeign m -> do
305305
return Nothing
306306
| otherwise -> do
307-
let ext = if takeExtension path == ".ts" then ".ts" else ".js"
307+
let ext = takeExtension path
308308
return $ Just (mkString $ T.pack $ "./foreign" ++ ext)
309309
Nothing | requiresForeign m -> throwError . errorMessage' (CF.moduleSourceSpan m) $ MissingFFIModule mn
310310
| otherwise -> return Nothing

src/Language/PureScript/Options.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ data Options = Options
1414
-- ^ Remove the comments from the generated js
1515
, optionsCodegenTargets :: S.Set CodegenTarget
1616
-- ^ Codegen targets (JS, CoreFn, etc.)
17+
, optionsFFIExts :: S.Set String
1718
} deriving Show
1819

1920
-- Default make options
2021
defaultOptions :: Options
21-
defaultOptions = Options False False (S.singleton JS)
22+
defaultOptions = Options False False (S.singleton JS) (S.singleton "js")
2223

2324
data CodegenTarget = JS | JSSourceMap | CoreFn | Docs
2425
deriving (Eq, Ord, Show)
@@ -30,3 +31,4 @@ codegenTargets = Map.fromList
3031
, ("corefn", CoreFn)
3132
, ("docs", Docs)
3233
]
34+

tests/TestMake.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Language.PureScript.Make.IdeCache (sqliteInit)
1111

1212
import Control.Concurrent (threadDelay)
1313
import Control.Monad (guard, void, forM_, when)
14+
import Control.Monad.Reader (asks)
1415
import Control.Exception (tryJust)
1516
import Control.Monad.IO.Class (liftIO)
1617
import Control.Concurrent.MVar (readMVar, newMVar, modifyMVar_)
@@ -703,7 +704,8 @@ compileWithOptions opts input = do
703704
(makeResult, _) <- P.runMake opts $ do
704705
ms <- CST.parseModulesFromFiles id moduleFiles
705706
let filePathMap = M.fromList $ map (\(fp, pm) -> (P.getModuleName $ CST.resPartial pm, Right fp)) ms
706-
foreigns <- P.inferForeignModules filePathMap
707+
ffiExts <- asks P.optionsFFIExts
708+
foreigns <- P.inferForeignModules ffiExts filePathMap
707709
let makeActions =
708710
(P.buildMakeActions modulesDir filePathMap foreigns True)
709711
{ P.progress = \case

tests/TestUtils.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ getPsModuleName psModule = case snd psModule of
240240
AST.Module _ _ (N.ModuleName t) _ _ -> t
241241

242242
makeActions :: [P.Module] -> M.Map P.ModuleName FilePath -> P.MakeActions P.Make
243-
makeActions modules foreigns = (P.buildMakeActions modulesDir (P.internalError "makeActions: input file map was read.") foreigns False)
243+
makeActions modules foreigns = (P.buildMakeActions modulesDir (P.internalError "makeActions: input file map was read.") foreigns mempty False)
244244
{ P.getInputTimestampsAndHashes = getInputTimestampsAndHashes
245245
, P.getOutputTimestamp = getOutputTimestamp
246246
, P.progress = const (pure ())
@@ -269,7 +269,7 @@ inferForeignModules
269269
:: MonadIO m
270270
=> [(FilePath, P.Module)]
271271
-> m (M.Map P.ModuleName FilePath)
272-
inferForeignModules = P.inferForeignModules . fromList
272+
inferForeignModules = P.inferForeignModules (P.optionsFFIExts P.defaultOptions) . fromList
273273
where
274274
fromList :: [(FilePath, P.Module)] -> M.Map P.ModuleName (Either P.RebuildPolicy FilePath)
275275
fromList = M.fromList . map ((P.getModuleName *** Right) . swap)

0 commit comments

Comments
 (0)