Code Monkey home page Code Monkey logo

Comments (5)

pepyakin avatar pepyakin commented on May 16, 2024 1

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.

pepyakin avatar pepyakin commented on May 16, 2024

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.

eira-fransham avatar eira-fransham commented on May 16, 2024

Maybe we want to have an export like memory.get_until_null(offset)

from wasmi.

stevemk14ebr avatar stevemk14ebr commented on May 16, 2024

See #256 for how to read strings from wasm

from wasmi.

Robbepop avatar Robbepop commented on May 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.