• Stars
    star
    669
  • Rank 67,451 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 1 year ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A JavaScript polyfill for WebAssembly

polywasm

This is a polyfill for WebAssembly. It implements enough of the WebAssembly API to be able to run a .wasm file in a JavaScript environment that lacks a WebAssembly implementation. This is done by parsing the .wasm file and translating each WebAssembly function to a JavaScript function. This is not nearly as fast as running WebAssembly natively, but it's better than it not running at all.

Live demo

This polyfill is used as a fallback WebAssembly implementation for esbuild's online playground. You can use these links below to compare the playground with this polyfill enabled vs. disabled:

The playground shows off esbuild, which is a JavaScript code transformation tool (among other things). You'll need to type or paste some JavaScript code into the (enter your code here) textbox for esbuild to run.

Why does this exist?

WebAssembly support is already widespread so you don't normally need a polyfill to use it. However, certain modern JavaScript environments have WebAssembly disabled. For example, Apple's Lockdown Mode (an opt-in security enhancement) disables WebAssembly in Safari. You can use this polyfill to make a WebAssembly-based app work in Safari in Lockdown Mode anyway. It will be extremely slow because Lockdown Mode also disables JavaScript optimizations, but sometimes performance isn't critical.

Another reason to use this might be to play around with WebAssembly execution. For example, this polyfill makes it pretty trivial to instrument each function call to add caller/callee tracing or to make a copy of memory before/after a function call, which could be useful for debugging.

This library also exists because I thought building it would be an interesting challenge. I learned some new things about WebAssembly's file format and intermediate representation while building it.

How to use it

You need to include this polyfill before code that uses the WebAssembly API:

<script type="module">
  import { WebAssembly } from 'polywasm'
  globalThis.WebAssembly = WebAssembly
</script>
<script src="app.js"></script>

The polyfill is small (only ~25kb when minified) and can potentially be optionally loaded only when needed. Keep in mind that this polyfill requires that your JavaScript environment supports the BigInt64Array API. If you want to build the polyfill yourself instead of installing it through npm, you can clone this repo and run npm ci follwed by npm run build.

Limitations

Here are some limitations to be aware of:

  • No validation: This does not fully validate the WebAssembly that it compiles. It assumes that the provided WebAssembly is valid. You should not use this library as a WebAssembly validator.

  • No traps: This does not generate traps for invalid situations (e.g. divide by zero). Generating traps would dramatically slow down the polyfill even more and correctly-designed WebAssembly shouldn't even encounter any traps in the first place.

  • No NaN bit patterns: This does not preserve NaN bit patterns. WebAssembly does this natively but JavaScript VMs canonicalize NaN bit patterns which prevents a JavaScript-based WebAssembly polyfill from preserving them.

  • Limited API support: This does not implement the full WebAssembly API. There's no reason it can't, but right now I have only implemented the parts of the API that I needed to be able to load and run a .wasm file and run the WebAssembly specification's core tests.

Performance

These are the times to run a sample WebAssembly task using the polyfill. Each row is a result reported by bench/index.html for that browser.

Browser Minimum time Median time
Chrome (JIT) 27ms 37ms
Firefox (JIT) 79ms 88ms
Chrome (no JIT) 94ms 97ms
Firefox (no JIT) 127ms 133ms
Safari (no JIT) 244ms 256ms

These are the times for the same benchmark but with this polyfill's optimizations disabled (to demonstrate that the optimizations done by this polyfill improve run time):

Browser Minimum time Median time
Chrome (JIT) 40ms 55ms
Firefox (JIT) 132ms 150ms
Chrome (no JIT) 131ms 137ms
Firefox (no JIT) 188ms 196ms
Safari (no JIT) 332ms 354ms

The optimizations cause the benchmark to run 1.4x to 1.7x faster depending on the browser.

Implementation details

Numeric representation

Integer values in JS are always 64-bit floats while integer values in WASM are sign-independent 32-bit or 64-bit values. When representing WASM integers in JS, they need to have some sign (either signed or unsigned). For example, the 32-bit integer with all bits set to 1 could either be -1 or 0xFFFF_FFFF in JS, and the 64-bit integer with all bits set to 1 could either be -1n or 0xFFFF_FFFF_FFFF_FFFF in JS.

In this implementation, 32-bit integers are always represented as signed JS numbers and 64-bit integers are always represented as unsigned JS bigints. The signed/unsigned choice is arbitrary but must be consistent for the compiled code to work. Signed numbers is used for 32-bit integers because the cast to signed (x|0) is shorter than the cast to unsigned (x>>>0) and because some JS VMs have certain optimizations that make signed integer arithmetic faster than unsigned integer arithmetic. Unsigned bigints is used for 64-bit integers because cast to unsigned can be done with the & operator but cast to signed can't be done with a single operator.

Note that this means signed 32-bit less-than of a and b is a < b but unsigned 32-bit less-than is (a >>> 0) < (b >>> 0). Similarly unsigned 64-bit less-than of a and b is a < b but signed 64-bit less-than is something like (i64[0] = a, i64[0]) < (i64[0] = b, i64[0]) where i64 is a BigInt64Array.

AST format

WebAssembly bytecode is decoded into an AST so that it can be optimized before converting it to JavaScript. The compiler only ever generates the AST for a single basic block (i.e. sequence of bytecodes without any jumps). The AST is stored as numbers in an array instead of as JavaScript objects for performance, which can matter a lot when the JavaScript JIT is disabled.

Each AST node takes the following form (given the index ptr of a node for which space has already been reserved):

ast[ptr] = opcode | (childCount << 8) | (outputStackSlot << 24)
ast[ptr + 1] = /* child 1 */
ast[ptr + 2] = /* child 2 */
...
ast[ptr + N] = /* child N */
ast[ptr + N + 1] = /* an optional extra payload (e.g. an offset for load/store) */

Encoding the child count in the node metadata and putting optional extra data after the children allows the AST to be traversed generically without needing to know the specifics of each node's internal format.

Optimizations

The AST is optimized using a declarative set of peephole optimization rules before it's converted into JavaScript. These optimizations are tuned for the Go compiler's WebAssembly output, which does a lot of unnecessary 64-bit math. That's mostly fine when running WebAssembly natively but is pretty expensive when running WebAssembly via JS using BigInts. Avoiding unnecessary BigInts gives a decent performance boost.

For example, WebAssembly bytecode that extends a 32-bit integer out to 64-bit, adds a constant, and then wraps that integer back to 32-bit can be more efficiently represented using a 32-bit add bytecode instead:

// Before optimization
i32_wrap_i64(
  i64_add(
    i64_extend_i32_u(X),
    i64_const(Y)))

// After optimization
i32_add(
  X,
  i32_const(Y))

More Repositories

1

esbuild

An extremely fast bundler for the web
Go
38,106
star
2

thumbhash

A very compact representation of an image placeholder
Swift
3,564
star
3

glfx.js

An image effects library for JavaScript using WebGL
JavaScript
3,288
star
4

node-source-map-support

Adds source map support to node.js (for stack traces)
JavaScript
2,162
star
5

csg.js

Constructive solid geometry on meshes using BSP trees in JavaScript
JavaScript
1,791
star
6

lightgl.js

A lightweight WebGL library
JavaScript
1,517
star
7

thinscript

A low-level programming language inspired by TypeScript
C
1,360
star
8

webgl-filter

An image editor in WebGL
JavaScript
1,047
star
9

webgl-water

WebGL Water Demo
JavaScript
1,016
star
10

theta

JavaScript
751
star
11

kiwi

A schema-based binary format for efficiently encoding trees of data
C++
673
star
12

source-map-visualization

A simple visualization of source map data
JavaScript
506
star
13

skew

A web-first, cross-platform programming language with an optimizing compiler
JavaScript
411
star
14

glslx

A GLSL type checker, code formatter, and minifier for WebGL
HTML
409
star
15

webgl-path-tracing

Path tracing in GLSL using WebGL
JavaScript
345
star
16

fsm

Finite State Machine Designer
JavaScript
294
star
17

rapt

Robots Are People Too, a platformer in HTML5
C++
209
star
18

float-toy

Use this to build intuition for the IEEE floating-point format
JavaScript
155
star
19

sky

A text editor written in the Skew programming language
Objective-C++
136
star
20

indirectbuffer

A small library for out-of-heap memory in emscripten
C++
129
star
21

font-texture-generator

HTML
122
star
22

buddy-malloc

An implementation of buddy memory allocation
C
100
star
23

pbjs

A minimal implementation of Google Protocol Buffers for JavaScript
JavaScript
95
star
24

packer

Simple port of /packer/ by Dean Edwards to node.js
JavaScript
86
star
25

webgl-recorder

Record all WebGL calls from any app for playback later
JavaScript
81
star
26

socket.io-python

A socket.io bridge for Python
Python
67
star
27

glslx-vscode

GLSLX support in Visual Studio Code
TypeScript
65
star
28

tla-fuzzer

A fuzzer for various top-level await bundling strategies
JavaScript
56
star
29

unwind

Universal disassembler for Python bytecode (supports Python 2 and 3)
Python
49
star
30

node-flatbuffers

An implementation of FlatBuffers in pure JavaScript
JavaScript
48
star
31

figma-fill-rule-editor

A Figma plugin for editing fill rules
HTML
43
star
32

polyfrag

Geometry library that splits polyhedra using 3D planes
C++
38
star
33

esbuild-plugin-glslx

A plugin for esbuild that enables importing *.glslx files.
JavaScript
37
star
34

emscripten-library-generator

A library generator for the emscripten compiler
JavaScript
37
star
35

gl4

A small library for working with OpenGL 4
C++
34
star
36

cs224final

Final project for CS 224: Interactive Computer Graphics
C++
34
star
37

bitscript

An experimental language that compiles to JavaScript and C++
JavaScript
32
star
38

silvermath.js

A high-quality equation editor for JavaScript
JavaScript
32
star
39

decorator-tests

JavaScript
32
star
40

package-json-browser-tests

JavaScript
26
star
41

banner

A python scraper for Banner, Brown University's course catalog
Python
26
star
42

bundler-esm-cjs-tests

JavaScript
23
star
43

gl

A small C++ library for working with OpenGL
C++
23
star
44

bend

A statically-typed programming language for the web
JavaScript
20
star
45

esoptimize

A JavaScript AST optimizer
JavaScript
18
star
46

wasm-lang

A hobby language that compiles to WebAssembly
TypeScript
18
star
47

soda

A course browser for Brown University
JavaScript
18
star
48

OES_texture_float_linear-polyfill

A polyfill for OES_texture_float_linear (a WebGL extension)
JavaScript
17
star
49

node-terminal-notifier

A node API for notifications on Mac OS X Lion
JavaScript
17
star
50

node-spacemouse

An OS X driver for the 3dconnexion SpaceMouse in JavaScript
JavaScript
17
star
51

mineverse

A little experiment, please ignore
JavaScript
17
star
52

cppcodegen

Generates C++ code from a JSON AST
TypeScript
15
star
53

simple_svg_parser

A small library for getting geometry out of an svg file
Python
15
star
54

ride

A web-based IDE for ROS
JavaScript
15
star
55

mobile-touchpads

Use one or more iPhones as remote touchpads in an HTML page
JavaScript
14
star
56

easyxml

Concise XML generation in Python
Python
14
star
57

jest-esbuild-demo

JavaScript
13
star
58

fast-closure-compiler

Make the Google Closure Compiler start faster
JavaScript
13
star
59

webgl-vr-flight

Needs an iPhone 6 and Google Cardboard
HTML
10
star
60

uint8array-json-parser

JSON.parse() for Uint8Array to get around the maximum string size in V8
JavaScript
9
star
61

webgl-vr-editor

Makefile
9
star
62

rand.js

Super simple pseudo-random number generator in JavaScript
JavaScript
8
star
63

oculus-rift-webgl

Oculus Rift experiments with WebGL
8
star
64

skew-nanojpeg

A port of the NanoJPEG library
HTML
7
star
65

minisharp

An optimizing compiler from a subset of C# to JavaScript
C#
6
star
66

mdi-material-ui-inline

Makefile
5
star
67

glvr

GLUT-style bindings for the Oculus Rift
C
5
star
68

gccjs

A simple build tool that uses Google Closure Compiler
Java
5
star
69

skew-lang.org

HTML
5
star
70

skew-vscode

Skew support in Visual Studio Code
TypeScript
5
star
71

skew-imports

4
star
72

css-import-tests

CSS
4
star
73

js-destructuring-fuzzer

JavaScript
4
star
74

section_testing

A Rust library for section-style testing
Rust
4
star
75

dlmax

Python
3
star
76

head-tracker-window

HTML
3
star
77

browser-source-map-bugs

JavaScript
3
star
78

skew-atom

Syntax highlighting for Skew in the Atom editor
CoffeeScript
2
star
79

entry-point-resolve-test

Testing what happens when you bundle a package name
Shell
2
star
80

clang-initializer-list-plugin

A plugin for clang that checks if a constructor is missing a POD field in its initializer list
C++
2
star
81

dome-calibration

Ignore this. I'm only using a public repository because I need a quick HTTPS website.
HTML
2
star
82

SimpleLibOVR

Simple C API for Oculus Rift orientation info
C++
2
star
83

skew-rapt

Makefile
1
star
84

thin-ir

TypeScript
1
star