Skip to content

Commit

Permalink
Adding Response based versions of validate, compile, and instantiate.
Browse files Browse the repository at this point in the history
This allows us to support streaming compilation and origin bound
sources for WebAssembly modules.

Proposes a mime type of application/wasm for WebAssembly modules.
  • Loading branch information
flagxor committed Feb 17, 2017
1 parent cd2af4d commit ef02af5
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion JS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ If the given `bytes` argument is not a
the returned `Promise` is [rejected](http://tc39.github.io/ecma262/#sec-rejectpromise)
with a [`TypeError`](https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror).

Otherwise, this function starts an asychronous task to compile a `WebAssembly.Module`
Otherwise, this function starts an asynchronous task to compile a `WebAssembly.Module`
as described in the [`WebAssembly.Module` constructor](#webassemblymodule-constructor).
On success, the `Promise` is [fulfilled](http://tc39.github.io/ecma262/#sec-fulfillpromise)
with the resulting `WebAssembly.Module` object. On failure, the `Promise` is
Expand Down
80 changes: 80 additions & 0 deletions Web.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,86 @@ and retrieve compiled modules from offline storage, instantiate compiled modules
with JavaScript imports, call the exported functions of instantiated modules,
alias the exported memory of instantiated modules, etc.

In addition to the core JavaScript API, the Web embedding includes additional
methods useful in that context.
Non-web embeddings are not required to support
these additional methods.

### Additional Web Embedding API

#### `WebAssembly.compileResponse`

The `compileResponse` function is overloaded based on types of its arguments.
If none of the following overloads match, then the returned `Promise` is
[rejected](http://tc39.github.io/ecma262/#sec-rejectpromise)
with a [`TypeError`](https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror).
Non-web embeddings are not required to support
the [`Response`](https://fetch.spec.whatwg.org/#response-class) or `Promise`
overload.

```
Promise<WebAssembly.Module> compileResponse(Response source)
Promise<WebAssembly.Module> compileResponse(Promise<Response> source)
```

The source of the material to compile can be
a [`Response`](https://fetch.spec.whatwg.org/#response-class)
or a Promise of a
[`Response`](https://fetch.spec.whatwg.org/#response-class).

This function starts an asynchronous task to compile a `WebAssembly.Module`
as described in the [`WebAssembly.Module` constructor](#webassemblymodule-constructor).
On success, the `Promise` is [fulfilled](http://tc39.github.io/ecma262/#sec-fulfillpromise)
with the resulting `WebAssembly.Module` object. On failure, the `Promise` is
[rejected](http://tc39.github.io/ecma262/#sec-rejectpromise) with a
`WebAssembly.CompileError`.

The `Response` or `Promise<Response>` is used as the source of the
bytes to compile.
WebAssembly `source` data is expected to have a mime type of `application/wasm`.
Other mime types
[reject](http://tc39.github.io/ecma262/#sec-rejectpromise) the Promise with a
`WebAssembly.CompileError`.

#### `WebAssembly.instantiateResponse`

The `instantiate` function is overloaded based on types of its arguments.
If none of the following overloads match, then the returned `Promise` is
[rejected](http://tc39.github.io/ecma262/#sec-rejectpromise)
with a [`TypeError`](https://tc39.github.io/ecma262/#sec-native-error-types-used-in-this-standard-typeerror).

```
Promise<{module:WebAssembly.Module, instance:WebAssembly.Instance}>
instantiateResponse(Response source [, importObject])
Promise<{module:WebAssembly.Module, instance:WebAssembly.Instance}>
instantiateResponse(Promise<Response> source [, importObject])
```

The source of the material to compile and instantiate can be
a [`Response`](https://fetch.spec.whatwg.org/#response-class)
or a Promise of a
[`Response`](https://fetch.spec.whatwg.org/#response-class).

This function starts an asynchronous task that first compiles a `WebAssembly.Module`
based on bytes from `source` as described in
the [`WebAssembly.Module` constructor](#webassemblymodule-constructor)
and then instantiate the resulting `Module` with `importObject` as described in the
[`WebAssembly.Instance` constructor](#webassemblyinstance-constructor).
On success, the `Promise` is [fulfilled](http://tc39.github.io/ecma262/#sec-fulfillpromise)
with a plain JavaScript object pair `{module, instance}` containing the resulting
`WebAssembly.Module` and `WebAssembly.Instance`. The 2 properties `module` and `instance` of the returned pair are configurable, enumerable and writable.

On failure, the `Promise` is
[rejected](http://tc39.github.io/ecma262/#sec-rejectpromise) with a
`WebAssembly.CompileError`, `WebAssembly.LinkError`, or `WebAssembly.RuntimeError`, depending on the cause of failure.

WebAssembly `source` data is expected to have a mime type of `application/wasm`.
Other mime types
[reject](http://tc39.github.io/ecma262/#sec-rejectpromise) the Promise with a
`WebAssembly.CompileError`.

## Modules

WebAssembly's [modules](Modules.md) allow for natural [integration with
Expand Down

0 comments on commit ef02af5

Please sign in to comment.