Skip to content

add invokeSync method#11

Open
nikpivkin wants to merge 1 commit intowapc:mainfrom
nikpivkin:invoke-sync
Open

add invokeSync method#11
nikpivkin wants to merge 1 commit intowapc:mainfrom
nikpivkin:invoke-sync

Conversation

@nikpivkin
Copy link

This PR adds an invokeSync method that is similar to invoke, only executed synchronously.

The need for a synchronous method appeared for calling a WASM function from another WASM module. A small example:

async function main() {
    buffer = await fs.readFile(path.join(__dirname, 'wasm', 'main.wasm'));
    const host = await instantiate(buffer);

    const policyWasm = await fs.readFile("policy.wasm");
    const policy = await loadPolicy(policyWasm, {}, {
        "greet": () => (msg) => {
            console.log("Message: ", msg);
            const payload = encode(msg);
            console.log("Payload: ", payload);
    
            res = host.invokeSync('greet', payload)
            return result = decode(res);
        }
    })

    const result = policy.evaluate({});
    console.log(JSON.stringify(result, null, 2));
}

Signed-off-by: Nikita Pivkin <nikita.pivkin@smartforce.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant