1 package Devel::BeginLift;
8 use Inline C => <<'EOC';
13 /* lifted from op.c (w/linklist -> Perl_linklist) */
15 #define LINKLIST(o) ((o)->op_next ? (o)->op_next : Perl_linklist((OP*)o))
17 /* pointer to old PL_check entersub entry to be populated in init */
19 STATIC OP *(*dbl_old_ck_entersub)(pTHX_ OP *op);
21 /* replacement PL_check entersub entry */
23 STATIC OP *dbl_ck_entersub(pTHX_ OP *o) {
28 I32 type = o->op_type;
32 SV** to_lift_pack_ref;
33 HV* to_lift_pack_hash;
34 SV** to_lift_flag_ref;
36 o = dbl_old_ck_entersub(aTHX_ o); /* let the original do its job */
38 kid = cUNOPo->op_first;
40 if (kid->op_type != OP_NULL) /* pushmark for method call ... */
43 last = kLISTOP->op_last;
45 if (last->op_type != OP_NULL) /* not what we expected */
48 kid = cUNOPx(last)->op_first;
50 if (kid->op_type != OP_GV) /* not a GV so ignore */
53 stash = GvSTASH(kGVOP_gv);
55 /* printf("Calling GV %s -> %s\n",
56 HvNAME(stash), GvNAME(kGVOP_gv)); */
58 to_lift = get_hv("Devel::BeginLift::lift", FALSE);
63 to_lift_pack_ref = hv_fetch(to_lift, HvNAME(stash), strlen(HvNAME(stash)),
66 if (!to_lift_pack_ref || !SvROK(*to_lift_pack_ref))
67 return o; /* not a hashref */
69 to_lift_pack_hash = (HV*) SvRV(*to_lift_pack_ref);
71 to_lift_flag_ref = hv_fetch(to_lift_pack_hash, GvNAME(kGVOP_gv),
72 strlen(GvNAME(kGVOP_gv)), FALSE);
74 if (!to_lift_flag_ref || !SvTRUE(*to_lift_flag_ref))
77 /* shamelessly lifted from fold_constants in op.c */
79 stack_save = PL_stack_sp;
85 if (PL_stack_sp > stack_save) { /* sub returned something */
86 sv = *(PL_stack_sp--);
87 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
88 pad_swipe(o->op_targ, FALSE);
89 else if (SvTEMP(sv)) { /* grab mortal temp? */
90 (void)SvREFCNT_inc(sv);
95 return newGVOP(OP_GV, 0, (GV*)sv);
96 return newSVOP(OP_CONST, 0, sv);
98 /* this bit not lifted, handles the 'sub doesn't return stuff' case
99 which fold_constants can ignore */
101 return newSVOP(OP_CONST, 0, &PL_sv_undef);
105 static int initialized = 0;
108 if (!initialized++) {
109 dbl_old_ck_entersub = PL_check[OP_ENTERSUB];
110 PL_check[OP_ENTERSUB] = dbl_ck_entersub;
115 /* ensure we only uninit when number of teardown calls matches
116 number of setup calls */
117 if (initialized && !--initialized) {
118 PL_check[OP_ENTERSUB] = dbl_old_ck_entersub;
123 # C code done, now for the perl.
126 my ($class, @args) = @_;
128 $class->setup_for($target => \@args);
134 $class->teardown_for($target);
138 my ($class, $target, $args) = @_;
140 $lift{$target}{$_} = 1 for @$args;
144 my ($class, $target) = @_;
145 delete $lift{$target};