Commit | Line | Data |
7d96ae4d |
1 | /* |
2 | * full definition of built-in type constraints (ware in Moose::Util::TypeConstraints::OptimizedConstraints) |
3 | */ |
4 | |
5 | #include "mouse.h" |
6 | |
7d96ae4d |
7 | #ifndef SvRXOK |
8 | #define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr)) |
9 | #endif |
10 | |
d33d8840 |
11 | typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv); |
12 | |
85a74fcf |
13 | /* |
14 | NOTE: mouse_tc_check() handles GETMAGIC |
15 | */ |
1d5ecd5f |
16 | int |
17 | mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) { |
28ba5a26 |
18 | CV* const cv = (CV*)SvRV(tc_code); |
7ca3334f |
19 | assert(SvTYPE(cv) == SVt_PVCV); |
d33d8840 |
20 | |
19462c57 |
21 | if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */ |
7ca3334f |
22 | MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr; |
d33d8840 |
23 | |
7ca3334f |
24 | assert(CvXSUBANY(cv).any_ptr != NULL); |
19462c57 |
25 | assert(mg->mg_ptr != NULL); |
28ba5a26 |
26 | |
a901dc2e |
27 | SvGETMAGIC(sv); |
7ca3334f |
28 | /* call the check function directly, skipping call_sv() */ |
29 | return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv); |
1d5ecd5f |
30 | } |
7ca3334f |
31 | else { /* custom */ |
1d5ecd5f |
32 | int ok; |
33 | dSP; |
34 | |
35 | ENTER; |
36 | SAVETMPS; |
37 | |
38 | PUSHMARK(SP); |
39 | XPUSHs(sv); |
40 | PUTBACK; |
41 | |
42 | call_sv(tc_code, G_SCALAR); |
43 | |
44 | SPAGAIN; |
6ad77996 |
45 | ok = sv_true(POPs); |
1d5ecd5f |
46 | PUTBACK; |
47 | |
48 | FREETMPS; |
49 | LEAVE; |
50 | |
51 | return ok; |
52 | } |
53 | } |
7d96ae4d |
54 | |
7d96ae4d |
55 | /* |
56 | The following type check functions return an integer, not a bool, to keep them simple, |
57 | so if you assign these return value to bool variable, you must use "expr ? TRUE : FALSE". |
58 | */ |
59 | |
60 | int |
19462c57 |
61 | mouse_tc_Any(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv PERL_UNUSED_DECL) { |
7d96ae4d |
62 | assert(sv); |
63 | return TRUE; |
64 | } |
65 | |
66 | int |
19462c57 |
67 | mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
68 | assert(sv); |
f6e3b8a9 |
69 | |
6ad77996 |
70 | if(sv_true(sv)){ |
7d96ae4d |
71 | if(SvIOKp(sv)){ |
f6e3b8a9 |
72 | return SvIVX(sv) == 1; |
7d96ae4d |
73 | } |
74 | else if(SvNOKp(sv)){ |
f6e3b8a9 |
75 | return SvNVX(sv) == 1.0; |
7d96ae4d |
76 | } |
f6e3b8a9 |
77 | else if(SvPOKp(sv)){ /* "1" */ |
78 | return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1'; |
7d96ae4d |
79 | } |
80 | else{ |
81 | return FALSE; |
82 | } |
83 | } |
84 | else{ |
90232975 |
85 | /* any false value must be boolean */ |
7d96ae4d |
86 | return TRUE; |
87 | } |
88 | } |
89 | |
90 | int |
19462c57 |
91 | mouse_tc_Undef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
92 | assert(sv); |
93 | return !SvOK(sv); |
94 | } |
95 | |
96 | int |
19462c57 |
97 | mouse_tc_Defined(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
98 | assert(sv); |
99 | return SvOK(sv); |
100 | } |
101 | |
102 | int |
19462c57 |
103 | mouse_tc_Value(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
104 | assert(sv); |
105 | return SvOK(sv) && !SvROK(sv); |
106 | } |
107 | |
108 | int |
19462c57 |
109 | mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
110 | assert(sv); |
111 | return LooksLikeNumber(sv); |
112 | } |
113 | |
678d1902 |
114 | static int |
0cfe08a4 |
115 | S_nv_is_integer(pTHX_ NV const nv) { |
116 | if(nv == (NV)(IV)nv){ |
117 | return TRUE; |
118 | } |
119 | else { |
120 | char buf[64]; /* Must fit sprintf/Gconvert of longest NV */ |
121 | char* p; |
f3e4c7eb |
122 | (void)Gconvert(nv, NV_DIG, 0, buf); |
0cfe08a4 |
123 | p = &buf[0]; |
124 | |
125 | /* -?[0-9]+ */ |
126 | if(*p == '-') p++; |
127 | |
128 | while(*p){ |
129 | if(!isDIGIT(*p)){ |
130 | return FALSE; |
131 | } |
132 | p++; |
133 | } |
134 | return TRUE; |
135 | } |
136 | } |
137 | |
138 | int |
19462c57 |
139 | mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
140 | assert(sv); |
141 | if(SvIOKp(sv)){ |
142 | return TRUE; |
143 | } |
9ab5eba8 |
144 | else if(SvNOKp(sv)) { |
0cfe08a4 |
145 | return S_nv_is_integer(aTHX_ SvNVX(sv)); |
7d96ae4d |
146 | } |
147 | else if(SvPOKp(sv)){ |
148 | int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL); |
149 | if(num_type){ |
150 | return !(num_type & IS_NUMBER_NOT_INT); |
151 | } |
152 | } |
153 | return FALSE; |
154 | } |
155 | |
156 | int |
19462c57 |
157 | mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
158 | assert(sv); |
159 | return SvOK(sv) && !SvROK(sv) && !isGV(sv); |
160 | } |
161 | |
162 | int |
19462c57 |
163 | mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){ |
7d96ae4d |
164 | assert(sv); |
165 | return is_class_loaded(sv); |
166 | } |
167 | |
168 | int |
19462c57 |
169 | mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
170 | assert(sv); |
171 | if(is_class_loaded(sv)){ |
172 | int ok; |
7d96ae4d |
173 | |
174 | ENTER; |
175 | SAVETMPS; |
176 | |
aa2d2e2c |
177 | ok = is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv)); |
7d96ae4d |
178 | |
179 | FREETMPS; |
180 | LEAVE; |
181 | |
182 | return ok; |
183 | } |
184 | return FALSE; |
185 | } |
186 | |
187 | int |
19462c57 |
188 | mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
189 | assert(sv); |
190 | return SvROK(sv); |
191 | } |
192 | |
193 | int |
bcf612a8 |
194 | mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) { |
7d96ae4d |
195 | assert(sv); |
bcf612a8 |
196 | if(SvROK(sv)){ |
197 | sv = SvRV(sv); |
198 | return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv)); |
199 | } |
200 | return FALSE; |
7d96ae4d |
201 | } |
202 | |
203 | int |
19462c57 |
204 | mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
205 | assert(sv); |
80aa5731 |
206 | return IsArrayRef(sv); |
7d96ae4d |
207 | } |
208 | |
209 | int |
19462c57 |
210 | mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
211 | assert(sv); |
80aa5731 |
212 | return IsHashRef(sv); |
7d96ae4d |
213 | } |
214 | |
215 | int |
19462c57 |
216 | mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
217 | assert(sv); |
80aa5731 |
218 | return IsCodeRef(sv); |
7d96ae4d |
219 | } |
220 | |
221 | int |
19462c57 |
222 | mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
223 | assert(sv); |
224 | return SvRXOK(sv); |
225 | } |
226 | |
227 | int |
19462c57 |
228 | mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
229 | assert(sv); |
230 | return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv)); |
231 | } |
232 | |
233 | int |
19462c57 |
234 | mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
235 | GV* gv; |
236 | assert(sv); |
237 | |
238 | /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */ |
239 | |
240 | gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv); |
241 | if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){ |
242 | IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv; |
243 | |
244 | if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){ |
245 | return TRUE; |
246 | } |
247 | } |
248 | |
016e4681 |
249 | return is_an_instance_of("IO::Handle", sv); |
7d96ae4d |
250 | } |
251 | |
252 | int |
19462c57 |
253 | mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { |
7d96ae4d |
254 | assert(sv); |
255 | return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv); |
256 | } |
257 | |
619338ac |
258 | /* Parameterized type constraints */ |
259 | |
f790c46b |
260 | static int |
619338ac |
261 | mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) { |
80aa5731 |
262 | if(IsArrayRef(sv)){ |
619338ac |
263 | AV* const av = (AV*)SvRV(sv); |
264 | I32 const len = av_len(av) + 1; |
265 | I32 i; |
266 | for(i = 0; i < len; i++){ |
267 | SV* const value = *av_fetch(av, i, TRUE); |
619338ac |
268 | if(!mouse_tc_check(aTHX_ param, value)){ |
269 | return FALSE; |
270 | } |
271 | } |
272 | return TRUE; |
273 | } |
274 | return FALSE; |
275 | } |
276 | |
f790c46b |
277 | static int |
619338ac |
278 | mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) { |
19462c57 |
279 | if(mouse_tc_HashRef(aTHX_ NULL, sv)){ |
619338ac |
280 | HV* const hv = (HV*)SvRV(sv); |
281 | HE* he; |
282 | |
283 | hv_iterinit(hv); |
284 | while((he = hv_iternext(hv))){ |
285 | SV* const value = hv_iterval(hv, he); |
619338ac |
286 | if(!mouse_tc_check(aTHX_ param, value)){ |
a1cf0531 |
287 | hv_iterinit(hv); /* reset */ |
619338ac |
288 | return FALSE; |
289 | } |
290 | } |
291 | return TRUE; |
292 | } |
293 | return FALSE; |
294 | } |
295 | |
f790c46b |
296 | static int |
619338ac |
297 | mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) { |
298 | if(SvOK(sv)){ |
299 | return mouse_tc_check(aTHX_ param, sv); |
300 | } |
301 | return TRUE; |
302 | } |
303 | |
f790c46b |
304 | static int |
305 | mouse_types_union_check(pTHX_ AV* const types, SV* const sv) { |
306 | I32 const len = AvFILLp(types) + 1; |
307 | I32 i; |
308 | |
309 | for(i = 0; i < len; i++){ |
310 | if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){ |
311 | return TRUE; |
312 | } |
313 | } |
314 | |
315 | return FALSE; |
316 | } |
317 | |
318 | static int |
319 | mouse_types_check(pTHX_ AV* const types, SV* const sv) { |
320 | I32 const len = AvFILLp(types) + 1; |
321 | I32 i; |
322 | |
323 | ENTER; |
324 | SAVE_DEFSV; |
325 | DEFSV_set(sv); |
326 | |
327 | for(i = 0; i < len; i++){ |
328 | if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){ |
329 | LEAVE; |
330 | return FALSE; |
331 | } |
332 | } |
333 | |
334 | LEAVE; |
335 | |
336 | return TRUE; |
337 | } |
338 | |
1d5ecd5f |
339 | /* |
340 | * This class_type generator is taken from Scalar::Util::Instance |
341 | */ |
342 | |
343 | #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION |
344 | typedef struct sui_cxt{ |
345 | GV* universal_isa; |
ebe91068 |
346 | GV* universal_can; |
1d5ecd5f |
347 | } my_cxt_t; |
348 | START_MY_CXT |
349 | |
350 | #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj) |
351 | #define MG_klass_pv(mg) ((mg)->mg_ptr) |
352 | #define MG_klass_len(mg) ((mg)->mg_len) |
353 | |
441964ce |
354 | static const char* |
f48920c1 |
355 | mouse_canonicalize_package_name(const char* name){ |
1d5ecd5f |
356 | |
357 | /* "::Foo" -> "Foo" */ |
358 | if(name[0] == ':' && name[1] == ':'){ |
359 | name += 2; |
360 | } |
361 | |
362 | /* "main::main::main::Foo" -> "Foo" */ |
363 | while(strnEQ(name, "main::", sizeof("main::")-1)){ |
364 | name += sizeof("main::")-1; |
365 | } |
366 | |
367 | return name; |
368 | } |
369 | |
370 | static int |
441964ce |
371 | mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){ |
1d5ecd5f |
372 | AV* const linearized_isa = mro_get_linear_isa(instance_stash); |
373 | SV** svp = AvARRAY(linearized_isa); |
374 | SV** const end = svp + AvFILLp(linearized_isa) + 1; |
375 | |
376 | while(svp != end){ |
377 | assert(SvPVX(*svp)); |
f48920c1 |
378 | if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){ |
1d5ecd5f |
379 | return TRUE; |
380 | } |
381 | svp++; |
382 | } |
383 | return FALSE; |
384 | } |
385 | |
ce89cb68 |
386 | #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c) |
387 | #define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b)) |
388 | |
fc650569 |
389 | STATIC_INLINE GV* |
ce89cb68 |
390 | mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){ |
391 | GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE); |
392 | if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */ |
393 | return *gvp; |
394 | } |
395 | |
396 | return gv_fetchmeth_autoload(stash, name, namelen, 0); |
397 | } |
398 | |
441964ce |
399 | int |
400 | mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){ |
401 | assert(stash); |
402 | assert(SvTYPE(stash) == SVt_PVHV); |
403 | |
016e4681 |
404 | if(IsObject(instance)){ |
441964ce |
405 | dMY_CXT; |
406 | HV* const instance_stash = SvSTASH(SvRV(instance)); |
28e3abdf |
407 | GV* const myisa = find_method_pvs(instance_stash, "isa"); |
441964ce |
408 | |
409 | /* the instance has no own isa method */ |
28e3abdf |
410 | if(myisa == NULL || GvCV(myisa) == GvCV(MY_CXT.universal_isa)){ |
441964ce |
411 | return stash == instance_stash |
412 | || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash)); |
413 | } |
414 | /* the instance has its own isa method */ |
415 | else { |
28e3abdf |
416 | SV* package; |
417 | int ok; |
1d5ecd5f |
418 | |
441964ce |
419 | ENTER; |
420 | SAVETMPS; |
1d5ecd5f |
421 | |
28e3abdf |
422 | package = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U); |
423 | ok = sv_true(mcall1s(instance, "isa", sv_2mortal(package))); |
1d5ecd5f |
424 | |
441964ce |
425 | FREETMPS; |
426 | LEAVE; |
1d5ecd5f |
427 | |
28e3abdf |
428 | return ok; |
441964ce |
429 | } |
1d5ecd5f |
430 | } |
441964ce |
431 | return FALSE; |
432 | } |
433 | |
434 | static int |
435 | mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){ |
436 | PERL_UNUSED_ARG(data); |
437 | return SvROK(sv) && SvOBJECT(SvRV(sv)); |
1d5ecd5f |
438 | } |
439 | |
ebe91068 |
440 | static int |
441 | mouse_can_methods(pTHX_ AV* const methods, SV* const instance){ |
442 | if(IsObject(instance)){ |
443 | dMY_CXT; |
444 | HV* const mystash = SvSTASH(SvRV(instance)); |
ce89cb68 |
445 | GV* const mycan = find_method_pvs(mystash, "can"); |
446 | bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE; |
ebe91068 |
447 | I32 const len = AvFILLp(methods) + 1; |
448 | I32 i; |
449 | for(i = 0; i < len; i++){ |
450 | SV* const name = MOUSE_av_at(methods, i); |
451 | |
452 | if(use_builtin){ |
ce89cb68 |
453 | if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){ |
ebe91068 |
454 | return FALSE; |
455 | } |
456 | } |
457 | else{ |
458 | bool ok; |
ebe91068 |
459 | |
460 | ENTER; |
461 | SAVETMPS; |
462 | |
28e3abdf |
463 | ok = sv_true(mcall1s(instance, "can", sv_mortalcopy(name))); |
ebe91068 |
464 | |
465 | FREETMPS; |
466 | LEAVE; |
467 | |
468 | if(!ok){ |
469 | return FALSE; |
470 | } |
471 | } |
472 | } |
473 | return TRUE; |
474 | } |
475 | return FALSE; |
476 | } |
477 | |
441964ce |
478 | static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */ |
f48920c1 |
479 | |
619338ac |
480 | static CV* |
19462c57 |
481 | mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) { |
619338ac |
482 | CV* xsub; |
916eb82f |
483 | |
19462c57 |
484 | xsub = newXS(name, XS_Mouse_constraint_check, __FILE__); |
916eb82f |
485 | CvXSUBANY(xsub).any_ptr = sv_magicext( |
486 | (SV*)xsub, |
487 | param, /* mg_obj: refcnt will be increased */ |
488 | PERL_MAGIC_ext, |
489 | &mouse_util_type_constraints_vtbl, |
1b364415 |
490 | (char*)fptr, /* mg_ptr */ |
916eb82f |
491 | 0 /* mg_len: 0 for static data */ |
492 | ); |
493 | |
494 | if(!name){ |
495 | sv_2mortal((SV*)xsub); |
496 | } |
497 | |
498 | return xsub; |
499 | } |
500 | |
501 | CV* |
672bde7c |
502 | mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){ |
441964ce |
503 | STRLEN klass_len; |
504 | const char* klass_pv = SvPV_const(klass, klass_len); |
916eb82f |
505 | SV* param; |
1b364415 |
506 | check_fptr_t fptr; |
1d5ecd5f |
507 | |
441964ce |
508 | klass_pv = mouse_canonicalize_package_name(klass_pv); |
1d5ecd5f |
509 | |
441964ce |
510 | if(strNE(klass_pv, "UNIVERSAL")){ |
916eb82f |
511 | param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD); |
1b364415 |
512 | fptr = (check_fptr_t)mouse_is_an_instance_of; |
441964ce |
513 | |
514 | } |
515 | else{ |
916eb82f |
516 | param = NULL; |
1b364415 |
517 | fptr = (check_fptr_t)mouse_is_an_instance_of_universal; |
441964ce |
518 | } |
519 | |
19462c57 |
520 | return mouse_tc_generate(aTHX_ predicate_name, fptr, param); |
441964ce |
521 | } |
f48920c1 |
522 | |
ebe91068 |
523 | CV* |
524 | mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){ |
525 | AV* av; |
526 | AV* const param = newAV_mortal(); |
527 | I32 len; |
528 | I32 i; |
529 | |
d06d9266 |
530 | must_ref(methods, "an ARRAY ref for method names", SVt_PVAV); |
ebe91068 |
531 | av = (AV*)SvRV(methods); |
532 | |
533 | len = av_len(av) + 1; |
534 | for(i = 0; i < len; i++){ |
535 | SV* const name = *av_fetch(av, i, TRUE); |
536 | STRLEN pvlen; |
537 | const char* const pv = SvPV_const(name, pvlen); |
538 | |
539 | av_push(param, newSVpvn_share(pv, pvlen, 0U)); |
540 | } |
541 | |
542 | return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param); |
543 | } |
544 | |
545 | |
19462c57 |
546 | XS(XS_Mouse_constraint_check) { |
1d5ecd5f |
547 | dVAR; |
548 | dXSARGS; |
441964ce |
549 | MAGIC* const mg = (MAGIC*)XSANY.any_ptr; |
1d5ecd5f |
550 | |
441964ce |
551 | if(items < 1){ |
19462c57 |
552 | croak("Too few arguments for type constraint check functions"); |
1d5ecd5f |
553 | } |
554 | |
441964ce |
555 | SvGETMAGIC( ST(0) ); |
556 | ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) ); |
1d5ecd5f |
557 | XSRETURN(1); |
558 | } |
559 | |
560 | static void |
561 | setup_my_cxt(pTHX_ pMY_CXT){ |
562 | MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV); |
563 | SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa); |
ebe91068 |
564 | |
565 | MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV); |
566 | SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can); |
1d5ecd5f |
567 | } |
568 | |
19462c57 |
569 | #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL) |
570 | |
1d5ecd5f |
571 | MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Util::TypeConstraints |
572 | |
646c0371 |
573 | PROTOTYPES: DISABLE |
574 | VERSIONCHECK: DISABLE |
1d5ecd5f |
575 | |
576 | BOOT: |
577 | { |
578 | MY_CXT_INIT; |
579 | setup_my_cxt(aTHX_ aMY_CXT); |
19462c57 |
580 | |
581 | /* setup built-in type constraints */ |
582 | DEFINE_TC(Any); |
583 | DEFINE_TC(Undef); |
584 | DEFINE_TC(Defined); |
585 | DEFINE_TC(Bool); |
586 | DEFINE_TC(Value); |
587 | DEFINE_TC(Ref); |
588 | DEFINE_TC(Str); |
589 | DEFINE_TC(Num); |
590 | DEFINE_TC(Int); |
591 | DEFINE_TC(ScalarRef); |
592 | DEFINE_TC(ArrayRef); |
593 | DEFINE_TC(HashRef); |
594 | DEFINE_TC(CodeRef); |
595 | DEFINE_TC(GlobRef); |
596 | DEFINE_TC(FileHandle); |
597 | DEFINE_TC(RegexpRef); |
598 | DEFINE_TC(Object); |
599 | DEFINE_TC(ClassName); |
600 | DEFINE_TC(RoleName); |
1d5ecd5f |
601 | } |
602 | |
603 | #ifdef USE_ITHREADS |
604 | |
605 | void |
606 | CLONE(...) |
607 | CODE: |
608 | { |
609 | MY_CXT_CLONE; |
610 | setup_my_cxt(aTHX_ aMY_CXT); |
611 | PERL_UNUSED_VAR(items); |
612 | } |
613 | |
614 | #endif /* !USE_ITHREADS */ |
615 | |
19462c57 |
616 | #define MOUSE_TC_MAYBE 0 |
617 | #define MOUSE_TC_ARRAY_REF 1 |
618 | #define MOUSE_TC_HASH_REF 2 |
1d5ecd5f |
619 | |
619338ac |
620 | CV* |
621 | _parameterize_ArrayRef_for(SV* param) |
622 | ALIAS: |
623 | _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF |
624 | _parameterize_HashRef_for = MOUSE_TC_HASH_REF |
625 | _parameterize_Maybe_for = MOUSE_TC_MAYBE |
626 | CODE: |
627 | { |
628 | check_fptr_t fptr; |
629 | SV* const tc_code = mcall0s(param, "_compiled_type_constraint"); |
80aa5731 |
630 | if(!IsCodeRef(tc_code)){ |
619338ac |
631 | croak("_compiled_type_constraint didn't return a CODE reference"); |
632 | } |
633 | |
634 | switch(ix){ |
635 | case MOUSE_TC_ARRAY_REF: |
636 | fptr = mouse_parameterized_ArrayRef; |
637 | break; |
638 | case MOUSE_TC_HASH_REF: |
639 | fptr = mouse_parameterized_HashRef; |
640 | break; |
641 | default: /* Maybe type */ |
642 | fptr = mouse_parameterized_Maybe; |
643 | } |
19462c57 |
644 | RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code); |
619338ac |
645 | } |
646 | OUTPUT: |
647 | RETVAL |
648 | |
f790c46b |
649 | MODULE = Mouse::Util::TypeConstraints PACKAGE = Mouse::Meta::TypeConstraint |
650 | |
a4b901ac |
651 | BOOT: |
652 | INSTALL_SIMPLE_READER(TypeConstraint, name); |
653 | INSTALL_SIMPLE_READER(TypeConstraint, parent); |
654 | INSTALL_SIMPLE_READER(TypeConstraint, message); |
655 | |
fc83f4cf |
656 | INSTALL_SIMPLE_READER(TypeConstraint, type_parameter); |
657 | |
a4b901ac |
658 | INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint); |
659 | INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */ |
660 | |
661 | INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion); |
fc83f4cf |
662 | INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */ |
a4b901ac |
663 | |
f790c46b |
664 | void |
665 | compile_type_constraint(SV* self) |
666 | CODE: |
667 | { |
6fab29c6 |
668 | AV* const checks = newAV_mortal(); |
f790c46b |
669 | SV* check; /* check function */ |
670 | SV* parent; |
671 | SV* types_ref; |
672 | |
f790c46b |
673 | for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){ |
674 | check = get_slots(parent, "hand_optimized_type_constraint"); |
675 | if(check && SvOK(check)){ |
19462c57 |
676 | if(!mouse_tc_CodeRef(aTHX_ NULL, check)){ |
f790c46b |
677 | croak("Not a CODE reference"); |
678 | } |
679 | av_unshift(checks, 1); |
680 | av_store(checks, 0, newSVsv(check)); |
681 | break; /* a hand optimized constraint must include all the parent */ |
682 | } |
683 | |
684 | check = get_slots(parent, "constraint"); |
685 | if(check && SvOK(check)){ |
19462c57 |
686 | if(!mouse_tc_CodeRef(aTHX_ NULL, check)){ |
f790c46b |
687 | croak("Not a CODE reference"); |
688 | } |
689 | av_unshift(checks, 1); |
690 | av_store(checks, 0, newSVsv(check)); |
691 | } |
692 | } |
693 | |
694 | check = get_slots(self, "constraint"); |
695 | if(check && SvOK(check)){ |
19462c57 |
696 | if(!mouse_tc_CodeRef(aTHX_ NULL, check)){ |
f790c46b |
697 | croak("Not a CODE reference"); |
698 | } |
699 | av_push(checks, newSVsv(check)); |
700 | } |
701 | |
702 | types_ref = get_slots(self, "type_constraints"); |
703 | if(types_ref && SvOK(types_ref)){ /* union type */ |
704 | AV* types; |
705 | AV* union_checks; |
706 | CV* union_check; |
707 | I32 len; |
708 | I32 i; |
709 | |
80aa5731 |
710 | if(!IsArrayRef(types_ref)){ |
f790c46b |
711 | croak("Not an ARRAY reference"); |
712 | } |
713 | types = (AV*)SvRV(types_ref); |
714 | len = av_len(types) + 1; |
715 | |
6fab29c6 |
716 | union_checks = newAV_mortal(); |
f790c46b |
717 | |
718 | for(i = 0; i < len; i++){ |
719 | SV* const tc = *av_fetch(types, i, TRUE); |
720 | SV* const c = get_slots(tc, "compiled_type_constraint"); |
19462c57 |
721 | if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){ |
c7576321 |
722 | mouse_throw_error(self, c, "'%"SVf"' has no compiled type constraint", self); |
f790c46b |
723 | } |
724 | av_push(union_checks, newSVsv(c)); |
725 | } |
726 | |
19462c57 |
727 | union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks); |
f790c46b |
728 | av_push(checks, newRV_inc((SV*)union_check)); |
729 | } |
730 | |
731 | if(AvFILLp(checks) < 0){ |
732 | check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE)); |
733 | } |
734 | else{ |
19462c57 |
735 | check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks)); |
f790c46b |
736 | } |
ca8e67d6 |
737 | (void)set_slots(self, "compiled_type_constraint", check); |
f790c46b |
738 | } |
739 | |
c7576321 |
740 | bool |
741 | check(SV* self, SV* sv) |
742 | CODE: |
743 | { |
744 | SV* const check = get_slots(self, "compiled_type_constraint"); |
745 | if(!(check && mouse_tc_CodeRef(aTHX_ NULL, check))){ |
746 | mouse_throw_error(self, check, "'%"SVf"' has no compiled type constraint", self); |
747 | } |
748 | RETVAL = mouse_tc_check(aTHX_ check, sv) ? TRUE : FALSE; |
749 | } |
750 | OUTPUT: |
751 | RETVAL |
752 | |