Skip to content

Isomorphic library to handle passing high-level data structures between AssemblyScript and JavaScript πŸ€πŸš€

License

Notifications You must be signed in to change notification settings

torch2424/as-bind

Repository files navigation

as-bind

Build Status npm bundle size (minified) npm npm version GitHub

Isomorphic library to handle passing high-level data structures between AssemblyScript and JavaScript. πŸ€πŸš€

Markdown Parser Demo

Asbind Markdown Parser Demo Gif

Table of Contents

Features

  • The library is isomorphic. Meaning it supports both the Browser, and Node! And has ESM, AMD, CommonJS, and IIFE bundles! 🌐
  • Wraps around the AssemblyScript Loader. The loader handles all the heavy-lifting of passing data into WebAssembly linear memory. πŸ’ͺ
  • Wraps around imported JavaScript functions, and exported AssemblyScript functions of the AssemblyScript Wasm Module. This allows high-level data types to be passed directly to exported AssemblyScript functions! 🀯
  • Moves a lot of work to compile-time using AssemblyScript Transforms and completely avoids module-specific β€œglue code”. πŸƒ
  • Installable from package managers (npm), with a modern JavaScript API syntax. πŸ“¦
  • The library is < 4KB (minified and gzip'd), including the AssemblyScript Loader ! 🌲
  • This library is currently (as of January, 2020) the wasm-bindgen of AssemblyScript. πŸ˜€

Installation

You can install as-bind in your project by running the following:

npm install --save as-bind

Quick Start

1. Compiling your Assemblyscript

To enable as-bind for your AssemblyScript Wasm modules, add the as-bind transform when compiling your module:

asc your-entryfile.ts --exportRuntime --transform as-bind [...other cli options...]

The things to notice are:

  • --transform as-bind - This is the as-bind transform that runs at compile time. It embeds all the required type information into the WebAssembly Module.
  • --exportRuntime - This is required for the AssemblyScript Loader to work properly. It exposes functions on the module to allocate memory from JavaScript.

For optional testing purposes , let's export an example function we can try in your-entryfile.ts:

export function myExportedFunctionThatTakesAString(value: string): string {
  return "AsBind: " + value;
}

2. In your Javascript

For browser JavaScript. We can do the following:

// If you are using a Javascript bundler, use the ESM bundle with import syntax
import * as AsBind from "as-bind";

// If you are not using a bundler add a <script> tag to your HTML
// Where the `src` points to the iife bundle (as-bind.iife.js), for example: https://unpkg.com/as-bind
// Then, INSTEAD of using the import syntax, do: `const { AsBind } = AsBindIIFE;`

const wasm = fetch("./path-to-my-wasm.wasm");

const asyncTask = async () => {
  const asBindInstance = await AsBind.instantiate(wasm);

  // You can now use your wasm / as-bind instance!
  const response = asBindInstance.exports.myExportedFunctionThatTakesAString(
    "Hello World!"
  );
  console.log(response); // AsBind: Hello World!
};
asyncTask();

For Node JavaScript, we would use the CommonJS bundle by doing the following:

// We need to import the direct as-bind.cjs.js for Node applications.
// This is because the default "main" key in the `package.json`,
// is the as-bind transform script
const AsBind = require("as-bind/dist/as-bind.cjs.js");

const fs = require("fs");

const wasm = fs.readFileSync("./path-to-my-wasm.wasm");

const asyncTask = async () => {
  const asBindInstance = await AsBind.instantiate(wasm);

  // You can now use your wasm / as-bind instance!
  const response = asBindInstance.exports.myExportedFunctionThatTakesAString(
    "Hello World!"
  );
  console.log(response); // AsBind: Hello World!
};
asyncTask();

Additional Examples

Passing a high-level type to a an exported function, and returning a high-level type

See the Quick Start

Passing a high-level type to an imported function

In this example, we will implement a console.log that we can call from AssemblyScript!

AssemblyScript

Inside of myWasmFileName.ts:

declare function consoleLog(message: string): void;

export function myExportedFunctionThatWillCallConsoleLog(): void {
  consoleLog("Hello from AS!");
}

JavaScript

import { AsBind } from "as-bind";

const wasm = fetch("./path-to-my-wasm.wasm");

const asyncTask = async () => {
  // Instantiate the wasm file, and pass in our importObject
  const asBindInstance = await AsBind.instantiate(wasm, {
    myWasmFileName: {
      consoleLog: message => {
        console.log(message);
      }
    }
  });

  // Should call consoleLog, and log: "Hello from AS!"
  asBindInstance.exports.myExportedFunctionThatWillCallConsoleLog();
};
asyncTask();

Supported Data Types

All primitive types, ie. Numbers (u8, f32, ...) , Strings, Typed Arrays (Uint8Array, Float32Array, ...) are supported. All of those types can also be used with Array<T>.

Custom classes are currently not support, but planned.

Browser SDK

as-bind works with the Browser SDK. For a fully working example, see the browser-sdk example.

Reference API

AsBind

The default exported ESM class of as-bind, also available as import { AsBind } from "as-bind" / const { AsBind } = require('as-bind').

Class Properties

The AsBind class is meant to vaguely act as the WebAssembly Object exposed to JavaScript environments.

version

AsBind.version: string

Value that is the current version of your imported AsBind.

instantiate
AsBind.instantiate: (
  moduleOrBuffer: (
    WebAssembly.Module |
    BufferSource |
    Response |
    PromiseLike<WebAssembly.Module>
  ),
  imports?: WasmImports
) => Promise<AsBindInstance>`

This function is the equivalent to the AssemblyScript Loader instantiate function, which is similar to the WebAssembly.instantiateStreaming function. It essentially takes as its parameters:

  • Any type of object that can be (resolved) and instantied into a WebAssembly instance. Which in our case would be an AsBindInstance.

  • A WebAssembly importObject, which would have all of your imported functions that can be called from within your AssemblyScript module.

instantiateSync
AsBind.instantiateSync: (
  moduleOrBuffer: (
    WebAssembly.Module |
    BufferSource
  ),
  imports?: WasmImports
) => AsBindInstance`

This is a synchronous version of AsBind.instantiate. This does not accept a promise-like as its module, and returns an AsBindInstance instead of a Promise that resolves an AsBindInstance. This is only reccomended for use in testing or development. Please see the Documentation sections for AsBind.instantiate for more information.

Instance Properties

An AsBindInstance is vaguely similar to a WebAssembly instance.

loadedModule

The raw, untouched instance of the WebAssembly Module.

exports

Similar to to WebAssembly.Instance.prototype.exports, this is an object containing all of the exported fields from the WebAssembly module. However, exported functions are bound / wrapped in which they will handle passing the supported high-level data types to the exported AssemblyScript function.

importObject

Similar to to WebAssembly.instantiateStreaming() importObject, this is the augmented importObject. It’s based on the original that was passed to one of the instantiation functions, but functions have been wrapped to handle high-level types.

Motivation

This library was inspired by several chats I had with some awesome buddies of mine in the WebAssembly Community:

  • Till Schneidereit and I had a chat about WasmBoy, and about how I had a really good experience writing the emulator, even though I had to do my own memory management. But they helped me realize, building something low level isn't that bad with manual memory management, but building something like a markdown parser would be very tedious since you have to manually write the string back and forth. Which then inspired this library, and its markdown parser demo.

  • While I was building WasmByExample I wanted to start building the "High Level Data Structures" section. I then realized how much work it would be to maintain code for passing data between WebAssembly Linear memory would be for each data type, and how much work it would be to created each individual example. Then, my buddy Ashley Williams helped me realize, if your docs are becoming too complex, it may be a good idea to write a tool. That way you have less docs to write, and users will have an easier time using your stuff!

Thus, this library was made to help AssemblyScript/JavaScript users build awesome things! I also want to give a huge thanks to the AssemblyScript team and community for the help they provided me. I'm super appreciative of you all! πŸ˜„πŸŽ‰

(Also! Huge Shoutout to @surma for doing the awesome refactor to use AssemblyScript compiler transforms! πŸ™)

Performance

TL;DR This library should be pretty darn fast. πŸ€”

The transform embeds all the required type information of imported and exported functions into a custom section of the WebAssembly module. All the runtime does is utilize the AssemblyScript Loader to convert these types from JS to ASC and vice-versa. Apart from Array<T>, which needs to be handled recursively, the overhead is fairly static and minimal.

In the future, these types of high-level data passing tools might not be needed at all, as the WebAssembly Inteface Types proposal aims to give WebAssembly an understanding of higher-level types.

Projects using as-bind

  • The as-bind example is a Markdown Parser, in which as-bind takes in a string, passes it to a rough markdown parser / compiler written in AssemblyScript, and returns a string. (Live Demo), (Source Code)

  • use-as-bind is a React hook for using as-bind with an as-bind enabled WASM source. It's goal is to provide a simple API for React users to add WASM to their apps. (Live Demo)

  • as-loader is a webpack loader for AssemblyScript with built-in as-bind support.

If you're project is using as-bind, and you would like to be featured here. Please open a README with links to your project, and if appropriate, explaining how as-bind is being used. 😊

Contributing

Contributions are definitely welcome! Feel free to open a PR for small fixes such as typos and things. Larger fixes, or new features should start out as an issue for discussion, in which then a PR should be made. πŸ₯³

This project will also adhere to the AssemblyScript Code of Conduct.

License

MIT. πŸ“

About

Isomorphic library to handle passing high-level data structures between AssemblyScript and JavaScript πŸ€πŸš€

Resources

License

Stars

Watchers

Forks

Packages

No packages published