rust-diplomat / diplomat

Experimental Rust tool for generating FFI definitions allowing many other languages to call Rust code
https://rust-diplomat.github.io/book/
Other
528 stars 51 forks source link

consider emitting the rust api information as json as well #472

Open devoncarew opened 6 months ago

devoncarew commented 6 months ago

In addition to generating bindings for n various languages, it would be useful to emit the rust api information as json. This would allow for some additional use cases w/o having to modify the diplomat generator tool.

For context, I'd like to call into the wasm module compiled from https://github.com/unicode-org/icu4x, but want to recover some of the api information (things like function parameter names and api documention). The wasm module itself contains function names, param types, and return value types.

robertbastian commented 6 months ago

Why JSON? Is there a commonly used schema to describe C APIs? Our C headers are of meant to be this representation, they are the standard file format to describe C APIs and are machine-readable. They are missing documentation at the moment though.

devoncarew commented 6 months ago

Well, two reasons:

Is there a commonly used schema to describe C APIs?

I wouldn't be to worried about trying to match an existing schema; just providing info like a list of functions, function arg info, and api doc text, would be useful.

sffc commented 6 months ago

I think we want to get to a point where third-party crates can consume diplomat_core::hir. I wouldn't be opposed to dumping that structure out into Serde.

Manishearth commented 6 months ago

I wanted Diplomat's hir to implement ser/de for this reason but never got around to it.

However this would be the regular hir, not "cleaned up" with things like renames applied. Bear in mind different backends see different things when it comes to APIs because of diplomat attributes.

devoncarew commented 6 months ago

However this would be the regular hir, not "cleaned up" with things like renames applied. Bear in mind different backends see different things when it comes to APIs because of diplomat attributes.

Yup, that makes sense. Given the info in any format, I think I could recover it enough for my purposes.

I'll check to make sure that debug builds of the rust -> wasm module don't already contain the names of function params (ala https://webassembly.github.io/spec/core/appendix/custom.html).

devoncarew commented 6 months ago

OK, even when removing the optimization settings and release mode from https://github.com/unicode-org/icu4x/blob/main/ffi/npm/Makefile, I was not able to get the rust toolchain to populate the 'Local Names' section in the wasm module (having all the info self-contained in the wasm module would have been nice).


update: it looks like the debug info is being written in DWARF format into custom WASM sections - .debug_info and related.