---
source: framec/tests/c_snapshots.rs
expression: "compile_fixture(\"10_actions\", \"c\")"
---
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>

// ============================================================================
// Actions_FrameDict - String-keyed dictionary
// ============================================================================

typedef struct Actions_FrameDictEntry {
    char* key;
    void* value;
    struct Actions_FrameDictEntry* next;
} Actions_FrameDictEntry;

typedef struct {
    Actions_FrameDictEntry** buckets;
    int bucket_count;
    int size;
} Actions_FrameDict;

static unsigned int Actions_hash_string(const char* str) {
    unsigned int hash = 5381;
    int c;
    while ((c = *str++)) {
        hash = ((hash << 5) + hash) + c;
    }
    return hash;
}

static Actions_FrameDict* Actions_FrameDict_new(void) {
    Actions_FrameDict* d = malloc(sizeof(Actions_FrameDict));
    d->bucket_count = 16;
    d->buckets = calloc(d->bucket_count, sizeof(Actions_FrameDictEntry*));
    d->size = 0;
    return d;
}

static void Actions_FrameDict_set(Actions_FrameDict* d, const char* key, void* value) {
    unsigned int idx = Actions_hash_string(key) % d->bucket_count;
    Actions_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            entry->value = value;
            return;
        }
        entry = entry->next;
    }
    Actions_FrameDictEntry* new_entry = malloc(sizeof(Actions_FrameDictEntry));
    new_entry->key = strdup(key);
    new_entry->value = value;
    new_entry->next = d->buckets[idx];
    d->buckets[idx] = new_entry;
    d->size++;
}

static void* Actions_FrameDict_get(Actions_FrameDict* d, const char* key) {
    unsigned int idx = Actions_hash_string(key) % d->bucket_count;
    Actions_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return entry->value;
        }
        entry = entry->next;
    }
    return NULL;
}

static int Actions_FrameDict_has(Actions_FrameDict* d, const char* key) {
    unsigned int idx = Actions_hash_string(key) % d->bucket_count;
    Actions_FrameDictEntry* entry = d->buckets[idx];
    while (entry) {
        if (strcmp(entry->key, key) == 0) {
            return 1;
        }
        entry = entry->next;
    }
    return 0;
}

static Actions_FrameDict* Actions_FrameDict_copy(Actions_FrameDict* src) {
    Actions_FrameDict* dst = Actions_FrameDict_new();
    for (int i = 0; i < src->bucket_count; i++) {
        Actions_FrameDictEntry* entry = src->buckets[i];
        while (entry) {
            Actions_FrameDict_set(dst, entry->key, entry->value);
            entry = entry->next;
        }
    }
    return dst;
}

static void Actions_FrameDict_destroy(Actions_FrameDict* d) {
    for (int i = 0; i < d->bucket_count; i++) {
        Actions_FrameDictEntry* entry = d->buckets[i];
        while (entry) {
            Actions_FrameDictEntry* next = entry->next;
            free(entry->key);
            free(entry);
            entry = next;
        }
    }
    free(d->buckets);
    free(d);
}

// ============================================================================
// Actions_FrameVec - Dynamic array
// ============================================================================

typedef struct {
    void** items;
    int size;
    int capacity;
} Actions_FrameVec;

static Actions_FrameVec* Actions_FrameVec_new(void) {
    Actions_FrameVec* v = malloc(sizeof(Actions_FrameVec));
    v->capacity = 8;
    v->size = 0;
    v->items = malloc(sizeof(void*) * v->capacity);
    return v;
}

static void Actions_FrameVec_push(Actions_FrameVec* v, void* item) {
    if (v->size >= v->capacity) {
        v->capacity *= 2;
        v->items = realloc(v->items, sizeof(void*) * v->capacity);
    }
    v->items[v->size++] = item;
}

static void* Actions_FrameVec_pop(Actions_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[--v->size];
}

static void* Actions_FrameVec_last(Actions_FrameVec* v) {
    if (v->size == 0) return NULL;
    return v->items[v->size - 1];
}

static void* Actions_FrameVec_get(Actions_FrameVec* v, int index) {
    if (index < 0 || index >= v->size) return NULL;
    return v->items[index];
}

static int Actions_FrameVec_size(Actions_FrameVec* v) {
    return v->size;
}

static void Actions_FrameVec_destroy(Actions_FrameVec* v) {
    if (!v) return;
    free(v->items);
    free(v);
}

static Actions_FrameVec* Actions_FrameVec_copy(Actions_FrameVec* src) {
    if (!src) return NULL;
    Actions_FrameVec* v = Actions_FrameVec_new();
    for (int i = 0; i < src->size; i++) {
        Actions_FrameVec_push(v, src->items[i]);
    }
    return v;
}

// ============================================================================
// Actions_pack_double / Actions_unpack_double — bit-pun doubles through void*
// ============================================================================

static inline void* Actions_pack_double(double v) {
    void* p = 0;
    memcpy(&p, &v, sizeof(double));
    return p;
}

static inline double Actions_unpack_double(void* p) {
    double d;
    memcpy(&d, &p, sizeof(double));
    return d;
}

// ============================================================================
// Actions_FrameEvent - Event routing object
// ============================================================================

typedef struct {
    const char* _message;
    Actions_FrameVec* _parameters;
    int _owns_parameters;
} Actions_FrameEvent;

static Actions_FrameEvent* Actions_FrameEvent_new(const char* message, Actions_FrameVec* parameters, int owns_parameters) {
    Actions_FrameEvent* e = malloc(sizeof(Actions_FrameEvent));
    e->_message = message;
    e->_parameters = parameters;
    e->_owns_parameters = owns_parameters;
    return e;
}

static void Actions_FrameEvent_destroy(Actions_FrameEvent* e) {
    if (e->_owns_parameters && e->_parameters) Actions_FrameVec_destroy(e->_parameters);
    free(e);
}

// ============================================================================
// Actions_FrameContext - Interface call context
// ============================================================================

typedef struct {
    Actions_FrameEvent* event;
    void* _return;
    Actions_FrameDict* _data;
    int _transitioned;
} Actions_FrameContext;

static Actions_FrameContext* Actions_FrameContext_new(Actions_FrameEvent* event, void* default_return) {
    Actions_FrameContext* ctx = malloc(sizeof(Actions_FrameContext));
    ctx->event = event;
    ctx->_return = default_return;
    ctx->_data = Actions_FrameDict_new();
    ctx->_transitioned = 0;
    return ctx;
}

static void Actions_FrameContext_destroy(Actions_FrameContext* ctx) {
    Actions_FrameDict_destroy(ctx->_data);
    free(ctx);
}

// ============================================================================
// Actions_Compartment - State closure
// ============================================================================

typedef struct Actions_Compartment {
    const char* state;
    Actions_FrameVec* state_args;
    Actions_FrameDict* state_vars;
    Actions_FrameVec* enter_args;
    Actions_FrameVec* exit_args;
    Actions_FrameEvent* forward_event;
    struct Actions_Compartment* parent_compartment;
    int _ref_count;
} Actions_Compartment;

static Actions_Compartment* Actions_Compartment_new(const char* state) {
    Actions_Compartment* c = malloc(sizeof(Actions_Compartment));
    c->state = state;
    c->state_args = Actions_FrameVec_new();
    c->state_vars = Actions_FrameDict_new();
    c->enter_args = Actions_FrameVec_new();
    c->exit_args = Actions_FrameVec_new();
    c->forward_event = NULL;
    c->parent_compartment = NULL;
    c->_ref_count = 1;
    return c;
}

static Actions_Compartment* Actions_Compartment_ref(Actions_Compartment* c) {
    if (c) c->_ref_count++;
    return c;
}

static void Actions_Compartment_unref(Actions_Compartment* c);
static void Actions_Compartment_unref(Actions_Compartment* c) {
    if (c == NULL) return;
    c->_ref_count--;
    if (c->_ref_count <= 0) {
        Actions_Compartment_unref(c->parent_compartment);
        Actions_FrameVec_destroy(c->state_args);
        Actions_FrameDict_destroy(c->state_vars);
        Actions_FrameVec_destroy(c->enter_args);
        Actions_FrameVec_destroy(c->exit_args);
        free(c);
    }
}

static Actions_Compartment* Actions_Compartment_copy(Actions_Compartment* src) {
    Actions_Compartment* c = malloc(sizeof(Actions_Compartment));
    c->state = src->state;
    c->state_args = Actions_FrameVec_copy(src->state_args);
    c->state_vars = Actions_FrameDict_copy(src->state_vars);
    c->enter_args = Actions_FrameVec_copy(src->enter_args);
    c->exit_args = Actions_FrameVec_copy(src->exit_args);
    c->forward_event = src->forward_event;  // Shallow copy OK
    c->parent_compartment = src->parent_compartment;
    return c;
}

static void Actions_Compartment_destroy(Actions_Compartment* c) {
    Actions_FrameVec_destroy(c->state_args);
    Actions_FrameDict_destroy(c->state_vars);
    Actions_FrameVec_destroy(c->enter_args);
    Actions_FrameVec_destroy(c->exit_args);
    free(c);
}

// Helper macros for context access
#define Actions_CTX(self) ((Actions_FrameContext*)Actions_FrameVec_last((self)->_context_stack))
#define Actions_PARAM(self, key) Actions_FrameDict_get(Actions_CTX(self)->event->_parameters, key)
#define Actions_RETURN(self) Actions_CTX(self)->_return
#define Actions_DATA(self, key) Actions_FrameDict_get(Actions_CTX(self)->_data, key)
#define Actions_DATA_SET(self, key, val) Actions_FrameDict_set(Actions_CTX(self)->_data, key, val)

// Forward declarations
typedef struct Actions Actions;
static void Actions_kernel(Actions* self, Actions_FrameEvent* __e);
static void Actions_router(Actions* self, Actions_FrameEvent* __e);
static void Actions_transition(Actions* self, Actions_Compartment* next);
static int Actions_hsm_chain(Actions* self, const char* leaf, const char*** out_chain);
static Actions_Compartment* Actions_prepareEnter(Actions* self, const char* leaf, Actions_FrameVec* state_args, Actions_FrameVec* enter_args);
static void Actions_prepareExit(Actions* self, Actions_FrameVec* exit_args);
static void Actions_state_Counting(Actions* self, Actions_FrameEvent* __e, Actions_Compartment* compartment);
static void Actions_s_Counting_hdl_user_get_total(Actions* self, Actions_FrameEvent* __e, Actions_Compartment* compartment);
static void Actions_s_Counting_hdl_user_increment(Actions* self, Actions_FrameEvent* __e, Actions_Compartment* compartment);
void Actions_increment (Actions* self, int n);
int Actions_get_total (Actions* self);
static void Actions__scale (Actions* self, int n);

struct Actions {
    Actions_FrameVec* _state_stack;
    Actions_Compartment* __compartment;
    Actions_Compartment* __next_compartment;
    Actions_FrameVec* _context_stack;
    int total;
};

Actions* Actions_new(void) {
    Actions* self = calloc(1, sizeof(Actions));
    self->_state_stack = Actions_FrameVec_new();
    self->_context_stack = Actions_FrameVec_new();
    self->total = 0;
    Actions_FrameVec* __sa = NULL;
    Actions_FrameVec* __ea = NULL;
    self->__compartment = Actions_prepareEnter(self, "Counting", __sa, __ea);
    self->__next_compartment = NULL;
    Actions_FrameEvent* __e = Actions_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    Actions_FrameContext* __ctx = Actions_FrameContext_new(__e, NULL);
    Actions_FrameVec_push(self->_context_stack, __ctx);
    Actions_FrameContext* __init_ctx = (Actions_FrameContext*)Actions_FrameVec_pop(self->_context_stack);
    Actions_FrameContext_destroy(__init_ctx);
    Actions_FrameEvent_destroy(__e);
    return self;
}

Actions* Actions_create(void) {
    Actions* self = Actions_new();
    self->_state_stack = Actions_FrameVec_new();
    self->_context_stack = Actions_FrameVec_new();
    self->total = 0;
    Actions_FrameVec* __sa = NULL;
    Actions_FrameVec* __ea = NULL;
    self->__compartment = Actions_prepareEnter(self, "Counting", __sa, __ea);
    self->__next_compartment = NULL;
    Actions_FrameEvent* __e = Actions_FrameEvent_new("$>", self->__compartment->enter_args, 0);
    Actions_FrameContext* __ctx = Actions_FrameContext_new(__e, NULL);
    Actions_FrameVec_push(self->_context_stack, __ctx);
    Actions_kernel(self, __e);
    Actions_FrameContext* __init_ctx = (Actions_FrameContext*)Actions_FrameVec_pop(self->_context_stack);
    Actions_FrameContext_destroy(__init_ctx);
    Actions_FrameEvent_destroy(__e);
    return self;
}

static int Actions_hsm_chain(Actions* self, const char* leaf, const char*** out_chain) {
    if (false) { (void)0; }
        else if (strcmp(leaf, "Counting") == 0) {
            static const char* __chain[] = { "Counting" };
            *out_chain = __chain;
            return 1;
        }
        *out_chain = NULL;
        return 0;
}

static Actions_Compartment* Actions_prepareEnter(Actions* self, const char* leaf, Actions_FrameVec* state_args, Actions_FrameVec* enter_args) {
    const char** chain = NULL;
    int n = Actions_hsm_chain(self, leaf, &chain);
    Actions_Compartment* comp = NULL;
    for (int i = 0; i < n; i++) {
        Actions_Compartment* nc = Actions_Compartment_new(chain[i]);
        if (state_args) {
            for (int j = 0; j < state_args->size; j++) Actions_FrameVec_push(nc->state_args, state_args->items[j]);
        }
        if (enter_args) {
            for (int j = 0; j < enter_args->size; j++) Actions_FrameVec_push(nc->enter_args, enter_args->items[j]);
        }
        nc->parent_compartment = comp;  // adopts ref
        comp = nc;
    }
    return comp;
}

static void Actions_prepareExit(Actions* self, Actions_FrameVec* exit_args) {
    Actions_Compartment* comp = self->__compartment;
    while (comp != NULL) {
        // Clear any prior exit_args before copying the new ones in.
        while (comp->exit_args->size > 0) comp->exit_args->size--;
        if (exit_args) {
            for (int j = 0; j < exit_args->size; j++) Actions_FrameVec_push(comp->exit_args, exit_args->items[j]);
        }
        comp = comp->parent_compartment;
    }
}

static void Actions_kernel(Actions* self, Actions_FrameEvent* __e) {
    Actions_router(self, __e);
    while (self->__next_compartment != NULL) {
        Actions_Compartment* next_compartment = self->__next_compartment;
        self->__next_compartment = NULL;
        // Exit the current (leaf) state
        Actions_FrameEvent* __exit_event = Actions_FrameEvent_new("<$", self->__compartment->exit_args, 0);
        Actions_router(self, __exit_event);
        Actions_FrameEvent_destroy(__exit_event);
        Actions_Compartment_unref(self->__compartment);
        self->__compartment = next_compartment;
        if (next_compartment->forward_event == NULL) {
            // No forwarded event — synthesize a fresh $>
            Actions_FrameEvent* __enter_event = Actions_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            Actions_router(self, __enter_event);
            Actions_FrameEvent_destroy(__enter_event);
        } else if (strcmp(next_compartment->forward_event->_message, "$>") == 0) {
            // Forwarded event IS $> — dispatch directly so the
            // destination's $> handler receives the caller's payload.
            // The forward_event is borrowed (owned by the wrapper that
            // queued the transition) — do NOT destroy it here.
            Actions_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            Actions_router(self, forward_event);
        } else {
            // Forwarded event is not $> — initialize the destination
            // with a fresh $>, then dispatch the forward to it. The
            // forward_event is borrowed; only the synthesized $> belongs
            // to the kernel and is freed here.
            Actions_FrameEvent* forward_event = next_compartment->forward_event;
            next_compartment->forward_event = NULL;
            Actions_FrameEvent* __enter_event = Actions_FrameEvent_new("$>", self->__compartment->enter_args, 0);
            Actions_router(self, __enter_event);
            Actions_FrameEvent_destroy(__enter_event);
            Actions_router(self, forward_event);
        }
        // Mark every stacked context as having transitioned. Read by
        // @@:self.X() guard so outer self-calls short-circuit.
        for (int __i = 0; __i < self->_context_stack->size; __i++) {
            ((Actions_FrameContext*)self->_context_stack->items[__i])->_transitioned = 1;
        }
    }
}

static void Actions_router(Actions* self, Actions_FrameEvent* __e) {
    if (strcmp(self->__compartment->state, "Counting") == 0) {
        Actions_state_Counting(self, __e, self->__compartment);
    }
}

static void Actions_transition(Actions* self, Actions_Compartment* next_compartment) {
    self->__next_compartment = next_compartment;
}

void Actions_destroy(Actions* self) {
    // Unref current compartment (may free if not on stack)
    if (self->__compartment) Actions_Compartment_unref(self->__compartment);
    if (self->__next_compartment) Actions_Compartment_unref(self->__next_compartment);
    // Unref all state stack entries
    if (self->_state_stack) {
        for (int __i = 0; __i < self->_state_stack->size; __i++) {
            Actions_Compartment_unref((Actions_Compartment*)self->_state_stack->items[__i]);
        }
        Actions_FrameVec_destroy(self->_state_stack);
    }
    if (self->_context_stack) Actions_FrameVec_destroy(self->_context_stack);
    free(self);
}

void Actions_increment(Actions* self, int n) {
    Actions_FrameVec* __params = Actions_FrameVec_new();
    Actions_FrameVec_push(__params, (void*)(intptr_t)n);
    Actions_FrameEvent* __e = Actions_FrameEvent_new("increment", __params, 1);
    Actions_FrameContext* __ctx = Actions_FrameContext_new(__e, NULL);
    Actions_FrameVec_push(self->_context_stack, __ctx);
    Actions_kernel(self, __e);
    Actions_FrameContext* __result_ctx = (Actions_FrameContext*)Actions_FrameVec_pop(self->_context_stack);
    Actions_FrameContext_destroy(__result_ctx);
    Actions_FrameEvent_destroy(__e);
}

int Actions_get_total(Actions* self) {
    Actions_FrameEvent* __e = Actions_FrameEvent_new("get_total", NULL, 0);
    Actions_FrameContext* __ctx = Actions_FrameContext_new(__e, NULL);
    Actions_FrameVec_push(self->_context_stack, __ctx);
    Actions_kernel(self, __e);
    Actions_FrameContext* __result_ctx = (Actions_FrameContext*)Actions_FrameVec_pop(self->_context_stack);
    int __result = (int)(intptr_t)__result_ctx->_return;
    Actions_FrameContext_destroy(__result_ctx);
    Actions_FrameEvent_destroy(__e);
    return __result;
}

static void Actions_state_Counting(Actions* self, Actions_FrameEvent* __e, Actions_Compartment* compartment) {
    if (strcmp(__e->_message, "get_total") == 0) {
        Actions_s_Counting_hdl_user_get_total(self, __e, compartment);
        return;
    }
    if (strcmp(__e->_message, "increment") == 0) {
        Actions_s_Counting_hdl_user_increment(self, __e, compartment);
        return;
    }
}

static void Actions_s_Counting_hdl_user_get_total(Actions* self, Actions_FrameEvent* __e, Actions_Compartment* compartment) {
    Actions_CTX(self)->_return = (void*)(intptr_t)(self.total);
}

static void Actions_s_Counting_hdl_user_increment(Actions* self, Actions_FrameEvent* __e, Actions_Compartment* compartment) {
    int n = (int)(intptr_t)Actions_FrameVec_get(__e->_parameters, 0);
    (void)n;
    self._scale(n)
}

static void Actions__scale(Actions* self, int n) {
                self.total = self.total + n * 2
}
