Comments (5)
yeah that could help, but at same time I actually wonder if we should put it in MemoryInstance
. I'm afraid that it could become a swiss-knife. At the same time, this functionality could be implemented externally by using with_direct_access
.
from wasmi.
Hello!
If you compile your code
$ echo 'use std::os::raw::c_char;
use std::ffi::CString;
#[no_mangle]
pub extern fn test3() -> *mut c_char {
CString::new("ohai!").unwrap()
.into_raw()
}
' | rustc --crate-type cdylib --target wasm32-unknown-unknown -
and look for the definition of test3
in the result file you will see:
$ wasm2wat rust_out.wasm | grep '(func $test3'
(func $test3 (type 9) (result i32)
(export "test3" (func $test3))
this means that your function indeed has a i32 return type. The number you see is an actual offset of the string in the linear memory of the wasm module. What you need to do is to get the linear memory from the module instance and then read bytes from it.
For example:
// Get the linear memory from the module instance.
// "memory" is the export name of the exported memory. It is usually called that, but YMMV.
let memory = instance.export_by_name("memory")
.expect("`memory` export not found")
.as_memory()
.expect("export name `memory` is not of memory type");
// Read memory contents
let size = 5;
let contents = memory.get(offset, size).expect("OOB error");
as you've might spotted already there is a problem: we need to provide the size for reading the contents and *mut c_char
doesn't specify it. Moreover, wasm doesn't support returning of multiple-results from functions. There are several workarounds, such as: read the string byte-by-byte until you reach the null-terminator, pass the size through memory or invert your API design (say, instead of returning the string, you call some function that receives the string).
Yes, some high-level bindings generator akin to wasm-bindgen would be useful although as I've said it's a priority right now.
from wasmi.
Maybe we want to have an export like memory.get_until_null(offset)
from wasmi.
See #256 for how to read strings from wasm
from wasmi.
I will close this issue as it is quite outdated, @stevemk14ebr gave a link to an answer and we will probably not introduce a specialized API to ready out strings to our linear memory abstractions.
from wasmi.
Related Issues (20)
- Big performance regression between 0.31 and 0.32 HOT 7
- Optimization: Special handling for common shadow stack instruction sequences HOT 1
- Create C bindings for Wasmi
- Simplification: Rewrite `iNN.sub` with immediate to new `iNN.add` variant
- Restore CI benchmarks HOT 1
- Wasmi versions `0.32.0-beta.6` cannot run `ffmpeg.wasm` correctly HOT 20
- Update `wasmi_wasi` WASI implementing dependencies HOT 1
- Optimization: Fuse transitive copies HOT 1
- Add `--verbose` mode to Wasmi CLI HOT 2
- Add support for fully hibernate-able Wasm instances HOT 7
- Handle host function calls the same as other instructions in the Wasmi executor HOT 1
- Make it possible to debug and pretty print Wasmi bytecode
- Add Wasmi execution profiling HOT 1
- The URL in the project description is 404 HOT 1
- Optimize `Linker` setup
- Add customizable `wasmi::Engine` limits
- Reconsider proper fuel charging for lazily compiled functions
- Reduce `wasmi::Engine` memory consumption for storing function artifacts HOT 3
- Try to put the Wasmi engine internals into its own crate
- Wasmi v0.32 stable release preparations
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wasmi.