Move tied hash checks
[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
6463e839 252 if(mg_find((SV*)args, PERL_MAGIC_tied)){
253 croak("You cannot use tied HASH reference as initializing arguments");
254 }
255
4e7e3250 256 ENTER;
257 SAVETMPS;
258
259 if(!ignore_triggers){
260 triggers_queue = newAV_mortal();
261 }
0aad0266 262
d4712779 263 for(i = 0; i < len; i++){
6bc5b495 264 SV* const attr = MOUSE_av_at(attrs, i);
265 AV* const xa = mouse_get_xa(aTHX_ attr);
4e7e3250 266
267 SV* const slot = MOUSE_xa_slot(xa);
268 U16 const flags = (U16)MOUSE_xa_flags(xa);
269 SV* const init_arg = MOUSE_xa_init_arg(xa);
270 HE* he;
271
272 if(SvOK(init_arg) && ( he = hv_fetch_ent(args, init_arg, FALSE, 0U) ) ){
273 SV* value = HeVAL(he);
274 if(flags & MOUSEf_ATTR_HAS_TC){
275 value = mouse_xa_apply_type_constraint(aTHX_ xa, value, flags);
276 }
277 set_slot(object, slot, value);
278 if(SvROK(value) && flags & MOUSEf_ATTR_IS_WEAK_REF){
279 weaken_slot(object, slot);
280 }
281 if(flags & MOUSEf_ATTR_HAS_TRIGGER && triggers_queue){
282 AV* const pair = newAV();
283 av_push(pair, newSVsv( mcall0s(attr, "trigger") ));
284 av_push(pair, newSVsv(value));
285
286 av_push(triggers_queue, (SV*)pair);
287 }
288 }
289 else { /* no init arg */
290 if(flags & (MOUSEf_ATTR_HAS_DEFAULT | MOUSEf_ATTR_HAS_BUILDER)){
291 if(!(flags & MOUSEf_ATTR_IS_LAZY)){
292 mouse_xa_set_default(aTHX_ xa, object);
293 }
294 }
295 else if(flags & MOUSEf_ATTR_IS_REQUIRED) {
296 mouse_throw_error(attr, NULL, "Attribute (%"SVf") is required", slot);
297 }
298 }
299 } /* for each attributes */
300
301 if(triggers_queue){
302 len = AvFILLp(triggers_queue) + 1;
303 for(i = 0; i < len; i++){
304 AV* const pair = (AV*)AvARRAY(triggers_queue)[i];
305 SV* const trigger = AvARRAY(pair)[0];
306 SV* const value = AvARRAY(pair)[1];
307
308 mcall1(object, trigger, value);
309 }
310 }
311
312 if(MOUSE_xc_flags(xc) & MOUSEf_XC_IS_ANON){
36d75a0a 313 set_slot(object, newSVpvs_flags("__METACLASS__", SVs_TEMP), meta);
d4712779 314 }
4e7e3250 315
316 FREETMPS;
317 LEAVE;
aa2d2e2c 318}
319
a5c683f6 320static SV*
1bbf8369 321mouse_initialize_metaclass(pTHX_ SV* const klass) {
322 SV* meta = get_metaclass(klass);
323
324 if(!SvOK(meta)){
325 dSP;
326 PUSHMARK(SP);
327
328 EXTEND(SP, 2);
329 mPUSHp("Mouse::Meta::Class", sizeof("Mouse::Meta::Class")-1);
330 PUSHs(klass);
331 PUTBACK;
332
333 call_method("initialize", G_SCALAR);
334 SPAGAIN;
335 meta = POPs;
336 PUTBACK;
337 }
338
339 return meta;
340}
341
adb5eb76 342static void
343mouse_buildall(pTHX_ AV* const xc, SV* const object, SV* const args) {
344 AV* const buildall = MOUSE_xc_buildall(xc);
345 I32 const len = AvFILLp(buildall) + 1;
346 I32 i;
347 for(i = 0; i < len; i++){
348 dSP;
349
350 PUSHMARK(SP);
351 EXTEND(SP, 2);
352 PUSHs(object);
353 PUSHs(args);
354 PUTBACK;
355
356 call_sv(AvARRAY(buildall)[i], G_VOID | G_DISCARD);
357 }
358}
359
646c0371 360MODULE = Mouse PACKAGE = Mouse
df6dd016 361
362PROTOTYPES: DISABLE
363
cccb83de 364BOOT:
365 mouse_package = newSVpvs_share("package");
366 mouse_namespace = newSVpvs_share("namespace");
3e44140b 367 mouse_methods = newSVpvs_share("methods");
a5df48e5 368 mouse_name = newSVpvs_share("name");
3e44140b 369
047d7af0 370 mouse_get_attribute = newSVpvs_share("get_attribute");
371 mouse_get_attribute_list = newSVpvs_share("get_attribute_list");
da4432f3 372
646c0371 373 MOUSE_CALL_BOOT(Mouse__Util);
1d5ecd5f 374 MOUSE_CALL_BOOT(Mouse__Util__TypeConstraints);
646c0371 375 MOUSE_CALL_BOOT(Mouse__Meta__Method__Accessor__XS);
aa2d2e2c 376 MOUSE_CALL_BOOT(Mouse__Meta__Attribute);
f48920c1 377
cccb83de 378
7d96ae4d 379MODULE = Mouse PACKAGE = Mouse::Meta::Module
380
381BOOT:
382 INSTALL_SIMPLE_READER_WITH_KEY(Module, name, package);
383 INSTALL_SIMPLE_READER_WITH_KEY(Module, _method_map, methods);
384 INSTALL_SIMPLE_READER_WITH_KEY(Module, _attribute_map, attributes);
385
386HV*
387namespace(SV* self)
388CODE:
389{
6a97bbda 390 SV* const package = get_slot(self, mouse_package);
7d96ae4d 391 if(!(package && SvOK(package))){
3e44140b 392 croak("No package name defined");
7d96ae4d 393 }
394 RETVAL = gv_stashsv(package, GV_ADDMULTI);
395}
396OUTPUT:
397 RETVAL
398
3e44140b 399# ignore extra arguments for extensibility
400void
401add_method(SV* self, SV* name, SV* code, ...)
402CODE:
403{
6a97bbda 404 SV* const package = get_slot(self, mouse_package); /* $self->{package} */
405 SV* const methods = get_slot(self, mouse_methods); /* $self->{methods} */
3e44140b 406 GV* gv;
407 SV* code_ref;
408
409 if(!(package && SvOK(package))){
410 croak("No package name defined");
411 }
412
413 SvGETMAGIC(name);
414 SvGETMAGIC(code);
415
416 if(!SvOK(name)){
417 mouse_throw_error(self, NULL, "You must define a method name");
418 }
419 if(!SvROK(code)){
420 mouse_throw_error(self, NULL, "You must define a CODE reference");
421 }
422
423 code_ref = code;
424 if(SvTYPE(SvRV(code_ref)) != SVt_PVCV){
425 SV* sv = code_ref; /* used in tryAMAGICunDEREF */
426 SV** sp = &sv; /* used in tryAMAGICunDEREF */
427 tryAMAGICunDEREF(to_cv); /* try \&{$code} */
24d5564d 428 if(!(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV)){
429 mouse_throw_error(self, NULL, "You must pass a CODE reference to add_method");
3e44140b 430 }
431 code_ref = sv;
432 }
433
434 /* *{$package . '::' . $name} -> *gv */
435 gv = gv_fetchpv(form("%"SVf"::%"SVf, package, name), GV_ADDMULTI, SVt_PVCV);
436 if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
437 SvREFCNT_dec(GvCV(gv));
438 GvCV(gv) = NULL;
439 }
440 sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
441
6fe2272b 442 set_slot(methods, name, code); /* $self->{methods}{$name} = $code */
3e44140b 443
855eff0e 444 /* name the CODE ref if it's anonymous */
445 {
446 CV* const code_entity = (CV*)SvRV(code_ref);
447 if(CvANON(code_entity)
448 && CvGV(code_entity) /* a cv under construction has no gv */ ){
449
450 CvGV(code_entity) = gv;
451 CvANON_off(code_entity);
452 }
453 }
3e44140b 454}
455
43165725 456MODULE = Mouse PACKAGE = Mouse::Meta::Class
457
458BOOT:
459 INSTALL_SIMPLE_READER(Class, roles);
460 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id);
80efe911 461 INSTALL_SIMPLE_READER(Class, is_immutable);
a5c683f6 462
787f84c2 463 INSTALL_CLASS_HOLDER(Class, method_metaclass, "Mouse::Meta::Method");
464 INSTALL_CLASS_HOLDER(Class, attribute_metaclass, "Mouse::Meta::Attribute");
465 INSTALL_CLASS_HOLDER(Class, constructor_class, "Mouse::Meta::Method::Constructor::XS");
466 INSTALL_CLASS_HOLDER(Class, destructor_class, "Mouse::Meta::Method::Destructor::XS");
a5c683f6 467
468 newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Constructor::XS", TRUE), "_generate_constructor",
469 newRV_inc((SV*)get_cvs("Mouse::Object::new", TRUE)));
470 newCONSTSUB(gv_stashpvs("Mouse::Meta::Method::Destructor::XS", TRUE), "_generate_destructor",
471 newRV_inc((SV*)get_cvs("Mouse::Object::DESTROY", TRUE)));
472
43165725 473
cccb83de 474void
475linearized_isa(SV* self)
476PPCODE:
477{
478 SV* const stash_ref = mcall0(self, mouse_namespace); /* $self->namespace */
479 AV* linearized_isa;
480 I32 len;
481 I32 i;
482 if(!(SvROK(stash_ref) && SvTYPE(SvRV(stash_ref)) == SVt_PVHV)){
483 croak("namespace() didn't return a HASH reference");
484 }
485 linearized_isa = mro_get_linear_isa((HV*)SvRV(stash_ref));
486 len = AvFILLp(linearized_isa) + 1;
487 EXTEND(SP, len);
488 for(i = 0; i < len; i++){
489 PUSHs(AvARRAY(linearized_isa)[i]);
490 }
491}
492
da4432f3 493void
494get_all_attributes(SV* self)
495PPCODE:
496{
0aad0266 497 AV* const xc = mouse_get_xc(aTHX_ self);
ae34e077 498 AV* const all_attrs = MOUSE_xc_attrall(xc);
da4432f3 499 I32 const len = AvFILLp(all_attrs) + 1;
500 I32 i;
501
502 EXTEND(SP, len);
503 for(i = 0; i < len; i++){
504 PUSHs( MOUSE_av_at(all_attrs, i) );
505 }
506}
441964ce 507
aa2d2e2c 508SV*
ba153b33 509new_object(SV* meta, ...)
aa2d2e2c 510CODE:
511{
aa2d2e2c 512 AV* const xc = mouse_get_xc(aTHX_ meta);
074a414d 513 HV* const args = mouse_buildargs(aTHX_ meta, NULL, ax, items);
d4712779 514
aa2d2e2c 515 RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc));
d4712779 516 mouse_class_initialize_object(aTHX_ meta, RETVAL, args, FALSE);
aa2d2e2c 517}
ba153b33 518OUTPUT:
519 RETVAL
aa2d2e2c 520
521void
4e7e3250 522_initialize_object(SV* meta, SV* object, HV* args, bool ignore_triggers = FALSE)
aa2d2e2c 523CODE:
524{
d4712779 525 mouse_class_initialize_object(aTHX_ meta, object, args, ignore_triggers);
aa2d2e2c 526}
527
43165725 528MODULE = Mouse PACKAGE = Mouse::Meta::Role
529
530BOOT:
531 INSTALL_SIMPLE_READER_WITH_KEY(Role, get_roles, roles);
532 INSTALL_SIMPLE_PREDICATE_WITH_KEY(Role, is_anon_role, anon_serial_id);
533
787f84c2 534 INSTALL_CLASS_HOLDER(Role, method_metaclass, "Mouse::Meta::Role::Method");
e058b279 535
aa2d2e2c 536MODULE = Mouse PACKAGE = Mouse::Object
43165725 537
074a414d 538SV*
539new(SV* klass, ...)
540CODE:
541{
1bbf8369 542 SV* const meta = mouse_initialize_metaclass(aTHX_ klass);
074a414d 543 AV* const xc = mouse_get_xc(aTHX_ meta);
544 UV const flags = MOUSE_xc_flags(xc);
545 SV* args;
074a414d 546
547 /* BUILDARGS */
548 if(flags & MOUSEf_XC_HAS_BUILDARGS){
adb5eb76 549 I32 i;
074a414d 550 SPAGAIN;
551
552 PUSHMARK(SP);
553 EXTEND(SP, items);
554 for(i = 0; i < items; i++){
555 PUSHs(ST(i));
556 }
557 //SP += items;
558 PUTBACK;
559 call_method("BUILDARGS", G_SCALAR);
560 SPAGAIN;
561 args = POPs;
562 PUTBACK;
563
564 if(!IsHashRef(args)){
565 croak("BUILDARGS did not return a HASH reference");
566 }
567 }
568 else{
569 args = newRV_inc((SV*)mouse_buildargs(aTHX_ meta, klass, ax, items));
570 sv_2mortal(args);
571 }
572
573 /* new_object */
574 RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc));
575 mouse_class_initialize_object(aTHX_ meta, RETVAL, (HV*)SvRV(args), FALSE);
576
adb5eb76 577 mouse_buildall(aTHX_ xc, RETVAL, args);
074a414d 578}
579OUTPUT:
580 RETVAL
581
a5c683f6 582void
583DESTROY(SV* object)
adb5eb76 584ALIAS:
585 DESTROY = 0
586 DEMOLISHALL = 1
aa2d2e2c 587CODE:
588{
a5c683f6 589 SV* const meta = get_metaclass(object);
590 AV* demolishall;
591 I32 len, i;
592
adb5eb76 593 PERL_UNUSED_VAR(ix);
594
a5c683f6 595 if(!IsObject(object)){
596 croak("You must not call DESTROY as a class method");
597 }
598
599 if(SvOK(meta)){
600 AV* const xc = mouse_get_xc(aTHX_ meta);
601
602 demolishall = MOUSE_xc_demolishall(xc);
603 }
adb5eb76 604 else { /* The metaclass is already destroyed */
a5c683f6 605 AV* const linearized_isa = mro_get_linear_isa(SvSTASH(SvRV(object)));
7efbc77d 606
a5c683f6 607 len = AvFILLp(linearized_isa) + 1;
7efbc77d 608
a5c683f6 609 demolishall = newAV_mortal();
610 for(i = 0; i < len; i++){
611 SV* const klass = MOUSE_av_at(linearized_isa, i);
612 HV* const st = gv_stashsv(klass, TRUE);
613 GV* const gv = stash_fetchs(st, "DEMOLISH", FALSE);
614 if(gv && GvCVu(gv)){
615 av_push(demolishall, newRV_inc((SV*)GvCV(gv)));
616 }
617 }
618 }
619
a5c683f6 620 len = AvFILLp(demolishall) + 1;
621 if(len > 0){
622 GV* const statusvalue = gv_fetchpvs("?", 0, SVt_PV);
623 SAVESPTR(GvSV(statusvalue)); /* local $? */
624 SAVESPTR(ERRSV); /* local $@ */
625
f8beb873 626 GvSV(statusvalue) = sv_newmortal();
627 ERRSV = newSVpvs_flags("", SVs_TEMP);
a5c683f6 628 for(i = 0; i < len; i++){
f8beb873 629 SPAGAIN;
a5c683f6 630
631 PUSHMARK(SP);
632 XPUSHs(object);
633 PUTBACK;
634
635 call_sv(AvARRAY(demolishall)[i], G_VOID | G_DISCARD | G_EVAL);
636 if(SvTRUE(ERRSV)){
637 SV* const e = newSVsv(ERRSV);
638
639 FREETMPS;
640 LEAVE;
641
642 sv_setsv(ERRSV, e);
643 SvREFCNT_dec(e);
644 croak(NULL); /* rethrow */
645 }
646 }
647 }
648}
649
650HV*
651BUILDARGS(SV* klass, ...)
7efbc77d 652CODE:
653{
a5c683f6 654 RETVAL = mouse_buildargs(aTHX_ NULL, klass, ax, items);
7efbc77d 655}
656OUTPUT:
657 RETVAL
658
a5c683f6 659
adb5eb76 660void
661BUILDALL(SV* self, SV* args)
662CODE:
663{
f50e50d6 664 SV* const meta = get_metaclass(self);
665 AV* const xc = mouse_get_xc(aTHX_ meta);
adb5eb76 666
667 if(!IsHashRef(args)){
668 croak("You must pass a HASH reference to BUILDALL");
669 }
adb5eb76 670 mouse_buildall(aTHX_ xc, self, args);
671}