racket / zuo

A tiny Racket for scripting
Other
263 stars 27 forks source link

Embedding example not working #5

Closed doppioandante closed 1 year ago

doppioandante commented 1 year ago

Hello, the embedding example, namely:

#include <stdio.h>
#include <string.h>
#include "zuo.h"

static zuo_ext_t *random_five(zuo_ext_t *args) {
  return zuo_ext_integer(5);
}

int main() {
  const char *prog = "#lang zuo/kernel (hash 'number (random-five))";
  zuo_ext_t *ht, *v;

  /* Step 1 */
  zuo_ext_primitive_init();
  zuo_ext_add_primitive(random_five, 1, "random-five");

  /* Step 2 */
  zuo_ext_image_init(NULL);

  /* Step 3 */
  zuo_ext_runtime_init(zuo_ext_false(), zuo_ext_empty_hash());

  /* Run `prog`: */
  ht = zuo_ext_eval_module(zuo_ext_symbol("five-app"), prog, strlen(prog));

  /* Inspect the result: */
  v = zuo_ext_hash_ref(ht, zuo_ext_symbol("number"), zuo_ext_false());
  if (zuo_ext_apply(zuo_ext_hash_ref(zuo_ext_kernel_env(),
                                     zuo_ext_symbol("integer?"),
                                     zuo_ext_false()),
                    zuo_ext_cons(v, zuo_ext_null()))
      == zuo_ext_true())
    printf("The answer was %d\n", (int)zuo_ext_integer_value(v));
  else
    printf("Something went wrong!\n");

  return 0;
}

Currently gives this error when run:

./example 
undefined: 'random-five
 in read-and-eval {2}
mflatt commented 1 year ago

Thanks for the report! I think you may be running into a bug recently fixed in response to #4. Are you working from a Git repo checkout or source from somewhere else (such as the Racket source distribution)?

doppioandante commented 1 year ago

Hello, sorry for the scarce info; I'm using master from this very repository. I updated to your latest fixes, and I figured out why we may be seeing different things:

✦ ❯ gcc -DZUO_EMBEDDED zuo.c example.c -o example

zuo on  main 
✦ ❯ ./example
The answer was 5

zuo on  main 
✦ ❯ gcc -DZUO_EMBEDDED image_zuo.c example.c -o example

zuo on  main took 3s 
✦ ❯ ./example
undefined: 'random-five
 in read-and-eval {2}

Is the generated image_zuo.c supposed to behave like this? Thanks for your time

mflatt commented 1 year ago

That difference is not expected. Has image_zuo.c been rebuilt (with make or even zuo) after pulling changes? Is the diff of zuo.c and zuo_image.c any more than changing EMBEDDED_IMAGE to 1 and defining emedded_boot_image_len and emedded_boot_image?

doppioandante commented 1 year ago

Yes, image_zuo.c has been regenerated and the diff also shows the hefty embedded_boot_image array being populated.

mflatt commented 1 year ago

Oh, I see it now. I still have been running the wrong example.

The problem is that the zuo_image.c version has a zuo/kernel that was created without the added primitive. I'll have to think about this issue more.

mflatt commented 1 year ago

I've addressed this problem mostly by adjusting the example and explanation. To better support new primitives, though, I also added support for an --image flag to local/image.zuo, which makes it easier to embed an image that's compatible with new primitives.