| 1 | /* vi: set ft=c : */ | ||
| 2 | |||
| 3 | #define newHELEMEXISTSOROP(flags, helem, other) S_newHELEMEXISTSOROP(aTHX_ flags, helem, other) | ||
| 4 | |||
| 5 | #if defined(OPpHELEMEXISTSOR_DELETE) | ||
| 6 | /* For now this is not in any Perl release but hopefully soon; maybe in time | ||
| 7 | * for 5.37.7 | ||
| 8 | * https://github.com/Perl/perl5/pull/20598 | ||
| 9 | */ | ||
| 10 | |||
| 11 | 0 | static OP *S_newHELEMEXISTSOROP(pTHX_ U32 flags, OP *helem, OP *other) | |
| 12 | { | ||
| 13 | 0 | return newLOGOP(OP_HELEMEXISTSOR, flags, helem, other); | |
| 14 | } | ||
| 15 | |||
| 16 | #else | ||
| 17 | |||
| 18 | enum { | ||
| 19 | OPpHELEMEXISTSOR_DELETE = (1<<7), | ||
| 20 | }; | ||
| 21 | |||
| 22 | static OP *pp_helemexistsor(pTHX) | ||
| 23 | { | ||
| 24 | dSP; | ||
| 25 | SV *keysv = POPs; | ||
| 26 | HV *hv = MUTABLE_HV(POPs); | ||
| 27 | bool is_delete = PL_op->op_private & OPpHELEMEXISTSOR_DELETE; | ||
| 28 | |||
| 29 | assert(SvTYPE(hv) == SVt_PVHV); | ||
| 30 | |||
| 31 | bool hv_is_magical = UNLIKELY(SvMAGICAL(hv)); | ||
| 32 | |||
| 33 | SV *val = NULL; | ||
| 34 | |||
| 35 | /* For magical HVs we have to ensure we invoke the EXISTS method first. For | ||
| 36 | * regular HVs we can just skip this and use the "pointer or NULL" result | ||
| 37 | * of the real hv_* functions | ||
| 38 | */ | ||
| 39 | if(hv_is_magical && !hv_exists_ent(hv, keysv, 0)) | ||
| 40 | goto other; | ||
| 41 | |||
| 42 | if(is_delete) { | ||
| 43 | val = hv_delete_ent(hv, keysv, 0, 0); | ||
| 44 | } | ||
| 45 | else { | ||
| 46 | HE *he = hv_fetch_ent(hv, keysv, 0, 0); | ||
| 47 | val = he ? HeVAL(he) : NULL; | ||
| 48 | |||
| 49 | /* A magical HV hasn't yet actually invoked the FETCH method. We must ask | ||
| 50 | * it to do so now | ||
| 51 | */ | ||
| 52 | if(hv_is_magical && val) | ||
| 53 | SvGETMAGIC(val); | ||
| 54 | } | ||
| 55 | |||
| 56 | if(!val) { | ||
| 57 | other: | ||
| 58 | PUTBACK; | ||
| 59 | return cLOGOP->op_other; | ||
| 60 | } | ||
| 61 | |||
| 62 | PUSHs(val); | ||
| 63 | RETURN; | ||
| 64 | } | ||
| 65 | |||
| 66 | static OP *S_newHELEMEXISTSOROP(pTHX_ U32 flags, OP *helem, OP *other) | ||
| 67 | { | ||
| 68 | assert(helem->op_type == OP_HELEM); | ||
| 69 | |||
| 70 | OP *o = newLOGOP_CUSTOM(&pp_helemexistsor, flags, helem, other); | ||
| 71 | |||
| 72 | OP *hvop = cBINOPx(helem)->op_first; | ||
| 73 | OP *keyop = OpSIBLING(hvop); | ||
| 74 | |||
| 75 | helem->op_targ = helem->op_type; | ||
| 76 | helem->op_type = OP_NULL; | ||
| 77 | helem->op_ppaddr = PL_ppaddr[OP_NULL]; | ||
| 78 | |||
| 79 | /* o is actually the structural-containing OP_NULL */ | ||
| 80 | OP *real_o = cUNOPo->op_first; | ||
| 81 | |||
| 82 | keyop->op_next = real_o; | ||
| 83 | |||
| 84 | return o; | ||
| 85 | } | ||
| 86 | |||
| 87 | #endif |