You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Error when calling js from wasm,error:WebAssembly Instantiation: Import #0 module="module" error: module is not an object or function #384
varimportObject={env: {// import as @external("env", "logf")logf(value){console.log("logf: "+value);},abort(msg,file,line,column){console.error("abort called at main.ts:"+line+":"+column);}},console: {// import as console.logilogi(value){console.log("logi: "+value);}}}fetch('dist/module.optimized.wasm').then(res=>res.arrayBuffer()).then(bytes=>newWebAssembly.Instance(newWebAssembly.Module(bytes),importObject)).then(mod=>{console.log(mod.exports.add(503,100));});
According to your code, the JS side expects an import named module with a property console.logi. This changed recently to use the enclosing file name without extension as the default module name (here: module.ts -> module), so it might be that the documentation isn't up to date.
According to your code, the JS side expects an import named module with a property console.logi. This changed recently to use the enclosing file name without extension as the default module name (here: module.ts -> module), so it might be that the documentation isn't up to date.
As it would complain with Import #0 module="index" .... And I can't get namespaces working, even if I add them under index. I don't mind this, but I just wanted to let you know 😄
I'm trying the example from #158, and I too get the error. I'm not sure how to fix it. I tried adding module: {} as mentioned above in #384 (comment), but the error remains the same.
declarenamespaceconsole{functionlogi(): i32}// this is exported so it can be called on the JS side.exportfunctionmul(a: i32,b: i32): i32{returna*b+console.logi()}
But it at runtime it says
Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #0 module="index" error: module is not an object or function
Okay, my file is called index.ts, so I changed module: {} to index: {}, and got passed that error.
But now I get
Uncaught (in promise) LinkError: WebAssembly.instantiate(): Import #0 module="index" function="console.logi" error: function import requires a callable
The reason I'm trying to use the namespace technique is that I wanted to write portable code, and decorators are only allowed on functions.
But I found a workaround: I put the imports with the @external decorators in a separate file. I have the following structure:
project
|-- index.ts
|-- imports.ts
|-- mul.ts
And here's the AS files:
// index.tsimport{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")declarefunctiongetFour(): i32export{getFour}
import{getFour}from'./imports'// this is exported so it can be called on the JS side.exportfunctionmul(a: i32,b: i32): i32{returna*b+getFour()}
Then in JS supply getFour:
const{mul}=(awaitWebAssembly.instantiateStreaming(response,{env: {// this is called by `assert()`ions in the AssemblyScript.// Useful for debugging.abort(...args){console.log(...args)},getFour(){return4},},})).instance.exportsmul(3,5)// it works
So, the mul code can import the getFour function, regardless of where it actually comes from.
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 ./imports module (f.e. using Webpack, or a CommonJS module that we place alongside the tsc output, etc). So at least the mul file is portable.
Ah, so if anyone else comes across this, this is because imports are namespaced per their filename. So importing console in index.ts, means console needs to be inside an index object in the imports.
Activity
dcodeIO commentedon Dec 20, 2018
According to your code, the JS side expects an import named
modulewith a propertyconsole.logi. This changed recently to use the enclosing file name without extension as the default module name (here: module.ts -> module), so it might be that the documentation isn't up to date.evanyuanvip commentedon Dec 21, 2018
Can you give me a simple example?
goldenratio commentedon Jan 4, 2019
@evanyuanvip you need to include
modulein yourimportObjectalso env.memory and env.tableExample:
source: https://gist.github.com/kripken/59c67556dc03bb6d57052fedef1e61ab#file-hello_world-html-L24
Demo: https://github.com/goldenratio/wasm-helloworld
evanyuanvip commentedon Jan 7, 2019
@goldenratio Thank you !
dcodeIO commentedon Feb 7, 2019
Closing this issue for now as it hasn't received any replies recently. Feel free to reopen it if necessary!
torch2424 commentedon Feb 28, 2019
@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
Import #0 module="index" .... And I can't get namespaces working, even if I add them under index. I don't mind this, but I just wanted to let you know 😄dcodeIO commentedon Feb 28, 2019
This works for me
but it seems that
@externalon namespaces does nothing currently. That the issue here?torch2424 commentedon Feb 28, 2019
Ahh, I didn't try declaring both the namespace, and exporting the log. I'll try that once I get the chance 👍
trusktr commentedon May 27, 2019
I'm trying the example from #158, and I too get the error. I'm not sure how to fix it. I tried adding
module: {}as mentioned above in #384 (comment), but the error remains the same.So in JS I have
then in AS I have
But it at runtime it says
How do we make it work?
trusktr commentedon May 27, 2019
Okay, my file is called
index.ts, so I changedmodule: {}toindex: {}, and got passed that error.But now I get
What did I do wrong?
trusktr commentedon May 27, 2019
The only way I can get it to work is by putting stuff in
env, then using the formbut I'd like to avoid the non-standard syntax (decorators only allowed on classes).
trusktr commentedon May 27, 2019
The reason I'm trying to use the
namespacetechnique is that I wanted to write portable code, and decorators are only allowed on functions.But I found a workaround: I put the imports with the
@externaldecorators in a separate file. I have the following structure:And here's the AS files:
Then in JS supply
getFour:So, the
mulcode can import thegetFourfunction, regardless of where it actually comes from.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
./importsmodule (f.e. using Webpack, or a CommonJS module that we place alongside thetscoutput, etc). So at least themulfile is portable.dcodeIO commentedon May 27, 2019
Internally, we usually have a file
env.tsthat we import from, containing the declares. This way, the module name will beenv.trusktr commentedon May 27, 2019
@dcodeIO Thanks, that's good to know. How do we get the
declare namespaceversion working?dcodeIO commentedon May 27, 2019
I think in
torch2424 commentedon May 28, 2019
Just in case it helps someone, this is my current importObject, that is passed during Wasm Instantiation.
anubhavgupta commentedon Aug 29, 2020
This works:
torch2424 commentedon Aug 31, 2020
Ah, so if anyone else comes across this, this is because imports are namespaced per their filename. So importing
consoleinindex.ts, means console needs to be inside an index object in the imports.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
trusktr commentedon Feb 7, 2021
What if two AS files have the same name, but they are at different paths in the file system?