Skip to content
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

Cannot access WebAssembly module exports before synchronous instantiation #6

Open
ghost opened this issue Aug 8, 2020 · 4 comments
Labels
question Further information is requested

Comments

@ghost
Copy link

ghost commented Aug 8, 2020

I get a problem when trying to access an uninitialized wasmModule. I used the example given here. I was attempting to perform a synchronous instantiation.

@MaxGraey
Copy link
Member

MaxGraey commented Aug 8, 2020

For async initialization you should defer log and wrap it to main for example:

javascript:

(async () => {
  const wasmModule = await loader.instantiate(module_wasm, {
    myConsole: {
      log(ptr) {
        console.log(wasmModule.exports.__getString(ptr));
      }
    }
  });
  wasmModule.exports.main();
})();

assemblyscript:

@external("myConsole", "log")
declare function log(str: string): void;

export function main(): void {
  log("Hello world!");
}

@MaxGraey MaxGraey added the question Further information is requested label Aug 8, 2020
@ghost
Copy link
Author

ghost commented Aug 8, 2020

Hi! Thanks for dropping the comment. I am getting an error that has to do with accessing the wasmModule variable from within the log function in the myConsole object (Node/JavaScript API - not within AssemblyScript). The same thing happens in both (a)synchronous cases. Here is the error signature:

UnhandledPromiseRejectionWarning: ReferenceError: Cannot access 'wasmModule' before initialization

@MaxGraey
Copy link
Member

MaxGraey commented Aug 8, 2020

Hmm, but it works in playground

@dcodeIO
Copy link
Member

dcodeIO commented Aug 8, 2020

The error appears to indicate that the module is being accessed before instantiation returns, i.e. code accessing the module runs before the await finished respectively then is called. This might also happen indirectly, for example if the module calls an import from WebAssembly during startup that in turn attempts to call back into WebAssembly from JS. If the latter is the case, --explicitStart may help (calling exports._start() manually after instantiation is complete).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants