WebAssembly, also called WASM, is binary format low level code developed to be executed inside browsers in the most efficient way. WebAssembly code is structured with following concepts −
Let us learn them in detail now.
Values in WebAssembly are meant to store complex data such as text, strings and vectors. WebAssembly supports the following −
Bytes is the simplest form of values supported in WebAssembly. The value is in hexadecimal format.
For exampleBytes represented as b, can also take natural numbers n, where n <256.
byte ::= 0x00| .... |0xFF
In WebAssembly, integers supported are as given below −
In WebAssembly floating point numbers supported are as follows −
Names are sequence of character, with scalar values defined by Unicode, which is available at the link http://www.unicode.org/versions/Unicode12.1.0/ given herewith.
The entities in WebAssembly are classified as types. The types supported are as stated below −
Let us study them one by one.
The values type supported by WebAssembly are as mentioned below −
valtype ::= i32|i64|f32|f64
The values written inside brackets are executed and stored inside result types. The result type is the output of the execution of a block of code made up of values.
resulttype::=[valtype?]
A function type will take in vector of parameters returns a vector of results.
functype::=[vec(valtype)]--> [vec(valtype)]
Limits are the storage range linked with memory and table types.
limits ::= {min u32, max u32}
Memory types deal with linear memories and the size range.
memtype ::= limits
Table Types are classified by the element type assigned to it.
tabletype ::= limits elemtype elemtype ::= funcref
Table type is dependent on the limit for the minimum and maximum size assigned to it.
Global Type holds the global variables that have the value, that can change or remain the same.
globaltype ::= mut valtype mut ::= const|var
External Types deals with imports and external values.
externtype ::= func functype | table tabletype | mem memtype | global globaltype
WebAssembly code is a sequence of instructions that follows a stack machine model. As WebAssembly follows a stack machine model, the instructions are pushed on the stack.
The argument values for a function, for example, are popped from stack and the result is pushed back on the stack. In the end, there will be only one value in the stack and that is the result.
Some of the commonly used Instructions are as follows −
Numeric Instructions are operations, which are performed on numeric value.
For examplenn, mm ::= 32|64 ibinop ::= add|sub|mul|div_sx|rem_sx|and|or|xor irelop ::= eq | ne | lt_sx | gt_sx | le_sx | ge_sx frelop ::= eq | ne | lt | gt | le | ge
Variable instructions are about accessing the local and global variables.
For example
To access local variables −
get_local $a get_local $b
To set local variables −
set_local $a set_local $b
To access global variables −
get_global $a get_global $b
To set global variables −
set_global $a set_global $b