-
Notifications
You must be signed in to change notification settings - Fork 135
/
role.pmc
671 lines (473 loc) · 18.3 KB
/
role.pmc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*
Copyright (C) 2001-2007, The Perl Foundation.
$Id$
=head1 NAME
src/pmc/role.pmc - Role - defines a role
=head1 DESCRIPTION
This class implements the Role PMC, a unit of class composition as outlined in
F<docs/pdds/pdd15_objects.pod>.
Role is not derived from any other PMC.
=head2 Structure
The Role PMC structure (C<Parrot_Role>) consists of five items:
=over 4
=item C<name>
The name of the role -- a STRING.
An empty STRING is allocated during initialization.
=item C<namespace>
The namespace the role is associated with, if any.
A Null PMC is allocated during initialization.
=item C<roles>
The list of roles from which this role is composed, if any.
An empty ResizablePMCArray is allocated during initialization.
=item C<methods>
The directory of method names and methods this role implements.
An empty Hash PMC is allocated during initialization.
=item C<attrib_metadata>
The directory of attribute names and attribute metadata this role contains.
An empty Hash PMC is allocated during initialization.
=cut
*/
#include "parrot/parrot.h"
#define PARROT_ROLE(o) ((Parrot_Role *) PMC_data(o))
/* This is the underlying structure of this PMC. */
typedef struct Parrot_Role {
STRING *name; /* The name of the role. */
PMC *namespace; /* The namespace it's linked to, if any. */
PMC *roles; /* Any roles that this role is composed from. */
PMC *methods; /* Hash of method names to methods in this role. */
PMC *attrib_metadata; /* Hash of attributes in this role to hashes of metadata. */
} Parrot_Role;
/* Takes a hash and initializes the role based on it. */
static void init_role_from_hash(Parrot_Interp interp, PMC *self, PMC *info)
{
Parrot_Role *role = PARROT_ROLE(self);
int have_name, have_ns;
PMC *old_ns;
int i;
/* Ensure we actually have some initialization info. */
if (PMC_IS_NULL(info))
return;
/* Check if we have a name and/or a namespace. */
have_name = VTABLE_exists_keyed_str(interp, info,
string_from_const_cstring(interp, "name", 0));
have_ns = VTABLE_exists_keyed_str(interp, info,
string_from_const_cstring(interp, "namespace", 0));
/* Take a copy of the current namespace the role is attached to. */
old_ns = role->namespace;
/* Let's roll (no pun intended!) If we have a namespace and a name,
* set both. */
if (have_name && have_ns) {
/* If we weren't passed a NameSpace PMC, assume it's something we have
* to look one up with and do so. */
PMC *namespace = VTABLE_get_pmc_keyed_str(interp, info,
string_from_const_cstring(interp, "namespace", 0));
if (!VTABLE_isa(interp, namespace, string_from_const_cstring(interp, "NameSpace", 0)))
namespace = Parrot_make_namespace_autobase(interp, namespace);
/* If we get something null back it's an error; otherwise, store it. */
if (!PMC_IS_NULL(namespace))
role->namespace = namespace;
else
real_exception(interp, NULL, E_NameError, "Namespace not found");
/* Set a (string) name. */
role->name = VTABLE_get_string_keyed_str(interp, info,
string_from_const_cstring(interp, "name", 0));
}
/* Otherwise, we may just have a name. */
else if (have_name) {
/* Set the name. */
role->name = VTABLE_get_string_keyed_str(interp, info,
string_from_const_cstring(interp, "name", 0));
/* Namespace is nested in the current namespace and with the name of
* the role. */
role->namespace = Parrot_make_namespace_keyed_str(interp,
CONTEXT(interp->ctx)->current_namespace, role->name);
}
/* Otherwise, we may just have a namespace. */
else if (have_ns) {
/* If we weren't passed a NameSpace PMC, assume it's something we have
* to look one up with and do so. */
PMC *namespace = VTABLE_get_pmc_keyed_str(interp, info,
string_from_const_cstring(interp, "namespace", 0));
if (!VTABLE_isa(interp, namespace, string_from_const_cstring(interp, "NameSpace", 0)))
namespace = Parrot_make_namespace_autobase(interp, namespace);
/* If we get something null back it's an error; otherwise, store it. */
if (!PMC_IS_NULL(namespace))
role->namespace = namespace;
else
real_exception(interp, NULL, E_NameError, "Namespace not found");
/* Name is that of the most nested part of the namespace. */
role->name = VTABLE_get_string(interp, namespace);
}
/* If we were attached to a namespce and are now attached to a new one,
* need to unset ourselves in the old namespace. */
if (!PMC_IS_NULL(old_ns) && role->namespace != old_ns)
Parrot_PCCINVOKE(interp, old_ns,
string_from_const_cstring(interp, "set_class", 0),
"P->", PMCNULL);
/* Link namespace to this role, if there is one. */
if (!PMC_IS_NULL(role->namespace))
Parrot_PCCINVOKE(interp, role->namespace,
string_from_const_cstring(interp, "set_class", 0),
"P->", self);
/* Initialize roles, if we have any. */
if (VTABLE_exists_keyed_str(interp, info,
string_from_const_cstring(interp, "roles", 0))) {
/* Loop over roles array and compose them. */
PMC *role_list = VTABLE_get_pmc_keyed_str(interp, info,
string_from_const_cstring(interp, "roles", 0));
int role_count = VTABLE_elements(interp, role_list);
for (i = 0; i < role_count; i++) {
PMC *cur_role = VTABLE_get_pmc_keyed_int(interp, role_list, i);
VTABLE_add_role(interp, self, cur_role);
}
}
/* Initialize attributes, if we have any. */
if (VTABLE_exists_keyed_str(interp, info,
string_from_const_cstring(interp, "attributes", 0))) {
/* Loop over attributes array and add them. */
PMC *attrib_name_list = VTABLE_get_pmc_keyed_str(interp, info,
string_from_const_cstring(interp, "attributes", 0));
int attrib_count = VTABLE_elements(interp, attrib_name_list);
for (i = 0; i < attrib_count; i++) {
STRING *attrib_name = VTABLE_get_string_keyed_int(interp,
attrib_name_list, i);
VTABLE_add_attribute(interp, self, attrib_name, PMCNULL);
}
}
/* Initialize methods, if we have any. */
if (VTABLE_exists_keyed_str(interp, info,
string_from_const_cstring(interp, "methods", 0))) {
/* Get the methods hash. */
PMC *methods = VTABLE_get_pmc_keyed_str(interp, info,
string_from_const_cstring(interp, "methods", 0));
/* Iterate over the list of methods. */
PMC *iter = VTABLE_get_iter(interp, methods);
while (VTABLE_get_bool(interp, iter)) {
/* Add the method. */
STRING *method_name = VTABLE_shift_string(interp, iter);
PMC *method_pmc = VTABLE_get_pmc_keyed_str(interp, methods, method_name);
VTABLE_add_method(interp, self, method_name, method_pmc);
}
}
}
/*
=back
=head2 Functions
=over 4
=cut
*/
pmclass Role
need_ext {
/*
=item C<void init()>
Initializes a Role PMC.
=item C<void init_pmc(PMC *init_data)>
Creates a Role and initializes it using the settings from the Hash passed in
C<init_data>.
=cut
*/
void init() {
Parrot_Role *role = NULL;
/* Set flags for custom DOD mark and destroy. */
PObj_custom_mark_SET(SELF);
PObj_active_destroy_SET(SELF);
/* Set up the object. */
role = mem_sys_allocate_zeroed(sizeof (Parrot_Role));
role->name = CONST_STRING(interp, "");
role->namespace = PMCNULL;
role->roles = pmc_new(interp, enum_class_ResizablePMCArray);
role->methods = pmc_new(interp, enum_class_Hash);
role->attrib_metadata = pmc_new(interp, enum_class_Hash);
PMC_data(SELF) = role;
}
void init_pmc(PMC* init_data) {
/* Create the role. */
SELF.init();
/* Initialize the role with the supplied data. */
init_role_from_hash(interp, SELF, init_data);
}
/*
=item C<void destroy()>
Free the memory associated with the object's underlying struct.
=cut
*/
void destroy() {
mem_sys_free(PMC_data(SELF));
}
/*
=item C<void mark()>
Mark referenced strings and PMCs in the structure as live.
=cut
*/
void mark() {
Parrot_Role *role = PARROT_ROLE(SELF);
if (role->name)
pobject_lives(interp, (PObj*)role->name);
if (role->namespace)
pobject_lives(interp, (PObj*)role->namespace);
if (role->roles)
pobject_lives(interp, (PObj*)role->roles);
if (role->methods)
pobject_lives(interp, (PObj*)role->methods);
if (role->attrib_metadata)
pobject_lives(interp, (PObj*)role->attrib_metadata);
}
/*
=item C<void add_attribute(STRING *name, PMC *type)>
Adds the given attribute with an optional type.
Enters the attribute in the C<attributes> array.
=cut
*/
void add_attribute(STRING *name, PMC *type) {
Parrot_Role *role = PARROT_ROLE(SELF);
PMC *new_attribute = pmc_new(interp, enum_class_Hash);
/* Set name and type. */
VTABLE_set_string_keyed_str(interp, new_attribute,
CONST_STRING(interp, "name"), name);
if (!PMC_IS_NULL(type)) {
VTABLE_set_pmc_keyed_str(interp, new_attribute,
CONST_STRING(interp, "type"), type);
}
/* Enter the attribute in the attributes array. */
VTABLE_set_pmc_keyed_str(interp, role->attrib_metadata, name, new_attribute);
}
/*
=item C<void add_method(STRING *name, PMC *sub)>
Adds the given sub PMC as a method with the given name.
=cut
*/
void add_method(STRING *name, PMC *sub) {
Parrot_Role *role = PARROT_ROLE(SELF);
/* If we have already added a method with this name... */
if (VTABLE_exists_keyed_str(interp, role->methods, name)) {
/* XXX Need to handle multi methods here. */
real_exception(interp, NULL, E_NotImplementedError,
"Currently, adding multiple methods of the same name is not supported.");
}
else {
/* Enter it into the table. */
VTABLE_set_pmc_keyed_str(interp, role->methods, name, sub);
}
}
/*
=item C<void add_role(PMC *role)>
Composes the supplied Role PMC into this role, provided there are no
conflicts.
=cut
*/
void add_role(PMC *role) {
Parrot_Role *this_role = PARROT_ROLE(SELF);
/* Do the composition. */
Parrot_ComposeRole(interp, role, PMCNULL, 0, PMCNULL, 0,
this_role->methods, this_role->roles);
}
/*
=item C<PMC *inspect_str(STRING *what)>
Provides introspection of a specific piece of information about the role. The
available information is:
=over 4
=item name - String PMC containing the name of the role
=item namespce - NameSpace PMC of the the namespace attached to the role
=item attributes - Hash keyed on attribute name, value is hash describing it
=item methods - Hash keyed on method name, value is an invokable PMC. Includes
methods composed in from roles.
=item roles - Array of Role PMCs. Includes roles done by the roles that were
composed into this role.
=back
=cut
*/
PMC* inspect_str(STRING *what) {
Parrot_Role *role = PARROT_ROLE(SELF);
/* What should we return? */
PMC *found;
if (string_equal(interp, what, CONST_STRING(interp, "name")) == 0) {
found = pmc_new(interp, enum_class_String);
VTABLE_set_string_native(interp, found, role->name);
}
else if (string_equal(interp, what, CONST_STRING(interp, "namespace")) == 0) {
/* Don't clone the namespace, as it's not part of our state. */
return role->namespace;
}
else if (string_equal(interp, what, CONST_STRING(interp, "attributes")) == 0) {
found = role->attrib_metadata;
}
else if (string_equal(interp, what, CONST_STRING(interp, "methods")) == 0) {
found = role->methods;
}
else if (string_equal(interp, what, CONST_STRING(interp, "roles")) == 0) {
found = role->roles;
}
else {
real_exception(interp, NULL, INVALID_OPERATION,
"Unknown introspection value '%S'", what);
}
/* Clone and return. */
return PMC_IS_NULL(found) ? PMCNULL : VTABLE_clone(interp, found);
}
/*
=item C<PMC *inspect()>
Returns a Hash describing the role, with key/value pairs as described in
inspect_str.
=cut
*/
PMC* inspect() {
/* Create a hash, then use inspect_str to get all of the data to
* fill it up with. */
PMC *metadata = pmc_new(interp, enum_class_Hash);
VTABLE_set_pmc_keyed_str(interp, metadata, CONST_STRING(interp, "name"),
VTABLE_inspect_str(interp, SELF, CONST_STRING(interp, "name")));
VTABLE_set_pmc_keyed_str(interp, metadata, CONST_STRING(interp, "namespace"),
VTABLE_inspect_str(interp, SELF, CONST_STRING(interp, "namespace")));
VTABLE_set_pmc_keyed_str(interp, metadata, CONST_STRING(interp, "attributes"),
VTABLE_inspect_str(interp, SELF, CONST_STRING(interp, "attributes")));
VTABLE_set_pmc_keyed_str(interp, metadata, CONST_STRING(interp, "methods"),
VTABLE_inspect_str(interp, SELF, CONST_STRING(interp, "methods")));
VTABLE_set_pmc_keyed_str(interp, metadata, CONST_STRING(interp, "roles"),
VTABLE_inspect_str(interp, SELF, CONST_STRING(interp, "roles")));
return metadata;
}
/*
* Below here are non-vtable methods that eventually will go in a role
* that is composed into here to optionally give a nice interface from
* PIR (ParrotRole isa Role does RoleMethods or something like this).
*/
/*
=back
=head2 Methods
=over 4
=item C<PCCMETHOD void
name(STRING *name :optional, int got_name :opt_flag)>
Sets the name of the role, and updates the namespace accoringly.
=cut
*/
PCCMETHOD void name(STRING *name :optional, int got_name :opt_flag) {
Parrot_Role *role = PARROT_ROLE(SELF);
STRING *ret_name = NULL;
if (got_name) {
/* We'll build a hash just containing the name, then give this to
* init_role_from_hash - saves some code duplication. */
PMC *naming_hash = pmc_new(interp, enum_class_Hash);
VTABLE_set_string_keyed_str(interp, naming_hash,
CONST_STRING(interp, "name"), name);
init_role_from_hash(interp, SELF, naming_hash);
}
ret_name = role->name;
PCCRETURN(STRING *ret_name);
}
/*
=item C<PCCMETHOD void
namespace()>
Gets the namespace associated with this role, if any.
=cut
*/
PCCMETHOD void namespace() {
Parrot_Role *role = PARROT_ROLE(SELF);
PMC *ret_namespace = role->namespace;
PCCRETURN(PMC *ret_namespace);
}
/*
=item C<PCCMETHOD void
attributes()>
Return a hash where the keys are attribute names and the values are hashes
providing a set of key/value pairs describing the attribute.
=cut
*/
PCCMETHOD void attributes() {
PMC *ret_attrib_metadata = VTABLE_inspect_str(interp, SELF,
CONST_STRING(interp, "attributes"));
PCCRETURN(PMC *ret_attrib_metadata);
}
/*
=item C<PCCMETHOD void
add_attribute(STRING *attribute_name,
PMC* attribute_type :optional, int got_type :opt_flag)>
Add an attribute to the role. Requires a name and, optionally, a type.
=cut
*/
PCCMETHOD void add_attribute(STRING *attribute_name,
PMC* attribute_type :optional, int got_type :opt_flag) {
VTABLE_add_attribute(interp, SELF, attribute_name,
got_type ? attribute_type : PMCNULL);
}
/*
=item C<PCCMETHOD void
methods()>
Return a hash where the keys are method names and the values are methods.
=cut
*/
PCCMETHOD void methods() {
PMC *ret_methods = VTABLE_inspect_str(interp, SELF,
CONST_STRING(interp, "methods"));
PCCRETURN(PMC *ret_methods);
}
/*
=item C<PCCMETHOD void
add_method(STRING *name, PMC *sub)>
Adds the given sub PMC as a method with the given name.
=cut
*/
PCCMETHOD void add_method(STRING *name, PMC *sub) {
VTABLE_add_method(interp, SELF, name, sub);
}
/*
=item C<PCCMETHOD void
roles()>
Return the roles array PMC.
=cut
*/
PCCMETHOD void roles() {
PMC *ret_roles = VTABLE_inspect_str(interp, SELF,
CONST_STRING(interp, "roles"));
PCCRETURN(PMC *ret_roles);
}
/*
=item C<PCCMETHOD void
add_role(PMC* role,
PMC* exclude_method :optional :named["exclude_method"],
int got_exclude_method :opt_flag,
PMC* alias_method :optional :named["alias_method"],
int got_alias_method :opt_flag)>
Compose the given role into this one, using the given exclusions and aliases.
=cut
*/
PCCMETHOD void add_role(PMC* role,
PMC* exclude_method :optional :named["exclude_method"],
int got_exclude_method :opt_flag,
PMC* alias_method :optional :named["alias_method"],
int got_alias_method :opt_flag) {
Parrot_Role *role_info = PARROT_ROLE(SELF);
Parrot_ComposeRole(interp, role, exclude_method, got_exclude_method,
alias_method, got_alias_method,
role_info->methods, role_info->roles);
}
/*
=item C<void inspect(STRING *what :optional)>
Gets all introspection data for the role or, if the optional string
parameter is supplied, a particular item of introspection data.
=cut
*/
PCCMETHOD void inspect(STRING *what :optional, int got_what :opt_flag) {
PMC *found;
/* Just delegate to the appropriate vtable method. */
if (got_what)
found = VTABLE_inspect_str(interp, SELF, what);
else
found = VTABLE_inspect(interp, SELF);
PCCRETURN(PMC *found);
}
} /* end pmclass Role */
/*
=back
=head1 STABILITY
Unstable. This PMC is under active development; major portions of the
interface have not yet been completed.
=head1 SEE ALSO
F<docs/pdds/pdd15_objects.pod>.
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4:
*/