Skip to content

Commit 8ccdb44

Browse files
committed
FFI exts - previous tests pass
1 parent b6378d6 commit 8ccdb44

File tree

9 files changed

+55
-36
lines changed

9 files changed

+55
-36
lines changed

app/Command/Compile.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ compile PSCMakeOptions{..} = do
7171
(makeErrors, makeWarnings) <- runMake pscmOpts $ do
7272
ms <- CST.parseModulesFromFiles id moduleFiles
7373
let filePathMap = M.fromList $ map (\(fp, pm) -> (P.getModuleName $ CST.resPartial pm, Right fp)) ms
74-
foreigns <- inferForeignModules filePathMap
75-
let makeActions = buildMakeActions pscmOutputDir filePathMap foreigns pscmUsePrefix
74+
foreigns <- inferForeignModules (P.optionsFFIExts pscmOpts) filePathMap
75+
let makeActions = buildMakeActions pscmOutputDir filePathMap foreigns (P.optionsFFIExts pscmOpts) pscmUsePrefix
7676
P.make makeActions (map snd ms)
7777
printWarningsAndErrors (P.optionsVerboseErrors pscmOpts) pscmJSONErrors moduleFiles makeWarnings makeErrors
7878
exitSuccess
@@ -131,9 +131,14 @@ targetParser =
131131
. T.unpack
132132
. T.strip
133133

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

src/Language/PureScript/Docs/Collect.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,10 @@ compileForDocs outputDir inputFiles = do
9393
fmap fst $ P.runMake testOptions $ do
9494
ms <- P.parseModulesFromFiles identity moduleFiles
9595
let filePathMap = Map.fromList $ map (\(fp, pm) -> (P.getModuleName $ P.resPartial pm, Right fp)) ms
96-
foreigns <- P.inferForeignModules filePathMap
96+
ffiExts <- asks P.optionsFFIExts
97+
foreigns <- P.inferForeignModules ffiExts filePathMap
9798
let makeActions =
98-
(P.buildMakeActions outputDir filePathMap foreigns False)
99+
(P.buildMakeActions outputDir filePathMap foreigns ffiExts False)
99100
{ P.progress = liftIO . TIO.hPutStr stdout . (<> "\n") . P.renderProgressMessage "Compiling documentation for "
100101
}
101102
P.make makeActions (map snd ms)

src/Language/PureScript/Ide/Rebuild.hs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ rebuildFile file actualFile codegenTargets runOpenBuild = do
7373
let filePathMap = M.singleton moduleName (Left P.RebuildAlways)
7474
let pureRebuild = fp == ""
7575
let modulePath = if pureRebuild then fp' else file
76-
foreigns <- P.inferForeignModules (M.singleton moduleName (Right modulePath))
77-
let makeEnv = P.buildMakeActions outputDirectory filePathMap foreigns False
76+
let opts = P.defaultOptions { P.optionsCodegenTargets = codegenTargets }
77+
foreigns <- P.inferForeignModules (P.optionsFFIExts opts) (M.singleton moduleName (Right modulePath))
78+
let makeEnv = P.buildMakeActions outputDirectory filePathMap foreigns (P.optionsFFIExts opts) False
7879
& (if pureRebuild then enableForeignCheck foreigns codegenTargets . shushCodegen else identity)
7980
& shushProgress
8081
-- Rebuild the single module using the cached externs
8182
(result, warnings) <- logPerf (labelTimespec "Rebuilding Module") $
82-
liftIO $ P.runMake (P.defaultOptions { P.optionsCodegenTargets = codegenTargets }) do
83+
liftIO $ P.runMake opts do
8384
newExterns <- P.rebuildModule makeEnv externs m
8485
unless pureRebuild
8586
$ updateCacheDb codegenTargets outputDirectory file actualFile moduleName
@@ -123,7 +124,8 @@ updateCacheDb codegenTargets outputDirectory file actualFile moduleName = do
123124

124125
foreignCacheInfo <-
125126
if S.member P.JS codegenTargets then do
126-
foreigns' <- P.inferForeignModules (M.singleton moduleName (Right (fromMaybe file actualFile)))
127+
let opts = P.defaultOptions { P.optionsCodegenTargets = codegenTargets }
128+
foreigns' <- P.inferForeignModules (P.optionsFFIExts opts) (M.singleton moduleName (Right (fromMaybe file actualFile)))
127129
for (M.lookup moduleName foreigns') \foreignPath -> do
128130
foreignHash <- P.hashFile foreignPath
129131
pure (normaliseForCache cwd foreignPath, (dayZero, foreignHash))

src/Language/PureScript/Interactive.hs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ rebuild loadedExterns m = do
6868
(P.buildMakeActions modulesDir
6969
filePathMap
7070
M.empty
71+
mempty
7172
False) { P.progress = const (return ()) }
7273

7374
filePathMap :: M.Map P.ModuleName (Either P.RebuildPolicy FilePath)
@@ -78,15 +79,17 @@ make
7879
:: [(FilePath, CST.PartialResult P.Module)]
7980
-> P.Make ([P.ExternsFile], P.Environment)
8081
make ms = do
81-
foreignFiles <- P.inferForeignModules filePathMap
82-
externs <- P.make (buildActions foreignFiles) (map snd ms)
82+
ffiExts <- asks P.optionsFFIExts
83+
foreignFiles <- P.inferForeignModules ffiExts filePathMap
84+
externs <- P.make (buildActions ffiExts foreignFiles) (map snd ms)
8385
return (externs, foldl' (flip P.applyExternsFileToEnvironment) P.initEnvironment externs)
8486
where
85-
buildActions :: M.Map P.ModuleName FilePath -> P.MakeActions P.Make
86-
buildActions foreignFiles =
87+
buildActions :: S.Set String -> M.Map P.ModuleName FilePath -> P.MakeActions P.Make
88+
buildActions ffiExts foreignFiles =
8789
P.buildMakeActions modulesDir
8890
filePathMap
8991
foreignFiles
92+
ffiExts
9093
False
9194

9295
filePathMap :: M.Map P.ModuleName (Either P.RebuildPolicy FilePath)

src/Language/PureScript/Make.hs

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

285285
-- | Infer the module name for a module by looking for the same filename with
286-
-- a .js or .ts extension.
286+
-- an FFI extension (e.g., .js, .ts, or other configured extensions).
287287
inferForeignModules
288288
:: forall m
289289
. MonadIO m
290-
=> M.Map ModuleName (Either RebuildPolicy FilePath)
290+
=> S.Set String
291+
-- ^ Set of FFI extensions to check (e.g., {"js", "ts"})
292+
-> M.Map ModuleName (Either RebuildPolicy FilePath)
291293
-> m (M.Map ModuleName FilePath)
292-
inferForeignModules =
294+
inferForeignModules exts =
293295
fmap (M.mapMaybe id) . traverse inferForeignModule
294296
where
295297
inferForeignModule :: Either RebuildPolicy FilePath -> m (Maybe FilePath)
296298
inferForeignModule (Left _) = return Nothing
297299
inferForeignModule (Right path) = do
298-
let
299-
jsFile = replaceExtension path "js"
300-
tsFile = replaceExtension path "ts"
301-
existsJs <- liftIO $ doesFileExist jsFile
302-
303-
if existsJs
304-
then return (Just jsFile)
305-
else do
306-
existsTs <- liftIO $ doesFileExist tsFile
307-
if existsTs
308-
then return (Just tsFile)
309-
else return Nothing
300+
-- Try each extension in order
301+
let extList = S.toList exts
302+
candidates = map (replaceExtension path) extList
303+
findFirst candidates
304+
305+
findFirst :: [FilePath] -> m (Maybe FilePath)
306+
findFirst [] = return Nothing
307+
findFirst (fp:fps) = do
308+
exists <- liftIO $ doesFileExist fp
309+
if exists
310+
then return (Just fp)
311+
else findFirst fps
310312

src/Language/PureScript/Make/Actions.hs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,12 @@ buildMakeActions
171171
-- ^ a map between module names and paths to the file containing the PureScript module
172172
-> M.Map ModuleName FilePath
173173
-- ^ a map between module name and the file containing the foreign javascript for the module
174+
-> S.Set String
175+
-- ^ the set of FFI file extensions
174176
-> Bool
175177
-- ^ Generate a prefix comment?
176178
-> MakeActions Make
177-
buildMakeActions outputDir filePathMap foreigns usePrefix =
179+
buildMakeActions outputDir filePathMap foreigns _ffiExts usePrefix =
178180
MakeActions getInputTimestampsAndHashes getOutputTimestamp readExterns codegen ffiCodegen progress readCacheDb writeCacheDb writePackageJson outputPrimDocs
179181
where
180182

@@ -261,7 +263,7 @@ buildMakeActions outputDir filePathMap foreigns usePrefix =
261263
| not $ requiresForeign m -> do
262264
return Nothing
263265
| otherwise -> do
264-
let ext = if takeExtension path == ".ts" then ".ts" else ".js"
266+
let ext = takeExtension path
265267
return $ Just (mkString $ T.pack $ "./foreign" ++ ext)
266268
Nothing | requiresForeign m -> throwError . errorMessage' (CF.moduleSourceSpan m) $ MissingFFIModule mn
267269
| 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: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Language.PureScript.CST qualified as CST
1010

1111
import Control.Concurrent (threadDelay)
1212
import Control.Monad (guard, void)
13+
import Control.Monad.Reader (asks)
1314
import Control.Exception (tryJust)
1415
import Control.Monad.IO.Class (liftIO)
1516
import Control.Concurrent.MVar (readMVar, newMVar, modifyMVar_)
@@ -229,9 +230,10 @@ compileWithOptions opts input = do
229230
(makeResult, _) <- P.runMake opts $ do
230231
ms <- CST.parseModulesFromFiles id moduleFiles
231232
let filePathMap = M.fromList $ map (\(fp, pm) -> (P.getModuleName $ CST.resPartial pm, Right fp)) ms
232-
foreigns <- P.inferForeignModules filePathMap
233+
ffiExts <- asks P.optionsFFIExts
234+
foreigns <- P.inferForeignModules ffiExts filePathMap
233235
let makeActions =
234-
(P.buildMakeActions modulesDir filePathMap foreigns True)
236+
(P.buildMakeActions modulesDir filePathMap foreigns ffiExts True)
235237
{ P.progress = \(P.CompilingModule mn _) ->
236238
liftIO $ modifyMVar_ recompiled (return . Set.insert mn)
237239
}

tests/TestUtils.hs

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

240240
makeActions :: [P.Module] -> M.Map P.ModuleName FilePath -> P.MakeActions P.Make
241-
makeActions modules foreigns = (P.buildMakeActions modulesDir (P.internalError "makeActions: input file map was read.") foreigns False)
241+
makeActions modules foreigns = (P.buildMakeActions modulesDir (P.internalError "makeActions: input file map was read.") foreigns mempty False)
242242
{ P.getInputTimestampsAndHashes = getInputTimestampsAndHashes
243243
, P.getOutputTimestamp = getOutputTimestamp
244244
, P.progress = const (pure ())
@@ -265,7 +265,7 @@ inferForeignModules
265265
:: MonadIO m
266266
=> [(FilePath, P.Module)]
267267
-> m (M.Map P.ModuleName FilePath)
268-
inferForeignModules = P.inferForeignModules . fromList
268+
inferForeignModules = P.inferForeignModules (P.optionsFFIExts P.defaultOptions) . fromList
269269
where
270270
fromList :: [(FilePath, P.Module)] -> M.Map P.ModuleName (Either P.RebuildPolicy FilePath)
271271
fromList = M.fromList . map ((P.getModuleName *** Right) . swap)

0 commit comments

Comments
 (0)