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 */
105 static U32 name_hash, namespace_hash, type_hash;
106 static SV *name_key, *namespace_key, *type_key;
108 const char *vartype_to_string(vartype_t type)
126 I32 vartype_to_svtype(vartype_t type)
130 return SVt_PV; /* or whatever */
144 vartype_t string_to_vartype(char *vartype)
146 if (strEQ(vartype, "SCALAR")) {
149 else if (strEQ(vartype, "ARRAY")) {
152 else if (strEQ(vartype, "HASH")) {
155 else if (strEQ(vartype, "CODE")) {
158 else if (strEQ(vartype, "IO")) {
162 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO'");
166 void _deconstruct_variable_name(char *variable, varspec_t *varspec)
168 if (!variable || !variable[0])
169 croak("You must pass a variable name");
171 varspec->type = VAR_NONE;
173 switch (variable[0]) {
175 varspec->type = VAR_SCALAR;
178 varspec->type = VAR_ARRAY;
181 varspec->type = VAR_HASH;
184 varspec->type = VAR_CODE;
188 if (varspec->type != VAR_NONE) {
189 varspec->name = &variable[1];
192 varspec->type = VAR_IO;
193 varspec->name = variable;
197 void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
203 val = hv_fetch_ent(variable, name_key, 0, name_hash);
205 croak("The 'name' key is required in variable specs");
207 valpv = HePV(val, len);
208 varspec->name = savepvn(valpv, len);
209 SAVEFREEPV(varspec->name);
211 val = hv_fetch_ent(variable, type_key, 0, type_hash);
213 croak("The 'type' key is required in variable specs");
215 valpv = HePV(val, len);
216 varspec->type = string_to_vartype(valpv);
219 int _valid_for_type(SV *value, vartype_t type)
221 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
225 return sv_type == SVt_NULL ||
231 return sv_type == SVt_PVAV;
233 return sv_type == SVt_PVHV;
235 return sv_type == SVt_PVCV;
237 return sv_type == SVt_PVIO;
243 HV *_get_namespace(SV *self)
252 call_method("namespace", G_SCALAR);
258 return (HV*)SvRV(ret);
261 SV *_get_name(SV *self)
270 call_method("name", G_SCALAR);
279 SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
285 namespace = _get_namespace(self);
286 entry = hv_fetch(namespace, variable->name, strlen(variable->name), vivify);
290 glob = (GV*)(*entry);
294 namesv = newSVsv(_get_name(self));
295 sv_catpvs(namesv, "::");
296 sv_catpv(namesv, variable->name);
298 /* can't use gv_init here, because it screws up @ISA in a way that I
299 * can't reproduce, but that CMOP triggers */
300 gv_fetchsv(namesv, GV_ADD, vartype_to_svtype(variable->type));
301 SvREFCNT_dec(namesv);
305 switch (variable->type) {
308 GvSetSV(glob, newSV(0));
312 GvSetAV(glob, newAV());
316 GvSetHV(glob, newHV());
319 croak("Don't know how to vivify CODE variables");
322 GvSetIO(glob, newIO());
325 croak("Unknown type in vivication");
329 switch (variable->type) {
333 return (SV*)GvAV(glob);
335 return (SV*)GvHV(glob);
337 return (SV*)GvCV(glob);
339 return (SV*)GvIO(glob);
345 MODULE = Package::Stash PACKAGE = Package::Stash
350 new(class, package_name)
358 if (!SvPOK(package_name))
359 croak("The constructor argument must be the name of a package");
363 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
364 SvREFCNT_dec(package_name);
365 SvREFCNT_dec(instance);
366 croak("Couldn't initialize the 'name' key, hv_store failed");
368 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
369 nsref = newRV_inc((SV*)namespace);
370 if (!hv_store(instance, "namespace", 9, nsref, 0)) {
372 SvREFCNT_dec(instance);
373 croak("Couldn't initialize the 'namespace' key, hv_store failed");
376 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashpv(class, 0));
386 if (!sv_isobject(self))
387 croak("Can't call name as a class method");
388 slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash);
389 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
399 if (!sv_isobject(self))
400 croak("Can't call namespace as a class method");
401 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
402 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
407 add_symbol(self, variable, initial=NULL, ...)
415 if (initial && !_valid_for_type(initial, variable.type))
416 croak("%s is not of type %s",
417 SvPV_nolen(initial), vartype_to_string(variable.type));
419 name = newSVsv(_get_name(self));
420 sv_catpvs(name, "::");
421 sv_catpv(name, variable.name);
423 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
425 char *filename = NULL, *namepv;
426 I32 first_line_num = -1, last_line_num = -1;
432 croak("add_symbol: Odd number of elements in %%opts");
434 for (i = 3; i < items; i += 2) {
436 key = SvPV_nolen(ST(i));
437 if (strEQ(key, "filename")) {
438 if (!SvPOK(ST(i + 1)))
439 croak("add_symbol: filename must be a string");
440 filename = SvPV_nolen(ST(i + 1));
442 else if (strEQ(key, "first_line_num")) {
443 if (!SvIOK(ST(i + 1)))
444 croak("add_symbol: first_line_num must be an integer");
445 first_line_num = SvIV(ST(i + 1));
447 else if (strEQ(key, "last_line_num")) {
448 if (!SvIOK(ST(i + 1)))
449 croak("add_symbol: last_line_num must be an integer");
450 last_line_num = SvIV(ST(i + 1));
454 if (!filename || first_line_num == -1) {
455 I32 cxix_from, cxix_to;
456 PERL_CONTEXT *cx, *ccstack;
459 cx = upcontext(0, &cop, &ccstack, &cxix_from, &cxix_to);
464 filename = CopFILE(cop);
465 if (first_line_num == -1)
466 first_line_num = cop->cop_line;
469 if (last_line_num == -1)
470 last_line_num = first_line_num;
472 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
473 dbsub = get_hv("DB::sub", 1);
474 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
475 namepv = SvPV(name, namelen);
476 if (!hv_store(dbsub, namepv, namelen, dbval, 0)) {
477 warn("Failed to update $DB::sub for subroutine %s", namepv);
482 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
483 * once' warnings in some situations... i can't reproduce this, but CMOP
485 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
490 if (SvROK(initial)) {
492 SvREFCNT_inc_simple_void_NN(val);
495 val = newSVsv(initial);
498 switch (variable.type) {
520 remove_glob(self, name)
524 hv_delete(_get_namespace(self), name, strlen(name), G_DISCARD);
527 has_symbol(self, variable)
534 namespace = _get_namespace(self);
535 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
540 GV *glob = (GV*)(*entry);
541 switch (variable.type) {
543 RETVAL = GvSVOK(glob) ? 1 : 0;
546 RETVAL = GvAVOK(glob) ? 1 : 0;
549 RETVAL = GvHVOK(glob) ? 1 : 0;
552 RETVAL = GvCVOK(glob) ? 1 : 0;
555 RETVAL = GvIOOK(glob) ? 1 : 0;
560 RETVAL = (variable.type == VAR_CODE);
566 get_symbol(self, variable)
572 val = _get_symbol(self, &variable, 0);
575 RETVAL = newRV_inc(val);
580 get_or_add_symbol(self, variable)
586 val = _get_symbol(self, &variable, 1);
589 RETVAL = newRV_inc(val);
594 remove_symbol(self, variable)
601 namespace = _get_namespace(self);
602 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
607 GV *glob = (GV*)(*entry);
608 switch (variable.type) {
627 if (variable.type == VAR_CODE) {
628 hv_delete(namespace, variable.name, strlen(variable.name), G_DISCARD);
633 list_all_symbols(self, vartype=VAR_NONE)
637 if (vartype == VAR_NONE) {
642 namespace = _get_namespace(self);
643 keys = hv_iterinit(namespace);
645 while ((entry = hv_iternext(namespace))) {
646 mPUSHs(newSVhe(entry));
655 namespace = _get_namespace(self);
656 hv_iterinit(namespace);
657 while ((val = hv_iternextsv(namespace, &key, &len))) {
683 else if (vartype == VAR_CODE) {
691 name_key = newSVpvs("name");
692 PERL_HASH(name_hash, "name", 4);
694 namespace_key = newSVpvs("namespace");
695 PERL_HASH(namespace_hash, "namespace", 9);
697 type_key = newSVpvs("type");
698 PERL_HASH(type_hash, "type", 4);