Skip to content

Commit

Permalink
Add option for making foreign calls safe
Browse files Browse the repository at this point in the history
  • Loading branch information
expipiplus1 committed Apr 22, 2018
1 parent 95e506d commit c2b6328
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions generate/src/Write/Cabal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ writeCabal modules platforms guardInfo =

{vcat $ writePlatformFlag <$> platforms}

flag safe-foreign-calls
description:
Do not mark foreign imports as 'unsafe'. This means that
callbacks from Vulkan to Haskell will work. If you are using
these then make sure this flag is enabled.
default: False

library
hs-source-dirs: src
-- We need to use cpphs, as regular cpp ruins latex math with lines
Expand All @@ -64,6 +71,9 @@ writeCabal modules platforms guardInfo =
build-depends: cpphs
exposed-modules: {indent (-2) . vcat . intercalatePrepend "," $ pretty . mName <$> unguardedModules}

if flag(safe-foreign-calls)
cpp-options: -DSAFE_FOREIGN_CALLS

{indent 0 . vcat $ writeGuardedModules <$> guardGroups}

build-depends: base >= 4.9 && < 5
Expand Down
6 changes: 5 additions & 1 deletion generate/src/Write/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ commandDoc c@Command {..} = do
(t, (is, es)) <- toHsType (commandType c)
let d getDoc = [qci|
{document getDoc (TopLevel cName)}
foreign import ccall "{cName}" {cName} :: {t}
foreign import ccall
#if !defined(SAFE_FOREIGN_CALLS)
unsafe
#endif
"{cName}" {cName} :: {t}
|]
pure (d, is, es)
6 changes: 5 additions & 1 deletion generate/src/Write/Loader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ writeFunction gm domain c@Command{..} = do
d= [qci|
{dropVk cName} :: {domain}Cmds -> {t}
{dropVk cName} deviceCmds = mk{upperCaseName} (p{upperCaseName} deviceCmds)
foreign import ccall unsafe "dynamic" mk{upperCaseName}
foreign import ccall
#if !defined(SAFE_FOREIGN_CALLS)
unsafe
#endif
"dynamic" mk{upperCaseName}
:: FunPtr {t} -> {t}
|]
pure ((d, gm =<< cPlatform), is, es)
Expand Down

0 comments on commit c2b6328

Please sign in to comment.