Cleanup treat_strings_as_paths
@@ -39,7 +39,7 @@ import Data.Semigroup (Semigroup(..)) -- includes (<>)
import Data.Text (Text)
import Foreign.Lua
( Lua, NumResults (..), getglobal, getmetatable, nth, pop, rawset, remove
- , setfield, top )
+ , top )
import Foreign.Lua.Call
import Foreign.Lua.Module hiding (preloadModule, pushModule)
import Foreign.Lua.Peek (Peeker, peekList, peekString)
@@ -266,21 +266,8 @@ add_extension = toHsFnPrecursor Path.addExtension
=#> [filepathResult "filepath with extension"]
#? "Adds an extension, even if there is already one."
-treat_strings_as_paths :: HaskellFunction
-treat_strings_as_paths = HaskellFunction
- { callFunction = do
- -- for some reason we can't just dump all functions into the
- -- string metatable, but have to use the string module for
- -- non-metamethods.
- pushString ""
- _ <- getmetatable top
- remove (nth 2) -- dummy string
- pushHaskellFunction add_extension *> setfield (nth 2) "__add"
- pushHaskellFunction combine *> setfield (nth 2) "__div"
- pop 1 -- string metatable
-
- getglobal "string"
- let pathFunctions =
+stringAugmentationFunctions :: [(String, HaskellFunction)]
+stringAugmentationFunctions =
[ ("directory", directory)
, ("filename", filename)
, ("is_absolute", is_absolute)
@@ -290,9 +277,22 @@ treat_strings_as_paths = HaskellFunction
, ("split_extension", split_extension)
, ("split_search_path", split_search_path)
]
- forM_ pathFunctions $ \(key, fn) -> do
- pushHaskellFunction fn
- setfield (nth 2) key
+
+treat_strings_as_paths :: HaskellFunction
+treat_strings_as_paths = HaskellFunction
+ { callFunction = do
+ let addField (k, v) =
+ pushString k *> pushHaskellFunction v *> rawset (nth 3)
+ -- for some reason we can't just dump all functions into the
+ -- string metatable, but have to use the string module for
+ -- non-metamethods.
+ pushString "" *> getmetatable top *> remove (nth 2)
+ mapM_ addField $ [("__add", add_extension), ("__div", combine)]
+ pop 1 -- string metatable
+
+ getglobal "string"
+ mapM_ addField stringAugmentationFunctions
+ pop 1 -- string module
return (0 :: NumResults)
, functionDoc = Nothing
