nyue opened issue #8762:
I am studying this C and WASI approach
https://docs.wasmtime.dev/examples-c-wasi.html
Let's say I have something like this
fn myfunction1(first_name: &str) { println!("My function 1, {}!", first_name); } fn myfunction2(last_name: &str) { println!("My function 2, {}!", last_name); }
Can I call myfunction1 and myfunction2 via wasmtime C-API ?
How would the C code call look like ? Is there some example I can reference for further studying ?
Cheers
ssnover commented on issue #8762:
Check out the next page: https://docs.wasmtime.dev/examples-c-linking.html
There they get a function which was defined and exported from the wasm module called
run
, then call it.// Lookup our `run` export function wasmtime_extern_t run; bool ok = wasmtime_instance_export_get(context, &linking1, "run", 3, &run); assert(ok); assert(run.kind == WASMTIME_EXTERN_FUNC); error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap); if (error != NULL || trap != NULL) exit_with_error("failed to call run", error, trap);
ssnover edited a comment on issue #8762:
Check out the next page: https://docs.wasmtime.dev/examples-c-linking.html
There they get a function which was defined and exported from the wasm module called
run
, then call it. (the following pages show some examples of passing arguments to the exported function)// Lookup our `run` export function wasmtime_extern_t run; bool ok = wasmtime_instance_export_get(context, &linking1, "run", 3, &run); assert(ok); assert(run.kind == WASMTIME_EXTERN_FUNC); error = wasmtime_func_call(context, &run.of.func, NULL, 0, NULL, 0, &trap); if (error != NULL || trap != NULL) exit_with_error("failed to call run", error, trap);
alexcrichton commented on issue #8762:
To better understand how to work with a module like this in C you'll need to probably understand how Rust translates to wasm. For example the functions you listed above there's a number of issues:
- Neither function will actually be exported from the WebAssembly module produced. They both lack
pub extern "C" fn ...
in addition to#[no_mangle]
. Hosts can only access exports of a wasm module, so you'll need to understand Rust's model for compiling to wasm (or play around with some examples).- The argument to these functions is
&str
which does not have a stable ABI in Rust. That will currently map to twoi32
function parameters in wasm. You'll need to understand what these are and how to pass them from the host. Right now I believe that this is a pointer parameter into linear memory and a string byte length. This may change in the future due to Rust's ABI guarantees so for example it would be recommended to take*const u8
andusize
parameters instead.- One argument is a pointer but you'll need to pass a valid pointer. Acquiring a valid pointer is not easy to do and would probably require the module to export a
malloc
-style function first. You'll need to call that, copy in the data, then pass the pointer to these functions.Overall the tl;dr; is that calling functions like the ones you're outlining is going to require a good deal of work. Much of that is sort of just the reality of wasm, it's a "virtual CPU" and there's lots of stuff you need to do to interact with a CPU. This is one of the reasons we've been working on the component model is that it ends up handling much of this for you. That being said the component model does not have a C API at this time, so I bring this up mostly to put something on your radar, not to propose a solution for you at this time.
alexcrichton closed issue #8762:
I am studying this C and WASI approach
https://docs.wasmtime.dev/examples-c-wasi.html
Let's say I have something like this
fn myfunction1(first_name: &str) { println!("My function 1, {}!", first_name); } fn myfunction2(last_name: &str) { println!("My function 2, {}!", last_name); }
Can I call myfunction1 and myfunction2 via wasmtime C-API ?
How would the C code call look like ? Is there some example I can reference for further studying ?
Cheers
alexcrichton commented on issue #8762:
I think this has been answered so I'm going to close this, but if there's still more questions feel free to comment and/or open another issue.
Last updated: Dec 23 2024 at 13:07 UTC