2 Copyright (C) 2001-2007, Parrot Foundation.
7 src/extend.c - Parrot extension interface
11 These are the functions that parrot extensions (i.e. parrot subroutines
12 written in C, or some other compiled language, rather than in parrot
15 There is a deliberate distancing from the internals here. Don't go
16 peeking inside -- you've as much access as bytecode does, but no more,
17 so we can provide backwards compatibility for as long as we possibly
28 /* Some internal notes. Parrot will die a horrible and bizarre death
29 if the stack start pointer's not set and a GC run is
30 triggered. The pointer *will* be set by the interpreter if the
31 interpreter calls code which calls these functions, so most
32 extension code is safe, no problem.
34 The problem comes in if these routines are called from *outside*
35 an interpreter. This happens when an embedding application calls
36 them to do stuff with PMCs, STRINGS, interpreter contents, and
37 suchlike things. This is perfectly legal -- in fact it's what
38 we've documented should be done -- but the problem is that the
39 stack base pointer will be NULL. This is Very Bad.
41 To deal with this there are two macros that are defined to handle
44 PARROT_CALLIN_START(interp) will figure out if the stack
45 anchor needs setting and, if so, will set it. It must *always*
46 come immediately after the last variable declared in the block
47 making the calls into the interpreter, as it declares a variable
50 PARROT_CALLIN_END(interp) will put the stack anchor back to
51 the way it was, and should always be the last statement before a
52 return. (If you have multiple returns have it in multiple times)
54 Not doing this is a good way to introduce bizarre heisenbugs, so
55 just do it. This is the only place they ought to have to be put
56 in, and most of the functions are already written, so it's not
57 like it's an onerous requirement.
61 #include "parrot/parrot.h"
62 #include "parrot/extend.h"
63 #include "pmc/pmc_sub.h"
64 #include "pmc/pmc_context.h"
66 /* HEADERIZER HFILE: include/parrot/extend.h */
70 =item C<int Parrot_vfprintf(PARROT_INTERP, Parrot_PMC pio, const char *s,
73 Writes a C string format with a varargs list to a PIO.
75 =item C<int Parrot_fprintf(PARROT_INTERP, Parrot_PMC pio, const char *s, ...)>
77 Writes a C string format with varargs to a PIO.
79 =item C<int Parrot_printf(NULLOK_INTERP, const char *s, ...)>
81 Writes a C string format with varargs to C<stdout>.
83 =item C<int Parrot_eprintf(NULLOK_INTERP, const char *s, ...)>
85 Writes a C string format with varargs to C<stderr>.
93 Parrot_vfprintf(PARROT_INTERP
, ARGIN(Parrot_PMC pio
),
94 ARGIN(const char *s
), va_list args
)
96 ASSERT_ARGS(Parrot_vfprintf
)
100 PARROT_CALLIN_START(interp
);
101 str
= Parrot_vsprintf_c(interp
, s
, args
);
102 retval
= Parrot_io_putps(interp
, pio
, str
);
103 PARROT_CALLIN_END(interp
);
110 Parrot_fprintf(PARROT_INTERP
, ARGIN(Parrot_PMC pio
),
111 ARGIN(const char *s
), ...)
113 ASSERT_ARGS(Parrot_fprintf
)
118 retval
= Parrot_vfprintf(interp
, pio
, s
, args
);
126 Parrot_printf(NULLOK_INTERP
, ARGIN(const char *s
), ...)
128 ASSERT_ARGS(Parrot_printf
)
134 retval
= Parrot_vfprintf(interp
, Parrot_io_STDOUT(interp
), s
, args
);
137 /* Be nice about this...
138 ** XXX BD Should this use the default Parrot_io_STDOUT or something?
140 retval
= vfprintf(stdout
, s
, args
);
149 Parrot_eprintf(NULLOK_INTERP
, ARGIN(const char *s
), ...)
151 ASSERT_ARGS(Parrot_eprintf
)
158 retval
= Parrot_vfprintf(interp
, Parrot_io_STDERR(interp
), s
, args
);
161 /* Be nice about this...
162 ** XXX BD Should this use the default Parrot_io_STDOUT or something?
164 retval
=vfprintf(stderr
, s
, args
);
174 =item C<Parrot_PMC Parrot_get_root_namespace(PARROT_INTERP)>
176 Return the root namespace
184 Parrot_get_root_namespace(PARROT_INTERP
)
186 ASSERT_ARGS(Parrot_get_root_namespace
)
187 return interp
->root_namespace
;
192 =item C<Parrot_String Parrot_PMC_get_string_intkey(PARROT_INTERP, Parrot_PMC
193 pmc, Parrot_Int key)>
195 Return the integer keyed string value of the passed-in PMC
203 Parrot_PMC_get_string_intkey(PARROT_INTERP
,
204 Parrot_PMC pmc
, Parrot_Int key
)
206 ASSERT_ARGS(Parrot_PMC_get_string_intkey
)
207 Parrot_String retval
;
208 PARROT_CALLIN_START(interp
);
209 retval
= VTABLE_get_string_keyed_int(interp
, pmc
, key
);
210 PARROT_CALLIN_END(interp
);
217 =item C<void * Parrot_PMC_get_pointer_intkey(PARROT_INTERP, Parrot_PMC pmc,
220 Return a pointer to the PMC indicated by an integer key.
227 PARROT_WARN_UNUSED_RESULT
228 PARROT_CAN_RETURN_NULL
230 Parrot_PMC_get_pointer_intkey(PARROT_INTERP
,
231 Parrot_PMC pmc
, Parrot_Int key
)
233 ASSERT_ARGS(Parrot_PMC_get_pointer_intkey
)
235 PARROT_CALLIN_START(interp
);
236 retval
= VTABLE_get_pointer_keyed_int(interp
, pmc
, key
);
237 PARROT_CALLIN_END(interp
);
243 =item C<Parrot_PMC Parrot_PMC_get_pmc_intkey(PARROT_INTERP, Parrot_PMC pmc,
246 Return the integer keyed PMC value of the passed-in PMC
254 Parrot_PMC_get_pmc_intkey(PARROT_INTERP
,
255 Parrot_PMC pmc
, Parrot_Int key
)
257 ASSERT_ARGS(Parrot_PMC_get_pmc_intkey
)
259 PARROT_CALLIN_START(interp
);
260 retval
= VTABLE_get_pmc_keyed_int(interp
, pmc
, key
);
261 PARROT_CALLIN_END(interp
);
267 =item C<Parrot_PMC Parrot_PMC_get_pmc_strkey(PARROT_INTERP, Parrot_PMC pmc,
270 Return the string keyed PMC value of the passed-in PMC
278 Parrot_PMC_get_pmc_strkey(PARROT_INTERP
,
279 Parrot_PMC pmc
, Parrot_String key
)
281 ASSERT_ARGS(Parrot_PMC_get_pmc_strkey
)
283 PARROT_CALLIN_START(interp
);
284 retval
= VTABLE_get_pmc_keyed_str(interp
, pmc
, key
);
285 PARROT_CALLIN_END(interp
);
291 =item C<Parrot_Int Parrot_PMC_get_intval(PARROT_INTERP, Parrot_PMC pmc)>
293 Return the signed integer value of the value in the PMC.
301 Parrot_PMC_get_intval(PARROT_INTERP
, Parrot_PMC pmc
)
303 ASSERT_ARGS(Parrot_PMC_get_intval
)
305 PARROT_CALLIN_START(interp
);
306 retval
= VTABLE_get_integer(interp
, pmc
);
307 PARROT_CALLIN_END(interp
);
313 =item C<Parrot_Int Parrot_PMC_get_intval_intkey(PARROT_INTERP, Parrot_PMC pmc,
316 Return the keyed, signed integer value of the value in the PMC.
324 Parrot_PMC_get_intval_intkey(PARROT_INTERP
,
325 Parrot_PMC pmc
, Parrot_Int key
)
327 ASSERT_ARGS(Parrot_PMC_get_intval_intkey
)
329 PARROT_CALLIN_START(interp
);
330 retval
= VTABLE_get_integer_keyed_int(interp
, pmc
, key
);
331 PARROT_CALLIN_END(interp
);
337 =item C<Parrot_Int Parrot_PMC_get_intval_pmckey(PARROT_INTERP, Parrot_PMC pmc,
340 Return the keyed, signed integer value of the value in the PMC.
348 Parrot_PMC_get_intval_pmckey(PARROT_INTERP
,
349 Parrot_PMC pmc
, Parrot_PMC key
)
351 ASSERT_ARGS(Parrot_PMC_get_intval_pmckey
)
353 PARROT_CALLIN_START(interp
);
354 retval
= VTABLE_get_integer_keyed(interp
, pmc
, key
);
355 PARROT_CALLIN_END(interp
);
361 =item C<Parrot_Float Parrot_PMC_get_numval(PARROT_INTERP, Parrot_PMC pmc)>
363 Return the floating-point value of the PMC.
371 Parrot_PMC_get_numval(PARROT_INTERP
, Parrot_PMC pmc
)
373 ASSERT_ARGS(Parrot_PMC_get_numval
)
375 PARROT_CALLIN_START(interp
);
376 retval
= VTABLE_get_number(interp
, pmc
);
377 PARROT_CALLIN_END(interp
);
383 =item C<Parrot_Float Parrot_PMC_get_numval_intkey(PARROT_INTERP, Parrot_PMC pmc,
386 Return the keyed, signed integer value of the value in the PMC.
394 Parrot_PMC_get_numval_intkey(PARROT_INTERP
,
395 Parrot_PMC pmc
, Parrot_Int key
)
397 ASSERT_ARGS(Parrot_PMC_get_numval_intkey
)
399 PARROT_CALLIN_START(interp
);
400 retval
= VTABLE_get_number_keyed_int(interp
, pmc
, key
);
401 PARROT_CALLIN_END(interp
);
407 =item C<char * Parrot_PMC_get_cstring_intkey(PARROT_INTERP, Parrot_PMC pmc,
410 Return a null-terminated string that represents the string value of the PMC.
412 Note that you must free this string with C<Parrot_str_free_cstring()>!
420 PARROT_CAN_RETURN_NULL
422 Parrot_PMC_get_cstring_intkey(PARROT_INTERP
,
423 Parrot_PMC pmc
, Parrot_Int key
)
425 ASSERT_ARGS(Parrot_PMC_get_cstring_intkey
)
426 STRING
*intermediate
;
429 PARROT_CALLIN_START(interp
);
430 intermediate
= VTABLE_get_string_keyed_int(interp
, pmc
, key
);
431 retval
= Parrot_str_to_cstring(interp
, intermediate
);
432 PARROT_CALLIN_END(interp
);
439 =item C<char * Parrot_PMC_get_cstring(PARROT_INTERP, Parrot_PMC pmc)>
441 Return a null-terminated string that represents the string value of the PMC.
443 Note that you must free this string with C<Parrot_str_free_cstring()>!
451 PARROT_CAN_RETURN_NULL
453 Parrot_PMC_get_cstring(PARROT_INTERP
, Parrot_PMC pmc
)
455 ASSERT_ARGS(Parrot_PMC_get_cstring
)
456 STRING
*intermediate
;
459 PARROT_CALLIN_START(interp
);
460 intermediate
= VTABLE_get_string(interp
, pmc
);
461 retval
= Parrot_str_to_cstring(interp
, intermediate
);
462 PARROT_CALLIN_END(interp
);
469 =item C<char * Parrot_PMC_get_cstringn(PARROT_INTERP, Parrot_PMC pmc, Parrot_Int
472 Return a null-terminated string for the PMC, along with the length.
474 Yes, right now this is a bit of a cheat. It needs fixing, but without
475 disturbing the interface.
477 Note that you must free the string with C<Parrot_str_free_cstring()>.
485 PARROT_CAN_RETURN_NULL
487 Parrot_PMC_get_cstringn(PARROT_INTERP
, ARGIN(Parrot_PMC pmc
),
488 ARGOUT(Parrot_Int
*length
))
490 ASSERT_ARGS(Parrot_PMC_get_cstringn
)
493 PARROT_CALLIN_START(interp
);
494 retval
= Parrot_str_to_cstring(interp
, VTABLE_get_string(interp
, pmc
));
495 *length
= strlen(retval
);
496 PARROT_CALLIN_END(interp
);
503 =item C<char * Parrot_PMC_get_cstringn_intkey(PARROT_INTERP, Parrot_PMC pmc,
504 Parrot_Int *length, Parrot_Int key)>
506 Return a null-terminated string for the PMC, along with the length.
508 Yes, right now this is a bit of a cheat. It needs fixing, but without
509 disturbing the interface.
511 Note that you must free this string with C<Parrot_str_free_cstring()>.
519 PARROT_CAN_RETURN_NULL
521 Parrot_PMC_get_cstringn_intkey(PARROT_INTERP
, ARGIN(Parrot_PMC pmc
),
522 ARGOUT(Parrot_Int
*length
), Parrot_Int key
)
524 ASSERT_ARGS(Parrot_PMC_get_cstringn_intkey
)
527 PARROT_CALLIN_START(interp
);
528 retval
= Parrot_str_to_cstring(interp
,
529 VTABLE_get_string_keyed_int(interp
, pmc
, key
));
530 *length
= strlen(retval
);
531 PARROT_CALLIN_END(interp
);
538 =item C<void Parrot_PMC_set_string(PARROT_INTERP, Parrot_PMC pmc, Parrot_String
541 Assign the passed-in Parrot string to the passed-in PMC.
549 Parrot_PMC_set_string(PARROT_INTERP
,
550 Parrot_PMC pmc
, Parrot_String value
)
552 ASSERT_ARGS(Parrot_PMC_set_string
)
553 PARROT_CALLIN_START(interp
);
554 VTABLE_set_string_native(interp
, pmc
, value
);
555 PARROT_CALLIN_END(interp
);
560 =item C<void Parrot_PMC_set_string_intkey(PARROT_INTERP, Parrot_PMC pmc,
561 Parrot_Int key, Parrot_String value)>
563 Assign the passed-in Parrot string to the passed-in PMC.
571 Parrot_PMC_set_string_intkey(PARROT_INTERP
,
572 Parrot_PMC pmc
, Parrot_Int key
, Parrot_String value
)
574 ASSERT_ARGS(Parrot_PMC_set_string_intkey
)
575 PARROT_CALLIN_START(interp
);
576 VTABLE_set_string_keyed_int(interp
, pmc
, key
, value
);
577 PARROT_CALLIN_END(interp
);
582 =item C<void Parrot_PMC_set_pmc_intkey(PARROT_INTERP, Parrot_PMC pmc, Parrot_Int
583 key, Parrot_PMC value)>
585 Assign the passed-in pmc to the passed-in slot of the passed-in PMC.
593 Parrot_PMC_set_pmc_intkey(PARROT_INTERP
,
594 Parrot_PMC pmc
, Parrot_Int key
, Parrot_PMC value
)
596 ASSERT_ARGS(Parrot_PMC_set_pmc_intkey
)
597 PARROT_CALLIN_START(interp
);
598 VTABLE_set_pmc_keyed_int(interp
, pmc
, key
, value
);
599 PARROT_CALLIN_END(interp
);
604 =item C<void Parrot_PMC_set_pmc_strkey(PARROT_INTERP, Parrot_PMC pmc,
605 Parrot_String key, Parrot_PMC value)>
607 Assign the passed-in pmc to the passed-in slot of the passed-in PMC.
615 Parrot_PMC_set_pmc_strkey(PARROT_INTERP
,
616 Parrot_PMC pmc
, Parrot_String key
, Parrot_PMC value
)
618 ASSERT_ARGS(Parrot_PMC_set_pmc_strkey
)
619 PARROT_CALLIN_START(interp
);
620 VTABLE_set_pmc_keyed_str(interp
, pmc
, key
, value
);
621 PARROT_CALLIN_END(interp
);
626 =item C<void Parrot_PMC_set_pmc_pmckey(PARROT_INTERP, Parrot_PMC pmc, Parrot_PMC
627 key, Parrot_PMC value)>
629 Assign the passed-in pmc to the passed-in slot of the passed-in PMC.
637 Parrot_PMC_set_pmc_pmckey(PARROT_INTERP
,
638 Parrot_PMC pmc
, Parrot_PMC key
, Parrot_PMC value
)
640 ASSERT_ARGS(Parrot_PMC_set_pmc_pmckey
)
641 PARROT_CALLIN_START(interp
);
642 VTABLE_set_pmc_keyed(interp
, pmc
, key
, value
);
643 PARROT_CALLIN_END(interp
);
648 =item C<void Parrot_PMC_set_pointer_intkey(PARROT_INTERP, Parrot_PMC pmc,
649 Parrot_Int key, void *value)>
651 Assign the passed-in pointer to the passed-in PMC.
659 Parrot_PMC_set_pointer_intkey(PARROT_INTERP
,
660 ARGIN(Parrot_PMC pmc
), Parrot_Int key
, ARGIN_NULLOK(void *value
))
662 ASSERT_ARGS(Parrot_PMC_set_pointer_intkey
)
663 PARROT_CALLIN_START(interp
);
664 VTABLE_set_pointer_keyed_int(interp
, pmc
, key
, value
);
665 PARROT_CALLIN_END(interp
);
670 =item C<void Parrot_PMC_set_intval(PARROT_INTERP, Parrot_PMC pmc, Parrot_Int
673 Assign the passed-in Parrot integer to the passed-in PMC.
681 Parrot_PMC_set_intval(PARROT_INTERP
, Parrot_PMC pmc
, Parrot_Int value
)
683 ASSERT_ARGS(Parrot_PMC_set_intval
)
684 PARROT_CALLIN_START(interp
);
685 VTABLE_set_integer_native(interp
, pmc
, value
);
686 PARROT_CALLIN_END(interp
);
691 =item C<void Parrot_PMC_set_intval_intkey(PARROT_INTERP, Parrot_PMC pmc,
692 Parrot_Int key, Parrot_Int value)>
694 Assign the passed-in Parrot integer to the passed-in PMC.
702 Parrot_PMC_set_intval_intkey(PARROT_INTERP
,
703 Parrot_PMC pmc
, Parrot_Int key
, Parrot_Int value
)
705 ASSERT_ARGS(Parrot_PMC_set_intval_intkey
)
706 PARROT_CALLIN_START(interp
);
707 VTABLE_set_integer_keyed_int(interp
, pmc
, key
, value
);
708 PARROT_CALLIN_END(interp
);
713 =item C<void Parrot_PMC_set_numval(PARROT_INTERP, Parrot_PMC pmc, Parrot_Float
716 Assign the passed-in Parrot number to the passed-in PMC.
724 Parrot_PMC_set_numval(PARROT_INTERP
, Parrot_PMC pmc
, Parrot_Float value
)
726 ASSERT_ARGS(Parrot_PMC_set_numval
)
727 PARROT_CALLIN_START(interp
);
728 VTABLE_set_number_native(interp
, pmc
, value
);
729 PARROT_CALLIN_END(interp
);
734 =item C<void Parrot_PMC_set_numval_intkey(PARROT_INTERP, Parrot_PMC pmc,
735 Parrot_Int key, Parrot_Float value)>
737 Assign the passed-in Parrot number to the passed-in PMC.
745 Parrot_PMC_set_numval_intkey(PARROT_INTERP
,
746 Parrot_PMC pmc
, Parrot_Int key
, Parrot_Float value
)
748 ASSERT_ARGS(Parrot_PMC_set_numval_intkey
)
749 PARROT_CALLIN_START(interp
);
750 VTABLE_set_number_keyed_int(interp
, pmc
, key
, value
);
751 PARROT_CALLIN_END(interp
);
756 =item C<void Parrot_PMC_set_cstring(PARROT_INTERP, Parrot_PMC pmc, const char
759 Assign the passed-in null-terminated C string to the passed-in PMC.
767 Parrot_PMC_set_cstring(PARROT_INTERP
, Parrot_PMC pmc
, ARGIN_NULLOK(const char *value
))
769 ASSERT_ARGS(Parrot_PMC_set_cstring
)
770 PARROT_CALLIN_START(interp
);
771 VTABLE_set_string_native(interp
, pmc
,
772 Parrot_str_new(interp
, value
, 0));
773 PARROT_CALLIN_END(interp
);
778 =item C<void Parrot_PMC_set_cstring_intkey(PARROT_INTERP, Parrot_PMC pmc,
779 Parrot_Int key, const char *value)>
781 Assign the passed-in null-terminated C string to the passed-in PMC.
789 Parrot_PMC_set_cstring_intkey(PARROT_INTERP
,
790 Parrot_PMC pmc
, Parrot_Int key
, ARGIN_NULLOK(const char *value
))
792 ASSERT_ARGS(Parrot_PMC_set_cstring_intkey
)
793 PARROT_CALLIN_START(interp
);
794 VTABLE_set_string_keyed_int(interp
, pmc
, key
,
795 Parrot_str_new(interp
, value
, 0));
796 PARROT_CALLIN_END(interp
);
801 =item C<void Parrot_PMC_set_cstringn(PARROT_INTERP, Parrot_PMC pmc, const char
802 *value, Parrot_UInt length)>
804 Assign the passed-in length-noted string to the passed-in PMC.
812 Parrot_PMC_set_cstringn(PARROT_INTERP
,
813 Parrot_PMC pmc
, ARGIN_NULLOK(const char *value
), Parrot_UInt length
)
815 ASSERT_ARGS(Parrot_PMC_set_cstringn
)
816 PARROT_CALLIN_START(interp
);
817 VTABLE_set_string_native(interp
, pmc
,
818 Parrot_str_new(interp
, value
, length
));
819 PARROT_CALLIN_END(interp
);
824 =item C<void Parrot_PMC_push_intval(PARROT_INTERP, Parrot_PMC pmc, Parrot_Int
827 Push the passed-in Parrot integer onto the passed in PMC
835 Parrot_PMC_push_intval(PARROT_INTERP
,
836 Parrot_PMC pmc
, Parrot_Int value
)
838 ASSERT_ARGS(Parrot_PMC_push_intval
)
839 PARROT_CALLIN_START(interp
);
840 VTABLE_push_integer(interp
, pmc
, value
);
841 PARROT_CALLIN_END(interp
);
846 =item C<void Parrot_PMC_push_numval(PARROT_INTERP, Parrot_PMC pmc, Parrot_Float
849 Push the passed-in Parrot number onto the passed in PMC
857 Parrot_PMC_push_numval(PARROT_INTERP
, Parrot_PMC pmc
, Parrot_Float value
)
859 ASSERT_ARGS(Parrot_PMC_push_numval
)
860 PARROT_CALLIN_START(interp
);
861 VTABLE_push_float(interp
, pmc
, value
);
862 PARROT_CALLIN_END(interp
);
867 =item C<void Parrot_PMC_push_pmcval(PARROT_INTERP, Parrot_PMC pmc, Parrot_PMC
870 Push the passed-in Parrot PMC onto the passed in PMC
878 Parrot_PMC_push_pmcval(PARROT_INTERP
, Parrot_PMC pmc
, Parrot_PMC value
)
880 ASSERT_ARGS(Parrot_PMC_push_pmcval
)
881 PARROT_CALLIN_START(interp
);
882 VTABLE_push_pmc(interp
, pmc
, value
);
883 PARROT_CALLIN_END(interp
);
888 =item C<void Parrot_PMC_delete_pmckey(PARROT_INTERP, Parrot_PMC pmc, Parrot_PMC
891 Deletes the value associated with the passed-in PMC from the PMC.
899 Parrot_PMC_delete_pmckey(PARROT_INTERP
, Parrot_PMC pmc
, Parrot_PMC key
)
901 ASSERT_ARGS(Parrot_PMC_delete_pmckey
)
902 PARROT_CALLIN_START(interp
);
903 VTABLE_delete_keyed(interp
, pmc
, key
);
904 PARROT_CALLIN_END(interp
);
909 =item C<void Parrot_PMC_set_cstringn_intkey(PARROT_INTERP, Parrot_PMC pmc,
910 Parrot_Int key, const char *value, Parrot_UInt length)>
912 Assign the passed-in length-noted string to the passed-in PMC.
920 Parrot_PMC_set_cstringn_intkey(PARROT_INTERP
,
921 Parrot_PMC pmc
, Parrot_Int key
, ARGIN_NULLOK(const char *value
),
924 ASSERT_ARGS(Parrot_PMC_set_cstringn_intkey
)
925 PARROT_CALLIN_START(interp
);
926 VTABLE_set_string_keyed_int(interp
, pmc
, key
,
927 Parrot_str_new(interp
, value
, length
));
928 PARROT_CALLIN_END(interp
);
933 =item C<Parrot_PMC Parrot_PMC_new(PARROT_INTERP, Parrot_Int type)>
935 Create and return a new PMC.
943 Parrot_PMC_new(PARROT_INTERP
, Parrot_Int type
)
945 ASSERT_ARGS(Parrot_PMC_new
)
947 PARROT_CALLIN_START(interp
);
948 newpmc
= pmc_new_noinit(interp
, type
);
949 VTABLE_init(interp
, newpmc
);
950 PARROT_CALLIN_END(interp
);
956 =item C<Parrot_Int Parrot_PMC_typenum(PARROT_INTERP, const char *_class)>
958 Returns the internal identifier that represents the named class.
966 Parrot_PMC_typenum(PARROT_INTERP
, ARGIN_NULLOK(const char *_class
))
968 ASSERT_ARGS(Parrot_PMC_typenum
)
970 PARROT_CALLIN_START(interp
);
971 retval
= pmc_type(interp
, Parrot_str_new(interp
, _class
, 0));
972 PARROT_CALLIN_END(interp
);
978 =item C<Parrot_PMC Parrot_PMC_null(void)>
980 Returns the special C<NULL> PMC.
988 Parrot_PMC_null(void)
990 ASSERT_ARGS(Parrot_PMC_null
)
996 =item C<void Parrot_free_cstring(char *string)>
998 Deallocate a C string that the interpreter has handed to you.
1006 Parrot_free_cstring(ARGIN_NULLOK(char *string
))
1008 ASSERT_ARGS(Parrot_free_cstring
)
1009 Parrot_str_free_cstring(string
);
1014 =item C<void Parrot_ext_call(PARROT_INTERP, Parrot_PMC sub_pmc, const char
1017 Call a Parrot subroutine or method with the given function signature. The
1018 function signature holds one type character for each argument or return, these
1026 Returns come after the arguments, separated by an arrow, so "PN->S" takes a PMC
1027 and a float as arguments and returns a string.
1029 Pass the variables for the arguments and returns in the same order as the
1030 signature, with returns as reference to the variable (so it can be modified).
1032 Parrot_ext_call(interp, sub, "P->S", pmc_arg, &string_result);
1034 To call a method, pass the object for the method as the first argument, and
1035 mark it in the signature as "Pi" ("i" stands for "invocant").
1037 Parrot_ext_call(interp, sub, "PiP->S", object_arg, pmc_arg, &string_result);
1045 Parrot_ext_call(PARROT_INTERP
, ARGIN(Parrot_PMC sub_pmc
),
1046 ARGIN(const char *signature
), ...)
1048 ASSERT_ARGS(Parrot_ext_call
)
1052 va_start(args
, signature
);
1053 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
, signature
, args
);
1056 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1061 =item C<void * Parrot_call_sub(PARROT_INTERP, Parrot_PMC sub_pmc, const char
1064 Call a parrot subroutine with the given function signature. The first char in
1065 C<signature> denotes the return value. Next chars are arguments.
1067 The return value of this function can be void or a pointer type.
1069 Signature chars are:
1082 PARROT_WARN_UNUSED_RESULT
1083 PARROT_CAN_RETURN_NULL
1085 Parrot_call_sub(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1086 ARGIN(const char *signature
), ...)
1088 ASSERT_ARGS(Parrot_call_sub
)
1092 void *result
= NULL
;
1093 const char *arg_sig
= signature
;
1094 char return_sig
= signature
[0];
1097 va_start(args
, signature
);
1098 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
,
1102 /* Add the return argument onto the call signature object (a bit
1103 * hackish, added for backward compatibility in deprecated API function,
1105 switch (return_sig
) {
1108 Parrot_String full_sig
= VTABLE_get_string(interp
, sig_object
);
1109 Parrot_str_concat(interp
, full_sig
,
1110 Parrot_str_new_constant(interp
, "->"), 0);
1116 Parrot_pcc_append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "P"),
1121 Parrot_ex_throw_from_c_args(interp
, NULL
,
1122 EXCEPTION_INVALID_OPERATION
,
1123 "Dispatch: invalid return type %c!", return_sig
);
1126 PMC_get_sub(interp
, sub_pmc
, sub
);
1127 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
),
1128 sub
->seg
->const_table
->constants
);
1129 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1137 =item C<Parrot_Int Parrot_call_sub_ret_int(PARROT_INTERP, Parrot_PMC sub_pmc,
1138 const char *signature, ...)>
1140 Like C<Parrot_call_sub>, with Parrot_Int return result.
1148 Parrot_call_sub_ret_int(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1149 ARGIN(const char *signature
), ...)
1151 ASSERT_ARGS(Parrot_call_sub_ret_int
)
1155 char return_sig
= signature
[0];
1156 const char *arg_sig
= signature
;
1160 va_start(args
, signature
);
1161 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
, arg_sig
, args
);
1164 /* Add the return argument onto the call signature object (a bit
1165 * hackish, added for backward compatibility in deprecated API function,
1167 Parrot_pcc_append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "I"), &result
);
1168 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1175 =item C<Parrot_Float Parrot_call_sub_ret_float(PARROT_INTERP, Parrot_PMC
1176 sub_pmc, const char *signature, ...)>
1178 Like C<Parrot_call_sub>, with Parrot_Float return result.
1186 Parrot_call_sub_ret_float(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1187 ARGIN(const char *signature
), ...)
1189 ASSERT_ARGS(Parrot_call_sub_ret_float
)
1192 Parrot_Float result
;
1193 char return_sig
= signature
[0];
1194 const char *arg_sig
= signature
;
1198 va_start(args
, signature
);
1199 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
, arg_sig
, args
);
1202 /* Add the return argument onto the call signature object (a bit
1203 * hackish, added for backward compatibility in deprecated API function,
1205 Parrot_pcc_append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "N"), &result
);
1206 PMC_get_sub(interp
, sub_pmc
, sub
);
1207 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1208 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1215 =item C<void * Parrot_call_method(PARROT_INTERP, Parrot_PMC sub_pmc, Parrot_PMC
1216 obj, Parrot_String method, const char *signature, ...)>
1218 Call the parrot subroutine C<sub> as a method on PMC object C<obj>. The method
1219 should have the name C<method> as a Parrot_string, and should have a function
1220 signature C<signature>. Any arguments to the method can be passed at the end
1221 as a variadic argument list.
1228 PARROT_WARN_UNUSED_RESULT
1229 PARROT_CAN_RETURN_NULL
1231 Parrot_call_method(PARROT_INTERP
, Parrot_PMC sub_pmc
, Parrot_PMC obj
,
1232 Parrot_String method
, ARGIN(const char *signature
), ...)
1234 ASSERT_ARGS(Parrot_call_method
)
1237 void *result
= NULL
;
1238 char return_sig
= signature
[0];
1239 char *arg_sig
= (char*)malloc(strlen(signature
)+2);
1244 strcat(arg_sig
, signature
);
1246 va_start(args
, signature
);
1247 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, obj
, arg_sig
, args
);
1251 /* Add the return argument onto the call signature object (a bit
1252 * hackish, added for backward compatibility in deprecated API function,
1254 switch (return_sig
) {
1257 Parrot_String full_sig
= VTABLE_get_string(interp
, sig_object
);
1258 Parrot_str_concat(interp
, full_sig
,
1259 Parrot_str_new_constant(interp
, "->"), 0);
1265 Parrot_pcc_append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "P"),
1270 Parrot_ex_throw_from_c_args(interp
, NULL
, EXCEPTION_INVALID_OPERATION
,
1271 "Dispatch: invalid return type %c!", return_sig
);
1274 PMC_get_sub(interp
, sub_pmc
, sub
);
1275 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1276 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1283 =item C<Parrot_Int Parrot_call_method_ret_int(PARROT_INTERP, Parrot_PMC sub_pmc,
1284 Parrot_PMC obj, Parrot_String method, const char *signature, ...)>
1286 Call the parrot subroutine C<sub> as a method on PMC object C<obj>. The method
1287 should have the name C<method> as a Parrot_string, and should have a function
1288 signature C<signature>. Any arguments to the method can be passed at the end
1289 as a variadic argument list.
1297 Parrot_call_method_ret_int(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1298 Parrot_PMC obj
, Parrot_String method
, ARGIN(const char *signature
), ...)
1300 ASSERT_ARGS(Parrot_call_method_ret_int
)
1303 Parrot_Int result
= 0;
1304 char return_sig
= signature
[0];
1305 char *arg_sig
= (char*)malloc(strlen(signature
)+2);
1310 strcat(arg_sig
, signature
);
1312 va_start(args
, signature
);
1313 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, obj
, arg_sig
, args
);
1317 Parrot_pcc_append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "I"), &result
);
1318 PMC_get_sub(interp
, sub_pmc
, sub
);
1319 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1320 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1327 =item C<Parrot_Float Parrot_call_method_ret_float(PARROT_INTERP, Parrot_PMC
1328 sub_pmc, Parrot_PMC obj, Parrot_String method, const char *signature, ...)>
1330 Call a parrot method for the given object.
1338 Parrot_call_method_ret_float(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1339 Parrot_PMC obj
, Parrot_String method
, ARGIN(const char *signature
), ...)
1341 ASSERT_ARGS(Parrot_call_method_ret_float
)
1344 Parrot_Float result
= 0.0;
1345 char return_sig
= signature
[0];
1346 char *arg_sig
= (char*)malloc(strlen(signature
)+2);
1351 strcat(arg_sig
, signature
);
1353 va_start(args
, signature
);
1354 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, obj
, arg_sig
, args
);
1358 Parrot_pcc_append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "N"), &result
);
1359 PMC_get_sub(interp
, sub_pmc
, sub
);
1360 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1361 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1368 =item C<Parrot_Int Parrot_get_intreg(PARROT_INTERP, Parrot_Int regnum)>
1370 Return the value of an integer register.
1378 Parrot_get_intreg(PARROT_INTERP
, Parrot_Int regnum
)
1380 ASSERT_ARGS(Parrot_get_intreg
)
1381 return REG_INT(interp
, regnum
);
1386 =item C<Parrot_Float Parrot_get_numreg(PARROT_INTERP, Parrot_Int regnum)>
1388 Return the value of a numeric register.
1396 Parrot_get_numreg(PARROT_INTERP
, Parrot_Int regnum
)
1398 ASSERT_ARGS(Parrot_get_numreg
)
1399 return REG_NUM(interp
, regnum
);
1404 =item C<Parrot_String Parrot_get_strreg(PARROT_INTERP, Parrot_Int regnum)>
1406 Return the value of a string register.
1414 Parrot_get_strreg(PARROT_INTERP
, Parrot_Int regnum
)
1416 ASSERT_ARGS(Parrot_get_strreg
)
1417 return REG_STR(interp
, regnum
);
1422 =item C<Parrot_PMC Parrot_get_pmcreg(PARROT_INTERP, Parrot_Int regnum)>
1424 Return the value of a PMC register.
1432 Parrot_get_pmcreg(PARROT_INTERP
, Parrot_Int regnum
)
1434 ASSERT_ARGS(Parrot_get_pmcreg
)
1435 return REG_PMC(interp
, regnum
);
1440 =item C<void Parrot_set_intreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Int
1443 Set the value of an I register.
1451 Parrot_set_intreg(PARROT_INTERP
, Parrot_Int regnum
,
1454 ASSERT_ARGS(Parrot_set_intreg
)
1455 REG_INT(interp
, regnum
) = value
;
1460 =item C<void Parrot_set_numreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Float
1463 Set the value of an N register.
1471 Parrot_set_numreg(PARROT_INTERP
, Parrot_Int regnum
,
1474 ASSERT_ARGS(Parrot_set_numreg
)
1475 REG_NUM(interp
, regnum
) = value
;
1480 =item C<void Parrot_set_strreg(PARROT_INTERP, Parrot_Int regnum, Parrot_String
1483 Set the value of an S register.
1491 Parrot_set_strreg(PARROT_INTERP
, Parrot_Int regnum
,
1492 Parrot_String value
)
1494 ASSERT_ARGS(Parrot_set_strreg
)
1495 REG_STR(interp
, regnum
) = value
;
1500 =item C<void Parrot_set_pmcreg(PARROT_INTERP, Parrot_Int regnum, Parrot_PMC
1503 Set the value of a P register.
1511 Parrot_set_pmcreg(PARROT_INTERP
, Parrot_Int regnum
,
1514 ASSERT_ARGS(Parrot_set_pmcreg
)
1515 REG_PMC(interp
, regnum
) = value
;
1518 /*=for api extend Parrot_new_string
1523 =item C<Parrot_String Parrot_new_string(PARROT_INTERP, const char *buffer,
1524 Parrot_UInt length, const char * const encoding_name, Parrot_UInt flags)>
1526 Create a new Parrot string from a passed-in buffer. Pass in a 0 for
1527 flags for right now.
1529 A copy of the buffer is made.
1536 PARROT_WARN_UNUSED_RESULT
1537 PARROT_CANNOT_RETURN_NULL
1539 Parrot_new_string(PARROT_INTERP
, ARGIN_NULLOK(const char *buffer
),
1540 Parrot_UInt length
, ARGIN_NULLOK(const char * const encoding_name
),
1543 ASSERT_ARGS(Parrot_new_string
)
1544 Parrot_String retval
;
1545 PARROT_CALLIN_START(interp
);
1546 retval
= string_make(interp
, buffer
, length
, encoding_name
, flags
);
1547 PARROT_CALLIN_END(interp
);
1553 =item C<Parrot_Language Parrot_find_language(PARROT_INTERP, char *language)>
1555 Find the magic language token for a language, by language name.
1562 PARROT_WARN_UNUSED_RESULT
1564 Parrot_find_language(SHIM_INTERP
, SHIM(char *language
))
1566 ASSERT_ARGS(Parrot_find_language
)
1572 =item C<void Parrot_register_pmc(PARROT_INTERP, Parrot_PMC pmc)>
1574 Add a reference of the PMC to the interpreter's GC registry. This prevents PMCs
1575 only known to extension from getting destroyed during GC runs.
1583 Parrot_register_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
1585 ASSERT_ARGS(Parrot_register_pmc
)
1586 PARROT_CALLIN_START(interp
);
1587 gc_register_pmc(interp
, pmc
);
1588 PARROT_CALLIN_END(interp
);
1593 =item C<void Parrot_unregister_pmc(PARROT_INTERP, Parrot_PMC pmc)>
1595 Remove a reference of the PMC from the interpreter's GC registry. If the
1596 reference count reaches zero, the PMC will be destroyed during the next GC run.
1604 Parrot_unregister_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
1606 ASSERT_ARGS(Parrot_unregister_pmc
)
1607 PARROT_CALLIN_START(interp
);
1608 gc_unregister_pmc(interp
, pmc
);
1609 PARROT_CALLIN_END(interp
);
1614 =item C<void Parrot_PMC_set_vtable(PARROT_INTERP, Parrot_PMC pmc, Parrot_VTABLE
1617 Replaces the vtable of the PMC.
1625 Parrot_PMC_set_vtable(SHIM_INTERP
, Parrot_PMC pmc
, Parrot_VTABLE vtable
)
1627 ASSERT_ARGS(Parrot_PMC_set_vtable
)
1628 pmc
->vtable
= vtable
;
1633 =item C<Parrot_VTABLE Parrot_get_vtable(PARROT_INTERP, Parrot_Int id)>
1635 Returns the vtable corresponding to the given PMC ID.
1642 PARROT_PURE_FUNCTION
1644 Parrot_get_vtable(PARROT_INTERP
, Parrot_Int id
)
1646 ASSERT_ARGS(Parrot_get_vtable
)
1647 return interp
->vtables
[id
];
1652 =item C<Parrot_PMC Parrot_sub_new_from_c_func(PARROT_INTERP, void (*func(void)),
1653 const char * signature)>
1655 Returns a PMC sub wrapper for a c function.
1663 Parrot_sub_new_from_c_func(PARROT_INTERP
,
1664 ARGIN(void (*func
)(void)), ARGIN(const char * signature
))
1666 ASSERT_ARGS(Parrot_sub_new_from_c_func
)
1667 Parrot_String sig
= Parrot_new_string(interp
, signature
, strlen(signature
),
1669 Parrot_PMC sub
= pmc_new(interp
, enum_class_NCI
);
1670 VTABLE_set_pointer_keyed_str(interp
, sub
, sig
, F2DPTR(func
));
1671 PObj_get_FLAGS(sub
) |= PObj_private1_FLAG
;
1677 =item C<Parrot_PMC Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)>
1679 Create a class with the type given
1687 Parrot_PMC_newclass(PARROT_INTERP
, Parrot_PMC classtype
)
1689 ASSERT_ARGS(Parrot_PMC_newclass
)
1691 PARROT_CALLIN_START(interp
);
1693 result
= pmc_new_init(interp
, enum_class_Class
, classtype
);
1695 PARROT_CALLIN_END(interp
);
1705 See F<include/parrot/extend.h> and F<docs/pdds/pdd11_extending.pod>.
1709 Initial version by Dan Sugalski.
1718 * c-file-style: "parrot"
1720 * vim: expandtab shiftwidth=4: