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))
31 /* HACK: scalar slots are always populated on perl < 5.10, so treat undef
32 * as nonexistent. this is consistent with the previous behavior of the pure
33 * perl version of this module (since this is the behavior that perl sees
36 #define GvSVOK(g) (GvSV(g) && SvTYPE(GvSV(g)) != SVt_NULL)
38 #define GvSVOK(g) GvSV(g)
41 #define GvAVOK(g) GvAV(g)
42 #define GvHVOK(g) GvHV(g)
43 #define GvCVOK(g) GvCVu(g) /* XXX: should this really be GvCVu? or GvCV? */
44 #define GvIOOK(g) GvIO(g)
46 /* see above - don't let scalar slots become unpopulated, this breaks
47 * assumptions in core */
49 #define GvSetSV(g,v) do { \
51 SvREFCNT_dec(GvSV(g)); \
52 if ((GvSV(g) = _v ? _v : newSV(0))) \
53 GvIMPORTED_SV_on(g); \
56 #define GvSetSV(g,v) do { \
57 SvREFCNT_dec(GvSV(g)); \
58 if ((GvSV(g) = (SV*)(v))) \
59 GvIMPORTED_SV_on(g); \
63 #define GvSetAV(g,v) do { \
64 SvREFCNT_dec(GvAV(g)); \
65 if ((GvAV(g) = (AV*)(v))) \
66 GvIMPORTED_AV_on(g); \
68 #define GvSetHV(g,v) do { \
69 SvREFCNT_dec(GvHV(g)); \
70 if ((GvHV(g) = (HV*)(v))) \
71 GvIMPORTED_HV_on(g); \
73 #define GvSetCV(g,v) do { \
74 SvREFCNT_dec(GvCV(g)); \
75 if ((GvCV_set(g, v))) { \
76 GvIMPORTED_CV_on(g); \
80 mro_method_changed_in(GvSTASH(g)); \
82 #define GvSetIO(g,v) do { \
83 SvREFCNT_dec(GvIO(g)); \
84 GvIOp(g) = (IO*)(v); \
94 VAR_GLOB, /* TODO: unimplemented */
95 VAR_FORMAT /* TODO: unimplemented */
103 static U32 name_hash, namespace_hash, type_hash;
104 static SV *name_key, *namespace_key, *type_key;
106 static const char *vartype_to_string(vartype_t type)
124 static I32 vartype_to_svtype(vartype_t type)
128 return SVt_PV; /* or whatever */
142 static vartype_t string_to_vartype(char *vartype)
144 if (strEQ(vartype, "SCALAR")) {
147 else if (strEQ(vartype, "ARRAY")) {
150 else if (strEQ(vartype, "HASH")) {
153 else if (strEQ(vartype, "CODE")) {
156 else if (strEQ(vartype, "IO")) {
160 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO', not '%s'", vartype);
164 static void _deconstruct_variable_name(SV *variable, varspec_t *varspec)
168 if (!SvCUR(variable))
169 croak("You must pass a variable name");
171 varspec->name = sv_2mortal(newSVsv(variable));
173 varpv = SvPV_nolen(varspec->name);
176 varspec->type = VAR_SCALAR;
177 sv_chop(varspec->name, &varpv[1]);
180 varspec->type = VAR_ARRAY;
181 sv_chop(varspec->name, &varpv[1]);
184 varspec->type = VAR_HASH;
185 sv_chop(varspec->name, &varpv[1]);
188 varspec->type = VAR_CODE;
189 sv_chop(varspec->name, &varpv[1]);
192 varspec->type = VAR_IO;
197 static void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
201 val = hv_fetch_ent(variable, name_key, 0, name_hash);
203 croak("The 'name' key is required in variable specs");
205 varspec->name = sv_2mortal(newSVsv(HeVAL(val)));
207 val = hv_fetch_ent(variable, type_key, 0, type_hash);
209 croak("The 'type' key is required in variable specs");
211 varspec->type = string_to_vartype(SvPV_nolen(HeVAL(val)));
214 static int _valid_for_type(SV *value, vartype_t type)
216 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
220 return sv_type != SVt_PVAV && sv_type != SVt_PVHV &&
221 sv_type != SVt_PVCV && sv_type != SVt_PVFM &&
224 return sv_type == SVt_PVAV;
226 return sv_type == SVt_PVHV;
228 return sv_type == SVt_PVCV;
230 return sv_type == SVt_PVIO;
236 static HV *_get_namespace(SV *self)
245 call_method("namespace", G_SCALAR);
251 return (HV*)SvRV(ret);
254 static SV *_get_name(SV *self)
263 call_method("name", G_SCALAR);
272 static void _expand_glob(SV *self, SV *varname)
276 name = newSVsv(_get_name(self));
277 sv_catpvs(name, "::");
278 sv_catsv(name, varname);
280 /* can't use gv_init here, because it screws up @ISA in a way that I
281 * can't reproduce, but that CMOP triggers */
282 gv_fetchsv(name, GV_ADD, SVt_NULL);
286 static SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
292 namespace = _get_namespace(self);
293 entry = hv_fetch_ent(namespace, variable->name, vivify, 0);
297 glob = (GV*)(HeVAL(entry));
299 _expand_glob(self, variable->name);
302 switch (variable->type) {
305 GvSetSV(glob, newSV(0));
309 GvSetAV(glob, newAV());
313 GvSetHV(glob, newHV());
316 croak("Don't know how to vivify CODE variables");
319 GvSetIO(glob, newIO());
322 croak("Unknown type in vivication");
326 switch (variable->type) {
330 return (SV*)GvAV(glob);
332 return (SV*)GvHV(glob);
334 return (SV*)GvCV(glob);
336 return (SV*)GvIO(glob);
342 MODULE = Package::Stash::XS PACKAGE = Package::Stash::XS
347 new(class, package_name)
353 if (!SvPOK(package_name))
354 croak("The constructor argument must be the name of a package");
358 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
359 SvREFCNT_dec(package_name);
360 SvREFCNT_dec(instance);
361 croak("Couldn't initialize the 'name' key, hv_store failed");
364 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashsv(class, 0));
374 if (!sv_isobject(self))
375 croak("Can't call name as a class method");
376 slot = hv_fetch_ent((HV*)SvRV(self), name_key, 0, name_hash);
377 RETVAL = slot ? SvREFCNT_inc_simple_NN(HeVAL(slot)) : &PL_sv_undef;
388 if (!sv_isobject(self))
389 croak("Can't call namespace as a class method");
390 #if PERL_VERSION < 10
391 package_name = _get_name(self);
392 RETVAL = newRV_inc((SV*)gv_stashpv(SvPV_nolen(package_name), GV_ADD));
394 slot = hv_fetch_ent((HV*)SvRV(self), namespace_key, 0, namespace_hash);
396 RETVAL = SvREFCNT_inc_simple_NN(HeVAL(slot));
402 package_name = _get_name(self);
403 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
404 nsref = newRV_inc((SV*)namespace);
406 if (!hv_store((HV*)SvRV(self), "namespace", 9, nsref, 0)) {
409 croak("Couldn't initialize the 'namespace' key, hv_store failed");
411 RETVAL = SvREFCNT_inc_simple_NN(nsref);
418 add_symbol(self, variable, initial=NULL, ...)
426 if (initial && !_valid_for_type(initial, variable.type))
427 croak("%s is not of type %s",
428 SvPV_nolen(initial), vartype_to_string(variable.type));
430 name = newSVsv(_get_name(self));
431 sv_catpvs(name, "::");
432 sv_catsv(name, variable.name);
434 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
436 char *filename = NULL;
437 I32 first_line_num = -1, last_line_num = -1;
443 croak("add_symbol: Odd number of elements in %%opts");
445 for (i = 3; i < items; i += 2) {
447 key = SvPV_nolen(ST(i));
448 if (strEQ(key, "filename")) {
449 if (!SvPOK(ST(i + 1)))
450 croak("add_symbol: filename must be a string");
451 filename = SvPV_nolen(ST(i + 1));
453 else if (strEQ(key, "first_line_num")) {
454 if (!SvIOK(ST(i + 1)))
455 croak("add_symbol: first_line_num must be an integer");
456 first_line_num = SvIV(ST(i + 1));
458 else if (strEQ(key, "last_line_num")) {
459 if (!SvIOK(ST(i + 1)))
460 croak("add_symbol: last_line_num must be an integer");
461 last_line_num = SvIV(ST(i + 1));
465 if (!filename || first_line_num == -1) {
467 filename = CopFILE(PL_curcop);
468 if (first_line_num == -1)
469 first_line_num = PL_curcop->cop_line;
472 if (last_line_num == -1)
473 last_line_num = first_line_num;
475 /* http://perldoc.perl.org/perldebguts.html#Debugger-Internals */
476 dbsub = get_hv("DB::sub", 1);
477 dbval = newSVpvf("%s:%d-%d", filename, first_line_num, last_line_num);
478 if (!hv_store_ent(dbsub, name, dbval, 0)) {
479 warn("Failed to update $DB::sub for subroutine %s",
485 /* GV_ADDMULTI rather than GV_ADD because otherwise you get 'used only
486 * once' warnings in some situations... i can't reproduce this, but CMOP
488 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
493 if (SvROK(initial)) {
495 SvREFCNT_inc_simple_void_NN(val);
498 val = newSVsv(initial);
501 switch (variable.type) {
523 remove_glob(self, name)
527 hv_delete_ent(_get_namespace(self), name, G_DISCARD, 0);
530 has_symbol(self, variable)
538 namespace = _get_namespace(self);
539 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
546 switch (variable.type) {
548 RETVAL = GvSVOK(glob) ? 1 : 0;
551 RETVAL = GvAVOK(glob) ? 1 : 0;
554 RETVAL = GvHVOK(glob) ? 1 : 0;
557 RETVAL = GvCVOK(glob) ? 1 : 0;
560 RETVAL = GvIOOK(glob) ? 1 : 0;
565 RETVAL = (variable.type == VAR_CODE);
571 get_symbol(self, variable)
577 val = _get_symbol(self, &variable, 0);
580 RETVAL = newRV_inc(val);
585 get_or_add_symbol(self, variable)
591 val = _get_symbol(self, &variable, 1);
594 RETVAL = newRV_inc(val);
599 remove_symbol(self, variable)
607 namespace = _get_namespace(self);
608 entry = hv_fetch_ent(namespace, variable.name, 0, 0);
615 switch (variable.type) {
634 if (variable.type == VAR_CODE) {
635 hv_delete_ent(namespace, variable.name, G_DISCARD, 0);
640 list_all_symbols(self, vartype=VAR_NONE)
644 if (vartype == VAR_NONE) {
649 namespace = _get_namespace(self);
650 keys = hv_iterinit(namespace);
652 while ((entry = hv_iternext(namespace))) {
653 mPUSHs(newSVhe(entry));
662 namespace = _get_namespace(self);
663 hv_iterinit(namespace);
664 while ((val = hv_iternextsv(namespace, &key, &len))) {
690 else if (vartype == VAR_CODE) {
697 get_all_symbols(self, vartype=VAR_NONE)
706 namespace = _get_namespace(self);
709 hv_iterinit(namespace);
710 while ((val = hv_iternextsv(namespace, &key, &len))) {
714 SV *keysv = newSVpvn(key, len);
715 _expand_glob(self, keysv);
722 hv_store(ret, key, len, newRV_inc(GvSV(gv)), 0);
726 hv_store(ret, key, len, newRV_inc((SV*)GvAV(gv)), 0);
730 hv_store(ret, key, len, newRV_inc((SV*)GvHV(gv)), 0);
734 hv_store(ret, key, len, newRV_inc((SV*)GvCV(gv)), 0);
738 hv_store(ret, key, len, newRV_inc((SV*)GvIO(gv)), 0);
741 hv_store(ret, key, len, SvREFCNT_inc_simple_NN(val), 0);
746 mPUSHs(newRV_noinc((SV*)ret));
750 name_key = newSVpvs("name");
751 PERL_HASH(name_hash, "name", 4);
753 namespace_key = newSVpvs("namespace");
754 PERL_HASH(namespace_hash, "namespace", 9);
756 type_key = newSVpvs("type");
757 PERL_HASH(type_hash, "type", 4);