Tiny optimization for DESTROY
[gitmo/Mouse.git] / xs-src / Mouse.xs
CommitLineData
1b812057 1#define NEED_newSVpvn_flags_GLOBAL
df6dd016 2#include "mouse.h"
3
cccb83de 4SV* mouse_package;
5SV* mouse_namespace;
3e44140b 6SV* mouse_methods;
a5df48e5 7SV* mouse_name;
047d7af0 8SV* mouse_get_attribute;
9SV* mouse_get_attribute_list;
cccb83de 10
4e7e3250 11#define MOUSE_xc_flags(a) SvUVX(MOUSE_av_at((a), MOUSE_XC_FLAGS))
a39e9541 12#define MOUSE_xc_gen(a) MOUSE_av_at((a), MOUSE_XC_GEN)
aa2d2e2c 13#define MOUSE_xc_stash(a) ( (HV*)MOUSE_av_at((a), MOUSE_XC_STASH) )
a39e9541 14#define MOUSE_xc_attrall(a) ( (AV*)MOUSE_av_at((a), MOUSE_XC_ATTRALL) )
15#define MOUSE_xc_buildall(a) ( (AV*)MOUSE_av_at((a), MOUSE_XC_BUILDALL) )
9974d611 16#define MOUSE_xc_demolishall(a) ( (AV*)MOUSE_av_at((a), MOUSE_XC_DEMOLISHALL) )
a39e9541 17
aa2d2e2c 18enum mouse_xc_flags_t {
19 MOUSEf_XC_IS_IMMUTABLE = 0x0001,
20 MOUSEf_XC_IS_ANON = 0x0002,
21 MOUSEf_XC_HAS_BUILDARGS = 0x0004,
22
23 MOUSEf_XC_mask = 0xFFFF /* not used */
24};
25
a39e9541 26/* Mouse XS Metaclass object */
27enum mouse_xc_ix_t{
4e7e3250 28 MOUSE_XC_FLAGS,
29
a39e9541 30 MOUSE_XC_GEN, /* class generation */
aa2d2e2c 31 MOUSE_XC_STASH, /* symbol table hash */
aa2d2e2c 32
a39e9541 33 MOUSE_XC_ATTRALL, /* all the attributes */
34 MOUSE_XC_BUILDALL, /* all the BUILD methods */
35 MOUSE_XC_DEMOLISHALL, /* all the DEMOLISH methods */
36
37 MOUSE_XC_last
38};
39
40static MGVTBL mouse_xc_vtbl; /* for identity */
41
42static void
43mouse_class_push_attribute_list(pTHX_ SV* const metaclass, AV* const attrall, HV* const seen){
44 dSP;
45 I32 n;
46
47 /* $meta->get_attribute_list */
48 PUSHMARK(SP);
49 XPUSHs(metaclass);
50 PUTBACK;
51
047d7af0 52 n = call_sv(mouse_get_attribute_list, G_ARRAY | G_METHOD);
a39e9541 53 for(NOOP; n > 0; n--){
54 SV* name;
55
56 SPAGAIN;
57 name = POPs;
58 PUTBACK;
59
60 if(hv_exists_ent(seen, name, 0U)){
61 continue;
62 }
63 (void)hv_store_ent(seen, name, &PL_sv_undef, 0U);
da4432f3 64
047d7af0 65 av_push(attrall, newSVsv( mcall1(metaclass, mouse_get_attribute, name) ));
a39e9541 66 }
67}
743ca82e 68
aa2d2e2c 69static int
70mouse_class_has_custom_buildargs(pTHX_ HV* const stash) {
71 XS(XS_Mouse__Object_BUILDARGS); /* prototype */
72
73 GV* const buildargs = gv_fetchmeth_autoload(stash, "BUILDARGS", sizeof("BUILDARGS")-1, 0);
aa2d2e2c 74
12872cc1 75 return buildargs && CvXSUB(GvCV(buildargs)) != XS_Mouse__Object_BUILDARGS;
aa2d2e2c 76}
77
a39e9541 78static void
79mouse_class_update_xc(pTHX_ SV* const metaclass PERL_UNUSED_DECL, HV* const stash, AV* const xc) {
80 AV* const linearized_isa = mro_get_linear_isa(stash);
81 I32 const len = AvFILLp(linearized_isa);
82 I32 i;
aa2d2e2c 83 U32 flags = 0x00;
a39e9541 84 AV* const attrall = newAV();
85 AV* const buildall = newAV();
86 AV* const demolishall = newAV();
87 HV* const seen = newHV(); /* for attributes */
da4432f3 88
a39e9541 89 ENTER;
90 SAVETMPS;
da4432f3 91
a39e9541 92 sv_2mortal((SV*)seen);
da4432f3 93
a39e9541 94 /* old data will be delete at the end of the perl scope */
95 av_delete(xc, MOUSE_XC_DEMOLISHALL, 0x00);
96 av_delete(xc, MOUSE_XC_BUILDALL, 0x00);
97 av_delete(xc, MOUSE_XC_ATTRALL, 0x00);
743ca82e 98
a39e9541 99 SvREFCNT_inc_simple_void_NN(linearized_isa);
100 sv_2mortal((SV*)linearized_isa);
da4432f3 101
a39e9541 102 /* update */
da4432f3 103
4e7e3250 104 if(predicate_calls(metaclass, "is_immutable")){
aa2d2e2c 105 flags |= MOUSEf_XC_IS_IMMUTABLE;
106 }
107
4e7e3250 108 if(predicate_calls(metaclass, "is_anon_class")){
109 flags |= MOUSEf_XC_IS_ANON;
110 }
111
aa2d2e2c 112 if(mouse_class_has_custom_buildargs(aTHX_ stash)){
113 flags |= MOUSEf_XC_HAS_BUILDARGS;
114 }
115
4e7e3250 116 av_store(xc, MOUSE_XC_FLAGS, newSVuv(flags));
a39e9541 117 av_store(xc, MOUSE_XC_ATTRALL, (SV*)attrall);
118 av_store(xc, MOUSE_XC_BUILDALL, (SV*)buildall);
119 av_store(xc, MOUSE_XC_DEMOLISHALL, (SV*)demolishall);
da4432f3 120
a39e9541 121 for(i = 0; i < len; i++){
047d7af0 122 SV* const klass = MOUSE_av_at(linearized_isa, i);
074a414d 123 HV* const st = gv_stashsv(klass, TRUE);
a39e9541 124 SV* meta;
125 GV* gv;
da4432f3 126
074a414d 127 gv = stash_fetchs(st, "BUILD", FALSE);
a39e9541 128 if(gv && GvCVu(gv)){
074a414d 129 av_unshift(buildall, 1);
130 av_store(buildall, 0, newRV_inc((SV*)GvCV(gv)));
a39e9541 131 }
da4432f3 132
074a414d 133 gv = stash_fetchs(st, "DEMOLISH", FALSE);
a39e9541 134 if(gv && GvCVu(gv)){
135 av_push(demolishall, newRV_inc((SV*)GvCV(gv)));
136 }
da4432f3 137
a39e9541 138 /* ATTRIBUTES */
aa2d2e2c 139 meta = get_metaclass(klass);
a39e9541 140 if(!SvOK(meta)){
141 continue; /* skip non-Mouse classes */
142 }
da4432f3 143
a39e9541 144 mouse_class_push_attribute_list(aTHX_ meta, attrall, seen);
145 }
da4432f3 146
a39e9541 147 FREETMPS;
148 LEAVE;
da4432f3 149
a39e9541 150 sv_setuv(MOUSE_xc_gen(xc), mro_get_pkg_gen(stash));
151}
da4432f3 152
3001d9ac 153static AV*
a39e9541 154mouse_get_xc(pTHX_ SV* const metaclass) {
155 AV* xc;
156 SV* gen;
157 HV* stash;
158 MAGIC* mg;
159
160 if(!IsObject(metaclass)){
161 croak("Not a Mouse metaclass");
162 }
da4432f3 163
a39e9541 164 mg = mouse_mg_find(aTHX_ SvRV(metaclass), &mouse_xc_vtbl, 0x00);
165 if(!mg){
166 SV* const package = get_slot(metaclass, mouse_package);
aa2d2e2c 167 STRLEN len;
168 const char* const pv = SvPV_const(package, len);
da4432f3 169
aa2d2e2c 170 stash = gv_stashpvn(pv, len, TRUE);
a39e9541 171 xc = newAV();
da4432f3 172
aa2d2e2c 173 mg = sv_magicext(SvRV(metaclass), (SV*)xc, PERL_MAGIC_ext, &mouse_xc_vtbl, pv, len);
a39e9541 174 SvREFCNT_dec(xc); /* refcnt++ in sv_magicext */
da4432f3 175
a39e9541 176 av_extend(xc, MOUSE_XC_last - 1);
aa2d2e2c 177
a5c683f6 178 av_store(xc, MOUSE_XC_GEN, newSVuv(0U));
aa2d2e2c 179 av_store(xc, MOUSE_XC_STASH, (SV*)stash);
4e7e3250 180
aa2d2e2c 181 SvREFCNT_inc_simple_void_NN(stash);
da4432f3 182 }
a39e9541 183 else{
a39e9541 184 xc = (AV*)MOUSE_mg_obj(mg);
da4432f3 185
a39e9541 186 assert(xc);
187 assert(SvTYPE(xc) == SVt_PVAV);
188 }
da4432f3 189
aa2d2e2c 190 gen = MOUSE_xc_gen(xc);
a5c683f6 191
192 if(SvUVX(gen) != 0U && MOUSE_xc_flags(xc) & MOUSEf_XC_IS_IMMUTABLE){
193 return xc;
194 }
195
aa2d2e2c 196 stash = MOUSE_xc_stash(xc);
197
a5c683f6 198 if(SvUVX(gen) != mro_get_pkg_gen(stash)){
a39e9541 199 mouse_class_update_xc(aTHX_ metaclass, stash, xc);
da4432f3 200 }
a39e9541 201
202 return xc;
203}
204
3001d9ac 205static HV*
074a414d 206mouse_buildargs(pTHX_ SV* metaclass, SV* const klass, I32 ax, I32 items) {
aa2d2e2c 207 HV* args;
074a414d 208
209 /* shift @_ */
210 ax++;
211 items--;
212
213 if(items == 1){
214 SV* const args_ref = ST(0);
aa2d2e2c 215 if(!IsHashRef(args_ref)){
216 if(!metaclass){ metaclass = get_metaclass(klass); }
217 mouse_throw_error(metaclass, NULL, "Single parameters to new() must be a HASH ref");
218 }
219 args = newHVhv((HV*)SvRV(args_ref));
220 sv_2mortal((SV*)args);
221 }
222 else{
223 I32 i;
224
6fab29c6 225 args = newHV_mortal();
aa2d2e2c 226
074a414d 227 if( (items % 2) != 0 ){
aa2d2e2c 228 if(!metaclass){ metaclass = get_metaclass(klass); }
229 mouse_throw_error(metaclass, NULL, "Odd number of parameters to new()");
230 }
231
074a414d 232 for(i = 0; i < items; i += 2){
aa2d2e2c 233 (void)hv_store_ent(args, ST(i), newSVsv(ST(i+1)), 0U);
234 }
235
236 }
237 return args;
238}
239
3001d9ac 240static void
d4712779 241mouse_class_initialize_object(pTHX_ SV* const meta, SV* const object, HV* const args, bool const ignore_triggers) {
242 AV* const xc = mouse_get_xc(aTHX_ meta);
243 AV* const attrs = MOUSE_xc_attrall(xc);
4e7e3250 244 I32 len = AvFILLp(attrs) + 1;
d4712779 245 I32 i;
4e7e3250 246 AV* triggers_queue = NULL;
247
074a414d 248 assert(meta || object);
249 assert(args);
250 assert(SvTYPE(args) == SVt_PVHV);
251
4e7e3250 252 ENTER;
253 SAVETMPS;
254
255 if(!ignore_triggers){
256 triggers_queue = newAV_mortal();
257 }
0aad0266 258
d4712779 259 for(i = 0; i < len; i++){
6bc5b495 260 SV* const attr = MOUSE_av_at(attrs, i);
261 AV* const xa = mouse_get_xa(aTHX_ attr);
4e7e3250 262
263 SV* const slot = MOUSE_xa_slot(xa);
264 U16 const flags = (U16)MOUSE_xa_flags(xa);
265 SV* const init_arg = MOUSE_xa_init_arg(xa);
266 HE* he;
267
268 if(SvOK(init_arg) && ( he = hv_fetch_ent(args, init_arg, FALSE, 0U) ) ){
269 SV* value = HeVAL(he);
270 if(flags & MOUSEf_ATTR_HAS_TC){
271 value = mouse_xa_apply_type_constraint(aTHX_ xa, value, flags);
272 }
273 set_slot(object, slot, value);
274 if(SvROK(value) && flags & MOUSEf_ATTR_IS_WEAK_REF){
275 weaken_slot(object, slot);
276 }
277 if(flags & MOUSEf_ATTR_HAS_TRIGGER && triggers_queue){
278 AV* const pair = newAV();
279 av_push(pair, newSVsv( mcall0s(attr, "trigger") ));
280 av_push(pair, newSVsv(value));
281
282 av_push(triggers_queue, (SV*)pair);
283 }
284 }
285 else { /* no init arg */
286 if(flags & (MOUSEf_ATTR_HAS_DEFAULT | MOUSEf_ATTR_HAS_BUILDER)){
287 if(!(flags & MOUSEf_ATTR_IS_LAZY)){
288 mouse_xa_set_default(aTHX_ xa, object);
289 }
290 }
291 else if(flags & MOUSEf_ATTR_IS_REQUIRED) {
292 mouse_throw_error(attr, NULL, "Attribute (%"SVf") is required", slot);
293 }
294 }
295 } /* for each attributes */
296
297 if(triggers_queue){
298 len = AvFILLp(triggers_queue) + 1;
299 for(i = 0; i < len; i++){
300 AV* const pair = (AV*)AvARRAY(triggers_queue)[i];
301 SV* const trigger = AvARRAY(pair)[0];
302 SV* const value = AvARRAY(pair)[1];
303
304 mcall1(object, trigger, value);
305 }
306 }
307
308 if(MOUSE_xc_flags(xc) & MOUSEf_XC_IS_ANON){
36d75a0a 309 set_slot(object, newSVpvs_flags("__METACLASS__", SVs_TEMP), meta);
d4712779 310 }
4e7e3250 311
312 FREETMPS;
313 LEAVE;
aa2d2e2c 314}
315
a5c683f6 316static SV*
1bbf8369 317mouse_initialize_metaclass(pTHX_ SV* const klass) {
318 SV* meta = get_metaclass(klass);
319
320 if(!SvOK(meta)){
321 dSP;
322 PUSHMARK(SP);
323
324 EXTEND(SP, 2);
325 mPUSHp("Mouse::Meta::Class", sizeof("Mouse::Meta::Class")-1);
326 PUSHs(klass);
327 PUTBACK;
328
329 call_method("initialize", G_SCALAR);
330 SPAGAIN;
331 meta = POPs;
332 PUTBACK;
333 }
334
335 return meta;
336}
337
646c0371 338MODULE = Mouse PACKAGE = Mouse
df6dd016 339
340PROTOTYPES: DISABLE
341
cccb83de 342BOOT:
343 mouse_package = newSVpvs_share("package");
344 mouse_namespace = newSVpvs_share("namespace");
3e44140b 345 mouse_methods = newSVpvs_share("methods");
a5df48e5 346 mouse_name = newSVpvs_share("name");
3e44140b 347
047d7af0 348 mouse_get_attribute = newSVpvs_share("get_attribute");
349 mouse_get_attribute_list = newSVpvs_share("get_attribute_list");
da4432f3 350
646c0371 351 MOUSE_CALL_BOOT(Mouse__Util);
1d5ecd5f 352 MOUSE_CALL_BOOT(Mouse__Util__TypeConstraints);
646c0371 353 MOUSE_CALL_BOOT(Mouse__Meta__Method__Accessor__XS);
aa2d2e2c 354 MOUSE_CALL_BOOT(Mouse__Meta__Attribute);
f48920c1 355
cccb83de 356
7d96ae4d 357MODULE = Mouse PACKAGE = Mouse::Meta::Module
358
359BOOT:
360 INSTALL_SIMPLE_READER_WITH_KEY(Module, name, package);
361 INSTALL_SIMPLE_READER_WITH_KEY(Module, _method_map, methods);
362 INSTALL_SIMPLE_READER_WITH_KEY(Module, _attribute_map, attributes);
363
364HV*
365namespace(SV* self)
366CODE:
367{
6a97bbda 368 SV* const package = get_slot(self, mouse_package);
7d96ae4d 369 if(!(package && SvOK(package))){
3e44140b 370 croak("No package name defined");
7d96ae4d 371 }
372 RETVAL = gv_stashsv(package, GV_ADDMULTI);
373}
374OUTPUT:
375 RETVAL
376
3e44140b 377# ignore extra arguments for extensibility
378void
379add_method(SV* self, SV* name, SV* code, ...)
380CODE:
381{
6a97bbda 382 SV* const package = get_slot(self, mouse_package); /* $self->{package} */
383 SV* const methods = get_slot(self, mouse_methods); /* $self->{methods} */
3e44140b 384 GV* gv;
385 SV* code_ref;
386
387 if(!(package && SvOK(package))){
388 croak("No package name defined");
389 }
390
391 SvGETMAGIC(name);
392 SvGETMAGIC(code);
393
394 if(!SvOK(name)){
395 mouse_throw_error(self, NULL, "You must define a method name");
396 }
397 if(!SvROK(code)){
398 mouse_throw_error(self, NULL, "You must define a CODE reference");
399 }
400
401 code_ref = code;
402 if(SvTYPE(SvRV(code_ref)) != SVt_PVCV){
403 SV* sv = code_ref; /* used in tryAMAGICunDEREF */
404 SV** sp = &sv; /* used in tryAMAGICunDEREF */
405 tryAMAGICunDEREF(to_cv); /* try \&{$code} */
24d5564d 406 if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)){
407 mouse_throw_error(self, NULL, "You must pass a CODE reference to add_method");
3e44140b 408 }
409 code_ref = sv;
410 }
411
412 /* *{$package . '::' . $name} -> *gv */
413 gv = gv_fetchpv(form("%"SVf"::%"SVf, package, name), GV_ADDMULTI, SVt_PVCV);
414 if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
415 SvREFCNT_dec(GvCV(gv));
416 GvCV(gv) = NULL;
417 }
418 sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
419
6fe2272b 420 set_slot(methods, name, code); /* $self->{methods}{$name} = $code */
3e44140b 421
855eff0e 422 /* name the CODE ref if it's anonymous */
423 {
424 CV* const code_entity = (CV*)SvRV(code_ref);
425 if(CvANON(code_entity)
426 && CvGV(code_entity) /* a cv under construction has no gv */ ){
427
428 CvGV(code_entity) = gv;
429 CvANON_off(code_entity);
430 }
431 }
3e44140b 432}
433
43165725 434MODULE = Mouse PACKAGE = Mouse::Meta::Class
435
436BOOT:
437 INSTALL_SIMPLE_READER(Class, roles);
438 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id);
a5c683f6 439
787f84c2 440 INSTALL_CLASS_HOLDER(Class, method_metaclass, "Mouse::Meta::Method");
441 INSTALL_CLASS_HOLDER(Class, attribute_metaclass, "Mouse::Meta::Attribute");
442 INSTALL_CLASS_HOLDER(Class, constructor_class, "Mouse::Meta::Method::Constructor::XS");
443 INSTALL_CLASS_HOLDER(Class, destructor_class, "Mouse::Meta::Method::Destructor::XS");
a5c683f6 444
445 newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Constructor::XS", TRUE), "_generate_constructor",
446 newRV_inc((SV*)get_cvs("Mouse::Object::new", TRUE)));
447 newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Destructor::XS", TRUE), "_generate_destructor",
448 newRV_inc((SV*)get_cvs("Mouse::Object::DESTROY", TRUE)));
449
43165725 450
cccb83de 451void
452linearized_isa(SV* self)
453PPCODE:
454{
455 SV* const stash_ref = mcall0(self, mouse_namespace); /* $self->namespace */
456 AV* linearized_isa;
457 I32 len;
458 I32 i;
459 if(!(SvROK(stash_ref) && SvTYPE(SvRV(stash_ref)) == SVt_PVHV)){
460 croak("namespace() didn't return a HASH reference");
461 }
462 linearized_isa = mro_get_linear_isa((HV*)SvRV(stash_ref));
463 len = AvFILLp(linearized_isa) + 1;
464 EXTEND(SP, len);
465 for(i = 0; i < len; i++){
466 PUSHs(AvARRAY(linearized_isa)[i]);
467 }
468}
469
da4432f3 470void
471get_all_attributes(SV* self)
472PPCODE:
473{
0aad0266 474 AV* const xc = mouse_get_xc(aTHX_ self);
ae34e077 475 AV* const all_attrs = MOUSE_xc_attrall(xc);
da4432f3 476 I32 const len = AvFILLp(all_attrs) + 1;
477 I32 i;
478
479 EXTEND(SP, len);
480 for(i = 0; i < len; i++){
481 PUSHs( MOUSE_av_at(all_attrs, i) );
482 }
483}
441964ce 484
aa2d2e2c 485SV*
ba153b33 486new_object(SV* meta, ...)
aa2d2e2c 487CODE:
488{
aa2d2e2c 489 AV* const xc = mouse_get_xc(aTHX_ meta);
074a414d 490 HV* const args = mouse_buildargs(aTHX_ meta, NULL, ax, items);
d4712779 491
aa2d2e2c 492 RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc));
d4712779 493 mouse_class_initialize_object(aTHX_ meta, RETVAL, args, FALSE);
aa2d2e2c 494}
ba153b33 495OUTPUT:
496 RETVAL
aa2d2e2c 497
498void
4e7e3250 499_initialize_object(SV* meta, SV* object, HV* args, bool ignore_triggers = FALSE)
aa2d2e2c 500CODE:
501{
d4712779 502 mouse_class_initialize_object(aTHX_ meta, object, args, ignore_triggers);
aa2d2e2c 503}
504
43165725 505MODULE = Mouse PACKAGE = Mouse::Meta::Role
506
507BOOT:
508 INSTALL_SIMPLE_READER_WITH_KEY(Role, get_roles, roles);
509 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Role, is_anon_role, anon_serial_id);
510
787f84c2 511 INSTALL_CLASS_HOLDER(Role, method_metaclass, "Mouse::Meta::Role::Method");
e058b279 512
aa2d2e2c 513MODULE = Mouse PACKAGE = Mouse::Object
43165725 514
074a414d 515SV*
516new(SV* klass, ...)
517CODE:
518{
1bbf8369 519 SV* const meta = mouse_initialize_metaclass(aTHX_ klass);
074a414d 520 AV* const xc = mouse_get_xc(aTHX_ meta);
521 UV const flags = MOUSE_xc_flags(xc);
522 SV* args;
a5c683f6 523 AV* buildall;
074a414d 524 I32 len, i;
525
526 /* BUILDARGS */
527 if(flags & MOUSEf_XC_HAS_BUILDARGS){
528 SPAGAIN;
529
530 PUSHMARK(SP);
531 EXTEND(SP, items);
532 for(i = 0; i < items; i++){
533 PUSHs(ST(i));
534 }
535 //SP += items;
536 PUTBACK;
537 call_method("BUILDARGS", G_SCALAR);
538 SPAGAIN;
539 args = POPs;
540 PUTBACK;
541
542 if(!IsHashRef(args)){
543 croak("BUILDARGS did not return a HASH reference");
544 }
545 }
546 else{
547 args = newRV_inc((SV*)mouse_buildargs(aTHX_ meta, klass, ax, items));
548 sv_2mortal(args);
549 }
550
551 /* new_object */
552 RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc));
553 mouse_class_initialize_object(aTHX_ meta, RETVAL, (HV*)SvRV(args), FALSE);
554
555 /* BUILDALL */
a5c683f6 556 buildall = MOUSE_xc_buildall(xc);
557 len = AvFILLp(buildall) + 1;
074a414d 558 for(i = 0; i < len; i++){
559 dSP;
560
561 PUSHMARK(SP);
562 EXTEND(SP, 2);
93fe8a5f 563 PUSHs(RETVAL); /* self */
074a414d 564 PUSHs(args);
565 PUTBACK;
566
a5c683f6 567 call_sv(AvARRAY(buildall)[i], G_VOID | G_DISCARD);
074a414d 568 }
569}
570OUTPUT:
571 RETVAL
572
a5c683f6 573void
574DESTROY(SV* object)
aa2d2e2c 575CODE:
576{
a5c683f6 577 SV* const meta = get_metaclass(object);
578 AV* demolishall;
579 I32 len, i;
580
581 if(!IsObject(object)){
582 croak("You must not call DESTROY as a class method");
583 }
584
585 if(SvOK(meta)){
586 AV* const xc = mouse_get_xc(aTHX_ meta);
587
588 demolishall = MOUSE_xc_demolishall(xc);
589 }
590 else {
591 AV* const linearized_isa = mro_get_linear_isa(SvSTASH(SvRV(object)));
7efbc77d 592
a5c683f6 593 len = AvFILLp(linearized_isa) + 1;
7efbc77d 594
a5c683f6 595 demolishall = newAV_mortal();
596 for(i = 0; i < len; i++){
597 SV* const klass = MOUSE_av_at(linearized_isa, i);
598 HV* const st = gv_stashsv(klass, TRUE);
599 GV* const gv = stash_fetchs(st, "DEMOLISH", FALSE);
600 if(gv && GvCVu(gv)){
601 av_push(demolishall, newRV_inc((SV*)GvCV(gv)));
602 }
603 }
604 }
605
606 /* DEMOLISHALL */
607 len = AvFILLp(demolishall) + 1;
608 if(len > 0){
609 GV* const statusvalue = gv_fetchpvs("?", 0, SVt_PV);
610 SAVESPTR(GvSV(statusvalue)); /* local $? */
611 SAVESPTR(ERRSV); /* local $@ */
612
f8beb873 613 GvSV(statusvalue) = sv_newmortal();
614 ERRSV = newSVpvs_flags("", SVs_TEMP);
a5c683f6 615 for(i = 0; i < len; i++){
f8beb873 616 SPAGAIN;
a5c683f6 617
618 PUSHMARK(SP);
619 XPUSHs(object);
620 PUTBACK;
621
622 call_sv(AvARRAY(demolishall)[i], G_VOID | G_DISCARD | G_EVAL);
623 if(SvTRUE(ERRSV)){
624 SV* const e = newSVsv(ERRSV);
625
626 FREETMPS;
627 LEAVE;
628
629 sv_setsv(ERRSV, e);
630 SvREFCNT_dec(e);
631 croak(NULL); /* rethrow */
632 }
633 }
634 }
635}
636
637HV*
638BUILDARGS(SV* klass, ...)
7efbc77d 639CODE:
640{
a5c683f6 641 RETVAL = mouse_buildargs(aTHX_ NULL, klass, ax, items);
7efbc77d 642}
643OUTPUT:
644 RETVAL
645
a5c683f6 646