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
185 Parrot_get_root_namespace(PARROT_INTERP
)
187 ASSERT_ARGS(Parrot_get_root_namespace
)
188 return interp
->root_namespace
;
193 =item C<Parrot_PMC Parrot_PMC_new(PARROT_INTERP, Parrot_Int type)>
195 Create and return a new PMC.
203 Parrot_PMC_new(PARROT_INTERP
, Parrot_Int type
)
205 ASSERT_ARGS(Parrot_PMC_new
)
207 PARROT_CALLIN_START(interp
);
208 newpmc
= Parrot_pmc_new_noinit(interp
, type
);
209 VTABLE_init(interp
, newpmc
);
210 PARROT_CALLIN_END(interp
);
216 =item C<Parrot_Int Parrot_PMC_typenum(PARROT_INTERP, const char *_class)>
218 Returns the internal identifier that represents the named class.
226 Parrot_PMC_typenum(PARROT_INTERP
, ARGIN_NULLOK(const char *_class
))
228 ASSERT_ARGS(Parrot_PMC_typenum
)
230 PARROT_CALLIN_START(interp
);
231 retval
= Parrot_pmc_get_type_str(interp
, Parrot_str_new(interp
, _class
, 0));
232 PARROT_CALLIN_END(interp
);
238 =item C<Parrot_PMC Parrot_PMC_null(void)>
240 Returns the special C<NULL> PMC.
249 Parrot_PMC_null(void)
251 ASSERT_ARGS(Parrot_PMC_null
)
257 =item C<void Parrot_free_cstring(char *string)>
259 Deallocate a C string that the interpreter has handed to you.
267 Parrot_free_cstring(ARGFREE(char *string
))
269 ASSERT_ARGS(Parrot_free_cstring
)
270 Parrot_str_free_cstring(string
);
275 =item C<void Parrot_ext_call(PARROT_INTERP, Parrot_PMC sub_pmc, const char
278 Call a Parrot subroutine or method with the given function signature. The
279 function signature holds one type character for each argument or return, these
287 Returns come after the arguments, separated by an arrow, so "PN->S" takes a PMC
288 and a float as arguments and returns a string.
290 Pass the variables for the arguments and returns in the same order as the
291 signature, with returns as reference to the variable (so it can be modified).
293 Parrot_ext_call(interp, sub, "P->S", pmc_arg, &string_result);
295 To call a method, pass the object for the method as the first argument, and
296 mark it in the signature as "Pi" ("i" stands for "invocant").
298 Parrot_ext_call(interp, sub, "PiP->S", object_arg, pmc_arg, &string_result);
306 Parrot_ext_call(PARROT_INTERP
, ARGIN(Parrot_PMC sub_pmc
),
307 ARGIN(const char *signature
), ...)
309 ASSERT_ARGS(Parrot_ext_call
)
312 const char *arg_sig
, *ret_sig
;
314 PMC
* const old_call_obj
= Parrot_pcc_get_signature(interp
,
315 CURRENT_CONTEXT(interp
));
316 Parrot_pcc_split_signature_string(signature
, &arg_sig
, &ret_sig
);
318 va_start(args
, signature
);
319 call_obj
= Parrot_pcc_build_call_from_varargs(interp
, PMCNULL
,
322 Parrot_pcc_invoke_from_sig_object(interp
, sub_pmc
, call_obj
);
323 call_obj
= Parrot_pcc_get_signature(interp
, CURRENT_CONTEXT(interp
));
324 Parrot_pcc_fill_params_from_varargs(interp
, call_obj
, ret_sig
, &args
,
325 PARROT_ERRORS_RESULT_COUNT_FLAG
);
327 Parrot_pcc_set_signature(interp
, CURRENT_CONTEXT(interp
), old_call_obj
);
333 =item C<Parrot_Int Parrot_get_intreg(PARROT_INTERP, Parrot_Int regnum)>
335 Return the value of an integer register.
344 Parrot_get_intreg(PARROT_INTERP
, Parrot_Int regnum
)
346 ASSERT_ARGS(Parrot_get_intreg
)
347 return REG_INT(interp
, regnum
);
352 =item C<Parrot_Float Parrot_get_numreg(PARROT_INTERP, Parrot_Int regnum)>
354 Return the value of a numeric register.
363 Parrot_get_numreg(PARROT_INTERP
, Parrot_Int regnum
)
365 ASSERT_ARGS(Parrot_get_numreg
)
366 return REG_NUM(interp
, regnum
);
371 =item C<Parrot_String Parrot_get_strreg(PARROT_INTERP, Parrot_Int regnum)>
373 Return the value of a string register.
382 Parrot_get_strreg(PARROT_INTERP
, Parrot_Int regnum
)
384 ASSERT_ARGS(Parrot_get_strreg
)
385 return REG_STR(interp
, regnum
);
390 =item C<Parrot_PMC Parrot_get_pmcreg(PARROT_INTERP, Parrot_Int regnum)>
392 Return the value of a PMC register.
401 Parrot_get_pmcreg(PARROT_INTERP
, Parrot_Int regnum
)
403 ASSERT_ARGS(Parrot_get_pmcreg
)
404 return REG_PMC(interp
, regnum
);
409 =item C<void Parrot_set_intreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Int
412 Set the value of an I register.
420 Parrot_set_intreg(PARROT_INTERP
, Parrot_Int regnum
,
423 ASSERT_ARGS(Parrot_set_intreg
)
424 REG_INT(interp
, regnum
) = value
;
429 =item C<void Parrot_set_numreg(PARROT_INTERP, Parrot_Int regnum, Parrot_Float
432 Set the value of an N register.
440 Parrot_set_numreg(PARROT_INTERP
, Parrot_Int regnum
,
443 ASSERT_ARGS(Parrot_set_numreg
)
444 REG_NUM(interp
, regnum
) = value
;
449 =item C<void Parrot_set_strreg(PARROT_INTERP, Parrot_Int regnum, Parrot_String
452 Set the value of an S register.
460 Parrot_set_strreg(PARROT_INTERP
, Parrot_Int regnum
,
463 ASSERT_ARGS(Parrot_set_strreg
)
464 REG_STR(interp
, regnum
) = value
;
469 =item C<void Parrot_set_pmcreg(PARROT_INTERP, Parrot_Int regnum, Parrot_PMC
472 Set the value of a P register.
480 Parrot_set_pmcreg(PARROT_INTERP
, Parrot_Int regnum
,
483 ASSERT_ARGS(Parrot_set_pmcreg
)
484 REG_PMC(interp
, regnum
) = value
;
487 /*=for api extend Parrot_new_string
492 =item C<Parrot_String Parrot_new_string(PARROT_INTERP, const char *buffer,
493 Parrot_UInt length, const char * const encoding_name, Parrot_UInt flags)>
495 Create a new Parrot string from a passed-in buffer. Pass in a 0 for
498 A copy of the buffer is made.
505 PARROT_WARN_UNUSED_RESULT
506 PARROT_CANNOT_RETURN_NULL
508 Parrot_new_string(PARROT_INTERP
, ARGIN_NULLOK(const char *buffer
),
509 Parrot_UInt length
, ARGIN_NULLOK(const char * const encoding_name
),
512 ASSERT_ARGS(Parrot_new_string
)
513 Parrot_String retval
;
514 PARROT_CALLIN_START(interp
);
515 retval
= string_make(interp
, buffer
, length
, encoding_name
, flags
);
516 PARROT_CALLIN_END(interp
);
522 =item C<Parrot_Language Parrot_find_language(PARROT_INTERP, const char
525 Find the magic language token for a language, by language name.
533 PARROT_WARN_UNUSED_RESULT
535 Parrot_find_language(SHIM_INTERP
, SHIM(const char *language
))
537 ASSERT_ARGS(Parrot_find_language
)
543 =item C<void Parrot_register_pmc(PARROT_INTERP, Parrot_PMC pmc)>
545 Add a reference of the PMC to the interpreter's GC registry. This prevents PMCs
546 only known to extension from getting destroyed during GC runs.
554 Parrot_register_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
556 ASSERT_ARGS(Parrot_register_pmc
)
557 PARROT_CALLIN_START(interp
);
558 Parrot_pmc_gc_register(interp
, pmc
);
559 PARROT_CALLIN_END(interp
);
564 =item C<void Parrot_unregister_pmc(PARROT_INTERP, Parrot_PMC pmc)>
566 Remove a reference of the PMC from the interpreter's GC registry. If the
567 reference count reaches zero, the PMC will be destroyed during the next GC run.
575 Parrot_unregister_pmc(PARROT_INTERP
, Parrot_PMC pmc
)
577 ASSERT_ARGS(Parrot_unregister_pmc
)
578 PARROT_CALLIN_START(interp
);
579 Parrot_pmc_gc_unregister(interp
, pmc
);
580 PARROT_CALLIN_END(interp
);
585 =item C<Parrot_PMC Parrot_sub_new_from_c_func(PARROT_INTERP, void (*func(void)),
586 const char * signature)>
588 Returns a PMC sub wrapper for a c function.
596 Parrot_sub_new_from_c_func(PARROT_INTERP
,
597 ARGIN(void (*func
)(void)), ARGIN(const char * signature
))
599 ASSERT_ARGS(Parrot_sub_new_from_c_func
)
600 Parrot_String sig
= Parrot_new_string(interp
, signature
, strlen(signature
),
602 Parrot_PMC sub
= Parrot_pmc_new(interp
, enum_class_NCI
);
603 VTABLE_set_pointer_keyed_str(interp
, sub
, sig
, F2DPTR(func
));
609 =item C<Parrot_PMC Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)>
611 Create a class with the type given
619 Parrot_PMC_newclass(PARROT_INTERP
, Parrot_PMC classtype
)
621 ASSERT_ARGS(Parrot_PMC_newclass
)
623 PARROT_CALLIN_START(interp
);
625 result
= Parrot_pmc_new_init(interp
, enum_class_Class
, classtype
);
627 PARROT_CALLIN_END(interp
);
637 See F<include/parrot/extend.h> and F<docs/pdds/pdd11_extending.pod>.
641 Initial version by Dan Sugalski.
650 * c-file-style: "parrot"
652 * vim: expandtab shiftwidth=4: