Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 379 lines (262 sloc) 9.651 kb
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
1 /*
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
2 Copyright (C) 2007-2008, The Perl 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
81 #include "parrot/parrot.h"
82
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
83 /*
84
85 =back
86
87 =head2 Functions
88
89 =over 4
90
91 =cut
92
93 */
94
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
95 pmclass Exporter need_ext {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
96
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
97 ATTR PMC *ns_src;
98 ATTR PMC *ns_dest;
99 ATTR PMC *globals;
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
100
101 /*
102
103 =item C<void init()>
104
b54e3d5 @particle [pmc]: documentation updates
particle authored
105 Initializes an Exporter PMC.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
106
107 =cut
108
109 */
110
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
111 VTABLE void init() {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
112 Parrot_Exporter *exp = NULL;
113
114 /* Set up the object. */
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
115 exp = mem_allocate_zeroed_typed(Parrot_Exporter);
116 exp->ns_src = PMCNULL;
117 exp->ns_dest = CONTEXT(interp->ctx)->current_namespace;
118 exp->globals = PMCNULL;
119 PMC_data(SELF) = exp;
366638e @particle [pmc]: set pmc flags after anchoring to prevent accidental collection du...
particle authored
120
121 /* Set flags for custom DOD mark and destroy. */
122 PObj_custom_mark_SET(SELF);
123 PObj_active_destroy_SET(SELF);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
124 }
125
126
127 /*
128
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
129 =item C<void destroy()>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
130
b54e3d5 @particle [pmc]: documentation updates
particle authored
131 Free the memory associated with the object's underlying struct.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
132
133 =cut
134
135 */
136
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
137 VTABLE void destroy() {
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
138 mem_sys_free(PMC_data(SELF));
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
139 }
140
141
142 /*
143
144 =item C<void mark()>
145
146 Mark referenced strings and PMCs in the structure as live.
147
148 =cut
149
150 */
151
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
152 VTABLE void mark() {
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
153 Parrot_Exporter *exp = PARROT_EXPORTER(SELF);
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
154
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
155 if (exp->ns_src)
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
156 pobject_lives(interp, (PObj *)exp->ns_src);
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
157 if (exp->ns_dest)
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
158 pobject_lives(interp, (PObj *)exp->ns_dest);
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
159 if (exp->globals)
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
160 pobject_lives(interp, (PObj *)exp->globals);
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
161 }
162
163
164 /*
165
166 =back
167
168 =head2 Methods
169
170 =over 4
171
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
172 =item C<METHOD
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
173 source(PMC *src :optional, int got_src :opt_flag)>
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
174
175 Accessor for the source NameSpace object (C<ns_src>.)
176 Sets the value if C<src> is passed, otherwise returns the value.
177 Throws an exception if a non-NameSpace PMC is passed.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
178
179 =cut
180
181 */
182
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
183 METHOD source(PMC *src :optional, int got_src :opt_flag) {
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
184 if (got_src) {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
185 if (src->vtable->base_type != enum_class_NameSpace) {
186 real_exception(interp, NULL, 0,
187 "source must be a NameSpace PMC");
188 }
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
189 SET_ATTR_ns_src(interp, SELF, src);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
190 }
191 else {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
192 PMC *tmp_ns_src;
193 GET_ATTR_ns_src(interp, SELF, tmp_ns_src);
194 RETURN(PMC *tmp_ns_src);
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
195 }
196 }
197
198
199 /*
200
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
201 =item C<METHOD
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
202 destination(PMC *dest :optional, int got_dest :opt_flag)>
9318910 @particle [pmc] Exporter updates
particle authored
203
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
204 Accessor for the destination NameSpace object (C<ns_dest>.)
205 Sets the value if C<dest> is passed, otherwise returns the value.
206 Throws an exception if a non-NameSpace PMC is passed.
9318910 @particle [pmc] Exporter updates
particle authored
207
208 =cut
209
210 */
211
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
212 METHOD destination(PMC *dest :optional, int got_dest :opt_flag) {
9318910 @particle [pmc] Exporter updates
particle authored
213 if (got_dest) {
ac3ceb6 @particle [pmc] finish Exporter's 'source' and 'destination' methods
particle authored
214 if (dest->vtable->base_type != enum_class_NameSpace) {
215 real_exception(interp, NULL, 0,
216 "destination must be a NameSpace PMC");
217 }
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
218 SET_ATTR_ns_dest(interp, SELF, dest);
9318910 @particle [pmc] Exporter updates
particle authored
219 }
220 else {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
221 PMC *tmp_ns_dest;
222 GET_ATTR_ns_dest(interp, SELF, tmp_ns_dest);
223 RETURN(PMC *tmp_ns_dest);
9318910 @particle [pmc] Exporter updates
particle authored
224 }
225 }
226
227
228 /*
229
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
230 =item C<METHOD
b29ad29 @particle [pmc] Exporter updates
particle authored
231 globals(PMC *glb :optional, int got_glb :opt_flag)>
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
232
abc4db2 @particle [pmc]: exporter api changes
particle authored
233 Accessor for the globals to export (C<globals>.)
234 Sets the value if C<glb> is passed, otherwise returns the value.
235 If C<glb> is a String, it is split on ascii whitespace, and each array member
236 is added as a hash key.
237 If C<glb> implements the array interface, each member is added as a hash key.
238 if C<glb> implements the hash interface, it is assigned to Exporter's
239 C<globals> attribute.
240 Throws an exception if an unknown PMC type is passed.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
241
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
242 =cut
243
244 */
245
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
246 METHOD globals(PMC *glb :optional, int got_glb :opt_flag) {
abc4db2 @particle [pmc]: exporter api changes
particle authored
247 STRING *s_str = CONST_STRING(interp, "String");
248 STRING *s_arr = CONST_STRING(interp, "array");
249 STRING *s_hash = CONST_STRING(interp, "hash");
9318910 @particle [pmc] Exporter updates
particle authored
250
b29ad29 @particle [pmc] Exporter updates
particle authored
251 if (got_glb) {
abc4db2 @particle [pmc]: exporter api changes
particle authored
252 STRING * const s_empty = CONST_STRING(interp, "");
253 PMC *temp_globals = pmc_new(interp, enum_class_Hash);
254
255 if (PMC_IS_NULL(glb)) {
256 temp_globals = PMCNULL;
257 }
258 else if (VTABLE_isa(interp, glb, s_str) || (VTABLE_does(interp, glb, s_arr))) {
259 PMC *glb_array;
260 INTVAL n, i;
261
262 if (VTABLE_isa(interp, glb, s_str))
263 glb_array = string_split(interp,
264 CONST_STRING(interp, " "),
265 VTABLE_get_string(interp, glb));
266 else
267 glb_array = glb;
268
269 n = VTABLE_elements(interp, glb_array);
270
271 if (n == 0)
272 temp_globals = PMCNULL;
273
6ab1a64 @paultcochrane [core] Corrected spacing of keywords wrt parentheses as per coding stand...
paultcochrane authored
274 for (i = 0; i < n; i++) {
abc4db2 @particle [pmc]: exporter api changes
particle authored
275 STRING * const item = VTABLE_get_string_keyed_int(interp, glb_array, i);
276 VTABLE_set_string_keyed_str(interp, temp_globals, item, s_empty);
277 }
278 }
279 else if (VTABLE_does(interp, glb, s_hash)) {
280 if (VTABLE_elements(interp, glb) == 0)
281 temp_globals = PMCNULL;
282 else
283 temp_globals = glb;
284 }
285 else {
286 real_exception(interp, NULL, 0,
287 "Invalid type %d in globals()", glb->vtable->base_type);
288 }
289
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
290 SET_ATTR_globals(interp, SELF, temp_globals);
b29ad29 @particle [pmc] Exporter updates
particle authored
291 }
292 else {
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
293 PMC *tmp_globals;
294 GET_ATTR_globals(interp, SELF, tmp_globals);
295 if (PMC_IS_NULL(tmp_globals)) {
296 RETURN(PMC *PMCNULL);
b29ad29 @particle [pmc] Exporter updates
particle authored
297 }
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
298 else
299 RETURN(PMC *tmp_globals);
9318910 @particle [pmc] Exporter updates
particle authored
300 }
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
301 }
302
303
304 /*
305
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
306 =item C<METHOD
37922cf @chromatic [lib/PMC] Modified :named parameter syntax in METHODs to match PDD03, pe...
chromatic authored
307 import(PMC *dest :optional :named("destination"), int got_dest :opt_flag,
308 PMC *src :optional :named("source"), int got_src :opt_flag,
309 PMC *globals :optional :named("globals"), int got_globals :opt_flag)>
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
310
311 Import C<globals> from the C<src> namespace to the C<dest> namespace.
abc4db2 @particle [pmc]: exporter api changes
particle authored
312 If C<src>, C<dest>, or C<globals> are passed, they override the current value.
b29ad29 @particle [pmc] Exporter updates
particle authored
313 C<import> follows the semantics of the C<export_to> method
314 of the C<NameSpace> PMC. in particular, if a NULL value is passed
315 for C<globals>, the default set of items will be imported.
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
316 Throws an exception upon error.
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
317
318 =cut
319
320 */
321
37922cf @chromatic [lib/PMC] Modified :named parameter syntax in METHODs to match PDD03, pe...
chromatic authored
322 METHOD import(PMC *dest :optional :named("destination"),
323 int got_dest :opt_flag,
324 PMC *src :optional :named("source"),
325 int got_src :opt_flag,
326 PMC *globals :optional :named("globals"),
327 int got_globals :opt_flag) {
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
328
b29ad29 @particle [pmc] Exporter updates
particle authored
329 PMC *ns_src, *ns_dest, *ns_globals;
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
330
331 if (got_src)
332 PCCINVOKE(interp, SELF, "source", PMC *src);
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
333
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
334 if (got_dest)
335 PCCINVOKE(interp, SELF, "destination", PMC *dest);
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
336
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
337 if (got_globals)
338 PCCINVOKE(interp, SELF, "globals", PMC *globals);
9318910 @particle [pmc] Exporter updates
particle authored
339
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
340 GET_ATTR_ns_src(interp, SELF, ns_src);
341 if (PMC_IS_NULL(ns_src))
9318910 @particle [pmc] Exporter updates
particle authored
342 real_exception(interp, NULL, 0, "source namespace not set");
f6d5c3e @chromatic [PMC] More minor cleanups and reformattings.
chromatic authored
343
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
344 GET_ATTR_ns_dest(interp, SELF, ns_dest);
345 if (PMC_IS_NULL(ns_dest))
9318910 @particle [pmc] Exporter updates
particle authored
346 real_exception(interp, NULL, 0, "destination namespace not set");
2f8fa82 @particle [pmc]: Exporter beginning 'import' method
particle authored
347
59661f1 @allisonrandal [pdd17pmc] Merging the pdd17pmc branch into trunk (r24435 to r26307).
allisonrandal authored
348 GET_ATTR_globals(interp, SELF, ns_globals);
b29ad29 @particle [pmc] Exporter updates
particle authored
349
350 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
351 }
352
353
b29ad29 @particle [pmc] Exporter updates
particle authored
354 } /* end pmclass Exporter */
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
355
356 /*
357
358 =back
359
6498378 @particle [pmc]: update Exporter documentation based on draft guidelines
particle authored
360 =head1 STABILITY
361
362 Unstable. This PMC is under active development; major portions of the
363 interface have not yet been completed.
364
9255c82 @paultcochrane [src] Set svn:keywords and svn:eol-style properties appropriately
paultcochrane authored
365 =head1 SEE ALSO
366
eae5b2f @particle [pmc]: documentation overhaul for Exporter
particle authored
367 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
368
369 =cut
370
371 */
372
373 /*
374 * Local variables:
375 * c-file-style: "parrot"
376 * End:
377 * vim: expandtab shiftwidth=4:
378 */
Something went wrong with that request. Please try again.