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 _real_gv_init(GV *gv, HV *stash, SV *name)
332 name_pv = SvPV(name, name_len);
333 gv_init(gv, stash, name_pv, name_len, 1);
335 /* XXX: copied and pasted from gv_fetchpvn_flags and such */
336 /* ignoring the stuff for CORE:: and main:: for now, and also
337 * ignoring the GvMULTI_on bits, since we pass 1 to gv_init above */
338 switch (name_pv[0]) {
340 if (strEQ(&name_pv[1], "SA")) {
344 sv_magic(MUTABLE_SV(av), MUTABLE_SV(gv), PERL_MAGIC_isa,
349 if (strEQ(&name_pv[1], "VERLOAD")) {
353 hv_magic(hv, NULL, PERL_MAGIC_overload);
361 static void _expand_glob(SV *self, SV *varname)
367 namespace = _get_namespace(self);
369 if (entry = hv_fetch_ent(namespace, varname, 0, 0)) {
370 glob = (GV*)HeVAL(entry);
372 croak("_expand_glob called on stash slot with expanded glob");
376 _real_gv_init(glob, namespace, varname);
377 if (!hv_store_ent(namespace, varname, (SV*)glob, 0)) {
378 croak("hv_store failed");
383 croak("_expand_glob called on nonexistent stash slot");
387 static SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
393 namespace = _get_namespace(self);
394 entry = hv_fetch_ent(namespace, variable->name, vivify, 0);
398 glob = (GV*)(HeVAL(entry));
400 _expand_glob(self, variable->name);
403 switch (variable->type) {
406 GvSetSV(glob, newSV(0));
410 GvSetAV(glob, newAV());
414 GvSetHV(glob, newHV());
417 croak("Don't know how to vivify CODE variables");
420 GvSetIO(glob, newIO());
423 croak("Unknown type in vivication");
427 switch (variable->type) {
431 return (SV*)GvAV(glob);
433 return (SV*)GvHV(glob);
435 return (SV*)GvCV(glob);
437 return (SV*)GvIO(glob);
443 MODULE = Package::Stash::XS PACKAGE = Package::Stash::XS
454 if (SvPOK(package)) {
455 if (!_is_valid_module_name(package))
456 croak("%s is not a module name", SvPV_nolen(package));
460 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package), 0)) {
461 SvREFCNT_dec(package);
462 SvREFCNT_dec(instance);
463 croak("Couldn't initialize the 'name' key, hv_store failed");
466 else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) {
469 if (!hv_store(instance, "namespace", 9, SvREFCNT_inc_simple_NN(package), 0)) {
470 SvREFCNT_dec(package);
471 SvREFCNT_dec(instance);
472 croak("Couldn't initialize the 'namespace' key, hv_store failed");
476 croak("Package::Stash->new must be passed the name of the package to access");
479 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashsv(class, 0));
489 if (!sv_isobject(self))
490 croak("Can't call name as a class method");
491 if (slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash)) {
492 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
495 croak("Can't get the name of an anonymous package");
507 if (!sv_isobject(self))
508 croak("Can't call namespace as a class method");
509 #if PERL_VERSION < 10
510 package_name = _get_name(self);
511 RETVAL = newRV_inc((SV*)gv_stashpv(SvPV_nolen(package_name), GV_ADD));
513 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
515 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
521 package_name = _get_name(self);
522 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
523 nsref = newRV_inc((SV*)namespace);
525 if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) {
528 croak("Couldn't initialize the 'namespace' key, hv_store failed");
530 RETVAL = SvREFCNT_inc_simple_NN(nsref);
537 add_symbol(self, variable, initial=NULL, ...)
546 if (initial && !_valid_for_type(initial, variable.type))
547 croak("%s is not of type %s",
548 SvPV_nolen(initial), vartype_to_string(variable.type));
550 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
552 char *filename = NULL;
553 I32 first_line_num = -1, last_line_num = -1;
558 croak("add_symbol: Odd number of elements in %%opts");
560 for (i = 3; i < items; i += 2) {
562 key = SvPV_nolen(ST(i));
563 if (strEQ(key, "filename")) {
564 if (!SvPOK(ST(i + 1)))
565 croak("add_symbol: filename must be a string");
566 filename = SvPV_nolen(ST(i + 1));
568 else if (strEQ(key, "first_line_num")) {
569 if (!SvIOK(ST(i + 1)))
570 croak("add_symbol: first_line_num must be an integer");
571 first_line_num = SvIV(ST(i + 1));
573 else if (strEQ(key, "last_line_num")) {
574 if (!SvIOK(ST(i + 1)))
575 croak("add_symbol: last_line_num must be an integer");
576 last_line_num = SvIV(ST(i + 1));
580 if (!filename || first_line_num == -1) {
582 filename = CopFILE(PL_curcop);
583 if (first_line_num == -1)
584 first_line_num = PL_curcop->cop_line;
587 if (last_line_num == -1)
588 last_line_num = first_line_num;
590 name = newSVsv(_get_name(self));
591 sv_catpvs(name, "::");
592 sv_catsv(name, variable.name);
594 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
595 dbsub = get_hv("DB::sub", 1);
596 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
597 if (!hv_store_ent(dbsub, name, dbval, 0)) {
598 warn("Failed to update $DB::sub for subroutine %s",
606 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
607 * once' warnings in some situations... i can't reproduce this, but CMOP
609 namespace = _get_namespace(self);
610 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
612 glob = (GV*)HeVAL(entry);
615 glob = (GV*)newSV(0);
616 _real_gv_init(glob, namespace, variable.name);
617 if (!hv_store_ent(namespace, variable.name, (SV*)glob, 0)) {
618 croak("hv_store failed");
625 if (SvROK(initial)) {
627 SvREFCNT_inc_simple_void_NN(val);
630 val = newSVsv(initial);
633 switch (variable.type) {
653 remove_glob(self, name)
657 hv_delete_ent(_get_namespace(self), name, G_DISCARD, 0);
660 has_symbol(self, variable)
668 namespace = _get_namespace(self);
669 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
676 switch (variable.type) {
678 RETVAL = GvSVOK(glob) ? 1 : 0;
681 RETVAL = GvAVOK(glob) ? 1 : 0;
684 RETVAL = GvHVOK(glob) ? 1 : 0;
687 RETVAL = GvCVOK(glob) ? 1 : 0;
690 RETVAL = GvIOOK(glob) ? 1 : 0;
695 RETVAL = (variable.type == VAR_CODE);
701 get_symbol(self, variable)
707 val = _get_symbol(self, &variable, 0);
710 RETVAL = newRV_inc(val);
715 get_or_add_symbol(self, variable)
721 val = _get_symbol(self, &variable, 1);
724 RETVAL = newRV_inc(val);
729 remove_symbol(self, variable)
737 namespace = _get_namespace(self);
738 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
745 switch (variable.type) {
764 if (variable.type == VAR_CODE) {
765 hv_delete_ent(namespace, variable.name, G_DISCARD, 0);
770 list_all_symbols(self, vartype=VAR_NONE)
774 if (vartype == VAR_NONE) {
779 namespace = _get_namespace(self);
780 keys = hv_iterinit(namespace);
782 while ((entry = hv_iternext(namespace))) {
783 mPUSHs(newSVhe(entry));
792 namespace = _get_namespace(self);
793 hv_iterinit(namespace);
794 while ((val = hv_iternextsv(namespace, &key, &len))) {
820 else if (vartype == VAR_CODE) {
827 get_all_symbols(self, vartype=VAR_NONE)
836 namespace = _get_namespace(self);
839 hv_iterinit(namespace);
840 while ((val = hv_iternextsv(namespace, &key, &len))) {
844 SV *keysv = newSVpvn(key, len);
845 _expand_glob(self, keysv);
852 hv_store(ret, key, len, newRV_inc(GvSV(gv)), 0);
856 hv_store(ret, key, len, newRV_inc((SV*)GvAV(gv)), 0);
860 hv_store(ret, key, len, newRV_inc((SV*)GvHV(gv)), 0);
864 hv_store(ret, key, len, newRV_inc((SV*)GvCV(gv)), 0);
868 hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0);
871 hv_store(ret, key, len, SvREFCNT_inc_simple_NN(val), 0);
876 mPUSHs(newRV_noinc((SV*)ret));
880 const char *vmre = "\\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\\z";
881 #if (PERL_VERSION < 9) || ((PERL_VERSION == 9) && (PERL_SUBVERSION < 5))
884 fakepmop.op_pmflags = 0;
885 valid_module_regex = pregcomp(vmre, vmre + strlen(vmre), &fakepmop);
889 re = newSVpv(vmre, 0);
890 valid_module_regex = pregcomp(re, 0);
893 name_key = newSVpvs("name");
894 PERL_HASH(name_hash, "name", 4);
896 namespace_key = newSVpvs("namespace");
897 PERL_HASH(namespace_hash, "namespace", 9);
899 type_key = newSVpvs("type");
900 PERL_HASH(type_hash, "type", 4);