Change inline_constructor => 0
[gitmo/Mouse.git] / xs-src / Mouse.xs
1 #define  NEED_newSVpvn_flags_GLOBAL
2 #include "mouse.h"
3
4 SV* mouse_package;
5 SV* mouse_namespace;
6 SV* mouse_methods;
7 SV* mouse_name;
8 SV* mouse_get_attribute;
9 SV* mouse_get_attribute_list;
10
11 #define MOUSE_xc_flags(a)       SvUVX(MOUSE_av_at((a), MOUSE_XC_FLAGS))
12 #define MOUSE_xc_gen(a)         MOUSE_av_at((a), MOUSE_XC_GEN)
13 #define MOUSE_xc_stash(a)       ( (HV*)MOUSE_av_at((a), MOUSE_XC_STASH) )
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) )
16 #define MOUSE_xc_demolishall(a) ( (AV*)MOUSE_av_at((a), MOUSE_XC_DEOLISHALL) )
17
18 enum 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
26 /* Mouse XS Metaclass object */
27 enum mouse_xc_ix_t{
28     MOUSE_XC_FLAGS,
29
30     MOUSE_XC_GEN,          /* class generation */
31     MOUSE_XC_STASH,        /* symbol table hash */
32
33     MOUSE_XC_BUILDARGS,    /* Custom BUILDARGS */
34
35     MOUSE_XC_ATTRALL,      /* all the attributes */
36     MOUSE_XC_BUILDALL,     /* all the BUILD methods */
37     MOUSE_XC_DEMOLISHALL,  /* all the DEMOLISH methods */
38
39     MOUSE_XC_last
40 };
41
42 static MGVTBL mouse_xc_vtbl; /* for identity */
43
44 static void
45 mouse_class_push_attribute_list(pTHX_ SV* const metaclass, AV* const attrall, HV* const seen){
46     dSP;
47     I32 n;
48
49     /* $meta->get_attribute_list */
50     PUSHMARK(SP);
51     XPUSHs(metaclass);
52     PUTBACK;
53
54     n = call_sv(mouse_get_attribute_list, G_ARRAY | G_METHOD);
55     for(NOOP; n > 0; n--){
56         SV* name;
57
58         SPAGAIN;
59         name = POPs;
60         PUTBACK;
61
62         if(hv_exists_ent(seen, name, 0U)){
63             continue;
64         }
65         (void)hv_store_ent(seen, name, &PL_sv_undef, 0U);
66
67         av_push(attrall, newSVsv( mcall1(metaclass, mouse_get_attribute, name) ));
68     }
69 }
70
71 static int
72 mouse_class_has_custom_buildargs(pTHX_ HV* const stash) {
73     XS(XS_Mouse__Object_BUILDARGS); /* prototype */
74
75     GV* const buildargs = gv_fetchmeth_autoload(stash, "BUILDARGS", sizeof("BUILDARGS")-1, 0);
76
77     return buildargs && CvXSUB(GvCV(buildargs)) == XS_Mouse__Object_BUILDARGS;
78 }
79
80 static void
81 mouse_class_update_xc(pTHX_ SV* const metaclass PERL_UNUSED_DECL, HV* const stash, AV* const xc) {
82     AV* const linearized_isa = mro_get_linear_isa(stash);
83     I32 const len            = AvFILLp(linearized_isa);
84     I32 i;
85     U32 flags             = 0x00;
86     AV* const attrall     = newAV();
87     AV* const buildall    = newAV();
88     AV* const demolishall = newAV();
89     HV* const seen        = newHV(); /* for attributes */
90
91     ENTER;
92     SAVETMPS;
93
94     sv_2mortal((SV*)seen);
95
96      /* old data will be delete at the end of the perl scope */
97     av_delete(xc, MOUSE_XC_DEMOLISHALL, 0x00);
98     av_delete(xc, MOUSE_XC_BUILDALL,    0x00);
99     av_delete(xc, MOUSE_XC_ATTRALL,     0x00);
100
101     SvREFCNT_inc_simple_void_NN(linearized_isa);
102     sv_2mortal((SV*)linearized_isa);
103
104     /* update */
105
106     if(predicate_calls(metaclass, "is_immutable")){
107         flags |= MOUSEf_XC_IS_IMMUTABLE;
108     }
109
110     if(predicate_calls(metaclass, "is_anon_class")){
111         flags |= MOUSEf_XC_IS_ANON;
112     }
113
114     if(mouse_class_has_custom_buildargs(aTHX_ stash)){
115         flags |= MOUSEf_XC_HAS_BUILDARGS;
116     }
117
118     av_store(xc, MOUSE_XC_FLAGS,       newSVuv(flags));
119     av_store(xc, MOUSE_XC_ATTRALL,     (SV*)attrall);
120     av_store(xc, MOUSE_XC_BUILDALL,    (SV*)buildall);
121     av_store(xc, MOUSE_XC_DEMOLISHALL, (SV*)demolishall);
122
123     for(i = 0; i < len; i++){
124         SV* const klass = MOUSE_av_at(linearized_isa, i);
125         SV* meta;
126         GV* gv;
127
128         gv = stash_fetchs(stash, "BUILD", FALSE);
129         if(gv && GvCVu(gv)){
130             av_push(buildall, newRV_inc((SV*)GvCV(gv)));
131         }
132
133         gv = stash_fetchs(stash, "DEMOLISH", FALSE);
134         if(gv && GvCVu(gv)){
135             av_push(demolishall, newRV_inc((SV*)GvCV(gv)));
136         }
137
138         /* ATTRIBUTES */
139         meta = get_metaclass(klass);
140         if(!SvOK(meta)){
141             continue; /* skip non-Mouse classes */
142         }
143
144         mouse_class_push_attribute_list(aTHX_ meta, attrall, seen);
145     }
146
147     FREETMPS;
148     LEAVE;
149
150     sv_setuv(MOUSE_xc_gen(xc), mro_get_pkg_gen(stash));
151 }
152
153 AV*
154 mouse_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     }
163
164     mg = mouse_mg_find(aTHX_ SvRV(metaclass), &mouse_xc_vtbl, 0x00);
165     if(!mg){
166         SV* const package = get_slot(metaclass, mouse_package);
167         STRLEN len;
168         const char* const pv = SvPV_const(package, len);
169
170         stash = gv_stashpvn(pv, len, TRUE);
171         xc    = newAV();
172
173         mg = sv_magicext(SvRV(metaclass), (SV*)xc, PERL_MAGIC_ext, &mouse_xc_vtbl, pv, len);
174         SvREFCNT_dec(xc); /* refcnt++ in sv_magicext */
175
176         av_extend(xc, MOUSE_XC_last - 1);
177
178         av_store(xc, MOUSE_XC_GEN, newSViv(0));
179         av_store(xc, MOUSE_XC_STASH, (SV*)stash);
180
181         SvREFCNT_inc_simple_void_NN(stash);
182     }
183     else{
184         xc    = (AV*)MOUSE_mg_obj(mg);
185
186         assert(xc);
187         assert(SvTYPE(xc) == SVt_PVAV);
188     }
189
190     gen   = MOUSE_xc_gen(xc);
191     stash = MOUSE_xc_stash(xc);
192
193     if(SvUV(gen) != mro_get_pkg_gen(stash)){
194         mouse_class_update_xc(aTHX_ metaclass, stash, xc);
195     }
196
197     return xc;
198 }
199
200 HV*
201 mouse_build_args(pTHX_ SV* metaclass, SV* const klass, I32 const start, I32 const items, I32 const ax) {
202     HV* args;
203     if((items - start) == 1){
204         SV* const args_ref = ST(start);
205         if(!IsHashRef(args_ref)){
206             if(!metaclass){ metaclass = get_metaclass(klass); }
207             mouse_throw_error(metaclass, NULL, "Single parameters to new() must be a HASH ref");
208         }
209         args = newHVhv((HV*)SvRV(args_ref));
210         sv_2mortal((SV*)args);
211     }
212     else{
213         I32 i;
214
215         args = newHV_mortal();
216
217         if( ((items - start) % 2) != 0 ){
218             if(!metaclass){ metaclass = get_metaclass(klass); }
219             mouse_throw_error(metaclass, NULL, "Odd number of parameters to new()");
220         }
221
222         for(i = start; i < items; i += 2){
223             (void)hv_store_ent(args, ST(i), newSVsv(ST(i+1)), 0U);
224         }
225
226     }
227     return args;
228 }
229
230 void
231 mouse_class_initialize_object(pTHX_ SV* const meta, SV* const object, HV* const args, bool const ignore_triggers) {
232     AV* const xc    = mouse_get_xc(aTHX_ meta);
233     AV* const attrs = MOUSE_xc_attrall(xc);
234     I32 len         = AvFILLp(attrs) + 1;
235     I32 i;
236     AV* triggers_queue = NULL;
237
238     ENTER;
239     SAVETMPS;
240
241     if(!ignore_triggers){
242         triggers_queue = newAV_mortal();
243     }
244
245     for(i = 0; i < len; i++){
246         SV* const attr = AvARRAY(attrs)[i];
247         AV* const xa   = mouse_get_xa(aTHX_ AvARRAY(attrs)[i]);
248
249         SV* const slot     = MOUSE_xa_slot(xa);
250         U16 const flags    = (U16)MOUSE_xa_flags(xa);
251         SV* const init_arg = MOUSE_xa_init_arg(xa);
252         HE* he;
253
254         if(SvOK(init_arg) && ( he = hv_fetch_ent(args, init_arg, FALSE, 0U) ) ){
255             SV* value = HeVAL(he);
256             if(flags & MOUSEf_ATTR_HAS_TC){
257                 value = mouse_xa_apply_type_constraint(aTHX_ xa, value, flags);
258             }
259             set_slot(object, slot, value);
260             if(SvROK(value) && flags & MOUSEf_ATTR_IS_WEAK_REF){
261                 weaken_slot(object, slot);
262             }
263             if(flags & MOUSEf_ATTR_HAS_TRIGGER && triggers_queue){
264                 AV* const pair = newAV();
265                 av_push(pair, newSVsv( mcall0s(attr, "trigger") ));
266                 av_push(pair, newSVsv(value));
267
268                 av_push(triggers_queue, (SV*)pair);
269             }
270         }
271         else { /* no init arg */
272             if(flags & (MOUSEf_ATTR_HAS_DEFAULT | MOUSEf_ATTR_HAS_BUILDER)){
273                 if(!(flags & MOUSEf_ATTR_IS_LAZY)){
274                     mouse_xa_set_default(aTHX_ xa, object);
275                 }
276             }
277             else if(flags & MOUSEf_ATTR_IS_REQUIRED) {
278                 mouse_throw_error(attr, NULL, "Attribute (%"SVf") is required", slot);
279             }
280         }
281     } /* for each attributes */
282
283     if(triggers_queue){
284         len = AvFILLp(triggers_queue) + 1;
285         for(i = 0; i < len; i++){
286             AV* const pair    = (AV*)AvARRAY(triggers_queue)[i];
287             SV* const trigger = AvARRAY(pair)[0];
288             SV* const value   = AvARRAY(pair)[1];
289
290             mcall1(object, trigger, value);
291         }
292     }
293
294     if(MOUSE_xc_flags(xc) & MOUSEf_XC_IS_ANON){
295         set_slot(object, newSVpvs_flags("__ANON__", SVs_TEMP), meta);
296     }
297
298     FREETMPS;
299     LEAVE;
300 }
301
302 MODULE = Mouse  PACKAGE = Mouse
303
304 PROTOTYPES: DISABLE
305
306 BOOT:
307     mouse_package   = newSVpvs_share("package");
308     mouse_namespace = newSVpvs_share("namespace");
309     mouse_methods   = newSVpvs_share("methods");
310     mouse_name      = newSVpvs_share("name");
311
312     mouse_get_attribute      = newSVpvs_share("get_attribute");
313     mouse_get_attribute_list = newSVpvs_share("get_attribute_list");
314
315     MOUSE_CALL_BOOT(Mouse__Util);
316     MOUSE_CALL_BOOT(Mouse__Util__TypeConstraints);
317     MOUSE_CALL_BOOT(Mouse__Meta__Method__Accessor__XS);
318     MOUSE_CALL_BOOT(Mouse__Meta__Attribute);
319
320
321 MODULE = Mouse  PACKAGE = Mouse::Meta::Module
322
323 BOOT:
324     INSTALL_SIMPLE_READER_WITH_KEY(Module, name, package);
325     INSTALL_SIMPLE_READER_WITH_KEY(Module, _method_map, methods);
326     INSTALL_SIMPLE_READER_WITH_KEY(Module, _attribute_map, attributes);
327
328 HV*
329 namespace(SV* self)
330 CODE:
331 {
332     SV* const package = get_slot(self, mouse_package);
333     if(!(package && SvOK(package))){
334         croak("No package name defined");
335     }
336     RETVAL = gv_stashsv(package, GV_ADDMULTI);
337 }
338 OUTPUT:
339     RETVAL
340
341 # ignore extra arguments for extensibility
342 void
343 add_method(SV* self, SV* name, SV* code, ...)
344 CODE:
345 {
346     SV* const package = get_slot(self, mouse_package); /* $self->{package} */
347     SV* const methods = get_slot(self, mouse_methods); /* $self->{methods} */
348     GV* gv;
349     SV* code_ref;
350
351     if(!(package && SvOK(package))){
352         croak("No package name defined");
353     }
354
355     SvGETMAGIC(name);
356     SvGETMAGIC(code);
357
358     if(!SvOK(name)){
359         mouse_throw_error(self, NULL, "You must define a method name");
360     }
361     if(!SvROK(code)){
362         mouse_throw_error(self, NULL, "You must define a CODE reference");
363     }
364
365     code_ref = code;
366     if(SvTYPE(SvRV(code_ref)) != SVt_PVCV){
367         SV*  sv = code_ref;  /* used in tryAMAGICunDEREF */
368         SV** sp = &sv;       /* used in tryAMAGICunDEREF */
369         tryAMAGICunDEREF(to_cv); /* try \&{$code} */
370         if(SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV){
371             mouse_throw_error(self, NULL, "Not a CODE reference");
372         }
373         code_ref = sv;
374     }
375
376     /*  *{$package . '::' . $name} -> *gv */
377     gv = gv_fetchpv(form("%"SVf"::%"SVf, package, name), GV_ADDMULTI, SVt_PVCV);
378     if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
379         SvREFCNT_dec(GvCV(gv));
380         GvCV(gv) = NULL;
381     }
382     sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
383
384     set_slot(methods, name, code); /* $self->{methods}{$name} = $code */
385
386     /* TODO: name the CODE ref if it's anonymous */
387     //code_entity = (CV*)SvRV(code_ref);
388     //if(CvANON(code_entity)
389     //    && CvGV(code_entity) /* a cv under construction has no gv */ ){
390
391     //    CvGV(code_entity) = gv;
392     //    CvANON_off(code_entity);
393     //}
394 }
395
396 MODULE = Mouse  PACKAGE = Mouse::Meta::Class
397
398 BOOT:
399     INSTALL_SIMPLE_READER(Class, roles);
400     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Class, is_anon_class, anon_serial_id);
401
402 void
403 linearized_isa(SV* self)
404 PPCODE:
405 {
406     SV* const stash_ref = mcall0(self, mouse_namespace); /* $self->namespace */
407     AV* linearized_isa;
408     I32 len;
409     I32 i;
410     if(!(SvROK(stash_ref) && SvTYPE(SvRV(stash_ref)) == SVt_PVHV)){
411         croak("namespace() didn't return a HASH reference");
412     }
413     linearized_isa = mro_get_linear_isa((HV*)SvRV(stash_ref));
414     len = AvFILLp(linearized_isa) + 1;
415     EXTEND(SP, len);
416     for(i = 0; i < len; i++){
417         PUSHs(AvARRAY(linearized_isa)[i]);
418     }
419 }
420
421 void
422 get_all_attributes(SV* self)
423 PPCODE:
424 {
425     AV* const xc        = mouse_get_xc(aTHX_ self);
426     AV* const all_attrs =  MOUSE_xc_attrall(xc);
427     I32 const len       = AvFILLp(all_attrs) + 1;
428     I32 i;
429
430     EXTEND(SP, len);
431     for(i = 0; i < len; i++){
432         PUSHs( MOUSE_av_at(all_attrs, i) );
433     }
434 }
435
436 SV*
437 new_object_(SV* meta, ...)
438 CODE:
439 {
440     HV* const args = mouse_build_args(aTHX_ meta, NULL, 1, items, ax);
441     AV* const xc   = mouse_get_xc(aTHX_ meta);
442
443     RETVAL = mouse_instance_create(aTHX_ MOUSE_xc_stash(xc));
444     mouse_class_initialize_object(aTHX_ meta, RETVAL, args, FALSE);
445 }
446
447
448 void
449 _initialize_object(SV* meta, SV* object, HV* args, bool ignore_triggers = FALSE)
450 CODE:
451 {
452     mouse_class_initialize_object(aTHX_ meta, object, args, ignore_triggers);
453 }
454
455 MODULE = Mouse  PACKAGE = Mouse::Meta::Role
456
457 BOOT:
458     INSTALL_SIMPLE_READER_WITH_KEY(Role, get_roles, roles);
459     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Role, is_anon_role, anon_serial_id);
460
461 MODULE = Mouse  PACKAGE = Mouse::Object
462
463 HV*
464 BUILDARGS(SV* klass, ...)
465 CODE:
466 {
467     RETVAL = mouse_build_args(aTHX_ NULL, klass, 1, items, ax);
468 }
469 OUTPUT:
470     RETVAL