Using node FFI to import types from .so

355 Views Asked by At

NodeFFI can be used to pull C files (specifically a .so) and read code from them. On that I had 2 questions. If I define a type in rust, will that show up as a type in javascript? Furthermore, is there anyway to use functions without setting parameter values (without the 'function': [ 'int', [ 'int', 'int' ] ]? If not how would I "declare" a type from the .so file?

1

There are 1 best solutions below

2
On BEST ANSWER

If I define a type in rust, will that show up as a type in javascript?

No. For one, the type system in JavaScript is very different from that in Rust. For another, a .so file cannot export any types at all. The memory layout of the types is expected to be known to the caller. In C, it would be declared in a header file (.h) that is provided along with the .so file.

Furthermore, is there anyway to use functions without setting parameter values (without the 'function': [ 'int', [ 'int', 'int' ] ]? If not how would I "declare" a type from the .so file?

As explained above: you don't.

But you can use the node-bindgen crate to generate Node.js wrapper code to invoke Rust code from Node.js relatively easily. This doesn't do what you asked, but it probably does what you need ;)