Skip to content

Commit 74b574f

Browse files
authored
winio: Add support for WINIO to haskeline. (#140)
1 parent d3885e4 commit 74b574f

File tree

1 file changed

+24
-2
lines changed
  • System/Console/Haskeline/Backend/Win32

1 file changed

+24
-2
lines changed

System/Console/Haskeline/Backend/Win32/Echo.hs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import Foreign.C.Types
2121
import Foreign.StablePtr (StablePtr, freeStablePtr, newStablePtr)
2222

2323
import GHC.IO.FD (FD(..))
24+
#if defined(__IO_MANAGER_WINIO__)
25+
import GHC.IO.Handle.Windows (handleToHANDLE)
26+
import GHC.IO.SubSystem ((<!>))
27+
#endif
2428
import GHC.IO.Handle.Types (Handle(..), Handle__(..))
2529

2630
import System.Win32.Types (HANDLE)
@@ -145,7 +149,26 @@ foreign import ccall unsafe "_get_osfhandle"
145149

146150
-- Originally authored by Max Bolingbroke in the ansi-terminal library
147151
withHandleToHANDLE :: Handle -> (HANDLE -> IO a) -> IO a
148-
withHandleToHANDLE haskell_handle action =
152+
#if defined(__IO_MANAGER_WINIO__)
153+
withHandleToHANDLE = withHandleToHANDLEPosix <!> withHandleToHANDLENative
154+
#else
155+
withHandleToHANDLE = withHandleToHANDLEPosix
156+
#endif
157+
158+
#if defined(__IO_MANAGER_WINIO__)
159+
withHandleToHANDLENative :: Handle -> (HANDLE -> IO a) -> IO a
160+
withHandleToHANDLENative haskell_handle action =
161+
-- Create a stable pointer to the Handle. This prevents the garbage collector
162+
-- getting to it while we are doing horrible manipulations with it, and hence
163+
-- stops it being finalized (and closed).
164+
withStablePtr haskell_handle $ const $ do
165+
windows_handle <- handleToHANDLE haskell_handle
166+
-- Do what the user originally wanted
167+
action windows_handle
168+
#endif
169+
170+
withHandleToHANDLEPosix :: Handle -> (HANDLE -> IO a) -> IO a
171+
withHandleToHANDLEPosix haskell_handle action =
149172
-- Create a stable pointer to the Handle. This prevents the garbage collector
150173
-- getting to it while we are doing horrible manipulations with it, and hence
151174
-- stops it being finalized (and closed).
@@ -162,7 +185,6 @@ withHandleToHANDLE haskell_handle action =
162185

163186
-- Finally, turn that (C-land) FD into a HANDLE using msvcrt
164187
windows_handle <- c_get_osfhandle fd
165-
166188
-- Do what the user originally wanted
167189
action windows_handle
168190

0 commit comments

Comments
 (0)