-
-
Notifications
You must be signed in to change notification settings - Fork 669
Error when calling js from wasm,error:WebAssembly Instantiation: Import #0 module="module" error: module is not an object or function #384
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
Comments
According to your code, the JS side expects an import named |
Can you give me a simple example? |
@evanyuanvip you need to include Example:
source: https://gist.github.com/kripken/59c67556dc03bb6d57052fedef1e61ab#file-hello_world-html-L24 Demo: https://github.com/goldenratio/wasm-helloworld |
@goldenratio Thank you ! |
Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary! |
@dcodeIO So if you get the chance, I think this still has some weird behavior. I know have to do this in wasmboy: https://github.com/torch2424/wasmboy/blob/80441371e2da5ecec3d7e0b6fb4d53e08aa008ca/core/portable/importObject.js As it would complain with |
This works for me declare namespace console {
@external("console", "log")
export function log(): void;
} but it seems that |
Ahh, I didn't try declaring both the namespace, and exporting the log. I'll try that once I get the chance 👍 |
I'm trying the example from #158, and I too get the error. I'm not sure how to fix it. I tried adding So in JS I have await WebAssembly.instantiateStreaming(response, {
module: {},
console: {
logi() {
return 4
},
},
}) then in AS I have declare namespace console {
function logi(): i32
}
// this is exported so it can be called on the JS side.
export function mul(a: i32, b: i32): i32 {
return a * b + console.logi()
} But it at runtime it says
How do we make it work? |
Okay, my file is called But now I get
What did I do wrong? |
The only way I can get it to work is by putting stuff in @external("env", "getFour")
declare function getFour(): i32; but I'd like to avoid the non-standard syntax (decorators only allowed on classes). |
The reason I'm trying to use the But I found a workaround: I put the imports with the
And here's the AS files: // index.ts
import {mul} from './mul'
export {mul} // this file import things passed in from JS, and exports them for use by other
// AS modules.
@external("env", "getFour")
declare function getFour(): i32
export {getFour} import {getFour} from './imports'
// this is exported so it can be called on the JS side.
export function mul(a: i32, b: i32): i32 {
return a * b + getFour()
} Then in JS supply const {mul} = (await WebAssembly.instantiateStreaming(response, {
env: {
// this is called by `assert()`ions in the AssemblyScript.
// Useful for debugging.
abort(...args) {
console.log(...args)
},
getFour() {
return 4
},
},
})).instance.exports
mul(3,5) // it works So, the Now we can at least have a better time writing portable code, because if we want to run this outside of AssemblyScript, then we can supply an alternative source for the |
Internally, we usually have a file |
@dcodeIO Thanks, that's good to know. How do we get the |
I think in declare namespace console {
function logi(): i32 // it's missing an export here
} |
Just in case it helps someone, this is my current importObject, that is passed during Wasm Instantiation. |
This works: // assemblyScript file
declare namespace console {
function log(str: string): void;
}
export function add(a: i32, b: i32): i32 {
console.log("AnubhavGupta");
return a + b;
} // JS file
await loader.instantiate(buffer,{
index: {
"console.log": (val) => console.log("log:", val)
}
}); |
Ah, so if anyone else comes across this, this is because imports are namespaced per their filename. So importing For example see the docs: https://www.assemblyscript.org/exports-and-imports.html#imports And if you don't want this behavior, and want control on how things are declared in the import object, see the custom naming in the docs: https://www.assemblyscript.org/exports-and-imports.html#imports For more general information on WebAssembly Imports in AssemblyScript, see: https://wasmbyexample.dev/examples/importing-javascript-functions-into-webassembly/importing-javascript-functions-into-webassembly.assemblyscript.en-us.html?programmingLanguage=assemblyscript |
What if two AS files have the same name, but they are at different paths in the file system? |
According to this #158, I tried it, but I was given an error. My code is:
The text was updated successfully, but these errors were encountered: