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)
43 #define GvSetSV(g,v) do { \
45 SvREFCNT_dec(GvSV(g)); \
46 if ((GvSV(g) = _v ? _v : newSV(0))) \
47 GvIMPORTED_SV_on(g); \
50 #define GvSetSV(g,v) do { \
51 SvREFCNT_dec(GvSV(g)); \
52 if ((GvSV(g) = (SV*)(v))) \
53 GvIMPORTED_SV_on(g); \
57 #define GvSetAV(g,v) do { \
58 SvREFCNT_dec(GvAV(g)); \
59 if ((GvAV(g) = (AV*)(v))) \
60 GvIMPORTED_AV_on(g); \
62 #define GvSetHV(g,v) do { \
63 SvREFCNT_dec(GvHV(g)); \
64 if ((GvHV(g) = (HV*)(v))) \
65 GvIMPORTED_HV_on(g); \
67 #define GvSetCV(g,v) do { \
68 SvREFCNT_dec(GvCV(g)); \
69 if ((GvCV(g) = (CV*)(v))) { \
70 GvIMPORTED_CV_on(g); \
74 mro_method_changed_in(GvSTASH(g)); \
76 #define GvSetIO(g,v) do { \
77 SvREFCNT_dec(GvIO(g)); \
78 GvIOp(g) = (IO*)(v); \
88 VAR_GLOB, /* TODO: unimplemented */
89 VAR_FORMAT /* TODO: unimplemented */
98 const char *vartype_to_string(vartype_t type)
116 I32 vartype_to_svtype(vartype_t type)
120 return SVt_PV; /* or whatever */
134 vartype_t string_to_vartype(char *vartype)
136 if (strEQ(vartype, "SCALAR")) {
139 else if (strEQ(vartype, "ARRAY")) {
142 else if (strEQ(vartype, "HASH")) {
145 else if (strEQ(vartype, "CODE")) {
148 else if (strEQ(vartype, "IO")) {
152 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO'");
156 void _deconstruct_variable_name(char *variable, varspec_t *varspec)
158 if (!variable || !variable[0])
159 croak("You must pass a variable name");
161 varspec->type = VAR_NONE;
163 switch (variable[0]) {
165 varspec->type = VAR_SCALAR;
168 varspec->type = VAR_ARRAY;
171 varspec->type = VAR_HASH;
174 varspec->type = VAR_CODE;
178 if (varspec->type != VAR_NONE) {
179 varspec->sigil = variable[0];
180 varspec->name = &variable[1];
183 varspec->type = VAR_IO;
184 varspec->sigil = '\0';
185 varspec->name = variable;
189 void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
193 val = hv_fetch(variable, "name", 4, 0);
195 croak("The 'name' key is required in variable specs");
197 varspec->name = savesvpv(*val);
198 SAVEFREEPV(varspec->name);
200 val = hv_fetch(variable, "sigil", 5, 0);
202 croak("The 'sigil' key is required in variable specs");
204 varspec->sigil = (SvPV_nolen(*val))[0];
206 val = hv_fetch(variable, "type", 4, 0);
208 croak("The 'type' key is required in variable specs");
210 varspec->type = string_to_vartype(SvPV_nolen(*val));
213 int _valid_for_type(SV *value, vartype_t type)
215 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
219 return sv_type == SVt_NULL ||
225 return sv_type == SVt_PVAV;
227 return sv_type == SVt_PVHV;
229 return sv_type == SVt_PVCV;
231 return sv_type == SVt_PVIO;
237 HV *_get_namespace(SV *self)
246 call_method("namespace", G_SCALAR);
252 return (HV*)SvRV(ret);
255 SV *_get_name(SV *self)
264 call_method("name", G_SCALAR);
273 SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
279 namespace = _get_namespace(self);
280 entry = hv_fetch(namespace, variable->name, strlen(variable->name), vivify);
284 glob = (GV*)(*entry);
288 namesv = newSVsv(_get_name(self));
289 sv_catpvs(namesv, "::");
290 sv_catpv(namesv, variable->name);
292 /* can't use gv_init here, because it screws up @ISA in a way that I
293 * can't reproduce, but that CMOP triggers */
294 gv_fetchsv(namesv, GV_ADD, vartype_to_svtype(variable->type));
295 SvREFCNT_dec(namesv);
299 switch (variable->type) {
302 GvSetSV(glob, newSV(0));
306 GvSetAV(glob, newAV());
310 GvSetHV(glob, newHV());
313 croak("Don't know how to vivify CODE variables");
316 GvSetIO(glob, newIO());
319 croak("Unknown type in vivication");
323 switch (variable->type) {
327 return (SV*)GvAV(glob);
329 return (SV*)GvHV(glob);
331 return (SV*)GvCV(glob);
333 return (SV*)GvIO(glob);
339 MODULE = Package::Stash PACKAGE = Package::Stash
344 new(class, package_name)
352 if (!SvPOK(package_name))
353 croak("The constructor argument must be the name of a package");
357 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
358 SvREFCNT_dec(package_name);
359 SvREFCNT_dec(instance);
360 croak("Couldn't initialize the 'name' key, hv_store failed");
362 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
363 nsref = newRV_inc((SV*)namespace);
364 if (!hv_store(instance, "namespace", 9, nsref, 0)) {
366 SvREFCNT_dec(instance);
367 croak("Couldn't initialize the 'namespace' key, hv_store failed");
370 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashpv(class, 0));
380 if (!sv_isobject(self))
381 croak("Can't call name as a class method");
382 slot = hv_fetch((HV*)SvRV(self), "name", 4, 0);
383 RETVAL = slot ? SvREFCNT_inc_simple_NN(*slot) : &PL_sv_undef;
393 if (!sv_isobject(self))
394 croak("Can't call namespace as a class method");
395 slot = hv_fetch((HV*)SvRV(self), "namespace", 9, 0);
396 RETVAL = slot ? SvREFCNT_inc_simple_NN(*slot) : &PL_sv_undef;
401 add_symbol(self, variable, initial=NULL, ...)
409 if (initial && !_valid_for_type(initial, variable.type))
410 croak("%s is not of type %s",
411 SvPV_nolen(initial), vartype_to_string(variable.type));
413 name = newSVsv(_get_name(self));
414 sv_catpvs(name, "::");
415 sv_catpv(name, variable.name);
417 /* XXX: come back to this when i feel like reimplementing caller() */
419 my $filename = $opts{filename};
420 my $first_line_num = $opts{first_line_num};
422 (undef, $filename, $first_line_num) = caller
423 if not defined $filename;
425 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
427 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
428 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
431 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
433 char *filename = NULL, *name;
434 I32 first_line_num, last_line_num;
437 croak("add_symbol: Odd number of elements in %%opts");
439 for (i = 3; i < items; i += 2) {
441 key = SvPV_nolen(ST(i));
442 if (strEQ(key, "filename")) {
443 if (!SvPOK(ST(i + 1)))
444 croak("add_symbol: filename must be a string");
445 filename = SvPV_nolen(ST(i + 1));
447 else if (strEQ(key, "first_line_num")) {
448 if (!SvIOK(ST(i + 1)))
449 croak("add_symbol: first_line_num must be an integer");
450 first_line_num = SvIV(ST(i + 1));
452 else if (strEQ(key, "last_line_num")) {
453 if (!SvIOK(ST(i + 1)))
454 croak("add_symbol: last_line_num must be an integer");
455 last_line_num = SvIV(ST(i + 1));
464 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
469 if (SvROK(initial)) {
471 SvREFCNT_inc_simple_void_NN(val);
474 val = newSVsv(initial);
477 switch (variable.type) {
499 remove_glob(self, name)
503 hv_delete(_get_namespace(self), name, strlen(name), G_DISCARD);
506 has_symbol(self, variable)
513 namespace = _get_namespace(self);
514 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
519 GV *glob = (GV*)(*entry);
520 switch (variable.type) {
522 RETVAL = GvSVOK(glob) ? 1 : 0;
525 RETVAL = GvAVOK(glob) ? 1 : 0;
528 RETVAL = GvHVOK(glob) ? 1 : 0;
531 RETVAL = GvCVOK(glob) ? 1 : 0;
534 RETVAL = GvIOOK(glob) ? 1 : 0;
539 RETVAL = (variable.type == VAR_CODE);
545 get_symbol(self, variable)
551 val = _get_symbol(self, &variable, 0);
554 RETVAL = newRV_inc(val);
559 get_or_add_symbol(self, variable)
565 val = _get_symbol(self, &variable, 1);
568 RETVAL = newRV_inc(val);
573 remove_symbol(self, variable)
580 namespace = _get_namespace(self);
581 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
586 GV *glob = (GV*)(*entry);
587 switch (variable.type) {
606 if (variable.type == VAR_CODE) {
607 hv_delete(namespace, variable.name, strlen(variable.name), G_DISCARD);
612 list_all_symbols(self, vartype=VAR_NONE)
616 if (vartype == VAR_NONE) {
621 namespace = _get_namespace(self);
622 keys = hv_iterinit(namespace);
624 while ((entry = hv_iternext(namespace))) {
625 mPUSHs(newSVhe(entry));
634 namespace = _get_namespace(self);
635 hv_iterinit(namespace);
636 while ((val = hv_iternextsv(namespace, &key, &len))) {
662 else if (vartype == VAR_CODE) {