Skip to content
This repository has been archived by the owner on Oct 16, 2022. It is now read-only.

Commit

Permalink
Newtype wrapper on udata.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyjh authored and valpackett committed Jul 5, 2018
1 parent 3a2e2f5 commit 76e67ea
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 0 additions & 1 deletion Setup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import Data.List (isPrefixOf)
import Distribution.Simple
-- import Distribution.Simple.Setup (BuildFlags (..), Flag (..))
import Distribution.Types.HookedBuildInfo
import System.Directory
import System.Exit (ExitCode (..))
Expand Down
12 changes: 7 additions & 5 deletions library/Scripting/Duktape/Raw.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type TimeoutCheck = IO Bool
type TimeoutCheckWrapped = FunPtr (IO Bool)
type CheckActionUData = Ptr TimeoutCheckWrapped

newtype InternalUData = InternalUData { getInternalUData :: Ptr () }

-- Static callback
foreign export ccall "hsduk_exec_timeout_check" execTimeoutCheck DukExecTimeoutCheckFunction

Expand Down Expand Up @@ -174,15 +176,15 @@ foreign import capi safe "duktape.h duk_push_context_dump"

-------------------------------------------------------------------------------------------------------

createHeap FunPtr DukAllocFunction FunPtr DukReallocFunction FunPtr DukFreeFunction Ptr () FunPtr DukFatalFunction IO (Maybe DuktapeCtx)
createHeap FunPtr DukAllocFunction FunPtr DukReallocFunction FunPtr DukFreeFunction InternalUData FunPtr DukFatalFunction IO (Maybe DuktapeCtx)
createHeap allocf reallocf freef udata fatalf = do
ptr c_duk_create_heap allocf reallocf freef udata fatalf
ptr c_duk_create_heap allocf reallocf freef (getInternalUData udata) fatalf
if ptr /= nullPtr
then newForeignPtr ptr (c_duk_destroy_heap ptr) >>= newMVar >>= return . Just
else return Nothing

createHeapF FunPtr DukFatalFunction IO (Maybe DuktapeCtx)
createHeapF = createHeap nullFunPtr nullFunPtr nullFunPtr nullPtr
createHeapF = createHeap nullFunPtr nullFunPtr nullFunPtr (InternalUData nullPtr)

-- | A TimeoutCheck is an IO action that returns True when the current script evaluation
-- should timeout (interpreter throws RangeError).
Expand All @@ -198,10 +200,10 @@ createGovernedHeap allocf reallocf freef timeoutCheck fatalf = do
where
-- TimeoutCheck is wrapped to pass as void* udata in `createHeap` and will be provided (by duktape)
-- back to `execTimeoutCheck` when the interpreter invokes that callback.
wrapTimeoutCheckUData TimeoutCheck IO (Ptr (), IO ())
wrapTimeoutCheckUData TimeoutCheck IO (InternalUData, IO ())
wrapTimeoutCheckUData check = do
wrapped wrapTimeoutCheck check
ptr malloc
poke ptr wrapped
let finalizers = free ptr >> freeHaskellFunPtr wrapped
return (castPtr ptr, finalizers)
return (InternalUData $ castPtr ptr, finalizers)

0 comments on commit 76e67ea

Please sign in to comment.