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
+8-4Lines changed: 8 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -123,13 +123,17 @@ for (let index = 0; index < length; index++)
123
123
| table |`WebAssembly.Table` object |
124
124
| function | WebAssembly exported function |
125
125
126
+
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.
127
+
126
128
1. JS module is parsed
127
129
1. wasm module is parsed
128
130
1. wasm module has a lexical environment created for its exports. All exports are initially in TDZ.
129
131
1. JS module is instantiated. Imports are bound to the same memory locations.
130
132
1. wasm module is instantiated evaluated. Functions are initialized. Memories and tables are initialized and filled with data/elem sections. Globals are initialized and initializer expressions are evaluated. The start function runs.
131
133
1. JS module is evaluated. All values are available.
132
134
135
+
The value of the export for `WebAssembly.Global` is provided directly on the JS namespace object, with mutable globals reflected as live bindings to JS.
136
+
133
137
#### Example
134
138
135
139
```js
@@ -177,7 +181,7 @@ Wasm exports can be imported as accurate, immutable bindings to other wasm modul
177
181
178
182
### wasm imports <- JS re-exports <- wasm exports
179
183
180
-
Any wasm exports that are re-exported via a JS module will be available to the other wasm module as accuratebindings. If the binding is a mutable global, then that binding will be a live export binding in JS reflecting changes to the Wasm global value. When imported from JS, the first and second Wasm modules will yield the same Wasm identities for the multiply exported Wasm objects.
184
+
Any wasm exports that are re-exported via a JS module will be available to the other wasm module as accurate, immutable bindings. The wasm export gets wrapped into a JS object (e.g. `WebAssembly.Global`) and then unwrapped to the wasm import in the importing wasm module. When imported from JS, the first and second Wasm modules will yield the same object identities for the multiply exported Wasm objects.
181
185
182
186
#### Example
183
187
@@ -213,7 +217,7 @@ export {memoryExport} from "./b.wasm";
213
217
214
218
1. JS module is parsed
215
219
1. wasm module is parsed
216
-
1. wasm module has a lexical environment created for its exports. All direct exports are initially in TDZ, indirect exports are available as they resolve.
220
+
1. wasm module has a lexical environment created for its exports. All exports are initially in TDZ.
217
221
1. JS module is instantiated. All imports (including functions) from the wasm module are memory locations holding undefined.
218
222
1. wasm module is instantiated and evaluated. Snapshots of imports are taken. Export bindings are initialized.
219
223
1. JS module is evaluated.
@@ -251,8 +255,8 @@ export function functionExport() {
251
255
1. wasm module is parsed
252
256
1. JS module is parsed
253
257
1. JS module is instantiated.
254
-
1. wasm module has a lexical environment created for its exports. All direct exports are initially in TDZ, indirect exports are available as they resolve.
255
-
1. JS module is evaluated. wasm exports in TDZ lead to a ReferenceError if used.
258
+
1. wasm module has a lexical environment created for its exports. All exports are initially in TDZ.
259
+
1. JS module is evaluated. wasm exports lead to a ReferenceError if used.
256
260
1. wasm module is instantiated and evaluated; wasm-exported bindings are updated to their appropriate JS API-exposed values.
Copy file name to clipboardExpand all lines: proposals/esm-integration/README.md
+13-14Lines changed: 13 additions & 14 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,22 +94,23 @@ In the proposed HTML integration of WebAssembly modules, the [module name](https
94
94
95
95
### "Snapshotting" imports
96
96
97
-
When imports are provided to WebAssembly modules in the host instance linking model from JavaScript, they are provided directly upfront.
97
+
When imports are provided to WebAssembly modules in the host instance linking model, they are provided directly upfront.
98
98
99
99
- This handling of imports could be called a "snapshotting" process: Later updates to the imported values won't be reflected within the WebAssembly module.
100
-
- Circular WebAssembly module bindings are not supported for direct exports: One of them will run first, and that one will find that the exports of the other aren't yet initialized, leading to a ReferenceError. Indirect exports (reexports) can be supported early in Wasm cycles, where a WebAssembly module exports the same binding it imports.
100
+
- Circular WebAssembly modules are not supported: One of them will run first, and that one will find that the exports of the other aren't yet initialized, leading to a ReferenceError.
101
101
102
102
See the FAQ for more explanation of the rationale for this design decision, and what features it enables which would be difficult or impossible otherwise.
103
103
104
-
### Live binding exports
104
+
### Progressive Implementation Support
105
105
106
-
WebAssembly modules support live exports bindings for mutable globals by reflecting the infallible ToJsValue() representation of their mutable global exports on their JavaScript environment bindings record.
106
+
It is possible to implement the Wasm-ESM integration in two stages:
107
107
108
-
For bindings that are exported from bindings that were initially imported i.e. indirect exports or reexports:
108
+
1. In the first stage only source phase imports of Wasm are supported (`import source fibModule from "./fib.wasm"`).
109
+
2. In the second stage, evaluation phase imports would be supported too (`import { fib } from "./fib.wasm"`).
109
110
110
-
1. WebAssembly modules that have indirect exports to JavaScript modules, these values are snapshotted at the time of execution of the WebAssembly module and reflected as captured bindings and not live bindings on the exports, even if the underlying JavaScript module might have modifications to the binding later on.
111
+
If initially implementing just source phase imports, the `GetExportedNames`, `ResolveExport`, `InitializeEnvironment`, and `ExecuteModule` abstract operations can be implemented as abstract operations unconditionally throwing a `SyntaxError` exception. In this case, module fetch and CSP integration is still required to be implemented as specified in this proposal.
111
112
112
-
2. WebAssembly modules that have indirect exports to WebAssembly modules, where those modules directly export mutable globals, support the live mutable global reference without capturing the import.
113
+
Implementers are encouraged to ship both stages at once, but it is deemed OK for implementers to initially ship the first stage and then quickly follow up with the second stage, if this aids "time to ship" in implementations.
113
114
114
115
## FAQ
115
116
@@ -119,7 +120,7 @@ Originally the ESM integration only provided the direct host instance linking mo
119
120
120
121
Supporting the [source phase](https://github.com/tc39/proposal-source-phase-imports) ESM integration is therefore a more general form of the ESM integration that shares the host resolver, while retaining linking flexibility for Js host embedding of Wasm.
121
122
122
-
While the source phase does not replace the instance linking model, is does offer a more general and flexible ESM integration as an addition.
123
+
While the source phase does not replace the instance linking model, is does offer a more general and flexible ESM integration.
123
124
124
125
### How does this relate to the Component Model?
125
126
@@ -149,7 +150,7 @@ The conversion is based on the type that the import and export was declared as,
149
150
150
151
### When one WebAssembly module imports another one, will there be overhead due to converting back and forth to JS values?
151
152
152
-
Bindings between WebAssembly modules, even those that are indirect through indirect exports (reexports) are directly linked, without needing to go through JS wrapping and unwrapping.
153
+
Note that exports of ES Module Records always have values that can be directly treated as JavaScript values. Although we're talking about conversions to and from JavaScript for these exports, it's expected that, in native implementations, the conversion to and from Javascript would "cancel out" and not lead to the use of wrappers in practice.
153
154
154
155
### Why are WebAssembly modules instantiated during the "Evaluation" phase?
155
156
@@ -171,11 +172,9 @@ Instead of including this check in the default semantics of functions, a trampol
171
172
172
173
### What does snapshotting imports mean for importing globals?
173
174
174
-
Imports are only snapshotted when they resolve to a JavaScript module. For imports that resolve to WebAssembly modules, these are always directly bound.
175
-
176
-
When a WebAssembly module imports a Global, that resolves to a module after resolving the binding through any indirect exports (reexports):
177
-
- If the resolved module is a JavaScript module, then the exporting module may either export a direct value or a `WebAssembly.Global` of the same type.
178
-
- If the resolved module is a WebAssembly module, then the exporting module must export a global that is an extern subtype of the importing global.
175
+
When a WebAssembly module imports a Global, there are two possible modes of operation:
176
+
- If the Global type is immutable (as declared in the importing module), then the exporting module may either export a numeric value or an immutable Global.
177
+
- If the Global type is mutable, then the exporting module must export a mutable Global. The snapshot here is "shallow" in the sense that modifications *within* this particular mutable Global object *will* be visible in the importing module (but, if the exporting module overwrites the entire binding with some unrelated value, this will not be noticed by the importing module).
0 commit comments