Dear Guile Maintainer,

I am using Guile 1.6.4 on a Debian (unstable) based i686--actually I do
not think the exact architecture is especially important since what I am
reporting is rather a problem with the documentation.  I would like to
define symbols for use in Guile and C code in the C code where I also
initialise (with `scm_c_define_gsubr') the C functions that have an
interface in Guile.  I have tried defining a global (in C) SCM variable
and setting the symbol's name with `scm_str2symbol' but the symbol thus
defined disappears unless I protect it (from the garbage collector?)
with `scm_gc_protect_object'.  Is that right?  Anyway, I have not been
able to find (in the Guile Reference Manual) an explanation of how to
define Guile symbols from the C startup code, and I think that should be
possible (to do it, and to find an explanation).

I have enclosed a complete example; it can be compiled using `gcc main.c
-lguile', and on the Guile command prompt, you can see the difference
between the symbols `exists' and `doesnt-exist' by typing

(equals-exists? 'exists)

and

(equals-doesnt-exist? 'doesnt-exist)

The functions `equals-exists?' and `equals-doesnt-exist?' are mirror
images of one another, as are the symbols `exists' and `doesnt-exist';
the only difference is that `exists' is protected using
`scm_gc_protect_object' while `doesnt-exist' is not...

Please let me know if you need more information.

Thanks,
Pascal Cedraschi
/* showcase for the use of scm_gc_protect_object */

#include <libguile.h>


/* Global variables to hold symbols; as their names indicate, the
 * first one is to survive garbage collection, the second isn't.
 */
SCM exists;
SCM doesnt_exist;


/* initialise the above topology symols */
/* return #t if sym equals exists, #f otherwise */
static SCM equals_exists_q (SCM sym)
{
	SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG1, "equals-exists?");
	return sym==exists?SCM_BOOL_T:SCM_BOOL_F;
};


/* return #t if sym equals doesnt_exist, #f otherwise */
static SCM equals_doesnt_exist_q (SCM sym)
{
	SCM_ASSERT(SCM_SYMBOLP(sym), sym, SCM_ARG1, "equals-doesnt-exist?");
	return sym==doesnt_exist?SCM_BOOL_T:SCM_BOOL_F;
};


/* Here's the beef.  The symbol exists is protected from the garbage
 * collector whereas the symbol doesnt_exist is not.  If we access the
 * global variables exist and doesnt_exist from C code (invoked in
 * turn from Scheme code), they behave differently since doesnt_exist
 * has already been garbage collected away by the time the scheme
 * interpreter is invoked.
 */
static void init (void)
{
	scm_gc_protect_object(exists=scm_str2symbol("exists"));
	doesnt_exist=scm_str2symbol("doesnt_exist");
	scm_c_define_gsubr("equals-exists?", 1, 0, 0, equals_exists_q);
	scm_c_define_gsubr("equals-doesnt-exist?", 1, 0, 0,
			   equals_doesnt_exist_q);
};


/* The actual main function.  main (below) does not much more than
 * invoke this function.
 */
static void inner_main (void *closure, int argc, char **argv)
{
	init();
	scm_shell(argc, argv);
};


/* Just the standard main for a Guile extended program.  The actual
 * work is done by inner_main above.
 */
int main (int argc, char *argv[])
{
	scm_boot_guile (argc, argv, inner_main, NULL);
	exit(0);  /* never get here */
};
_______________________________________________
Bug-guile mailing list
[EMAIL PROTECTED]
http://mail.gnu.org/mailman/listinfo/bug-guile

Reply via email to