Somtimes the user’s browser is not the newest version. It may not support WebAssembly or a few functions. So we have to test it before using.

Is WebAssembly Supported?

Import a WebAssembly module and create an instance of it. Track this process to see if any problems arise.

<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="./jquery.js"></script>
<title>Test</title>
</head>
<body>
<script>
  function isWebAssemblySupported()
  {
    try{
      if( typeof WebAssembly === "object")
      {
        const module = new WebAssembly.Module(new Uint8Array([0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00]));
        if( module instanceof WebAssembly.Module )
        {
          const moduleInstance = new WebAssembly.Instance(module);
          return (moduleInstance instanceof WebAssembly.Instance);
        }
      }
    }
    catch (err)
    {
      console.log( err );
    }

    return false;
  }

  console.log((isWebAssemblySupported() ? "WebAssembly is supported!": "WebAssembly is not supported!"));
</script>
</body>
</html>

Is The Function Available?

  if( typeof WebAssembly.instantiateStreaming === "function" )
  {
    console.log("You can use the WebAssembly.instantiateStreaming!");
  }
  else
  {
    console.log("WebAssembly.instantiateStreaming is not a function!");
  }

The code snippet was uploaded to GitHub: learnWebAssembly

Categories: Web

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments

Content Summary
: Input your strings, the tool can get a brief summary of the content for you.

X
0
Would love your thoughts, please comment.x
()
x