2019-05-01_Linux_Format

(singke) #1

9992May 01e102yxpli0xlnshow May 2019 LXF249 95


Speed up web apps coding academy


AboutWebASSembly


Must specify at least one of --node, --chrome,
--firefox, or --safari
When you define one of the allowed command line
options, the command downloads the required
packages and begin testing. The only requirement for
the test to run is that you should have the desired web
browser already installed.
Again, talking more about testing is beyond the
scope of this tutorial, but you can find plenty more
resources online.


Go WebAssembly!
In this final section we’ll talk about the way the Go
programming language generates WebAssembly code,
in case you want to compare Rust with Go in relation to
WebAssembly generation. This section will not talk
about publishing the generated WebAssembly code –
just the Go way of generating WebAssembly code.
The good thing is that as far as Go is concerned,
WebAssembly is just another supported architecture.
Therefore you can use the cross-compilation
capabilities of Go in order to create your WebAssembly
binary files. Additionally, you will not need to install any
extra packages for WebAssembly support in Go.
So if you have a Go source file called hw.go, you can
compile it and generate a WebAssembly file as easily as
the following:
$ GOOS=js GOARCH=wasm go build -o main.wasm hw.
go
The hw.go file contains ordinary Go code without
any indication that it is going to be compiled into
WebAssembly. It is the values of GOOS and GOARCH
that specify and tell Go to create WebAssembly code. If
you do not specify the right values, the compilation will
not generate WebAssembly code or might fail
completely. There are various ways to see the output of
main.wasm, including using your favourite web
browser. However, if you have Node.js already installed
on your Linux machine, there is a much easier way to
see the output, which will save you from loading the file
in your web browser:
$ export PATH=”$PATH:$(go env GOROOT)/misc/
wasm”
$ GOOS=js GOARCH=wasm go run.
Hello World!


The entire process is also illustrated in Figure 6
below, which also includes the Go code of hw.go.
However, choosing between Go and Rust for creating
WebAssembly code is a matter of personal preference.
It looks like WebAssembly is here to stay, so it would
be a good idea to learn more about it and how you can
generate WebAssembly code from Rust.
You should keep four things in your mind after
reading this tutorial. First is that WebAssembly is not a
programming language, second is that you can generate
WebAssembly code from many existing programming
languages. The third one is that after creating the
WebAssembly code you no longer need the compiler of
the programming language that you used; and the last
one is that some JavaScript code is still required. Enjoy
WebAssembly and happy coding!

WebAssembly (aka Wasm) is a machine-model and executable
format targeted for a virtual machine, designed for efficiency – both
in speed and file size. That means that you can use a WebAssembly
binary on any platform you want without a single change. It also
means that WebAssembly binaries tend to be quite large in size,
though this shouldn’t be a problem with modern computers.
WebAssembly comes in two formats: a plain-text format and a
binary format. Plain-text format WebAssembly files have the WAT
extension, whereas binary files have the WASM file extension. A WAT
file uses S-expressions, whereas the binary format can be executed
by Wasm virtual machines.
An S-expression is an older textual format used for representing
trees. In the case of WebAssembly, an S-expression is used for
describing modules, which are the fundamental code units in
WebAssembly. Note that if you have a WASM binary file, you’ll have
to load and use it using the JavaScript API.
Apart from Rust, WebAssembly can also be generated by other
programming languages that have support for static typing, including
Go, C and C++. Functions in WebAssembly should be included in
modules – in our case, this is being taken care of by Rust, and most
other languages provide the same support.

ImpRoVe youRCoDeSkIllS Subscribe now at http://bit.ly/LinuxFormat


Figure 5: This figure illustrates the use of the Cargo tool by showing
its default help screen.

Figure 6: This is the Go way of creating WebAssembly code – and illustrates a trick that can help
you execute your WebAssembly binary without the need for a webserver.

youcanlearn
moreabout
WebAssembly
byvisiting
WebAssembly:
https://
webassembly.
organdhttps://
developer.
mozilla.org/
en-uS/docs/
WebAssembly.
Free download pdf