Stream: git-wasmtime

Topic: wasmtime / issue #8762 [question] Can we call specific fu...


view this post on Zulip Wasmtime GitHub notifications bot (Jun 09 2024 at 01:05):

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

view this post on Zulip Wasmtime GitHub notifications bot (Jun 09 2024 at 21:48):

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);

view this post on Zulip Wasmtime GitHub notifications bot (Jun 09 2024 at 21:50):

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);

view this post on Zulip Wasmtime GitHub notifications bot (Jun 10 2024 at 15:00):

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:

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.

view this post on Zulip Wasmtime GitHub notifications bot (Jun 17 2024 at 20:12):

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

view this post on Zulip Wasmtime GitHub notifications bot (Jun 17 2024 at 20:12):

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: Nov 22 2024 at 16:03 UTC