Binary format for the web
Looping
Normally, graphical C++ applications run in an infinite loop. They react to events and process them with each loop iteration. At the same time, rendering occurs, and a wait is built in to keep the frame rate (frames per second, or fps) constant.
This behavior is less suitable for browsers, because the continuous loop makes it impossible to return control to the browser. The Emscripten SDK therefore provides a function for C++ code to prepare the main()
loop for use in JavaScript. This function guarantees that the website is not caught in an infinite loop. The Emscripten API reference [18] reveals more, especially the passage on the emscripten_set_main_loop()
function.
Matching Filesystems
Native programming languages often use direct, synchronous access to the filesystem. Browsers, however, do not access the filesystem directly, and JavaScript usually runs file operations asynchronously. Emscripten provides a virtual filesystem that allows the browser to run native applications without major changes. Applications that use synchronous file access, access the virtual filesystem via an API (Figure 4).
By default, Emscripten relies on MemFs [20], a filesystem that resides in memory, that Emscripten automatically mounts on the root directory (/
) when necessary (when the code requests file operations). MemFs users use emcc
to add files and directories to the filesystem that are then available later when the program runs in the browser.
A website with access to MemFs loads the files via JavaScript; it does not allow synchronous file access until the entire filesystem is available in memory. Because MemFs only exists in memory, writes are lost on refreshing the website page.
Listing 2 shows a simple C program that reads a passwd
file from the current directory and outputs the first entry of each line. MemFs needs to provide the necessary text file at build time; emcc
provides two options, preload-file
and embed-file
.
Listing 2
vfs.c
Whereas preload-file
instructs JavaScript to load the transferred files when calling the website, embed-file
integrates the files directly into the JavaScript file produced. If the developer now uses
emcc vfs.c -s WASM=1 -o vfs.html --preload-file passwd
to call emcc
, the compiler generates a file named vfs.data
, in addition to the WASM, JavaScript, and HTML files containing the transferred file. The program then accesses these at run time.
To save data, Emscripten provides two MemFs alternatives, depending on the application: node-fs [21] and IDBFS [22]. The node-fs library is only used if the application runs within a Node.js environment. Thanks to node-fs, applications access the entire local filesystem via the Node.js API. If you need the browser application to write persistent files, IDBFS provides a synchronized IndexedDB instance.
Additional restrictions for portability are described in the Emscripten docs [23]; they mainly relate to multithreading applications and programs that involve low-level hardware features and native assembler instructions.
Chain of Tools
Emscripten provides a generic toolchain that also creates asm.js code, and WebAssembly has its own tools for reading and editing WASM files directly. The two sets of tools are the WebAssembly Binary Toolkit (WABT, "wabbit") [24] and Binaryen [25].
WABT provides tools that help translate between the WebAssembly's binary and text formats, an objdump
version for WASM files, and a WASM interpreter. In contrast, Binaryen primarily offers a library to facilitate the compilation of code in the WebAssembly format within other toolchains. It also provides some additional tools to convert between the WebAssembly binary and text formats and between asm.js and WASM, as well as a WASM interpreter. Last but not least, Binaryen has a JavaScript library of tools.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.