lyuxiaosu opened issue #6633:
Hi, I use wasmtime C API to load a .wat module (which uses WASI) and execute it. It can successfully execute the module and print out "Hello World!" on the stdout. However, I want to retrieve the output as a host variable, is it possible?
I defined the following code trying to get the result:wasmtime_val_t results[1]; error = wasmtime_func_call(context, &run.of.func, NULL, 0, results, 1, &trap); if (error != NULL || trap != NULL) exit_with_error("failed to call run", error, trap); printf("output result is %s\n", (char*)wasmtime_externref_data(results[0].of.externref));
This code doesn't work and it print out:
error: failed to call run expected 0 results, got 1
I made something wrong here or is it possible to get the output? I think it should malloc memory for
results
on the host runtime and thenwasmtime_func_call
will write the output to it, however, now the error seems not the memory allocation issue. Any suggestions for this. Thanks very much.
lyuxiaosu edited issue #6633:
Hi, I use wasmtime C API to load a .wat module (which uses WASI) and execute it. It can successfully execute the module and print out "Hello World!" on the stdout. However, I want to retrieve the output as a host runtime variable, is it possible?
I defined the following code trying to get the result:wasmtime_val_t results[1]; error = wasmtime_func_call(context, &run.of.func, NULL, 0, results, 1, &trap); if (error != NULL || trap != NULL) exit_with_error("failed to call run", error, trap); printf("output result is %s\n", (char*)wasmtime_externref_data(results[0].of.externref));
This code doesn't work and it print out:
error: failed to call run expected 0 results, got 1
I made something wrong here or is it possible to get the output? I think it should malloc memory for
results
on the host runtime and thenwasmtime_func_call
will write the output to it, however, now the error seems not the memory allocation issue. Any suggestions for this. Thanks very much.
lyuxiaosu commented on issue #6633:
The .wat module I used doesn't return a char* string result, it just print out "Hello World!" and I want to retrieve this "Hello World!".
jameysharp commented on issue #6633:
It sounds like you want to capture stdout.
The Wasmtime C API currently offers two options for what to do with the guest's stdout: you can direct it to a file using
wasi_config_set_stdout_file
, or have it forwarded to the host's stdout withwasi_config_inherit_stdout
.In your case the best I think you can do today is have the output written to a file and then read the contents of that file after the guest exits.
I believe there are more options if you use Wasmtime's Rust API instead, so the C API could provide other options for what to do with stdout, but nobody has implemented those.
Or, if you can change the code that's running in the guest, then you could pick another way of passing the string out instead of using stdout.
lyuxiaosu commented on issue #6633:
Thanks @jameysharp . Yes, I want to capture the guest stdout. Thanks for letting me know these two interfaces and I tried both of them, and they all works. For now, I will use
wasi_config_set_stdout_file
for my need, though it could let developers to change their code,wasi_config_set_stdout_file
is still necessary for those who don't want to change their code. Thanks for your help!
jameysharp commented on issue #6633:
Great, I'm glad that worked! I'll go ahead and close this issue but please do let us know if you have more questions. You can also discuss how to use Wasmtime in our chat at https://bytecodealliance.zulipchat.com/#narrow/stream/217126-wasmtime if that's more convenient.
jameysharp closed issue #6633:
Hi, I use wasmtime C API to load a .wat module (which uses WASI) and execute it. It can successfully execute the module and print out "Hello World!" on the stdout. However, I want to retrieve the output as a host runtime variable, is it possible?
I defined the following code trying to get the result:wasmtime_val_t results[1]; error = wasmtime_func_call(context, &run.of.func, NULL, 0, results, 1, &trap); if (error != NULL || trap != NULL) exit_with_error("failed to call run", error, trap); printf("output result is %s\n", (char*)wasmtime_externref_data(results[0].of.externref));
This code doesn't work and it print out:
error: failed to call run expected 0 results, got 1
I made something wrong here or is it possible to get the output? I think it should malloc memory for
results
on the host runtime and thenwasmtime_func_call
will write the output to it, however, now the error seems not the memory allocation issue. Any suggestions for this. Thanks very much.
Last updated: Dec 23 2024 at 12:05 UTC