-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
memory import undefined #2200
Comments
I believe you'll need to import |
I'm running into a similar issue in a project, and importing For what it's worth, my code can be found here: https://github.com/noloerino/duna/tree/reg-display/duna_frontend (JS stuff is in |
I think that may be a bug in our configuration right now in that case. We're generating |
FWIW: I noticed I couldn't import the .wasm file in TS so I changed to a javascript file and memory is still undefined. "export 'memory' was not found in 'wasm-rust/index_bg' I also tried using wasm-pack over the webpack plugin version and still same issue. Something else must be off with my configuration.... |
Hello! We are running into this as well. We have found a workaround that works for us, change the import like @alexcrichton said:
But if we only do that typescript will complain that it does not find the module, we fix that by renaming the I am interested in contributing a fix to this issue if it would be merged. 🙂 |
I'd be totally down for merging a fix for this! I didn't realize that |
it's works with our particular setup, i am not 100% sure it is a fix for all environments. Another option would be to rename |
Hi there! You could also pass the memory through the js wrapper: export { memory } from './index_bg.wasm'; It comes down to your intentions - what should a package consumer use? the js wrapper or the wasm file itself? Or both? ...then you might want to have definitions for both files (index_bg.d.ts and index_bg.wasm.d.ts). I am also very interested in accessing the memory in a typescript setup! :) Edit: |
I had the same issue, but the inital example I followed used |
I'm running into a similar (possibly related?) issue, reported at rustwasm/wasm-pack#1030. |
I had the same issue. It's quite annoying when you're making your first steps with Rust and wasm. I followed everything in the tutorial and it just doesn't work. In my case I fixed it liked this (adding .wasm to wasm_game_of_life_bg):
|
Same for me here. The tutorial needs to be corrected. |
I'm using vite to start the project and the same problem arose😔 I am using the plugin vite-plugin-rsw. In the file |
I solved the problem by reading the source code of rwasm, I should use memory like this: import init from 'wasm-game-of-life'
const wasmInit = await init()
const memory = wasmInit.memory
// ... |
Hi, just surfacing, i'm still seeing this issue following the rust wasm tutorial: https://rustwasm.github.io/docs/book/game-of-life/implementing.html |
rustwasm/wasm_game_of_life#53 (plus rustwasm/book#223) is an alternate solution, which avoids needing the module's raw memory in the first place. @alexcrichton do you have access to those repos to take a look? |
I can add others to those repos but I unfortunately don't have time to maintain them myself. @Liamolucko would you like me to add you to those? |
Yes. |
Besides: I use |
I ran into the same issue, and none of the workarounds above didn't work for my case. JIC, someone like me needs another solution; I share mine here. Basically, I gave up using objects exported by the generated package and set it up myself by following the definition. import buildWasmModule from "wasm-game-of-life/wasm_game_of_life_bg.wasm";
import * as bg from "wasm-game-of-life/wasm_game_of_life_bg.js";
...
buildWasmModule({'./wasm_game_of_life_bg.js': bg}).then(wasmModule => {
bg.__wbg_set_wasm(wasmModule) // This was originally done before being exported in the package.
main(bg.Universe, bg.Cell, wasmModule.memory, canvas) // Finally I could use the objects along with the target([WebAssembly.Memory](https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/Memory)).
}) The original commit in my project is here. Then, I wanted to convert it into TypeScript. The usage code didn't need any changes but I needed to define the type of the wasm module myself to resolve type errors. type BGModule = {}
type WASMModule = { memory: WebAssembly.Memory }
declare module 'wasm-game-of-life/wasm_game_of_life_bg.wasm' {
export default function buildWasmModule(importObject: { './wasm_game_of_life_bg.js': BGModule }): Promise<WASMModule>
} The original commit in my project is here and here. I hope this will help someone. |
I needed to add
I tried again |
I was facing same issue with my own project, this is what worked for me ...
let wasm;
...
// your code
// the code uses memory by `wasm.memory`
...
wasm = await init(); // do this wherever you initialize Hope this helps! |
I've been going through the rust wasm book and I've gotten stuck on a problem.
Summary
I'm have an issue where
memory
is undefined when I import it. So when I try to access buffer, it ofc errors out. Are there any known situations that would cause this?
Additional Details
I'm using the Webpack Wasm-pack plugin.
The project is can be found here: https://github.com/jregistr/wasm-game-of-life
The text was updated successfully, but these errors were encountered: