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))
27 /* HACK: scalar slots are always populated on perl < 5.10, so treat undef
28 * as nonexistent. this is consistent with the previous behavior of the pure
29 * perl version of this module (since this is the behavior that perl sees
32 #define GvSVOK(g) (GvSV(g) && SvTYPE(GvSV(g)) != SVt_NULL)
34 #define GvSVOK(g) GvSV(g)
37 #define GvAVOK(g) GvAV(g)
38 #define GvHVOK(g) GvHV(g)
39 #define GvCVOK(g) GvCVu(g) /* XXX: should this really be GvCVu? or GvCV? */
40 #define GvIOOK(g) GvIO(g)
42 /* see above - don't let scalar slots become unpopulated, this breaks
43 * assumptions in core */
45 #define GvSetSV(g,v) do { \
47 SvREFCNT_dec(GvSV(g)); \
48 if ((GvSV(g) = _v ? _v : newSV(0))) \
49 GvIMPORTED_SV_on(g); \
52 #define GvSetSV(g,v) do { \
53 SvREFCNT_dec(GvSV(g)); \
54 if ((GvSV(g) = (SV*)(v))) \
55 GvIMPORTED_SV_on(g); \
59 #define GvSetAV(g,v) do { \
60 SvREFCNT_dec(GvAV(g)); \
61 if ((GvAV(g) = (AV*)(v))) \
62 GvIMPORTED_AV_on(g); \
64 #define GvSetHV(g,v) do { \
65 SvREFCNT_dec(GvHV(g)); \
66 if ((GvHV(g) = (HV*)(v))) \
67 GvIMPORTED_HV_on(g); \
69 #define GvSetCV(g,v) do { \
70 SvREFCNT_dec(GvCV(g)); \
71 if ((GvCV(g) = (CV*)(v))) { \
72 GvIMPORTED_CV_on(g); \
76 mro_method_changed_in(GvSTASH(g)); \
78 #define GvSetIO(g,v) do { \
79 SvREFCNT_dec(GvIO(g)); \
80 GvIOp(g) = (IO*)(v); \
83 /* XXX: the core implementation of caller() is private, so we need a
84 * a reimplementation. luckily, padwalker already has done this. rafl says
85 * that there should be a public interface in 5.14, so maybe look into
86 * converting to use that at some point */
87 #include "stolen_bits_of_padwalker.c"
96 VAR_GLOB, /* TODO: unimplemented */
97 VAR_FORMAT /* TODO: unimplemented */
106 static U32 name_hash, namespace_hash, type_hash;
107 static SV *name_key, *namespace_key, *type_key;
109 const char *vartype_to_string(vartype_t type)
127 I32 vartype_to_svtype(vartype_t type)
131 return SVt_PV; /* or whatever */
145 vartype_t string_to_vartype(char *vartype)
147 if (strEQ(vartype, "SCALAR")) {
150 else if (strEQ(vartype, "ARRAY")) {
153 else if (strEQ(vartype, "HASH")) {
156 else if (strEQ(vartype, "CODE")) {
159 else if (strEQ(vartype, "IO")) {
163 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO'");
167 void _deconstruct_variable_name(SV *varsv, varspec_t *varspec)
172 variable = SvPV(varsv, len);
174 croak("You must pass a variable name");
176 switch (variable[0]) {
178 varspec->type = VAR_SCALAR;
179 varspec->name = &variable[1];
180 varspec->namelen = len - 1;
183 varspec->type = VAR_ARRAY;
184 varspec->name = &variable[1];
185 varspec->namelen = len - 1;
188 varspec->type = VAR_HASH;
189 varspec->name = &variable[1];
190 varspec->namelen = len - 1;
193 varspec->type = VAR_CODE;
194 varspec->name = &variable[1];
195 varspec->namelen = len - 1;
198 varspec->type = VAR_IO;
199 varspec->name = variable;
200 varspec->namelen = len;
205 void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
211 val = hv_fetch_ent(variable, name_key, 0, name_hash);
213 croak("The 'name' key is required in variable specs");
215 valpv = HePV(val, len);
216 varspec->name = savepvn(valpv, len);
217 SAVEFREEPV(varspec->name);
218 varspec->namelen = len;
220 val = hv_fetch_ent(variable, type_key, 0, type_hash);
222 croak("The 'type' key is required in variable specs");
224 valpv = HePV(val, len);
225 varspec->type = string_to_vartype(valpv);
228 int _valid_for_type(SV *value, vartype_t type)
230 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
234 return sv_type == SVt_NULL ||
240 return sv_type == SVt_PVAV;
242 return sv_type == SVt_PVHV;
244 return sv_type == SVt_PVCV;
246 return sv_type == SVt_PVIO;
252 HV *_get_namespace(SV *self)
261 call_method("namespace", G_SCALAR);
267 return (HV*)SvRV(ret);
270 SV *_get_name(SV *self)
279 call_method("name", G_SCALAR);
288 void _expand_glob(SV *self, char *name)
292 namesv = newSVsv(_get_name(self));
293 sv_catpvs(namesv, "::");
294 sv_catpv(namesv, name);
296 /* can't use gv_init here, because it screws up @ISA in a way that I
297 * can't reproduce, but that CMOP triggers */
298 gv_fetchsv(namesv, GV_ADD, SVt_NULL);
299 SvREFCNT_dec(namesv);
302 SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
308 namespace = _get_namespace(self);
309 entry = hv_fetch(namespace, variable->name, variable->namelen, vivify);
313 glob = (GV*)(*entry);
315 _expand_glob(self, variable->name);
318 switch (variable->type) {
321 GvSetSV(glob, newSV(0));
325 GvSetAV(glob, newAV());
329 GvSetHV(glob, newHV());
332 croak("Don't know how to vivify CODE variables");
335 GvSetIO(glob, newIO());
338 croak("Unknown type in vivication");
342 switch (variable->type) {
346 return (SV*)GvAV(glob);
348 return (SV*)GvHV(glob);
350 return (SV*)GvCV(glob);
352 return (SV*)GvIO(glob);
358 MODULE = Package::Stash PACKAGE = Package::Stash
363 new(class, package_name)
371 if (!SvPOK(package_name))
372 croak("The constructor argument must be the name of a package");
376 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
377 SvREFCNT_dec(package_name);
378 SvREFCNT_dec(instance);
379 croak("Couldn't initialize the 'name' key, hv_store failed");
381 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
382 nsref = newRV_inc((SV*)namespace);
383 if (!hv_store(instance, "namespace", 9, nsref, 0)) {
385 SvREFCNT_dec(instance);
386 croak("Couldn't initialize the 'namespace' key, hv_store failed");
389 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashpv(class, 0));
399 if (!sv_isobject(self))
400 croak("Can't call name as a class method");
401 slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash);
402 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
412 if (!sv_isobject(self))
413 croak("Can't call namespace as a class method");
414 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
415 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
420 add_symbol(self, variable, initial=NULL, ...)
428 if (initial && !_valid_for_type(initial, variable.type))
429 croak("%s is not of type %s",
430 SvPV_nolen(initial), vartype_to_string(variable.type));
432 name = newSVsv(_get_name(self));
433 sv_catpvs(name, "::");
434 sv_catpv(name, variable.name);
436 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
438 char *filename = NULL, *namepv;
439 I32 first_line_num = -1, last_line_num = -1;
445 croak("add_symbol: Odd number of elements in %%opts");
447 for (i = 3; i < items; i += 2) {
449 key = SvPV_nolen(ST(i));
450 if (strEQ(key, "filename")) {
451 if (!SvPOK(ST(i + 1)))
452 croak("add_symbol: filename must be a string");
453 filename = SvPV_nolen(ST(i + 1));
455 else if (strEQ(key, "first_line_num")) {
456 if (!SvIOK(ST(i + 1)))
457 croak("add_symbol: first_line_num must be an integer");
458 first_line_num = SvIV(ST(i + 1));
460 else if (strEQ(key, "last_line_num")) {
461 if (!SvIOK(ST(i + 1)))
462 croak("add_symbol: last_line_num must be an integer");
463 last_line_num = SvIV(ST(i + 1));
467 if (!filename || first_line_num == -1) {
468 I32 cxix_from, cxix_to;
469 PERL_CONTEXT *cx, *ccstack;
472 cx = upcontext(0, &cop, &ccstack, &cxix_from, &cxix_to);
477 filename = CopFILE(cop);
478 if (first_line_num == -1)
479 first_line_num = cop->cop_line;
482 if (last_line_num == -1)
483 last_line_num = first_line_num;
485 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
486 dbsub = get_hv("DB::sub", 1);
487 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
488 namepv = SvPV(name, namelen);
489 if (!hv_store(dbsub, namepv, namelen, dbval, 0)) {
490 warn("Failed to update $DB::sub for subroutine %s", namepv);
495 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
496 * once' warnings in some situations... i can't reproduce this, but CMOP
498 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
503 if (SvROK(initial)) {
505 SvREFCNT_inc_simple_void_NN(val);
508 val = newSVsv(initial);
511 switch (variable.type) {
533 remove_glob(self, namesv)
540 name = SvPV(namesv, len);
541 hv_delete(_get_namespace(self), name, len, G_DISCARD);
544 has_symbol(self, variable)
551 namespace = _get_namespace(self);
552 entry = hv_fetch(namespace, variable.name, variable.namelen, 0);
557 GV *glob = (GV*)(*entry);
558 switch (variable.type) {
560 RETVAL = GvSVOK(glob) ? 1 : 0;
563 RETVAL = GvAVOK(glob) ? 1 : 0;
566 RETVAL = GvHVOK(glob) ? 1 : 0;
569 RETVAL = GvCVOK(glob) ? 1 : 0;
572 RETVAL = GvIOOK(glob) ? 1 : 0;
577 RETVAL = (variable.type == VAR_CODE);
583 get_symbol(self, variable)
589 val = _get_symbol(self, &variable, 0);
592 RETVAL = newRV_inc(val);
597 get_or_add_symbol(self, variable)
603 val = _get_symbol(self, &variable, 1);
606 RETVAL = newRV_inc(val);
611 remove_symbol(self, variable)
618 namespace = _get_namespace(self);
619 entry = hv_fetch(namespace, variable.name, variable.namelen, 0);
624 GV *glob = (GV*)(*entry);
625 switch (variable.type) {
644 if (variable.type == VAR_CODE) {
645 hv_delete(namespace, variable.name, variable.namelen, G_DISCARD);
650 list_all_symbols(self, vartype=VAR_NONE)
654 if (vartype == VAR_NONE) {
659 namespace = _get_namespace(self);
660 keys = hv_iterinit(namespace);
662 while ((entry = hv_iternext(namespace))) {
663 mPUSHs(newSVhe(entry));
672 namespace = _get_namespace(self);
673 hv_iterinit(namespace);
674 while ((val = hv_iternextsv(namespace, &key, &len))) {
700 else if (vartype == VAR_CODE) {
708 name_key = newSVpvs("name");
709 PERL_HASH(name_hash, "name", 4);
711 namespace_key = newSVpvs("namespace");
712 PERL_HASH(namespace_hash, "namespace", 9);
714 type_key = newSVpvs("type");
715 PERL_HASH(type_hash, "type", 4);