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))
34 VAR_GLOB, /* TODO: unimplemented */
35 VAR_FORMAT /* TODO: unimplemented */
44 const char *vartype_to_string(vartype_t type)
62 I32 vartype_to_svtype(vartype_t type)
66 return SVt_PV; /* or whatever */
80 vartype_t string_to_vartype(char *vartype)
82 if (strEQ(vartype, "SCALAR")) {
85 else if (strEQ(vartype, "ARRAY")) {
88 else if (strEQ(vartype, "HASH")) {
91 else if (strEQ(vartype, "CODE")) {
94 else if (strEQ(vartype, "IO")) {
98 croak("Type must be one of 'SCALAR', 'ARRAY', 'HASH', 'CODE', or 'IO'");
102 void _deconstruct_variable_name(char *variable, varspec_t *varspec)
104 if (!variable || !variable[0])
105 croak("You must pass a variable name");
107 varspec->type = VAR_NONE;
109 switch (variable[0]) {
111 varspec->type = VAR_SCALAR;
114 varspec->type = VAR_ARRAY;
117 varspec->type = VAR_HASH;
120 varspec->type = VAR_CODE;
124 if (varspec->type != VAR_NONE) {
125 varspec->sigil = variable[0];
126 varspec->name = &variable[1];
129 varspec->type = VAR_IO;
130 varspec->sigil = '\0';
131 varspec->name = variable;
135 void _deconstruct_variable_hash(HV *variable, varspec_t *varspec)
139 val = hv_fetch(variable, "name", 4, 0);
141 croak("The 'name' key is required in variable specs");
143 varspec->name = savesvpv(*val);
144 SAVEFREEPV(varspec->name);
146 val = hv_fetch(variable, "sigil", 5, 0);
148 croak("The 'sigil' key is required in variable specs");
150 varspec->sigil = (SvPV_nolen(*val))[0];
152 val = hv_fetch(variable, "type", 4, 0);
154 croak("The 'type' key is required in variable specs");
156 varspec->type = string_to_vartype(SvPV_nolen(*val));
159 int _valid_for_type(SV *value, vartype_t type)
161 svtype sv_type = SvROK(value) ? SvTYPE(SvRV(value)) : SVt_NULL;
165 return sv_type == SVt_NULL ||
171 return sv_type == SVt_PVAV;
173 return sv_type == SVt_PVHV;
175 return sv_type == SVt_PVCV;
177 return sv_type == SVt_PVIO;
183 HV *_get_namespace(SV *self)
192 call_method("namespace", G_SCALAR);
198 return (HV*)SvRV(ret);
201 SV *_get_name(SV *self)
210 call_method("name", G_SCALAR);
219 SV *_get_symbol(SV *self, varspec_t *variable, int vivify)
225 namespace = _get_namespace(self);
226 entry = hv_fetch(namespace, variable->name, strlen(variable->name), vivify);
230 glob = (GV*)(*entry);
234 namesv = newSVsv(_get_name(self));
235 sv_catpvs(namesv, "::");
236 sv_catpv(namesv, variable->name);
238 /* can't use gv_init here, because it screws up @ISA in a way that I
239 * can't reproduce, but that CMOP triggers */
240 gv_fetchsv(namesv, GV_ADD, vartype_to_svtype(variable->type));
241 SvREFCNT_dec(namesv);
245 switch (variable->type) {
248 GvSV(glob) = newSV(0);
252 GvAV(glob) = newAV();
256 GvHV(glob) = newHV();
259 croak("Don't know how to vivify CODE variables");
262 GvIOp(glob) = newIO();
265 croak("Unknown type in vivication");
269 switch (variable->type) {
273 return (SV*)GvAV(glob);
275 return (SV*)GvHV(glob);
277 return (SV*)GvCV(glob);
279 return (SV*)GvIO(glob);
285 MODULE = Package::Stash PACKAGE = Package::Stash
290 new(class, package_name)
298 if (!SvPOK(package_name))
299 croak("The constructor argument must be the name of a package");
303 if (!hv_store(instance, "name", 4, SvREFCNT_inc_simple_NN(package_name), 0)) {
304 SvREFCNT_dec(package_name);
305 SvREFCNT_dec(instance);
306 croak("Couldn't initialize the 'name' key, hv_store failed");
308 namespace = gv_stashpv(SvPV_nolen(package_name), GV_ADD);
309 nsref = newRV_inc((SV*)namespace);
310 if (!hv_store(instance, "namespace", 9, nsref, 0)) {
312 SvREFCNT_dec(instance);
313 croak("Couldn't initialize the 'namespace' key, hv_store failed");
316 RETVAL = sv_bless(newRV_noinc((SV*)instance), gv_stashpv(class, 0));
326 if (!sv_isobject(self))
327 croak("Can't call name as a class method");
328 slot = hv_fetch((HV*)SvRV(self), "name", 4, 0);
329 RETVAL = slot ? SvREFCNT_inc_simple_NN(*slot) : &PL_sv_undef;
339 if (!sv_isobject(self))
340 croak("Can't call namespace as a class method");
341 slot = hv_fetch((HV*)SvRV(self), "namespace", 9, 0);
342 RETVAL = slot ? SvREFCNT_inc_simple_NN(*slot) : &PL_sv_undef;
347 add_symbol(self, variable, initial=NULL, ...)
355 if (initial && !_valid_for_type(initial, variable.type))
356 croak("%s is not of type %s",
357 SvPV_nolen(initial), vartype_to_string(variable.type));
359 name = newSVsv(_get_name(self));
360 sv_catpvs(name, "::");
361 sv_catpv(name, variable.name);
363 /* XXX: come back to this when i feel like reimplementing caller() */
365 my $filename = $opts{filename};
366 my $first_line_num = $opts{first_line_num};
368 (undef, $filename, $first_line_num) = caller
369 if not defined $filename;
371 my $last_line_num = $opts{last_line_num} || ($first_line_num ||= 0);
373 # http://perldoc.perl.org/perldebguts.html#Debugger-Internals
374 $DB::sub{$pkg . '::' . $name} = "$filename:$first_line_num-$last_line_num";
377 if (items > 2 && (PL_perldb & 0x10) && variable.type == VAR_CODE) {
379 char *filename = NULL, *name;
380 I32 first_line_num, last_line_num;
383 croak("add_symbol: Odd number of elements in %%opts");
385 for (i = 3; i < items; i += 2) {
387 key = SvPV_nolen(ST(i));
388 if (strEQ(key, "filename")) {
389 if (!SvPOK(ST(i + 1)))
390 croak("add_symbol: filename must be a string");
391 filename = SvPV_nolen(ST(i + 1));
393 else if (strEQ(key, "first_line_num")) {
394 if (!SvIOK(ST(i + 1)))
395 croak("add_symbol: first_line_num must be an integer");
396 first_line_num = SvIV(ST(i + 1));
398 else if (strEQ(key, "last_line_num")) {
399 if (!SvIOK(ST(i + 1)))
400 croak("add_symbol: last_line_num must be an integer");
401 last_line_num = SvIV(ST(i + 1));
410 glob = gv_fetchsv(name, GV_ADDMULTI, vartype_to_svtype(variable.type));
415 if (SvROK(initial)) {
417 SvREFCNT_inc_simple_void_NN(val);
420 val = newSVsv(initial);
423 switch (variable.type) {
425 SvREFCNT_dec(GvSV(glob));
427 GvIMPORTED_SV_on(glob);
430 SvREFCNT_dec(GvAV(glob));
431 GvAV(glob) = (AV*)val;
432 GvIMPORTED_AV_on(glob);
435 SvREFCNT_dec(GvHV(glob));
436 GvHV(glob) = (HV*)val;
437 GvIMPORTED_HV_on(glob);
440 SvREFCNT_dec(GvCV(glob));
441 GvCV(glob) = (CV*)val;
442 GvIMPORTED_CV_on(glob);
445 mro_method_changed_in(GvSTASH(glob));
448 SvREFCNT_dec(GvIO(glob));
449 GvIOp(glob) = (IO*)val;
457 remove_glob(self, name)
461 hv_delete(_get_namespace(self), name, strlen(name), G_DISCARD);
464 has_symbol(self, variable)
471 namespace = _get_namespace(self);
472 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
477 GV *glob = (GV*)(*entry);
478 switch (variable.type) {
480 RETVAL = GvSV(glob) ? 1 : 0;
483 RETVAL = GvAV(glob) ? 1 : 0;
486 RETVAL = GvHV(glob) ? 1 : 0;
489 RETVAL = GvCV(glob) ? 1 : 0;
492 RETVAL = GvIO(glob) ? 1 : 0;
497 RETVAL = (variable.type == VAR_CODE);
503 get_symbol(self, variable)
509 val = _get_symbol(self, &variable, 0);
512 RETVAL = newRV_inc(val);
517 get_or_add_symbol(self, variable)
523 val = _get_symbol(self, &variable, 1);
526 RETVAL = newRV_inc(val);
531 remove_symbol(self, variable)
538 namespace = _get_namespace(self);
539 entry = hv_fetch(namespace, variable.name, strlen(variable.name), 0);
544 GV *glob = (GV*)(*entry);
545 switch (variable.type) {
547 GvSV(glob) = (SV *)NULL;
550 GvAV(glob) = (AV *)NULL;
553 GvHV(glob) = (HV *)NULL;
556 GvCV(glob) = (CV *)NULL;
558 mro_method_changed_in(GvSTASH(glob));
561 GvIOp(glob) = (IO *)NULL;
566 if (variable.type == VAR_CODE) {
567 hv_delete(namespace, variable.name, strlen(variable.name), G_DISCARD);
572 list_all_symbols(self, vartype=VAR_NONE)
576 if (vartype == VAR_NONE) {
581 namespace = _get_namespace(self);
582 keys = hv_iterinit(namespace);
584 while ((entry = hv_iternext(namespace))) {
585 mPUSHs(newSVhe(entry));
594 namespace = _get_namespace(self);
595 hv_iterinit(namespace);
596 while ((val = hv_iternextsv(namespace, &key, &len))) {
622 else if (vartype == VAR_CODE) {