-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAssert.xs
68 lines (53 loc) · 1.59 KB
/
Assert.xs
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
61
62
63
64
65
66
67
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifndef OpSIBLING
#define OpSIBLING(o) ((o)->op_sibling)
#endif
STATIC OP*
assert_checker(pTHX_ OP *op, GV *namegv, SV *ckobj) {
op_free(op);
return newOP(OP_NULL, 0);
}
STATIC CV*
find_context_cv(pTHX) {
PERL_CONTEXT* cx;
for (cx = cxstack + cxstack_ix; cx >= cxstack; --cx) {
if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) return cx->blk_sub.cv;
if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.cv) return cx->blk_eval.cv;
}
return PL_main_cv;
}
MODULE = Devel::Assert PACKAGE = Devel::Assert
PROTOTYPES: DISABLE
BOOT:
{
CV* assert_cv = get_cv("Devel::Assert::assert_off", 0);
cv_set_call_checker(assert_cv, assert_checker, (SV*)assert_cv);
}
void
assert_on(...)
PPCODE:
{
if (items != 1) croak("Wrong number of arguments in assert() call");
if (SvTRUE(ST(0))) XSRETURN_UNDEF;
PUSHMARK(SP);
EXTEND(SP, 3);
/* B.xs declares 'make_*_object' as static, so had to borrow it */
#define push_b_object(type, ptr) STMT_START { \
SV* tmpsv = sv_newmortal(); \
sv_setiv(newSVrv(tmpsv, type), PTR2IV(ptr));\
PUSHs(tmpsv); \
} STMT_END
OP* o = cUNOP->op_first;
OP* kid = OpSIBLING(o);
if (!kid || kid->op_type == OP_NULL) o = cUNOPo->op_first;
push_b_object("B::UNOP", o);
push_b_object("B::COP", PL_curcop);
push_b_object("B::CV", find_context_cv(aTHX));
PUTBACK;
call_sv((SV*)get_cv("Devel::Assert::assert_fail", 0), G_VOID);
SPAGAIN;
XSRETURN_UNDEF;
}