### Grow WebAssembly Memory Source: https://webassembly.org/getting-started/js-api Increases the size of an existing WebAssembly Memory object by a specified number of WebAssembly pages. Growing beyond the maximum limit will throw a RangeError. ```javascript memory.grow(1); ``` -------------------------------- ### Create WebAssembly Memory in JavaScript Source: https://webassembly.org/getting-started/js-api Creates a WebAssembly Memory object with specified initial and maximum page sizes. Pages are fixed at 64KiB. This memory can be used to store data for WebAssembly modules. ```javascript var memory = new WebAssembly.Memory({ initial: 10, maximum: 100 }); ``` -------------------------------- ### Fetch, Compile, and Instantiate WebAssembly Module (JavaScript) Source: https://webassembly.org/getting-started/js-api Demonstrates the complete process of loading a WebAssembly module from a URL, compiling its bytes, and instantiating it with the necessary imports. It uses the Fetch API and promises to handle the asynchronous operations, finally executing an exported function. ```javascript fetch('simple.wasm') .then((response) => response.arrayBuffer()) .then((bytes) => instantiate(bytes, importObject)) .then((instance) => instance.exports.e()); ``` -------------------------------- ### Instantiate WebAssembly Module from Bytes and Imports (JavaScript) Source: https://webassembly.org/getting-started/js-api Combines WebAssembly compilation and instantiation into a single asynchronous operation. It takes raw Wasm bytes and an import object, returning a Promise that resolves to a WebAssembly Instance. This is a common pattern for loading and running Wasm modules. ```javascript function instantiate(bytes, imports) { return WebAssembly.compile(bytes).then( (m) => new WebAssembly.Instance(m, imports) ); } ``` -------------------------------- ### WebAssembly Module Memory Export (Wasm) Source: https://webassembly.org/getting-started/js-api A WebAssembly module definition that exports a linear memory named 'mem' and a function 'accumulate'. This allows JavaScript to access and interact with the module's memory. ```wast (module (memory (export "mem") 1) (func (export "accumulate") (param $ptr i32) (param $length i32) ...)) ``` -------------------------------- ### Define Simple WebAssembly Module with Import and Export (WebAssembly Text Format) Source: https://webassembly.org/getting-started/js-api A basic WebAssembly module written in the text format. It imports a function 'i' from a namespace 'imports' and exports a function named 'e'. The exported function 'e' calls the imported function 'i' with a constant value. ```webassembly (module (func $i (import "imports" "i") (param i32)) (func (export "e") i32.const 42 call $i)) ``` -------------------------------- ### Access and Write to WebAssembly Memory Source: https://webassembly.org/getting-started/js-api Accesses the underlying ArrayBuffer of a WebAssembly Memory object and uses a typed array (Uint32Array) to write a value directly into the memory. This allows for direct data manipulation. ```javascript new Uint32Array(memory.buffer)[0] = 42; ``` -------------------------------- ### Populate and Use Exported WebAssembly Memory Source: https://webassembly.org/getting-started/js-api Populates the memory exported by a WebAssembly instance with integer data using a Uint32Array and then calls an exported function 'accumulate' to process the data. This demonstrates direct memory interaction from JavaScript. ```javascript var i32 = new Uint32Array(instance.exports.mem); for (var i = 0; i < 10; i++) i32[i] = i; var sum = instance.exports.accumulate(0, 10); ``` -------------------------------- ### Create Import Object for WebAssembly Module (JavaScript) Source: https://webassembly.org/getting-started/js-api Defines the JavaScript object that will be passed as imports to a WebAssembly module. This object mirrors the two-level namespace defined in the WebAssembly module's import section. In this case, it provides the 'i' function for the 'imports' namespace. ```javascript var importObject = { imports: { i: (arg) => console.log(arg) } }; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.