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 append_result(PARROT_INTERP, PMC *sig_object, Parrot_String type,
1017 Helper function between old and new style PCC to append return pointer to signature.
1019 To be removed with deprecated functions.
1026 append_result(PARROT_INTERP
, ARGIN(PMC
*sig_object
), ARGIN(Parrot_String type
), ARGIN(void *result
))
1028 ASSERT_ARGS(append_result
)
1029 Parrot_String full_sig
;
1031 Parrot_PMC return_flags
;
1032 Parrot_Int return_flags_size
;
1034 Parrot_String return_name
= Parrot_str_new_constant(interp
, "returns");
1035 Parrot_String return_flags_name
= Parrot_str_new_constant(interp
, "return_flags");
1036 Parrot_String sig_name
= Parrot_str_new_constant(interp
, "signature");
1038 full_sig
= VTABLE_get_string(interp
, sig_object
);
1040 Parrot_str_concat(interp
, full_sig
, Parrot_str_new_constant(interp
, "->"), 0);
1041 Parrot_str_concat(interp
, full_sig
, type
, 0);
1043 returns
= VTABLE_get_attr_str(interp
, sig_object
, return_name
);
1044 if (PMC_IS_NULL(returns
)) {
1045 returns
= pmc_new(interp
, enum_class_CallSignatureReturns
);
1046 VTABLE_set_attr_str(interp
, sig_object
, return_name
, returns
);
1048 VTABLE_set_pointer_keyed_int(interp
, returns
, VTABLE_elements(interp
, returns
), result
);
1050 /* Update returns_flag */
1051 return_flags
= VTABLE_get_attr_str(interp
, sig_object
, return_flags_name
);
1052 if (PMC_IS_NULL(return_flags
)) {
1053 /* Create return_flags for single element */
1054 return_flags
= pmc_new(interp
, enum_class_FixedIntegerArray
);
1055 return_flags_size
= 0;
1056 VTABLE_set_integer_native(interp
, return_flags
, 1);
1057 VTABLE_set_attr_str(interp
, sig_object
, return_flags_name
, return_flags
);
1060 /* Extend return_flags by one element */
1061 return_flags_size
= VTABLE_elements(interp
, return_flags
);
1062 VTABLE_set_integer_native(interp
, return_flags
, return_flags_size
+ 1);
1064 switch (Parrot_str_indexed(interp
, type
, 0)) {
1066 VTABLE_set_integer_keyed_int(interp
, return_flags
, return_flags_size
,
1068 VTABLE_push_integer(interp
, returns
, PARROT_ARG_INTVAL
);
1071 VTABLE_set_integer_keyed_int(interp
, return_flags
, return_flags_size
,
1072 PARROT_ARG_FLOATVAL
);
1073 VTABLE_push_integer(interp
, returns
, PARROT_ARG_FLOATVAL
);
1076 VTABLE_set_integer_keyed_int(interp
, return_flags
, return_flags_size
,
1078 VTABLE_push_integer(interp
, returns
, PARROT_ARG_STRING
);
1081 VTABLE_set_integer_keyed_int(interp
, return_flags
, return_flags_size
,
1083 VTABLE_push_integer(interp
, returns
, PARROT_ARG_PMC
);
1086 Parrot_ex_throw_from_c_args(interp
, NULL
,
1087 EXCEPTION_INVALID_OPERATION
,
1088 "invalid signature string element!");
1095 =item C<void Parrot_ext_call(PARROT_INTERP, Parrot_PMC sub_pmc, const char
1098 Call a Parrot subroutine or method with the given function signature. The
1099 function signature holds one type character for each argument or return, these
1107 Returns come after the arguments, separated by an arrow, so "PN->S" takes a PMC
1108 and a float as arguments and returns a string.
1110 Pass the variables for the arguments and returns in the same order as the
1111 signature, with returns as reference to the variable (so it can be modified).
1113 Parrot_ext_call(interp, sub, "P->S", pmc_arg, &string_result);
1115 To call a method, pass the object for the method as the first argument, and
1116 mark it in the signature as "Pi" ("i" stands for "invocant").
1118 Parrot_ext_call(interp, sub, "PiP->S", object_arg, pmc_arg, &string_result);
1126 Parrot_ext_call(PARROT_INTERP
, ARGIN(Parrot_PMC sub_pmc
),
1127 ARGIN(const char *signature
), ...)
1129 ASSERT_ARGS(Parrot_ext_call
)
1133 va_start(args
, signature
);
1134 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
, signature
, args
);
1137 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1142 =item C<void * Parrot_call_sub(PARROT_INTERP, Parrot_PMC sub_pmc, const char
1145 Call a parrot subroutine with the given function signature. The first char in
1146 C<signature> denotes the return value. Next chars are arguments.
1148 The return value of this function can be void or a pointer type.
1150 Signature chars are:
1163 PARROT_WARN_UNUSED_RESULT
1164 PARROT_CAN_RETURN_NULL
1166 Parrot_call_sub(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1167 ARGIN(const char *signature
), ...)
1169 ASSERT_ARGS(Parrot_call_sub
)
1173 void *result
= NULL
;
1174 const char *arg_sig
= signature
;
1175 char return_sig
= signature
[0];
1178 va_start(args
, signature
);
1179 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
,
1183 /* Add the return argument onto the call signature object (a bit
1184 * hackish, added for backward compatibility in deprecated API function,
1186 switch (return_sig
) {
1189 Parrot_String full_sig
= VTABLE_get_string(interp
, sig_object
);
1190 Parrot_str_concat(interp
, full_sig
,
1191 Parrot_str_new_constant(interp
, "->"), 0);
1197 append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "P"), &result
);
1201 Parrot_ex_throw_from_c_args(interp
, NULL
,
1202 EXCEPTION_INVALID_OPERATION
,
1203 "Dispatch: invalid return type %c!", return_sig
);
1206 PMC_get_sub(interp
, sub_pmc
, sub
);
1207 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
),
1208 sub
->seg
->const_table
->constants
);
1209 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1217 =item C<Parrot_Int Parrot_call_sub_ret_int(PARROT_INTERP, Parrot_PMC sub_pmc,
1218 const char *signature, ...)>
1220 Like C<Parrot_call_sub>, with Parrot_Int return result.
1228 Parrot_call_sub_ret_int(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1229 ARGIN(const char *signature
), ...)
1231 ASSERT_ARGS(Parrot_call_sub_ret_int
)
1235 char return_sig
= signature
[0];
1236 const char *arg_sig
= signature
;
1240 va_start(args
, signature
);
1241 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
, arg_sig
, args
);
1244 /* Add the return argument onto the call signature object (a bit
1245 * hackish, added for backward compatibility in deprecated API function,
1247 append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "I"), &result
);
1248 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1255 =item C<Parrot_Float Parrot_call_sub_ret_float(PARROT_INTERP, Parrot_PMC
1256 sub_pmc, const char *signature, ...)>
1258 Like C<Parrot_call_sub>, with Parrot_Float return result.
1266 Parrot_call_sub_ret_float(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1267 ARGIN(const char *signature
), ...)
1269 ASSERT_ARGS(Parrot_call_sub_ret_float
)
1272 Parrot_Float result
;
1273 char return_sig
= signature
[0];
1274 const char *arg_sig
= signature
;
1278 va_start(args
, signature
);
1279 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, PMCNULL
, arg_sig
, args
);
1282 /* Add the return argument onto the call signature object (a bit
1283 * hackish, added for backward compatibility in deprecated API function,
1285 append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "N"), &result
);
1286 PMC_get_sub(interp
, sub_pmc
, sub
);
1287 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1288 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1295 =item C<void * Parrot_call_method(PARROT_INTERP, Parrot_PMC sub_pmc, Parrot_PMC
1296 obj, Parrot_String method, const char *signature, ...)>
1298 Call the parrot subroutine C<sub> as a method on PMC object C<obj>. The method
1299 should have the name C<method> as a Parrot_string, and should have a function
1300 signature C<signature>. Any arguments to the method can be passed at the end
1301 as a variadic argument list.
1308 PARROT_WARN_UNUSED_RESULT
1309 PARROT_CAN_RETURN_NULL
1311 Parrot_call_method(PARROT_INTERP
, Parrot_PMC sub_pmc
, Parrot_PMC obj
,
1312 Parrot_String method
, ARGIN(const char *signature
), ...)
1314 ASSERT_ARGS(Parrot_call_method
)
1317 void *result
= NULL
;
1318 char return_sig
= signature
[0];
1319 char *arg_sig
= (char*)malloc(strlen(signature
)+2);
1324 strcat(arg_sig
, signature
);
1326 va_start(args
, signature
);
1327 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, obj
, arg_sig
, args
);
1331 /* Add the return argument onto the call signature object (a bit
1332 * hackish, added for backward compatibility in deprecated API function,
1334 switch (return_sig
) {
1337 Parrot_String full_sig
= VTABLE_get_string(interp
, sig_object
);
1338 Parrot_str_concat(interp
, full_sig
,
1339 Parrot_str_new_constant(interp
, "->"), 0);
1345 append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "P"), &result
);
1349 Parrot_ex_throw_from_c_args(interp
, NULL
, EXCEPTION_INVALID_OPERATION
,
1350 "Dispatch: invalid return type %c!", return_sig
);
1353 PMC_get_sub(interp
, sub_pmc
, sub
);
1354 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1355 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1362 =item C<Parrot_Int Parrot_call_method_ret_int(PARROT_INTERP, Parrot_PMC sub_pmc,
1363 Parrot_PMC obj, Parrot_String method, const char *signature, ...)>
1365 Call the parrot subroutine C<sub> as a method on PMC object C<obj>. The method
1366 should have the name C<method> as a Parrot_string, and should have a function
1367 signature C<signature>. Any arguments to the method can be passed at the end
1368 as a variadic argument list.
1376 Parrot_call_method_ret_int(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1377 Parrot_PMC obj
, Parrot_String method
, ARGIN(const char *signature
), ...)
1379 ASSERT_ARGS(Parrot_call_method_ret_int
)
1382 Parrot_Int result
= 0;
1383 char return_sig
= signature
[0];
1384 char *arg_sig
= (char*)malloc(strlen(signature
)+2);
1389 strcat(arg_sig
, signature
);
1391 va_start(args
, signature
);
1392 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, obj
, arg_sig
, args
);
1396 append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "I"), &result
);
1397 PMC_get_sub(interp
, sub_pmc
, sub
);
1398 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1399 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1406 =item C<Parrot_Float Parrot_call_method_ret_float(PARROT_INTERP, Parrot_PMC
1407 sub_pmc, Parrot_PMC obj, Parrot_String method, const char *signature, ...)>
1409 Call a parrot method for the given object.
1417 Parrot_call_method_ret_float(PARROT_INTERP
, Parrot_PMC sub_pmc
,
1418 Parrot_PMC obj
, Parrot_String method
, ARGIN(const char *signature
), ...)
1420 ASSERT_ARGS(Parrot_call_method_ret_float
)
1423 Parrot_Float result
= 0.0;
1424 char return_sig
= signature
[0];
1425 char *arg_sig
= (char*)malloc(strlen(signature
)+2);
1430 strcat(arg_sig
, signature
);
1432 va_start(args
, signature
);
1433 sig_object
= Parrot_pcc_build_sig_object_from_varargs(interp
, obj
, arg_sig
, args
);
1437 append_result(interp
, sig_object
, Parrot_str_new_constant(interp
, "N"), &result
);
1438 PMC_get_sub(interp
, sub_pmc
, sub
);
1439 Parrot_pcc_set_constants(interp
, CURRENT_CONTEXT(interp
), sub
->seg
->const_table
->constants
);
1440 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, sig_object
);
1447 =item C<Parrot_Int Parrot_get_intreg(PARROT_INTERP, Parrot_Int regnum)>
1449 Return the value of an integer register.
1457 Parrot_get_intreg(PARROT_INTERP
, Parrot_Int regnum
)
1459 ASSERT_ARGS(Parrot_get_intreg
)
1460 return REG_INT(interp
, regnum
);
1465 =item C<Parrot_Float Parrot_get_numreg(PARROT_INTERP, Parrot_Int regnum)>
1467 Return the value of a numeric register.
1475 Parrot_get_numreg(PARROT_INTERP
, Parrot_Int regnum
)
1477 ASSERT_ARGS(Parrot_get_numreg
)
1478 return REG_NUM(interp
, regnum
);
1483 =item C<Parrot_String Parrot_get_strreg(PARROT_INTERP, Parrot_Int regnum)>
1485 Return the value of a string register.
1493 Parrot_get_strreg(PARROT_INTERP
, Parrot_Int regnum
)
1495 ASSERT_ARGS(Parrot_get_strreg
)
1496 return REG_STR(interp
, regnum
);
1501 =item C<Parrot_PMC Parrot_get_pmcreg(PARROT_INTERP, Parrot_Int regnum)>
1503 Return the value of a PMC register.
1511 Parrot_get_pmcreg(PARROT_INTERP
, Parrot_Int regnum
)
1513 ASSERT_ARGS(Parrot_get_pmcreg
)
1514 return REG_PMC(interp
, regnum
);
1519 =item C<void Parrot_set_intreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Int
1522 Set the value of an I register.
1530 Parrot_set_intreg(PARROT_INTERP
, Parrot_Int regnum
,
1533 ASSERT_ARGS(Parrot_set_intreg
)
1534 REG_INT(interp
, regnum
) = value
;
1539 =item C<void Parrot_set_numreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Float
1542 Set the value of an N register.
1550 Parrot_set_numreg(PARROT_INTERP
, Parrot_Int regnum
,
1553 ASSERT_ARGS(Parrot_set_numreg
)
1554 REG_NUM(interp
, regnum
) = value
;
1559 =item C<void Parrot_set_strreg(PARROT_INTERP, Parrot_Int regnum, Parrot_String
1562 Set the value of an S register.
1570 Parrot_set_strreg(PARROT_INTERP
, Parrot_Int regnum
,
1571 Parrot_String value
)
1573 ASSERT_ARGS(Parrot_set_strreg
)
1574 REG_STR(interp
, regnum
) = value
;
1579 =item C<void Parrot_set_pmcreg(PARROT_INTERP, Parrot_Int regnum, Parrot_PMC
1582 Set the value of a P register.
1590 Parrot_set_pmcreg(PARROT_INTERP
, Parrot_Int regnum
,
1593 ASSERT_ARGS(Parrot_set_pmcreg
)
1594 REG_PMC(interp
, regnum
) = value
;
1597 /*=for api extend Parrot_new_string
1602 =item C<Parrot_String Parrot_new_string(PARROT_INTERP, const char *buffer,
1603 Parrot_UInt length, const char * const encoding_name, Parrot_UInt flags)>
1605 Create a new Parrot string from a passed-in buffer. Pass in a 0 for
1606 flags for right now.
1608 A copy of the buffer is made.
1615 PARROT_WARN_UNUSED_RESULT
1616 PARROT_CANNOT_RETURN_NULL
1618 Parrot_new_string(PARROT_INTERP
, ARGIN_NULLOK(const char *buffer
),
1619 Parrot_UInt length
, ARGIN_NULLOK(const char * const encoding_name
),
1622 ASSERT_ARGS(Parrot_new_string
)
1623 Parrot_String retval
;
1624 PARROT_CALLIN_START(interp
);
1625 retval
= string_make(interp
, buffer
, length
, encoding_name
, flags
);
1626 PARROT_CALLIN_END(interp
);
1632 =item C<Parrot_Language Parrot_find_language(PARROT_INTERP, char *language)>
1634 Find the magic language token for a language, by language name.
1641 PARROT_WARN_UNUSED_RESULT
1643 Parrot_find_language(SHIM_INTERP
, SHIM(char *language
))
1645 ASSERT_ARGS(Parrot_find_language
)
1651 =item C<void Parrot_register_pmc(PARROT_INTERP, Parrot_PMC pmc)>
1653 Add a reference of the PMC to the interpreter's GC registry. This prevents PMCs
1654 only known to extension from getting destroyed during GC runs.
1662 Parrot_register_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
1664 ASSERT_ARGS(Parrot_register_pmc
)
1665 PARROT_CALLIN_START(interp
);
1666 gc_register_pmc(interp
, pmc
);
1667 PARROT_CALLIN_END(interp
);
1672 =item C<void Parrot_unregister_pmc(PARROT_INTERP, Parrot_PMC pmc)>
1674 Remove a reference of the PMC from the interpreter's GC registry. If the
1675 reference count reaches zero, the PMC will be destroyed during the next GC run.
1683 Parrot_unregister_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
1685 ASSERT_ARGS(Parrot_unregister_pmc
)
1686 PARROT_CALLIN_START(interp
);
1687 gc_unregister_pmc(interp
, pmc
);
1688 PARROT_CALLIN_END(interp
);
1693 =item C<void Parrot_PMC_set_vtable(PARROT_INTERP, Parrot_PMC pmc, Parrot_VTABLE
1696 Replaces the vtable of the PMC.
1704 Parrot_PMC_set_vtable(SHIM_INTERP
, Parrot_PMC pmc
, Parrot_VTABLE vtable
)
1706 ASSERT_ARGS(Parrot_PMC_set_vtable
)
1707 pmc
->vtable
= vtable
;
1712 =item C<Parrot_VTABLE Parrot_get_vtable(PARROT_INTERP, Parrot_Int id)>
1714 Returns the vtable corresponding to the given PMC ID.
1721 PARROT_PURE_FUNCTION
1723 Parrot_get_vtable(PARROT_INTERP
, Parrot_Int id
)
1725 ASSERT_ARGS(Parrot_get_vtable
)
1726 return interp
->vtables
[id
];
1731 =item C<Parrot_PMC Parrot_sub_new_from_c_func(PARROT_INTERP, void (*func(void)),
1732 const char * signature)>
1734 Returns a PMC sub wrapper for a c function.
1742 Parrot_sub_new_from_c_func(PARROT_INTERP
,
1743 ARGIN(void (*func
)(void)), ARGIN(const char * signature
))
1745 ASSERT_ARGS(Parrot_sub_new_from_c_func
)
1746 Parrot_String sig
= Parrot_new_string(interp
, signature
, strlen(signature
),
1748 Parrot_PMC sub
= pmc_new(interp
, enum_class_NCI
);
1749 VTABLE_set_pointer_keyed_str(interp
, sub
, sig
, F2DPTR(func
));
1750 PObj_get_FLAGS(sub
) |= PObj_private1_FLAG
;
1756 =item C<Parrot_PMC Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)>
1758 Create a class with the type given
1766 Parrot_PMC_newclass(PARROT_INTERP
, Parrot_PMC classtype
)
1768 ASSERT_ARGS(Parrot_PMC_newclass
)
1770 PARROT_CALLIN_START(interp
);
1772 result
= pmc_new_init(interp
, enum_class_Class
, classtype
);
1774 PARROT_CALLIN_END(interp
);
1784 See F<include/parrot/extend.h> and F<docs/pdds/pdd11_extending.pod>.
1788 Initial version by Dan Sugalski.
1797 * c-file-style: "parrot"
1799 * vim: expandtab shiftwidth=4: