npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@xtuc/asyncify-wasm

v2.0.0

Published

Standalone Asyncify helper for Binaryen

Downloads

348

Readme

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.

About this fork

Asyncify relies on a reserved space in memory to save and restore the program's stack for async operations.

The original version of Asyncify is hardcoding the reserved stack space's size to 1024 bytes and at a specific location. If the stack of your program exceeded 1024 bytes it would run into a trap.

This fork allows the user to customize where and how large the Asyncify stack space is (see WebAssembly side usage).

Usage

WebAssembly side

Allocate the Asyncify stack space (statically or dynamically) and expose its location / size with get_asyncify_stack_space_ptr / get_asyncify_stack_space_size functions respectively:

/// Arbitrary stack size of 50kib.
const ASYNCIFY_STACK_SIZE: usize = 50 * 1024;
/// Scratch space used by Asyncify to save/restore stacks.
static ASYNCIFY_STACK: [u8; ASYNCIFY_STACK_SIZE] = [0; ASYNCIFY_STACK_SIZE];

#[no_mangle]
extern "C" fn get_asyncify_stack_space_ptr() -> i32 {
    ASYNCIFY_STACK.as_ptr() as i32
}

#[no_mangle]
extern "C" fn get_asyncify_stack_space_size() -> i32 {
    ASYNCIFY_STACK_SIZE as i32
}

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] [[email protected],...] in.wasm -o out.wasm

JavaScript side

Install:

yarn add --dev @xtuc/asyncify-wasm
# or
npm install -D @xtuc/asyncify-wasm

First, import asyncify via:

import * as Asyncify from '@xtuc/asyncify-wasm';

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();