Skip to main content
Skip table of contents

Calling a WebAssembly

A WebAssembly (abbreviated wasm) is a portable, binary format for holding compiled code that can be called from web client and server applications. General info. can be found here. Details on building and compiling a WebAssembly can be found here.

A WebAssembly can be called by a ClearBlade code-service running on V8. This example shows you how to call a WebAssembly from a V8 code-service. Creation of the WebAssembly is mentioned but is out of the scope of this document.


PREREQUISITES:

  1. Tools required to compile a WebAssembly pre-installed. There are several such tools to choose from, e.g. assemblyscript NPM module. Building the WebAssembly is mentioned but is out of the scope of this document.

STEPS:

  1. Create your WebAssembly. For example:

  2. Edit and save the source…

    image-20260330-212427.png

    1. Build the WebAssembly (e.g. npm run asbuild)

    2. Look for the generated .wasm file (e.g. build/release.wasm). The .wasm can be renamed.

  3. Upload the .wasm file to a Bucket:

    1. In a ClearBlade system choose Files then Add Bucket Set

      image-20260330-211525.png

    2. Name the new Bucket Set and choose the appropriate Platform Storage. Bucket Sets can be stored locally, in a Google Bucket or in an AWS S3 bucket.

      image-20260330-211740.png

    3. Select Add File, decide if the WebAssembly will be added to the Inbox, Outbox, or Sandbox folder. Then browse to your .wasm

      image-20260330-211950.png

  4. Write the code-service:

    1. Make sure to make the clearblade_async library a dependency of the the code-service or any of its libraries.

    2. Choose Engine type V8

      image-20260330-212922.png
    3. Use a function like this for loading the WebAssembly:

      CODE
      async function loadWasmFromBucket (deployment, file) {
              const fs = new ClearBladeAsync.FS(deployment)
              const resp = await fs.readFile(file)
              const mod = new WebAssembly.Module(resp)
              const instance = new WebAssembly.Instance(mod)
              return instance
        }

      This function may be added to a library that can be called from the code-service.

    4. Write the rest of the service, save and test

      image-20260330-212800.png

      image-20260330-213118.png

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.