Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions include/chainblocks.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ enum CBType : uint8_t {

enum CBChainState : uint8_t {
Continue, // Nothing happened, continue
Rebase, // Continue this chain but put the local chain initial input as next
// input
Restart, // Restart the local chain from the top (notice not the root!)
Return, // Used in conditional paths, end this chain and return previous
// output
Stop, // Stop the chain execution (including root)
Return, // Control flow, end this chain/flow and return previous output
Rebase, // Continue but put the local chain initial input as next input
Restart, // Restart the current chain from the top (non inline chains)
Stop // Stop the flow execution
};

// These blocks run fully inline in the runchain threaded execution engine
Expand Down Expand Up @@ -476,20 +474,14 @@ struct CBFlow {

struct CBVarPayload {
union {
#if defined(__cplusplus) || defined(CB_USE_ENUMS)
enum CBChainState chainState;
#else
CBChainState chainState;
#endif
CBBool boolValue;

struct {
CBPointer objectValue;
int32_t objectVendorId;
int32_t objectTypeId;
};

CBBool boolValue;

CBInt intValue;
CBInt2 int2Value;
CBInt3 int3Value;
Expand Down Expand Up @@ -776,7 +768,7 @@ typedef void(__cdecl *CBThrowException)(const char *errorText)

typedef void(__cdecl *CBThrowExceptionSimple)() __attribute__((noreturn));

typedef struct CBVar(__cdecl *CBSuspend)(struct CBContext *context,
typedef CBChainState(__cdecl *CBSuspend)(struct CBContext *context,
double seconds);

typedef void(__cdecl *CBCloneVar)(struct CBVar *dst, const struct CBVar *src);
Expand All @@ -800,9 +792,19 @@ typedef struct CBValidationResult(__cdecl *CBValidateBlocks)(
CBlocks blocks, CBValidationCallback callback, void *userData,
struct CBInstanceData data);

typedef struct CBVar(__cdecl *CBRunBlocks)(CBlocks blocks,
#if defined(__cplusplus) || defined(CB_USE_ENUMS)
typedef enum CBChainState(__cdecl *CBRunBlocks)(CBlocks blocks,
struct CBContext *context,
struct CBVar input,
struct CBVar *output,
const CBBool handleReturn);
#else
typedef CBChainState(__cdecl *CBRunBlocks)(CBlocks blocks,
struct CBContext *context,
struct CBVar input);
struct CBVar input,
struct CBVar *output,
const CBBool handleReturn);
#endif

typedef void(__cdecl *CBLog)(const char *msg);

Expand Down Expand Up @@ -892,8 +894,6 @@ struct CBCore {
// before calling any of those make sure to release
// and call destructors manually!
CBThrowException throwException;
CBThrowExceptionSimple throwCancellation;
CBThrowExceptionSimple throwRestart;
// To be used within blocks, to suspend the coroutine
CBSuspend suspend;

Expand Down
72 changes: 4 additions & 68 deletions include/chainblocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,6 @@ class ActivationError : public CBException {
bool fatal;
};

class ChainCancellation : public ActivationError {
public:
ChainCancellation() : ActivationError("", CBChainState::Stop, false) {}
};

using ChainStop = ChainCancellation;

class ChainRestart : public ActivationError {
public:
ChainRestart() : ActivationError("", CBChainState::Restart, false) {}
};

class ComposeError : public CBException {
public:
explicit ComposeError(std::string_view msg, bool fatal = true)
Expand Down Expand Up @@ -205,10 +193,7 @@ struct Parameters {
};

struct Var : public CBVar {
explicit Var() : CBVar() {
valueType = None;
payload.chainState = CBChainState::Continue;
}
explicit Var() : CBVar() { valueType = None; }

explicit Var(const CBVar &other) {
memcpy((void *)this, (void *)&other, sizeof(CBVar));
Expand Down Expand Up @@ -276,58 +261,9 @@ struct Var : public CBVar {
}
}

constexpr static CBVar Empty() {
CBVar res{};
return res;
}

constexpr static CBVar True() {
CBVar res{};
res.valueType = CBType::Bool;
// notice we need to use chain state
// as this is the active member
// should be fixed in c++20 tho
res.payload.chainState = (CBChainState)1;
return res;
}

constexpr static CBVar False() {
CBVar res{};
res.valueType = CBType::Bool;
// notice we need to use chain state
// as this is the active member
// should be fixed in c++20 tho
res.payload.chainState = (CBChainState)0;
return res;
}

constexpr static CBVar Stop() {
CBVar res{};
res.valueType = None;
res.payload.chainState = CBChainState::Stop;
return res;
}

constexpr static CBVar Restart() {
CBVar res{};
res.valueType = None;
res.payload.chainState = CBChainState::Restart;
return res;
}

constexpr static CBVar Return() {
CBVar res{};
res.valueType = None;
res.payload.chainState = CBChainState::Return;
return res;
}

constexpr static CBVar Rebase() {
CBVar res{};
res.valueType = None;
res.payload.chainState = CBChainState::Rebase;
return res;
}
constexpr static CBVar Empty{};
constexpr static CBVar True{{true}, {nullptr}, 0, CBType::Bool};
constexpr static CBVar False{{false}, {nullptr}, 0, CBType::Bool};

template <typename T>
static Var Object(T valuePtr, uint32_t objectVendorId,
Expand Down
16 changes: 5 additions & 11 deletions include/dllblock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,7 @@ class Core {
sCore._core.throwException(errorText);
}

[[noreturn]] static void throwCancellation(const char *errorText) {
sCore._core.throwCancellation();
}

[[noreturn]] static void throwRestart(const char *errorText) {
sCore._core.throwRestart();
}

static CBVar suspend(CBContext *context, double seconds) {
static CBChainState suspend(CBContext *context, double seconds) {
return sCore._core.suspend(context, seconds);
}

Expand Down Expand Up @@ -180,8 +172,10 @@ class Core {
return sCore._core.validateBlocks(blocks, callback, userData, data);
}

static CBVar runBlocks(CBlocks blocks, CBContext *context, CBVar input) {
return sCore._core.runBlocks(blocks, context, input);
static CBChainState runBlocks(CBlocks blocks, CBContext *context, CBVar input,
CBVar *output,
const bool handleReturn = false) {
return sCore._core.runBlocks(blocks, context, input, output, handleReturn);
}

static void log(const char *msg) { sCore._core.log(msg); }
Expand Down
11 changes: 3 additions & 8 deletions include/ops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,12 @@ ALWAYS_INLINE inline bool operator==(const CBVar &a, const CBVar &b) {
return false;

switch (a.valueType) {
case None:
case CBType::Any:
case EndOfBlittableTypes:
return true;
case StackIndex:
return a.payload.stackIndexValue == b.payload.stackIndexValue;
case None:
return a.payload.chainState == b.payload.chainState;
case Object:
return a.payload.objectValue == b.payload.objectValue;
case Enum:
Expand Down Expand Up @@ -403,8 +402,6 @@ ALWAYS_INLINE inline bool operator<(const CBVar &a, const CBVar &b) {
return false;

switch (a.valueType) {
case None:
return a.payload.chainState < b.payload.chainState;
case StackIndex:
return a.payload.stackIndexValue < b.payload.stackIndexValue;
case Enum:
Expand Down Expand Up @@ -506,6 +503,7 @@ ALWAYS_INLINE inline bool operator<(const CBVar &a, const CBVar &b) {
case Block:
case Object:
case CBType::Any:
case None:
case EndOfBlittableTypes:
throw chainblocks::InvalidVarTypeError(
"Comparison operator < not supported for the given type: " +
Expand Down Expand Up @@ -597,8 +595,6 @@ ALWAYS_INLINE inline bool operator<=(const CBVar &a, const CBVar &b) {
return false;

switch (a.valueType) {
case None:
return a.payload.chainState <= b.payload.chainState;
case StackIndex:
return a.payload.stackIndexValue <= b.payload.stackIndexValue;
case Enum:
Expand Down Expand Up @@ -700,6 +696,7 @@ ALWAYS_INLINE inline bool operator<=(const CBVar &a, const CBVar &b) {
case Block:
case Object:
case CBType::Any:
case None:
case EndOfBlittableTypes:
throw chainblocks::InvalidVarTypeError(
"Comparison operator <= not supported for the given type: " +
Expand Down Expand Up @@ -873,8 +870,6 @@ template <> struct hash<CBVar> {
auto res = hash<int>()(int(var.valueType));
switch (var.valueType) {
case None:
MAGIC_HASH(var.payload.chainState);
break;
case Any:
break;
case Object:
Expand Down
50 changes: 13 additions & 37 deletions include/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ template <class CB_CORE> class TBlocksVar {
return _chainValidation;
}

CBVar activate(CBContext *context, const CBVar &input) {
return CB_CORE::runBlocks(_blocks, context, input);
CBChainState activate(CBContext *context, const CBVar &input, CBVar &output,
const bool handleReturn = false) {
return CB_CORE::runBlocks(_blocks, context, input, &output, handleReturn);
}

operator bool() const { return _blocksArray.size() > 0; }
Expand Down Expand Up @@ -285,14 +286,9 @@ template <class CB_CORE> struct AsyncOp {
// Wait suspending!
while (true) {
auto state = asyncRes.wait_for(std::chrono::seconds(0));
if (state == std::future_status::ready)
if (state == std::future_status::ready ||
CB_CORE::suspend(_context, 0) != CBChainState::Continue)
break;
auto chainState = CB_CORE::suspend(_context, 0);
if (chainState.payload.chainState == Restart) {
CB_CORE::throwRestart();
} else if (chainState.payload.chainState != Continue) {
CB_CORE::throwCancellation();
}
}
// This should also throw if we had exceptions
return asyncRes.get();
Expand All @@ -301,14 +297,9 @@ template <class CB_CORE> struct AsyncOp {
CBVar operator()(std::future<CBVar> &fut) {
while (true) {
auto state = fut.wait_for(std::chrono::seconds(0));
if (state == std::future_status::ready)
if (state == std::future_status::ready ||
CB_CORE::suspend(_context, 0) != CBChainState::Continue)
break;
auto chainState = CB_CORE::suspend(_context, 0);
if (chainState.payload.chainState == Restart) {
CB_CORE::throwRestart();
} else if (chainState.payload.chainState != Continue) {
CB_CORE::throwCancellation();
}
}
// This should also throw if we had exceptions
return fut.get();
Expand All @@ -317,14 +308,9 @@ template <class CB_CORE> struct AsyncOp {
void operator()(std::future<void> &fut) {
while (true) {
auto state = fut.wait_for(std::chrono::seconds(0));
if (state == std::future_status::ready)
if (state == std::future_status::ready ||
CB_CORE::suspend(_context, 0) != CBChainState::Continue)
break;
auto chainState = CB_CORE::suspend(_context, 0);
if (chainState.payload.chainState == Restart) {
CB_CORE::throwRestart();
} else if (chainState.payload.chainState != Continue) {
CB_CORE::throwCancellation();
}
}
// This should also throw if we had exceptions
fut.get();
Expand Down Expand Up @@ -354,14 +340,9 @@ template <class CB_CORE> struct AsyncOp {

while (true) {
auto state = fut.wait_for(std::chrono::seconds(0));
if (state == std::future_status::ready)
if (state == std::future_status::ready ||
CB_CORE::suspend(_context, 0) != CBChainState::Continue)
break;
auto chainState = CB_CORE::suspend(_context, 0);
if (chainState.payload.chainState == Restart) {
CB_CORE::throwRestart();
} else if (chainState.payload.chainState != Continue) {
CB_CORE::throwCancellation();
}
}

if (p)
Expand Down Expand Up @@ -395,14 +376,9 @@ template <class CB_CORE> struct AsyncOp {

while (true) {
auto state = fut.wait_for(std::chrono::seconds(0));
if (state == std::future_status::ready)
if (state == std::future_status::ready ||
CB_CORE::suspend(_context, 0) != CBChainState::Continue)
break;
auto chainState = CB_CORE::suspend(_context, 0);
if (chainState.payload.chainState == Restart) {
CB_CORE::throwRestart();
} else if (chainState.payload.chainState != Continue) {
CB_CORE::throwCancellation();
}
}

if (p)
Expand Down
Loading