/
objects.c
2350 lines (1810 loc) · 64 KB
/
objects.c
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
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2001-2007, The Perl Foundation.
$Id$
=head1 NAME
objects.c - Class and object
=head1 DESCRIPTION
Handles class and object manipulation.
=head2 Functions
=over 4
=cut
*/
#define PARROT_IN_OBJECTS_C
#include "parrot/parrot.h"
#include "parrot/objects.h"
#include "objects.str"
/* HEADERIZER HFILE: include/parrot/objects.h */
/* HEADERIZER BEGIN: static */
static INTVAL attr_str_2_num(PARROT_INTERP,
NOTNULL(PMC *object),
NOTNULL(STRING *attr))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC* C3_merge(PARROT_INTERP, NOTNULL(PMC *merge_list))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
static PMC* class_mro_merge(PARROT_INTERP, NOTNULL(PMC *seqs))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
static PMC* create_class_mro(PARROT_INTERP, NOTNULL(PMC *_class))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void create_deleg_pmc_vtable(PARROT_INTERP,
NOTNULL(PMC *_class),
int full)
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void debug_trace_find_meth(PARROT_INTERP,
NOTNULL(PMC *_class),
NOTNULL(STRING *name),
NULLOK(PMC *sub))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
static void do_initcall(PARROT_INTERP,
NULLOK(PMC* _class),
NULLOK(PMC *object),
NULLOK(PMC *init))
__attribute__nonnull__(1);
static void fail_if_exist(PARROT_INTERP, NOTNULL(PMC *name))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC * find_method_direct_1(PARROT_INTERP,
NOTNULL(PMC *_class),
NOTNULL(STRING *method_name))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC* find_vtable_meth_ns(PARROT_INTERP,
NOTNULL(PMC *ns),
INTVAL vtable_index)
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC* get_init_meth(PARROT_INTERP,
NOTNULL(PMC *_class),
NOTNULL(STRING *prop_str),
NOTNULL(STRING **meth_str))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(4);
static void instantiate_object(PARROT_INTERP,
NOTNULL(PMC *object),
NULLOK(PMC *init))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void invalidate_all_caches(PARROT_INTERP)
__attribute__nonnull__(1);
static void invalidate_type_caches(PARROT_INTERP, UINTVAL type)
__attribute__nonnull__(1);
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
static PMC* not_empty(PARROT_INTERP, NOTNULL(PMC *seqs))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
static void parrot_class_register(PARROT_INTERP,
NOTNULL(PMC *name),
NOTNULL(PMC *new_class),
NULLOK(PMC *parent),
NOTNULL(PMC *mro))
__attribute__nonnull__(1)
__attribute__nonnull__(2)
__attribute__nonnull__(3)
__attribute__nonnull__(5);
static void rebuild_attrib_stuff(PARROT_INTERP, NOTNULL(PMC *_class))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
PARROT_WARN_UNUSED_RESULT
static INTVAL register_type(PARROT_INTERP, NOTNULL(PMC *name))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
/* HEADERIZER END: static */
/*
=item C<Parrot_get_vtable_index>
Return index if C<name> is a valid vtable slot name.
=cut
*/
PARROT_API
INTVAL
Parrot_get_vtable_index(PARROT_INTERP, NOTNULL(const STRING *name))
{
char * const name_c = string_to_cstring(interp, name);
/* some of the first "slots" don't have names. skip 'em. */
INTVAL low = PARROT_VTABLE_LOW;
INTVAL high = NUM_VTABLE_FUNCTIONS + PARROT_VTABLE_LOW;
while (low < high) {
const INTVAL mid = (low + high) / 2;
const char * const meth_c = Parrot_vtable_slot_names[mid];
/* RT#45965 slot_names still have __ in front */
const INTVAL cmp = strcmp(name_c, meth_c + 2);
if (cmp == 0) {
string_cstring_free(name_c);
return mid;
}
else if (cmp > 0)
low = mid + 1;
else
high = mid;
}
string_cstring_free(name_c);
return -1;
}
/*
=item C<find_vtable_meth_ns>
Return Sub PMC if a method with the vtable name exists in ns
=cut
*/
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC*
find_vtable_meth_ns(PARROT_INTERP, NOTNULL(PMC *ns), INTVAL vtable_index)
{
return VTABLE_get_pmc_keyed_int(interp, ns, vtable_index);
}
/*
=item C<Parrot_find_vtable_meth>
Given pmc, run through its mro looking for the meth vtable method.
Return the vtable method PMC if found.
=cut
*/
PARROT_API
PARROT_CAN_RETURN_NULL
PMC*
Parrot_find_vtable_meth(PARROT_INTERP, NOTNULL(PMC *pmc), NOTNULL(STRING *meth))
{
INTVAL i, n;
PMC *ns, *mro;
PMC *_class = pmc;
/* Get index in Parrot_vtable_slot_names[]. */
const INTVAL vtable_index = Parrot_get_vtable_index(interp, meth);
if (vtable_index == -1)
return PMCNULL;
/* Get class. */
if (PObj_is_object_TEST(pmc))
_class = GET_CLASS(PMC_data_typed(pmc, Buffer), pmc);
/* Get MRO and iterate over it to find method with a matching
vtable index or double-underscored name. */
mro = _class->vtable->mro;
n = VTABLE_elements(interp, mro);
for (i = 0; i < n; ++i) {
_class = VTABLE_get_pmc_keyed_int(interp, mro, i);
ns = VTABLE_pmc_namespace(interp, _class);
if (!PMC_IS_NULL(ns)) {
PMC * const res = find_vtable_meth_ns(interp, ns, vtable_index);
if (!PMC_IS_NULL(res))
return res;
}
}
/* If we get here, method is not overridden in the class. */
return PMCNULL;
}
/*
=item C<readable_name>
Given a String or Key PMC return the STRING* representation
RT#45967 this function, key_set_to_string, and the key PMC get_repr should be consolidated
=cut
*/
PARROT_API
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
STRING*
readable_name(PARROT_INTERP, NOTNULL(PMC *name))
{
STRING *join_on;
PMC *array;
if (name->vtable->base_type == enum_class_String)
return VTABLE_get_string(interp, name);
join_on = CONST_STRING(interp, ";");
array = pmc_new(interp, enum_class_ResizableStringArray);
PARROT_ASSERT(name->vtable->base_type == enum_class_Key);
while (name) {
VTABLE_push_string(interp, array, key_string(interp, name));
name = key_next(interp, name);
}
return string_join(interp, join_on, array);
}
/*
=item C<fail_if_exist>
Throws an exception if a PMC or class with the same name already exists.
RT#45969 uses global class registry
=cut
*/
static void
fail_if_exist(PARROT_INTERP, NOTNULL(PMC *name))
{
INTVAL type;
PMC * const classname_hash = interp->class_hash;
PMC * const type_pmc = (PMC *)VTABLE_get_pointer_keyed(interp,
classname_hash, name);
if (PMC_IS_NULL(type_pmc) ||
type_pmc->vtable->base_type == enum_class_NameSpace)
type = 0;
else
type = VTABLE_get_integer(interp, type_pmc);
if (type > enum_type_undef) {
/* RT#45971 get printable name */
real_exception(interp, NULL, INVALID_OPERATION,
"Class %Ss already registered!\n",
VTABLE_get_string(interp, name));
}
if (type < enum_type_undef)
real_exception(interp, NULL, INVALID_OPERATION,
"native type with name '%s' already exists - "
"can't register Class", data_types[type].name);
}
/* Take the class and completely rebuild the attribute stuff for
it. Horribly destructive, and definitely not a good thing to do if
there are instantiated objects for the class */
static void
rebuild_attrib_stuff(PARROT_INTERP, NOTNULL(PMC *_class))
{
INTVAL attr_count, cur_offset, n_class, n_mro, offset;
PMC *attr_offset_hash, *mro, *attribs;
SLOTTYPE *class_slots;
#ifndef NDEBUG
PMC * const orig_class = _class;
#endif
/* attrib count isn't set yet, a GC caused by concat could
* corrupt data under construction
*/
Parrot_block_DOD(interp);
class_slots = PMC_data_typed(_class, SLOTTYPE *);
attr_offset_hash = pmc_new(interp, enum_class_Hash);
set_attrib_num(_class, class_slots, PCD_ATTRIBUTES, attr_offset_hash);
mro = _class->vtable->mro;
n_mro = VTABLE_elements(interp, mro);
/* walk from oldest parent down to n_class == 0 which is this class */
cur_offset = 0;
for (n_class = n_mro - 1; n_class >= 0; --n_class) {
STRING *classname;
_class = VTABLE_get_pmc_keyed_int(interp, mro, n_class);
/* this Class isa PMC - no attributes there */
if (!PObj_is_class_TEST(_class))
continue;
class_slots = PMC_data_typed(_class, SLOTTYPE *);
classname = VTABLE_get_string(interp,
get_attrib_num(class_slots, PCD_CLASS_NAME));
attribs = get_attrib_num(class_slots, PCD_CLASS_ATTRIBUTES);
attr_count = VTABLE_elements(interp, attribs);
if (attr_count) {
STRING * const partial_name = string_concat(interp, classname,
string_from_cstring(interp, "\0", 1), 0);
for (offset = 0; offset < attr_count; offset++) {
STRING * const attr_name = VTABLE_get_string_keyed_int(interp, attribs, offset);
STRING * const full_name = string_concat(interp, partial_name, attr_name, 0);
/* store this attribute with short and full name */
VTABLE_set_integer_keyed_str(interp, attr_offset_hash,
attr_name, cur_offset);
VTABLE_set_integer_keyed_str(interp, attr_offset_hash,
full_name, cur_offset);
cur_offset++;
}
}
}
#ifndef NDEBUG
PARROT_ASSERT(_class == orig_class);
#endif
/* And note the totals */
CLASS_ATTRIB_COUNT(_class) = cur_offset;
Parrot_unblock_DOD(interp);
}
/*
=item C<create_deleg_pmc_vtable>
Create a vtable that dispatches either to the contained PMC in the first
attribute (deleg_pmc) or to an overridden method (delegate), depending
on the existence of the method for this class.
=cut
*/
static void
create_deleg_pmc_vtable(PARROT_INTERP, NOTNULL(PMC *_class), int full)
{
int i;
const char *meth;
STRING meth_str;
DECL_CONST_CAST;
PMC * const vtable_pmc = get_attrib_num(PMC_data_typed(_class,
SLOTTYPE *), PCD_OBJECT_VTABLE);
VTABLE * const vtable = (VTABLE *)PMC_struct_val(vtable_pmc);
VTABLE * const ro_vtable = vtable->ro_variant_vtable;
VTABLE * const deleg_pmc_vtable = interp->vtables[enum_class_deleg_pmc];
VTABLE * const object_vtable = interp->vtables[enum_class_ParrotObject];
VTABLE * const ro_object_vtable = object_vtable->ro_variant_vtable;
VTABLE * const delegate_vtable = interp->vtables[enum_class_delegate];
memset(&meth_str, 0, sizeof (meth_str));
meth_str.encoding = Parrot_fixed_8_encoding_ptr;
meth_str.charset = Parrot_default_charset_ptr;
for (i = 0; (meth = Parrot_vtable_slot_names[i]) != NULL; ++i) {
if (!*meth)
continue;
/* strip underscores from method name */
meth_str.strstart = (char *)const_cast(meth + 2);
meth_str.strlen = meth_str.bufused = strlen(meth) - 2;
meth_str.hashval = 0;
if (!PMC_IS_NULL(Parrot_find_vtable_meth(interp, _class, &meth_str))) {
/* the method exists; keep the ParrotObject delegate vtable slot */
((void **)vtable)[i] = ((void**)object_vtable)[i];
if (ro_vtable)
((void **)ro_vtable)[i] = ((void**)ro_object_vtable)[i];
}
else if (full) {
/*
* the method doesn't exist; put in the deleg_pmc vtable,
* but only if ParrotObject hasn't overridden the method
*/
if (((void **)delegate_vtable)[i] == ((void**)object_vtable)[i]) {
if (ro_vtable)
((void **)ro_vtable)[i] = ((void**)deleg_pmc_vtable)[i];
((void **)vtable)[i] = ((void**)deleg_pmc_vtable)[i];
}
else {
((void **)vtable)[i] = ((void**)object_vtable)[i];
if (ro_vtable)
((void **)ro_vtable)[i] = ((void**)ro_object_vtable)[i];
}
}
}
}
/*
=item C<Parrot_MMD_method_name>
Return the method name for the given MMD enum.
=cut
*/
PARROT_API
PARROT_PURE_FUNCTION
PARROT_CAN_RETURN_NULL
const char*
Parrot_MMD_method_name(SHIM_INTERP, INTVAL idx)
{
PARROT_ASSERT(idx >= 0);
if (idx >= MMD_USER_FIRST)
return NULL;
return Parrot_mmd_func_names[idx];
}
/*
=item C<Parrot_MMD_method_idx>
Return the MMD function number for method name or -1 on failure.
RT#45973 allow dynamic expansion at runtime.
=cut
*/
PARROT_API
PARROT_PURE_FUNCTION
INTVAL
Parrot_MMD_method_idx(SHIM_INTERP, NOTNULL(const char *name))
{
INTVAL i;
for (i = 0; i < MMD_USER_FIRST; ++i) {
if (strcmp(Parrot_mmd_func_names[i], name) == 0)
return i;
}
return -1;
}
/*
=item C<Parrot_single_subclass>
Subclass a class. Single parent class, nice and straightforward. If
C<child_class> is C<NULL>, this is an anonymous subclass we're creating,
function.
=cut
*/
PARROT_API
PARROT_WARN_UNUSED_RESULT
PARROT_CANNOT_RETURN_NULL
PMC *
Parrot_single_subclass(PARROT_INTERP, NOTNULL(PMC *base_class), NULLOK(PMC *name))
{
PMC *child_class, *parents, *temp_pmc, *mro;
SLOTTYPE *child_class_array;
int parent_is_class;
/* Set the classname, if we have one */
if (!PMC_IS_NULL(name)) {
fail_if_exist(interp, name);
}
else {
/* RT#45975 not really threadsafe but good enough for now */
static int anon_count;
STRING * const child_class_name =
Parrot_sprintf_c(interp, "%c%canon_%d", 0, 0, ++anon_count);
name = pmc_new(interp, enum_class_String);
VTABLE_set_string_native(interp, name, child_class_name);
}
/* ParrotClass is the baseclass anyway, so build just a new class */
if (base_class == interp->vtables[enum_class_ParrotClass]->pmc_class)
return pmc_new_init(interp, enum_class_ParrotClass, name);
parent_is_class = PObj_is_class_TEST(base_class);
child_class = pmc_new(interp, enum_class_ParrotClass);
/* Hang an array off the data pointer */
set_attrib_array_size(child_class, PCD_MAX);
child_class_array = PMC_data_typed(child_class, SLOTTYPE *);
set_attrib_flags(child_class);
/* We will have five entries in this array */
/* We have the same number of attributes as our parent */
CLASS_ATTRIB_COUNT(child_class) = parent_is_class ?
CLASS_ATTRIB_COUNT(base_class) : 0;
/* Our parent class array has a single member in it */
parents = pmc_new(interp, enum_class_ResizablePMCArray);
VTABLE_set_integer_native(interp, parents, 1);
VTABLE_set_pmc_keyed_int(interp, parents, 0, base_class);
set_attrib_num(child_class, child_class_array, PCD_PARENTS, parents);
set_attrib_num(child_class, child_class_array, PCD_CLASS_NAME, name);
/* Our mro list is a clone of our parent's mro list,
* with our self unshifted onto the beginning */
mro = VTABLE_clone(interp, base_class->vtable->mro);
VTABLE_unshift_pmc(interp, mro, child_class);
/* But we have no attributes of our own. Yet */
temp_pmc = pmc_new(interp, enum_class_ResizablePMCArray);
set_attrib_num(child_class, child_class_array, PCD_CLASS_ATTRIBUTES,
temp_pmc);
parrot_class_register(interp, name, child_class, base_class, mro);
rebuild_attrib_stuff(interp, child_class);
if (!parent_is_class) {
/* we append one attribute to hold the PMC */
Parrot_add_attribute(interp, child_class,
CONST_STRING(interp, "__value"));
/*
* then create a vtable derived from ParrotObject and
* deleg_pmc - the ParrotObject vtable is already built
*/
create_deleg_pmc_vtable(interp, child_class, 1);
}
else {
/*
* if any parent isa PMC, then still individual vtables might
* be overridden in this subclass
*/
int i, any_pmc_parent;
const int n = VTABLE_elements(interp, mro);
any_pmc_parent = 0;
/* 0 = this, 1 = parent (handled above), 2 = grandpa */
for (i = 2; i < n; ++i) {
const PMC * const parent = VTABLE_get_pmc_keyed_int(interp, mro, i);
if (!PObj_is_class_TEST(parent)) {
any_pmc_parent = 1;
break;
}
}
if (any_pmc_parent)
create_deleg_pmc_vtable(interp, child_class, 0);
}
return child_class;
}
/*
=item C<Parrot_new_class>
Creates a new class, named C<class_name>.
=cut
*/
PARROT_API
void
Parrot_new_class(PARROT_INTERP, NOTNULL(PMC *_class), NOTNULL(PMC *name))
{
SLOTTYPE *class_array;
PMC *mro;
/* check against duplicate newclasses */
fail_if_exist(interp, name);
/* Hang an array off the data pointer, empty of course */
set_attrib_array_size(_class, PCD_MAX);
class_array = PMC_data_typed(_class, SLOTTYPE *);
/* set_attrib_flags(_class); init does it */
/* Our parent class array has nothing in it */
set_attrib_num(_class, class_array, PCD_PARENTS,
pmc_new(interp, enum_class_ResizablePMCArray));
/* RT#45977 create all class structures in constant PMC pool */
/*
* create MRO (method resolution order) array
* first entry is this class itself
*/
mro = pmc_new(interp, enum_class_ResizablePMCArray);
VTABLE_push_pmc(interp, mro, _class);
/* no attributes yet */
set_attrib_num(_class, class_array, PCD_CLASS_ATTRIBUTES,
pmc_new(interp, enum_class_ResizablePMCArray));
/* Set the classname */
set_attrib_num(_class, class_array, PCD_CLASS_NAME, name);
parrot_class_register(interp, name, _class, NULL, mro);
rebuild_attrib_stuff(interp, _class);
}
/*
=item C<Parrot_class_lookup>
Looks for the class named C<class_name> and returns it if it exists.
Otherwise it returns C<PMCNULL>.
=cut
*/
PARROT_API
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
PMC *
Parrot_class_lookup(PARROT_INTERP, NOTNULL(STRING *class_name))
{
const INTVAL type = pmc_type(interp, class_name);
PMC *pmc;
if (type <= 0)
return PMCNULL;
pmc = interp->vtables[type]->pmc_class;
PARROT_ASSERT(pmc);
return pmc;
}
/*
=item C<Parrot_class_lookup_p>
Looks for the class named C<class_name> and returns it if it exists.
Otherwise it returns C<PMCNULL>.
=cut
*/
PARROT_CAN_RETURN_NULL
PARROT_WARN_UNUSED_RESULT
PMC *
Parrot_class_lookup_p(PARROT_INTERP, NOTNULL(PMC *class_name))
{
const INTVAL type = pmc_type_p(interp, class_name);
PMC *pmc;
if (type <= 0)
return PMCNULL;
pmc = interp->vtables[type]->pmc_class;
PARROT_ASSERT(pmc);
return pmc;
}
PARROT_WARN_UNUSED_RESULT
static INTVAL
register_type(PARROT_INTERP, NOTNULL(PMC *name))
{
INTVAL type;
PMC *classname_hash, *item;
/* so pt_shared_fixup() can safely do a type lookup */
LOCK_INTERPRETER(interp);
classname_hash = interp->class_hash;
type = interp->n_vtable_max++;
/* Have we overflowed the table? */
if (type >= interp->n_vtable_alloced)
parrot_realloc_vtables(interp);
/* set entry in name->type hash */
item = pmc_new(interp, enum_class_Integer);
PMC_int_val(item) = type;
VTABLE_set_pmc_keyed(interp, classname_hash, name, item);
UNLOCK_INTERPRETER(interp);
return type;
}
/*
=item C<parrot_class_register>
This is the way to register a new Parrot class as an instantiable
type. Doing this involves putting it in the class hash, setting its
vtable so that the C<init> method initializes objects of the class rather than
the class itself, and adding it to the interpreter's base type table so
you can create a new C<foo> in PASM like this: C<new Px, foo>.
=cut
*/
static void
parrot_class_register(PARROT_INTERP, NOTNULL(PMC *name),
NOTNULL(PMC *new_class), NULLOK(PMC *parent), NOTNULL(PMC *mro))
{
VTABLE *new_vtable, *parent_vtable;
PMC *vtable_pmc, *ns, *top;
const INTVAL new_type = register_type(interp, name);
/* Build a new vtable for this class
* The child class PMC gets the vtable of its parent class or
* a ParrotClass vtable
*/
if (parent && PObj_is_class_TEST(parent))
parent_vtable = parent->vtable;
else
parent_vtable = new_class->vtable;
new_vtable = Parrot_clone_vtable(interp, parent_vtable);
/* Set the vtable's type to the newly allocated type */
new_vtable->base_type = new_type;
/* And cache our class PMC in the vtable so we can find it later */
new_vtable->pmc_class = new_class;
new_vtable->mro = mro;
if (parent_vtable->ro_variant_vtable)
new_vtable->ro_variant_vtable =
Parrot_clone_vtable(interp, parent_vtable->ro_variant_vtable);
/* Reset the init method to our instantiation method */
new_vtable->init = Parrot_instantiate_object;
new_vtable->init_pmc = Parrot_instantiate_object_init;
new_class->vtable = new_vtable;
/* Put our new vtable in the global table */
interp->vtables[new_type] = new_vtable;
/* check if we already have a NameSpace */
top = CONTEXT(interp->ctx)->current_namespace;
ns = VTABLE_get_pmc_keyed(interp, top, name);
/* RT#45979 nested, use current as base ? */
if (PMC_IS_NULL(ns)) {
/* RT#45983 try HLL namespace too */
top = Parrot_get_ctx_HLL_namespace(interp);
ns = VTABLE_get_pmc_keyed(interp, top, name);
}
if (PMC_IS_NULL(ns)) {
ns = pmc_new(interp, enum_class_NameSpace);
VTABLE_set_pmc_keyed(interp, top, name, ns);
}
/* attach namespace to vtable */
new_vtable->_namespace = ns;
if (new_vtable->ro_variant_vtable) {
VTABLE * const ro_vt = new_vtable->ro_variant_vtable;
ro_vt->base_type = new_vtable->base_type;
ro_vt->pmc_class = new_vtable->pmc_class;
ro_vt->mro = new_vtable->mro;
ro_vt->_namespace = new_vtable->_namespace;
}
/*
* prepare object vtable - again that of the parent or
* a ParrotObject vtable
*/
if (parent && PObj_is_class_TEST(parent)) {
vtable_pmc =
get_attrib_num((SLOTTYPE *)PMC_data(parent), PCD_OBJECT_VTABLE);
parent_vtable = (VTABLE *)PMC_struct_val(vtable_pmc);
}
else
parent_vtable = interp->vtables[enum_class_ParrotObject];
new_vtable = Parrot_clone_vtable(interp, parent_vtable);
if (parent_vtable->ro_variant_vtable)
new_vtable->ro_variant_vtable =
Parrot_clone_vtable(interp, parent_vtable->ro_variant_vtable);
new_vtable->base_type = new_type;
new_vtable->mro = mro;
new_vtable->pmc_class = new_class;
set_attrib_num(new_class, (SLOTTYPE*)PMC_data(new_class), PCD_OBJECT_VTABLE,
vtable_pmc = constant_pmc_new(interp, enum_class_VtableCache));
PMC_struct_val(vtable_pmc) = new_vtable;
/* attach namespace to object vtable too */
new_vtable->_namespace = ns;
if (new_vtable->ro_variant_vtable) {
VTABLE * const ro_vt = new_vtable->ro_variant_vtable;
ro_vt->base_type = new_vtable->base_type;
ro_vt->pmc_class = new_vtable->pmc_class;
ro_vt->mro = new_vtable->mro;
ro_vt->_namespace = new_vtable->_namespace;
}
}
PARROT_WARN_UNUSED_RESULT
PARROT_CAN_RETURN_NULL
static PMC*
get_init_meth(PARROT_INTERP, NOTNULL(PMC *_class),
NOTNULL(STRING *prop_str), NOTNULL(STRING **meth_str))
{
STRING *meth;
HashBucket *b;
PMC *props, *ns, *method;
*meth_str = NULL;
#if 0
PMC *prop;
prop = VTABLE_getprop(interp, _class, prop_str);
if (!VTABLE_defined(interp, prop))
return PMCNULL;
meth = VTABLE_get_string(interp, prop);
#else
props = PMC_metadata(_class);
if (!props)
return PMCNULL;
b = parrot_hash_get_bucket(interp,
(Hash*) PMC_struct_val(props), prop_str);
if (!b)
return PMCNULL;
meth = PMC_str_val((PMC*) b->value);
#endif
*meth_str = meth;
ns = VTABLE_pmc_namespace(interp, _class);
method = VTABLE_get_pmc_keyed_str(interp, ns, meth);
return method;
}
static void
do_initcall(PARROT_INTERP, NULLOK(PMC* _class), NULLOK(PMC *object),
NULLOK(PMC *init))
{
PMC * const classsearch_array = _class->vtable->mro;
INTVAL i, nparents;
/*
* 1) if class has a CONSTRUCT property run it on the object
* no redispatch
*
* RT#45985 isn't CONSTRUCT for creating new objects?
*/
STRING *meth_str;
PMC *meth = get_init_meth(interp, _class,
CONST_STRING(interp, "CONSTRUCT"), &meth_str);
int default_meth;
if (!PMC_IS_NULL(meth)) {
if (init)
Parrot_run_meth_fromc_args(interp, meth,
object, meth_str, "vP", init);
else
Parrot_run_meth_fromc_args(interp, meth,
object, meth_str, "v");
}
/*
* 2. if class has a BUILD property call it for all classes
* in reverse search order - this class last.
*
* Note: mro contains this class as first element
*/
nparents = VTABLE_elements(interp, classsearch_array);
for (i = nparents - 1; i >= 0; --i) {
PMC * const parent_class =
VTABLE_get_pmc_keyed_int(interp, classsearch_array, i);
/* if it's a PMC, we put one PMC of that type into
* the attribute slot #0 and call init() on that PMC
*/
if (!PObj_is_class_TEST(parent_class)) {
PMC *attr, *next_parent;
SLOTTYPE *obj_data;
/*
* but only if init isn't inherited
* or rather just on the last non-class parent
*/
PARROT_ASSERT(i >= 1);
next_parent = VTABLE_get_pmc_keyed_int(interp,
classsearch_array, i - 1);
if (!PObj_is_class_TEST(next_parent))
continue;
attr = pmc_new_noinit(interp, parent_class->vtable->base_type);
obj_data = PMC_data_typed(object, SLOTTYPE *);
set_attrib_num(object, obj_data, 0, attr);
VTABLE_init(interp, attr);
continue;
}
meth = get_init_meth(interp, parent_class,
CONST_STRING(interp, "BUILD"), &meth_str);
/* no method found and no BUILD property set? */
if (PMC_IS_NULL(meth) && meth_str == NULL) {
PMC *ns;
INTVAL vtable_index;
/* use __init or __init_pmc (depending on if an argument was passed)
* as fallback constructor method, if it exists */
if (init)
meth_str = CONST_STRING(interp, "init_pmc");
else
meth_str = CONST_STRING(interp, "init");