In this part, we're going to construct a simple web application using our knowledge of JavaScript and wasm modules. We start from the C++ side, creating a simple module that exports a function triple
, which triples an input number. This function will then be compiled to a wasm
module and used in our web app. Here is the C++ code:
TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4extern "C" {
5 int triple(int x) {
6 return x * 3;
7 }
8}
9
10int main() {
11 return 0;
12}
We export the triple
function by enclosing it within extern "C"
which prevents C++ name mangling and makes it recognizable in the wasm
module as is. With this module, we can now offer functionality to the users of our web app that efficiently multiplies a number by three, which provides a foundational step in integrating more complex functionality in future applications.
xxxxxxxxxx
12
using namespace std;
extern "C" {
int triple(int x) {
return x * 3;
}
}
int main() {
return 0;
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment