rustwebassemblyrust-cargonearnear-sdk-rs

How to link a WASM binary inside near-sdk Rust code that targets WASM


I have a WASM function that I want to use in my near-sdk Rust project that targets WASM. How can I call this function inside my Rust code?


Solution

  • You haven't really told enough about your situation to give a detailed answer. (Where does that WASM function come from? What environment are you using, if not JS?) But I can tell you the general three approaches you have:

    1. Use a WASM interpreter that can run anywhere including on WASM, like wasm3, to execute the "function" from your "Rust project". This will be slow.
    2. Give your "Rust project" an imported function that instructs whatever engine is running that to run some other WASM module held in the "Rust project"s memory. This can be done, JS or not, but you'd need to be in control of the execution engine
    3. Link the two wasm modules into one: "function" WASM file could essentially be a static library, and you can link it like one. However, this requires that the "function" WASM file was produced with this in mind, it must have the necessary linking custom section. (You can't just smack two normal WASM files together, they wouldn't know how to coordinate static memory use, e.g.)