📜 ⬆️ ⬇️

WebAssembly: the beginning of a new era

The web has a great future.
Yesterday Brendan Ike “blew up” the web development community : the web will get a new low-level binary compiled format that will work much better than JavaScript.
Google, Microsoft, Mozilla, and several independent experts are working on a new project in the W3C WebAssembly Community Group , and what they are working on is not a small thing.

WebAssembly is:


What does this mean for javascript?

Before answering this question, let's rewind time ago. Back in time to React, Angular, Backbone and jQuery.

Here we are on the spot.
A network is a collection of hypertext documents displayed using message delivery systems, which, however, are not yet interconnected. The first web server was launched on the NeXT computer (see below) at CERN ...

image
')
In the yard in 1991. I have not had time to turn gray. I'm trying to crack a ten-thousand text quest game (I didn't count them all).

I chose a kind of programming language for this purpose. At that time, I already had time to get bored and BASIC, and Pascal. I wanted to use C, but I could not: I was still saving for the boxed version of Borland Turbo C ++ (they literally come in packed boxes with instructions and an installation disk). Then I did not have even the simplest Turbo Assembler.

I wrote in assembly language and “compiled” into an executable program using the command line in DOS and “debug”. If this sounds crazy, trust me, it was so. I bet that even those of you who are lucky enough to work with DOS do not know that you can use “debug” to debug assembler and disassemblerise (reverse engineering) the existing code.

Sounds cool? Not. I hated it. I could not wait for the moment when I could sit down at the new Borland Turbo C ++ and program it humanly. In general, I was given it. Hooray!

I liked Borland Turbo C ++ because Borland Turbo Assembler was preinstalled on it. What?! Why program in assembly language when you have a wonderful high-level, object-oriented C ++ tool?

Sometimes you want to work with bare metal, or close to it, without melting the brain. By the way, did I mention that I wrote a lot of machine code before using C ++?
I've gone crazy.
To achieve real results is not so easy when writing code in assembly language. Then why do we need WebAssembly?

We need WebAssembly because, with all the flexibility of Javascript, it is still difficult to imagine many things that we would like to see in it, and the peculiarities of the language that we need to add there will only complicate the language, which already confuses users .

WebAssembly allows us to work with simple, low-level blocks of code that can be used to program anything.

How is it different from javascript? The key word is “low level”. It defines primitives, including a number of types and operations on these types, literals for them, control-flow, calls, heaps, etc.

These are very simple primitives. Nothing complicated. There is no complex object system (prototype or some other). There is no built-in, automatic “garbage collection” following you and periodically stopping you when she needs to collect garbage.

In fact, it will be useful not to think of WebAssembly as a language at all. It's more like a car, a bit like a Turing machine ...

What is WebAssembly?

WebAssembly defines an abstract syntax tree (AST), which is stored in binary format . Binary is great because it allows you to create smaller applications . Surely you are wondering how you can debug a binary code.

Fortunately, the development of a debugger that will work in browsers is already underway, and the abstract syntax tree will be presented in a (relatively) easily readable text format . I would like to show examples, but so far there are not so many of them. Perhaps the code on WebAssembly will still be harder to read than similar code written by hands in JavaScript, but at least it will be as easy to read as ASM.js. Maybe even easier. We'll see.

What will WebAssembly be used for?

Among other things, it can be used for simple work with threads and SIMD (single instruction, multiple data) - in other words, with one command stream and several data streams. You can queue up multiple data blocks, and then register one command to work with them simultaneously.

This means that parallel processing of streaming video will be processed by the processor. If you keep your finger on the pulse, you have heard about this solution in JS, but I have always found it inconvenient to solve low-level things using JavaScript.

In such cases, you probably should forget about the object system, “garbage collectors” and dynamic query processing. Just put the data streams in the queue and process them as quickly and efficiently as possible.

What about applications?

At the moment, applications like Ableton Live (writing music) and Adobe Premiere Pro (creating video) are not very suitable for porting to the Web. I note that this is possible, but still difficult. There are many more problems to solve. For example, you need to decide how best to synchronize data for real-time applications.

In addition, it will be possible to develop applications that will transfer large amounts of data through networks of processing functions. For example, such as the application of a guitar pedal . When people think about such things, they usually don’t think about JavaScript. Many people probably think that this is pure madness to even try.

But, in any case, JavaScript is a really great language to build most of the application that you can only dream about.
WebAssembly organically fills in the gaps that exist in the JavaScript functionality.
There are gaps in JavaScript functionality, and this is not a secret. Even his most devoted fans will hardly argue with the statement that sometimes the tongue tries to “swallow” too much, losing flexibility and effectiveness. Just yesterday, I thought that these spaces could be eliminated by simply adding more functionality to the JavaScript itself. Brendan Ike suggested the same way at the Fluent conference. I applauded.

However, all this time we missed one moment: everyone wants to program in high-level language, but at the same time be able to “sink” to assembly language, when it is necessary to increase speed.
WebAssembly can increase the speed of javascript at times!
I’m sure that dozens, if not hundreds, of articles about WebAssembly will appear across the web today. I am more than sure that most of them will be devoted not to what I wrote earlier, but to a completely different feature of this programming language. From her, by the way, I am also delighted.

WebAssembly allows you to use more languages ​​in web development

Of course, in fact, we do not need WebAssembly to use other programming languages ​​in web development. For example, we already have excellent AAA game engines that work fine with compilation from JavaScript.
If you hear that JavaScript is slow ... It is not.
WebAssembly adds things that most JS developers don't want to see in JavaScript. The functionality itself is needed, but there’s definitely no place in JavaScript. Especially since we can get all these functions using compilation from other programming languages.

In fact, WebAssembly provides us with an alternative compiler - created specifically for this purpose.

Now, it will be much easier for us to port code that depends heavily on, for example, shared memory chains. I am sure that writing a compiler for WebAssembly will be easier than writing a compiler for JavaScript, and all because the first one guarantees the best transfer of language functions to a given abstract syntax tree.

The fact that all the old programming languages ​​can now be used on the Web without any problems is good, but the main thing is not the case. The fact is that:
WebAssembly is a great reason for developers to start working on new programming languages.
The network has a great future. So it is better to go out of the shadows and get down to business before it’s too late.

Add-ons + FAQ

Q: What is wasm?
A: Abbreviation for W eb As se m bly.

Q: Why not use JVM?
A: Attempts to add JVM to browsers using plugins were more than once. Unfortunately, nothing good came of it. JavaScript has a built-in virtual machine, so adding another will result in a second set of API connections to give the virtual machine access to DOM, networks, sensors, input devices, and so on. For this you have to sacrifice something. For example, how will the processes in a virtual machine distribute the available resources among themselves? To answer this question is more difficult than it seems.

At first, WebAssembly will work as an ASM.js polyfill, that is, it will be able to use a JavaScript virtual machine. The language design developed on the basis of this, and that is why WebAssembly will have more smooth integration with browsers than alternative virtual machines can offer.

Q: Does the appearance of WebAssembly mean that in the future there will be many new programming languages? Will this lead to fragmentation?
JavaScript is completely safe. Its ecosystem will flourish for many more years. WebAssembly is more about performance, diversity, and movement, not fragmentation.
A: JavaScript has always had very serious competition on the server side, as well as in programming for embedded systems, such as small computers and robots. Despite the presence of a large number of fairly good alternatives with developed ecosystems and professional development teams, Node continues to rapidly increase its presence on servers of startups and commercial enterprises.

In addition, JavaScript has a good support in the face of a community of developers, as well as a powerful ecosystem. I like to add to the graphs of the popularity of various modules. You look at these curves and you are amazed.
image
Pay attention to the green curve. This is npm, the standard JavaScript repository that comes bundled with Node.

JavaScript is increasingly used in game development, in programming robots and IoT devices. Although there is significant competition in these areas from C, C ++ and Java, this does not affect the position of JavaScript as the main web programming language. All developers have a choice, and they use JavaScript for a reason, but because they like it.

JavaScript will survive. “There are only two kinds of programming languages: those that people constantly complain about, and those that nobody uses.” Bjarne Stroustrup

Useful materials

W3C WebAssembly Community Group
Mailing list
IRC: irc: //irc.w3.org: 6667 / # webassembly
Github
Who's involved?

about the author

Eric Elliott is the author of the books “Programming JavaScript Applications” and “Learn JavaScript Universal Development with Node, ES6, & React” . He participated in the development of software products for Adobe Systems, Zumba Fitness, The Wall Street Journal, ESPN, BBC, as well as for artists such as Usher, Frank Ocean, Metallica and many others. Eric spends most of his time in Silicon Valley with the most beautiful woman in the world.

Worked on the translation: greebn9k (Sergey Gribnyak), seninrom (Roman Senin), silmarilion (Andrey Khakharev)
Singree

Source: https://habr.com/ru/post/261205/


All Articles