2 Copyright (C) 2001-2010, 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_callcontext.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_PMC Parrot_PMC_new(PARROT_INTERP, Parrot_Int type)>
194 Create and return a new PMC.
202 Parrot_PMC_new(PARROT_INTERP
, Parrot_Int type
)
204 ASSERT_ARGS(Parrot_PMC_new
)
206 PARROT_CALLIN_START(interp
);
207 newpmc
= Parrot_pmc_new_noinit(interp
, type
);
208 VTABLE_init(interp
, newpmc
);
209 PARROT_CALLIN_END(interp
);
215 =item C<Parrot_Int Parrot_PMC_typenum(PARROT_INTERP, const char *_class)>
217 Returns the internal identifier that represents the named class.
225 Parrot_PMC_typenum(PARROT_INTERP
, ARGIN_NULLOK(const char *_class
))
227 ASSERT_ARGS(Parrot_PMC_typenum
)
229 PARROT_CALLIN_START(interp
);
230 retval
= Parrot_pmc_get_type_str(interp
, Parrot_str_new(interp
, _class
, 0));
231 PARROT_CALLIN_END(interp
);
237 =item C<Parrot_PMC Parrot_PMC_null(void)>
239 Returns the special C<NULL> PMC.
247 Parrot_PMC_null(void)
249 ASSERT_ARGS(Parrot_PMC_null
)
255 =item C<void Parrot_free_cstring(char *string)>
257 Deallocate a C string that the interpreter has handed to you.
265 Parrot_free_cstring(ARGFREE(char *string
))
267 ASSERT_ARGS(Parrot_free_cstring
)
268 Parrot_str_free_cstring(string
);
273 =item C<void Parrot_ext_call(PARROT_INTERP, Parrot_PMC sub_pmc, const char
276 Call a Parrot subroutine or method with the given function signature. The
277 function signature holds one type character for each argument or return, these
285 Returns come after the arguments, separated by an arrow, so "PN->S" takes a PMC
286 and a float as arguments and returns a string.
288 Pass the variables for the arguments and returns in the same order as the
289 signature, with returns as reference to the variable (so it can be modified).
291 Parrot_ext_call(interp, sub, "P->S", pmc_arg, &string_result);
293 To call a method, pass the object for the method as the first argument, and
294 mark it in the signature as "Pi" ("i" stands for "invocant").
296 Parrot_ext_call(interp, sub, "PiP->S", object_arg, pmc_arg, &string_result);
304 Parrot_ext_call(PARROT_INTERP
, ARGIN(Parrot_PMC sub_pmc
),
305 ARGIN(const char *signature
), ...)
307 ASSERT_ARGS(Parrot_ext_call
)
310 const char *arg_sig
, *ret_sig
;
312 PMC
* const old_call_obj
= Parrot_pcc_get_signature(interp
,
313 CURRENT_CONTEXT(interp
));
314 Parrot_pcc_split_signature_string(signature
, &arg_sig
, &ret_sig
);
316 va_start(args
, signature
);
317 call_obj
= Parrot_pcc_build_call_from_varargs(interp
, PMCNULL
,
320 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, call_obj
);
321 call_obj
= Parrot_pcc_get_signature(interp
, CURRENT_CONTEXT(interp
));
322 Parrot_pcc_fill_params_from_varargs(interp
, call_obj
, ret_sig
, &args
,
323 PARROT_ERRORS_RESULT_COUNT_FLAG
);
325 Parrot_pcc_set_signature(interp
, CURRENT_CONTEXT(interp
), old_call_obj
);
331 =item C<Parrot_Int Parrot_get_intreg(PARROT_INTERP, Parrot_Int regnum)>
333 Return the value of an integer register.
341 Parrot_get_intreg(PARROT_INTERP
, Parrot_Int regnum
)
343 ASSERT_ARGS(Parrot_get_intreg
)
344 return REG_INT(interp
, regnum
);
349 =item C<Parrot_Float Parrot_get_numreg(PARROT_INTERP, Parrot_Int regnum)>
351 Return the value of a numeric register.
359 Parrot_get_numreg(PARROT_INTERP
, Parrot_Int regnum
)
361 ASSERT_ARGS(Parrot_get_numreg
)
362 return REG_NUM(interp
, regnum
);
367 =item C<Parrot_String Parrot_get_strreg(PARROT_INTERP, Parrot_Int regnum)>
369 Return the value of a string register.
377 Parrot_get_strreg(PARROT_INTERP
, Parrot_Int regnum
)
379 ASSERT_ARGS(Parrot_get_strreg
)
380 return REG_STR(interp
, regnum
);
385 =item C<Parrot_PMC Parrot_get_pmcreg(PARROT_INTERP, Parrot_Int regnum)>
387 Return the value of a PMC register.
395 Parrot_get_pmcreg(PARROT_INTERP
, Parrot_Int regnum
)
397 ASSERT_ARGS(Parrot_get_pmcreg
)
398 return REG_PMC(interp
, regnum
);
403 =item C<void Parrot_set_intreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Int
406 Set the value of an I register.
414 Parrot_set_intreg(PARROT_INTERP
, Parrot_Int regnum
,
417 ASSERT_ARGS(Parrot_set_intreg
)
418 REG_INT(interp
, regnum
) = value
;
423 =item C<void Parrot_set_numreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Float
426 Set the value of an N register.
434 Parrot_set_numreg(PARROT_INTERP
, Parrot_Int regnum
,
437 ASSERT_ARGS(Parrot_set_numreg
)
438 REG_NUM(interp
, regnum
) = value
;
443 =item C<void Parrot_set_strreg(PARROT_INTERP, Parrot_Int regnum, Parrot_String
446 Set the value of an S register.
454 Parrot_set_strreg(PARROT_INTERP
, Parrot_Int regnum
,
457 ASSERT_ARGS(Parrot_set_strreg
)
458 REG_STR(interp
, regnum
) = value
;
463 =item C<void Parrot_set_pmcreg(PARROT_INTERP, Parrot_Int regnum, Parrot_PMC
466 Set the value of a P register.
474 Parrot_set_pmcreg(PARROT_INTERP
, Parrot_Int regnum
,
477 ASSERT_ARGS(Parrot_set_pmcreg
)
478 REG_PMC(interp
, regnum
) = value
;
481 /*=for api extend Parrot_new_string
486 =item C<Parrot_String Parrot_new_string(PARROT_INTERP, const char *buffer,
487 Parrot_UInt length, const char * const encoding_name, Parrot_UInt flags)>
489 Create a new Parrot string from a passed-in buffer. Pass in a 0 for
492 A copy of the buffer is made.
499 PARROT_WARN_UNUSED_RESULT
500 PARROT_CANNOT_RETURN_NULL
502 Parrot_new_string(PARROT_INTERP
, ARGIN_NULLOK(const char *buffer
),
503 Parrot_UInt length
, ARGIN_NULLOK(const char * const encoding_name
),
506 ASSERT_ARGS(Parrot_new_string
)
507 Parrot_String retval
;
508 PARROT_CALLIN_START(interp
);
509 retval
= string_make(interp
, buffer
, length
, encoding_name
, flags
);
510 PARROT_CALLIN_END(interp
);
516 =item C<Parrot_Language Parrot_find_language(PARROT_INTERP, char *language)>
518 Find the magic language token for a language, by language name.
525 PARROT_WARN_UNUSED_RESULT
527 Parrot_find_language(SHIM_INTERP
, SHIM(char *language
))
529 ASSERT_ARGS(Parrot_find_language
)
535 =item C<void Parrot_register_pmc(PARROT_INTERP, Parrot_PMC pmc)>
537 Add a reference of the PMC to the interpreter's GC registry. This prevents PMCs
538 only known to extension from getting destroyed during GC runs.
546 Parrot_register_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
548 ASSERT_ARGS(Parrot_register_pmc
)
549 PARROT_CALLIN_START(interp
);
550 Parrot_pmc_gc_register(interp
, pmc
);
551 PARROT_CALLIN_END(interp
);
556 =item C<void Parrot_unregister_pmc(PARROT_INTERP, Parrot_PMC pmc)>
558 Remove a reference of the PMC from the interpreter's GC registry. If the
559 reference count reaches zero, the PMC will be destroyed during the next GC run.
567 Parrot_unregister_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
569 ASSERT_ARGS(Parrot_unregister_pmc
)
570 PARROT_CALLIN_START(interp
);
571 Parrot_pmc_gc_unregister(interp
, pmc
);
572 PARROT_CALLIN_END(interp
);
577 =item C<Parrot_PMC Parrot_sub_new_from_c_func(PARROT_INTERP, void (*func(void)),
578 const char * signature)>
580 Returns a PMC sub wrapper for a c function.
588 Parrot_sub_new_from_c_func(PARROT_INTERP
,
589 ARGIN(void (*func
)(void)), ARGIN(const char * signature
))
591 ASSERT_ARGS(Parrot_sub_new_from_c_func
)
592 Parrot_String sig
= Parrot_new_string(interp
, signature
, strlen(signature
),
594 Parrot_PMC sub
= Parrot_pmc_new(interp
, enum_class_NCI
);
595 VTABLE_set_pointer_keyed_str(interp
, sub
, sig
, F2DPTR(func
));
601 =item C<Parrot_PMC Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)>
603 Create a class with the type given
611 Parrot_PMC_newclass(PARROT_INTERP
, Parrot_PMC classtype
)
613 ASSERT_ARGS(Parrot_PMC_newclass
)
615 PARROT_CALLIN_START(interp
);
617 result
= Parrot_pmc_new_init(interp
, enum_class_Class
, classtype
);
619 PARROT_CALLIN_END(interp
);
629 See F<include/parrot/extend.h> and F<docs/pdds/pdd11_extending.pod>.
633 Initial version by Dan Sugalski.
642 * c-file-style: "parrot"
644 * vim: expandtab shiftwidth=4: