unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* proposal: scm_c_public_ref et al
@ 2011-03-06 11:24 Andy Wingo
  2011-03-06 16:24 ` Mark H Weaver
  2011-03-06 22:22 ` Ludovic Courtès
  0 siblings, 2 replies; 7+ messages in thread
From: Andy Wingo @ 2011-03-06 11:24 UTC (permalink / raw)
  To: guile-devel

Hey all,

As we move more and more to writing code in Scheme and not in C, it
becomes apparent that it is more cumbersome to reference Scheme
values than it should be.

I propose that we add helper C APIs like these:

    SCM scm_public_lookup (SCM module_name, SCM sym);
    SCM scm_private_lookup (SCM module_name, SCM sym);

    Look up a variable bound to SYM in the module named MODULE_NAME.  If
    the module does not exist or the symbol is unbound, signal an
    error.  The "public" variant looks in the public interface of the
    module, while scm_private_lookup looks into the module itself.

Then also:

    SCM scm_public_ref (SCM module_name, SCM sym);

    Equivalent to scm_variable_ref (scm_public_ref (module_name, sym)).

    SCM scm_private_ref (SCM module_name, SCM sym);

    Equivalent to scm_variable_ref (scm_private_ref (module_name, sym)).

And then:

    SCM scm_c_public_lookup (const char *module_name, const char *name);
    SCM scm_c_private_lookup (const char *module_name, const char *name);
    SCM scm_c_public_ref (const char *module_name, const char *name);
    SCM scm_c_private_ref (const char *module_name, const char *name);

    Like the above, but with locale-encoded C strings, for convenience.
    Module names are encoded as for `scm_c_resolve_module'.

With all these, we can happily implement Bruce Korb's
`eval-string-from-file-line' using the new `(ice-9 eval-string)', and
our code becomes:

    SCM scm_c_eval_string_from_file_line (const char *str, const char *file, int line)
    {
      return scm_call_5 (scm_c_public_ref ("ice-9 eval-string", "eval-string"),
                         scm_from_locale_string (the_string),
                         scm_from_locale_keyword ("file"),
                         scm_from_locale_string (file_name),
                         scm_from_locale_keyword ("line"),
                         scm_from_int (line));
     }

scm_call_N doesn't go up to 5 yet, but it probably should.  We can cache
the var if we want:

    SCM scm_c_eval_string_from_file_line (const char *str, const char *file, int line)
    {
      static SCM eval_string_var = SCM_BOOL_F;

      if (scm_is_false (eval_string_var))
        eval_string_var = scm_c_public_lookup ("ice-9 eval-string", "eval-string");

      return scm_call_5 (scm_variable_ref (eval_string_var),
                         scm_from_locale_string (the_string),
                         scm_from_locale_keyword ("file"),
                         scm_from_locale_string (file_name),
                         scm_from_locale_keyword ("line"),
                         scm_from_int (line));
     }

And we can use this strategy for easily moving over more C code to
Scheme modules as time goes on.

(If we make this convenient enough, we might be able to avoid defining
scm_c_eval_string_from_file_line ourselves, and just say to call the
function from ice-9 eval-string; but that's another discussion.)

Any thoughts?

Andy
-- 
http://wingolog.org/



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-03-07 10:45 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-03-06 11:24 proposal: scm_c_public_ref et al Andy Wingo
2011-03-06 16:24 ` Mark H Weaver
2011-03-06 17:10   ` Thien-Thi Nguyen
2011-03-06 21:34     ` Andy Wingo
2011-03-06 21:10   ` Andy Wingo
2011-03-06 22:22 ` Ludovic Courtès
2011-03-07 10:45   ` Andy Wingo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).