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')