Skip to content

Commit 2e1c731

Browse files
committed
Add table of contents
1 parent c2c66af commit 2e1c731

File tree

1 file changed

+37
-79
lines changed

1 file changed

+37
-79
lines changed

README.md

Lines changed: 37 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,37 @@
11
# WebAssembly examples
22

3-
### Resources
3+
Getting started with hand-written WebAssembly
4+
5+
- [Resources](#resources)
6+
- [Tools](#tools)
7+
- [Getting started](#getting-started)
8+
- [Types](#types)
9+
- [Functions](#functions)
10+
- [if...else statement](#ifelse-statement)
11+
- [Ternary operator](#ternary-operator)
12+
- [Blocks of code](#blocks-of-code)
13+
- [Operators](#operators)
14+
- [Linear Memory](#linear-memory)
15+
- [Conversion](#conversion)
16+
17+
## Resources
18+
419
- [WebAssembly Docs](https://webassembly.org/docs/high-level-goals/)
520
- [WebAssembly Github](https://github.com/webassembly)
621
- [WebAssembly MDN](https://developer.mozilla.org/en-US/docs/WebAssembly)
722
- [Awesome Wasm](https://github.com/mbasso/awesome-wasm)
823

9-
### Tools
24+
## Tools
25+
1026
- [WebAssembly Studio](https://webassembly.studio/)
1127
- [WebAssembly Explorer](https://mbebenita.github.io/WasmExplorer/)
1228
- [Wat2Wasm](https://cdn.rawgit.com/WebAssembly/wabt/aae5a4b7/demo/wat2wasm/)
1329
- [Wasm2Wat](https://cdn.rawgit.com/WebAssembly/wabt/aae5a4b7/demo/wasm2wat/)
1430

15-
### Getting started
31+
## Getting started
1632

1733
Create file *module.wat*
34+
1835
```WebAssembly
1936
(module
2037
;; calling JavaScript from WebAssembly
@@ -35,16 +52,19 @@ Create file *module.wat*
3552
```
3653

3754
Convert from WebAssembly text format to the WebAssembly binary format
55+
3856
```
3957
wat2wasm module.wat -o module.wasm
4058
```
4159

4260
Run a web server
61+
4362
```
4463
python3 -m http.server 8000
4564
```
4665

4766
Load and run WebAssembly code
67+
4868
```JavaScript
4969
let importObject = {
5070
env: {
@@ -62,13 +82,15 @@ fetch('module.wasm').then(response =>
6282
});
6383
```
6484

65-
### Types
85+
## Types
86+
6687
- `i32`: 32-bit integer
6788
- `i64`: 64-bit integer
6889
- `f32`: 32-bit floating point
6990
- `f64`: 64-bit floating point
7091

71-
### Functions
92+
## Functions
93+
7294
All code in WebAssembly is grouped into functions
7395

7496
```cpp
@@ -84,7 +106,7 @@ int add(int a, int b) {
84106
(get_local $b)))
85107
```
86108

87-
### if...else statement
109+
## if...else statement
88110

89111
There are two ways to express `if`...`else` statement
90112

@@ -124,7 +146,7 @@ There are two ways to express `if`...`else` statement
124146
)
125147
```
126148

127-
### Ternary operator
149+
## Ternary operator
128150

129151
```cpp
130152
condition ? m : n
@@ -138,7 +160,7 @@ condition ? m : n
138160
)
139161
```
140162

141-
### Blocks of code
163+
## Blocks of code
142164

143165
- `block`
144166
- `loop`
@@ -202,7 +224,8 @@ for (int i = 0; i < 10; i++) {
202224
)
203225
```
204226

205-
### Operators
227+
## Operators
228+
206229
| | i32 | i64 | f32 | f64 |
207230
|:-:| --- | --- | --- | --- |
208231
| + | `add` | `add` | `add` | `add` |
@@ -217,7 +240,7 @@ for (int i = 0; i < 10; i++) {
217240
| > | `gt_s` | `gt_s` | `gt` | `gt` |
218241
| >= | `ge_s` | `ge_s` | `ge` | `ge` |
219242

220-
### Linear Memory
243+
## Linear Memory
221244

222245
`memory` is a sandboxed array of bytes
223246

@@ -240,73 +263,8 @@ for (int i = 0; i < 10; i++) {
240263
(get_local $b)))
241264
```
242265

243-
### Conversion
266+
## Conversion
244267

245-
```WebAssembly
246-
(func $i64_to_i32 (param i64) (result i32)
247-
(i32.wrap/i64
248-
(get_local 0)))
249-
250-
(func $f32_to_i32 (param f32) (result i32)
251-
(i32.trunc_s/f32
252-
(get_local 0)))
253-
254-
(func $f64_to_i32 (param f64) (result i32)
255-
(i32.trunc_s/f64
256-
(get_local 0)))
257-
258-
(func $i32_to_i64 (param i32) (result i64)
259-
(i64.extend_s/i32
260-
(get_local 0)))
261-
262-
(func $f32_to_i64 (param f32) (result i64)
263-
(i64.trunc_s/f32
264-
(get_local 0)))
265-
266-
(func $f64_to_i64 (param f64) (result i64)
267-
(i64.trunc_s/f64
268-
(get_local 0)))
269-
270-
(func $i32_to_f32 (param i32) (result f32)
271-
(f32.convert_s/i32
272-
(get_local 0)))
273-
274-
(func $i64_to_f32 (param i64) (result f32)
275-
(f32.convert_s/i64
276-
(get_local 0)))
277-
278-
(func $f64_to_f32 (param f64) (result f32)
279-
(f32.demote/f64
280-
(get_local 0)))
281-
282-
(func $i32_to_f64 (param i32) (result f64)
283-
(f64.convert_s/i32
284-
(get_local 0)))
285-
286-
(func $i64_to_f64 (param i64) (result f64)
287-
(f64.convert_s/i64
288-
(get_local 0)))
289-
290-
(func $f32_to_f64 (param f32) (result f64)
291-
(f64.promote/f32
292-
(get_local 0)))
293-
294-
(func $i32_to_bool (param i32) (result i32)
295-
(i32.ne
296-
(get_local 0)
297-
(i32.const 0)))
298-
299-
(func $i32_to_i8 (param i32) (result i32)
300-
(i32.shr_s
301-
(i32.shl
302-
(get_local 0)
303-
(i32.const 24))
304-
(i32.const 24)))
305-
306-
(func $i32_to_i16 (param i32) (result i32)
307-
(i32.shr_s
308-
(i32.shl
309-
(get_local 0)
310-
(i32.const 16))
311-
(i32.const 16)))
312-
```
268+
Methods used for conversion:
269+
`wrap`, `trunc_s`, `extend_s`, `convert_s`, `demote`, `promote`
270+
[Examples](src/conversion.wat)

0 commit comments

Comments
 (0)