fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / src / extend.c
blob3e9499ad077e79b7943008087839e430d055ffc3
1 /*
2 Copyright (C) 2001-2010, Parrot Foundation.
3 $Id$
5 =head1 NAME
7 src/extend.c - Parrot extension interface
9 =head1 DESCRIPTION
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
13 bytecode) may access.
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
18 can.
20 =head2 Functions
22 =over 4
24 =cut
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
42 the problem.
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
48 and has some code.
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,
71 va_list args)>
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>.
87 =cut
91 PARROT_EXPORT
92 int
93 Parrot_vfprintf(PARROT_INTERP, ARGIN(Parrot_PMC pio),
94 ARGIN(const char *s), va_list args)
96 ASSERT_ARGS(Parrot_vfprintf)
97 STRING * str;
98 INTVAL retval;
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);
105 return retval;
108 PARROT_EXPORT
110 Parrot_fprintf(PARROT_INTERP, ARGIN(Parrot_PMC pio),
111 ARGIN(const char *s), ...)
113 ASSERT_ARGS(Parrot_fprintf)
114 va_list args;
115 INTVAL retval;
117 va_start(args, s);
118 retval = Parrot_vfprintf(interp, pio, s, args);
119 va_end(args);
121 return retval;
124 PARROT_EXPORT
126 Parrot_printf(NULLOK_INTERP, ARGIN(const char *s), ...)
128 ASSERT_ARGS(Parrot_printf)
129 va_list args;
130 INTVAL retval;
131 va_start(args, s);
133 if (interp) {
134 retval = Parrot_vfprintf(interp, Parrot_io_STDOUT(interp), s, args);
136 else {
137 /* Be nice about this...
138 ** XXX BD Should this use the default Parrot_io_STDOUT or something?
140 retval = vfprintf(stdout, s, args);
142 va_end(args);
144 return retval;
147 PARROT_EXPORT
149 Parrot_eprintf(NULLOK_INTERP, ARGIN(const char *s), ...)
151 ASSERT_ARGS(Parrot_eprintf)
152 va_list args;
153 INTVAL retval;
155 va_start(args, s);
157 if (interp) {
158 retval = Parrot_vfprintf(interp, Parrot_io_STDERR(interp), s, args);
160 else {
161 /* Be nice about this...
162 ** XXX BD Should this use the default Parrot_io_STDOUT or something?
164 retval=vfprintf(stderr, s, args);
167 va_end(args);
169 return retval;
174 =item C<Parrot_PMC Parrot_get_root_namespace(PARROT_INTERP)>
176 Return the root namespace
178 =cut
182 PARROT_EXPORT
183 PARROT_PURE_FUNCTION
184 Parrot_PMC
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.
197 =cut
201 PARROT_EXPORT
202 Parrot_PMC
203 Parrot_PMC_new(PARROT_INTERP, Parrot_Int type)
205 ASSERT_ARGS(Parrot_PMC_new)
206 Parrot_PMC newpmc;
207 PARROT_CALLIN_START(interp);
208 newpmc = Parrot_pmc_new_noinit(interp, type);
209 VTABLE_init(interp, newpmc);
210 PARROT_CALLIN_END(interp);
211 return newpmc;
216 =item C<Parrot_Int Parrot_PMC_typenum(PARROT_INTERP, const char *_class)>
218 Returns the internal identifier that represents the named class.
220 =cut
224 PARROT_EXPORT
225 Parrot_Int
226 Parrot_PMC_typenum(PARROT_INTERP, ARGIN_NULLOK(const char *_class))
228 ASSERT_ARGS(Parrot_PMC_typenum)
229 Parrot_Int retval;
230 PARROT_CALLIN_START(interp);
231 retval = Parrot_pmc_get_type_str(interp, Parrot_str_new(interp, _class, 0));
232 PARROT_CALLIN_END(interp);
233 return retval;
238 =item C<Parrot_PMC Parrot_PMC_null(void)>
240 Returns the special C<NULL> PMC.
242 =cut
246 PARROT_EXPORT
247 PARROT_PURE_FUNCTION
248 Parrot_PMC
249 Parrot_PMC_null(void)
251 ASSERT_ARGS(Parrot_PMC_null)
252 return PMCNULL;
257 =item C<void Parrot_free_cstring(char *string)>
259 Deallocate a C string that the interpreter has handed to you.
261 =cut
265 PARROT_EXPORT
266 void
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
276 *signature, ...)>
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
280 are:
282 I ... Parrot_Int
283 N ... Parrot_Float
284 S ... Parrot_String
285 P ... Parrot_PMC
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);
300 =cut
304 PARROT_EXPORT
305 void
306 Parrot_ext_call(PARROT_INTERP, ARGIN(Parrot_PMC sub_pmc),
307 ARGIN(const char *signature), ...)
309 ASSERT_ARGS(Parrot_ext_call)
310 va_list args;
311 PMC *call_obj;
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,
320 arg_sig, &args);
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);
326 va_end(args);
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.
337 =cut
341 PARROT_EXPORT
342 PARROT_PURE_FUNCTION
343 Parrot_Int
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.
356 =cut
360 PARROT_EXPORT
361 PARROT_PURE_FUNCTION
362 Parrot_Float
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.
375 =cut
379 PARROT_EXPORT
380 PARROT_PURE_FUNCTION
381 Parrot_String
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.
394 =cut
398 PARROT_EXPORT
399 PARROT_PURE_FUNCTION
400 Parrot_PMC
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
410 value)>
412 Set the value of an I register.
414 =cut
418 PARROT_EXPORT
419 void
420 Parrot_set_intreg(PARROT_INTERP, Parrot_Int regnum,
421 Parrot_Int value)
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
430 value)>
432 Set the value of an N register.
434 =cut
438 PARROT_EXPORT
439 void
440 Parrot_set_numreg(PARROT_INTERP, Parrot_Int regnum,
441 Parrot_Float value)
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
450 value)>
452 Set the value of an S register.
454 =cut
458 PARROT_EXPORT
459 void
460 Parrot_set_strreg(PARROT_INTERP, Parrot_Int regnum,
461 Parrot_String value)
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
470 value)>
472 Set the value of a P register.
474 =cut
478 PARROT_EXPORT
479 void
480 Parrot_set_pmcreg(PARROT_INTERP, Parrot_Int regnum,
481 Parrot_PMC value)
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
496 flags for right now.
498 A copy of the buffer is made.
500 =cut
504 PARROT_EXPORT
505 PARROT_WARN_UNUSED_RESULT
506 PARROT_CANNOT_RETURN_NULL
507 Parrot_String
508 Parrot_new_string(PARROT_INTERP, ARGIN_NULLOK(const char *buffer),
509 Parrot_UInt length, ARGIN_NULLOK(const char * const encoding_name),
510 Parrot_UInt flags)
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);
517 return retval;
522 =item C<Parrot_Language Parrot_find_language(PARROT_INTERP, const char
523 *language)>
525 Find the magic language token for a language, by language name.
527 =cut
531 PARROT_EXPORT
532 PARROT_PURE_FUNCTION
533 PARROT_WARN_UNUSED_RESULT
534 Parrot_Language
535 Parrot_find_language(SHIM_INTERP, SHIM(const char *language))
537 ASSERT_ARGS(Parrot_find_language)
538 return 0;
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.
548 =cut
552 PARROT_EXPORT
553 void
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.
569 =cut
573 PARROT_EXPORT
574 void
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.
590 =cut
594 PARROT_EXPORT
595 Parrot_PMC
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),
601 (char *) NULL, 0);
602 Parrot_PMC sub = Parrot_pmc_new(interp, enum_class_NCI);
603 VTABLE_set_pointer_keyed_str(interp, sub, sig, F2DPTR(func));
604 return sub;
609 =item C<Parrot_PMC Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)>
611 Create a class with the type given
613 =cut
617 PARROT_EXPORT
618 Parrot_PMC
619 Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)
621 ASSERT_ARGS(Parrot_PMC_newclass)
622 Parrot_PMC result;
623 PARROT_CALLIN_START(interp);
625 result = Parrot_pmc_new_init(interp, enum_class_Class, classtype);
627 PARROT_CALLIN_END(interp);
628 return result;
633 =back
635 =head1 SEE ALSO
637 See F<include/parrot/extend.h> and F<docs/pdds/pdd11_extending.pod>.
639 =head1 HISTORY
641 Initial version by Dan Sugalski.
643 =cut
649 * Local variables:
650 * c-file-style: "parrot"
651 * End:
652 * vim: expandtab shiftwidth=4: