GithubHelp home page GithubHelp logo

isabella232 / asyncify Goto Github PK

View Code? Open in Web Editor NEW

This project forked from googlechromelabs/asyncify

0.0 0.0 0.0 1.25 MB

Standalone Asyncify helper for Binaryen

License: Apache License 2.0

JavaScript 84.72% HTML 1.97% PowerShell 0.75% Rust 5.17% WebAssembly 7.40%

asyncify's Introduction

Asyncify

This is a JavaScript wrapper intended to be used with Asyncify feature of Binaryen.

Together, they allow to use asynchronous APIs (such as most Web APIs) from within WebAssembly written and compiled from any source language.

Usage

WebAssembly side

Import and use required APIs as regular synchronous FFI functions in your code.

After the code is compiled to WebAssembly, post-process it using wasm-opt from the Binaryen toolchain:

wasm-opt --asyncify [-O] [--pass-arg=[email protected],...] in.wasm -o out.wasm

JavaScript side

First, import asyncify via:

import * as Asyncify from 'https://unpkg.com/asyncify-wasm?module';

Compilation / instantiation APIs are designed to be drop-in replacements for those of regular WebAssembly interface, but with async support.

Then, you can use new Asyncify.Instance, Asyncify.instantiate and Asyncify.instantiateStreaming like you would with corresponding WebAssembly functions, but with added support for async imports and all exports wrapped into async functions, too.

For example:

let { instance } = await Asyncify.instantiateStreaming(fetch('./out.wasm'), {
  get_resource_text: async url => {
    let response = await fetch(readWasmString(instance, url));
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }
    return passStringToWasm(instance, await response.text());
  }
});

await instance.exports._start();

asyncify's People

Contributors

rreverser avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.