Skip to content

Commit

Permalink
Change strawman text format in explainer to conform to Custom Annotat…
Browse files Browse the repository at this point in the history
…ion Syntax
  • Loading branch information
Luke Wagner committed Apr 11, 2019
1 parent a3f7a4e commit 3119695
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions proposals/webidl-bindings/Explainer.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,20 @@ dictionary Contact {
};
```

could be defined in a WebAssembly module with this (strawman text-format)
statement:
could be defined in a WebAssembly module with this statement defined in a
strawman text format syntax that is built on throughout this explainer:

```wasm
(webidl-type $Contact (dict (field "name" DOMString) (field "age" long)))
(@webidl type $Contact (dict (field "name" DOMString) (field "age" long)))
```

Following the general WebAssembly text format convention, `$Contact` may be
used throughout the Web IDL Bindings Section, with uses converted to the
integer index of the type definition in the Web IDL Types Subsection when the
(The `@webidl` token indicates that the expression is a [Custom Annotation]
and can be skipped by a text-format parser that doesn't support Web IDL
Bindings (by jumping to the closing paren).)

Following the general WebAssembly text format convention, `$Contact` is a label
for the Web IDL type definition and may be used throughout the Web IDL Bindings
Section, with the uses converted to the integer index of the type when the
`.wat` is converted to `.wasm`.


Expand Down Expand Up @@ -262,7 +266,7 @@ text-format) statement would define a Web IDL Function type compatible with
`addContact`:

```wasm
(webidl-type $AddContactFuncWebIDL
(@webidl type $AddContactFuncWebIDL
(func (method any) (param (dict $Contact) DOMString) (result bool)))
```

Expand All @@ -280,7 +284,7 @@ type which the WebAssembly module would use to call `addContact`:
With all these, a Web IDL function binding named `$addContactBinding` can be defined:

```wasm
(webidl-func-binding $addContactBinding import $AddContactFuncWasm $AddContactFuncWebIDL
(@webidl func-binding $addContactBinding import $AddContactFuncWasm $AddContactFuncWebIDL
(param
(as type=any idx=0)
(dict $Contact (utf8-str type=DOMString off-idx=1 len-idx=2) (as type=long idx=3))
Expand All @@ -297,7 +301,7 @@ Lastly, we need the WebAssembly function import statement (that is called by the

```wasm
(func $addContact (import "ContactDB" "addContact") (type $AddContactFuncWasm))
(webidl-bind $addContact $addContactBinding)
(@webidl bind $addContact $addContactBinding)
```

The `webidl-bind` statement is necessary because a single binding can be used
Expand Down Expand Up @@ -465,10 +469,12 @@ The signature of `encodeInto` could be defined with these (strawman text-format)
statements:

```wasm
(webidl-type $TEEIR
(@webidl type $TextEncoderEncodeIntoResult
(dict (field "read" unsigned long long) (field "written" unsigned long long)))
(webidl-type $EncodeIntoFuncWebIDL
(func (method any) (param USVString Uint8Array) (result (dict $TEEIR))))
(@webidl type $EncodeIntoFuncWebIDL
(func (method any)
(param USVString Uint8Array)
(result (dict $TextEncoderEncodeIntoResult))))
```

The WebAssembly signature and import which would be used to call `encodeInto`
Expand All @@ -483,10 +489,10 @@ Finally, the binding is accomplished with these two (strawman text-format)
statements:

```wasm
(webidl-func-binding $encodeIntoBinding import $EncodeIntoFuncWasm $EncodeIntoFuncWebIDL
(@webidl func-binding $encodeIntoBinding import $EncodeIntoFuncWasm $EncodeIntoFuncWebIDL
(param (as type=any idx=0) (as type=any idx=1) (view type=uint8 off-idx=2 len-idx=3))
(result (as type=i64 (field "read" (get 0))) (as type=i64 (field "written" (get 0)))))
(webidl-bind $encodeInto $encodeIntoBinding)
(@webidl bind $encodeInto $encodeIntoBinding)
```

This function binding can be visualized:
Expand All @@ -499,14 +505,14 @@ import the `TextEncoder` constructor, with the strawman
(the callee constructor).

```wasm
(webidl-type $CtorFuncWebIDL (func (constructor default-new-target) (result any)))
(@webidl type $CtorFuncWebIDL (func (constructor default-new-target) (result any)))
(type $CtorFuncWasm (func (result anyref)))
(func $TextEncoderCtor (import "TextEncoder" "ctor") (type $CtorFuncWasm))
(webidl-func-binding $CtorBinding import $CtorFuncWasm $CtorFuncWebIDL
(@webidl func-binding $CtorBinding import $CtorFuncWasm $CtorFuncWebIDL
(result (as type=anyref (get 0))))
(webidl-bind $TextEncoderCtor $CtorBinding)
(@webidl bind $TextEncoderCtor $CtorBinding)
```

With these bindings, a WebAssembly module can call `$TextEncoderCtor`, store
Expand Down Expand Up @@ -630,6 +636,8 @@ constraints of Web APIs' backwards-compatibility.

[ESM-integration]: https://github.com/WebAssembly/esm-integration

[Custom Annotation]: https://github.com/WebAssembly/annotations/blob/master/proposals/annotations/Overview.md

[HTML module loader]: https://html.spec.whatwg.org/multipage/webappapis.html#integration-with-the-javascript-module-system
[`TextEncoder`]: https://encoding.spec.whatwg.org/#interface-textencoder
[`TextDecoder`]: https://encoding.spec.whatwg.org/#interface-textdecoder
Expand Down

0 comments on commit 3119695

Please sign in to comment.