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 _expand_glob(SV *self, SV *varname)
334 namespace = _get_namespace(self);
336 if (entry = hv_fetch_ent(namespace, varname, 0, 0)) {
337 glob = (GV*)HeVAL(entry);
339 croak("_expand_glob called on stash slot with expanded glob");
345 varname_pv = SvPV(varname, varname_len);
346 gv_init(glob, namespace, varname_pv, varname_len, 1);
348 if (!hv_store_ent(namespace, varname, (SV*)glob, 0)) {
349 croak("hv_store failed");
354 croak("_expand_glob called on nonexistent stash slot");
358 static SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
364 namespace = _get_namespace(self);
365 entry = hv_fetch_ent(namespace, variable->name, vivify, 0);
369 glob = (GV*)(HeVAL(entry));
371 _expand_glob(self, variable->name);
374 switch (variable->type) {
377 GvSetSV(glob, newSV(0));
381 GvSetAV(glob, newAV());
385 GvSetHV(glob, newHV());
388 croak("Don't know how to vivify CODE variables");
391 GvSetIO(glob, newIO());
394 croak("Unknown type in vivication");
398 switch (variable->type) {
402 return (SV*)GvAV(glob);
404 return (SV*)GvHV(glob);
406 return (SV*)GvCV(glob);
408 return (SV*)GvIO(glob);
414 MODULE = Package::Stash::XS PACKAGE = Package::Stash::XS
425 if (SvPOK(package)) {
426 if (!_is_valid_module_name(package))
427 croak("%s is not a module name", SvPV_nolen(package));
431 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package), 0)) {
432 SvREFCNT_dec(package);
433 SvREFCNT_dec(instance);
434 croak("Couldn't initialize the 'name' key, hv_store failed");
437 else if (SvROK(package) && SvTYPE(SvRV(package)) == SVt_PVHV) {
440 if (!hv_store(instance, "namespace", 9, SvREFCNT_inc_simple_NN(package), 0)) {
441 SvREFCNT_dec(package);
442 SvREFCNT_dec(instance);
443 croak("Couldn't initialize the 'namespace' key, hv_store failed");
447 croak("Package::Stash->new must be passed the name of the package to access");
450 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashsv(class, 0));
460 if (!sv_isobject(self))
461 croak("Can't call name as a class method");
462 if (slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash)) {
463 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
466 croak("Can't get the name of an anonymous package");
478 if (!sv_isobject(self))
479 croak("Can't call namespace as a class method");
480 #if PERL_VERSION < 10
481 package_name = _get_name(self);
482 RETVAL = newRV_inc((SV*)gv_stashpv(SvPV_nolen(package_name), GV_ADD));
484 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
486 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
492 package_name = _get_name(self);
493 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
494 nsref = newRV_inc((SV*)namespace);
496 if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) {
499 croak("Couldn't initialize the 'namespace' key, hv_store failed");
501 RETVAL = SvREFCNT_inc_simple_NN(nsref);
508 add_symbol(self, variable, initial=NULL, ...)
517 if (initial && !_valid_for_type(initial, variable.type))
518 croak("%s is not of type %s",
519 SvPV_nolen(initial), vartype_to_string(variable.type));
521 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
523 char *filename = NULL;
524 I32 first_line_num = -1, last_line_num = -1;
529 croak("add_symbol: Odd number of elements in %%opts");
531 for (i = 3; i < items; i += 2) {
533 key = SvPV_nolen(ST(i));
534 if (strEQ(key, "filename")) {
535 if (!SvPOK(ST(i + 1)))
536 croak("add_symbol: filename must be a string");
537 filename = SvPV_nolen(ST(i + 1));
539 else if (strEQ(key, "first_line_num")) {
540 if (!SvIOK(ST(i + 1)))
541 croak("add_symbol: first_line_num must be an integer");
542 first_line_num = SvIV(ST(i + 1));
544 else if (strEQ(key, "last_line_num")) {
545 if (!SvIOK(ST(i + 1)))
546 croak("add_symbol: last_line_num must be an integer");
547 last_line_num = SvIV(ST(i + 1));
551 if (!filename || first_line_num == -1) {
553 filename = CopFILE(PL_curcop);
554 if (first_line_num == -1)
555 first_line_num = PL_curcop->cop_line;
558 if (last_line_num == -1)
559 last_line_num = first_line_num;
561 name = newSVsv(_get_name(self));
562 sv_catpvs(name, "::");
563 sv_catsv(name, variable.name);
565 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
566 dbsub = get_hv("DB::sub", 1);
567 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
568 if (!hv_store_ent(dbsub, name, dbval, 0)) {
569 warn("Failed to update $DB::sub for subroutine %s",
577 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
578 * once' warnings in some situations... i can't reproduce this, but CMOP
580 namespace = _get_namespace(self);
581 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
583 glob = (GV*)HeVAL(entry);
588 glob = (GV*)newSV(0);
589 varname_pv = SvPV(variable.name, varname_len);
590 gv_init(glob, namespace, varname_pv, varname_len, 1);
591 if (!hv_store_ent(namespace, variable.name, (SV*)glob, 0)) {
592 croak("hv_store failed");
599 if (SvROK(initial)) {
601 SvREFCNT_inc_simple_void_NN(val);
604 val = newSVsv(initial);
607 switch (variable.type) {
627 remove_glob(self, name)
631 hv_delete_ent(_get_namespace(self), name, G_DISCARD, 0);
634 has_symbol(self, variable)
642 namespace = _get_namespace(self);
643 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
650 switch (variable.type) {
652 RETVAL = GvSVOK(glob) ? 1 : 0;
655 RETVAL = GvAVOK(glob) ? 1 : 0;
658 RETVAL = GvHVOK(glob) ? 1 : 0;
661 RETVAL = GvCVOK(glob) ? 1 : 0;
664 RETVAL = GvIOOK(glob) ? 1 : 0;
669 RETVAL = (variable.type == VAR_CODE);
675 get_symbol(self, variable)
681 val = _get_symbol(self, &variable, 0);
684 RETVAL = newRV_inc(val);
689 get_or_add_symbol(self, variable)
695 val = _get_symbol(self, &variable, 1);
698 RETVAL = newRV_inc(val);
703 remove_symbol(self, variable)
711 namespace = _get_namespace(self);
712 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
719 switch (variable.type) {
738 if (variable.type == VAR_CODE) {
739 hv_delete_ent(namespace, variable.name, G_DISCARD, 0);
744 list_all_symbols(self, vartype=VAR_NONE)
748 if (vartype == VAR_NONE) {
753 namespace = _get_namespace(self);
754 keys = hv_iterinit(namespace);
756 while ((entry = hv_iternext(namespace))) {
757 mPUSHs(newSVhe(entry));
766 namespace = _get_namespace(self);
767 hv_iterinit(namespace);
768 while ((val = hv_iternextsv(namespace, &key, &len))) {
794 else if (vartype == VAR_CODE) {
801 get_all_symbols(self, vartype=VAR_NONE)
810 namespace = _get_namespace(self);
813 hv_iterinit(namespace);
814 while ((val = hv_iternextsv(namespace, &key, &len))) {
818 SV *keysv = newSVpvn(key, len);
819 _expand_glob(self, keysv);
826 hv_store(ret, key, len, newRV_inc(GvSV(gv)), 0);
830 hv_store(ret, key, len, newRV_inc((SV*)GvAV(gv)), 0);
834 hv_store(ret, key, len, newRV_inc((SV*)GvHV(gv)), 0);
838 hv_store(ret, key, len, newRV_inc((SV*)GvCV(gv)), 0);
842 hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0);
845 hv_store(ret, key, len, SvREFCNT_inc_simple_NN(val), 0);
850 mPUSHs(newRV_noinc((SV*)ret));
854 const char *vmre = "\\A[0-9A-Z_a-z]+(?:::[0-9A-Z_a-z]+)*\\z";
855 #if (PERL_VERSION < 9) || ((PERL_VERSION == 9) && (PERL_SUBVERSION < 5))
858 fakepmop.op_pmflags = 0;
859 valid_module_regex = pregcomp(vmre, vmre + strlen(vmre), &fakepmop);
863 re = newSVpv(vmre, 0);
864 valid_module_regex = pregcomp(re, 0);
867 name_key = newSVpvs("name");
868 PERL_HASH(name_hash, "name", 4);
870 namespace_key = newSVpvs("namespace");
871 PERL_HASH(namespace_hash, "namespace", 9);
873 type_key = newSVpvs("type");
874 PERL_HASH(type_hash, "type", 4);