new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

Deno · Reference · all subjects

api/wasm

16 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Wasm module import and type checking in Deno 2.1+

Starting in Deno 2.1, WebAssembly modules can be imported directly and their use is type checked. WebAssembly modules can be imported as ES modules in TypeScript/JavaScript files.

Importing and executing a compiled Wasm module

A WebAssembly module compiled to .wasm format can be imported using an import statement. For example, import { add } from "./add.wasm"; will import the add function from the compiled module and allow it to be called as a regular function.

Type checking Wasm function calls

Deno type checks function calls to Wasm module exports. If a function parameter is defined as i32 (number) in Wasm and a string is passed instead, deno check will report a TS2345 error indicating the type mismatch.

Wasm modules importing JavaScript/TypeScript modules

WebAssembly modules can import other modules using import statements in WAT format. A Wasm module can use (import "./file.ts" "functionName" (func ...)) to import and call functions from JavaScript or TypeScript files.

Overriding Wasm import specifiers with deno.json import map

When a Wasm module uses non-relative specifiers like "env" instead of relative paths, use an import map in deno.json to redirect them. For example, set {"imports": {"env": "./env.ts"}} to map the "env" specifier to a local file.

WebAssembly.Module and WebAssembly.Instance API

To use Wasm in Deno via the WebAssembly API: first fetch and obtain the Wasm binary as a Uint8Array, then create a WebAssembly.Module object from the binary, then instantiate it with new WebAssembly.Instance(module). The exports can then be accessed via instance.exports.

WebAssembly instantiateStreaming for efficient loading

WebAssembly.instantiateStreaming() is the most efficient way to fetch, compile, and instantiate a Wasm module in one operation. Example: const { instance, module } = await WebAssembly.instantiateStreaming(fetch("https://example.com/file.wasm"));. The .wasm file must be served with the application/wasm MIME type.

WebAssembly.compileStreaming for pre-compilation

WebAssembly.compileStreaming() can be used to compile a Wasm module from a stream without instantiating it, allowing additional work on the module before instantiation. After compilation, call WebAssembly.instantiate(module) to create an instance.

Fallback Wasm APIs for non-streaming scenarios

If streaming methods cannot be used, the less efficient fallback methods are WebAssembly.compile() and WebAssembly.instantiate() for non-streaming compilation and instantiation.

WASI Preview 1 module support in Deno

Deno supports WASI Preview 1 (wasip1) modules through the node:wasi module. To run a WASI Preview 1 module: import WASI from node:wasi, create a WASI instance with version: "preview1" and provide args and env objects, compile and instantiate the Wasm module with wasi.getImportObject() as the imports, then call wasi.start(instance).

WASI Preview 1 file system access via preopens

A WASI Preview 1 instance starts with no file system access. To grant access to directories, pass a preopens map like { "/data": "./data" } to the WASI constructor, mapping guest paths to real disk paths. The Deno process requires corresponding --allow-read and --allow-write permissions.

WASI Preview 2 components and jco tool

Deno supports WASI Preview 2 (wasip2) components built on the component model. These do not load through the WebAssembly API or node:wasi. Run a Preview 2 component directly with: deno run -A npm:@bytecodealliance/jco run hello.wasm args. To call a component from code, transpile it to JavaScript with: deno run -A npm:@bytecodealliance/jco transpile hello.wasm -o out, then import the generated ES module.

Rust compilation targets for WASI

Compile Rust to WASI Preview 1 with: rustc --target wasm32-wasip1 -O hello.rs -o hello.wasm. Compile to WASI Preview 2 with: rustc --target wasm32-wasip2 -O hello.rs -o hello.wasm.

wasmbuild tool for Rust and Wasm in Deno

wasmbuild is an official Deno tool that automates compiling Rust to WebAssembly and generating TypeScript bindings. It provides full type checking through generated TypeScript definitions and produces JavaScript compatible with bundlers like esbuild. Generated files can be committed to source control.

Using web_sys and js_sys for Rust Wasm bindings

The web_sys Rust crate provides bindings to most Web APIs available in Deno. The js_sys crate provides bindings to JavaScript's standard built-in objects. These are useful when doing extensive work with Web APIs in Rust compiled to WebAssembly.

Wasm optimization for production builds

For production, optimize WebAssembly binaries for size when serving over a network, or for speed when executing computationally intensive tasks on a server. The rust-wasm group maintains a list of tools for optimizing and manipulating WebAssembly binaries.

Give your agent this brain