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) newSVpvn(HePV(he))
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 #define GvSetSV(g,v) do { \
43 SvREFCNT_dec(GvSV(g)); \
44 if ((GvSV(g) = (SV*)(v))) \
45 GvIMPORTED_SV_on(g); \
47 #define GvSetAV(g,v) do { \
48 SvREFCNT_dec(GvAV(g)); \
49 if ((GvAV(g) = (AV*)(v))) \
50 GvIMPORTED_AV_on(g); \
52 #define GvSetHV(g,v) do { \
53 SvREFCNT_dec(GvHV(g)); \
54 if ((GvHV(g) = (HV*)(v))) \
55 GvIMPORTED_HV_on(g); \
57 #define GvSetCV(g,v) do { \
58 SvREFCNT_dec(GvCV(g)); \
59 if ((GvCV(g) = (CV*)(v))) { \
60 GvIMPORTED_CV_on(g); \
64 mro_method_changed_in(GvSTASH(g)); \
66 #define GvSetIO(g,v) do { \
67 SvREFCNT_dec(GvIO(g)); \
68 GvIOp(g) = (IO*)(v); \
78 VAR_GLOB, /* TODO: unimplemented */
79 VAR_FORMAT /* TODO: unimplemented */
88 const char *vartype_to_string(vartype_t type)
106 I32 vartype_to_svtype(vartype_t type)
110 return SVt_PV; /* or whatever */
124 vartype_t string_to_vartype(char *vartype)
126 if (strEQ(vartype, "SCALAR")) {
129 else if (strEQ(vartype, "ARRAY")) {
132 else if (strEQ(vartype, "HASH")) {
135 else if (strEQ(vartype, "CODE")) {
138 else if (strEQ(vartype, "IO")) {
142 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO'");
146 void _deconstruct_variable_name(char *variable, varspec_t *varspec)
148 if (!variable || !variable[0])
149 croak("You must pass a variable name");
151 varspec->type = VAR_NONE;
153 switch (variable[0]) {
155 varspec->type = VAR_SCALAR;
158 varspec->type = VAR_ARRAY;
161 varspec->type = VAR_HASH;
164 varspec->type = VAR_CODE;
168 if (varspec->type != VAR_NONE) {
169 varspec->sigil = variable[0];
170 varspec->name = &variable[1];
173 varspec->type = VAR_IO;
174 varspec->sigil = '\0';
175 varspec->name = variable;
179 void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
183 val = hv_fetch(variable, "name", 4, 0);
185 croak("The 'name' key is required in variable specs");
187 varspec->name = savesvpv(*val);
188 SAVEFREEPV(varspec->name);
190 val = hv_fetch(variable, "sigil", 5, 0);
192 croak("The 'sigil' key is required in variable specs");
194 varspec->sigil = (SvPV_nolen(*val))[0];
196 val = hv_fetch(variable, "type", 4, 0);
198 croak("The 'type' key is required in variable specs");
200 varspec->type = string_to_vartype(SvPV_nolen(*val));
203 int _valid_for_type(SV *value, vartype_t type)
205 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
209 return sv_type == SVt_NULL ||
215 return sv_type == SVt_PVAV;
217 return sv_type == SVt_PVHV;
219 return sv_type == SVt_PVCV;
221 return sv_type == SVt_PVIO;
227 HV *_get_namespace(SV *self)
236 call_method("namespace", G_SCALAR);
242 return (HV*)SvRV(ret);
245 SV *_get_name(SV *self)
254 call_method("name", G_SCALAR);
263 SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
269 namespace = _get_namespace(self);
270 entry = hv_fetch(namespace, variable->name, strlen(variable->name), vivify);
274 glob = (GV*)(*entry);
278 namesv = newSVsv(_get_name(self));
279 sv_catpvs(namesv, "::");
280 sv_catpv(namesv, variable->name);
282 /* can't use gv_init here, because it screws up @ISA in a way that I
283 * can't reproduce, but that CMOP triggers */
284 gv_fetchsv(namesv, GV_ADD, vartype_to_svtype(variable->type));
285 SvREFCNT_dec(namesv);
289 switch (variable->type) {
292 GvSetSV(glob, newSV(0));
296 GvSetAV(glob, newAV());
300 GvSetHV(glob, newHV());
303 croak("Don't know how to vivify CODE variables");
306 GvSetIO(glob, newIO());
309 croak("Unknown type in vivication");
313 switch (variable->type) {
317 return (SV*)GvAV(glob);
319 return (SV*)GvHV(glob);
321 return (SV*)GvCV(glob);
323 return (SV*)GvIO(glob);
329 MODULE = Package::Stash PACKAGE = Package::Stash
334 new(class, package_name)
342 if (!SvPOK(package_name))
343 croak("The constructor argument must be the name of a package");
347 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
348 SvREFCNT_dec(package_name);
349 SvREFCNT_dec(instance);
350 croak("Couldn't initialize the 'name' key, hv_store failed");
352 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
353 nsref = newRV_inc((SV*)namespace);
354 if (!hv_store(instance, "namespace", 9, nsref, 0)) {
356 SvREFCNT_dec(instance);
357 croak("Couldn't initialize the 'namespace' key, hv_store failed");
360 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashpv(class, 0));
370 if (!sv_isobject(self))
371 croak("Can't call name as a class method");
372 slot = hv_fetch((HV*)SvRV(self), "name", 4, 0);
373 RETVAL = slot ? SvREFCNT_inc_simple_NN(*slot) : &PL_sv_undef;
383 if (!sv_isobject(self))
384 croak("Can't call namespace as a class method");
385 slot = hv_fetch((HV*)SvRV(self), "namespace", 9, 0);
386 RETVAL = slot ? SvREFCNT_inc_simple_NN(*slot) : &PL_sv_undef;
391 add_symbol(self, variable, initial=NULL, ...)
399 if (initial && !_valid_for_type(initial, variable.type))
400 croak("%s is not of type %s",
401 SvPV_nolen(initial), vartype_to_string(variable.type));
403 name = newSVsv(_get_name(self));
404 sv_catpvs(name, "::");
405 sv_catpv(name, variable.name);
407 /* XXX: come back to this when i feel like reimplementing caller() */
409 my $filename = $opts{filename};
410 my $first_line_num = $opts{first_line_num};
412 (undef, $filename, $first_line_num) = caller
413 if not defined $filename;
415 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
417 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
418 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
421 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
423 char *filename = NULL, *name;
424 I32 first_line_num, last_line_num;
427 croak("add_symbol: Odd number of elements in %%opts");
429 for (i = 3; i < items; i += 2) {
431 key = SvPV_nolen(ST(i));
432 if (strEQ(key, "filename")) {
433 if (!SvPOK(ST(i + 1)))
434 croak("add_symbol: filename must be a string");
435 filename = SvPV_nolen(ST(i + 1));
437 else if (strEQ(key, "first_line_num")) {
438 if (!SvIOK(ST(i + 1)))
439 croak("add_symbol: first_line_num must be an integer");
440 first_line_num = SvIV(ST(i + 1));
442 else if (strEQ(key, "last_line_num")) {
443 if (!SvIOK(ST(i + 1)))
444 croak("add_symbol: last_line_num must be an integer");
445 last_line_num = SvIV(ST(i + 1));
454 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
459 if (SvROK(initial)) {
461 SvREFCNT_inc_simple_void_NN(val);
464 val = newSVsv(initial);
467 switch (variable.type) {
489 remove_glob(self, name)
493 hv_delete(_get_namespace(self), name, strlen(name), G_DISCARD);
496 has_symbol(self, variable)
503 namespace = _get_namespace(self);
504 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
509 GV *glob = (GV*)(*entry);
510 switch (variable.type) {
512 RETVAL = GvSVOK(glob) ? 1 : 0;
515 RETVAL = GvAVOK(glob) ? 1 : 0;
518 RETVAL = GvHVOK(glob) ? 1 : 0;
521 RETVAL = GvCVOK(glob) ? 1 : 0;
524 RETVAL = GvIOOK(glob) ? 1 : 0;
529 RETVAL = (variable.type == VAR_CODE);
535 get_symbol(self, variable)
541 val = _get_symbol(self, &variable, 0);
544 RETVAL = newRV_inc(val);
549 get_or_add_symbol(self, variable)
555 val = _get_symbol(self, &variable, 1);
558 RETVAL = newRV_inc(val);
563 remove_symbol(self, variable)
570 namespace = _get_namespace(self);
571 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
576 GV *glob = (GV*)(*entry);
577 switch (variable.type) {
596 if (variable.type == VAR_CODE) {
597 hv_delete(namespace, variable.name, strlen(variable.name), G_DISCARD);
602 list_all_symbols(self, vartype=VAR_NONE)
606 if (vartype == VAR_NONE) {
611 namespace = _get_namespace(self);
612 keys = hv_iterinit(namespace);
614 while ((entry = hv_iternext(namespace))) {
615 mPUSHs(newSVhe(entry));
624 namespace = _get_namespace(self);
625 hv_iterinit(namespace);
626 while ((val = hv_iternextsv(namespace, &key, &len))) {
652 else if (vartype == VAR_CODE) {