Chandra

 view release on metacpan or  search on metacpan

xs/bind.xs  view on Meta::CPAN

    RETVAL = sv_bless(newRV_noinc((SV *)self_hv), gv_stashpv(class, GV_ADD));
}
OUTPUT:
    RETVAL

SV *
bind(self, name, callback)
    SV *self
    SV *name
    SV *callback
CODE:
{
    if (!SvOK(name))
        croak("bind() requires a name");
    if (!SvROK(callback) || SvTYPE(SvRV(callback)) != SVt_PVCV)
        croak("bind() requires a coderef");

    HV *reg = _bind_get_registry(aTHX);
    STRLEN nlen;
    const char *nstr = SvPV(name, nlen);
    (void)hv_store(reg, nstr, (I32)nlen, SvREFCNT_inc(SvRV(callback)), 0);

    RETVAL = SvREFCNT_inc(self);
}
OUTPUT:
    RETVAL

SV *
unbind(self, name)
    SV *self
    SV *name
CODE:
{
    HV *reg = _bind_get_registry(aTHX);
    STRLEN nlen;
    const char *nstr = SvPV(name, nlen);
    (void)hv_delete(reg, nstr, (I32)nlen, G_DISCARD);
    RETVAL = SvREFCNT_inc(self);
}
OUTPUT:
    RETVAL

int
is_bound(self, name)
    SV *self
    SV *name
CODE:
{
    PERL_UNUSED_VAR(self);
    HV *reg = _bind_get_registry(aTHX);
    STRLEN nlen;
    const char *nstr = SvPV(name, nlen);
    RETVAL = hv_exists(reg, nstr, (I32)nlen);
}
OUTPUT:
    RETVAL

void
list(self)
    SV *self
PPCODE:
{
    PERL_UNUSED_VAR(self);
    HV *reg = _bind_get_registry(aTHX);
    I32 num = hv_iterinit(reg);
    HE *entry;
    EXTEND(SP, num);
    while ((entry = hv_iternext(reg)) != NULL) {
        PUSHs(sv_2mortal(newSVsv(hv_iterkeysv(entry))));
    }
}

void
register_handler(class, id, callback)
    SV *class
    SV *id
    SV *callback
CODE:
{
    PERL_UNUSED_VAR(class);
    HV *reg = _bind_get_registry(aTHX);
    STRLEN nlen;
    const char *nstr = SvPV(id, nlen);
    (void)hv_store(reg, nstr, (I32)nlen, SvREFCNT_inc(SvROK(callback) ? SvRV(callback) : callback), 0);
}

SV *
dispatch(self, json_str)
    SV *self
    SV *json_str
CODE:
{
    SV *err = NULL;
    SV *decoded = _bind_json_decode(aTHX_ json_str, &err);

    if (err) {
        /* warn "Chandra::Bind: Failed to parse JSON: $@" */
        warn("Chandra::Bind: Failed to parse JSON: %" SVf, SVfARG(err));
        HV *ret = newHV();
        SV *msg = newSVpvs("Invalid JSON: ");
        sv_catsv(msg, err);
        (void)hv_stores(ret, "error", msg);
        SvREFCNT_dec(err);
        RETVAL = newRV_noinc((SV *)ret);
    }
    else if (!SvROK(decoded) || SvTYPE(SvRV(decoded)) != SVt_PVHV) {
        /* Not a hash - raw */
        HV *ret = newHV();
        (void)hv_stores(ret, "type", newSVpvs("raw"));
        (void)hv_stores(ret, "data", SvREFCNT_inc(json_str));
        SvREFCNT_dec(decoded);
        RETVAL = newRV_noinc((SV *)ret);
    }
    else {
        HV *msg_hv = (HV *)SvRV(decoded);
        SV **type_svp = hv_fetchs(msg_hv, "type", 0);
        const char *type = (type_svp && *type_svp && SvOK(*type_svp))
                           ? SvPV_nolen(*type_svp) : "";

        if (strEQ(type, "call")) {
            /* === _handle_call === */



( run in 0.704 second using v1.01-cache-2.11-cpan-71847e10f99 )