/* ... Safe version of call_sv() ........................................... */
-STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, SV *dsv) {
-#define vmg_call_sv(S, F, D) vmg_call_sv(aTHX_ (S), (F), (D))
+STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, int (*cleanup)(pTHX_ void *), void *ud) {
+#define vmg_call_sv(S, F, C, U) vmg_call_sv(aTHX_ (S), (F), (C), (U))
I32 ret, cxix, in_eval = 0;
PERL_CONTEXT saved_cx;
SV *old_err = NULL;
++PL_Ierror_count;
#endif
} else if (!in_eval) {
- if (dsv) {
- /* We are about to croak() while dsv is being destroyed. Try to clean up
- * things a bit. */
- MAGIC *mg = SvMAGIC(dsv);
- SvREFCNT_dec((SV *) mg->mg_ptr);
- /* mg->mg_obj may not be refcounted if the data constructor returned the
- * variable itself. */
- if (mg->mg_flags & MGf_REFCOUNTED)
- SvREFCNT_dec(mg->mg_obj);
- SvMAGIC_set(dsv, mg->mg_moremagic);
- Safefree(mg);
- mg_magical(dsv);
- SvREFCNT_dec(dsv);
- }
- croak(NULL);
+ if (!cleanup || cleanup(aTHX_ ud))
+ croak(NULL);
}
} else {
if (old_err) {
PUSHs(args[i]);
PUTBACK;
- vmg_call_sv(ctor, G_SCALAR, NULL);
+ vmg_call_sv(ctor, G_SCALAR, 0, NULL);
SPAGAIN;
nsv = POPs;
XPUSHs(vmg_op_info(opinfo));
PUTBACK;
- vmg_call_sv(cb, G_SCALAR, NULL);
+ vmg_call_sv(cb, G_SCALAR, 0, NULL);
SPAGAIN;
svr = POPs;
XPUSHs(vmg_op_info(opinfo));
PUTBACK;
- vmg_call_sv(w->cb_len, G_SCALAR, NULL);
+ vmg_call_sv(w->cb_len, G_SCALAR, 0, NULL);
SPAGAIN;
svr = POPs;
/* ... free magic .......................................................... */
+STATIC int vmg_svt_free_cleanup(pTHX_ void *ud) {
+ SV *sv = VOID2(SV *, ud);
+ MAGIC *mg;
+
+ /* We are about to croak() while sv is being destroyed. Try to clean up
+ * things a bit. */
+ mg = SvMAGIC(sv);
+ SvREFCNT_dec((SV *) mg->mg_ptr);
+ /* mg->mg_obj may not be refcounted if the data constructor returned the
+ * variable itself. */
+ if (mg->mg_flags & MGf_REFCOUNTED)
+ SvREFCNT_dec(mg->mg_obj);
+ SvMAGIC_set(sv, mg->mg_moremagic);
+ Safefree(mg);
+ mg_magical(sv);
+ SvREFCNT_dec(sv);
+
+ /* After that, propagate the error upwards. */
+ return 1;
+}
+
STATIC int vmg_svt_free(pTHX_ SV *sv, MAGIC *mg) {
const vmg_wizard *w;
int ret = 0;
XPUSHs(vmg_op_info(w->opinfo));
PUTBACK;
- vmg_call_sv(w->cb_free, G_SCALAR, sv);
+ vmg_call_sv(w->cb_free, G_SCALAR, vmg_svt_free_cleanup, sv);
SPAGAIN;
svr = POPs;