Skip to content

Commit 8272bec

Browse files
authored
Imrove syntax highlighting (#36)
1 parent 884066d commit 8272bec

File tree

1 file changed

+31
-24
lines changed

1 file changed

+31
-24
lines changed

proposals/esm-integration/EXAMPLES.md

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ When the Wasm module is evaluated, the exported value of the JS module is used i
88

99
| import type | behavior |
1010
|-------------|----------|
11-
| global | If the exported value is a WebAssembly.Global object, throw an exception if types mismatch, otherwise take that object as the import. Otherwise, if the imported type is `const`, cast the imported value to the appropriate numeric type, and create a new const global to hold the result. Otherwise, throw an exception. |
12-
| memory | Check that the exported value is a WebAssembly.Memory object which meets the type imported; use it if so, otherwise, throw an exception |
13-
| table | Check that the exported value is a WebAssembly.Table object which meets the type imported; use it if so, otherwise, throw an exception |
11+
| global | If the exported value is a `WebAssembly.Global` object, throw an exception if types mismatch, otherwise take that object as the import. Otherwise, if the imported type is `const`, cast the imported value to the appropriate numeric type, and create a new const global to hold the result. Otherwise, throw an exception. |
12+
| memory | Check that the exported value is a `WebAssembly.Memory` object which meets the type imported; use it if so, otherwise, throw an exception |
13+
| table | Check that the exported value is a `WebAssembly.Table` object which meets the type imported; use it if so, otherwise, throw an exception |
1414
| function | If the exported value is a WebAssembly exported function based on the same types, make use of it. Otherwise, [create a host function](https://webassembly.github.io/spec/js-api/index.html#create-a-host-function) out of the JS function which includes the casts implied by its type. |
1515

1616
While WebAssembly only has the concept of globals, JS could export either a regular JS value or a `WebAssembly.Global`.
@@ -21,7 +21,7 @@ The sequence of operations for a wasm module which depends on a JS module is as
2121
1. JS module is parsed
2222
1. JS module is instantiated. Function declaration exports are initialized. Other exports are set to undefined or are in TDZ.
2323
1. wasm module has an exports lexical environment created. Imports are bound to memory locations but values are not snapshotted yet.
24-
1. JS module is evaluated. All of its remaining exports (i.e. non-fuction values) are initialized. Any top-level statements are executed.
24+
1. JS module is evaluated. All of its remaining exports (i.e. non-function values) are initialized. Any top-level statements are executed.
2525
1. wasm module is instantiated and its start function runs. All imports are snapshotted. All exports are initialized.
2626

2727
Note: because these are snapshots of values, this does not maintain the live-binding semantics that exists between JS modules. For example, let's say JS module exports a function, called `foo`. The WebAssembly module imports `foo`. Then, after evaluation, the JS module sets `foo` to a different function. Other JS modules would see this update. However, WebAssembly modules will not.
@@ -30,12 +30,13 @@ Note: because these are snapshots of values, this does not maintain the live-bin
3030

3131
##### Function imports
3232

33-
```
33+
```wasm
3434
// main.wasm
3535
(module
3636
(import "./counter.js" "getCount" (func $getCount (func (result i32))))
3737
)
38-
38+
```
39+
```js
3940
// counter.js
4041
let count = 42;
4142

@@ -49,12 +50,13 @@ export {getCount};
4950

5051
@TODO add example of WebAssembly.Global being updated
5152

52-
```
53+
```wasm
5354
// main.wasm
5455
(module
5556
(import "./counter.js" "count" (global i32))
5657
)
57-
58+
```
59+
```js
5860
// counter.js
5961
let count = 42;
6062
export {count};
@@ -68,9 +70,9 @@ export {count};
6870

6971
| export type | imported value |
7072
|-------------|---------------------------|
71-
| global | WebAssembly.Global object |
72-
| memory | WebAssembly.Memory object |
73-
| table | WebAssembly.Table object |
73+
| global | `WebAssembly.Global` object |
74+
| memory | `WebAssembly.Memory` object |
75+
| table | `WebAssembly.Table` object |
7476
| function | WebAssembly exported function |
7577

7678
Wasm bindings cannot be reassigned as it can in JS, so the exported value will not change in their object identity. But the value that it points to (e.g. `.value` in the case of `WebAssembly.Global`) can change.
@@ -86,13 +88,14 @@ Currently, the value of the export for something like `WebAssembly.Global` would
8688

8789
#### Example
8890

89-
```
91+
```js
9092
// main.js
9193
import {count, increment} from "./counter.wasm";
9294
console.log(count.value); // logs 5
9395
increment();
9496
console.log(count.value); // logs 6
95-
97+
```
98+
```wasm
9699
// counter.wasm
97100
(module
98101
(func $increment
@@ -111,7 +114,7 @@ Wasm exports can be imported as accurate, immutable bindings to other wasm modul
111114

112115
#### Example
113116

114-
```
117+
```wasm
115118
// main.wasm
116119
(module
117120
(import "./counter.wasm" "count" (global i32))
@@ -136,15 +139,17 @@ Any wasm exports that are re-exported via a JS module will be available to the o
136139

137140
#### Example
138141

139-
```
142+
```wasm
140143
// main.wasm
141144
(module
142145
(import "./a.js" "memoryExport" (memory 0))
143146
)
144-
147+
```
148+
```js
145149
// a.js
146150
export {memoryExport} from "./b.wasm";
147-
151+
```
152+
```wasm
148153
// b.wasm
149154
(module
150155
(memory 1)
@@ -155,9 +160,9 @@ export {memoryExport} from "./b.wasm";
155160
### JS <-> wasm cycle (where JS is higher in the module graph)
156161

157162
#### JS exports
158-
| export type | value (not a WebAssembly.Global)* | global | memory | table | function |
163+
| export type | value (not a `WebAssembly.Global`)* | global | memory | table | function |
159164
|-|-------------------------------|--------|--------|-------|----------|
160-
| | 0 if a const import and not in TDZ, otherwise Error | Error | Error | Error | snapshot if it is a function declaration, otherwise Error |
165+
| | `0` if a const import and not in TDZ, otherwise `Error` | `Error` | `Error` | `Error` | snapshot if it is a function declaration, otherwise `Error` |
161166

162167
#### wasm exports
163168
| export type | global | memory | table | function |
@@ -173,13 +178,14 @@ export {memoryExport} from "./b.wasm";
173178

174179
#### Example
175180

176-
```
181+
```js
177182
// a.js
178183
import {memoryExport} from "./b.wasm";
179184
export function functionExport() {
180185
// do something with memory and DOM
181186
}
182-
187+
```
188+
```wasm
183189
// b.wasm
184190
(module
185191
(memory 1)
@@ -198,7 +204,7 @@ export function functionExport() {
198204
#### JS exports
199205
| export type | value (not a WebAssembly.Global)* | global | memory | table | function |
200206
|-|-------------------------------|--------|--------|-------|----------|
201-
| | Error | snapshot | snapshot | snapshot | snapshot |
207+
| | `Error` | snapshot | snapshot | snapshot | snapshot |
202208

203209
1. wasm module is parsed
204210
1. JS module is parsed
@@ -209,14 +215,15 @@ export function functionExport() {
209215

210216
#### Examples
211217

212-
```
218+
```wasm
213219
// a.wasm
214220
(module
215221
(memory 1)
216222
(export "memoryExport" (memory 0))
217223
(import "./b.js" "functionExport" (func $functionExport (result i32)))
218224
)
219-
225+
```
226+
```js
220227
// b.js
221228
import {memoryExport} from "./a.wasm";
222229
export function functionExport() {

0 commit comments

Comments
 (0)