Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 330 lines (223 sloc) 7.375 kb
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
1 /*
2 Copyright (C) 2007, The Perl Foundation.
3 $Id$
4
5 =head1 NAME
6
7 src/pmc/exporter.pmc - Export globals from one namespace to another
8
9 =head1 DESCRIPTION
10
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
11 Exports globals from one namespace to another.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
12
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
13 Exporter is not derived from any other PMC, and does not provide any
14 standard interface--its inteface consists solely of non-vtable methods.
15
16 =head2 Structure
17
18 The Exporter PMC structure (C<Parrot_Exporter>) consists of three items:
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
19
20 =over 4
21
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
22 =item C<ns_src>
23
24 The source namespace -- a NameSpace PMC.
25 An empty PMC of this type is allocated upon initialization.
26
27 =item C<ns_dest>
28
29 The destination namespace -- a NameSpace PMC.
30 An empty PMC of this type is allocated upon initialization.
31
32 =item C<globals>
33
34 The globals to export -- a ResizableStringArray.
35 An empty PMC of this type is allocated during initialization.
36
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
37 =cut
38
39 */
40
41 #include "parrot/parrot.h"
42 #define PARROT_EXPORTER(e) ((Parrot_Exporter *) PMC_data(e))
43
44 typedef struct Parrot_Exporter {
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
45 PMC *ns_src;
46 PMC *ns_dest;
47 PMC *globals;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
48 } Parrot_Exporter;
49
50
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
51 /*
52
53 =back
54
55 =head2 Functions
56
57 =over 4
58
59 =cut
60
61 */
62
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
63 pmclass Exporter
64 need_ext {
65
66
67 /*
68
69 =item C<void init()>
70
71 Instantiates an Exporter.
72
73 =cut
74
75 */
76
77 void init() {
78 Parrot_Exporter *exp = NULL;
79
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
80 /* Set flags for custom DOD mark and destroy. */
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
81 PObj_custom_mark_SET(SELF);
82 PObj_active_destroy_SET(SELF);
83
84 /* Set up the object. */
85 exp = mem_sys_allocate_zeroed(sizeof(Parrot_Exporter));
86 exp->ns_src = pmc_new(interp, enum_class_NameSpace);
87 exp->ns_dest = pmc_new(interp, enum_class_NameSpace);
88 exp->globals = pmc_new(interp, enum_class_ResizableStringArray);
89 PMC_data(SELF) = exp;
90 }
91
92
93 /*
94
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
95 =item C<void destroy()>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
96
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
97 Free the object's underlying struct.
98
99 =cut
100
101 */
102
103 void destroy() {
104 mem_sys_free(PMC_data(SELF));
105 }
106
107
108 /*
109
110 =item C<void mark()>
111
112 Mark referenced strings and PMCs in the structure as live.
113
114 =cut
115
116 */
117
118 void mark() {
119 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
120 if (exp->ns_src)
121 pobject_lives(interp, (PObj*)exp->ns_src);
122 if (exp->ns_dest)
123 pobject_lives(interp, (PObj*)exp->ns_dest);
124 if (exp->globals)
125 pobject_lives(interp, (PObj*)exp->globals);
126 }
127
128
129 /*
130
131 =back
132
133 =head2 Methods
134
135 =over 4
136
137 =item C<PCCMETHOD void
138 source(PMC *src :optional, int got_src :opt_flag)>
139
140 Accessor for the source NameSpace object (C<ns_src>.)
141 Sets the value if C<src> is passed, otherwise returns the value.
142 Throws an exception if a non-NameSpace PMC is passed.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
143
144 =cut
145
146 */
147
9318910 @particle [pmc] Exporter updates
particle authored
148 PCCMETHOD void source(PMC *src :optional, int got_src :opt_flag) {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
149 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
150
151 /* TODO deal with non-namespace pmcs */
152 if (got_src) {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
153 if (src->vtable->base_type != enum_class_NameSpace) {
154 real_exception(interp, NULL, 0,
155 "source must be a NameSpace PMC");
156 return;
157 }
158
159 exp->ns_src = src;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
160 }
161 else {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
162 PMC *tmp_ns_src;
163 tmp_ns_src = exp->ns_src;
164 PCCRETURN(PMC *tmp_ns_src);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
165 }
166 }
167
168
169 /*
170
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
171 =item C<PCCMETHOD void
172 destination(PMC *dest :optional, int got_dest :opt_flag)>
9318910 @particle [pmc] Exporter updates
particle authored
173
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
174 Accessor for the destination NameSpace object (C<ns_dest>.)
175 Sets the value if C<dest> is passed, otherwise returns the value.
176 Throws an exception if a non-NameSpace PMC is passed.
9318910 @particle [pmc] Exporter updates
particle authored
177
178 =cut
179
180 */
181
182 PCCMETHOD void destination(PMC *dest :optional, int got_dest :opt_flag) {
183 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
184
185 /* TODO deal with non-namespace pmcs */
186 if (got_dest) {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
187 if (dest->vtable->base_type != enum_class_NameSpace) {
188 real_exception(interp, NULL, 0,
189 "destination must be a NameSpace PMC");
190 return;
191 }
192 exp->ns_dest = dest;
9318910 @particle [pmc] Exporter updates
particle authored
193 }
194 else {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
195 PMC *tmp_ns_dest;
196 tmp_ns_dest = exp->ns_dest;
197 PCCRETURN(PMC *tmp_ns_dest);
9318910 @particle [pmc] Exporter updates
particle authored
198 }
199 }
200
201
202 /*
203
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
204 =item C<PCCMETHOD void
205 globals(PMC *glb_array :optional, int got_glb_array :opt_flag)>
206
207 Accessor for the array of globals to export (C<globals>.)
208 Sets the array if C<glb_array> is passed, otherwise returns the value.
209
210 TODO: need to add some type checking code.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
211
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
212 TODO: does not deal with non-array PMCs yet.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
213
214 =cut
215
216 */
217
9318910 @particle [pmc] Exporter updates
particle authored
218 PCCMETHOD void globals(PMC *glb_array :optional, int got_glb_array :opt_flag) {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
219 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
9318910 @particle [pmc] Exporter updates
particle authored
220 PMC *ret_globals;
221
222 /* TODO deal properly with non-array pmcs */
223 if (got_glb_array) {
224 exp->globals = VTABLE_clone(interp, glb_array);
225 }
226 else {
227 ret_globals = VTABLE_clone(interp, exp->globals);
228 PCCRETURN(PMC *ret_globals);
229 }
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
230 }
231
232
233 /*
234
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
235 =item C<PCCMETHOD void
236 add_global(PMC *global :optional, int has_global :opt_flag)>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
237
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
238 Add C<global> to the array of globals (C<globals>.)
239 Sets the array if C<glb_array> is passed, otherwise does nothing.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
240
241 =cut
242
243 */
244
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
245 PCCMETHOD void add_global(PMC *global :optional, int has_global :opt_flag) {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
246 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
247 if (has_global) {
248 VTABLE_push_string(interp, exp->globals,
249 VTABLE_get_string(interp, global));
250 }
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
251 }
252
253
254 /*
255
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
256 =item C<PCCMETHOD void
257 import(PMC *dest :optional :named["destination"], int got_dest :opt_flag,
258 PMC *src :optional :named["source"], int got_src :opt_flag,
259 PMC *globals :optional :named["globals"], int got_globals :opt_flag)>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
260
261 Import C<globals> from the C<src> namespace to the C<dest> namespace.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
262 If C<src>, C<dest>, or C<globals> are passed, they will override
263 the current value.
264 Throws an exception upon error.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
265
266 =cut
267
268 */
269
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
270 PCCMETHOD void import(PMC *dest :optional :named["destination"], int got_dest :opt_flag,
271 PMC *src :optional :named["source"], int got_src :opt_flag,
272 PMC *globals :optional :named["globals"], int got_globals :opt_flag) {
273 /*
274 * notes:
275 * for each global,
276 * find global in source namespace, throw exception if not found
277 * find global in destination namespace, throw warning if found
278 * store global in destination namespace
279 */
280
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
281 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
282 int copy_of_globals;
283
284 if (got_src)
285 PCCINVOKE(interp, SELF, "source", PMC *src);
286 if (got_dest)
287 PCCINVOKE(interp, SELF, "destination", PMC *dest);
288 if (got_globals)
289 PCCINVOKE(interp, SELF, "globals", PMC *globals);
9318910 @particle [pmc] Exporter updates
particle authored
290
291 if (exp->globals == PMCNULL) {
292 real_exception(interp, NULL, 0, "no globals to import");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
293 return;
9318910 @particle [pmc] Exporter updates
particle authored
294 }
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
295 if (exp->ns_src == PMCNULL) {
9318910 @particle [pmc] Exporter updates
particle authored
296 real_exception(interp, NULL, 0, "source namespace not set");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
297 return;
9318910 @particle [pmc] Exporter updates
particle authored
298 }
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
299 if (exp->ns_dest == PMCNULL) {
9318910 @particle [pmc] Exporter updates
particle authored
300 real_exception(interp, NULL, 0, "destination namespace not set");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
301 return;
9318910 @particle [pmc] Exporter updates
particle authored
302 }
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
303
304 copy_of_globals = VTABLE_get_integer(interp, exp->globals);
305 return;
306 /* TODO for each global, look up in source and alias to dest */
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
307 }
308
309
310 }
311
312 /*
313
314 =back
315
316 =head1 SEE ALSO
317
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
318 F<docs/pdds/pdd17_basic_types.pod>, F<docs/pdds/pdd21_namespaces.pod>.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
319
320 =cut
321
322 */
323
324 /*
325 * Local variables:
326 * c-file-style: "parrot"
327 * End:
328 * vim: expandtab shiftwidth=4:
329 */
Something went wrong with that request. Please try again.