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

Unexpected behaviour with async/await logic in JS host #812

Closed
yourheropaul opened this issue Sep 3, 2019 · 5 comments
Closed

Unexpected behaviour with async/await logic in JS host #812

yourheropaul opened this issue Sep 3, 2019 · 5 comments

Comments

@yourheropaul
Copy link

yourheropaul commented Sep 3, 2019

I'm building a WASM application that relies upon external, asynchronous operations carried out by the host. The WASM part must initiate an unknown number of requests by means of a function call to the host, and then await responses.

Awaiting promises is causing an issue, where the first value is always ignored

Context

I have this WASM code:

declare namespace operation {
    @external("operation", "call")
    function call(name:string): u32
}

declare namespace console {
    @external("console", "log")
    export function logs(val: u32): void;
}

export function myOperation(): void {
    console.log(1)
    const response = operation.call("some-name")
    console.log(4) // See below
    // Do something with response
}

Correspondingly, operation.call() is provided in the Node.js application using the loader:

export class WASMRumtime {
    protected api: ApiPromiseInterface
    protected module: ASUtil

    constructor(src: Uint8Array, api: ApiPromiseInterface) {
        this.module = instantiateBuffer(src, {
            env: {/* standard env config*/}.
            operation: {
                call: async (name: number) => {
                    console.log(2)
                    // This always returns a 0 value the first time
                    const v = await this.api.somePromiseReturningU32(this.moddule.__getString(name))
                    console.log(3)
                    return v
                }
            },
            console: { log: (value:number) => console.log(value) } }
        })
    }
}

Expected results

When myOperation() is called on the WASM, it should call operation.call() and get some value back. The first call is always a zero value, which is never returned by the promise in this case. Subsequent calls produce the previous value.

To try to understand what's happened, I've included some console.log() statements. I expected them to the called in numeric order, but they're not. The output to the console is:

1 <-- inside the WASM, before call to operation.call()
2 <-- inside the JS callback, before await
4 <-- inside the WASM, after operation.call() should have returned
3 <-- Inside the JS callback, after the await

Any idea why this might be?

Additionally, since function callback references and the table seem to have been disabled, is there any better way to get asynchronous data from the WASM host on demand?

@MaxGraey
Copy link
Member

MaxGraey commented Sep 3, 2019

You try do some continuation (interrupt wasm execution by host code and come back later in the same frame with continue execution). That's not work in WebAssembly without extra efforts. See this experimental PR which describe pretty simple example usage of Binaryen's Asyncify. But this required latest Binaryen which not yet landed

@yourheropaul
Copy link
Author

Thanks, I'll try that out!

Is there any other way of achieving a callback function, given that the Table is removed?

@MaxGraey
Copy link
Member

MaxGraey commented Sep 3, 2019

You could use table. Just add --importTable flag and create it in env: { table: new WebAssembly.Table({ initial:1, element:"anyfunc" }) }

@yourheropaul
Copy link
Author

--importTable during compile time solves the problem for my use case, thanks!

@stale
Copy link

stale bot commented Oct 4, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Oct 4, 2019
@stale stale bot closed this as completed Oct 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants