Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 360 lines (250 sloc) 9.419 kb
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
1 /*
48311a5 @petdance teeny consting
petdance authored
2 Copyright (C) 2007-2009, Parrot Foundation.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
3 $Id$
4
5 =head1 NAME
6
7 src/pmc/exporter.pmc - Export globals from one namespace to another
8
abc4db2 @particle [pmc]: exporter api changes
particle authored
9 =head1 SYNOPSIS
10
11 You can use Exporter in PIR to import subs from a library. At its simplest:
12
13 .sub main :main
14 load_bytecode 'Test/More.pir'
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
15
bdc07b9 @chromatic [PMC] Add Exporter documentation into POD (Pabellon, RT #44487).
chromatic authored
16 .local pmc exporter, src_ns, dest_ns
17 src_ns = get_namespace [ 'Test'; 'More' ]
abc4db2 @particle [pmc]: exporter api changes
particle authored
18 exporter = new 'Exporter'
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
19
bdc07b9 @chromatic [PMC] Add Exporter documentation into POD (Pabellon, RT #44487).
chromatic authored
20 exporter.'import'( src_ns :named('source') 'plan ok' :named('globals') )
abc4db2 @particle [pmc]: exporter api changes
particle authored
21 plan(1)
22 ok(1, 'exporter has imported the requested functions')
23 .end
24
bdc07b9 @chromatic [PMC] Add Exporter documentation into POD (Pabellon, RT #44487).
chromatic authored
25 You can also specify the exporter attributes before making the import call,
26 for example to import into the alternate namespace 'Foo' you could use the
27 following code:
28
29 src_ns = get_namespace [ 'Test'; 'More' ]
30 dest_ns = get_namespace [ 'Foo' ]
31 exporter.'source'(src_ns)
32 exporter.'destination'(dest_ns)
33 exporter.'import'('plan ok' :named('globals'))
34
35 You can even import subroutines under different names if globals is a hash:
36
37 globals = new 'Hash'
38 globals['plan'] = 'steps'
39 globals['ok'] = 'passed'
40 exporter.'import'(globals :named('globals'))
41 steps(1)
42 passed(1)
43
abc4db2 @particle [pmc]: exporter api changes
particle authored
44
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
45 =head1 DESCRIPTION
46
b29ad29 @particle [pmc] Exporter updates
particle authored
47 Exports globals from one namespace to another. Exporter always uses
6498378 @particle [pmc]: update Exporter documentation based on draft guidelines
particle authored
48 the typed namespace interface, as outlined in
49 F<docs/pdds/pdd21_namespaces.pod>.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
50
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
51 Exporter is not derived from any other PMC, and does not provide any
abc4db2 @particle [pmc]: exporter api changes
particle authored
52 vtable interface--its interface consists solely of non-vtable methods.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
53
54 =head2 Structure
55
56 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
57
58 =over 4
59
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
60 =item C<ns_src>
61
62 The source namespace -- a NameSpace PMC.
5b556ee @particle [pmc]: Exporter is complete
particle authored
63 A Null PMC is allocated during initialization.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
64
65 =item C<ns_dest>
66
67 The destination namespace -- a NameSpace PMC.
4b47d4c @particle [pmc]: Exporter
particle authored
68 A PMC representing the current namespace is allocated upon initialization.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
69
70 =item C<globals>
71
bdc07b9 @chromatic [PMC] Add Exporter documentation into POD (Pabellon, RT #44487).
chromatic authored
72 The globals to export -- a PMC that implements a hash, an array, a String
73 containing a list of space-separated subroutine names or Null.
74
b29ad29 @particle [pmc] Exporter updates
particle authored
75 A Null PMC is allocated during initialization.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
76
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
77 =cut
78
79 */
80
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
81 /*
82
83 =back
84
85 =head2 Functions
86
87 =over 4
88
89 =cut
90
91 */
92
6b81760 @Whiteknight [pmc] we don't need need_ext anymore, so BALEETED
Whiteknight authored
93 pmclass Exporter auto_attrs {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
94
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
95 ATTR PMC *ns_src;
96 ATTR PMC *ns_dest;
97 ATTR PMC *globals;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
98
99 /*
100
101 =item C<void init()>
102
b54e3d5 @particle [pmc]: documentation updates
particle authored
103 Initializes an Exporter PMC.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
104
105 =cut
106
107 */
108
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
109 VTABLE void init() {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
110 /* Set up the object. */
ac99f44 @NotFound [cage] full use of SET/GET ATTR in exporter PMC
NotFound authored
111 SET_ATTR_ns_src(INTERP, SELF, PMCNULL);
540d4c3 @bacek Merge context_pmc3 branch into trunk.
bacek authored
112 SET_ATTR_ns_dest(INTERP, SELF, Parrot_pcc_get_namespace(INTERP, CURRENT_CONTEXT(INTERP)));
ac99f44 @NotFound [cage] full use of SET/GET ATTR in exporter PMC
NotFound authored
113 SET_ATTR_globals(INTERP, SELF, PMCNULL);
366638e @particle [pmc]: set pmc flags after anchoring to prevent accidental collection…
particle authored
114
1997c49 @cotto [gc] remove/replace DOD references in core PMCs
cotto authored
115 /* Set flags for custom GC mark and destroy. */
366638e @particle [pmc]: set pmc flags after anchoring to prevent accidental collection…
particle authored
116 PObj_custom_mark_SET(SELF);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
117 }
118
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
119 /*
120
121 =item C<void mark()>
122
123 Mark referenced strings and PMCs in the structure as live.
124
125 =cut
126
127 */
128
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
129 VTABLE void mark() {
ac99f44 @NotFound [cage] full use of SET/GET ATTR in exporter PMC
NotFound authored
130 PMC *ns_src;
131 PMC *ns_dest;
132 PMC *globals;
e8c1290 @NotFound [cage] codingstd hard tabs, mikehh++
NotFound authored
133 GET_ATTR_ns_src(INTERP, SELF, ns_src);
134 GET_ATTR_ns_dest(INTERP, SELF, ns_dest);
135 GET_ATTR_globals(INTERP, SELF, globals);
ac99f44 @NotFound [cage] full use of SET/GET ATTR in exporter PMC
NotFound authored
136
18dd147 @NotFound [gc] add new mark_... _alive functions, use it in a lot of mark vtabl…
NotFound authored
137 Parrot_gc_mark_PMC_alive(interp, ns_src);
138 Parrot_gc_mark_PMC_alive(interp, ns_dest);
139 Parrot_gc_mark_PMC_alive(interp, globals);
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
140 }
141
142
143 /*
144
145 =back
146
147 =head2 Methods
148
149 =over 4
150
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
151 =item C<METHOD
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
152 source(PMC *src :optional, int got_src :opt_flag)>
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
153
154 Accessor for the source NameSpace object (C<ns_src>.)
155 Sets the value if C<src> is passed, otherwise returns the value.
156 Throws an exception if a non-NameSpace PMC is passed.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
157
158 =cut
159
160 */
161
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
162 METHOD source(PMC *src :optional, int got_src :opt_flag) {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
163 if (got_src) {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
164 if (src->vtable->base_type != enum_class_NameSpace)
165 Parrot_ex_throw_from_c_args(interp, NULL, 0,
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
166 "source must be a NameSpace PMC");
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
167
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
168 SET_ATTR_ns_src(interp, SELF, src);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
169 }
170 else {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
171 PMC *tmp_ns_src;
172 GET_ATTR_ns_src(interp, SELF, tmp_ns_src);
173 RETURN(PMC *tmp_ns_src);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
174 }
175 }
176
177
178 /*
179
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
180 =item C<METHOD
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
181 destination(PMC *dest :optional, int got_dest :opt_flag)>
9318910 @particle [pmc] Exporter updates
particle authored
182
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
183 Accessor for the destination NameSpace object (C<ns_dest>.)
184 Sets the value if C<dest> is passed, otherwise returns the value.
185 Throws an exception if a non-NameSpace PMC is passed.
9318910 @particle [pmc] Exporter updates
particle authored
186
187 =cut
188
189 */
190
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
191 METHOD destination(PMC *dest :optional, int got_dest :opt_flag) {
9318910 @particle [pmc] Exporter updates
particle authored
192 if (got_dest) {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
193
194 if (dest->vtable->base_type != enum_class_NameSpace)
195 Parrot_ex_throw_from_c_args(interp, NULL, 0,
196 "destination must be a NameSpace PMC");
197
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
198 SET_ATTR_ns_dest(interp, SELF, dest);
9318910 @particle [pmc] Exporter updates
particle authored
199 }
200 else {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
201 PMC *tmp_ns_dest;
202 GET_ATTR_ns_dest(interp, SELF, tmp_ns_dest);
203 RETURN(PMC *tmp_ns_dest);
9318910 @particle [pmc] Exporter updates
particle authored
204 }
205 }
206
207
208 /*
209
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
210 =item C<METHOD
b29ad29 @particle [pmc] Exporter updates
particle authored
211 globals(PMC *glb :optional, int got_glb :opt_flag)>
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
212
abc4db2 @particle [pmc]: exporter api changes
particle authored
213 Accessor for the globals to export (C<globals>.)
214 Sets the value if C<glb> is passed, otherwise returns the value.
215 If C<glb> is a String, it is split on ascii whitespace, and each array member
216 is added as a hash key.
217 If C<glb> implements the array interface, each member is added as a hash key.
218 if C<glb> implements the hash interface, it is assigned to Exporter's
219 C<globals> attribute.
220 Throws an exception if an unknown PMC type is passed.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
221
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
222 =cut
223
224 */
225
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
226 METHOD globals(PMC *glb :optional, int got_glb :opt_flag) {
0006ce7 @chromatic [src] Fixed all CONST_STRING macros so that they don't break across l…
chromatic authored
227 STRING * const s_str = CONST_STRING(interp, "String");
228 STRING * const s_arr = CONST_STRING(interp, "array");
229 STRING * const s_hash = CONST_STRING(interp, "hash");
230 STRING * const s_space = CONST_STRING(interp, " ");
9318910 @particle [pmc] Exporter updates
particle authored
231
b29ad29 @particle [pmc] Exporter updates
particle authored
232 if (got_glb) {
abc4db2 @particle [pmc]: exporter api changes
particle authored
233 STRING * const s_empty = CONST_STRING(interp, "");
234 PMC *temp_globals = pmc_new(interp, enum_class_Hash);
235
236 if (PMC_IS_NULL(glb)) {
237 temp_globals = PMCNULL;
238 }
239 else if (VTABLE_isa(interp, glb, s_str) || (VTABLE_does(interp, glb, s_arr))) {
240 PMC *glb_array;
241 INTVAL n, i;
242
243 if (VTABLE_isa(interp, glb, s_str))
b6bc0c3 @allisonrandal [pdd28str] Merging the pdd27mmd branch into trunk for r36123 to r36318.
allisonrandal authored
244 glb_array = Parrot_str_split(interp, s_space,
0006ce7 @chromatic [src] Fixed all CONST_STRING macros so that they don't break across l…
chromatic authored
245 VTABLE_get_string(interp, glb));
abc4db2 @particle [pmc]: exporter api changes
particle authored
246 else
247 glb_array = glb;
248
249 n = VTABLE_elements(interp, glb_array);
250
251 if (n == 0)
252 temp_globals = PMCNULL;
253
6ab1a64 @paultcochrane [core] Corrected spacing of keywords wrt parentheses as per coding st…
paultcochrane authored
254 for (i = 0; i < n; i++) {
abc4db2 @particle [pmc]: exporter api changes
particle authored
255 STRING * const item = VTABLE_get_string_keyed_int(interp, glb_array, i);
256 VTABLE_set_string_keyed_str(interp, temp_globals, item, s_empty);
257 }
258 }
259 else if (VTABLE_does(interp, glb, s_hash)) {
260 if (VTABLE_elements(interp, glb) == 0)
261 temp_globals = PMCNULL;
262 else
263 temp_globals = glb;
264 }
265 else {
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
266 Parrot_ex_throw_from_c_args(interp, NULL, 0,
abc4db2 @particle [pmc]: exporter api changes
particle authored
267 "Invalid type %d in globals()", glb->vtable->base_type);
268 }
269
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
270 SET_ATTR_globals(interp, SELF, temp_globals);
b29ad29 @particle [pmc] Exporter updates
particle authored
271 }
272 else {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
273 PMC *tmp_globals;
274 GET_ATTR_globals(interp, SELF, tmp_globals);
275 if (PMC_IS_NULL(tmp_globals)) {
276 RETURN(PMC *PMCNULL);
b29ad29 @particle [pmc] Exporter updates
particle authored
277 }
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
278 else
279 RETURN(PMC *tmp_globals);
9318910 @particle [pmc] Exporter updates
particle authored
280 }
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
281 }
282
283
284 /*
285
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
286 =item C<METHOD
37922cf @chromatic [lib/PMC] Modified :named parameter syntax in METHODs to match PDD03,…
chromatic authored
287 import(PMC *dest :optional :named("destination"), int got_dest :opt_flag,
288 PMC *src :optional :named("source"), int got_src :opt_flag,
289 PMC *globals :optional :named("globals"), int got_globals :opt_flag)>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
290
291 Import C<globals> from the C<src> namespace to the C<dest> namespace.
abc4db2 @particle [pmc]: exporter api changes
particle authored
292 If C<src>, C<dest>, or C<globals> are passed, they override the current value.
b29ad29 @particle [pmc] Exporter updates
particle authored
293 C<import> follows the semantics of the C<export_to> method
294 of the C<NameSpace> PMC. in particular, if a NULL value is passed
295 for C<globals>, the default set of items will be imported.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
296 Throws an exception upon error.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
297
298 =cut
299
300 */
301
37922cf @chromatic [lib/PMC] Modified :named parameter syntax in METHODs to match PDD03,…
chromatic authored
302 METHOD import(PMC *dest :optional :named("destination"),
303 int got_dest :opt_flag,
304 PMC *src :optional :named("source"),
305 int got_src :opt_flag,
306 PMC *globals :optional :named("globals"),
307 int got_globals :opt_flag) {
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
308
b29ad29 @particle [pmc] Exporter updates
particle authored
309 PMC *ns_src, *ns_dest, *ns_globals;
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
310
311 if (got_src)
312 PCCINVOKE(interp, SELF, "source", PMC *src);
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
313
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
314 if (got_dest)
315 PCCINVOKE(interp, SELF, "destination", PMC *dest);
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
316
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
317 if (got_globals)
318 PCCINVOKE(interp, SELF, "globals", PMC *globals);
9318910 @particle [pmc] Exporter updates
particle authored
319
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
320 GET_ATTR_ns_src(interp, SELF, ns_src);
321 if (PMC_IS_NULL(ns_src))
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
322 Parrot_ex_throw_from_c_args(interp, NULL, 0, "source namespace not set");
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
323
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
324 GET_ATTR_ns_dest(interp, SELF, ns_dest);
325 if (PMC_IS_NULL(ns_dest))
2255840 @allisonrandal [pdd17pmc] Merging the pdd25cx branch into trunk for r26555 to r29949.
allisonrandal authored
326 Parrot_ex_throw_from_c_args(interp, NULL, 0,
327 "destination namespace not set");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
328
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
329 GET_ATTR_globals(interp, SELF, ns_globals);
b29ad29 @particle [pmc] Exporter updates
particle authored
330
331 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
332 }
333
334
b29ad29 @particle [pmc] Exporter updates
particle authored
335 } /* end pmclass Exporter */
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
336
337 /*
338
339 =back
340
6498378 @particle [pmc]: update Exporter documentation based on draft guidelines
particle authored
341 =head1 STABILITY
342
343 Unstable. This PMC is under active development; major portions of the
344 interface have not yet been completed.
345
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
346 =head1 SEE ALSO
347
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
348 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
349
350 =cut
351
352 */
353
354 /*
355 * Local variables:
356 * c-file-style: "parrot"
357 * End:
358 * vim: expandtab shiftwidth=4:
359 */
Something went wrong with that request. Please try again.