PERL_STATIC_INLINE void xs_incset(pTHX_ const char *const unixname, const STRLEN unixlen, SV* xsfile) { HV *inchv = GvHVn(PL_incgv); #if 0 SV** const svp = hv_fetch(inchv, unixname, unixlen, 0); if (!svp) #endif (void)hv_store(inchv, unixname, unixlen, SvREFCNT_inc_simple_NN(xsfile), 0); }
STATIC void S_mro_gather_and_rename(pTHX_ HV * const stashes, HV * const seen_stashes, HV *stash, HV *oldstash, SV *namesv) { XPVHV* xhv; HE *entry; I32 riter = -1; I32 items = 0; const bool stash_had_name = stash && HvENAME(stash); bool fetched_isarev = FALSE; HV *seen = NULL; HV *isarev = NULL; SV **svp = NULL; PERL_ARGS_ASSERT_MRO_GATHER_AND_RENAME; /* We use the seen_stashes hash to keep track of which packages have been encountered so far. This must be separate from the main list of stashes, as we need to distinguish between stashes being assigned and stashes being replaced/deleted. (A nested stash can be on both sides of an assignment. We cannot simply skip iterating through a stash on the right if we have seen it on the left, as it will not get its ename assigned to it.) To avoid allocating extra SVs, instead of a bitfield we can make bizarre use of immortals: &PL_sv_undef: seen on the left (oldstash) &PL_sv_no : seen on the right (stash) &PL_sv_yes : seen on both sides */ if(oldstash) { /* Add to the big list. */ struct mro_meta * meta; HE * const entry = (HE *) hv_common( seen_stashes, NULL, (const char *)&oldstash, sizeof(HV *), 0, HV_FETCH_LVALUE|HV_FETCH_EMPTY_HE, NULL, 0 ); if(HeVAL(entry) == &PL_sv_undef || HeVAL(entry) == &PL_sv_yes) { oldstash = NULL; goto check_stash; } HeVAL(entry) = HeVAL(entry) == &PL_sv_no ? &PL_sv_yes : &PL_sv_undef; meta = HvMROMETA(oldstash); (void) hv_store( stashes, (const char *)&oldstash, sizeof(HV *), meta->isa ? SvREFCNT_inc_simple_NN((SV *)meta->isa) : &PL_sv_yes, 0 ); CLEAR_LINEAR(meta); /* Update the effective name. */ if(HvENAME_get(oldstash)) { const HEK * const enamehek = HvENAME_HEK(oldstash); if(SvTYPE(namesv) == SVt_PVAV) { items = AvFILLp((AV *)namesv) + 1; svp = AvARRAY((AV *)namesv); } else { items = 1; svp = &namesv; } while (items--) { const U32 name_utf8 = SvUTF8(*svp); STRLEN len; const char *name = SvPVx_const(*svp, len); if(PL_stashcache) { DEBUG_o(Perl_deb(aTHX_ "mro_gather_and_rename clearing PL_stashcache for '%"SVf"'\n", SVfARG(*svp))); (void)hv_delete(PL_stashcache, name, name_utf8 ? -(I32)len : (I32)len, G_DISCARD); } ++svp; hv_ename_delete(oldstash, name, len, name_utf8); if (!fetched_isarev) { /* If the name deletion caused a name change, then we * are not going to call mro_isa_changed_in with this * name (and not at all if it has become anonymous) so * we need to delete old isarev entries here, both * those in the superclasses and this class's own list * of subclasses. We simply delete the latter from * PL_isarev, since we still need it. hv_delete morti- * fies it for us, so sv_2mortal is not necessary. */ if(HvENAME_HEK(oldstash) != enamehek) { if(meta->isa && HvARRAY(meta->isa)) mro_clean_isarev(meta->isa, name, len, 0, 0, name_utf8 ? HVhek_UTF8 : 0); isarev = (HV *)hv_delete(PL_isarev, name, name_utf8 ? -(I32)len : (I32)len, 0); fetched_isarev=TRUE; } } } } } check_stash: if(stash) { if(SvTYPE(namesv) == SVt_PVAV) { items = AvFILLp((AV *)namesv) + 1; svp = AvARRAY((AV *)namesv); } else { items = 1; svp = &namesv; } while (items--) { const U32 name_utf8 = SvUTF8(*svp); STRLEN len; const char *name = SvPVx_const(*svp++, len); hv_ename_add(stash, name, len, name_utf8); } /* Add it to the big list if it needs * mro_isa_changed_in called on it. That happens if it was * detached from the symbol table (so it had no HvENAME) before * being assigned to the spot named by the 'name' variable, because * its cached isa linearisation is now stale (the effective name * having changed), and subclasses will then use that cache when * mro_package_moved calls mro_isa_changed_in. (See * [perl #77358].) * * If it did have a name, then its previous name is still * used in isa caches, and there is no need for * mro_package_moved to call mro_isa_changed_in. */ entry = (HE *) hv_common( seen_stashes, NULL, (const char *)&stash, sizeof(HV *), 0, HV_FETCH_LVALUE|HV_FETCH_EMPTY_HE, NULL, 0 ); if(HeVAL(entry) == &PL_sv_yes || HeVAL(entry) == &PL_sv_no) stash = NULL; else { HeVAL(entry) = HeVAL(entry) == &PL_sv_undef ? &PL_sv_yes : &PL_sv_no; if(!stash_had_name) { struct mro_meta * const meta = HvMROMETA(stash); (void) hv_store( stashes, (const char *)&stash, sizeof(HV *), meta->isa ? SvREFCNT_inc_simple_NN((SV *)meta->isa) : &PL_sv_yes, 0 ); CLEAR_LINEAR(meta); } } } if(!stash && !oldstash) /* Both stashes have been encountered already. */ return; /* Add all the subclasses to the big list. */ if(!fetched_isarev) { /* If oldstash is not null, then we can use its HvENAME to look up the isarev hash, since all its subclasses will be listed there. It will always have an HvENAME. It the HvENAME was removed above, then fetch_isarev will be true, and this code will not be reached. If oldstash is null, then this is an empty spot with no stash in it, so subclasses could be listed in isarev hashes belonging to any of the names, so we have to check all of them. */ assert(!oldstash || HvENAME(oldstash)); if (oldstash) { /* Extra variable to avoid a compiler warning */ const HEK * const hvename = HvENAME_HEK(oldstash); fetched_isarev = TRUE; svp = hv_fetchhek(PL_isarev, hvename, 0); if (svp) isarev = MUTABLE_HV(*svp); } else if(SvTYPE(namesv) == SVt_PVAV) { items = AvFILLp((AV *)namesv) + 1; svp = AvARRAY((AV *)namesv); } else { items = 1; svp = &namesv; } } if( isarev || !fetched_isarev ) { while (fetched_isarev || items--) { HE *iter; if (!fetched_isarev) { HE * const he = hv_fetch_ent(PL_isarev, *svp++, 0, 0); if (!he || !(isarev = MUTABLE_HV(HeVAL(he)))) continue; } hv_iterinit(isarev); while((iter = hv_iternext(isarev))) { HV* revstash = gv_stashsv(hv_iterkeysv(iter), 0); struct mro_meta * meta; if(!revstash) continue; meta = HvMROMETA(revstash); (void) hv_store( stashes, (const char *)&revstash, sizeof(HV *), meta->isa ? SvREFCNT_inc_simple_NN((SV *)meta->isa) : &PL_sv_yes, 0 ); CLEAR_LINEAR(meta); } if (fetched_isarev) break; } } /* This is partly based on code in hv_iternext_flags. We are not call- ing that here, as we want to avoid resetting the hash iterator. */ /* Skip the entire loop if the hash is empty. */ if(oldstash && HvUSEDKEYS(oldstash)) { xhv = (XPVHV*)SvANY(oldstash); seen = (HV *) sv_2mortal((SV *)newHV()); /* Iterate through entries in the oldstash, adding them to the list, meanwhile doing the equivalent of $seen{$key} = 1. */ while (++riter <= (I32)xhv->xhv_max) { entry = (HvARRAY(oldstash))[riter]; /* Iterate through the entries in this list */ for(; entry; entry = HeNEXT(entry)) { const char* key; I32 len; /* If this entry is not a glob, ignore it. Try the next. */ if (!isGV(HeVAL(entry))) continue; key = hv_iterkey(entry, &len); if ((len > 1 && key[len-2] == ':' && key[len-1] == ':') || (len == 1 && key[0] == ':')) { HV * const oldsubstash = GvHV(HeVAL(entry)); SV ** const stashentry = stash ? hv_fetch(stash, key, HeUTF8(entry) ? -(I32)len : (I32)len, 0) : NULL; HV *substash = NULL; /* Avoid main::main::main::... */ if(oldsubstash == oldstash) continue; if( ( stashentry && *stashentry && isGV(*stashentry) && (substash = GvHV(*stashentry)) ) || (oldsubstash && HvENAME_get(oldsubstash)) ) { /* Add :: and the key (minus the trailing ::) to each name. */ SV *subname; if(SvTYPE(namesv) == SVt_PVAV) { SV *aname; items = AvFILLp((AV *)namesv) + 1; svp = AvARRAY((AV *)namesv); subname = sv_2mortal((SV *)newAV()); while (items--) { aname = newSVsv(*svp++); if (len == 1) sv_catpvs(aname, ":"); else { sv_catpvs(aname, "::"); sv_catpvn_flags( aname, key, len-2, HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES ); } av_push((AV *)subname, aname); } } else { subname = sv_2mortal(newSVsv(namesv)); if (len == 1) sv_catpvs(subname, ":"); else { sv_catpvs(subname, "::"); sv_catpvn_flags( subname, key, len-2, HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES ); } } mro_gather_and_rename( stashes, seen_stashes, substash, oldsubstash, subname ); } (void)hv_store(seen, key, HeUTF8(entry) ? -(I32)len : (I32)len, &PL_sv_yes, 0); } } } } /* Skip the entire loop if the hash is empty. */ if (stash && HvUSEDKEYS(stash)) { xhv = (XPVHV*)SvANY(stash); riter = -1; /* Iterate through the new stash, skipping $seen{$key} items, calling mro_gather_and_rename(stashes,seen,entry,NULL, ...). */ while (++riter <= (I32)xhv->xhv_max) { entry = (HvARRAY(stash))[riter]; /* Iterate through the entries in this list */ for(; entry; entry = HeNEXT(entry)) { const char* key; I32 len; /* If this entry is not a glob, ignore it. Try the next. */ if (!isGV(HeVAL(entry))) continue; key = hv_iterkey(entry, &len); if ((len > 1 && key[len-2] == ':' && key[len-1] == ':') || (len == 1 && key[0] == ':')) { HV *substash; /* If this entry was seen when we iterated through the oldstash, skip it. */ if(seen && hv_exists(seen, key, HeUTF8(entry) ? -(I32)len : (I32)len)) continue; /* We get here only if this stash has no corresponding entry in the stash being replaced. */ substash = GvHV(HeVAL(entry)); if(substash) { SV *subname; /* Avoid checking main::main::main::... */ if(substash == stash) continue; /* Add :: and the key (minus the trailing ::) to each name. */ if(SvTYPE(namesv) == SVt_PVAV) { SV *aname; items = AvFILLp((AV *)namesv) + 1; svp = AvARRAY((AV *)namesv); subname = sv_2mortal((SV *)newAV()); while (items--) { aname = newSVsv(*svp++); if (len == 1) sv_catpvs(aname, ":"); else { sv_catpvs(aname, "::"); sv_catpvn_flags( aname, key, len-2, HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES ); } av_push((AV *)subname, aname); } } else { subname = sv_2mortal(newSVsv(namesv)); if (len == 1) sv_catpvs(subname, ":"); else { sv_catpvs(subname, "::"); sv_catpvn_flags( subname, key, len-2, HeUTF8(entry) ? SV_CATUTF8 : SV_CATBYTES ); } } mro_gather_and_rename( stashes, seen_stashes, substash, NULL, subname ); } } } } } }
PJS_EXTERN JSBool PJS_Call_sv_with_jsvals_rsv( pTHX_ JSContext *cx, JSObject *obj, SV *code, SV *caller, /* Will be disposed inside */ uintN argc, jsval *argv, SV **rsv, I32 flag ) { dSP; JSBool ok = JS_TRUE; uintN arg; I32 rcount = caller ? 1 : 0; PJS_Context *pcx = PJS_GET_CONTEXT(cx); if(SvROK(code) && SvTYPE(SvRV(code)) == SVt_PVCV) { ENTER; SAVETMPS; PUSHMARK(SP) ; sv_setiv(save_scalar(PJS_Context_SV), PTR2IV(pcx)); EXTEND(SP, argc + rcount); PUTBACK; /* From here we are working with the global stack, * a) at PUSH time we can fail, so we need to abort the call * b) Want to avoid copying local <=> global SP at every single PUSH * * Before 'call_sv', rcount is the number of SVs pushed so far */ if(caller) *++PL_stack_sp = sv_2mortal(caller); if(argv && !(flag & G_NOARGS)) { /* HACK: We use G_NOARGS as a guard against use argv[-1] to get This. * Needed for the use in PJS_invoke_perl_property_setter where given * argc is faked */ SV *This; ok = PJS_ReflectJS2Perl(aTHX_ cx, argv[-1], &This, 0); if(ok) sv_setsv(save_scalar(PJS_This), sv_2mortal(This)); else goto forget; } else flag &= ~G_NOARGS; for(arg = 0; arg < argc; arg++) { SV *sv; ok = PJS_ReflectJS2Perl(aTHX_ cx, argv[arg], &sv, 1); if(!ok) { rcount += arg; goto forget; } *++PL_stack_sp = sv_2mortal(sv); } rcount = call_sv(code, flag | G_EVAL); if(rsv) { if(flag == G_SCALAR || rcount == 1) *rsv = SvREFCNT_inc_simple_NN(*PL_stack_sp); else *rsv = newRV((SV *)av_make(rcount, PL_stack_sp-rcount+1)); SAVEMORTALIZESV(*rsv); } forget: PL_stack_sp -= rcount; FREETMPS; LEAVE; if(ok && SvTRUE(ERRSV)) { propagate2JS(aTHX_ pcx, obj); ok = JS_FALSE; } } else croak("Not a coderef"); return ok; }
/* Wrap a JS value to export into perl * Returns a new SV, REFCNT_dec is caller's responsability */ JSBool PJS_ReflectJS2Perl( pTHX_ JSContext *cx, jsval value, SV** sv, int full ) { if(JSVAL_IS_PRIMITIVE(value)) { *sv = PrimJSVALToSV(aTHX_ cx, value); if(*sv) return JS_TRUE; } else if(JSVAL_IS_OBJECT(value)) { PJS_Context *pcx = PJS_GET_CONTEXT(cx); JSObject *object = JSVAL_TO_OBJECT(value); JSClass *clasp = PJS_GET_CLASS(cx, object); const char *classname = clasp->name; JSObject *passport; SV *wrapper; SV *box; char hkey[32]; jsval temp = JSVAL_VOID; snprintf(hkey, 32, "%p", (void *)object); PJS_DEBUG2("Wrapping a %s(%s)\n", classname, hkey); if(PJS_getFlag(pcx, "ConvertRegExp") && strEQ(classname, "RegExp")) { jsval src; char *str; if(JS_CallFunctionName(cx, object, "toSource", 0, NULL, &src) && (str = JS_GetStringBytes(JS_ValueToString(cx, src))) ) { dSP; SV *tmp = newSVpvf("qr%s", str); eval_sv(tmp, G_SCALAR); sv_free(tmp); // Don't leak SPAGAIN; tmp = POPs; PUTBACK; if(!SvTRUE(ERRSV)) { *sv = SvREFCNT_inc_simple_NN(tmp); return JS_TRUE; } } return JS_FALSE; } if(IS_PERL_CLASS(clasp)) { /* IS_PERL_CLASS means actual perl object is there */ SV *priv = (SV *)JS_GetPrivate(cx, object); if(priv && SvOK(priv) && SvROK(priv)) { *sv = SvREFCNT_inc_simple_NN(priv); return JS_TRUE; } croak("A private %s?!\n", classname); return JS_FALSE; } /* Common JSObject case */ /* Check registered perl visitors */ JS_LookupProperty(cx, pcx->pvisitors, hkey, &temp); if(temp != JSVAL_VOID) { /* Already registered, so exits a reference in perl space * _must_ hold a PASSPORT */ assert(JSVAL_TO_OBJECT(temp) == object); box = PJS_GetPassport(aTHX_ cx, object); SvREFCNT_inc_void_NN(box); /* In perl should be one more */ PJS_DEBUG1("Cached!: %s\n", hkey); } else { /* Check if with a PASSPORT */ JS_LookupPropertyWithFlags(cx, object, PJS_PASSPORT_PROP, 0, &temp); if(JSVAL_IS_OBJECT(temp) && (passport = JSVAL_TO_OBJECT(temp)) && PJS_GET_CLASS(cx, passport) == &passport_class && JS_GetReservedSlot(cx, passport, 0, &temp) && object == (JSObject *)JSVAL_TO_PRIVATE(temp) ) { /* Yes, reentering perl */ box = (SV *)JS_GetPrivate(cx, passport); /* Here we don't increment refcount, the ownership in passport is * transferred to perl land. */ PJS_DEBUG1("Reenter: %s\n", hkey); } else { /* No, first time, must wrap the object */ SV *boxref; const char *package; SV *robj = newSV(0); SV *rjsv = newSV(0); if (JS_ObjectIsFunction(cx, object)) package = PJS_FUNCTION_PACKAGE; else if(JS_IsArrayObject(cx, object)) package = PJS_ARRAY_PACKAGE; else if(strEQ(classname, PJS_PACKAGE_CLASS_NAME)) package = PJS_STASH_PACKAGE; #if JS_HAS_XML_SUPPORT else if(strEQ(classname, "XML")) package = PJS_XMLOBJ_PACKAGE; #endif else if(strEQ(classname, "Error")) package = PJS_ERROR_PACKAGE; else { SV **sv = hv_fetch(get_hv(NAMESPACE"ClassMap", 1), classname, strlen(classname), 0); if(sv) package = SvPV_nolen(*sv); else package = PJS_OBJECT_PACKAGE; } sv_setref_pv(robj, PJS_RAW_OBJECT, (void*)object); sv_setref_iv(rjsv, PJS_RAW_JSVAL, (IV)value); boxref = PJS_CallPerlMethod(aTHX_ cx, "__new", sv_2mortal(newSVpv(package, 0)), // package sv_2mortal(robj), // content sv_2mortal(rjsv), // jsval NULL ); if(!boxref) return JS_FALSE; if(!SvOK(boxref) || !sv_derived_from(boxref, PJS_BOXED_PACKAGE)) croak("PJS_Assert: Contructor must return a "NAMESPACE"Boxed"); /* Create a new PASSPORT */ passport = JS_NewObject(cx, &passport_class, NULL, object); if(!passport || !JS_DefineProperty(cx, object, PJS_PASSPORT_PROP, OBJECT_TO_JSVAL(passport), NULL, NULL, JSPROP_READONLY | JSPROP_PERMANENT)) return JS_FALSE; box = SvRV(boxref); /* boxref is mortal, so we need to increment its rc, at end of * scope, PASSPORT owns created box */ JS_SetPrivate(cx, passport, (void *)SvREFCNT_inc_simple_NN(box)); JS_SetReservedSlot(cx, passport, 0, PRIVATE_TO_JSVAL(object)); PJS_DEBUG2("New boxed: %s brc: %d\n", hkey, SvREFCNT(box)); } /* Root object adding it to pvisitors list, will be unrooted by * jsc_free_root at Boxed DESTROY time */ JS_DefineProperty(cx, pcx->pvisitors, hkey, value, NULL, NULL, 0); } /* Here the RC of box in PASSPORT reflects wrapper's ownership */ if(full && PJS_getFlag(pcx, "AutoTie") && (strEQ(classname, "Object") || strEQ(classname, "Array")) ) { /* Return tied */ AV *avbox = (AV *)SvRV(box); SV **last; SV *tied; SV *tier; if(strEQ(classname, "Array")) { last = av_fetch(avbox, 6, 1); if(last && SvOK(*last) && SvROK(*last)) { // Cached *sv = newSVsv(*last); sv_free(box); /* Hard copy 'sv' owns the reference */ return JS_TRUE; } tied = (SV *)newAV(); } else { // Object last = av_fetch(avbox, 5, 1); if(last && SvOK(*last) && SvROK(*last)) { // Cached *sv = newSVsv(*last); sv_free(box); /* Hard copy 'sv' owns the reference */ return JS_TRUE; } tied = (SV *)newHV(); } /* hv_magic below own a reference to box, we use an explicit path, * to make clear that to perl land only one reference is given */ tier = newRV_inc(box); hv_magic((HV *)tied, (GV *)tier, PERL_MAGIC_tied); sv_free(tier); wrapper = newRV_noinc(tied); /* Don't leak the hidden tied variable */ /* Save in cache a weaken copy, the cache itself dosn't hold a reference */ sv_setsv(*last, wrapper); sv_rvweaken(*last); PJS_DEBUG1("Return tied for %s\n", SvPV_nolen(tier)); } else { wrapper = newRV_noinc(box); /* Transfer ownership to wrapper */ #if PERL_VERSION < 9 sv_bless(wrapper, SvSTASH(box)); #endif } *sv = wrapper; return JS_TRUE; } return JS_FALSE; }