Socket
Book a DemoInstallSign in
Socket

async-wasm

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-wasm

Add asynchronous external JS calls to your WASM that behave as sync WASM functions

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

async-wasm

Add asynchronous external JS calls to your WASM that behave as sync WASM functions

npm install async-wasm

Currently there is no way to do async calls from wasm (all external calls are sync). This means it's tricky to call out from WASM to do I/O in Javascript.

This module makes was support that, by using a Worker thread from Node.js to run the WASM and resolve the async calls in the main worker, while the thread is blocking.

NOTE: You might have to run your program with node --experimental-worker app.js since this is using the worker_threads core module.

Usage

const AsyncWasm = require('async-wasm')

const fs = require('fs')

// Create a new async WASM worker based on a WASM buffer.
// In this example, hello.wasm has one external function hello_world,
// that calls out to a function called fetch in the hypermachine namespace.

const aw = new AsyncWasm(fs.readFileSync('hello.wasm'), {
  hypermachine: {
    fetch (ptr, cb) {
      console.log('fetch is called with argument:', ptr)

      // read(ptr, len, cb) reads from the wasm memory
      aw.read(ptr, 10, function (_, buf) {
        console.log('read', buf)

        // write(ptr, buf, cb) writes to the wasm memory
        aw.write(ptr, Buffer.from('lolol'), function () {
          console.log('wrote data')

          aw.read(ptr, 10, function (_, buf) {
            console.log('read now', buf)

            // Call the callback with a numeric return value when done
            // If you call it with an error, -1 is returned in wasm.
            cb(null, 10)
          })
        })
      })
    }
  }
})

// Invoke external methods on the WASM using the `.call(name, ...args, cb)` method.
aw.call('hello_world', function (err, val) {
  console.log('done', err, val)
  aw.destroy()
})

See the example folder for a full example that contains the WAT/WASM code as well.

License

MIT

FAQs

Package last updated on 23 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts