/* DO NOT EDIT!!!
 * Autogenerated by try_perlapi.pl
 */


#include <EXTERN.h>
#define PERL_EXT 1
#include <perl.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include "try_perlapi.h"
#include "perlmodule.h"
#include "lang_lock.h"
#include "thrd_ctx.h"

static LOGOP dummy_op;

void
fake_inittry(void)
{
    Zero(&dummy_op, 1, LOGOP);
    dummy_op.op_flags |= OPf_WANT_SCALAR;
    dummy_op.op_next = Nullop;
}

static void
fake_entertry(PERL_CONTEXT **cx_ptr)
{
    PERL_CONTEXT *cx;
    I32 gimme;
    dCTXP;

    PL_op = (OP*)&dummy_op;
    gimme = GIMME_V;

    ENTER;
    SAVETMPS;
    dMARK;

//    Perl_push_return(aTHX_ Nullop);
    *cx_ptr = cx = cx_pushblock((CXt_EVAL|CXp_TRYBLOCK), gimme, MARK, PL_stack_sp);
    #define WITHOUT_PUSHEVAL
    #ifndef WITHOUT_PUSHEVAL
    PUSHEVAL(cx, 0);
        #endif
    PL_eval_root = PL_op;
    PL_in_eval = EVAL_INEVAL;
    sv_setpvn(ERRSV, "", 0);
}

static void
fake_leavetry(I32 oldscope, PERL_CONTEXT *cx)
{
    dCTXP;
    if (PL_scopestack_ix > oldscope) {
        PMOP *newpm;
        I32 optype;
        SV **newsp;
        I32 gimme;

        CX_LEAVE_SCOPE(cx);
        #if 0
        cx_popsub(cx);
                #endif
        cx_popblock(cx);
    #ifndef WITHOUT_PUSHEVAL
        POPEVAL(cx);
        #endif
        #if 0
        CX_POP(cx);
        #endif
//        Perl_pop_return(aTHX);
        PL_curpm = newpm;
    }

    FREETMPS;
    LEAVE;
}

int
try_array_len(AV* av)
{
    dJMPENV;
    dCTXP;
    PERL_CONTEXT *cx=NULL;
    int jmp_status;
    volatile I32 oldscope = PL_scopestack_ix;
    int RETVAL;

    ASSERT_LOCK_PERL;
    fake_entertry(&cx);

    JMPENV_PUSH(jmp_status);
    if (jmp_status == 0) {

        RETVAL = av_len(av)+1;

    }
    else if (jmp_status == 3) {
        /* caught an exception, $@ should be set */
        assert(SvTRUE(ERRSV));
        PYTHON_LOCK;
        propagate_errsv();
        PYTHON_UNLOCK;

        RETVAL = -1;
    }
    else {
        fprintf(stderr, "should not happen, jmp_status = %d\n", jmp_status);
    }
    JMPENV_POP;
    fake_leavetry(oldscope, cx);
    return RETVAL;
}

SV**
try_av_fetch(AV* av, I32 key, I32 lval)
{
    dJMPENV;
    dCTXP;
    PERL_CONTEXT *cx=NULL;
    int jmp_status;
    volatile I32 oldscope = PL_scopestack_ix;
    SV** RETVAL;

    ASSERT_LOCK_PERL;
    fake_entertry(&cx);

    JMPENV_PUSH(jmp_status);
    if (jmp_status == 0) {

        RETVAL = av_fetch(av, key, lval);

    }
    else if (jmp_status == 3) {
        /* caught an exception, $@ should be set */
        assert(SvTRUE(ERRSV));
        PYTHON_LOCK;
        propagate_errsv();
        PYTHON_UNLOCK;

        RETVAL = NULL;
    }
    else {
        fprintf(stderr, "should not happen, jmp_status = %d\n", jmp_status);
    }
    JMPENV_POP;
    fake_leavetry(oldscope, cx);
    return RETVAL;
}

int
try_SvGETMAGIC(SV* sv)
{
    dJMPENV;
    dCTXP;
    PERL_CONTEXT *cx=NULL;
    int jmp_status;
    volatile I32 oldscope = PL_scopestack_ix;
    int RETVAL;

    ASSERT_LOCK_PERL;
    fake_entertry(&cx);

    JMPENV_PUSH(jmp_status);
    if (jmp_status == 0) {

        SvGETMAGIC(sv);
        RETVAL = 0;

    }
    else if (jmp_status == 3) {
        /* caught an exception, $@ should be set */
        assert(SvTRUE(ERRSV));
        PYTHON_LOCK;
        propagate_errsv();
        PYTHON_UNLOCK;

        RETVAL = -1;
    }
    else {
        fprintf(stderr, "should not happen, jmp_status = %d\n", jmp_status);
    }
    JMPENV_POP;
    fake_leavetry(oldscope, cx);
    return RETVAL;
}

int
try_SvSETMAGIC(SV* sv)
{
    dJMPENV;
    dCTXP;
    PERL_CONTEXT *cx=NULL;
    int jmp_status;
    volatile I32 oldscope = PL_scopestack_ix;
    int RETVAL;

    ASSERT_LOCK_PERL;
    fake_entertry(&cx);

    JMPENV_PUSH(jmp_status);
    if (jmp_status == 0) {

        SvSETMAGIC(sv);
        RETVAL = 0;

    }
    else if (jmp_status == 3) {
        /* caught an exception, $@ should be set */
        assert(SvTRUE(ERRSV));
        PYTHON_LOCK;
        propagate_errsv();
        PYTHON_UNLOCK;

        RETVAL = -1;
    }
    else {
        fprintf(stderr, "should not happen, jmp_status = %d\n", jmp_status);
    }
    JMPENV_POP;
    fake_leavetry(oldscope, cx);
    return RETVAL;
}

