[CM] Weirdness with s7_load_c_string_with_environment, xxd fun

Christos Vagias chris.actondev at gmail.com
Thu Jul 8 17:34:27 PDT 2021


Hi Bil,

I'm getting some weird behavior with s7_load_c_string_with_environment.
I don't have a working example atm, it's late and just wanted to report this :)

Some description of what I'm doing:

calling s7_eval_c_string("(load \"main.scm\")");
and I have the load function replaced:

s7_define_function(sc, "load", load_with_resources, 1, 1, false,
                     "(load file (let (rootlet)))\n\
Tries to load \"file\" as an embedded resource first, and if it fails
it fallbacks to s7's load function");

And load_with_resources will either load a file from the memory (from
an included file produced with xxd -i) or try to actually load it from
the filesystem.

s7_pointer load_with_resources(s7_scheme *sc, s7_pointer args) {
  s7_pointer sc_file = s7_car(args);
  const char *file = s7_string(s7_car(args));
  s7_pointer env = s7_nil(sc);
  args = s7_cdr(args);
  if (args != s7_nil(sc))
    env = s7_car(args);

  Resource r = get_resource(sc, file);
  if (!r.data) {
    printf("Failed to load %s as embedded resource, trying normal
load\n", file);
    s7_pointer res = s7_load_with_environment(sc, file, env);
    // res might be null, should be careful to not return this
    // since this is an s7 binding
    if (!res) {
      return (s7_error(
          sc, s7_make_symbol(sc, "error"),
          s7_list(sc, 2, s7_make_string(sc, "load: can't open ~S"), sc_file)));
    }
    return res;
  }

  return s7_load_c_string_with_environment(sc, (char *)r.data, r.size,
                                           env);
}

And from the main.scm file, I'm loading other files:

(catch #t (lambda ()
                 (load "some-scheme-file.scm" some-env))
               (lambda (tag info) <print error> ))

And here is where I'm getting an error:
        unbound-variable
        unbound variable (symbol "\x94;\x10;")

When not replacing the "load" function (so that means loading the
files from the actual filesystem) all goes well. But, when loading the
files via the s7_load_c_string_with_environment
this error occurs.

If this is not helpful at all, I'll try to make some isolated demo
that demonstrates the problem.

Another note, s7_load_with_environment returns NULL if something goes
wrong, so returning
its result from an s7_function can lead to crashes. I know this is
documented but still seemed a bit weird for a behavior.

Best,
Christos


More information about the Cmdist mailing list