Skip to content

Commit

Permalink
exec script returns repl state
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcardon committed Jan 9, 2025
1 parent 4790e0e commit 98a922f
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pact-lsp/Pact/Core/LanguageServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ setupAndProcessFile nuri content = do
, _replTestResults = []
}
stateRef <- newIORef rstate
res <- runReplT stateRef (processFile Repl.interpretEvalBigStep nuri content)
res <- evalReplM stateRef (processFile Repl.interpretEvalBigStep nuri content)
st <- readIORef stateRef
pure $ (st,) <$> res
where
Expand Down
2 changes: 1 addition & 1 deletion pact-repl/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ main = O.execParser argParser >>= \case
exitEither _ Left {} = die "Load failed"
exitEither m (Right t) = m t >> exitSuccess
exitLoad = exitEither (\_ -> hPutStrLn stderr "Load successful" >> hFlush stderr)
runScript f dolog = execScript dolog f >>= exitLoad
runScript f dolog = execScript dolog f >>= exitLoad . fst
printVersion = putStrLn ("pact version " <> showVersion PI.version)
printBuiltins = traverse_ (\bi -> T.putStrLn $ "\"" <> bi <> "\"") replCoreBuiltinNames

Expand Down
8 changes: 5 additions & 3 deletions pact-repl/Pact/Core/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ import Pact.Core.Serialise
import Pact.Core.Info
import Pact.Core.Errors

execScript :: Bool -> FilePath -> IO (Either (PactError SpanInfo) [ReplCompileValue])
execScript :: Bool -> FilePath -> IO (Either (PactError SpanInfo) [ReplCompileValue], ReplState ReplCoreBuiltin)
execScript dolog f = do
pdb <- mockPactDb serialisePact_repl_spaninfo
ee <- defaultEvalEnv pdb replBuiltinMap
ref <- newIORef (mkReplState ee logger)
runReplT ref $ loadFile f interpretEvalDirect
v <- evalReplM ref $ loadFile f interpretEvalDirect
state <- readIORef ref
pure (v, state)
where
logger :: Text -> EvalM e b i ()
logger
Expand All @@ -55,7 +57,7 @@ runRepl = do
ee <- defaultEvalEnv pdb replBuiltinMap
let display' rcv = runInputT replSettings (displayOutput rcv)
ref <- newIORef (mkReplState ee display')
runReplT ref (runInputT replSettings loop) >>= \case
evalReplM ref (runInputT replSettings loop) >>= \case
Left err -> do
putStrLn "Exited repl session with error:"
putStrLn $ T.unpack $ replError (SourceCode "(interactive)" "") err
Expand Down
7 changes: 3 additions & 4 deletions pact-repl/Pact/Core/Repl/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Pact.Core.Repl.Utils
( ReplDebugFlag(..)
, printDebug
, replFlagSet
, runReplT
, evalReplM
, ReplState(..)
, replFlags
, replEvalEnv
Expand Down Expand Up @@ -204,9 +204,8 @@ replCompletion natives =
dns = defNames ems
in fmap ((renderModuleName mn <> ".") <>) dns

runReplT :: IORef (ReplState b) -> ReplM b a -> IO (Either (PactError SpanInfo) a)
runReplT env st = runEvalMResult (ReplEnv env) def st

evalReplM :: IORef (ReplState b) -> ReplM b a -> IO (Either (PactError SpanInfo) a)
evalReplM env st = runEvalMResult (ReplEnv env) def st

replError
:: SourceCode
Expand Down
2 changes: 1 addition & 1 deletion pact-tests/Pact/Core/Test/GasGolden.hs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,6 @@ runGasTest file interpret = do
let source = SourceCode file src
let rstate = mkReplState ee' (const (pure ())) & replCurrSource .~ source
stateRef <- newIORef rstate
runReplT stateRef (interpret source) >>= \case
evalReplM stateRef (interpret source) >>= \case
Left _ -> pure Nothing
Right _ -> Just <$> readIORef gasRef
2 changes: 1 addition & 1 deletion pact-tests/Pact/Core/Test/ReplTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ runReplTest (ReplSourceDir path) pdb file src interp = do
let source = SourceCode (path </> file) src
let rstate = mkReplState ee (const (pure ())) & replCurrSource .~ source
stateRef <- newIORef rstate
runReplT stateRef (interp source) >>= \case
evalReplM stateRef (interp source) >>= \case
Left e -> let
rendered = replError (SourceCode file src) e
in assertFailure (T.unpack rendered)
Expand Down
2 changes: 1 addition & 1 deletion pact-tests/Pact/Core/Test/StaticErrorTests.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ runStaticTest label src interp predicate = do
& replCurrSource .~ source
& replNativesEnabled .~ True
stateRef <- newIORef rstate
v <- runReplT stateRef (interpretReplProgram interp source)
v <- evalReplM stateRef (interpretReplProgram interp source)
case v of
Left err ->
assertBool ("Expected Error to match predicate, but got " <> show err <> " instead") (predicate err)
Expand Down

0 comments on commit 98a922f

Please sign in to comment.