/
packfiledirectory.pmc
289 lines (187 loc) · 6.25 KB
/
packfiledirectory.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
/*
Copyright (C) 2001-2014, Parrot Foundation.
=head1 NAME
src/pmc/packfiledirectory.pmc - PackfileDirectory PMC
=head1 DESCRIPTION
This class implements a PackfileDirectory object, a segment of the .pbc data
file used for listing the rest of the file's contents. Essentially this
segment is a container for all other segments within a Packfile.
See packfile.pmc for the toplevel Packfile interface, see packfilesegment.pmc
for the list of common methods every packfile segment pmc must implement; see
PDD13 for the design spec.
=head2 Methods
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* HEADERIZER END: static */
pmclass PackfileDirectory auto_attrs extends PackfileSegment {
/* Directory is a hash of Segments */
ATTR PMC *hash;
/*
=item C<void init()>
Initialize PackfileDirectory.
=cut
*/
VTABLE void init() {
Parrot_PackfileDirectory_attributes * const attrs =
PMC_data_typed(SELF, Parrot_PackfileDirectory_attributes*);
attrs->hash = Parrot_pmc_new(INTERP, enum_class_Hash);
PObj_custom_mark_SET(SELF);
}
/*
=item C<void mark()>
Marks the object as live.
=cut
*/
VTABLE void mark() :no_wb {
Parrot_PackfileDirectory_attributes * const attrs =
PARROT_PACKFILEDIRECTORY(SELF);
Parrot_gc_mark_PMC_alive(INTERP, attrs->hash);
SUPER();
}
/*
=item C<void set_pointer(void *ptr)>
Initialize PackfileDirectory from PackFile_Directory.
=cut
*/
VTABLE void set_pointer(void *ptr) {
const PackFile_Directory * const pfd = (const PackFile_Directory *)ptr;
size_t i;
SUPER(ptr);
/* Iterate over elements and create corresponded PMCs */
for (i=0; i < pfd->num_segments; ++i) {
PackFile_Segment * const pfseg = pfd->segments[i];
int pmc_type;
PMC *segment;
STRING *name;
switch (pfseg->type) {
case PF_DIR_SEG:
pmc_type = enum_class_PackfileDirectory;
break;
case PF_CONST_SEG:
pmc_type = enum_class_PackfileConstantTable;
break;
case PF_ANNOTATIONS_SEG:
pmc_type = enum_class_PackfileAnnotations;
break;
case PF_DEBUG_SEG:
pmc_type = enum_class_PackfileDebug;
break;
case PF_BYTEC_SEG:
pmc_type = enum_class_PackfileBytecodeSegment;
break;
case PF_UNKNOWN_SEG:
default:
pmc_type = enum_class_PackfileRawSegment;
break;
}
segment = Parrot_pmc_new(INTERP, pmc_type);
name = pfseg->name;
VTABLE_set_pmc_keyed_str(INTERP, SELF, name, segment);
/* Initialize internal PMC structure */
VTABLE_set_pointer(INTERP, segment, pfseg);
}
}
/*
=item C<void *get_pointer()>
Creates PackFile for given directory.
=cut
*/
VTABLE void *get_pointer() :no_wb {
Parrot_PackfileDirectory_attributes * attrs =
PARROT_PACKFILEDIRECTORY(SELF);
PackFile * const pf = Parrot_pf_new(INTERP, 0); /* dummy PackFile... */
PackFile_Directory * const pfdir = &pf->directory;
PMC * const iter = VTABLE_get_iter(INTERP, attrs->hash);
/* Create Segments. Add to Directory with transfering ownership */
while (VTABLE_get_bool(INTERP, iter)) {
STRING * const name = VTABLE_shift_string(INTERP, iter);
PMC * const seg = VTABLE_get_pmc_keyed_str(INTERP, attrs->hash, name);
PackFile_Segment * const pfseg = (PackFile_Segment *)VTABLE_get_pointer(INTERP, seg);
pfseg->pf = pf;
pfseg->name = name;
Parrot_pf_add_segment(INTERP, pfdir, pfseg);
}
return pf;
}
/*
=item C<INTVAL elements()>
Get the number of elements in the array.
=cut
*/
VTABLE INTVAL elements() :no_wb {
return VTABLE_elements(INTERP, PARROT_PACKFILEDIRECTORY(SELF)->hash);
}
/*
=item C<PMC *get_iter()>
Return a new iterator for the directory.
=cut
*/
VTABLE PMC *get_iter() :no_wb {
return VTABLE_get_iter(INTERP, PARROT_PACKFILEDIRECTORY(SELF)->hash);
}
/*
=item C<PMC *get_pmc_keyed_str(STRING *name)>
Search the array for a segment with the given name, and return it if it exists.
=cut
*/
VTABLE PMC *get_pmc_keyed_str(STRING *name) :no_wb {
return VTABLE_get_pmc_keyed_str(INTERP,
PARROT_PACKFILEDIRECTORY(SELF)->hash, name);
}
/*
=item C<PMC *get_pmc_keyed(PMC *key)>
Fetch a keyed string value from the packfiledirectory object.
=cut
*/
VTABLE PMC *get_pmc_keyed(PMC *key) :no_wb {
STRING * const s_key = VTABLE_get_string(INTERP, key);
return VTABLE_get_pmc_keyed_str(INTERP,
PARROT_PACKFILEDIRECTORY(SELF)->hash, s_key);
}
/*
=item C<void set_pmc_keyed_str(STRING *name, PMC *segment)>
Add a segment with the given name to the directory. If a segment with this
name already existed, it will be replaced with the new segment.
=cut
TODO: check type
*/
VTABLE void set_pmc_keyed_str(STRING *name, PMC *segment) :manual_wb {
Parrot_pcc_invoke_method_from_c_args(INTERP, segment,
Parrot_str_new_constant(INTERP, "set_directory"),
"P->", SELF);
VTABLE_set_pmc_keyed_str(INTERP,
PARROT_PACKFILEDIRECTORY(SELF)->hash, name, segment);
}
/*
=item C<PMC *set_pmc_keyed(PMC *key, PMC *segment)>
Add a segment with the given name to the directory. Dispatches to
set_pmc_keyed_str.
=cut
*/
VTABLE void set_pmc_keyed(PMC *key, PMC *segment) :manual_wb {
STRING * const s_key = VTABLE_get_string(INTERP, key);
VTABLE_set_pmc_keyed_str(INTERP, SELF, s_key, segment);
}
/*
=item C<PMC *delete_keyed(PMC *key)>
Delete the specified segment from the directory.
=cut
*/
VTABLE void delete_keyed(PMC *key) :manual_wb {
VTABLE_delete_keyed(INTERP,
PARROT_PACKFILEDIRECTORY(SELF)->hash, key);
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/