5 #define NEED_newRV_noinc
6 #define NEED_sv_2pv_flags
10 #define gv_fetchsv(n,f,t) gv_fetchpv(SvPV_nolen(n), f, t)
13 #ifndef mro_method_changed_in
14 #define mro_method_changed_in(x) PL_sub_generation++
18 #define newSVhe(he) newSVhek(HeKEY_hek(he))
20 #define newSVhe(he) newSVpv(HePV(he, PL_na), 0)
24 #define savesvpv(s) savepv(SvPV_nolen(s))
28 #define GvCV_set(gv, cv) (GvCV(gv) = (CV*)(cv))
32 #define SVT_SCALAR(svt) (svt <= SVt_PVLV)
36 #define SVT_ARRAY(svt) (svt == SVt_PVAV)
40 #define SVT_HASH(svt) (svt == SVt_PVHV)
44 #define SVT_CODE(svt) (svt == SVt_PVCV)
48 #define SVT_IO(svt) (svt == SVt_PVIO)
52 #define SVT_FORMAT(svt) (svt == SVt_PVFM)
55 /* HACK: scalar slots are always populated on perl < 5.10, so treat undef
56 * as nonexistent. this is consistent with the previous behavior of the pure
57 * perl version of this module (since this is the behavior that perl sees
60 #define GvSVOK(g) (GvSV(g) && SvTYPE(GvSV(g)) != SVt_NULL)
62 #define GvSVOK(g) GvSV(g)
65 #define GvAVOK(g) GvAV(g)
66 #define GvHVOK(g) GvHV(g)
67 #define GvCVOK(g) GvCVu(g) /* XXX: should this really be GvCVu? or GvCV? */
68 #define GvIOOK(g) GvIO(g)
70 /* see above - don't let scalar slots become unpopulated, this breaks
71 * assumptions in core */
73 #define GvSetSV(g,v) do { \
75 SvREFCNT_dec(GvSV(g)); \
76 if ((GvSV(g) = _v ? _v : newSV(0))) \
77 GvIMPORTED_SV_on(g); \
80 #define GvSetSV(g,v) do { \
81 SvREFCNT_dec(GvSV(g)); \
82 if ((GvSV(g) = (SV*)(v))) \
83 GvIMPORTED_SV_on(g); \
87 #define GvSetAV(g,v) do { \
88 SvREFCNT_dec(GvAV(g)); \
89 if ((GvAV(g) = (AV*)(v))) \
90 GvIMPORTED_AV_on(g); \
92 #define GvSetHV(g,v) do { \
93 SvREFCNT_dec(GvHV(g)); \
94 if ((GvHV(g) = (HV*)(v))) \
95 GvIMPORTED_HV_on(g); \
97 #define GvSetCV(g,v) do { \
98 SvREFCNT_dec(GvCV(g)); \
99 if ((GvCV_set(g, (CV*)(v)))) { \
100 GvIMPORTED_CV_on(g); \
104 if (HvENAME_get(GvSTASH(g))) \
105 mro_method_changed_in(GvSTASH(g)); \
107 #define GvSetIO(g,v) do { \
108 SvREFCNT_dec(GvIO(g)); \
109 GvIOp(g) = (IO*)(v); \
119 VAR_GLOB, /* TODO: unimplemented */
120 VAR_FORMAT /* TODO: unimplemented */
128 static U32 name_hash, namespace_hash, type_hash;
129 static SV *name_key, *namespace_key, *type_key;
130 static REGEXP *valid_module_regex;
132 static const char *vartype_to_string(vartype_t type)
150 static I32 vartype_to_svtype(vartype_t type)
154 return SVt_PV; /* or whatever */
168 static vartype_t string_to_vartype(char *vartype)
170 if (strEQ(vartype, "SCALAR")) {
173 else if (strEQ(vartype, "ARRAY")) {
176 else if (strEQ(vartype, "HASH")) {
179 else if (strEQ(vartype, "CODE")) {
182 else if (strEQ(vartype, "IO")) {
186 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO', not '%s'", vartype);
190 static int _is_valid_module_name(SV *package)
196 buf = SvPV(package, len);
198 /* whee cargo cult */
200 sv_upgrade(sv, SVt_PV);
208 return pregexec(valid_module_regex, buf, buf + len, buf, 1, sv, 1);
211 static void _deconstruct_variable_name(SV *variable, varspec_t *varspec)
215 if (!SvCUR(variable))
216 croak("You must pass a variable name");
218 varspec->name = sv_2mortal(newSVsv(variable));
220 varpv = SvPV_nolen(varspec->name);
223 varspec->type = VAR_SCALAR;
224 sv_chop(varspec->name, &varpv[1]);
227 varspec->type = VAR_ARRAY;
228 sv_chop(varspec->name, &varpv[1]);
231 varspec->type = VAR_HASH;
232 sv_chop(varspec->name, &varpv[1]);
235 varspec->type = VAR_CODE;
236 sv_chop(varspec->name, &varpv[1]);
239 varspec->type = VAR_IO;
244 static void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
248 val = hv_fetch_ent(variable, name_key, 0, name_hash);
250 croak("The 'name' key is required in variable specs");
252 varspec->name = sv_2mortal(newSVsv(HeVAL(val)));
254 val = hv_fetch_ent(variable, type_key, 0, type_hash);
256 croak("The 'type' key is required in variable specs");
258 varspec->type = string_to_vartype(SvPV_nolen(HeVAL(val)));
261 static void _check_varspec_is_valid(varspec_t *varspec)
263 if (strstr(SvPV_nolen(varspec->name), "::")) {
264 croak("Variable names may not contain ::");
268 static int _valid_for_type(SV *value, vartype_t type)
270 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
274 /* XXX is a glob a scalar? assigning a glob to the scalar slot seems
275 * to work here, but in pure perl i'm pretty sure it goes to the EGV
276 * slot, which seems more correct to me. just disable it for now
278 return SVT_SCALAR(sv_type) && sv_type != SVt_PVGV;
280 return SVT_ARRAY(sv_type);
282 return SVT_HASH(sv_type);
284 return SVT_CODE(sv_type);
286 return SVT_IO(sv_type);
292 static HV *_get_namespace(SV *self)
301 call_method("namespace", G_SCALAR);
307 return (HV*)SvRV(ret);
310 static SV *_get_name(SV *self)
319 call_method("name", G_SCALAR);
328 static void _real_gv_init(GV *gv, HV *stash, SV *name)
333 name_pv = SvPV(name, name_len);
334 gv_init(gv, stash, name_pv, name_len, 1);
336 /* XXX: copied and pasted from gv_fetchpvn_flags and such */
337 /* ignoring the stuff for CORE:: and main:: for now, and also
338 * ignoring the GvMULTI_on bits, since we pass 1 to gv_init above */
339 switch (name_pv[0]) {
341 if (strEQ(&name_pv[1], "SA")) {
345 sv_magic(MUTABLE_SV(av), MUTABLE_SV(gv), PERL_MAGIC_isa,
350 if (strEQ(&name_pv[1], "VERLOAD")) {
354 hv_magic(hv, NULL, PERL_MAGIC_overload);
362 static void _expand_glob(SV *self, SV *varname)
368 namespace = _get_namespace(self);
370 if (entry = hv_fetch_ent(namespace, varname, 0, 0)) {
371 glob = (GV*)HeVAL(entry);
373 croak("_expand_glob called on stash slot with expanded glob");
377 _real_gv_init(glob, namespace, varname);
378 if (!hv_store_ent(namespace, varname, (SV*)glob, 0)) {
379 croak("hv_store failed");
384 croak("_expand_glob called on nonexistent stash slot");
388 static SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
394 namespace = _get_namespace(self);
395 entry = hv_fetch_ent(namespace, variable->name, vivify, 0);
399 glob = (GV*)(HeVAL(entry));
401 _expand_glob(self, variable->name);
404 switch (variable->type) {
407 GvSetSV(glob, newSV(0));
411 GvSetAV(glob, newAV());
415 GvSetHV(glob, newHV());
418 croak("Don't know how to vivify CODE variables");
421 GvSetIO(glob, newIO());
424 croak("Unknown type in vivication");
428 switch (variable->type) {
432 return (SV*)GvAV(glob);
434 return (SV*)GvHV(glob);
436 return (SV*)GvCV(glob);
438 return (SV*)GvIO(glob);
444 MODULE = Package::Stash::XS PACKAGE = Package::Stash::XS
455 if (SvPOK(package)) {
456 if (!_is_valid_module_name(package))
457 croak("%s is not a module name", SvPV_nolen(package));
461 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package), 0)) {
462 SvREFCNT_dec(package);
463 SvREFCNT_dec(instance);
464 croak("Couldn't initialize the 'name' key, hv_store failed");
467 else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) {
470 if (!hv_store(instance, "namespace", 9, SvREFCNT_inc_simple_NN(package), 0)) {
471 SvREFCNT_dec(package);
472 SvREFCNT_dec(instance);
473 croak("Couldn't initialize the 'namespace' key, hv_store failed");
477 croak("Package::Stash->new must be passed the name of the package to access");
480 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashsv(class, 0));
490 if (!sv_isobject(self))
491 croak("Can't call name as a class method");
492 if (slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash)) {
493 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
496 croak("Can't get the name of an anonymous package");
508 if (!sv_isobject(self))
509 croak("Can't call namespace as a class method");
510 #if PERL_VERSION < 10
511 package_name = _get_name(self);
512 RETVAL = newRV_inc((SV*)gv_stashpv(SvPV_nolen(package_name), GV_ADD));
514 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
516 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
522 package_name = _get_name(self);
523 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
524 nsref = newRV_inc((SV*)namespace);
526 if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) {
529 croak("Couldn't initialize the 'namespace' key, hv_store failed");
531 RETVAL = SvREFCNT_inc_simple_NN(nsref);
538 add_symbol(self, variable, initial=NULL, ...)
547 if (initial && !_valid_for_type(initial, variable.type))
548 croak("%s is not of type %s",
549 SvPV_nolen(initial), vartype_to_string(variable.type));
551 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
553 char *filename = NULL;
554 I32 first_line_num = -1, last_line_num = -1;
559 croak("add_symbol: Odd number of elements in %%opts");
561 for (i = 3; i < items; i += 2) {
563 key = SvPV_nolen(ST(i));
564 if (strEQ(key, "filename")) {
565 if (!SvPOK(ST(i + 1)))
566 croak("add_symbol: filename must be a string");
567 filename = SvPV_nolen(ST(i + 1));
569 else if (strEQ(key, "first_line_num")) {
570 if (!SvIOK(ST(i + 1)))
571 croak("add_symbol: first_line_num must be an integer");
572 first_line_num = SvIV(ST(i + 1));
574 else if (strEQ(key, "last_line_num")) {
575 if (!SvIOK(ST(i + 1)))
576 croak("add_symbol: last_line_num must be an integer");
577 last_line_num = SvIV(ST(i + 1));
581 if (!filename || first_line_num == -1) {
583 filename = CopFILE(PL_curcop);
584 if (first_line_num == -1)
585 first_line_num = PL_curcop->cop_line;
588 if (last_line_num == -1)
589 last_line_num = first_line_num;
591 name = newSVsv(_get_name(self));
592 sv_catpvs(name, "::");
593 sv_catsv(name, variable.name);
595 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
596 dbsub = get_hv("DB::sub", 1);
597 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
598 if (!hv_store_ent(dbsub, name, dbval, 0)) {
599 warn("Failed to update $DB::sub for subroutine %s",
607 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
608 * once' warnings in some situations... i can't reproduce this, but CMOP
610 namespace = _get_namespace(self);
611 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
613 glob = (GV*)HeVAL(entry);
616 glob = (GV*)newSV(0);
617 _real_gv_init(glob, namespace, variable.name);
618 if (!hv_store_ent(namespace, variable.name, (SV*)glob, 0)) {
619 croak("hv_store failed");
626 if (SvROK(initial)) {
628 SvREFCNT_inc_simple_void_NN(val);
631 val = newSVsv(initial);
634 switch (variable.type) {
654 remove_glob(self, name)
658 hv_delete_ent(_get_namespace(self), name, G_DISCARD, 0);
661 has_symbol(self, variable)
669 namespace = _get_namespace(self);
670 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
677 switch (variable.type) {
679 RETVAL = GvSVOK(glob) ? 1 : 0;
682 RETVAL = GvAVOK(glob) ? 1 : 0;
685 RETVAL = GvHVOK(glob) ? 1 : 0;
688 RETVAL = GvCVOK(glob) ? 1 : 0;
691 RETVAL = GvIOOK(glob) ? 1 : 0;
696 RETVAL = (variable.type == VAR_CODE);
702 get_symbol(self, variable)
708 val = _get_symbol(self, &variable, 0);
711 RETVAL = newRV_inc(val);
716 get_or_add_symbol(self, variable)
722 val = _get_symbol(self, &variable, 1);
725 RETVAL = newRV_inc(val);
730 remove_symbol(self, variable)
738 namespace = _get_namespace(self);
739 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
746 switch (variable.type) {
765 if (variable.type == VAR_CODE) {
766 hv_delete_ent(namespace, variable.name, G_DISCARD, 0);
771 list_all_symbols(self, vartype=VAR_NONE)
775 if (vartype == VAR_NONE) {
780 namespace = _get_namespace(self);
781 keys = hv_iterinit(namespace);
783 while ((entry = hv_iternext(namespace))) {
784 mPUSHs(newSVhe(entry));
793 namespace = _get_namespace(self);
794 hv_iterinit(namespace);
795 while ((val = hv_iternextsv(namespace, &key, &len))) {
821 else if (vartype == VAR_CODE) {
828 get_all_symbols(self, vartype=VAR_NONE)
837 namespace = _get_namespace(self);
840 hv_iterinit(namespace);
841 while ((val = hv_iternextsv(namespace, &key, &len))) {
845 SV *keysv = newSVpvn(key, len);
846 _expand_glob(self, keysv);
853 hv_store(ret, key, len, newRV_inc(GvSV(gv)), 0);
857 hv_store(ret, key, len, newRV_inc((SV*)GvAV(gv)), 0);
861 hv_store(ret, key, len, newRV_inc((SV*)GvHV(gv)), 0);
865 hv_store(ret, key, len, newRV_inc((SV*)GvCV(gv)), 0);
869 hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0);
872 hv_store(ret, key, len, SvREFCNT_inc_simple_NN(val), 0);
877 mPUSHs(newRV_noinc((SV*)ret));
881 const char *vmre = "\\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\\z";
882 #if (PERL_VERSION < 9) || ((PERL_VERSION == 9) && (PERL_SUBVERSION < 5))
885 fakepmop.op_pmflags = 0;
886 valid_module_regex = pregcomp(vmre, vmre + strlen(vmre), &fakepmop);
890 re = newSVpv(vmre, 0);
891 valid_module_regex = pregcomp(re, 0);
894 name_key = newSVpvs("name");
895 PERL_HASH(name_hash, "name", 4);
897 namespace_key = newSVpvs("namespace");
898 PERL_HASH(namespace_hash, "namespace", 9);
900 type_key = newSVpvs("type");
901 PERL_HASH(type_hash, "type", 4);