This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Provide a BGET_svtype, required by #28667
[perl5.git] / ext / ByteLoader / bytecode.h
index c0093a5..fd2c4f6 100644 (file)
@@ -13,30 +13,42 @@ typedef char *pvindex;
 
 /* all this should be made endianness-agnostic */
 
-#define BGET_U8(arg)   arg = BGET_FGETC()
-#define BGET_U16(arg)  \
-       BGET_FREAD(&arg, sizeof(U16), 1)
-#define BGET_U32(arg)  \
-       BGET_FREAD(&arg, sizeof(U32), 1)
-#define BGET_UV(arg)   \
-       BGET_FREAD(&arg, sizeof(UV), 1)
-#define BGET_PADOFFSET(arg)    \
-       BGET_FREAD(&arg, sizeof(PADOFFSET), 1)
-
-#define BGET_I32(arg)  BGET_U32(arg)
-#define BGET_IV(arg)   BGET_UV(arg)
+#define BGET_U8(arg) STMT_START {                                      \
+       const int _arg = BGET_FGETC();                                  \
+       if (_arg < 0) {                                                 \
+           Perl_croak(aTHX_                                            \
+                      "EOF or error while trying to read 1 byte for U8"); \
+       }                                                               \
+       arg = (U8) _arg;                                                \
+    } STMT_END
+
+#define BGET_U16(arg)          BGET_OR_CROAK(arg, U16)
+#define BGET_I32(arg)          BGET_OR_CROAK(arg, U32)
+#define BGET_U32(arg)          BGET_OR_CROAK(arg, U32)
+#define BGET_IV(arg)           BGET_OR_CROAK(arg, IV)
+#define BGET_PADOFFSET(arg)    BGET_OR_CROAK(arg, PADOFFSET)
+#define BGET_long(arg)         BGET_OR_CROAK(arg, long)
+#define BGET_svtype(arg)       BGET_OR_CROAK(arg, svtype)
+
+#define BGET_OR_CROAK(arg, type) STMT_START {                          \
+       if (BGET_FREAD(&arg, sizeof(type), 1) < 1) {                    \
+           Perl_croak(aTHX_                                            \
+                      "EOF or error while trying to read %d bytes for %s", \
+                      sizeof(type), STRINGIFY(type));                  \
+       }                                                               \
+    } STMT_END
 
 #define BGET_PV(arg)   STMT_START {                                    \
        BGET_U32(arg);                                                  \
        if (arg) {                                                      \
-           New(666, bstate->bs_pv.xpv_pv, arg, char);                  \
-           bl_read(bstate->bs_fdata, (void*)bstate->bs_pv.xpv_pv, arg, 1);\
-           bstate->bs_pv.xpv_len = arg;                                \
-           bstate->bs_pv.xpv_cur = arg - 1;                            \
+           Newx(bstate->bs_pv.pvx, arg, char);                 \
+           bl_read(bstate->bs_fdata, bstate->bs_pv.pvx, arg, 1);       \
+           bstate->bs_pv.xpv.xpv_len = arg;                            \
+           bstate->bs_pv.xpv.xpv_cur = arg - 1;                        \
        } else {                                                        \
-           bstate->bs_pv.xpv_pv = 0;                                   \
-           bstate->bs_pv.xpv_len = 0;                                  \
-           bstate->bs_pv.xpv_cur = 0;                                  \
+           bstate->bs_pv.pvx = 0;                                      \
+           bstate->bs_pv.xpv.xpv_len = 0;                              \
+           bstate->bs_pv.xpv.xpv_cur = 0;                              \
        }                                                               \
     } STMT_END
 
@@ -61,12 +73,12 @@ typedef char *pvindex;
 #define BGET_op_tr_array(arg) do {                     \
        unsigned short *ary, len;                       \
        BGET_U16(len);                                  \
-       New(666, ary, len, unsigned short);             \
+       Newx(ary, len, unsigned short);         \
        BGET_FREAD(ary, sizeof(unsigned short), len);   \
        arg = (char *) ary;                             \
     } while (0)
 
-#define BGET_pvcontents(arg)   arg = bstate->bs_pv.xpv_pv
+#define BGET_pvcontents(arg)   arg = bstate->bs_pv.pvx
 #define BGET_strconst(arg) STMT_START {        \
        for (arg = PL_tokenbuf; (*arg = BGET_FGETC()); arg++) /* nothing */; \
        arg = PL_tokenbuf;                      \
@@ -89,7 +101,13 @@ typedef char *pvindex;
        arg = arg ? savepv(arg) : arg;                  \
     } STMT_END
 
-#define BSET_ldspecsv(sv, arg) sv = specialsv_list[arg]
+#define BSET_ldspecsv(sv, arg) STMT_START {                            \
+       if(arg >= sizeof(specialsv_list) / sizeof(specialsv_list[0])) { \
+           Perl_croak(aTHX_ "Out of range special SV number %d", arg); \
+       }                                                               \
+       sv = specialsv_list[arg];                                       \
+    } STMT_END
+
 #define BSET_ldspecsvx(sv, arg) STMT_START {   \
        BSET_ldspecsv(sv, arg);                 \
        BSET_OBJ_STOREX(sv);                    \
@@ -120,23 +138,30 @@ typedef char *pvindex;
     } STMT_END
 
 #define BSET_sv_magic(sv, arg)         sv_magic(sv, Nullsv, arg, 0, 0)
-#define BSET_mg_name(mg, arg)  mg->mg_ptr = arg; mg->mg_len = bstate->bs_pv.xpv_cur
+#define BSET_mg_name(mg, arg)  mg->mg_ptr = arg; mg->mg_len = bstate->bs_pv.xpv.xpv_cur
 #define BSET_mg_namex(mg, arg)                 \
        (mg->mg_ptr = (char*)SvREFCNT_inc((SV*)arg),    \
         mg->mg_len = HEf_SVKEY)
+#define BSET_xmg_stash(sv, arg) *(SV**)&(((XPVMG*)SvANY(sv))->xmg_stash) = (arg)
 #define BSET_sv_upgrade(sv, arg)       (void)SvUPGRADE(sv, arg)
+#define BSET_xrv(sv, arg) SvRV_set(sv, arg)
 #define BSET_xpv(sv)   do {    \
-       SvPV_set(sv, bstate->bs_pv.xpv_pv);     \
-       SvCUR_set(sv, bstate->bs_pv.xpv_cur);   \
-       SvLEN_set(sv, bstate->bs_pv.xpv_len);   \
+       SvPV_set(sv, bstate->bs_pv.pvx);        \
+       SvCUR_set(sv, bstate->bs_pv.xpv.xpv_cur);       \
+       SvLEN_set(sv, bstate->bs_pv.xpv.xpv_len);       \
     } while (0)
+#define BSET_xpv_cur(sv, arg) SvCUR_set(sv, arg)
+#define BSET_xpv_len(sv, arg) SvLEN_set(sv, arg)
+#define BSET_xiv(sv, arg) SvIV_set(sv, arg)
+#define BSET_xnv(sv, arg) SvNV_set(sv, arg)
+
 #define BSET_av_extend(sv, arg)        av_extend((AV*)sv, arg)
 
 #define BSET_av_push(sv, arg)  av_push((AV*)sv, arg)
 #define BSET_av_pushx(sv, arg) (AvARRAY(sv)[++AvFILLp(sv)] = arg)
 #define BSET_hv_store(sv, arg) \
-       hv_store((HV*)sv, bstate->bs_pv.xpv_pv, bstate->bs_pv.xpv_cur, arg, 0)
-#define BSET_pv_free(pv)       Safefree(pv.xpv_pv)
+       hv_store((HV*)sv, bstate->bs_pv.pvx, bstate->bs_pv.xpv.xpv_cur, arg, 0)
+#define BSET_pv_free(p)        Safefree(p)
 
 
 #ifdef USE_ITHREADS
@@ -146,7 +171,7 @@ typedef char *pvindex;
     STMT_START { \
         SV* repointer; \
        REGEXP* rx = arg ? \
-           CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, cPMOPx(o)) : \
+           CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv.xpv_cur, cPMOPx(o)) : \
            Null(REGEXP*); \
         if(av_len((AV*) PL_regex_pad[0]) > -1) { \
             repointer = av_pop((AV*)PL_regex_pad[0]); \
@@ -165,7 +190,7 @@ typedef char *pvindex;
 #define BSET_pregcomp(o, arg) \
     STMT_START { \
        PM_SETRE(((PMOP*)o), (arg ? \
-            CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv_cur, cPMOPx(o)): \
+            CALLREGCOMP(aTHX_ arg, arg + bstate->bs_pv.xpv.xpv_cur, cPMOPx(o)): \
             Null(REGEXP*))); \
     } STMT_END
 
@@ -181,7 +206,7 @@ typedef char *pvindex;
                sv = (SV*)newHV();                      \
                break;                                  \
            default:                                    \
-               sv = NEWSV(0,0);                        \
+               sv = newSV(0);                          \
                SvUPGRADE(sv, (arg));                   \
            }
 #define BSET_newsvx(sv, arg) STMT_START {              \
@@ -189,14 +214,14 @@ typedef char *pvindex;
            SvFLAGS(sv) = arg;                          \
            BSET_OBJ_STOREX(sv);                        \
        } STMT_END
-#define BSET_newop(o, arg)                             \
-       ((o = (OP*)safemalloc(arg)), memzero((char*)o,arg))
+
+#define BSET_newop(o, arg)     NewOpSz(666, o, arg)
 #define BSET_newopx(o, arg) STMT_START {       \
        register int sz = arg & 0x7f;           \
-       register OP* new = (OP*) safemalloc(sz);\
-       memzero(new, sz);                       \
-       /* new->op_next = o; XXX */             \
-       o = new;                                \
+       register OP* newop;                     \
+       BSET_newop(newop, sz);                  \
+       /* newop->op_next = o; XXX */           \
+       o = newop;                              \
        arg >>=7;                               \
        BSET_op_type(o, arg);                   \
        BSET_OBJ_STOREX(o);                     \
@@ -213,6 +238,8 @@ typedef char *pvindex;
        return 0;                               \
     } STMT_END
 
+#define BSET_op_pmstashpv(op, arg)     PmopSTASHPV_set(op, arg)
+
 /* 
  * stolen from toke.c: better if that was a function.
  * in toke.c there are also #ifdefs for dosish systems and i/o layers
@@ -306,7 +333,7 @@ typedef char *pvindex;
            GvCV(CvGV(cv)) = 0;               /* cv has been hijacked */\
             call_list(oldscope, PL_beginav);           \
             PL_curcop = &PL_compiling;                 \
-            PL_compiling.op_private = (U8)(PL_hints & HINT_PRIVATE_MASK);\
+            CopHINTS_set(&PL_compiling, PL_hints);     \
             LEAVE;                                     \
        } STMT_END
 #define BSET_push_init(ary,cv)                         \
@@ -336,6 +363,32 @@ typedef char *pvindex;
        mg_set(*hv_store(GvHV(gv_fetchpv("SIG", TRUE, SVt_PVHV)),       \
                name, strlen(name), cv, 0))
 
+#define BSET_xhv_name(hv, name)        hv_name_set((HV*)hv, name, strlen(name), 0)
+#define BSET_cop_arybase(c, b) CopARYBASE_set(c, b)
+#define BSET_cop_warnings(c, w) \
+       STMT_START {                                                    \
+           if (specialWARN((STRLEN *)w)) {                             \
+               c->cop_warnings = (STRLEN *)w;                          \
+           } else {                                                    \
+               STRLEN len;                                             \
+               const char *const p = SvPV_const(w, len);               \
+               c->cop_warnings =                                       \
+                   Perl_new_warnings_bitfield(aTHX_ NULL, p, len);     \
+               SvREFCNT_dec(w);                                        \
+           }                                                           \
+       } STMT_END
+#define BSET_gp_file(gv, file) \
+       STMT_START {                                                    \
+           STRLEN len = strlen(file);                                  \
+           U32 hash;                                                   \
+           PERL_HASH(hash, file, len);                                 \
+           if(GvFILE_HEK(gv)) {                                        \
+               Perl_unshare_hek(aTHX_ GvFILE_HEK(gv));                 \
+           }                                                           \
+           GvGP(gv)->gp_file_hek = share_hek(file, len, hash);         \
+           Safefree(file);                                             \
+       } STMT_END
+
 /* NOTE: the bytecode header only sanity-checks the bytecode. If a script cares about
  * what version of Perl it's being called under, it should do a 'use 5.006_001' or
  * equivalent. However, since the header includes checks requiring an exact match in