/
packfileview.pmc
396 lines (288 loc) · 10.2 KB
/
packfileview.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
/*
Copyright (C) 2011, Parrot Foundation.
=head1 NAME
src/pmc/packfileview.pmc - PackfileView PMC
=head1 DESCRIPTION
This class implements a user-accessible wrapper for the PackFile* structure
used internally by Parrot. It is essentially an interface for the user to call
Packfile subsystem API calls, and work with the PackFile* structures output
from utilities which generate them.
=head2 VTABLES
=over 4
=cut
*/
/* HEADERIZER HFILE: none */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
PARROT_CAN_RETURN_NULL
static PackFile_ConstTable * get_const_table(PARROT_INTERP,
ARGIN(PMC * self))
__attribute__nonnull__(1)
__attribute__nonnull__(2);
#define ASSERT_ARGS_get_const_table __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(interp) \
, PARROT_ASSERT_ARG(self))
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=item C<static PackFile_ConstTable * get_const_table(PARROT_INTERP, PMC * self)>
Get the constant table for the PackFile*. Return NULL if there's a problem.
=cut
*/
PARROT_CAN_RETURN_NULL
static PackFile_ConstTable *
get_const_table(PARROT_INTERP, ARGIN(PMC * self))
{
ASSERT_ARGS(get_const_table)
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(self);
PackFile * const pf = attrs->pf;
PackFile_ByteCode * bt;
if (!pf)
return NULL;
bt = pf->cur_cs;
if (!bt)
return NULL;
return bt->const_table;
}
pmclass PackfileView auto_attrs {
ATTR PackFile * pf;
ATTR STRING * path;
/*
=item C<VTABLE void init()>
Create the new PMC and set flags
=item C<VTABLE void destroy()>
Destroy the PMC *AND* the underlying PackFile*
=item C<VTABLE void mark()>
Mark the PMC and the PackFile* contents
=cut
*/
VTABLE void mark() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
if (attrs->pf != NULL)
Parrot_pf_mark_packfile(INTERP, attrs->pf);
if (!STRING_IS_NULL(attrs->path))
Parrot_gc_mark_STRING_alive(INTERP, attrs->path);
}
VTABLE void init() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
attrs->pf = NULL;
PObj_custom_mark_destroy_SETALL(SELF);
}
VTABLE void destroy() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
if (attrs->pf != NULL)
Parrot_pf_destroy(INTERP, attrs->pf);
}
/*
=item C<VTABLE void set_pointer(void *ptr)>
Set a pointer to a PackFile* structure. This can only be done once. Attempting
to set a new pointer will throw an exception. This rule is in place because
the lifetime of the PackFile* is tied to the lifetime of the PMC wrapping it,
and the PMC provides access to the PackFile* by the GC. Overwriting a pointer
to an old PackFile* structure will cause memory leaks, prematurely collected
Subs, and other creepy behaviors.
=item C<VTABLE void *get_pointer()>
Get a pointer to the underlying PackFile* structure
=item C<VTABLE INTVAL get_bool()>
Return 1 (true) if this PMC is currently pointing to a PackFile*. 0 (false)
otherwise.
=cut
*/
VTABLE void set_pointer(void * ptr) {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
if (attrs->pf != NULL)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_INVALID_OPERATION,
"Cannot overwrite existing pointer in PackfileView");
attrs->pf = (PackFile*)ptr;
if (ptr != NULL)
PARROT_GC_WRITE_BARRIER(INTERP, SELF);
}
VTABLE void *get_pointer() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
return attrs->pf;
}
VTABLE INTVAL get_bool() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
return (attrs->pf != NULL);
}
/*
=item C<VTABLE void set_string_native(STRING *path)>
=item C<VTABLE STRING *get_string_native(STRING *path)>
=cut
*/
VTABLE void set_string_native(STRING * path) {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
attrs->path = path;
}
VTABLE STRING * get_string_native() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
return attrs->path ? attrs->path : STRINGNULL;
}
/*
=item C<VTABLE PMC *get_pmc_keyed_int(INTVAL idx)>
Get a PMC from the constants table, by index
=item C<VTABLE STRING *get_string_keyed_int(INTVAL idx)>
Get a STRING from the constants table, by index
=item C<VTABLE FLOATVAL get_number_keyed_int(INTVAL idx)>
Get a FLOATVAL from the constants table, by index
=cut
*/
VTABLE PMC * get_pmc_keyed_int(INTVAL idx) {
PackFile_ConstTable * const ct = get_const_table(INTERP, SELF);
if (!ct)
return PMCNULL;
if (idx < 0 || idx >= ct->pmc.const_count) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"PMC constant index out of bounds");
}
return ct->pmc.constants[idx];
}
VTABLE STRING * get_string_keyed_int(INTVAL idx) {
PackFile_ConstTable * const ct = get_const_table(INTERP, SELF);
if (!ct)
return STRINGNULL;
if (idx < 0 || idx >= ct->str.const_count) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"STRING constant index out of bounds");
}
return ct->str.constants[idx];
}
VTABLE FLOATVAL get_number_keyed_int(INTVAL idx) {
PackFile_ConstTable * const ct = get_const_table(INTERP, SELF);
if (!ct)
return 0.0;
if (idx < 0 || idx >= ct->num.const_count) {
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_OUT_OF_BOUNDS,
"PMC constant index out of bounds");
}
return ct->num.constants[idx];
}
/*
=back
=head2 METHODS
=over 4
=item C<METHOD constant_counts()>
Return a 3-element FixedIntegerArray with the total number of FLOATVAL, STRING
and PMC constants in the respective constant tables.
=cut
*/
METHOD constant_counts() {
PackFile_ConstTable * const ct = get_const_table(INTERP, SELF);
if (!ct)
Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_UNEXPECTED_NULL,
"NULL or Malformed Packfile");
else {
PMC * const counts = Parrot_pmc_new(INTERP, enum_class_FixedIntegerArray);
VTABLE_set_integer_native(INTERP, counts, 3);
VTABLE_set_integer_keyed_int(INTERP, counts, 0, ct->num.const_count);
VTABLE_set_integer_keyed_int(INTERP, counts, 1, ct->str.const_count);
VTABLE_set_integer_keyed_int(INTERP, counts, 2, ct->pmc.const_count);
RETURN(PMC *counts);
}
}
/*
=item C<METHOD main_sub()>
Get the :main sub from the packfile, if any.
=item C<METHOD trigger(STRING *flag)>
Trigger subs with the given flag. Currently supported values are "load" and
"init".
=item C<METHOD all_subs()>
Return a ResizablePMCArray with all Sub constants from the constant table.
=cut
*/
METHOD main_sub() {
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
if (attrs->pf == NULL)
RETURN(PMC *PMCNULL);
else {
PMC * const main_sub = Parrot_pf_get_packfile_main_sub(INTERP, SELF);
RETURN(PMC *main_sub);
}
}
METHOD subs_by_flag(STRING *flag) {
PMC * const subs = Parrot_pf_subs_by_flag(INTERP, SELF, flag);
RETURN(PMC *subs);
}
METHOD all_subs() {
PMC * const array = Parrot_pmc_new(INTERP, enum_class_ResizablePMCArray);
PackFile_ConstTable * const ct = get_const_table(INTERP, SELF);
if (ct) {
INTVAL i;
STRING * const SUB = CONST_STRING(interp, "Sub");
for (i = 0; i < ct->pmc.const_count; ++i) {
PMC * const x = ct->pmc.constants[i];
if (VTABLE_isa(interp, x, SUB))
VTABLE_push_pmc(INTERP, array, x);
}
}
RETURN(PMC *array);
}
/*
=item C<METHOD serialized_size()>
Return the size, in bytes, for how large this packfile will be if serialized.
=item C<METHOD serialize()>
Serialize the packfile into a STRING buffer
=item C<METHOD deserialize(STRING *pbc)>
Read a PackFile* in from a STRING buffer and set it as the current PackFile*
ptr in this PMC. Subject to the normal set_pointer restrictions.
=cut
*/
METHOD serialized_size() {
INTVAL size;
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
if (attrs->pf == NULL)
RETURN(INTVAL 0);
size = Parrot_pf_serialized_size(INTERP, attrs->pf);
RETURN(INTVAL size);
}
METHOD serialize() {
STRING *str;
Parrot_PackfileView_attributes * const attrs =
PARROT_PACKFILEVIEW(SELF);
if (attrs->pf == NULL)
RETURN(STRING *STRINGNULL);
str = Parrot_pf_serialize(INTERP, attrs->pf);
RETURN(STRING *str);
}
METHOD deserialize(STRING *pbc) {
PackFile * const pf = Parrot_pf_deserialize(INTERP, pbc);
VTABLE_set_pointer(INTERP, SELF, pf);
}
/*
=item C<METHOD read_from_file(STRING *filename)>
Read a PackFile* from the given .pbc file and set it as the current PackFile*
pointer in this PMC. Subject to the same restrictions as set_pointer.
=item C<METHOD write_to_file(STRING *filename)>
Serialize the contents of the PackFile in this PMC and write them out to the
given .pbc bytecode file.
=cut
*/
METHOD read_from_file(STRING * filename) {
PackFile * const pf = Parrot_pf_read_pbc_file(INTERP, filename);
VTABLE_set_pointer(INTERP, SELF, pf);
}
METHOD write_to_file(STRING *filename) {
Parrot_pf_write_pbc_file(INTERP, SELF, filename);
}
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/