Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a browser supports WebAssembly?

With support for WebAssembly coming into all new major browsers, how can I check whether the current browser which is visiting my website supports it?

like image 465
Ayush Gupta Avatar asked Dec 19 '17 04:12

Ayush Gupta


People also ask

Is WebAssembly supported by all browsers?

WebAssembly is a new type of code that can be run in modern web browsers — it is a low-level assembly-like language with a compact binary format that runs with near-native performance and provides languages such as C/C++, C# and Rust with a compilation target so that they can run on the web.

Does Google Chrome support WebAssembly?

Do all browsers support WebAssembly? Five major browsers support WebAssembly. Google Chrome, Firefox, Opera, Edge, and Safari are the big five browsers that support Wasm.


2 Answers

There are a few ways that you can detect the presence of WebAssembly. The basic one is to check whether WebAssembly if of type "object" in the global scope, but "global scope" is a tricky thing to get to in different JavaScript environments (main browser thread, worker, node.js).

Doing so is also not technically sufficient because you could have WebAssembly support but be unable to actually compile or instantiate because of CSP (and exactly what CSP disallows isn't standardized yet, work ongoing here).

A conservative check could be as follows:

const supported = (() => {      try {          if (typeof WebAssembly === "object"              && typeof WebAssembly.instantiate === "function") {              const module = new WebAssembly.Module(Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00));              if (module instanceof WebAssembly.Module)                  return new WebAssembly.Instance(module) instanceof WebAssembly.Instance;          }      } catch (e) {      }      return false;  })();    console.log(supported ? "WebAssembly is supported" : "WebAssembly is not supported");

It does the following:

  • Check whether WebAssembly is accessible in the current scope. If it's not global we don't really care!
  • See whether it has the .instantiate function, which we don't actually use here but which you'd want to use when you actually instantiate because it's asynchronous and can handle large modules on the main thread or off.
  • Try to synchronously compile the smallest possible module (magic number '\0', 'a', 's', 'm', followed by version number 1 encoded as a uint32), and see if we get a WebAssembly.Module out of it.
  • Finally, try to synchronously instantiate that module, and check that it's a WebAssembly.Instance.

This is a bit much but should work regardless of:

  • Where code is running (main thread, worker, node.js).
  • How CSP ends up being standardized.
like image 88
JF Bastien Avatar answered Oct 14 '22 03:10

JF Bastien


(Insufficent reputation to comment so....) If you're testing on older browsers the () => syntax is not supported, so it can be done as a function all instead:

function wasmSupported() {     // try/catch, return false; as in JF Bastien's answer }      if(wasmSupported()) { ... } 
like image 34
winter_limelight Avatar answered Oct 14 '22 05:10

winter_limelight