For the complete documentation index, see llms.txt. This page is also available as Markdown.

WASM in the Browser

Learn to deploy BoxLang in the browser using WASM and MatchBox

The full MatchBox docs now live at matchbox.boxlang.io. Try the online playground, take the online course, or see the MatchBox overview.

JavaScript + WebAssembly

MatchBox can compile BoxLang for the browser and Node.js via WebAssembly. There are three distinct modes, chosen to match different use-cases.

Mode
Command
Use case

JS Module (AOT)

--target js

Ship a pre-compiled BoxLang app as an ES module

WASM binary (AOT)

--target wasm

Raw .wasm binary, brought your own loader

Runtime (JIT-like)

Build pkg/ once, call run_boxlang()

Execute source strings at runtime in the browser


Mode 1: JavaScript ES Module (AOT)

This is the recommended way to ship a BoxLang application to the browser. MatchBox compiles your script to bytecode, embeds it in a WASM binary, and wraps it in a JavaScript ES module that bootstraps the runtime automatically.

Compile

matchbox --target js my_lib.bxs
# Produces: my_lib.js  +  my_lib.wasm

Use in HTML

<!DOCTYPE html>
<html>
<head><title>BoxLang App</title></head>
<body>
<script type="module">
    import { greet, calculate } from './my_lib.js';

    const result = await greet("Developer");
    document.body.textContent = result;
</script>
</body>
</html>

Use in Node.js

Exporting Functions

BoxLang functions defined at the top level of your script are automatically exported. Access modifiers have no effect on WASM exports — all functions are accessible:


Mode 2: Raw WASM Binary (AOT)

Use --target wasm when you want the raw .wasm file and full control over how it is loaded. This is useful for integration with non-standard JS runtimes, edge platforms, or when you are bundling the WASM via a tool like Webpack or Vite.

Load it manually using the standard WebAssembly API:

Refer to the MDN WebAssembly docs for the full instantiation API.


Mode 3: Runtime Mode (Dynamic Execution)

In runtime mode you ship the MatchBox engine itself (pkg/) and execute BoxLang source code dynamically at run time — similar to a JIT. This is useful for:

  • Allowing user-provided BoxLang scripts in your application.

  • Interactive BoxLang playgrounds.

  • Server-side rendering on a WASM-capable edge runtime.

HTML Integration

Persistent VM (calling functions by name)

For apps that need to call multiple BoxLang functions efficiently, use the persistent BoxLangVM instance rather than re-initialising on every call:


JavaScript Interop from BoxLang

When BoxLang code is running inside a browser WASM context, it can access the JavaScript environment through the js global:

js.* is only available in browser WASM. Using it in a native build throws a runtime error.


Serving Locally for Development

Browsers block WASM file loading over file://. Serve your project with any local HTTP server:

Then open http://localhost:8080 in your browser.


Production Deployment

The WASM output files (*.js + *.wasm) can be deployed to any static hosting service:

  • CDN — Upload to S3, Cloudflare R2, or a similar object store.

  • Vercel / Netlify — Drop the files into your project's output directory.

  • Edge Workers — The raw WASM binary can be loaded directly into Cloudflare Workers or similar runtimes. See WASM Container for details.

Ensure your server sets the correct Content-Type for .wasm files:

Most modern hosts handle this automatically.

Last updated

Was this helpful?