5. jex

"jex" is like runts rex, but for janet. When a trigger happens, jex will call a janet function.

5.1. jex node

<<function_declarations>>=
int node_jex(gf_node *node, monolith_d *m, const char *fun);
<<functions>>=
typedef struct {
    char *fun;
    monolith_d *m;
} jex_d;

static void jex_compute(gf_node *node)
{
    int blksize;
    int s;
    jex_d *jex;
    gf_cable *in;
    GFFLT smp;
    JanetTable *env;
    JanetBindingType result;
    Janet fun;

    blksize = gf_node_blksize(node);
    jex = gf_node_get_data(node);
    gf_node_get_cable(node, 0, &in);

    for (s = 0; s < blksize; s++) {
        smp = gf_cable_get(in, s);
        if (smp != 0) {
            env = monolith_janet_env(jex->m);
            result = janet_resolve(env,
                                   janet_csymbol(jex->fun),
                                   &fun);
            if (result == JANET_BINDING_DEF) {
                /* janet_pcall(janet_unwrap_function(fun), */
                /*             0, */
                /*             NULL, */
                /*             &fun, */
                /*             NULL); */
                janet_call(janet_unwrap_function(fun),
                            0,
                            NULL);
            }
        }
    }
}

static void jex_destroy(gf_node *node)
{
    gf_patch *patch;
    jex_d *jex;

    jex = gf_node_get_data(node);

    gf_node_get_patch(node, &patch);
    gf_memory_free(patch, (void **)&jex->fun);
    gf_memory_free(patch, (void **)&jex);
    gf_node_cables_free(node);
}

int node_jex(gf_node *node, monolith_d *m, const char *fun)
{
    jex_d *jex;
    gf_patch *patch;
    char *pfun;

    gf_node_get_patch(node, &patch);
    gf_memory_alloc(patch, sizeof(jex_d), (void **)&jex);
    jex->m = m;
    gf_memory_alloc(patch, strlen(fun) + 1, (void **)&pfun);
    strcpy(pfun, fun);
    jex->fun = pfun;
    gf_node_cables_alloc(node, 1);
    gf_node_set_compute(node, jex_compute);
    gf_node_set_destroy(node, jex_destroy);
    gf_node_set_data(node, jex);
    return GF_OK;
}

5.2. jex word

<<function_declarations>>=
int load_jex(monolith_d *m);
<<functions>>=
<<rproc_jex>>
int load_jex(monolith_d *m)
{
    monolith_runt_keyword(m, "jex", 3, rproc_jex, m);
    return RUNT_OK;
}
<<rproc_jex>>=
static runt_int rproc_jex(runt_vm *vm, runt_ptr p)
{
    monolith_d *m;
    runt_int rc;
    rgf_param in;
    gf_patch *patch;
    gf_node *node;
    const char *fun;
    runt_stacklet *s;

    rc = runt_ppop(vm, &s);
    RUNT_ERROR_CHECK(rc);
    fun = runt_to_string(s->p);

    rc = rgf_get_param(vm, &in);
    RUNT_ERROR_CHECK(rc);

    m = runt_to_cptr(p);

    patch = monolith_graforge_get(m);
    rc = gf_patch_new_node(patch, &node);
    GF_RUNT_ERROR_CHECK(rc);

    rc = node_jex(node, m, fun);
    GF_RUNT_ERROR_CHECK(rc);
    rgf_set_param(vm, node, &in, 0);
    return RUNT_OK;
}



prev | home | next