tagged release 0.6.4
[parrot.git] / docs / pdds / draft / pdd11_extending.pod
blobc232c506abbeb4f00ed326555a9914a54db0902a
1 # Copyright (C) 2001-2007, The Perl Foundation.
2 # $Id$
4 =head1 NAME
6 docs/pdds/pdd11_extending.pod - The Parrot Extension System
8 =head1 ABSTRACT
10 The extension API for Parrot is a simple, somewhat abstract, interface to
11 Parrot for code written in C or other compiled languages. It provides about
12 the same level of access to Parrot  that bytecode programs have.
14 =head1 DESCRIPTION
16 The API presents to C programs roughly the same interface presented to
17 bytecode programs--that is, a C extension can see everything that a bytecode
18 program can see, including Parrot's base architecture, registers, stacks, and
19 whatnot. This view isn't required, however, and often extension code won't
20 need or want to know what Parrot's internal structures look like. For this
21 reason the functions in the extension API are divided into two broad groups,
22 one that has no particular knowledge of the internals and one that does.
24 The stability of the two API groups is guaranteed separately. Group 1, the
25 internals unaware group, should be good basically forever. Group 2, the
26 internals aware group, is only guaranteed for the lifetime of the current
27 architecture. (It's likely that both groups will last equally long; however,
28 the Group 1 interface could reasonably be emulated on a different engine,
29 while the Group 2 interface is more closely tied to Parrot).
31 B<Note:> The extension API has not yet been completely specified. New
32 functions may be added, and those described below may change or be removed.
33 You have been warned...
35 =head1 IMPLEMENTATION
37 =head2 API - Group 1: Internals-unaware functions
39 These functions are the ones that are largely unaware of the structure and
40 architecture of Parrot. They deal mainly in data as abstract entities, and
41 Parrot itself as a black box that, at best, can make subroutine or method
42 calls. This is sufficient for many extensions which act as black box
43 processing units and in turn treat Parrot itself as a black box.
45 =head3 PMC access functions
47 The following functions are for storing and retrieving data inside PMCs. Note
48 that use of the _keyed functions with non-aggregate PMCs will generally just
49 result in Parrot throwing an exception.
51 =over 4
53 =item C<Parrot_PMC_get_string(interp, pmc)>
55 Returns a Parrot_String that represents the string value of the PMC.
57 =item C<Parrot_PMC_get_string_intkey(interp, pmc, Parrot_Int key)>
59 Keyed version of C<Parrot_PMC_get_string>. Returns a Parrot_String
60 representing the string value of whatever is stored at the element of the PMC
61 indexed by C<key>.
63 =item C<Parrot_PMC_get_pointer(interp, pmc)>
65 Returns a pointer to some item of data. The details of what the pointer points
66 to depend on the particular PMC. This function is useful for dealing with PMCs
67 that hold pointers to arbitrary data.
69 =item C<Parrot_PMC_get_pointer_intkey(interp, pmc, Parrot_Int key)>
71 A keyed version of C<Parrot_PMC_get_pointer>. Returns the pointer value of
72 whatever is stored at the element of the PMC indexed by C<key>.
74 =item C<Parrot_PMC_get_intval(interp, pmc)>
76 Returns the integer value of the PMC.
78 =item C<Parrot_PMC_get_intval_intkey(interp, pmc, Parrot_Int key)>
80 A keyed version of C<Parrot_PMC_get_intval>. Returns the integer value of
81 whatever is stored at the element of the PMC indexed by C<key>.
83 =item C<Parrot_PMC_get_numval(interp, pmc)>
85 Returns the numeric value of the PMC.
87 =item C<Parrot_PMC_get_numval_intkey(interp, pmc, Parrot_Int key)>
89 A keyed version of C<Parrot_PMC_get_numval>. Returns the numeric value of
90 whatever is stored at the element of the PMC indexed by C<key>.
92 =item C<Parrot_PMC_get_cstring(interp, pmc)>
94 Returns a C-string (char *) that represents the string value of the PMC. The
95 memory the char * points to is a copy of the original value, and changing it
96 will not change the original in any way.
98 This memory will I<not> be reclaimed by garbage collection, nor may it be
99 returned to the system with C<free>. It must be returned with
100 C<Parrot_free_cstring>.
102 =item C<Parrot_PMC_get_cstring_intkey(interp, pmc, Parrot_Int key)>
104 A keyed version of C<Parrot_PMC_get_cstring>. Returns a C-string representing
105 the string value of whatever is stored at the element of  the PMC indexed by
106 C<key>.
108 =item C<Parrot_PMC_get_cstringn(interp, pmc, &len)>
110 Returns a C-string (char *) that represents the string value of the PMC. The
111 memory the char * points to is a copy of the original value, and changing it
112 will not change the original in any way. The C<len> parameter is the address
113 of an integer that will get the length of the string as Parrot knows it.
115 This memory will I<not> be reclaimed by garbage collection, nor may it be
116 returned to the system with C<free>. It must be returned with
117 C<Parrot_free_cstring>.
119 =item C<Parrot_PMC_get_cstringn_intkey(interp, pmc, &len, Parrot_Int key)>
121 A keyed version of C<Parrot_PMC_get_cstringn>. Returns a C-string representing
122 the string value of whatever is stored at the element of  the PMC indexed by
123 C<key>. Stores the length of the string in C<len>.
125 =item C<Parrot_PMC_get_pmc_intkey(interp, pmc, Parrot_Int key)>
127 Returns the PMC stored at the element of the passed-in PMC that is indexed by
128 C<key>.
130 =item C<Parrot_PMC_set_string(interp, pmc, Parrot_String value)>
132 Assign the passed-in Parrot_String to the passed-in PMC.
134 =item C<Parrot_PMC_set_string_intkey(interp,
135 pmc, Parrot_String value, Parrot_Int key)>
137 Keyed version of C<Parrot_PMC_set_string>. Assigns C<value> to the PMC stored
138 at element <key> of the passed-in PMC.
140 =item C<Parrot_PMC_set_pointer(interp, pmc, void *value)>
142 Assign the passed-in pointer to the passed-in PMC.
144 =item C<Parrot_PMC_set_pointer_intkey(interp, pmc, void *value,
145 Parrot_Int key)>
147 Keyed version of C<Parrot_PMC_set_pointer>. Assigns C<value> to the PMC stored
148 at element <key> of the passed-in PMC.
150 =item C<Parrot_PMC_set_pmc_intkey(interp, pmc, Parrot_PMC value,
151 Parrot_Int key)>
153 Assigns C<value> to the PMC stored at element <key> of the passed-in PMC.
155 =item C<Parrot_PMC_set_intval(interp, pmc, Parrot_Int value)>
157 Assign the passed-in Parrot integer to the passed-in PMC.
159 =item C<Parrot_PMC_set_intval_intkey(interp,
160 pmc, Parrot_Int value, Parrot_Int key)>
162 Keyed version of C<Parrot_PMC_set_intval>. Assigns C<value> to the PMC stored
163 at element <key> of the passed-in PMC.
165 =item C<Parrot_PMC_set_numval(interp, pmc, Parrot_Float value)>
167 Assign the passed-in Parrot number to the passed-in PMC.
169 =item C<Parrot_PMC_set_numval_intkey(interp,
170 pmc, Parrot_Float value, Parrot_Int key)>
172 Keyed version of C<Parrot_PMC_set_numval>. Assigns C<value> to the PMC stored
173 at element <key> of the passed-in PMC.
175 =item C<Parrot_PMC_set_cstring(interp, pmc, const char *value)>
177 Convert the passed-in null-terminated C string to a Parrot_String and assign
178 it to the passed-in PMC.
180 =item C<Parrot_PMC_set_cstring_intkey(interp,
181 pmc, const char *value, Parrot_Int key)>
183 Keyed version of C<Parrot_PMC_set_cstring>. Converts C<value> to a
184 Parrot_String and assigns it to the PMC stored at element <key> of the
185 passed-in PMC.
187 =item C<Parrot_PMC_set_cstringn(interp,
188 pmc, const char *value, Parrot_Int length)>
190 Convert the passed-in null-terminated C string to a Parrot_String of length
191 C<length> and assign it to the passed-in PMC. If C<value> is longer than
192 C<length>, then only the first C<length> characters will be converted. If
193 C<value> is shorter than C<length>, then the extra characters in the
194 Parrot_String should be assumed to contain garbage.
196 =item C<Parrot_PMC_set_cstringn_intkey(interp,
197 pmc, const char *value, Parrot_int length, Parrot_Int key)>
199 Keyed version of C<Parrot_PMC_set_cstringn>. Converts the first C<length>
200 characters of C<value> to a Parrot_String and assigns the resulting string to
201 the PMC stored at element <key> of the passed-in PMC.
203 =back
205 =head3 Creation and destruction
207 Functions used to create and destroy PMCs, Parrot_Strings, etc.
209 =over 4
211 =item C<Parrot_PMC_new(interp, Parrot_Int typenum)>
213 Creates and returns a new PMC. C<typenum> is an integer identifier that
214 specifies the type of PMC required. The C<typenum> corresponding to a
215 particular PMC class name can be found using C<Parrot_PMC_typenum>.
217 =item C<Parrot_PMC_typenum(interp, const char* class)>
219 Returns the internal integer identifier corresponding to a PMC with class name
220 C<class>.
222 =item C<Parrot_PMC_null()>
224 Returns the special C<NULL> PMC.
226 =item C<Parrot_new_string(interp,
227 char *buffer, int length, Parrot_Encoding encoding,
228 Parrot_CharType charset, Parrot_Language language, Parrot_Int flags)>
230 Create a new Parrot string from a passed-in buffer. If the C<encoding>,
231 C<charset>, or C<language> are unspecified (i.e. if you pass in 0), then the
232 defaults are used. Otherwise, the functions C<Parrot_find_encoding>,
233 C<Parrot_find_chartype> and C<Parrot_find_language> (all described below) can
234 be used to find the appropriate values for a particular choice of  encoding,
235 chartype or language.
237 Flag values are currently undocumented.
239 Note that a copy of the buffer is made.
241 =item C<Parrot_find_encoding(interp, char *encoding_name)>
243 Find the magic token for an encoding, by name.
245 =item C<Parrot_find_chartype(interp, char *chartype)>
247 Find the magic token for a chartype, by name.
249 =item C<Parrot_find_language(interp, char *language)>
251 Find the magic token for a language, by language name.
253 =item C<Parrot_free_cstring(char* string)>
255 Deallocates a C string that the interpreter has handed to you. This function
256 must be used to free strings produced by  C<Parrot_PMC_get_cstring> and
257 C<Parrot_PMC_get_cstringn>, as these will not be reclaimed by the garbage
258 collector. It should not be used to deallocate strings that do not come from
259 Parrot.
261 =item C<Parrot_register_pmc(interp, pmc)>
263 Add a reference to the PMC to the interpreter's DOD registry. This prevents
264 PMCs known only to extensions from getting destroyed during  DOD runs.
266 =item C<Parrot_unregister_pmc(interp, pmc)>
268 Remove a reference to the PMC from the interpreter's DOD registry.  If the
269 reference count reaches zero, the PMC will be destroyed during the  next DOD
270 run.
272 =back
274 =head3 Subroutine and method calls
276 Functions to call Parrot subroutines and methods
278 =over 4
280 =item C<Parrot_call_sub(interp, Parrot_PMC sub, Parrot_Int argcount, ...)>
282 Calls a Parrot subroutine, with C<argcount> PMC parameters. This function sets
283 up Parrot's registers in line with the Parrot calling conventions; see
284 L<pdd03_calling_conventions.pod> for more details.
286 =item C<Parrot_call_method(interp,
287 Parrot_PMC sub, Parrot_String method, Parrot_Int argcount, ...)>
289 Calls a Parrot method named C<method> with C<argcount> PMC parameters. NB.
290 This is not yet implemented and may change.
292 =back
294 =head2 API - Group 2: Internals aware
296 The internals-aware functions are for those extensions that need to query or
297 alter the state of Parrot's internals in some way.
299 Register access functions
301 The following functions allow the values stored in Parrot's registers to be
302 accessed. An attempt to access a non-existent register (e.g. string register
303 -123) will cause the function to throw an exception (well, it will once we
304 actually implement some bounds-checking...).  The value stored in an
305 uninitialized register is undefined; it may well be zero (or NULL), but do not
306 rely on this being the case.
308 =over 4
310 =item C<Parrot_get_intreg(interp, Parrot_Int regnum)>
312 Return the value of an integer register.
314 =item C<Parrot_get_numreg(interp, Parrot_Int regnum)>
316 Return the value of a numeric register.
318 =item C<Parrot_get_strreg(interp, Parrot_Int regnum)>
320 Return the value of a string register.
322 =item C<Parrot_get_pmcreg(interp, Parrot_Int regnum)>
324 Return the value of a PMC register.
326 =back
328 =head1 ATTACHMENTS
330 None.
332 =head1 FOOTNOTES
334 None.
336 =head1 REFERENCES
338 F<docs/glossary.pod>
340 =head1 VERSION
342 =head2 CURRENT
344     Maintainer:
345     Class: Internals
346     PDD Number: 11
347     Version: 1.0
348     Status: Developing
349     Last Modified: February 20, 2004
350     PDD Format: 1
351     Language: English
353 =head2 HISTORY
355 =over 4
357 =item Version 1
359 None. First version
361 =back
363 =head1 CHANGES
365 =over 4
367 =item Version 1.0
369 None. First version
371 =back
373 =cut