This document explains how to compile SmashLang code to WebAssembly (WASM) for browser execution.
SmashLang's WebAssembly support allows you to:
- Compile SmashLang code to run in web browsers
- Create JavaScript bindings for easy integration
- Build standalone web applications
- Share code between server and client
To compile SmashLang code to WebAssembly, you'll need:
- Emscripten: The WebAssembly compiler toolchain
- Node.js: For JavaScript bundling and testing
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh # Add this to your .bashrc or .zshrc for persistenceTo compile a SmashLang file to WebAssembly:
./scripts/compile_wasm.sh your_app.smashThis will create the following files in the dist directory:
your_app.wasm: The WebAssembly binaryyour_app.js: JavaScript glue code generated by Emscriptenyour_app.bindings.js: JavaScript bindings for easier usage
Generate a standalone HTML file with a demo interface:
./scripts/compile_wasm.sh --standalone your_app.smashWatch for changes and automatically recompile:
./scripts/compile_wasm.sh --watch --serve your_app.smashThis will start a development server and recompile whenever the source file changes.
Options:
-o, --output DIR Output directory (default: dist)
-t, --target TYPE Target platform: web, node (default: web)
-n, --no-optimize Disable optimization
-s, --standalone Generate standalone HTML file
-w, --watch Watch for changes and recompile
--serve Start a development server
--port PORT Development server port (default: 8080)
-h, --help Display this help message
After compiling your SmashLang code to WebAssembly, you can use it in your web application:
<!-- Include the generated JavaScript files -->
<script src="your_app.js"></script>
<script src="your_app.bindings.js"></script>
<script>
// Initialize the SmashLang module
const smash = new SmashLang();
// Wait for initialization
async function init() {
await smash.init();
// Call functions exported from your SmashLang code
const result = smash.add(5, 7);
console.log(result); // 12
const greeting = smash.greet("World");
console.log(greeting); // "Hello, World from SmashLang WASM!"
}
init();
</script>Here's a simple SmashLang file that can be compiled to WebAssembly:
// Import the standard library
import { console } from 'stdio';
// A simple function that adds two numbers
export function add(a: number, b: number): number {
return a + b;
}
// A function that returns a greeting string
export function greet(name: string): string {
return `Hello, ${name} from SmashLang WASM!`;
}
// Main function that runs when the module is loaded
export function main(): void {
console.log('SmashLang WASM module initialized');
}
Compile this file with:
./scripts/compile_wasm.sh --standalone examples/wasm/hello_wasm.smashThen open dist/hello_wasm.html in your browser to see it in action.
The WebAssembly compilation process involves several steps:
- SmashLang to C: Your SmashLang code is first transpiled to C code
- C to WebAssembly: Emscripten compiles the C code to WebAssembly
- JavaScript Bindings: Wrapper code is generated to make the WASM module easy to use from JavaScript
- HTML Demo: (Optional) A standalone HTML file is created with a demo interface
Current limitations of SmashLang WebAssembly support:
- DOM manipulation requires JavaScript interop
- File system access is limited in browser environments
- Some advanced SmashLang features may not be fully supported in WASM yet
- Direct DOM manipulation API
- WebGL/Canvas rendering support
- Web Workers integration
- Better debugging tools
- Smaller WASM binary size optimization