aboutsummaryrefslogtreecommitdiff
path: root/montis/src/Montis/Core/Start.hs
blob: 54ec8c52d6c7e0c1efe18683f7a4b3b92ae018a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module Montis.Core.Start where

import Data.ByteString qualified as BS
import Data.ByteString.Char8 qualified as CH
import Data.Void
import Foreign (Word32, newStablePtr)
import Foreign.C (CChar)
import Foreign.Ptr
import Montis.Core.Monad
import Montis.Core.State
import Montis.Core.State.Marshal (demarshalState)

type MontisColdStart = Ptr Void -> IO OpqStT

type MontisHotStart = Ptr Void -> Ptr CChar -> Word32 -> IO OpqStT

hotStartMontis :: MontisConfig -> MontisHotStart
hotStartMontis config self chars len = do
  bs <- BS.packCStringLen (chars, fromIntegral len)

  let ctx = Context config (SelfPtr self)
      st = demarshalState config (CH.unpack bs)

  ((), st') <- runMontis ctx st (resetHook config)
  newStablePtr (ctx, st')

-- Used to start montis given the provided config.
coldStartMontis :: MontisConfig -> MontisColdStart
coldStartMontis conf selfPtr =
  let ctx = Context conf (SelfPtr selfPtr)
      st =
        State
          { currentHooks = startingHooks conf,
            extensibleState = mempty
          }
   in do
        ((), st') <- runMontis ctx st (startupHook conf)
        newStablePtr (ctx, st')