Skip to content

Commit

Permalink
Fix repl trace vs print
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcardon committed Feb 24, 2025
1 parent 6a0cf3e commit b7ff15a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 16 deletions.
3 changes: 2 additions & 1 deletion pact-lsp/Pact/Core/LanguageServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ setupAndProcessFile nuri content = do
, _replLoad = doLoad
, _replLogType = ReplStdOut
, _replLoadedFiles = mempty
, _replOutputLine = const $ const $ pure ()
, _replTraceLine = const $ const $ pure ()
, _replPrintLine = const $ const $ pure ()
, _replTestResults = []
}
stateRef <- newIORef rstate
Expand Down
4 changes: 2 additions & 2 deletions pact-repl/Pact/Core/IR/Eval/Direct/ReplBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ envSetDebug info b _env = \case
let flagsToSet = S.difference (S.union currFlags flags) (S.intersection currFlags flags)
replFlags .== flagsToSet
pure flagsToSet
replPrintLn' info $ renderCompactText' $ "set debug flags to " <> pretty (S.toList flagsSet)
replTraceLn' info $ renderCompactText' $ "set debug flags to " <> pretty (S.toList flagsSet)
return VUnit
args -> argsError info b args

Expand Down Expand Up @@ -583,7 +583,7 @@ load info b _env = \case
args -> argsError info b args
where
load' sourceFile reset = do
replPrintLn info $ PString $ "Loading " <> sourceFile <> "..."
replTraceLn info $ PString $ "Loading " <> sourceFile <> "..."
fload <- useReplState replLoad
fload (T.unpack sourceFile) reset
return VUnit
Expand Down
17 changes: 12 additions & 5 deletions pact-repl/Pact/Core/Repl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ import Pact.Core.Info
import Pact.Core.Errors

execScript :: Bool -> FilePath -> IO (Either (PactError FileLocSpanInfo) [ReplCompileValue], ReplState ReplCoreBuiltin)
execScript dolog f = do
execScript traceEnabled f = do
pdb <- mockPactDb serialisePact_repl_fileLocSpanInfo
ee <- defaultEvalEnv pdb replBuiltinMap
ref <- newIORef (mkReplState' ee logger)
let replState = mkReplState' ee printLogger & replTraceLine .~ traceLogger
ref <- newIORef replState
v <- evalReplM ref $ loadFile interpretEvalDirect f True
state <- readIORef ref
pure (v, state)
where
logger :: FileLocSpanInfo -> Text -> EvalM e b i ()
logger (FileLocSpanInfo file info) v
| dolog = liftIO $ T.putStrLn $ T.pack file <> ":" <> renderCompactText info <> ": " <> v
logWithTrace traceType (FileLocSpanInfo file info) v =
liftIO $ T.putStrLn $ T.concat [T.pack file, ":", renderCompactText info, ":", traceType, ": ", v]
printLogger :: FileLocSpanInfo -> Text -> EvalM e b i ()
printLogger floc v
| traceEnabled = logWithTrace "Print" floc v
| otherwise = liftIO $ T.putStrLn v
traceLogger :: FileLocSpanInfo -> Text -> EvalM e b i ()
traceLogger floc v
| traceEnabled = logWithTrace "Trace" floc v
| otherwise = pure ()

-- | Render a nice error
Expand Down
8 changes: 5 additions & 3 deletions pact-repl/Pact/Core/Repl/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ mkReplState ee printfn loadFn =
, _replTLDefPos = mempty
, _replTx = Nothing
, _replNativesEnabled = False
, _replOutputLine = printfn
, _replTraceLine = printfn
, _replPrintLine = printfn
, _replLoad = loadFn
, _replLoadedFiles = mempty
, _replTestResults = []
Expand All @@ -121,7 +122,8 @@ mkReplState' ee printfn =
, _replTLDefPos = mempty
, _replTx = Nothing
, _replNativesEnabled = False
, _replOutputLine = printfn
, _replTraceLine = printfn
, _replPrintLine = printfn
, _replLoad = \f reset -> void (loadFile interpretEvalDirect f reset)
, _replLoadedFiles = mempty
, _replTestResults = []
Expand Down Expand Up @@ -309,7 +311,7 @@ interpretReplProgram interpreter sc@(SourceCode sourceFp source) = do
| otherwise = Lisp.parseReplProgram lexerOutput
displayValue :: FileLocSpanInfo -> ReplCompileValue -> ReplM ReplCoreBuiltin ReplCompileValue
displayValue _info v@(RCompileValue (InterpretValue PUnit _)) = pure v
displayValue info p = p <$ replPrintLn info p
displayValue info p = p <$ replTraceLn info p
sliceCode = \case
Lisp.TLModule{} -> sliceFromSource
Lisp.TLInterface{} -> sliceFromSource
Expand Down
4 changes: 2 additions & 2 deletions pact-repl/Pact/Core/Repl/Runtime/ReplBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ envSetDebug info b cont handler _env = \case
let flagsToSet = S.difference (S.union currFlags flags) (S.intersection currFlags flags)
replFlags .== flagsToSet
pure flagsToSet
replPrintLn' info $ renderCompactText' $ "set debug flags to " <> pretty (S.toList flagsSet)
replTraceLn' info $ renderCompactText' $ "set debug flags to " <> pretty (S.toList flagsSet)
returnCEKValue cont handler $ VUnit
args -> argsError info b args

Expand Down Expand Up @@ -614,7 +614,7 @@ load info b cont handler _env = \case
args -> argsError info b args
where
load' sourceFile reset = do
replPrintLn info $ PString $ "Loading " <> sourceFile <> "..."
replTraceLn info $ PString $ "Loading " <> sourceFile <> "..."
fload <- useReplState replLoad
fload (T.unpack sourceFile) reset
returnCEKValue cont handler VUnit
Expand Down
15 changes: 14 additions & 1 deletion pact-repl/Pact/Core/Repl/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ module Pact.Core.Repl.Utils
, gasLogEntrytoPactValue
, replPrintLn
, replPrintLn'
, replTraceLn
, replTraceLn'
, recordTestSuccess
, recordTestFailure
) where
Expand Down Expand Up @@ -245,14 +247,25 @@ gasLogEntrytoPactValue entry = PString $ renderCompactText' $ n <> ": " <> prett
where
n = pretty (_gleArgs entry) <+> pretty (_gleInfo entry)

replTraceLn :: Pretty a => FileLocSpanInfo -> a -> EvalM 'ReplRuntime b FileLocSpanInfo ()
replTraceLn info p = replTraceLn' info (renderCompactText p)

replTraceLn' :: FileLocSpanInfo -> Text -> EvalM 'ReplRuntime b FileLocSpanInfo ()
replTraceLn' info p = do
r <- getReplState
case _replLogType r of
ReplStdOut -> _replTraceLine r info p
ReplLogOut v ->
liftIO (modifyIORef' v ((p, info):))

replPrintLn :: Pretty a => FileLocSpanInfo -> a -> EvalM 'ReplRuntime b FileLocSpanInfo ()
replPrintLn info p = replPrintLn' info (renderCompactText p)

replPrintLn' :: FileLocSpanInfo -> Text -> EvalM 'ReplRuntime b FileLocSpanInfo ()
replPrintLn' info p = do
r <- getReplState
case _replLogType r of
ReplStdOut -> _replOutputLine r info p
ReplStdOut -> _replPrintLine r info p
ReplLogOut v ->
liftIO (modifyIORef' v ((p, info):))

Expand Down
10 changes: 8 additions & 2 deletions pact/Pact/Core/Environment/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ module Pact.Core.Environment.Types
, replNativesEnabled
, replCurrSource
, replTx
, replOutputLine
, replTraceLine
, replPrintLine
, replTestResults
, replLoad
, replLoadedFiles
Expand Down Expand Up @@ -382,7 +383,11 @@ data ReplOutput where
ReplStdOut :: ReplOutput
ReplLogOut :: IORef [(Text, FileLocSpanInfo)] -> ReplOutput

-- | The type of a
type OutputWithLoc b = FileLocSpanInfo -> Text -> EvalM 'ReplRuntime b FileLocSpanInfo ()

-- | Passed in repl environment
-- TODO: move to Repl.Types
data ReplState b
= ReplState
{ _replFlags :: Set ReplDebugFlag
Expand All @@ -403,7 +408,8 @@ data ReplState b
-- ^ The current repl tx, if one has been initiated
, _replNativesEnabled :: Bool
-- ^ Are repl natives enabled in module code
, _replOutputLine :: !(FileLocSpanInfo -> Text -> EvalM 'ReplRuntime b FileLocSpanInfo ())
, _replTraceLine :: !(OutputWithLoc b)
, _replPrintLine :: !(OutputWithLoc b)
-- ^ The output line function, as an entry in the repl env
-- to allow for custom output handling, e.g haskeline
, _replLoad :: !(FilePath -> Bool -> EvalM 'ReplRuntime b FileLocSpanInfo ())
Expand Down

0 comments on commit b7ff15a

Please sign in to comment.