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/js-promise-integration/Overview.md
+9-4Lines changed: 9 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ For more information, please refer to the notes and slides for the [June 28, 202
8
8
9
9
Following feedback that the Stacks Subgroup had received from TC39, this proposal allows *only* WebAssembly stacks to be suspended—it makes no changes to the JavaScript language and, in particular, does not indirectly enable support for detached `async`/`await` in JavaScript.
10
10
11
-
In addition, this proposal does not imply any change to the WebAssembly language. There are no new WebAssembly instructions, nor are there any additional WebAssembly types specified. Semantically, all of the changes outlined are at the boundary between WebAssembly and JavaScript.
11
+
In addition, this proposal does not imply any change to either JavaScript or to the WebAssembly language. There are no new WebAssembly instructions, nor are there any additional WebAssembly types specified. Semantically, all of the changes outlined are at the boundary between WebAssembly and JavaScript.
12
12
13
13
This proposal depends heavily on the [js-types](https://github.com/WebAssembly/js-types/) proposal, which introduces `WebAssembly.Function` as a subclass of `Function`.
14
14
@@ -105,13 +105,15 @@ and the revised import is:
105
105
We prepare the JavaScript `compute_delta` function for our use by constructing a `WebAssembly.Function` object from it, and setting the `suspending` attribute to `first`:
106
106
```
107
107
var suspending_compute_delta = new WebAssembly.Function(
108
-
{parameters:[],results:['externref']},
108
+
{parameters:[],results:['f64']},
109
109
compute_delta,
110
110
{suspending:"first"}
111
111
)
112
112
```
113
113
There are three possiblities for assigning a value to the `suspending` attribute: `"first"`, `"last"` and `"none"`. These relate to which argument of `$compute_delta_import` actually has the suspender. In our case it makes no difference whether we use `"first"` or `"last"` because the are no other arguments. Using `"none"` is a signal that the function is not actually suspending.
114
114
115
+
The return type of `suspending_compute_delta` is an `"f64"`, because that is what the WebAssembly module is importing. However, the actual function that is executed returns a `Promise` of a `f64`. The importing WebAssembly module never sees that `Promise` object—it is consumed by the function generated via teh `WebAssembly.Function` constructor.
116
+
115
117
The complete import object looks like:
116
118
```
117
119
var init_state = () => 2.71;
@@ -133,6 +135,7 @@ The process of wrapping exports is a little different to wrapping imports; in pa
133
135
```
134
136
var sampleModule = WebAssembly.instantiate(demoBuffer,importObj);
135
137
var update_state = new WebAssembly.function(
138
+
{parameters:[], results:['externref']},
136
139
sampleModule.exports.update_state_export,
137
140
{promising : "first"})
138
141
```
@@ -224,20 +227,22 @@ Importantly, functions written in JavaScript are *not* suspendable, conforming t
224
227
225
228
The constructor for `WebAssembly.Function`, when it has a `promising` attribute in its `usage` dictionary, and a `type` argument of the form:
expects its `func` argument to be a WebAssembly function of type:
230
233
```
231
234
(params externref t0 .. tn) (results r0 .. rk)
232
235
```
233
-
if the value of `promising` is `"first"`, and of type:
236
+
if the value of `promising` is `"first"`, and `func` should be of type:
234
237
```
235
238
(params t0 .. tn externref) (results r0 .. rk)
236
239
```
237
240
if the value of `promising` is `"last"`.
238
241
239
242
If the value of `promising` is `"none"`, then this specification does not apply to the constructed function.
240
243
244
+
Note that the return type of the `WebAssembly.Function` is fixed to `externref`. This is because the wrapped function may return a `Promise`. Because the function is not always expected to return a `Promise`—if the export returns normally as opposed to suspending then it will typically not return a `Promise`; however, type consistentency requires that any non-`Promise` return value must be boxed as an `externref`. This boxing is implemented as part of the constructed function.
245
+
241
246
0. Let `func` be the function that is created using this variant of the `WebAssembly.Function` constructor. This function, when called with arguments `args`, will:
242
247
1. Allocate a new `Suspender` object and pass it as an additional argument to `args` to the `func` argument in the `WebAssembly.Function` constructor.
243
248
2. Changes the state of `suspender`'s state to **Active**[`caller`] (where `caller` is the current caller)
0 commit comments