FFI function call fails by passing pointer contains struct

Create issue
Issue #60 resolved
Takashi Kato repo owner created an issue

This fails with panic.

(import (rnrs) (sagittarius ffi))

(define-c-struct bar
  (int bi)
  (char* bc*))
(define-c-struct foo
  (struct bar fb)
  (long fl))

(define so (open-shared-library "foo.so"))

(define dump-foo (c-function so void dump_foo (void*)))

(let ((fp (allocate-c-struct foo))
      (bp (allocate-c-struct bar)))
  (bar-bi-set! bp 1234)
  (bar-bc*-set! bp "hello world")
  (foo-fb-set! fp bp)
  (print (foo-fb-ref fp))
  (foo-fl-set! 98765)
  (dump-foo fp))

Where the foo.so is defined like this

#include <stdio.h>

#ifdef _MSC_VER
# define EXPORT __declspec(dllexport)
# define EXPORT

struct foo
  struct {
    int bi;
    char *bc;
  } bar;
  long fl;

EXPORT void dump_foo(struct foo *st_foo)
  fprintf(stderr, "foo.bar.bi  [%d]\n", st_foo->bar.bi);
  fprintf(stderr, "foo.bar.bc* [%s]\n", st_foo->bar.bc);
  fprintf(stderr, "foo.fl      [%ld]\n", st_foo->fl);

Comments (2)

  1. Takashi Kato reporter

    The issue is c-struct-set!. It's passing the value to pointer-set-c-pointer! but this doesn't support struct member properly (and it's impossible to resolve). So c-struct-set! should handle internal struct differently.

  2. Log in to comment