Skip to content

Commit d5d2b02

Browse files
committed
Added a quick guide to the introduction section
* Also updated to use tolc_create_bindings instead of tolc_create_translation
1 parent 1c28062 commit d5d2b02

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.15)
22
project(
33
Frontend.wasm
4-
VERSION 0.4.0
4+
VERSION 0.4.1
55
LANGUAGES CXX)
66

77
configure_file(docs/ReleaseNotes/version.in

docs/ReleaseNotes/v0.4.1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# News #
2+
3+
## Documentation ##
4+
5+
* Added a quick guide to using C++ from javascript via WebAssembly
6+

docs/public/introduction.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ In order for `C++` to be called from `javascript` there has to be an interface l
44
To be as close to what an engineer would have written, `tolc` generates human readable [`embind11`](https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#embind).
55
This is then compiled to a `.wasm` and a `.js` file that `javascript` can import.
66

7-
## Prerequisites ##
7+
## Using a `C++` library from `javascript` ##
8+
9+
This is a quick guide to using a `C++` library (here called `MyLib`) from `javascript`. We will:
10+
11+
1. Download and use `Tolc`
12+
2. Download and set up `Emscripten`
13+
3. Use the resulting `WebAssembly` from `javascript`
814

915
The following works on all supported platforms. On all platforms you need `git` available in your `path`. Commands that should be run from a terminal starts with `$ `, while comments starts with `# `.
1016

@@ -13,6 +19,7 @@ The following works on all supported platforms. On all platforms you need `git`
1319
Just add the following in a `CMakeLists.txt` below where the library you intend to use from `javascript` is defined:
1420

1521
```cmake
22+
# Download Tolc
1623
# Can be ["latest", "v0.2.0", ...]
1724
set(tolc_version latest)
1825
include(FetchContent)
@@ -29,17 +36,17 @@ find_package(
2936
${tolc_entry_SOURCE_DIR}
3037
REQUIRED)
3138
32-
tolc_create_translation(
39+
tolc_create_bindings(
3340
TARGET MyLib
3441
LANGUAGE wasm
35-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/wasm-bindings)
42+
OUTPUT wasm-bindings)
3643
```
3744

38-
Assuming your library is called `MyLib`, and the bindings should be generated to `${CMAKE_CURRENT_BINARY_DIR}/wasm-bindings`.
45+
Assuming your library is called `MyLib`, and the bindings should be generated to the directory `wasm-bindings`.
3946

4047
### Downloading `Emscripten` ###
4148

42-
In order to compile your library to `WebAssembly`, you need to download the [`Emscripten compiler`](https://emscripten.org/). This is typically done via their `Emscripten SDK`. Navigate to the directory where you want to install and run the following commands:
49+
In order to compile your library to `WebAssembly`, you need to download the [`Emscripten compiler`](https://emscripten.org/). This is typically done via the `Emscripten SDK`. Navigate to the directory where you want to install and run the following commands:
4350

4451
```shell
4552
# Download SDK
@@ -71,9 +78,9 @@ $ emsdk.bat install 3.1.3
7178
$ emsdk.bat activate 3.1.3
7279
```
7380

74-
---
81+
### Configuring Your Project ###
7582

76-
Now when configuring your `CMake` project, pass the toolchain flag `-DCMAKE_TOOLCHAIN_FILE=${EMSDK_DIRECTORY}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake`. Where you need to replace `${EMSDK_DIRECTORY}` with the directory of the previously downloaded `Emscripten SDK`.
83+
Now when configuring your `CMake` project, pass the toolchain flag `-DCMAKE_TOOLCHAIN_FILE=${EMSDK_DIRECTORY}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake`. Where you need to replace `${EMSDK_DIRECTORY}` with the directory of the previously downloaded `Emscripten SDK`. Note that the directory separator used by `CMake` is always forward slash (`/`), even on Windows.
7784

7885
Example:
7986

@@ -82,3 +89,19 @@ Example:
8289
$ cmake -S. -Bbuild -DCMAKE_TOOLCHAIN_FILE=${EMSDK_DIRECTORY}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
8390
```
8491

92+
### Using From `javascript` ###
93+
94+
Looking into `build/tolc` you should see `MyLib.js` aswell as `MyLib.wasm`. `MyLib.js` exports a `Promise` that loads the built `WebAssembly`. Here is an example usage:
95+
96+
```javascript
97+
// run.js
98+
const loadMyLib = require('./build/MyLib');
99+
100+
loadMyLib().then(MyLib => {
101+
// From here you can use the C++ functions of your library as usual
102+
MyLib.myCppFunction();
103+
});
104+
```
105+
106+
If you want to see what more is supported you can take a look at [the Examples section](./examples.md).
107+

0 commit comments

Comments
 (0)