wit-bindgen icon indicating copy to clipboard operation
wit-bindgen copied to clipboard

How do I return list from C code?

Open hellozyemlya opened this issue 6 months ago • 1 comments

I have wit file like this:

package my:demo;

world demo {
  record preset {
    id: u16,
    len: u16,
    offset: u16
  }
  export get-presets: func() -> list<preset>;
}

How do I return that list in function void exports_demo_get_presets(demo_list_preset_t *ret); implementation? I do something like this:

void exports_demo_get_presets(demo_list_preset_t *ret) {
  ret.ptr = malloc(sizeof(struct demo_preset_t) * 2);
  ret.len = 2;
}

but who is responsible for free the ret.ptr? Will that happen automatically?

hellozyemlya avatar Jun 29 '25 09:06 hellozyemlya

Some excellent documentation was just written up at https://github.com/bytecodealliance/wit-bindgen/blob/main/crates/c/README.md quite recently, mind reading that over and seeing if it answers your question?

alexcrichton avatar Jun 30 '25 14:06 alexcrichton