WebAssembly

:For its website, see Webassembly.org
WebAssembly (or shortly Wasm) is a programming language designed for executable programs as well as software interfaces for facilitating interactions between such programs and their host environment."Understanding WebAssembly text format" - MDN Web Docs It was designed for modern web browsers by W3C (World Wide Web Consortium). Its format defines a portable binary-code format. It is named to evoke the concept of assembly languagehttps://webassembly.org/. It is portable, platform-independent and is cross-platform.
==Overview==
WebAssembly is a low-level programming language for web browsers that is used for the near-native code execution speed of programs and web pages in the web browser. Similar to the general assembly language, compilers like Emscripten can compile codes written in C or C++ to WebAssembly code using the Binaryen. Web browsers do not allow WebAssembly code to directly access the Document Object Model. Wasm code must defer to JavaScript for this.
==Uses==
WebAssembly can also be used as a core specification for the JavaScript API, which is for embedding systemshttps://webassembly.github.io/spec/core/syntax/conventions.html. WebAssembly is also used for virtual machines and programs within the pages or independently. 
==Examples==
The example below shows "hello world" text inserted to the paragraph element from WebAssembly, which is a compiled binary format, in an HTML page. The code below is a text format code of WebAssembly, which exports the "Hello, World!\00" string:
(module
 (import "console" "log" (func $log (param i32) (param i32)))
 (memory $0 1)
 (export "memory" (memory $0))
 (data (i32.const 0) "Hello, World!\00")
 (func $helloWorld
 i32.const 0
 i32.const 13 ; length of "Hello, World!"
 call $log
 )
 (export "helloWorld" (func $helloWorld))
)
The text format code needs to be compiled into binary code using a tool like wat2wasm. After being compiled, the HTML code below shows that the JavaScript function exports the binary-format WebAssembly code:
<!DOCTYPE html>
<html>
<body>
 <script>
 const memory = new WebAssembly.Memory({initial:1}); //Get the initial memory binary-format WebAssembly code
 const importObject = {
 console: {
 log: function(offset, length) {
 const bytes = new Uint8Array(memory.buffer); //Get an array of bytes of Wasm string using Uint8Array, which requires a buffer of the memory
 let string = ; //A standard string
 for(let i = offset; i < offset + length; i++) {
 string += String.fromCharCode(bytes[i]); //Make a loop to insert a character from an array to a standard string
 }
 // Create a paragraph element and append it to the body
 const p = document.createElement('p'); //Create a paragraph
 p.textContent = string; //Insert a string to a paragraph
 document.body.appendChild(p); //Put the paragraph to the 'body' section
 }
 },
 js: { mem: memory }
 };
 fetch('hello.wasm').then(response => //Fetch the binary-format WebAssembly code
 response.arrayBuffer()
 ).then(bytes =>
 WebAssembly.instantiate(bytes, importObject) //Instantiate the object to import for the binary-format WebAssembly code
 ).then(results => {
 results.instance.exports.helloWorld(); //If the results success, it will call the helloWorld() function from WebAssembly code
 });
 </script>
</body>
</html>
The table below shows another example of a factorial function written in C and its corresponding WebAssembly code after compilation, shown both in .wat text format and in .wasm binary format, that is executed by a web browser or run-time environment that supports WebAssembly.
C source code and corresponding WebAssembly
C source code	WebAssembly (.wat) text format	WebAssembly (.wasm) binary format
int factorial(int n) {
 if (n == 0)
 return 1;
 else
 return n * factorial(n-1);
}	
(func (param i64) (result i64)
 local.get 0
 i64.eqz
 if (result i64)
 i64.const 1
 else
 local.get 0
 local.get 0
 i64.const 1
 i64.sub
 call 0
 i64.mul
 end)	
00 61 73 6D 01 00 00 00
01 06 01 60 01 7E 01 7E
03 02 01 00
0A 17 01
15 00
20 00
50
04 7E
42 01
05
20 00
20 00
42 01
7D
10 00
7E
0B
0B
==References==
Category:Programming languages