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 mro_method_changed_in(GvSTASH(g)); \
106 #define GvSetIO(g,v) do { \
107 SvREFCNT_dec(GvIO(g)); \
108 GvIOp(g) = (IO*)(v); \
118 VAR_GLOB, /* TODO: unimplemented */
119 VAR_FORMAT /* TODO: unimplemented */
127 static U32 name_hash, namespace_hash, type_hash;
128 static SV *name_key, *namespace_key, *type_key;
129 static REGEXP *valid_module_regex;
131 static const char *vartype_to_string(vartype_t type)
149 static I32 vartype_to_svtype(vartype_t type)
153 return SVt_PV; /* or whatever */
167 static vartype_t string_to_vartype(char *vartype)
169 if (strEQ(vartype, "SCALAR")) {
172 else if (strEQ(vartype, "ARRAY")) {
175 else if (strEQ(vartype, "HASH")) {
178 else if (strEQ(vartype, "CODE")) {
181 else if (strEQ(vartype, "IO")) {
185 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO', not '%s'", vartype);
189 static int _is_valid_module_name(SV *package)
195 buf = SvPV(package, len);
197 /* whee cargo cult */
199 sv_upgrade(sv, SVt_PV);
207 return pregexec(valid_module_regex, buf, buf + len, buf, 1, sv, 1);
210 static void _deconstruct_variable_name(SV *variable, varspec_t *varspec)
214 if (!SvCUR(variable))
215 croak("You must pass a variable name");
217 varspec->name = sv_2mortal(newSVsv(variable));
219 varpv = SvPV_nolen(varspec->name);
222 varspec->type = VAR_SCALAR;
223 sv_chop(varspec->name, &varpv[1]);
226 varspec->type = VAR_ARRAY;
227 sv_chop(varspec->name, &varpv[1]);
230 varspec->type = VAR_HASH;
231 sv_chop(varspec->name, &varpv[1]);
234 varspec->type = VAR_CODE;
235 sv_chop(varspec->name, &varpv[1]);
238 varspec->type = VAR_IO;
243 static void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
247 val = hv_fetch_ent(variable, name_key, 0, name_hash);
249 croak("The 'name' key is required in variable specs");
251 varspec->name = sv_2mortal(newSVsv(HeVAL(val)));
253 val = hv_fetch_ent(variable, type_key, 0, type_hash);
255 croak("The 'type' key is required in variable specs");
257 varspec->type = string_to_vartype(SvPV_nolen(HeVAL(val)));
260 static void _check_varspec_is_valid(varspec_t *varspec)
262 if (strstr(SvPV_nolen(varspec->name), "::")) {
263 croak("Variable names may not contain ::");
267 static int _valid_for_type(SV *value, vartype_t type)
269 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
273 /* XXX is a glob a scalar? assigning a glob to the scalar slot seems
274 * to work here, but in pure perl i'm pretty sure it goes to the EGV
275 * slot, which seems more correct to me. just disable it for now
277 return SVT_SCALAR(sv_type) && sv_type != SVt_PVGV;
279 return SVT_ARRAY(sv_type);
281 return SVT_HASH(sv_type);
283 return SVT_CODE(sv_type);
285 return SVT_IO(sv_type);
291 static HV *_get_namespace(SV *self)
300 call_method("namespace", G_SCALAR);
306 return (HV*)SvRV(ret);
309 static SV *_get_name(SV *self)
318 call_method("name", G_SCALAR);
327 static void _expand_glob(SV *self, SV *varname)
333 namespace = _get_namespace(self);
335 if (entry = hv_fetch_ent(namespace, varname, 0, 0)) {
336 glob = (GV*)HeVAL(entry);
338 croak("_expand_glob called on stash slot with expanded glob");
344 varname_pv = SvPV(varname, varname_len);
345 gv_init(glob, namespace, varname_pv, varname_len, 1);
347 if (!hv_store_ent(namespace, varname, (SV*)glob, 0)) {
348 croak("hv_store failed");
353 croak("_expand_glob called on nonexistent stash slot");
357 static SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
363 namespace = _get_namespace(self);
364 entry = hv_fetch_ent(namespace, variable->name, vivify, 0);
368 glob = (GV*)(HeVAL(entry));
370 _expand_glob(self, variable->name);
373 switch (variable->type) {
376 GvSetSV(glob, newSV(0));
380 GvSetAV(glob, newAV());
384 GvSetHV(glob, newHV());
387 croak("Don't know how to vivify CODE variables");
390 GvSetIO(glob, newIO());
393 croak("Unknown type in vivication");
397 switch (variable->type) {
401 return (SV*)GvAV(glob);
403 return (SV*)GvHV(glob);
405 return (SV*)GvCV(glob);
407 return (SV*)GvIO(glob);
413 MODULE = Package::Stash::XS PACKAGE = Package::Stash::XS
424 if (SvPOK(package)) {
425 if (!_is_valid_module_name(package))
426 croak("%s is not a module name", SvPV_nolen(package));
430 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package), 0)) {
431 SvREFCNT_dec(package);
432 SvREFCNT_dec(instance);
433 croak("Couldn't initialize the 'name' key, hv_store failed");
436 else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) {
439 if (!hv_store(instance, "namespace", 9, SvREFCNT_inc_simple_NN(package), 0)) {
440 SvREFCNT_dec(package);
441 SvREFCNT_dec(instance);
442 croak("Couldn't initialize the 'namespace' key, hv_store failed");
446 croak("Package::Stash->new must be passed the name of the package to access");
449 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashsv(class, 0));
459 if (!sv_isobject(self))
460 croak("Can't call name as a class method");
461 if (slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash)) {
462 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
465 croak("Can't get the name of an anonymous package");
477 if (!sv_isobject(self))
478 croak("Can't call namespace as a class method");
479 #if PERL_VERSION < 10
480 package_name = _get_name(self);
481 RETVAL = newRV_inc((SV*)gv_stashpv(SvPV_nolen(package_name), GV_ADD));
483 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
485 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
491 package_name = _get_name(self);
492 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
493 nsref = newRV_inc((SV*)namespace);
495 if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) {
498 croak("Couldn't initialize the 'namespace' key, hv_store failed");
500 RETVAL = SvREFCNT_inc_simple_NN(nsref);
507 add_symbol(self, variable, initial=NULL, ...)
516 if (initial && !_valid_for_type(initial, variable.type))
517 croak("%s is not of type %s",
518 SvPV_nolen(initial), vartype_to_string(variable.type));
520 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
522 char *filename = NULL;
523 I32 first_line_num = -1, last_line_num = -1;
528 croak("add_symbol: Odd number of elements in %%opts");
530 for (i = 3; i < items; i += 2) {
532 key = SvPV_nolen(ST(i));
533 if (strEQ(key, "filename")) {
534 if (!SvPOK(ST(i + 1)))
535 croak("add_symbol: filename must be a string");
536 filename = SvPV_nolen(ST(i + 1));
538 else if (strEQ(key, "first_line_num")) {
539 if (!SvIOK(ST(i + 1)))
540 croak("add_symbol: first_line_num must be an integer");
541 first_line_num = SvIV(ST(i + 1));
543 else if (strEQ(key, "last_line_num")) {
544 if (!SvIOK(ST(i + 1)))
545 croak("add_symbol: last_line_num must be an integer");
546 last_line_num = SvIV(ST(i + 1));
550 if (!filename || first_line_num == -1) {
552 filename = CopFILE(PL_curcop);
553 if (first_line_num == -1)
554 first_line_num = PL_curcop->cop_line;
557 if (last_line_num == -1)
558 last_line_num = first_line_num;
560 name = newSVsv(_get_name(self));
561 sv_catpvs(name, "::");
562 sv_catsv(name, variable.name);
564 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
565 dbsub = get_hv("DB::sub", 1);
566 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
567 if (!hv_store_ent(dbsub, name, dbval, 0)) {
568 warn("Failed to update $DB::sub for subroutine %s",
576 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
577 * once' warnings in some situations... i can't reproduce this, but CMOP
579 namespace = _get_namespace(self);
580 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
582 glob = (GV*)HeVAL(entry);
585 glob = (GV*)newSV(0);
586 gv_init(glob, namespace, "ANON", 4, 1);
587 if (!hv_store_ent(namespace, variable.name, (SV*)glob, 0)) {
588 croak("hv_store failed");
595 if (SvROK(initial)) {
597 SvREFCNT_inc_simple_void_NN(val);
600 val = newSVsv(initial);
603 switch (variable.type) {
623 remove_glob(self, name)
627 hv_delete_ent(_get_namespace(self), name, G_DISCARD, 0);
630 has_symbol(self, variable)
638 namespace = _get_namespace(self);
639 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
646 switch (variable.type) {
648 RETVAL = GvSVOK(glob) ? 1 : 0;
651 RETVAL = GvAVOK(glob) ? 1 : 0;
654 RETVAL = GvHVOK(glob) ? 1 : 0;
657 RETVAL = GvCVOK(glob) ? 1 : 0;
660 RETVAL = GvIOOK(glob) ? 1 : 0;
665 RETVAL = (variable.type == VAR_CODE);
671 get_symbol(self, variable)
677 val = _get_symbol(self, &variable, 0);
680 RETVAL = newRV_inc(val);
685 get_or_add_symbol(self, variable)
691 val = _get_symbol(self, &variable, 1);
694 RETVAL = newRV_inc(val);
699 remove_symbol(self, variable)
707 namespace = _get_namespace(self);
708 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
715 switch (variable.type) {
734 if (variable.type == VAR_CODE) {
735 hv_delete_ent(namespace, variable.name, G_DISCARD, 0);
740 list_all_symbols(self, vartype=VAR_NONE)
744 if (vartype == VAR_NONE) {
749 namespace = _get_namespace(self);
750 keys = hv_iterinit(namespace);
752 while ((entry = hv_iternext(namespace))) {
753 mPUSHs(newSVhe(entry));
762 namespace = _get_namespace(self);
763 hv_iterinit(namespace);
764 while ((val = hv_iternextsv(namespace, &key, &len))) {
790 else if (vartype == VAR_CODE) {
797 get_all_symbols(self, vartype=VAR_NONE)
806 namespace = _get_namespace(self);
809 hv_iterinit(namespace);
810 while ((val = hv_iternextsv(namespace, &key, &len))) {
814 SV *keysv = newSVpvn(key, len);
815 _expand_glob(self, keysv);
822 hv_store(ret, key, len, newRV_inc(GvSV(gv)), 0);
826 hv_store(ret, key, len, newRV_inc((SV*)GvAV(gv)), 0);
830 hv_store(ret, key, len, newRV_inc((SV*)GvHV(gv)), 0);
834 hv_store(ret, key, len, newRV_inc((SV*)GvCV(gv)), 0);
838 hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0);
841 hv_store(ret, key, len, SvREFCNT_inc_simple_NN(val), 0);
846 mPUSHs(newRV_noinc((SV*)ret));
850 const char *vmre = "\\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\\z";
851 #if (PERL_VERSION < 9) || ((PERL_VERSION == 9) && (PERL_SUBVERSION < 5))
854 fakepmop.op_pmflags = 0;
855 valid_module_regex = pregcomp(vmre, vmre + strlen(vmre), &fakepmop);
859 re = newSVpv(vmre, 0);
860 valid_module_regex = pregcomp(re, 0);
863 name_key = newSVpvs("name");
864 PERL_HASH(name_hash, "name", 4);
866 namespace_key = newSVpvs("namespace");
867 PERL_HASH(namespace_hash, "namespace", 9);
869 type_key = newSVpvs("type");
870 PERL_HASH(type_hash, "type", 4);