background
WebAssembly, or Wasm for short, originated from front-end technology.
Even with the support of JIT, the performance of js is still not ideal in scenarios with a large amount of calculation. After the trial of asm.js, it was finally finalized with Wasm, and it was supported by the four major browsers.
The original Wasm was mainly used in WEB applications. With the birth of WASI, it was expanded to wider scenarios, such as server-side technology.
What is Wasm
Wasm is not a language in the conventional sense, but just a binary instruction standard based on a stack virtual machine.
For example, Lua is a language because of its programmability, while Lua bytecode has almost no programmability (it is not impossible to write by hand).
Wasm is similar to the position of Lua bytecode, except that it is more low-level and has a wider range of applications.
Wasm is designed to be a compilation target for other languages, and currently supports C/Rust and so on.
How Wasm works
Since Wasm is only a standard, the specific execution is done by a virtual machine, and there are many implementations of the virtual machine, similar to the official Lua and LuaJIT.
There are also many specific operating modes: interpreter, JIT, AOT. What's more interesting is that in the Wasm circle, it seems that AOT technology is more popular than other languages.
The specific implementation details of the virtual machine will be introduced later.
Features of Wasm
Wasm has an excellent design concept and has its obvious advantages, but sometimes there is a price to pay for the advantages.
high performance
This is one of Wasm's original design intentions. It has close to native performance. Of course, it also depends on the specific implementation of the virtual machine.
In terms of instruction design, Wasm is sufficiently low-level and simple, so theoretically it can be close to native performance.
Memory safety
Wasm is designed to be memory safe, especially in the WEB scene. In many cases, the executed code does not know from whom, the underlying security is very important.
Specifically, Wasm's memory model is very simple, there is only one linear memory, and the reading and writing of the memory that Wasm can operate occurs within this memory range.
Wasm will not have pointers flying around. Some are only offset
. The purpose is that when malicious Wasm is executed, it is impossible to read and write arbitrary data memory in the process.
Of course, the price is also there. There will be some discounts for flexibility. In many cases, one more memory copy is required.
sandbox
Wasm runs in a sandbox environment, and its capabilities are limited. Some external calls required are provided to it by an external host.
For example, when Wasm needs to read files, it also needs to run the host environment of Wasm and provide it with the corresponding API.
Cross language
Cross-language is one of the highlights of Wasm. In my opinion, it can reduce language disputes to some extent (language disputes are actually quite interesting topics, and you can have a chat when you have time).
As an intermediate standard product, Wasm can connect developers at both ends:
- Upper application developer
- Low-level service developers
The underlying service developers only need to provide a sandbox environment for running Wasm, including a virtual machine running Wasm, and expose certain capabilities of the service in the sandbox.
Upper-level application developers can choose their favorite language and the Wasm-SDK of the corresponding language (corresponding to the basic service capabilities exposed in the sandbox) to generate Wasm.
In theory, it is a good plan.
At last
Wasm is also an embedded solution, similar to Lua to some extent.
In my opinion, Wasm still has a long way to go in his teenage years.
- The underlying capabilities need to be enhanced, such as languages with GC, generating Wasm is a difficult problem.
- The surrounding ecology also needs to be developed. It is still in its infancy, although some prototypes of design can be seen.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。