Use WASI and WebAssembly to Make Applications Portable

In this Shortcut, we’ll look a little further behind the curtain and gain more insight into where all of this WebAssembly business is going. I will introduce you to a part of the platform called the WebAssembly System Interface (WASI). WASI is a crucial part of the vision to produce safe, fast, and portable code, but it does require some nuance in how you think about software, and this may differ from your current thinking. Whereas you have seen how WebAssembly can make code portable, applications usually rely on the availability of specific reusable APIs, which is where platform lock-in can start to happen. If the behavior your application needs does not exist on a runtime platform, your application will not work. This is the motivation behind efforts like the Wine project, which brings Windows applications to Linux, macOS, and other Unices.

In the GitHub repository for this Shortcut, you should enter the directory 17-Wasi. This contains copy.cpp, the same file we used in the “Use Emscripten and WebAssembly to Do File I/O in the Browser” Shortcut. It is reused here for convenience and clarity:

#include <fstream> #include <iostream> using namespace std; int main() { string line; ifstream infile("copy.cpp"); ofstream outfile("copy.txt"); if( infile && outfile ) { while (getline(infile, line)) { outfile << line << "\n"; } cout << "Done\n"; } else { printf("File is ...

Get Use WASI and WebAssembly to Make Applications Portable now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.