Skip to content

Commit e014c04

Browse files
committed
Clarify some issues about type of functions
1 parent 6509938 commit e014c04

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

proposals/js-promise-integration/Overview.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ For more information, please refer to the notes and slides for the [June 28, 202
88

99
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.
1010

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.
1212

1313
This proposal depends heavily on the [js-types](https://github.com/WebAssembly/js-types/) proposal, which introduces `WebAssembly.Function` as a subclass of `Function`.
1414

@@ -105,13 +105,15 @@ and the revised import is:
105105
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`:
106106
```
107107
var suspending_compute_delta = new WebAssembly.Function(
108-
{parameters:[],results:['externref']},
108+
{parameters:[],results:['f64']},
109109
compute_delta,
110110
{suspending:"first"}
111111
)
112112
```
113113
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.
114114

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+
115117
The complete import object looks like:
116118
```
117119
var init_state = () => 2.71;
@@ -133,6 +135,7 @@ The process of wrapping exports is a little different to wrapping imports; in pa
133135
```
134136
var sampleModule = WebAssembly.instantiate(demoBuffer,importObj);
135137
var update_state = new WebAssembly.function(
138+
{parameters:[], results:['externref']},
136139
sampleModule.exports.update_state_export,
137140
{promising : "first"})
138141
```
@@ -224,20 +227,22 @@ Importantly, functions written in JavaScript are *not* suspendable, conforming t
224227

225228
The constructor for `WebAssembly.Function`, when it has a `promising` attribute in its `usage` dictionary, and a `type` argument of the form:
226229
```
227-
{ parameters: ["t0", .., "tn"], results: [r0, .., rk]}
230+
{ parameters: ["t0", .., "tn"], results: ['externref'']}
228231
```
229232
expects its `func` argument to be a WebAssembly function of type:
230233
```
231234
(params externref t0 .. tn) (results r0 .. rk)
232235
```
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:
234237
```
235238
(params t0 .. tn externref) (results r0 .. rk)
236239
```
237240
if the value of `promising` is `"last"`.
238241

239242
If the value of `promising` is `"none"`, then this specification does not apply to the constructed function.
240243

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+
241246
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:
242247
1. Allocate a new `Suspender` object and pass it as an additional argument to `args` to the `func` argument in the `WebAssembly.Function` constructor.
243248
2. Changes the state of `suspender`'s state to **Active**[`caller`] (where `caller` is the current caller)

0 commit comments

Comments
 (0)