Permalink
Browse files

Add a new Parrot_api_set_stdhandle routine to the embedding API to se…

…t a custom IO-alike PMC as one of the standard handles for the interp
  • Loading branch information...
1 parent 8013788 commit f7fec052e07769f2c1976b8f225c2cd1f4f6539b @Whiteknight Whiteknight committed May 22, 2012
Showing with 40 additions and 36 deletions.
  1. +11 −0 include/parrot/api.h
  2. +29 −36 src/embed/api.c
View
@@ -210,6 +210,15 @@ Parrot_Int Parrot_api_set_runcore(
__attribute__nonnull__(2);
PARROT_API
+Parrot_Int Parrot_api_set_stdhandle(
+ Parrot_PMC interp_pmc,
+ Parrot_PMC handle,
+ Parrot_Int fileno,
+ ARGOUT(Parrot_PMC *old_handle))
+ __attribute__nonnull__(4)
+ FUNC_MODIFIES(*old_handle);
+
+PARROT_API
Parrot_Int Parrot_api_set_warnings(Parrot_PMC interp_pmc, Parrot_Int flags);
PARROT_API
@@ -278,6 +287,8 @@ Parrot_Int Parrot_api_wrap_pointer(
PARROT_ASSERT_ARG(name))
#define ASSERT_ARGS_Parrot_api_set_runcore __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
PARROT_ASSERT_ARG(corename))
+#define ASSERT_ARGS_Parrot_api_set_stdhandle __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
+ PARROT_ASSERT_ARG(old_handle))
#define ASSERT_ARGS_Parrot_api_set_warnings __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_Parrot_api_toggle_gc __attribute__unused__ int _ASSERT_ARGS_CHECK = (0)
#define ASSERT_ARGS_Parrot_api_unwrap_pointer __attribute__unused__ int _ASSERT_ARGS_CHECK = (\
View
@@ -33,16 +33,18 @@ This file implements functions of the Parrot embedding interface.
Gets the results of the last API function call and stores the results in
C<is_error>, C<exception>, C<exit_code> and C<errmsg>. This function returns
a true value if this call is successful and false value otherwise. The stored
-information is as follow:
+information is as follows:
C<is_error> a true value if an unhandled exception was thrown or the program
terminated with an error condition and a false value otherwise.
-C<exception> the last exception thrown.
+C<exception> the last exception thrown, PMCNULL if none.
-C<exit_code> the exit code of the running program.
+C<exit_code> the exit code of the running program, if it expected to exit
+now. C<0> for no error.
-C<errmsg> contains an string with the last error message.
+C<errmsg> contains an string with the last error message if any,
+C<STRINGNULL> if none.
=cut
@@ -79,7 +81,7 @@ Parrot_api_get_result(Parrot_PMC interp_pmc, ARGOUT(Parrot_Int *is_error),
Parrot_PMC exception, Parrot_String *bt)>
Gets the backtrace of the interpreter's call chain for the given exception
-C<expcetion> and stores the results in string C<bt>. This function returns a
+C<exception> and stores the results in string C<bt>. This function returns a
true value if this call is successful and false value otherwise.
=cut
@@ -421,49 +423,40 @@ Parrot_api_add_dynext_search_path(Parrot_PMC interp_pmc,
/*
-=item C<Parrot_Int Parrot_api_set_stdhandles(Parrot_PMC interp_pmc, Parrot_Int
-in, Parrot_Int out, Parrot_Int err)>
+=item C<Parrot_Int Parrot_api_set_stdhandle(Parrot_PMC interp_pmc, Parrot_PMC
+handle, Parrot_Int fileno, Parrot_PMC *old_handle)>
-Set the C<interp_pmc>'s standard file descriptors STDIN, STDOUT, STDERR. Any
-file descriptor set to C<PIO_INVALID_HANDLE> is ignored. This function returns
-a true value if this call is successful and false value otherwise.
+Set one of the C<interp_pmc>'s standard IO PMCs. The handle PMC C<handle> is
+an IO-type PMC (such as FileHandle or StringHandle). The fileno is one of
+C<0> for stdin, C<1> for stdout and C<2> for stderr. Other values are not
+(currently) allowed.
=cut
*/
-/* Whiteknight told me that theres no way to test this for now, so it should be
-commented out, for now.
+
PARROT_API
Parrot_Int
-Parrot_api_set_stdhandles(Parrot_PMC interp_pmc, Parrot_Int in,
- Parrot_Int out, Parrot_Int err)
+Parrot_api_set_stdhandle(Parrot_PMC interp_pmc, Parrot_PMC handle,
+ Parrot_Int fileno, ARGOUT(Parrot_PMC *old_handle))
{
- ASSERT_ARGS(Parrot_api_set_stdhandles)
+ ASSERT_ARGS(Parrot_api_set_stdhandle)
EMBED_API_CALLIN(interp_pmc, interp)
- void *dummy;
-
- if (PIO_INVALID_HANDLE != (PIOHANDLE)in) {
- PMC * const pmc = Parrot_pmc_new(interp, enum_class_FileHandle);
- Parrot_io_set_os_handle(interp, pmc, (PIOHANDLE)in);
- dummy = (void *)Parrot_io_stdhandle(interp, PIO_STDIN_FILENO, pmc);
- }
-
- if (PIO_INVALID_HANDLE != (PIOHANDLE)out) {
- PMC * const pmc = Parrot_pmc_new(interp, enum_class_FileHandle);
- Parrot_io_set_os_handle(interp, pmc, (PIOHANDLE)out);
- dummy = (void *)Parrot_io_stdhandle(interp, PIO_STDOUT_FILENO, pmc);
+
+ switch (fileno) {
+ case PIO_STDIN_FILENO:
+ case PIO_STDOUT_FILENO:
+ case PIO_STDERR_FILENO:
+ *old_handle = Parrot_io_stdhandle(interp, fileno, handle);
+ break;
+ default:
+ Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
+ "Cannot set new handle %d. Must be one of 0, 1 or 2", fileno);
}
-
- if (PIO_INVALID_HANDLE != (PIOHANDLE)err) {
- PMC * const pmc = Parrot_pmc_new(interp, enum_class_FileHandle);
- Parrot_io_set_os_handle(interp, pmc, (PIOHANDLE)err);
- dummy = (void *)Parrot_io_stdhandle(interp, PIO_STDERR_FILENO, pmc);
- }
- UNUSED(dummy);
-
+
EMBED_API_CALLOUT(interp_pmc, interp)
-}*/
+}
/*

0 comments on commit f7fec05

Please sign in to comment.