| 1 | /* |
| 2 | * Safe::Hole - make a hole to the original main compartment in the Safe compartment |
| 3 | * Copyright 1999-2001, Sey Nakajima, All rights reserved. |
| 4 | * This program is free software under the GPL. |
| 5 | */ |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | #include "EXTERN.h" |
| 10 | #include "perl.h" |
| 11 | #include "XSUB.h" |
| 12 | #ifdef __cplusplus |
| 13 | } |
| 14 | #endif |
| 15 | |
| 16 | #define OP_MASK_BUF_SIZE (MAXO + 100) |
| 17 | |
| 18 | /* A reference to a dummy string with the real opmask, if any, attached as magic */ |
| 19 | |
| 20 | static SV* _get_current_opmask() { |
| 21 | SV *opmask; |
| 22 | SV *saved_PL_op_mask = NULL; |
| 23 | opmask = newSVpvn("Opcode Mask",11); |
| 24 | if ( PL_op_mask ) { |
| 25 | saved_PL_op_mask = sv_2mortal(newSVpvn(PL_op_mask,OP_MASK_BUF_SIZE)); |
| 26 | } |
| 27 | sv_magic(opmask,saved_PL_op_mask,'~',"Safe::Hole opmask",17); |
| 28 | return newRV_noinc(opmask); |
| 29 | } |
| 30 | |
| 31 | MODULE = Safe::Hole PACKAGE = Safe::Hole |
| 32 | |
| 33 | void |
| 34 | _hole_call_sv(stashref, opmask, codesv) |
| 35 | SV * stashref |
| 36 | SV * opmask |
| 37 | SV * codesv |
| 38 | PPCODE: |
| 39 | /*** This code is copied from Opcode::_safe_call_sv and modified ***/ |
| 40 | GV *gv; |
| 41 | AV *av; |
| 42 | SV *saved_PL_op_mask; |
| 43 | MAGIC *magic; |
| 44 | I32 j,ac; |
| 45 | |
| 46 | ENTER; |
| 47 | |
| 48 | if ( SvTRUE(opmask)) { |
| 49 | SAVEVPTR(PL_op_mask); |
| 50 | if ( SvMAGICAL(opmask) && |
| 51 | (magic = mg_find(opmask, '~')) && |
| 52 | magic->mg_ptr && |
| 53 | !strncmp(magic->mg_ptr,"Safe::Hole opmask",17) ) { |
| 54 | if ( saved_PL_op_mask = magic->mg_obj ) { |
| 55 | PL_op_mask = SvPVX(saved_PL_op_mask); |
| 56 | } else { |
| 57 | PL_op_mask = NULL; |
| 58 | } |
| 59 | } else { |
| 60 | croak("Opmask argument lacks suitable 'Safe::Hole opmask' magic"); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | save_aptr(&PL_endav); |
| 65 | PL_endav = (AV*)sv_2mortal((SV*)newAV()); /* ignore END blocks for now */ |
| 66 | |
| 67 | save_hptr(&PL_defstash); /* save current default stack */ |
| 68 | save_hptr(&PL_globalstash); /* save current global stash */ |
| 69 | /* the assignment to global defstash changes our sense of 'main' */ |
| 70 | if( !SvROK(stashref) || SvTYPE(SvRV(stashref)) != SVt_PVHV ) |
| 71 | croak("stash reference required"); |
| 72 | PL_defstash = (HV*)SvRV(stashref); |
| 73 | PL_globalstash = GvHV(gv_fetchpv("CORE::GLOBAL::", GV_ADDWARN, SVt_PVHV)); |
| 74 | |
| 75 | /* defstash must itself contain a main:: so we'll add that now */ |
| 76 | /* take care with the ref counts (was cause of long standing bug) */ |
| 77 | /* XXX I'm still not sure if this is right, GV_ADDWARN should warn! */ |
| 78 | gv = gv_fetchpv("main::", GV_ADDWARN, SVt_PVHV); |
| 79 | sv_free((SV*)GvHV(gv)); |
| 80 | GvHV(gv) = (HV*)SvREFCNT_inc(PL_defstash); |
| 81 | PUSHMARK(SP); |
| 82 | |
| 83 | perl_call_sv(codesv, GIMME); |
| 84 | SPAGAIN; /* for the PUTBACK added by xsubpp */ |
| 85 | LEAVE; |
| 86 | |
| 87 | SV* |
| 88 | _get_current_opmask() |