Skip to content

Commit

Permalink
zz
Browse files Browse the repository at this point in the history
  • Loading branch information
3dgen committed May 9, 2020
1 parent 622c794 commit c554374
Show file tree
Hide file tree
Showing 391 changed files with 164,538 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .bookignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# only for gitbook

/docs
/examples
25 changes: 25 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# http://editorconfig.org/

root = true

# Unix-style newlines with a newline ending every file
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*]
indent_style = tab
indent_size = 4
tab_width = 4

[*.{go,ts,js,c,cc,cpp,h,py,proto}]
charset = utf-8
indent_style = tab
tab_width = 4

# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.go linguist-language=WebAssembly
49 changes: 49 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Node rules:
## Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

## Dependency directory
## Commenting this out is preferred by some people, see
## https://docs.npmjs.com/misc/faq#should-i-check-my-node_modules-folder-into-git
node_modules

# Book build output
_book

# eBook build output
*.epub
*.mobi
#*.pdf

*.o
*.obj
*.exe

# macOS
.DS_Store

*.a
*.lib
*.so
*.dll
*.obj
*.o
a.out

.oracle_jre_usage

# wasm
*.wasm
*.wasm2wat.txt

*.map

book.pdf
book_*.pdf

# emcc
*.out.js

# go
go.mod
go.sum
101 changes: 101 additions & 0 deletions en/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# WebAssembly friendly programming with C/C++

This book introduces the use of C/C++ for WebAssembly development. For the basics of WebAssembly, please refer to the "WebAssembly Primer"(Simplified Chinese), the sale address:

- EPUB:[https://www.epubit.com/book/detail/40619](https://www.epubit.com/book/detail/40619)
- JD:[https://item.jd.com/12499372.html](https://item.jd.com/12499372.html)

----

![](cover.png)

- Author: Ending,Github [@3dgen](https://github.com/3dgen)
- Author: ChaiShushan,Github [@chai2010](https://github.com/chai2010),Twitter [@chaishushan](https://twitter.com/chaishushan)
- Translator: Ending,Github [@3dgen](https://github.com/3dgen)
- Translator: yushih, Github [@yushih](https://github.com/yushih)
- Project: https://github.com/3dgen/cppwasm-book

## Preface

> *Ending's law: "Any application that can be compiled to WebAssembly, will be compiled to WebAssembly eventually."*
WebAssembly is the newest virtual machine standard for the web. C/C++ source code can be compiled into the WebAssembly binary format(.wasm) through the Emscripten toolchain, and then imported into web pages for JavaScript calls - this means that programs written in C/C++ can run in the web page.

This book introduces how to develope WebAssembly modules by C/C++ from the basic use of Emscripten; some general design principles and technical frameworks are proposed based on the first-hand experience gained by the author in the actual project.

We believe that an ideal Web-oriented C/C++ project should be insensitive to the compilation target - can be compiled to native code, or can be compiled to WebAssembly and run in the web page, the switch between each other only need to change the target configuration. So, we can make full use of the powerful development, debugging, analysis, testing and other functions of the existing IDE environment to improve project quality and reduce development costs.

However, the operating environment of WebAssembly is very different from the native code. Therefore, in order to achieve the above ideal goal, the characteristics (or limitations) of the Web environment must be fully considered, from the overall framework to the interface design and even to the data exchange between functions. This is the connotation of "WebAssembly friendly" implemented in this book.

## Read online

- [SUMMARY.md](SUMMARY.md)
- https://3dgen.cn/cppwasm-book

## Reference

- ["WebAssembly Primer"](https://www.epubit.com/book/detail/40619)
- [https://github.com/chai2010/awesome-wasm-zh](https://github.com/chai2010/awesome-wasm-zh)

----

## Progress

* [Chapter 1 Getting started with Emscripten](ch1-quick-guide/readme.md)
* [x] [1.1 Installing Emscripten](ch1-quick-guide/ch1-01-install.md)
* [x] [1.2 Hello, world!](ch1-quick-guide/ch1-02-helloworld.md)
* [x] [1.3 Taking a look at the Emscripten glue code](ch1-quick-guide/ch1-03-glue-code.md)
* [x] [1.4 Selecting compilation target](ch1-quick-guide/ch1-04-compile.md)

* [Chapter 2 Connecting C and JavaScript](ch2-c-js/readme.md)
* [x] [2.1 Calling compiled C functions from JavaScript](ch2-c-js/ch2-01-js-call-c.md)
* [x] [2.2 Implement C API in JavaScript](ch2-c-js/ch2-02-implement-c-api-in-js.md)
* [x] [2.3 Memory model](ch2-c-js/ch2-03-mem-model.md)
* [x] [2.4 Exchange data between C and JavaScript](ch2-c-js/ch2-04-data-exchange.md)
* [x] [2.5 Using `EM_ASM`](ch2-c-js/ch2-05-em-asm.md)
* [x] [2.6 Using `emscripten_run_script`](ch2-c-js/ch2-06-run-script.md)
* [x] [2.7 Using `ccall`/`cwrap`](ch2-c-js/ch2-07-ccall-cwrap.md)
* [x] [2.8 Supplement](ch2-c-js/ch2-08-ext.md)

* [Chapter 3 Emscripten runtime](ch3-runtime/readme.md)
* [x] [3.1 Runtime lifecycle](ch3-runtime/ch3-01-main.md)
* [x] [3.2 Message loop](ch3-runtime/ch3-02-message-loop.md)
* [x] [3.3 File system](ch3-runtime/ch3-03-fs.md)
* [x] [3.4 Memory management](ch3-runtime/ch3-04-mem.md)
* [x] [3.5 Customize Module object](ch3-runtime/ch3-05-module.md)
* [x] [3.6 Summary](ch3-runtime/ch3-06-summary.md)

* [Chapter 4 General techniques that WebAssembly friendly](ch4-techniques/readme.md)
* [x] [4.1 Message loop detaching](ch4-techniques/ch4-01-msg-loop-detach.md)
* [x] [4.2 Memory alignment](ch4-techniques/ch4-02-align.md)
* [x] [4.3 Exporting C++ objects using the C interface](ch4-techniques/ch4-03-export-obj.md)
* [x] [4.4 Lifecycle control for C++ object](ch4-techniques/ch4-04-obj-life-cycle.md)
* [x] [4.5 Importing JavaScript object using C interface](ch4-techniques/ch4-05-import-js-obj.md)
* [x] [4.6 Be careful with `int64`](ch4-techniques/ch4-06-int64-issue.md)
* [x] [4.7 Forget about filesystem](ch4-techniques/ch4-07-forget-about-fs.md)

* [Chapter 5 Network IO](ch5-net/readme.md)
* [x] [5.1 XMLHttpRequest](ch5-net/ch5-01-http.md)
* [x] [5.2 WebSocket](ch5-net/ch5-02-websocket.md)
<!--* [ ] 5.3 fetch-->

* [Chapter 6 Multithreading](ch6-threads/readme.md)
* [x] [6.1 Multithreading in JavaScript](ch6-threads/ch6-01-worker.md)
* [x] [6.2 Using Emscripten in Web Worker](ch6-threads/ch6-02-sample.md)
<!--* [ ] 6.3 A simple framework for multithreading-->

* [Chapter 7 GUI](ch7-gui/readme.md)
* [x] [7.1 Canvas](ch7-gui/ch7-01-canvas.md)
* [x] [7.2 Mouse events](ch7-gui/ch7-02-mouse.md)
* [x] [7.3 Keyboard events](ch7-gui/ch7-03-keyboard.md)
* [x] [7.4 Conway's Game of Life](ch7-gui/ch7-04-life.md)

<!--* Chapter 8 Project management
* [ ] 8.1 Using Makefile
* [ ] 8.2 Using static library-->

----

## Copyright


48 changes: 48 additions & 0 deletions en/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Summary

* [Chapter 1 Getting started with Emscripten](ch1-quick-guide/readme.md)
* [1.1 Installing Emscripten](ch1-quick-guide/ch1-01-install.md)
* [1.2 Hello, world!](ch1-quick-guide/ch1-02-helloworld.md)
* [1.3 Taking a look at the Emscripten glue code](ch1-quick-guide/ch1-03-glue-code.md)
* [1.4 Selecting compilation target](ch1-quick-guide/ch1-04-compile.md)

* [Chapter 2 Connecting C and JavaScript](ch2-c-js/readme.md)
* [2.1 Calling compiled C functions from JavaScript](ch2-c-js/ch2-01-js-call-c.md)
* [2.2 Implement C API in JavaScript](ch2-c-js/ch2-02-implement-c-api-in-js.md)
* [2.3 Memory model](ch2-c-js/ch2-03-mem-model.md)
* [2.4 Exchange data between C and JavaScript](ch2-c-js/ch2-04-data-exchange.md)
* [2.5 Using `EM_ASM`](ch2-c-js/ch2-05-em-asm.md)
* [2.6 Using `emscripten_run_script`](ch2-c-js/ch2-06-run-script.md)
* [2.7 Using `ccall`/`cwrap`](ch2-c-js/ch2-07-ccall-cwrap.md)
* [2.8 Supplement](ch2-c-js/ch2-08-ext.md)

* [Chapter 3 Emscripten runtime](ch3-runtime/readme.md)
* [3.1 Runtime lifecycle](ch3-runtime/ch3-01-main.md)
* [3.2 Message loop](ch3-runtime/ch3-02-message-loop.md)
* [3.3 File system](ch3-runtime/ch3-03-fs.md)
* [3.4 Memory management](ch3-runtime/ch3-04-mem.md)
* [3.5 Customize Module object](ch3-runtime/ch3-05-module.md)
* [3.6 Summary](ch3-runtime/ch3-06-summary.md)

* [Chapter 4 General techniques that WebAssembly friendly](ch4-techniques/readme.md)
* [4.1 Message loop detaching](ch4-techniques/ch4-01-msg-loop-detach.md)
* [4.2 Memory alignment](ch4-techniques/ch4-02-align.md)
* [4.3 Exporting C++ objects using the C interface](ch4-techniques/ch4-03-export-obj.md)
* [4.4 Lifecycle control for C++ object](ch4-techniques/ch4-04-obj-life-cycle.md)
* [4.5 Importing JavaScript object using C interface](ch4-techniques/ch4-05-import-js-obj.md)
* [4.6 Be careful with `int64`](ch4-techniques/ch4-06-int64-issue.md)
* [4.7 Forget about filesystem](ch4-techniques/ch4-07-forget-about-fs.md)

* [Chapter 5 Network IO](ch5-net/readme.md)
* [5.1 XMLHttpRequest](ch5-net/ch5-01-http.md)
* [5.2 WebSocket](ch5-net/ch5-02-websocket.md)

* [Chapter 6 Multithreading](ch6-threads/readme.md)
* [6.1 Multithreading in JavaScript](ch6-threads/ch6-01-worker.md)
* [6.2 Using Emscripten in Web Worker](ch6-threads/ch6-02-sample.md)

* [Chapter 7 GUI](ch7-gui/readme.md)
* [7.1 Canvas](ch7-gui/ch7-01-canvas.md)
* [7.2 Mouse events](ch7-gui/ch7-02-mouse.md)
* [7.3 Keyboard events](ch7-gui/ch7-03-keyboard.md)
* [7.4 Conway's Game of Life](ch7-gui/ch7-04-life.md)
20 changes: 20 additions & 0 deletions en/book.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"gitbook": "3.x",
"title": "WebAssembly friendly programming with C/C++",
"description": "WebAssembly friendly programming with C/C++ -- Emscripten practice",
"language": "en",

"structure": {
"readme": "preface.md"
},

"pluginsConfig": {
"theme-default": {
"showLevel": false
}
},

"plugins": [
"-search"
]
}
86 changes: 86 additions & 0 deletions en/ch1-quick-guide/ch1-01-install.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# 1.1 Installing Emscripten

Emscripten includes the complete set of tools include LLVM/Node.js/Python/Java/etc. that required to compile C/C++ code into WebAssembly, which does not depend on any other compiler environment.

## 1.1.1 Installing Emscripten using the `emsdk` command line

Emsdk is a set of Python 2 based scripts, so you need to install Python 2.7.12 or newer first . Python download address: [https://www.python.org/downloads/] (https://www.python.org/downloads/)

### `Downloading emsdk`

Once Python is ready, download the emsdk toolkit. If you're familiar with git, just use the following command to clone the emsdk library to local folder:

```
git clone https://github.com/juj/emsdk.git
```

If you are not familiar with git, you can visit [https://github.com/juj/emsdk](https://github.com/juj/emsdk) then download the emsdk library and extract it via "Clone or download" at the top right of the page. Figure:

![](images/download_emsdk.png)

### `Installing and activating Emscripten`

For MacOS or Linux users, `cd` into the folder where emsdk is located in a terminal and execute the following command:

```
./emsdk update
./emsdk install latest
```

Emsdk will download and install the latest components of Emscripten from the Internet automatically. After the installation is complete, execute the following command to activate Emscripten:

```
./emsdk activate latest
```

Everytime a new terminal instance was created, you should `cd` into the folder where emsdk is located, and execute the following command to activate Emscripten environment variables for the current terminal:

```
source ./emsdk_env.sh
```

The installation on Windows is basically the same, the difference is to use `emsdk.bat` instead of `emsdk`, use `emsdk_env.bat` instead of `source./emsdk_env.sh`. The following commands are used to install and activate Emscripten:

```
emsdk.bat update
emsdk.bat install latest
emsdk.bat activate latest
```

The following commands are used to activate Emscripten environment variables:

```
emsdk_env.bat
```

> **tips** Installation and activation only need to be run once. On Windows, if you want to register Emscripten's environment variables as global variables, you can run `emsdk.bat activate latest --global` as an administrator. This will change the system environment variables so that you don't need to run `emsdk_env.bat` in every newly created terminal, this method has potential side effects: it changes environment variables to Emscripten's built-in Node.js/Python/Java, and if other versions of these components are installed on the system, conflicts may arise.
## 1.1.2 Installing Emscripten in a Docker environment

If you are familiar with Docker, you can also install Emscripten in a Docker environment. Emscripten in the Docker is completely isolated and does not have any impact on the host environment. The `apiaryio/emcc` image provides a complete Embscripten package.

For example, the following command will compile hello.c using `emcc`:

```
$ docker run --rm -it -v `pwd`:/src apiaryio/emcc emcc hello.c
```

The parameter `--rm` indicates that the container resource is deleted after the end of the run. The parameter `-it` indicates the standard input and output of the orientation container to the command line environment. The parameter `-v 'pwd':/src` indicates that the current directory is mapped to The /src directory of the container. The following `apiaryio/emcc` is the name of the corresponding mirror of the container, which contains the Emscripten development environment. The final emcc parameter represents the command that is running in the container, which is consistent with the local emcc command.

## 1.1.3 Verifying the installation

`emcc` is the core command of Emscripten. If the installation and activation is correct, execute `emcc -v` and you'll see version information like this:

```
emcc -v
emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 1.38.11
clang version 6.0.1 (emscripten 1.38.11 : 1.38.11)
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: E:\Tool\emsdk\clang\e1.38.11_64bit
INFO:root:(Emscripten: Running sanity checks)
```

For more details on the installation of Emscripten, you can visit: [http://kripken.github.io/emscripten-site/docs/getting_started/downloads.html] (http://kripken.github.io/emscripten-site/ Docs/getting_started/downloads.html)

Emscripten has officially supported WebAssembly since v1.37.3, users who have installed older versions of Emscripten should upgrade to the latest. The contents of this book are based on Emscripten 1.38.11.
Loading

0 comments on commit c554374

Please sign in to comment.