| WebAssembly | |
|---|---|
| Paradigm | structured;stack machine[1] |
| Designed by | W3C |
| Developer | |
| First appeared | March 2017; 8 years ago (2017-03) |
| OS | Platform independent |
| License | Apache License 2.0 |
| Filename extensions |
|
| Website | webassembly |
| Influenced by | |
WebAssembly (Wasm) defines aportablebinary-code format and a corresponding text format forexecutable programs[2] as well assoftware interfaces for facilitating communication between such programs and their host environment.[3][4][5][6]
The main goal of WebAssembly is to facilitate high-performance applications onweb pages, but it is also designed to be usable in non-web environments.[7] It is anopen standard[8][9] intended to support any language on any operating system,[10] and in practice many of the most popular languages already have at least some level of support.
Announced in 2015 (2015) and first released in March 2017 (2017-03), WebAssembly became aWorld Wide Web Consortium (W3C) recommendation on 5 December 2019[11][12][13] and it received theProgramming Languages Software Award fromACMSIGPLAN in 2021.[14] The W3C maintains the standard with contributions fromMozilla,Microsoft,Google,Apple,Fastly,Intel, andRed Hat.[15][16]
The name "WebAssembly" is intended to suggest bringingassembly language programming to theWorld Wide Web, where it will be executedclient-side, by the website-user's computer via the user'sweb browser. To accomplish this, WebAssembly must be much more hardware-independent than a true assembly language.
WebAssembly was first announced in 2015,[17] and the first demonstration was executingUnity'sAngry Bots inFirefox,[18]Google Chrome,[19] andMicrosoft Edge [Legacy].[20] The precursor technologies wereasm.js fromMozilla andGoogle Native Client,[21][22] and the initial implementation was based on the feature set of asm.js.[23][note 1]
In March 2017, the design of theminimum viable product (MVP) was declared to be finished and the preview phase ended.[25] In late September 2017,Safari 11 was released with support. In February 2018, the WebAssembly Working Group published three public working drafts for the Core Specification, JavaScript Interface, and Web API.[26][27][28][29]
The MVP was focused on low-level languages such as C and C++, with the intent to add features useful for high-level languages in a future version.[30]
Multithreading is currently a draft, but has been supported by Chrome since version 75 in June 2019, Firefox version 79, and Safari version 14.1[31][32][33]
The WebAssembly 2.0 specification was finished in 2022 and became a W3C standard in December 2024. It adds manySIMD-related instructions and a new v128 datatype, with the ability for functions to return multiple values, mass memory initialize/copy instructions, and reference types, which are opaque pointers to objects outside of the linear memory.[34]
WebAssembly 3.0 was released in September 2025. New features include a 64-bit address space, multiple address spaces, exception handling, and garbage collected struct and array types.[35] Support for garbage collection enables more efficient compilation for high-level languages[36][37], but WasmGC from WebAssembly 3.0 does not have the capabilities required by .NET runtime[38].
While WebAssembly was initially designed to permit near-native code execution speed in the web browser, it has been considered valuable outside of such, in more generalized contexts.[39][40] A WebAssembly runtime environment is a low-levelvirtual stack machine, akin toJVM orFlash VM; it can be embedded into any host application, and thus there have been created standalone WebAssembly runtime environments, includingWasmtime [Wikidata] andWasmer [Wikidata].[9][10] WebAssembly runtime environments are embedded inapplication servers to host "server-side" WebAssembly applications and in other applications to supportplug-in-based software extension architectures, e.g., "WebAssembly for Proxies" (Proxy-Wasm) which specifies a WebAssembly-basedABI for extendingproxy servers.[41][42]
In November 2017, Mozilla declared support "in all major browsers",[43] after WebAssembly was enabled by default in Edge [Legacy] 16.[44] This support also includes mobile web browsers for iOS and Android. As of March 2024,[update] 99% of tracked web browsers support WebAssembly (version 1.0),[45] more than for its predecessorasm.js.[46] For some extensions, from the 2.0 draft standard, support may be lower, but still more than 90% of web browsers may already support, e.g. the reference types extension.[47]
Non-browser WebAssembly runtimes include Wasmer,[10] Wasmtime,[48] WAMR, WAVM, wasm3, and others.[49] These systems execute precompiled Wasm modules and often provide additional APIs for embedding WebAssembly in different environments. Use cases for wasm outside the browser include plug-in interfaces and lightweight virtualization.[50]
WebAssembly implementations generally use eitherahead-of-time (AOT) orjust-in-time (JIT) compilation, though some may also use aninterpreter. While the first implementations appeared in web browsers, there are now many non-browser implementations for general-purpose use.
Because WebAssembly executables are precompiled, a variety of programming languages can target Wasm.[51] Compilation is achieved either through direct output to Wasm or via intermediate virtual machines implemented in Wasm.
Notable toolchains include:
As of 2021, around 40 programming languages support WebAssembly as a compilation target.[53] Examples include:
Web browsers do not permit WebAssembly code to directly manipulate theDocument Object Model. Wasm code must defer toJavaScript for this.[note 2]
In an October 2023 survey of developers, less than half of the 303 participants were satisfied with the state of WebAssembly. A large majority cited the need for improvement in four areas: WASI, debugging support, integration with JavaScript and browser APIs, and build tooling.[73]
For memory-intensive allocations in WebAssembly, there are "grave limitations that make many applications infeasible to bereliably deployed on mobile browsers [..] Currently allocating more than ~300MB of memory is not reliable on Chrome on Android without resorting to Chrome-specific workarounds, nor in Safari on iOS."[74]
All major browsers allow WebAssembly ifContent-Security-Policy is not specified, or if the valueunsafe-eval is used, but behave differently otherwise;[75] Chromerequiresunsafe-eval,[76][77] though a worker thread can be a workaround.[77]
In June 2018, a security researcher presented the possibility of using WebAssembly to circumvent browser mitigations forSpectre andMeltdown security vulnerabilities once support forthreads with shared memory is added. Due to this concern, WebAssembly developers put the feature on hold.[78][79] However, in order to explore these future language extensions, Google Chrome added experimental support for the WebAssembly thread proposal in October 2018.[80]
WebAssembly has been criticized for allowing greater ease of hiding the evidence formalware writers, scammers andphishing attackers; WebAssembly is present on the user's machine only in its compiled form, which "[makes malware] detection difficult".[81] Speed and the easy ability to conceal in WebAssembly have led to its use in hiddencrypto mining within the website visitor's device.[81][82]Coinhive, a now defunct service facilitating cryptocurrency mining in website visitors' browsers, claims their "miner uses WebAssembly and runs with about 65% of the performance of a native Miner."[citation needed] A June 2019 study from theTechnische Universität Braunschweig analyzed the usage of WebAssembly in the Alexa top 1 million websites and found the prevalent use was for malicious crypto mining, and that malware accounted for more than half of the WebAssembly-using websites studied.[83][84] An April 2021 study fromUniversität Stuttgart found that since then crypto mining has been marginalized, falling to below 1% of all WebAssembly modules gathered from a wide range of sources, also including the Alexa top 1 million websites.[85]
As WebAssembly supports only structuredcontrol flow, it is amenable toward security verification techniques includingsymbolic execution.[86]
Early on, the execution speed of a Wasm program was benchmarked to be around 91% of (or about 10% slower than) a comparable native program, not including load/instantiation time; however, various later benchmarks indicate a wide range of performance characteristics, from 33% to 200%[citation needed] of the execution speed of native code, depending on the task.
In 2019, a group of researchers from theUniversity of Massachusetts Amherst presented a comprehensive analysis of WebAssembly's performance compared to native code.[87] This study used theSPEC CPU suite of benchmarks and a system called "Browsix-Wasm" to rununmodified Unix applications in the browser, thereby allowing for a test of real-world applications, finding a significant performance gap between Wasm execution and native execution; in particular, Wasm programs showed an average slowdown of 45% in Firefox and 55% in Chrome across the real-world benchmarks; peak slowdowns resulted in a Wasm program taking 2.08 times as long to run in Firefox and 2.5 times as long to run in Chrome. The paper identified several reasons for this performance difference, including missing optimizations, code generation issues in WebAssembly compilers, and inherent limitations of the WebAssembly platform itself.
A 2021 study suggested that WebAssembly is much faster than JavaScript in certain cases, such as running a complex function on a small file (e.g., processing a graphics file); however, at the time, the JavaScript interpreter had some optimizations available that the WebAssembly implementations did not have (e.g.,Just-in-time compilation).[88]
In 2022, it was determined by researchers that a Wasm program runs at about 120% of (or 20% faster than) the speed of a comparable JavaScript program.[89] Those findings align with the experience of astartup company named "Zaplib", whose founders summarized in a blog that it was shutting down due to lack of performance in WebAssembly.[90] Their goal had been to significantly increase the performance of existingweb apps by incrementally porting them toRust/Wasm; however, porting a customer's simulator from JavaScript yielded only a 5% improvement in performance. Similarly, regardingFigma, they stated the following:
[Upon] closer inspection it seems that [Figma's] use of Wasm is more due to historical accidents—wanting to build inC++ to hedge for their native app—than for critical performance needs. Figma files are processed in C++/Wasm, and this is likely a huge speedup, but most of Figma's performance magic is due to theirWebGL renderer.
In 2023, a study of Wasm's performance for cryptographic tasks indicated that "when using the fastest runtime, WebAssembly was only about 2.32 times slower (median) than native code with architecture-specific optimizations." This result excluded 2 tests where the native code benefited from special instructions implemented directly in the CPU of the target platform; for those particular tests, the Wasm programs were "80 times slower than native code".[91]
Benchmarking has revealed several other pain-points for WebAssembly, such as poor performance because of no direct access to the DOM,[note 3] a problem which is being addressed.[93]
WebAssembly System Interface (WASI) is a simple interface (ABI andAPI) designed byMozilla, which is intended to be portable to any platform.[94] It providesPOSIX-like features like file I/O constrained bycapability-based security.[95][96] There are additional proposed ABI/APIs.[97][98]
WASI is influenced byCloudABI andCapsicum.[99]
Solomon Hykes [fr], a co-founder ofDocker, wrote in 2019, "If WASM+WASI existed in 2008, we wouldn't have needed to create Docker. That's how important it is. WebAssembly on the server is the future of computing."[100]
The general standard provides core specifications for the JavaScript API and details on embedding.[5]
Wasm code (binary code, i.e. bytecode) is intended to be run on aportable virtualstack machine (VM).[101] The VM is designed to be faster to parse and execute than JavaScript and to have compact code representation.[102] Any external functionality (likesyscalls) that may be expected by Wasm binary code is not stipulated by the standard; instead, the standard specifies how the host environment can provide such an interface via a "module".[103][9]
A Wasm program is designed as a separate module containing collections of various Wasm-defined values and program type definitions. These are provided in either binary or textual format (see below) that have a common structure.[104] Such a module may provide a start function that is executed upon instantiation of a wasm binary.
The core standard for the binary format of a Wasm program defines aninstruction set architecture (ISA); each operation that can be executed by the VM is assigned a specificbinary encoding (an "opcode"), but the exact way in which the operation is implemented is not specified, allowing for flexibility in the construction of a VM.[105] The list of instructions includes standard memory load/store instructions, numeric, parametric,control of flowinstruction types and Wasm-specific variable instructions.[106]
The number of opcodes used in the original standard (MVP) was a bit fewer than 200 of the 256 possible opcodes. Subsequent versions of WebAssembly pushed the number of opcodes a bit over 200. TheWebAssembly SIMD proposal (for parallel processing) introduces an alternateopcode prefix (0xfd) for128-bit wideSIMD instructions; the concatenation of the SIMD prefix, plus an opcode that is valid after the SIMD prefix, forms each SIMD opcode. The SIMD opcodes bring an additional 236 instructions to theMVP's SIMD capability (for a total of around 436 instructions)[107][108] This set of instructions[109] are enabled by default across a number of important implementations:[110]
These SIMD opcodes are both portable and intended to map directly to native instruction sets likex86-64 andARM. In contrast, SIMD instructions are not directly supported by Java'sJVM[note 4] or.NET'sCIL; however, both do have some APIs for parallel processing, which provide SIMD speedups. A newer set of "relaxed SIMD" instructions allow for a limited amount of implementation-defined behavior to improve performance.[112][113]
In March 2017, the WebAssembly Community Group reached consensus on the initial ("MVP") binary format, JavaScript API, and reference interpreter.[114] It defines a WebAssembly binary format (.wasm), which is not designed to be used by humans, as well as a human-readable WebAssembly text format (.wat) that resembles a cross between S-expressions and traditional assembly languages. The binary format is straightforward and designed to allow streaming compilation, so compilation can begin before the module is finished downloading, and to allow functions to be compiled in parallel.[30]
The table below shows an example of afactorial function written inC and its corresponding WebAssembly code after compilation, shown both in.wat text format (a human-readable textual representation of WebAssembly) and in.wasm binary format (the rawbytecode, expressed below inhexadecimal), that is executed by a Web browser or run-time environment that supports WebAssembly.
| C source code | WebAssembly.wat text format | WebAssembly.wasm binary format |
|---|---|---|
intfactorial(intn){if(n==0){return1;}else{returnn*factorial(n-1);}} | (func(parami64)(resulti64)local.get0i64.eqzif(resulti64)i64.const1elselocal.get0local.get0i64.const1i64.subcall0i64.mulend) | 00 61 73 6D 01 00 00 0001 06 01 60 01 7E 01 7E03 02 01 000A 17 0115 0020 005004 7E42 010520 0020 0042 017D10 007E0B0B |
All integer constants are encoded using a space-efficient, variable-lengthLEB128 encoding.[115]
The WebAssembly text format is more canonically written in a folded format usingS-expressions. For instructions and expressions, this format is purelysyntactic sugar and has no behavioral differences with the linear format.[116] Throughwasm2wat, the code above decompiles to:
(module(type$t0(func(parami64)(resulti64)))(func$f0(type$t0)(param$p0i64)(resulti64)(if$I0(resulti64);; $I0 is an unused label name(i64.eqz(local.get$p0));; the name $p0 is the same as 0 here(then(i64.const1))(else(i64.mul(local.get$p0)(call$f0;; the name $f0 is the same as 0 here(i64.sub(local.get$p0)(i64.const1))))))))
A module is implicitly generated by the compiler. The function is referenced by an entry of the type table in the binary, hence a type section and thetype emitted by the decompiler.[117] The compiler and decompiler can be accessed online.[118]
Data in memory is stored in a large, growable array of bytes termed alinear memory. Linear memory is separate from the wasm module'scall stack and code and the engine's memory. This allows running wasm code in the same process as the JavaScript virtual machine it's embedded in without violating memory safety.[30]
A module contains a list of global variables separate from linear memory, which may be mutable or immutable. Functions declare a list of local variables. A function's parameters are also local variables.
Instructions operate on astack of values. The stack layout is fixed (branches must push and pop the same number of operands), so wasm engines can compile the stack away, producing efficient machine code operations on registers, rather than managing a stack at runtime. A stack machine architecture was chosen as it can have higher code density than aregister machine.[30]
Unlike typical assembly languages, wasm only usesstructured control flow similar to high-level programming languages. The intentional lack of support forjump instructions makes it simple to validate and compile wasm code in asingle pass, and makes it easier to read code disassembled into the text format.
Blocks are delimited with theblock,loop, andif/else constructs. Branch instructions contain a label that identifies the depth of a block it is nested in:br 0 will branch to the block the instruction is contained in,br 1 will branch to the block that block is contained in, and so on. Branches to ablock orif jump to the end of the block like a break statement, while branches to aloop jump back to thebeginning of the loop, similar to a continue statement. Thebr_table instruction takes an input index and jumps to a target from a list of labels. A C-style switch statement can be expressed with abr_table inside a series of nested blocks.[30][119]
Compilers targeting wasm convert unstructured control flow into high-level loops using Emscripten's Relooper algorithm, originally designed to target JavaScript.[120]
Function declarations are listed in a separate section before the section containing the function bodies.Function pointers are emulated using an index into the global functions table, since the wasm code cannot access the linear memory. Functions can also be imported, providing theforeign function interface to communicate with JavaScript or other embedders.[30]
WebAssembly code can be considered astructured stack machine; a machine where most computations use a stack of values, but control flow is expressed in structured constructs such as blocks, ifs, and loops. In practice, implementations need not maintain an actual value stack, nor actual data structures for control; they need only behave as if they did so.
WebAssembly is an open standard...
WebAssembly is a ... code format
WebAssembly is a programming language that has multiple concrete representations (its binary format and the text format). Both map to a common structure.
... this specification is complemented by additional documents defining interfaces to specific embedding environments such as the Web. These will each define a WebAssembly application programming interface (API) suitable for a given environment.
Its main goal is to enable high performance applications on the Web, but it does not make any Web-specific assumptions or provide Web-specific features, so it can be employed in other environments as well.
While the Web is the primary motivation for WebAssembly, nothing in its design depends on the Web or a JavaScript environment. It is an open standard specifically designed for embedding in multiple contexts, and we expect that stand-alone implementations will become available in the future.
Compile everything to WebAssembly. Run it on any OS or embed it into other languages.
WASM-GC does not have capabilities required by .NET runtime. It is not possible to replace .NET runtime's GC with WASM-GC.
{{cite web}}: CS1 maint: url-status (link)The goal of this crate is to provide Rust bindings to the Web APIs and to allow a high degree of interoperability between Rust and JavaScript.
Raw API bindings for Web APIs. This is a procedurally generated crate from browser WebIDL which provides a binding to all APIs that browser provide on the web.
This article incorporates text from afree content work. Licensed under Apache License 2.0 (license statement/permission). Text taken fromText Format, jfbastien; rossberg-chromium; kripken; titzer; s3ththompson; sunfishcode; lukewagner; flagxor; enricobacis; c3d; binji; andrewosh, GitHub. WebAssembly/design.