Skip to content

Commit c849de3

Browse files
committed
More documentation about how to use WebAssembly within a web page
* Also add blog link
1 parent f696d28 commit c849de3

File tree

5 files changed

+80
-13
lines changed

5 files changed

+80
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.17)
22

33
project(
44
tolc
5-
VERSION 0.4.0
5+
VERSION 0.4.1
66
LANGUAGES CXX)
77

88
configure_file(docs/ReleaseNotes/version.in

cmake/packaging/tolc/tolcCreateTranslation.cmake

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,25 @@ function(tolc_create_bindings)
6464
OUTPUT
6565
${ARG_OUTPUT})
6666

67-
if(${ARG_LANGUAGE} MATCHES "python")
67+
if(ARG_LANGUAGE STREQUAL "python")
6868
# NOTE: Variable injected from tolcConfig file
6969
get_pybind11(VERSION ${tolc_pybind11_version})
7070
# Create the python module
7171
pybind11_add_module(${tolc_target_name}
7272
${ARG_OUTPUT}/${ARG_TARGET}_python.cpp)
73-
elseif(${ARG_LANGUAGE} MATCHES "wasm")
73+
elseif(ARG_LANGUAGE STREQUAL "wasm")
7474
# Assumes that the Emscripten toolchain file is used
7575
# Will result in a .js and a .wasm file
7676
add_executable(${tolc_target_name} ${ARG_OUTPUT}/${ARG_TARGET}_wasm.cpp)
7777

7878
# Export Promise as 'loadMyLib' for module 'MyLib'
7979
# -s MODULARIZE=1 sets it as a promise based load
8080
# Note that this is necessary for preJS to work properly
81-
set_target_properties(
82-
${tolc_target_name}
83-
PROPERTIES
81+
set_property(
82+
TARGET ${tolc_target_name}
83+
PROPERTY
8484
LINK_FLAGS
85-
"-s MODULARIZE=1 -s EXPORT_NAME=\"load${ARG_TARGET}\" --pre-js ${ARG_OUTPUT}/pre.js -lembind"
85+
"-s MODULARIZE=1 -s EXPORT_NAME='load${ARG_TARGET}' --pre-js ${ARG_OUTPUT}/pre.js -lembind "
8686
)
8787
else()
8888
error_with_usage(

docs/ReleaseNotes/v0.4.1.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# News #
2+
3+
## Documentation ##
4+
5+
* Add examples on how to use the `WebAssembly` from a web page.
6+
7+
## Minor ##
8+
9+
* Make it easier to add linker flags to `WebAssembly` targets
10+
* More info in the documentation: [https://docs.tolc.io/webassembly/introduction/](https://docs.tolc.io/webassembly/introduction/)

docs/packaging/docs/webassembly/introduction.md

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# WebAssembly with Tolc #
22

33
In order for `C++` to be called from `javascript` there has to be an interface level. `tolc` generates this level from your already written `C++` interface.
4-
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).
4+
To be as close to what an engineer would have written, `tolc` generates human readable [`embind`](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

77
## Using a `C++` library from `javascript` ##
@@ -29,17 +29,18 @@ FetchContent_Declare(
2929
)
3030
FetchContent_Populate(tolc_entry)
3131
32+
set(tolc_DIR ${tolc_entry_SOURCE_DIR}/lib/cmake/tolc)
3233
find_package(
3334
tolc
3435
CONFIG
35-
PATHS
36-
${tolc_entry_SOURCE_DIR}
37-
REQUIRED)
36+
REQUIRED
37+
)
3838
3939
tolc_create_bindings(
4040
TARGET MyLib
4141
LANGUAGE wasm
42-
OUTPUT wasm-bindings)
42+
OUTPUT wasm-bindings
43+
)
4344
```
4445

4546
Assuming your library is called `MyLib`, and the bindings should be generated to the directory `wasm-bindings`.
@@ -80,7 +81,8 @@ $ emsdk.bat activate 3.1.3
8081

8182
### Configuring Your Project ###
8283

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.
84+
Since `CMake` doesn't have native support for `WebAssembly` we have to provide a `toolchain` file, fortunately for us, `Emscripten` provides us with one.
85+
When configuring your `CMake` project, just 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.
8486

8587
Example:
8688

@@ -89,6 +91,7 @@ Example:
8991
$ cmake -S. -Bbuild -DCMAKE_TOOLCHAIN_FILE=${EMSDK_DIRECTORY}/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake
9092
```
9193

94+
9295
### Using From `javascript` ###
9396

9497
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:
@@ -103,5 +106,58 @@ loadMyLib().then(MyLib => {
103106
});
104107
```
105108

109+
Running the file as normal:
110+
111+
```shell
112+
$ node run.js
113+
```
114+
115+
116+
### Using from a web page ###
117+
118+
By default `Emscripten` assumes that you're running your code in a `node` environment (e.g. having access to the filesystem).
119+
This is not the case on a web page served to a browser. If we add the link flag `-s ENVIRONMENT='web'` to `Emscripten` it will produce a serveable `WebAssembly` module.
120+
Since `Tolc` exposes a `CMake` build target for the module, all we have to do is add the flag ourself:
121+
122+
```cmake
123+
# Creates the CMake target ${TARGET}_${LANGUAGE}
124+
# In this case: MyLib_wasm
125+
tolc_create_bindings(
126+
TARGET MyLib
127+
LANGUAGE wasm
128+
OUTPUT wasm-bindings
129+
)
130+
131+
# Want to deploy to a web page
132+
set_property(
133+
TARGET MyLib_wasm
134+
APPEND_STRING
135+
PROPERTY LINK_FLAGS "-s ENVIRONMENT='web'")
136+
```
137+
138+
Then we copy over `MyLib.js` and `MyLib.wasm` to our web application and load them as shown previously:
139+
140+
```javascript
141+
// app.js
142+
const loadMyLib = require('./MyLib');
143+
144+
loadMyLib().then(MyLib => {
145+
// From here you can use the C++ functions of your library as usual
146+
MyLib.myCppFunction();
147+
});
148+
```
149+
150+
Assuming you've loaded the `javascript` within your page:
151+
152+
```html
153+
<!-- index.html -->
154+
...
155+
<head>
156+
<script type="text/javascript" src="./app.js"></script>
157+
</head>
158+
...
159+
```
160+
161+
106162
If you want to see what more is supported you can take a look at [the Examples section](./examples.md).
107163

docs/packaging/tolc_theme/base.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<div class="navbar-end">
5454
<a class="navbar-item is-size-5" href="https://tolc.io">Getting Started</a>
5555
<a id="active-item" class="navbar-item is-size-5" href="https://docs.tolc.io/">Docs</a>
56+
<a class="navbar-item is-size-5" href="https://tolc.io/blog">Blog</a>
5657
<a class="navbar-item is-size-5" href="https://tolc.io/live">Live Demo</a>
5758
<a class="navbar-item is-size-5" href="https://tolc.io/products">Products</a>
5859
<a class="navbar-item is-size-5" href="https://tolc.io/account">Account</a>

0 commit comments

Comments
 (0)