@@ -21,6 +21,10 @@ import Foreign.C.Types
21
21
import Foreign.StablePtr (StablePtr , freeStablePtr , newStablePtr )
22
22
23
23
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
24
28
import GHC.IO.Handle.Types (Handle (.. ), Handle__ (.. ))
25
29
26
30
import System.Win32.Types (HANDLE )
@@ -145,7 +149,26 @@ foreign import ccall unsafe "_get_osfhandle"
145
149
146
150
-- Originally authored by Max Bolingbroke in the ansi-terminal library
147
151
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 =
149
172
-- Create a stable pointer to the Handle. This prevents the garbage collector
150
173
-- getting to it while we are doing horrible manipulations with it, and hence
151
174
-- stops it being finalized (and closed).
@@ -162,7 +185,6 @@ withHandleToHANDLE haskell_handle action =
162
185
163
186
-- Finally, turn that (C-land) FD into a HANDLE using msvcrt
164
187
windows_handle <- c_get_osfhandle fd
165
-
166
188
-- Do what the user originally wanted
167
189
action windows_handle
168
190
0 commit comments