In our previous sections, we have familiarized ourselves with WebAssembly (Wasm) and have seen how to compile C++ into wasm modules. Now let's delve deeper into the core concepts of Wasm that include elements like memory management, stack machine, and linear memory.
Memory Management: WebAssembly deals with two types of memory: Stack and Heap. The stack is used primarily for storing function call information and local variables, whereas heap is used to store global variables, and for dynamically allocated memory. Remember, Wasm memory is linear and byte-addressable, starting from an index 0.
Stack Machine: WebAssembly is based on a stack machine concept. This means computations are based on a stack where operands are pushed onto the stack, and operations like addition and multiplication pop their operands from the stack and push the results back to the stack.
Linear Memory: Wasm uses a large contiguous memory array, also known as linear memory. It can be grown dynamically but cannot be shrunk. The memory is shared between all the functions within a Wasm module and can also be accessed and manipulated from JavaScript.
Understanding these core concepts is fundamental to mastering WebAssembly. The stack and linear memory form the backbone of Wasm's execution, while memory management is crucial to efficient and secure operations.