From: Vincent Pit Date: Thu, 26 Nov 2009 20:50:50 +0000 (+0100) Subject: Factor the V::M magic searching logic into a new vmg_find() X-Git-Tag: v0.39~16 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=110ed6888ddf13f751e5ee0de5b0d8188e2f956e;p=perl%2Fmodules%2FVariable-Magic.git Factor the V::M magic searching logic into a new vmg_find() --- diff --git a/Magic.xs b/Magic.xs index 6ffe42d..1920c78 100644 --- a/Magic.xs +++ b/Magic.xs @@ -588,6 +588,27 @@ STATIC const MGWIZ *vmg_wizard_mgwiz(pTHX_ SV *wiz) { /* --- User-level functions implementation --------------------------------- */ +STATIC const MAGIC *vmg_find(pTHX_ const SV *sv, SV *wiz) { +#define vmg_find(S, W) vmg_find(aTHX_ (S), (W)) + const MAGIC *mg, *moremagic; + UV wid; + + if (SvTYPE(sv) < SVt_PVMG) + return NULL; + + wid = vmg_wizard_id(wiz); + for (mg = SvMAGIC(sv); mg; mg = moremagic) { + moremagic = mg->mg_moremagic; + if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { + UV zid = vmg_wizard_id(mg->mg_ptr); + if (zid == wid) + return mg; + } + } + + return NULL; +} + /* ... Construct private data .............................................. */ STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, AV *args) { @@ -626,23 +647,8 @@ STATIC SV *vmg_data_new(pTHX_ SV *ctor, SV *sv, AV *args) { STATIC SV *vmg_data_get(pTHX_ SV *sv, SV *wiz) { #define vmg_data_get(S, W) vmg_data_get(aTHX_ (S), (W)) - MAGIC *mg, *moremagic; - - if (SvTYPE(sv) >= SVt_PVMG) { - UV wid = vmg_wizard_id(wiz); - for (mg = SvMAGIC(sv); mg; mg = moremagic) { - moremagic = mg->mg_moremagic; - if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - UV zid = vmg_wizard_id(mg->mg_ptr); - if (zid == wid) - break; - } - } - if (mg) - return mg->mg_obj; - } - - return NULL; + const MAGIC *mg = vmg_find(sv, wiz); + return mg ? mg->mg_obj : NULL; } /* ... Magic cast/dispell .................................................. */ @@ -664,24 +670,16 @@ STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) STATIC UV vmg_cast(pTHX_ SV *sv, SV *wiz, AV *args) { #define vmg_cast(S, W, A) vmg_cast(aTHX_ (S), (W), (A)) - MAGIC *mg = NULL, *moremagic = NULL; - SV *data; - const MGWIZ *w = vmg_wizard_mgwiz(wiz); - U32 oldgmg = SvGMAGICAL(sv); + MAGIC *mg, *moremagic = NULL; + SV *data; + const MGWIZ *w; + U32 oldgmg; - if (SvTYPE(sv) >= SVt_PVMG) { - UV wid = vmg_wizard_id(wiz); - for (mg = SvMAGIC(sv); mg; mg = moremagic) { - moremagic = mg->mg_moremagic; - if (mg->mg_type == PERL_MAGIC_ext && mg->mg_private == SIG_WIZ) { - UV zid = vmg_wizard_id(mg->mg_ptr); - if (zid == wid) - break; - } - } - if (mg) - return 1; - } + if (vmg_find(sv, wiz)) + return 1; + + w = vmg_wizard_mgwiz(wiz); + oldgmg = SvGMAGICAL(sv); data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args) : NULL; mg = sv_magicext(sv, data, PERL_MAGIC_ext, w->vtbl, (const char *) wiz, HEf_SVKEY);