You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/esm-integration/EXAMPLES.md
+31-24Lines changed: 31 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,9 @@ When the Wasm module is evaluated, the exported value of the JS module is used i
8
8
9
9
| import type | behavior |
10
10
|-------------|----------|
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 |
14
14
| 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. |
15
15
16
16
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
21
21
1. JS module is parsed
22
22
1. JS module is instantiated. Function declaration exports are initialized. Other exports are set to undefined or are in TDZ.
23
23
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.
25
25
1. wasm module is instantiated and its start function runs. All imports are snapshotted. All exports are initialized.
26
26
27
27
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
@TODO add example of WebAssembly.Global being updated
51
52
52
-
```
53
+
```wasm
53
54
// main.wasm
54
55
(module
55
56
(import "./counter.js" "count" (global i32))
56
57
)
57
-
58
+
```
59
+
```js
58
60
// counter.js
59
61
let count =42;
60
62
export {count};
@@ -68,9 +70,9 @@ export {count};
68
70
69
71
| export type | imported value |
70
72
|-------------|---------------------------|
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 |
74
76
| function | WebAssembly exported function |
75
77
76
78
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
86
88
87
89
#### Example
88
90
89
-
```
91
+
```js
90
92
// main.js
91
93
import {count, increment} from"./counter.wasm";
92
94
console.log(count.value); // logs 5
93
95
increment();
94
96
console.log(count.value); // logs 6
95
-
97
+
```
98
+
```wasm
96
99
// counter.wasm
97
100
(module
98
101
(func $increment
@@ -111,7 +114,7 @@ Wasm exports can be imported as accurate, immutable bindings to other wasm modul
111
114
112
115
#### Example
113
116
114
-
```
117
+
```wasm
115
118
// main.wasm
116
119
(module
117
120
(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
136
139
137
140
#### Example
138
141
139
-
```
142
+
```wasm
140
143
// main.wasm
141
144
(module
142
145
(import "./a.js" "memoryExport" (memory 0))
143
146
)
144
-
147
+
```
148
+
```js
145
149
// a.js
146
150
export {memoryExport} from"./b.wasm";
147
-
151
+
```
152
+
```wasm
148
153
// b.wasm
149
154
(module
150
155
(memory 1)
@@ -155,9 +160,9 @@ export {memoryExport} from "./b.wasm";
155
160
### JS <-> wasm cycle (where JS is higher in the module graph)
156
161
157
162
#### 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 |
0 commit comments