[plt-scheme] loading extension in a module

From: Ian Zimmerman (itz at buug.org)
Date: Mon Nov 29 23:42:15 EST 2004

Daniel> To write your C extension as a module, you create a module
Daniel> namespace with scheme_primitive_module, then add your primitives
Daniel> to it, and finally close the namespace with
Daniel> scheme_finish_primitive_module:

But this is not what I want to do.  It could be a possible workaround,
but before I go into workarounds I want to know if my original intent is
doable and how.

Clearly, as the transcript in my post shows, when load-extension is
called at the toplevel, it directly imports the new primitives into the
toplevel namespace.  I just want to do the same thing _in a regular
Scheme module_, i.e. make the C primitives be part of the module
namespace.  Maybe you didn't follow the link to the files, so I enclose
relevant parts of them below (I can't post them verbatim because they
have long lines).

BTW, I believe you meant to name the parameter to your example
scheme_initialize enclosing_ns.

Daniel> Scheme_Object* scheme_initialize(Scheme_Env* ns)
Daniel> {
Daniel> Scheme_Env* ns = scheme_primitive_module(scheme_module_name(),
Daniel> enclosing_ns);
Daniel> scheme_add_global("c-lstat", my_c_lstat_primitive, ns);
Daniel> ...
Daniel> scheme_finish_primitive_module(ns);
Daniel> return scheme_void;
Daniel> }

Here are the excerpts:


(module stat mzscheme

  ;; ...

  (load-relative-extension "plt_stat.so")

  ;; ... definition of repack-c-stat somewhere here

  (define (lstat filename) (repack-c-stat c-lstat "lstat" filename))
  (define (stat  filename) (repack-c-stat c-stat  "stat"  filename))

     stat lstat

  ;; ...


struct plt_prim_info
  Scheme_Prim* prim;
  const char* name;
  int mina;
  int maxa;
typedef struct plt_prim_info plt_prim_info_t;

static const plt_prim_info_t plt_stat_prims [] =
    { plt_stat  , "c-stat", 1, 1 },
    { plt_lstat , "c-lstat", 1, 1 },
    { NULL, NULL, 0, 0 }

/* ... */

scheme_initialize (Scheme_Env* namespace)
  for (const plt_prim_info_t* pprim = plt_stat_prims; pprim->name != NULL;
      Scheme_Object* pob =
        scheme_make_prim_w_arity (pprim->prim, pprim->name, pprim->mina,
      scheme_add_global (pprim->name, pob, namespace);
        /* ... */
  return scheme_void;

scheme_reload (Scheme_Env* namespace)
  (void) namespace;             /* unused */
  return scheme_void;

scheme_module_name (void)
  return scheme_false;

"It's not true or not."  A reality show producer (real quote)

Posted on the users mailing list.