Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 346 lines (237 sloc) 8.173 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
b29ad29 @particle [pmc] Exporter updates
particle authored
11 Exports globals from one namespace to another. Exporter always uses
6498378 @particle [pmc]: update Exporter documentation based on draft guidelines
particle authored
12 the typed namespace interface, as outlined in
13 F<docs/pdds/pdd21_namespaces.pod>.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
14
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
15 Exporter is not derived from any other PMC, and does not provide any
16 standard interface--its inteface consists solely of non-vtable methods.
17
18 =head2 Structure
19
20 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
21
22 =over 4
23
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
24 =item C<ns_src>
25
26 The source namespace -- a NameSpace PMC.
5b556ee @particle [pmc]: Exporter is complete
particle authored
27 A Null PMC is allocated during initialization.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
28
29 =item C<ns_dest>
30
31 The destination namespace -- a NameSpace PMC.
4b47d4c @particle [pmc]: Exporter
particle authored
32 A PMC representing the current namespace is allocated upon initialization.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
33
34 =item C<globals>
35
36 The globals to export -- a ResizableStringArray.
b29ad29 @particle [pmc] Exporter updates
particle authored
37 A Null PMC is allocated during initialization.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
38
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
39 =cut
40
41 */
42
43 #include "parrot/parrot.h"
44 #define PARROT_EXPORTER(e) ((Parrot_Exporter *) PMC_data(e))
45
46 typedef struct Parrot_Exporter {
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
47 PMC *ns_src;
48 PMC *ns_dest;
49 PMC *globals;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
50 } Parrot_Exporter;
51
52
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
53 /*
54
55 =back
56
57 =head2 Functions
58
59 =over 4
60
61 =cut
62
63 */
64
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
65 pmclass Exporter
66 need_ext {
67
68
69 /*
70
71 =item C<void init()>
72
b54e3d5 @particle [pmc]: documentation updates
particle authored
73 Initializes an Exporter PMC.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
74
75 =cut
76
77 */
78
79 void init() {
80 Parrot_Exporter *exp = NULL;
81
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
82 /* Set flags for custom DOD mark and destroy. */
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
83 PObj_custom_mark_SET(SELF);
84 PObj_active_destroy_SET(SELF);
85
86 /* Set up the object. */
b54e3d5 @particle [pmc]: documentation updates
particle authored
87 exp = mem_sys_allocate_zeroed(sizeof (Parrot_Exporter));
88 exp->ns_src = PMCNULL;
89 exp->ns_dest = CONTEXT(interp->ctx)->current_namespace;
90 exp->globals = PMCNULL;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
91 PMC_data(SELF) = exp;
92 }
93
94
95 /*
96
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
97 =item C<void destroy()>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
98
b54e3d5 @particle [pmc]: documentation updates
particle authored
99 Free the memory associated with the object's underlying struct.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
100
101 =cut
102
103 */
104
105 void destroy() {
106 mem_sys_free(PMC_data(SELF));
107 }
108
109
110 /*
111
112 =item C<void mark()>
113
114 Mark referenced strings and PMCs in the structure as live.
115
116 =cut
117
118 */
119
120 void mark() {
121 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
122 if (exp->ns_src)
123 pobject_lives(interp, (PObj*)exp->ns_src);
124 if (exp->ns_dest)
125 pobject_lives(interp, (PObj*)exp->ns_dest);
126 if (exp->globals)
127 pobject_lives(interp, (PObj*)exp->globals);
128 }
129
130
131 /*
132
133 =back
134
135 =head2 Methods
136
137 =over 4
138
139 =item C<PCCMETHOD void
140 source(PMC *src :optional, int got_src :opt_flag)>
141
142 Accessor for the source NameSpace object (C<ns_src>.)
143 Sets the value if C<src> is passed, otherwise returns the value.
144 Throws an exception if a non-NameSpace PMC is passed.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
145
146 =cut
147
148 */
149
9318910 @particle [pmc] Exporter updates
particle authored
150 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
151 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
152
153 if (got_src) {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
154 if (src->vtable->base_type != enum_class_NameSpace) {
155 real_exception(interp, NULL, 0,
156 "source must be a NameSpace PMC");
157 return;
158 }
159
160 exp->ns_src = src;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
161 }
162 else {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
163 PMC *tmp_ns_src;
164 tmp_ns_src = exp->ns_src;
165 PCCRETURN(PMC *tmp_ns_src);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
166 }
167 }
168
169
170 /*
171
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
172 =item C<PCCMETHOD void
173 destination(PMC *dest :optional, int got_dest :opt_flag)>
9318910 @particle [pmc] Exporter updates
particle authored
174
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
175 Accessor for the destination NameSpace object (C<ns_dest>.)
176 Sets the value if C<dest> is passed, otherwise returns the value.
177 Throws an exception if a non-NameSpace PMC is passed.
9318910 @particle [pmc] Exporter updates
particle authored
178
179 =cut
180
181 */
182
183 PCCMETHOD void destination(PMC *dest :optional, int got_dest :opt_flag) {
184 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
185
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
b29ad29 @particle [pmc] Exporter updates
particle authored
205 globals(PMC *glb :optional, int got_glb :opt_flag)>
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
206
207 Accessor for the array of globals to export (C<globals>.)
b29ad29 @particle [pmc] Exporter updates
particle authored
208 Sets the array if C<glb> is passed, otherwise returns the value.
209 If C<glb> is a String, it is split on ascii whitespace.
210 If C<glb> is a FixedStringArray, it is cloned and set.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
211
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
212 =cut
213
214 */
215
b29ad29 @particle [pmc] Exporter updates
particle authored
216 PCCMETHOD void globals(PMC *glb :optional, int got_glb :opt_flag) {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
217 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
9318910 @particle [pmc] Exporter updates
particle authored
218 PMC *ret_globals;
4b47d4c @particle [pmc]: Exporter
particle authored
219 STRING *s_str = CONST_STRING(interp, "String");
187500a @particle [pmc]: remove some Exporter warnings -- jonathan++
particle authored
220 STRING *s_arr = CONST_STRING(interp, "array");
9318910 @particle [pmc] Exporter updates
particle authored
221
b29ad29 @particle [pmc] Exporter updates
particle authored
222 if (got_glb) {
4b47d4c @particle [pmc]: Exporter
particle authored
223 if (PMC_IS_NULL(glb))
b29ad29 @particle [pmc] Exporter updates
particle authored
224 exp->globals = PMCNULL;
4b47d4c @particle [pmc]: Exporter
particle authored
225 else if (VTABLE_isa(interp, glb, s_str))
187500a @particle [pmc]: remove some Exporter warnings -- jonathan++
particle authored
226 exp->globals = string_split(interp,
227 CONST_STRING(interp, " "),
228 VTABLE_get_string(interp, glb));
229 else if (VTABLE_does(interp, glb, s_arr))
4b47d4c @particle [pmc]: Exporter
particle authored
230 exp->globals = VTABLE_clone(interp, glb);
231 else
b29ad29 @particle [pmc] Exporter updates
particle authored
232 real_exception(interp, NULL, 0,
4b47d4c @particle [pmc]: Exporter
particle authored
233 "Invalid type %d in globals()", glb->vtable->base_type);
b29ad29 @particle [pmc] Exporter updates
particle authored
234 }
235 else {
236 if (PMC_IS_NULL(exp->globals)) {
237 PCCRETURN(PMC *PMCNULL);
238 }
239 else {
240 ret_globals = VTABLE_clone(interp, exp->globals);
241 PCCRETURN(PMC *ret_globals);
242 }
9318910 @particle [pmc] Exporter updates
particle authored
243 }
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
244 }
245
246
247 /*
248
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
249 =item C<PCCMETHOD void
250 add_global(PMC *global :optional, int has_global :opt_flag)>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
251
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
252 Add C<global> to the array of globals (C<globals>.)
4b47d4c @particle [pmc]: Exporter
particle authored
253 Sets the array if C<global> is passed, otherwise does nothing.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
254
255 =cut
256
257 */
258
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
259 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
260 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
261 if (has_global) {
b29ad29 @particle [pmc] Exporter updates
particle authored
262 if (PMC_IS_NULL(exp->globals))
263 exp->globals = pmc_new(interp, enum_class_ResizableStringArray);
264
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
265 VTABLE_push_string(interp, exp->globals,
266 VTABLE_get_string(interp, global));
267 }
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
268 }
269
270
271 /*
272
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
273 =item C<PCCMETHOD void
274 import(PMC *dest :optional :named["destination"], int got_dest :opt_flag,
275 PMC *src :optional :named["source"], int got_src :opt_flag,
276 PMC *globals :optional :named["globals"], int got_globals :opt_flag)>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
277
278 Import C<globals> from the C<src> namespace to the C<dest> namespace.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
279 If C<src>, C<dest>, or C<globals> are passed, they will override
280 the current value.
b29ad29 @particle [pmc] Exporter updates
particle authored
281 C<import> follows the semantics of the C<export_to> method
282 of the C<NameSpace> PMC. in particular, if a NULL value is passed
283 for C<globals>, the default set of items will be imported.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
284 Throws an exception upon error.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
285
286 =cut
287
288 */
289
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
290 PCCMETHOD void import(PMC *dest :optional :named["destination"], int got_dest :opt_flag,
291 PMC *src :optional :named["source"], int got_src :opt_flag,
292 PMC *globals :optional :named["globals"], int got_globals :opt_flag) {
293
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
294 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
b29ad29 @particle [pmc] Exporter updates
particle authored
295 PMC *ns_src, *ns_dest, *ns_globals;
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
296
297 if (got_src)
298 PCCINVOKE(interp, SELF, "source", PMC *src);
299 if (got_dest)
300 PCCINVOKE(interp, SELF, "destination", PMC *dest);
301 if (got_globals)
302 PCCINVOKE(interp, SELF, "globals", PMC *globals);
9318910 @particle [pmc] Exporter updates
particle authored
303
3939845 @particle [pmc]: Exporter code and test cleanups
particle authored
304 if (PMC_IS_NULL(exp->ns_src)) {
9318910 @particle [pmc] Exporter updates
particle authored
305 real_exception(interp, NULL, 0, "source namespace not set");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
306 return;
9318910 @particle [pmc] Exporter updates
particle authored
307 }
3939845 @particle [pmc]: Exporter code and test cleanups
particle authored
308 if (PMC_IS_NULL(exp->ns_dest)) {
9318910 @particle [pmc] Exporter updates
particle authored
309 real_exception(interp, NULL, 0, "destination namespace not set");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
310 return;
9318910 @particle [pmc] Exporter updates
particle authored
311 }
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
312
b29ad29 @particle [pmc] Exporter updates
particle authored
313 ns_src = exp->ns_src;
314 ns_dest = exp->ns_dest;
315 ns_globals = exp->globals;
316
317 PCCINVOKE(interp, ns_src, "export_to", PMC *ns_dest, PMC *ns_globals);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
318 }
319
320
b29ad29 @particle [pmc] Exporter updates
particle authored
321 } /* end pmclass Exporter */
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
322
323 /*
324
325 =back
326
6498378 @particle [pmc]: update Exporter documentation based on draft guidelines
particle authored
327 =head1 STABILITY
328
329 Unstable. This PMC is under active development; major portions of the
330 interface have not yet been completed.
331
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
332 =head1 SEE ALSO
333
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
334 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
335
336 =cut
337
338 */
339
340 /*
341 * Local variables:
342 * c-file-style: "parrot"
343 * End:
344 * vim: expandtab shiftwidth=4:
345 */
Something went wrong with that request. Please try again.