1 # Copyright (C) 2001-2009, Parrot Foundation.
4 =head1 [DRAFT] PDD 11: Extending
8 The extension API for Parrot is a simple, somewhat abstract, interface to
9 Parrot for code written in C or other compiled languages. It provides about
10 the same level of access to Parrot that bytecode programs have.
14 The API presents to C programs roughly the same interface presented to
15 bytecode programs--that is, a C extension can see everything that a bytecode
16 program can see, including Parrot's base architecture, registers, stacks, and
17 whatnot. This view isn't required, however, and often extension code won't
18 need or want to know what Parrot's internal structures look like. For this
19 reason the functions in the extension API are divided into two broad groups,
20 one that has no particular knowledge of the internals and one that does.
22 The stability of the two API groups is guaranteed separately. Group 1, the
23 internals unaware group, should be good basically forever. Group 2, the
24 internals aware group, is only guaranteed for the lifetime of the current
25 architecture. (It's likely that both groups will last equally long; however,
26 the Group 1 interface could reasonably be emulated on a different engine,
27 while the Group 2 interface is more closely tied to Parrot).
29 B<Note:> The extension API has not yet been completely specified. New
30 functions may be added, and those described below may change or be removed.
31 You have been warned...
35 =head3 API - Group 1: Internals-unaware functions
37 These functions are the ones that are largely unaware of the structure and
38 architecture of Parrot. They deal mainly in data as abstract entities, and
39 Parrot itself as a black box that, at best, can make subroutine or method
40 calls. This is sufficient for many extensions which act as black box
41 processing units and in turn treat Parrot itself as a black box.
43 =head4 PMC access functions
45 The following functions are for storing and retrieving data inside PMCs. Note
46 that use of the _keyed functions with non-aggregate PMCs will generally just
47 result in Parrot throwing an exception.
51 =item C<Parrot_PMC_get_string(interp, pmc)>
53 Returns a Parrot_String that represents the string value of the PMC.
55 =item C<Parrot_PMC_get_string_intkey(interp, pmc, Parrot_Int key)>
57 Keyed version of C<Parrot_PMC_get_string>. Returns a Parrot_String
58 representing the string value of whatever is stored at the element of the PMC
61 =item C<Parrot_PMC_get_pointer(interp, pmc)>
63 Returns a pointer to some item of data. The details of what the pointer points
64 to depend on the particular PMC. This function is useful for dealing with PMCs
65 that hold pointers to arbitrary data.
67 =item C<Parrot_PMC_get_pointer_intkey(interp, pmc, Parrot_Int key)>
69 A keyed version of C<Parrot_PMC_get_pointer>. Returns the pointer value of
70 whatever is stored at the element of the PMC indexed by C<key>.
72 =item C<Parrot_PMC_get_intval(interp, pmc)>
74 Returns the integer value of the PMC.
76 =item C<Parrot_PMC_get_intval_intkey(interp, pmc, Parrot_Int key)>
78 A keyed version of C<Parrot_PMC_get_intval>. Returns the integer value of
79 whatever is stored at the element of the PMC indexed by C<key>.
81 =item C<Parrot_PMC_get_numval(interp, pmc)>
83 Returns the numeric value of the PMC.
85 =item C<Parrot_PMC_get_numval_intkey(interp, pmc, Parrot_Int key)>
87 A keyed version of C<Parrot_PMC_get_numval>. Returns the numeric value of
88 whatever is stored at the element of the PMC indexed by C<key>.
90 =item C<Parrot_PMC_get_cstring(interp, pmc)>
92 Returns a C-string (char *) that represents the string value of the PMC. The
93 memory the char * points to is a copy of the original value, and changing it
94 will not change the original in any way.
96 This memory will I<not> be reclaimed by garbage collection, nor may it be
97 returned to the system with C<free>. It must be returned with
98 C<Parrot_free_cstring>.
100 =item C<Parrot_PMC_get_cstring_intkey(interp, pmc, Parrot_Int key)>
102 A keyed version of C<Parrot_PMC_get_cstring>. Returns a C-string representing
103 the string value of whatever is stored at the element of the PMC indexed by
106 =item C<Parrot_PMC_get_cstringn(interp, pmc, &len)>
108 Returns a C-string (char *) that represents the string value of the PMC. The
109 memory the char * points to is a copy of the original value, and changing it
110 will not change the original in any way. The C<len> parameter is the address
111 of an integer that will get the length of the string as Parrot knows it.
113 This memory will I<not> be reclaimed by garbage collection, nor may it be
114 returned to the system with C<free>. It must be returned with
115 C<Parrot_free_cstring>.
117 =item C<Parrot_PMC_get_cstringn_intkey(interp, pmc, &len, Parrot_Int key)>
119 A keyed version of C<Parrot_PMC_get_cstringn>. Returns a C-string representing
120 the string value of whatever is stored at the element of the PMC indexed by
121 C<key>. Stores the length of the string in C<len>.
123 =item C<Parrot_PMC_get_pmc_intkey(interp, pmc, Parrot_Int key)>
125 Returns the PMC stored at the element of the passed-in PMC that is indexed by
128 =item C<Parrot_PMC_set_string(interp, pmc, Parrot_String value)>
130 Assign the passed-in Parrot_String to the passed-in PMC.
132 =item C<Parrot_PMC_set_string_intkey(interp,
133 pmc, Parrot_String value, Parrot_Int key)>
135 Keyed version of C<Parrot_PMC_set_string>. Assigns C<value> to the PMC stored
136 at element <key> of the passed-in PMC.
138 =item C<Parrot_PMC_set_pointer(interp, pmc, void *value)>
140 Assign the passed-in pointer to the passed-in PMC.
142 =item C<Parrot_PMC_set_pointer_intkey(interp, pmc, void *value,
145 Keyed version of C<Parrot_PMC_set_pointer>. Assigns C<value> to the PMC stored
146 at element <key> of the passed-in PMC.
148 =item C<Parrot_PMC_set_pmc_intkey(interp, pmc, Parrot_PMC value,
151 Assigns C<value> to the PMC stored at element <key> of the passed-in PMC.
153 =item C<Parrot_PMC_set_intval(interp, pmc, Parrot_Int value)>
155 Assign the passed-in Parrot integer to the passed-in PMC.
157 =item C<Parrot_PMC_set_intval_intkey(interp,
158 pmc, Parrot_Int value, Parrot_Int key)>
160 Keyed version of C<Parrot_PMC_set_intval>. Assigns C<value> to the PMC stored
161 at element <key> of the passed-in PMC.
163 =item C<Parrot_PMC_set_numval(interp, pmc, Parrot_Float value)>
165 Assign the passed-in Parrot number to the passed-in PMC.
167 =item C<Parrot_PMC_set_numval_intkey(interp,
168 pmc, Parrot_Float value, Parrot_Int key)>
170 Keyed version of C<Parrot_PMC_set_numval>. Assigns C<value> to the PMC stored
171 at element <key> of the passed-in PMC.
173 =item C<Parrot_PMC_set_cstring(interp, pmc, const char *value)>
175 Convert the passed-in null-terminated C string to a Parrot_String and assign
176 it to the passed-in PMC.
178 =item C<Parrot_PMC_set_cstring_intkey(interp,
179 pmc, const char *value, Parrot_Int key)>
181 Keyed version of C<Parrot_PMC_set_cstring>. Converts C<value> to a
182 Parrot_String and assigns it to the PMC stored at element <key> of the
185 =item C<Parrot_PMC_set_cstringn(interp,
186 pmc, const char *value, Parrot_Int length)>
188 Convert the passed-in null-terminated C string to a Parrot_String of length
189 C<length> and assign it to the passed-in PMC. If C<value> is longer than
190 C<length>, then only the first C<length> characters will be converted. If
191 C<value> is shorter than C<length>, then the extra characters in the
192 Parrot_String should be assumed to contain garbage.
194 =item C<Parrot_PMC_set_cstringn_intkey(interp,
195 pmc, const char *value, Parrot_int length, Parrot_Int key)>
197 Keyed version of C<Parrot_PMC_set_cstringn>. Converts the first C<length>
198 characters of C<value> to a Parrot_String and assigns the resulting string to
199 the PMC stored at element <key> of the passed-in PMC.
201 =item C<Parrot_PMC_push_float( interp, Parrot_PMC pmc, Parrot_Float value )>
203 Push a float onto an aggregate PMC, such as a ResizablePMCArray.
206 =item C<Parrot_PMC_push_integer( interp, Parrot_PMC pmc, Parrot_Int value )>
208 Push a integer onto an aggregate PMC, such as a ResizableIntegerArray.
211 =item C<Parrot_PMC_push_pmc( interp, Parrot_PMC pmc, Parrot_PMC value )>
213 Push a PMC value onto an aggregate PMC, such as a ResizablePMCArray.
216 =item C<Parrot_PMC_push_string( interp, Parrot_PMC pmc, Parrot_String value )>
218 Push a Parrot_String onto an aggregate PMC, such as a ResizableStringArray.
221 =item C<Parrot_PMC_pop_float( interp, Parrot_PMC pmc )>
223 Pop a Parrot_Float off of an aggregate PMC and returns it.
225 =item C<Parrot_PMC_pop_integer( interp, Parrot_PMC pmc )>
227 Pop a Parrot_Int off of an aggregate PMC and returns it.
229 =item C<Parrot_PMC_pop_pmc( interp, Parrot_PMC pmc )>
231 Pop a PMC off of an aggregate PMC and returns it.
233 =item C<Parrot_PMC_pop_string( interp, Parrot_PMC pmc )>
235 Pop a Parrot_String off of an aggregate PMC and returns it.
239 =head4 Creation and destruction
241 Functions used to create and destroy PMCs, Parrot_Strings, etc.
245 =item C<Parrot_PMC_new(interp, Parrot_Int typenum)>
247 Creates and returns a new PMC. C<typenum> is an integer identifier that
248 specifies the type of PMC required. The C<typenum> corresponding to a
249 particular PMC class name can be found using C<Parrot_PMC_typenum>.
251 =item C<Parrot_PMC_typenum(interp, const char* class)>
253 Returns the internal integer identifier corresponding to a PMC with class name
256 =item C<Parrot_PMC_null()>
258 Returns the special C<NULL> PMC.
260 =item C<Parrot_new_string(interp,
261 char *buffer, int length, Parrot_Encoding encoding,
262 Parrot_CharType charset, Parrot_Language language, Parrot_Int flags)>
264 Create a new Parrot string from a passed-in buffer. If the C<encoding>,
265 C<charset>, or C<language> are unspecified (i.e. if you pass in 0), then the
266 defaults are used. Otherwise, the functions C<Parrot_find_encoding>,
267 C<Parrot_find_chartype> and C<Parrot_find_language> (all described below) can
268 be used to find the appropriate values for a particular choice of encoding,
269 chartype or language.
271 Flag values are currently undocumented.
273 Note that a copy of the buffer is made.
275 =item C<Parrot_find_encoding(interp, char *encoding_name)>
277 Find the magic token for an encoding, by name.
279 =item C<Parrot_find_chartype(interp, char *chartype)>
281 Find the magic token for a chartype, by name.
283 =item C<Parrot_find_language(interp, char *language)>
285 Find the magic token for a language, by language name.
287 =item C<Parrot_free_cstring(char* string)>
289 Deallocates a C string that the interpreter has handed to you. This function
290 must be used to free strings produced by C<Parrot_PMC_get_cstring> and
291 C<Parrot_PMC_get_cstringn>, as these will not be reclaimed by the garbage
292 collector. It should not be used to deallocate strings that do not come from
295 =item C<Parrot_register_pmc(interp, pmc)>
297 Add a reference to the PMC to the interpreter's GC registry. This prevents
298 PMCs known only to extensions from getting destroyed during GC runs.
300 =item C<Parrot_unregister_pmc(interp, pmc)>
302 Remove a reference to the PMC from the interpreter's GC registry. If the
303 reference count reaches zero, the PMC will be destroyed during the next GC
308 =head4 Subroutine and method calls
310 Functions to call Parrot subroutines and methods
314 TODO: Add new call functions here
318 =head3 API - Group 2: Internals aware
320 The internals-aware functions are for those extensions that need to query or
321 alter the state of Parrot's internals in some way.
323 Register access functions
325 The following functions allow the values stored in Parrot's registers to be
326 accessed. An attempt to access a non-existent register (e.g. string register
327 -123) will cause the function to throw an exception (well, it will once we
328 actually implement some bounds-checking...). The value stored in an
329 uninitialized register is undefined; it may well be zero (or NULL), but do not
330 rely on this being the case.
334 =item C<Parrot_get_intreg(interp, Parrot_Int regnum)>
336 Return the value of an integer register.
338 =item C<Parrot_get_numreg(interp, Parrot_Int regnum)>
340 Return the value of a numeric register.
342 =item C<Parrot_get_strreg(interp, Parrot_Int regnum)>
344 Return the value of a string register.
346 =item C<Parrot_get_pmcreg(interp, Parrot_Int regnum)>
348 Return the value of a PMC register.
373 Last Modified: February 20, 2004
403 vim: expandtab shiftwidth=4: