remove deprecation notice for TT #449
[parrot.git] / src / extend.c
blob1930cc29181aebf94c3d0f5970e5b68c8ba9c608
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_PMC
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.
196 =cut
200 PARROT_EXPORT
201 Parrot_PMC
202 Parrot_PMC_new(PARROT_INTERP, Parrot_Int type)
204 ASSERT_ARGS(Parrot_PMC_new)
205 Parrot_PMC newpmc;
206 PARROT_CALLIN_START(interp);
207 newpmc = Parrot_pmc_new_noinit(interp, type);
208 VTABLE_init(interp, newpmc);
209 PARROT_CALLIN_END(interp);
210 return newpmc;
215 =item C<Parrot_Int Parrot_PMC_typenum(PARROT_INTERP, const char *_class)>
217 Returns the internal identifier that represents the named class.
219 =cut
223 PARROT_EXPORT
224 Parrot_Int
225 Parrot_PMC_typenum(PARROT_INTERP, ARGIN_NULLOK(const char *_class))
227 ASSERT_ARGS(Parrot_PMC_typenum)
228 Parrot_Int retval;
229 PARROT_CALLIN_START(interp);
230 retval = Parrot_pmc_get_type_str(interp, Parrot_str_new(interp, _class, 0));
231 PARROT_CALLIN_END(interp);
232 return retval;
237 =item C<Parrot_PMC Parrot_PMC_null(void)>
239 Returns the special C<NULL> PMC.
241 =cut
245 PARROT_EXPORT
246 Parrot_PMC
247 Parrot_PMC_null(void)
249 ASSERT_ARGS(Parrot_PMC_null)
250 return PMCNULL;
255 =item C<void Parrot_free_cstring(char *string)>
257 Deallocate a C string that the interpreter has handed to you.
259 =cut
263 PARROT_EXPORT
264 void
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
274 *signature, ...)>
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
278 are:
280 I ... Parrot_Int
281 N ... Parrot_Float
282 S ... Parrot_String
283 P ... Parrot_PMC
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);
298 =cut
302 PARROT_EXPORT
303 void
304 Parrot_ext_call(PARROT_INTERP, ARGIN(Parrot_PMC sub_pmc),
305 ARGIN(const char *signature), ...)
307 ASSERT_ARGS(Parrot_ext_call)
308 va_list args;
309 PMC *call_obj;
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,
318 arg_sig, &args);
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);
324 va_end(args);
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.
335 =cut
339 PARROT_EXPORT
340 Parrot_Int
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.
353 =cut
357 PARROT_EXPORT
358 Parrot_Float
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.
371 =cut
375 PARROT_EXPORT
376 Parrot_String
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.
389 =cut
393 PARROT_EXPORT
394 Parrot_PMC
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
404 value)>
406 Set the value of an I register.
408 =cut
412 PARROT_EXPORT
413 void
414 Parrot_set_intreg(PARROT_INTERP, Parrot_Int regnum,
415 Parrot_Int value)
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
424 value)>
426 Set the value of an N register.
428 =cut
432 PARROT_EXPORT
433 void
434 Parrot_set_numreg(PARROT_INTERP, Parrot_Int regnum,
435 Parrot_Float value)
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
444 value)>
446 Set the value of an S register.
448 =cut
452 PARROT_EXPORT
453 void
454 Parrot_set_strreg(PARROT_INTERP, Parrot_Int regnum,
455 Parrot_String value)
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
464 value)>
466 Set the value of a P register.
468 =cut
472 PARROT_EXPORT
473 void
474 Parrot_set_pmcreg(PARROT_INTERP, Parrot_Int regnum,
475 Parrot_PMC value)
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
490 flags for right now.
492 A copy of the buffer is made.
494 =cut
498 PARROT_EXPORT
499 PARROT_WARN_UNUSED_RESULT
500 PARROT_CANNOT_RETURN_NULL
501 Parrot_String
502 Parrot_new_string(PARROT_INTERP, ARGIN_NULLOK(const char *buffer),
503 Parrot_UInt length, ARGIN_NULLOK(const char * const encoding_name),
504 Parrot_UInt flags)
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);
511 return retval;
516 =item C<Parrot_Language Parrot_find_language(PARROT_INTERP, char *language)>
518 Find the magic language token for a language, by language name.
520 =cut
524 PARROT_EXPORT
525 PARROT_WARN_UNUSED_RESULT
526 Parrot_Language
527 Parrot_find_language(SHIM_INTERP, SHIM(char *language))
529 ASSERT_ARGS(Parrot_find_language)
530 return 0;
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.
540 =cut
544 PARROT_EXPORT
545 void
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.
561 =cut
565 PARROT_EXPORT
566 void
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.
582 =cut
586 PARROT_EXPORT
587 Parrot_PMC
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),
593 (char *) NULL, 0);
594 Parrot_PMC sub = Parrot_pmc_new(interp, enum_class_NCI);
595 VTABLE_set_pointer_keyed_str(interp, sub, sig, F2DPTR(func));
596 return sub;
601 =item C<Parrot_PMC Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)>
603 Create a class with the type given
605 =cut
609 PARROT_EXPORT
610 Parrot_PMC
611 Parrot_PMC_newclass(PARROT_INTERP, Parrot_PMC classtype)
613 ASSERT_ARGS(Parrot_PMC_newclass)
614 Parrot_PMC result;
615 PARROT_CALLIN_START(interp);
617 result = Parrot_pmc_new_init(interp, enum_class_Class, classtype);
619 PARROT_CALLIN_END(interp);
620 return result;
625 =back
627 =head1 SEE ALSO
629 See F<include/parrot/extend.h> and F<docs/pdds/pdd11_extending.pod>.
631 =head1 HISTORY
633 Initial version by Dan Sugalski.
635 =cut
641 * Local variables:
642 * c-file-style: "parrot"
643 * End:
644 * vim: expandtab shiftwidth=4: