summaryrefslogtreecommitdiffstats
path: root/smartmatch.xs
blob: b3e12c5a71c146801c815198c73fcd4e1d218434 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#include "ppport.h"

#include "hook_op_check_smartmatch.h"

STATIC OP*
smartmatch_cb(pTHX_ OP *o, void *user_data)
{
    OP *left, *right, *cb_op, *list, *new;

    left = cBINOPo->op_first;
    right = left->op_sibling;

    o->op_flags &= ~OPf_KIDS;
    op_free(o);

    cb_op = newSVOP(OP_CONST, 0, (SV*)user_data);
    list = newLISTOP(OP_LIST, 0, left, right);
    new = newUNOP(OP_ENTERSUB, OPf_STACKED,
                  op_append_elem(OP_LIST, list, cb_op));

    return new;
}

UV
hook_op_check_smartmatch(void *user_data)
{
    return hook_op_check(OP_SMARTMATCH, smartmatch_cb, user_data);
}

void *
hook_op_check_smartmatch_remove(UV id)
{
    return hook_op_check_remove(OP_SMARTMATCH, id);
}

MODULE = smartmatch  PACKAGE = smartmatch

PROTOTYPES: DISABLE

UV
register (cb)
    SV *cb;
    CODE:
        if (!SvROK(cb) || SvTYPE(SvRV(cb)) != SVt_PVCV) {
            croak("not a coderef");
        }

        RETVAL = hook_op_check_smartmatch(newSVsv(cb));
    OUTPUT:
        RETVAL

void
unregister (id)
    UV id;
    CODE:
        hook_op_check_smartmatch_remove(id);