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 SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
294 namespace = _get_namespace(self);
295 entry = hv_fetch(namespace, variable->name, variable->namelen, vivify);
299 glob = (GV*)(*entry);
303 namesv = newSVsv(_get_name(self));
304 sv_catpvs(namesv, "::");
305 sv_catpv(namesv, variable->name);
307 /* can't use gv_init here, because it screws up @ISA in a way that I
308 * can't reproduce, but that CMOP triggers */
309 gv_fetchsv(namesv, GV_ADD, vartype_to_svtype(variable->type));
310 SvREFCNT_dec(namesv);
314 switch (variable->type) {
317 GvSetSV(glob, newSV(0));
321 GvSetAV(glob, newAV());
325 GvSetHV(glob, newHV());
328 croak("Don't know how to vivify CODE variables");
331 GvSetIO(glob, newIO());
334 croak("Unknown type in vivication");
338 switch (variable->type) {
342 return (SV*)GvAV(glob);
344 return (SV*)GvHV(glob);
346 return (SV*)GvCV(glob);
348 return (SV*)GvIO(glob);
354 MODULE = Package::Stash PACKAGE = Package::Stash
359 new(class, package_name)
367 if (!SvPOK(package_name))
368 croak("The constructor argument must be the name of a package");
372 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
373 SvREFCNT_dec(package_name);
374 SvREFCNT_dec(instance);
375 croak("Couldn't initialize the 'name' key, hv_store failed");
377 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
378 nsref = newRV_inc((SV*)namespace);
379 if (!hv_store(instance, "namespace", 9, nsref, 0)) {
381 SvREFCNT_dec(instance);
382 croak("Couldn't initialize the 'namespace' key, hv_store failed");
385 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashpv(class, 0));
395 if (!sv_isobject(self))
396 croak("Can't call name as a class method");
397 slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash);
398 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
408 if (!sv_isobject(self))
409 croak("Can't call namespace as a class method");
410 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
411 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
416 add_symbol(self, variable, initial=NULL, ...)
424 if (initial && !_valid_for_type(initial, variable.type))
425 croak("%s is not of type %s",
426 SvPV_nolen(initial), vartype_to_string(variable.type));
428 name = newSVsv(_get_name(self));
429 sv_catpvs(name, "::");
430 sv_catpv(name, variable.name);
432 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
434 char *filename = NULL, *namepv;
435 I32 first_line_num = -1, last_line_num = -1;
441 croak("add_symbol: Odd number of elements in %%opts");
443 for (i = 3; i < items; i += 2) {
445 key = SvPV_nolen(ST(i));
446 if (strEQ(key, "filename")) {
447 if (!SvPOK(ST(i + 1)))
448 croak("add_symbol: filename must be a string");
449 filename = SvPV_nolen(ST(i + 1));
451 else if (strEQ(key, "first_line_num")) {
452 if (!SvIOK(ST(i + 1)))
453 croak("add_symbol: first_line_num must be an integer");
454 first_line_num = SvIV(ST(i + 1));
456 else if (strEQ(key, "last_line_num")) {
457 if (!SvIOK(ST(i + 1)))
458 croak("add_symbol: last_line_num must be an integer");
459 last_line_num = SvIV(ST(i + 1));
463 if (!filename || first_line_num == -1) {
464 I32 cxix_from, cxix_to;
465 PERL_CONTEXT *cx, *ccstack;
468 cx = upcontext(0, &cop, &ccstack, &cxix_from, &cxix_to);
473 filename = CopFILE(cop);
474 if (first_line_num == -1)
475 first_line_num = cop->cop_line;
478 if (last_line_num == -1)
479 last_line_num = first_line_num;
481 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
482 dbsub = get_hv("DB::sub", 1);
483 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
484 namepv = SvPV(name, namelen);
485 if (!hv_store(dbsub, namepv, namelen, dbval, 0)) {
486 warn("Failed to update $DB::sub for subroutine %s", namepv);
491 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
492 * once' warnings in some situations... i can't reproduce this, but CMOP
494 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
499 if (SvROK(initial)) {
501 SvREFCNT_inc_simple_void_NN(val);
504 val = newSVsv(initial);
507 switch (variable.type) {
529 remove_glob(self, namesv)
536 name = SvPV(namesv, len);
537 hv_delete(_get_namespace(self), name, len, G_DISCARD);
540 has_symbol(self, variable)
547 namespace = _get_namespace(self);
548 entry = hv_fetch(namespace, variable.name, variable.namelen, 0);
553 GV *glob = (GV*)(*entry);
554 switch (variable.type) {
556 RETVAL = GvSVOK(glob) ? 1 : 0;
559 RETVAL = GvAVOK(glob) ? 1 : 0;
562 RETVAL = GvHVOK(glob) ? 1 : 0;
565 RETVAL = GvCVOK(glob) ? 1 : 0;
568 RETVAL = GvIOOK(glob) ? 1 : 0;
573 RETVAL = (variable.type == VAR_CODE);
579 get_symbol(self, variable)
585 val = _get_symbol(self, &variable, 0);
588 RETVAL = newRV_inc(val);
593 get_or_add_symbol(self, variable)
599 val = _get_symbol(self, &variable, 1);
602 RETVAL = newRV_inc(val);
607 remove_symbol(self, variable)
614 namespace = _get_namespace(self);
615 entry = hv_fetch(namespace, variable.name, variable.namelen, 0);
620 GV *glob = (GV*)(*entry);
621 switch (variable.type) {
640 if (variable.type == VAR_CODE) {
641 hv_delete(namespace, variable.name, variable.namelen, G_DISCARD);
646 list_all_symbols(self, vartype=VAR_NONE)
650 if (vartype == VAR_NONE) {
655 namespace = _get_namespace(self);
656 keys = hv_iterinit(namespace);
658 while ((entry = hv_iternext(namespace))) {
659 mPUSHs(newSVhe(entry));
668 namespace = _get_namespace(self);
669 hv_iterinit(namespace);
670 while ((val = hv_iternextsv(namespace, &key, &len))) {
696 else if (vartype == VAR_CODE) {
704 name_key = newSVpvs("name");
705 PERL_HASH(name_hash, "name", 4);
707 namespace_key = newSVpvs("namespace");
708 PERL_HASH(namespace_hash, "namespace", 9);
710 type_key = newSVpvs("type");
711 PERL_HASH(type_hash, "type", 4);