Skip to content

Commit

Permalink
add resume
Browse files Browse the repository at this point in the history
  • Loading branch information
rsoeldner committed Sep 13, 2023
1 parent a3deb2a commit 01c8e0a
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 9 deletions.
3 changes: 3 additions & 0 deletions pact-core/Pact/Core/Builtin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ data RawBuiltin
| RawB64Decode
| RawStrToList
| RawYield
| RawResume
| RawBind
deriving (Eq, Show, Ord, Bounded, Enum)

Expand Down Expand Up @@ -303,6 +304,7 @@ rawBuiltinToText = \case
RawB64Decode -> "base64-decode"
RawStrToList -> "str-to-list"
RawYield -> "yield"
RawResume -> "resume"
RawBind -> "bind"

instance IsBuiltin RawBuiltin where
Expand Down Expand Up @@ -382,6 +384,7 @@ instance IsBuiltin RawBuiltin where
RawB64Decode -> 1
RawStrToList -> 1
RawYield -> 1
RawResume -> 1
RawBind -> 2

rawBuiltinNames :: [Text]
Expand Down
11 changes: 9 additions & 2 deletions pact-core/Pact/Core/Errors.hs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ data EvalError
-- ^ Def is not a closure
| NoSuchKeySet KeySetName
| YieldOutsiteDefPact
| NoActivePactExec
| NoYieldInPactExec
| ContinuePactInvalidContext Integer Integer Integer
deriving Show

instance Pretty EvalError where
Expand Down Expand Up @@ -319,8 +322,12 @@ instance Pretty EvalError where
EvalError txt ->
Pretty.hsep ["Program encountered an unhandled raised error:", pretty txt]
YieldOutsiteDefPact ->
Pretty.hsep ["Scope error: executed yield outsite a defpact"]

"Scope error: executed yield outsite a defpact"
NoActivePactExec ->
"No active pactExec"
NoYieldInPactExec -> "No yield in pact exec"
ContinuePactInvalidContext userStep currStep maxStep ->
Pretty.hsep ["Continue pact step with invalid context: user: ", pretty userStep, ", current: ", pretty currStep, ", max: ", pretty maxStep]
_ -> error "todo: render"


Expand Down
4 changes: 2 additions & 2 deletions pact-core/Pact/Core/IR/Eval/CEK.hs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ mkDefPactClosure
=> FullyQualifiedName
-> DefPact Name Type b i
-> m (CEKValue b i m)
mkDefPactClosure fqn (DefPact _ _ mrty (step :| steps) _) = case step of
mkDefPactClosure fqn (DefPact _ _ mrty (step :| steps) info) = case step of
Step step' _ -> toClosure False step'
StepWithRollback step' _ _ -> toClosure True step'
where
Expand All @@ -217,7 +217,7 @@ mkDefPactClosure fqn (DefPact _ _ mrty (step :| steps) _) = case step of
Lam li args body i ->
let dpc = DefPactClosure fqn li (_argType <$> args) (NE.length args) body rb nSteps mrty i
in pure (VDefPactClosure dpc)
_ -> undefined
_ -> throwExecutionError info (InvariantFailure "Step is not lambda")

-- Todo: fail invariant
nameToFQN :: Applicative f => Name -> f FullyQualifiedName
Expand Down
13 changes: 12 additions & 1 deletion pact-core/Pact/Core/IR/Eval/RawBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -594,9 +594,19 @@ coreYield info b = mkBuiltinFn info b \cont handler -> \case
Nothing -> throwExecutionError info YieldOutsiteDefPact
Just pe -> do
setEvalState esPactExec (Just pe{_peYield = Just (Yield o)})
returnCEKValue cont handler VUnit
returnCEKValue cont handler (VObject o)
args -> argsError info b args

coreResume :: (IsBuiltin b, MonadEval b i m) => i -> b -> NativeFn b i m
coreResume info b = mkBuiltinFn info b \cont handler -> \case
[VClosure clo] -> do
mpe <- useEvalState esPactExec
case mpe of
Nothing -> throwExecutionError info NoActivePactExec
Just pe -> case _peYield pe of
Nothing -> throwExecutionError info NoYieldInPactExec
Just (Yield resumeObj) -> applyLam clo [VObject resumeObj] cont handler
args -> argsError info b args

-----------------------------------
-- try-related ops
Expand Down Expand Up @@ -911,4 +921,5 @@ rawBuiltinLiftedRuntime f i = \case
RawB64Decode -> coreB64Decode i (f RawB64Decode)
RawStrToList -> strToList i (f RawStrToList)
RawYield -> coreYield i (f RawYield)
RawResume -> coreResume i (f RawResume)
RawBind -> coreBind i (f RawBind)
10 changes: 6 additions & 4 deletions pact-core/Pact/Core/Repl/Runtime/ReplBuiltin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Control.Lens
import Pact.Core.Builtin
import Pact.Core.Literal
-- import Pact.Core.Gas
-- import Pact.Core.Errors
import Pact.Core.Errors

import Pact.Core.IR.Eval.Runtime
import Pact.Core.IR.Term
Expand Down Expand Up @@ -104,9 +104,8 @@ continuePact info b = mkReplBuiltinFn info b \cont handler -> \case
Nothing -> pure (VError "No pact exec environment found!")
Just pe -> lookupFqName (pe ^. peContinuation . pcName) >>= \case
Just (DPact dp)
| s >= toInteger (_peStepCount pe) -> undefined
| s <= toInteger (_peStep pe) -> undefined
| otherwise -> do
| s == toInteger (_peStep pe) + 1 &&
s < toInteger (_peStepCount pe) -> do
let
step = _dpSteps dp NE.!! fromInteger s
args' = VPactValue <$> pe ^. peContinuation . pcArgs
Expand All @@ -119,9 +118,12 @@ continuePact info b = mkReplBuiltinFn info b \cont handler -> \case
StepWithRollback s' _rb _ -> toClosure s'
setEvalState esPactExec (Just $ over peStep (+1) pe)
returnCEK cont handler v
| otherwise ->
throwExecutionError info (ContinuePactInvalidContext s (toInteger (_peStep pe)) (toInteger (_peStepCount pe)))
_ -> pure (VError "continuation is not a defpact")
args -> argsError info b args


coreEnvStackFrame :: (IsBuiltin b, Default i) => i -> ReplBuiltin b -> ReplBuiltinFn b i
coreEnvStackFrame info b = mkReplBuiltinFn info b \cont handler -> \case
[_] -> do
Expand Down

0 comments on commit 01c8e0a

Please sign in to comment.