Skip to content

Commit 54da3d5

Browse files
committed
Extensible encoding of function signatures (#640)
* Prettify section names * Restructure encoding of function signatures * Revert "[Binary 11] Update the version number to 0xB." * Leave index space for growing the number of base types * Comments addressed * clarify how export/import names convert to JS strings (#569) (#573) * When embedded in the web, clarify how export/import names convert to JS strings (#569) * Fixes suggested by @jf * Address more feedback Added a link to http://monsur.hossa.in/2012/07/20/utf-8-in-javascript.html. Simplified the decoding algorithm thanks to Luke's feedback. * Access to proprietary APIs apart from HTML5 (#656) * comments
1 parent 07c9074 commit 54da3d5

File tree

3 files changed

+51
-6
lines changed

3 files changed

+51
-6
lines changed

BinaryEncoding.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ A four-byte little endian unsigned integer.
3535
### varint32
3636
A [Signed LEB128](https://en.wikipedia.org/wiki/LEB128#Signed_LEB128) variable-length integer, limited to int32 values.
3737

38+
### varuint1
39+
A [LEB128](https://en.wikipedia.org/wiki/LEB128) variable-length integer, limited to the values 0 or 1. `varuint1` values may contain leading zeros. (This type is mainly used for compatibility with potential future extensions.)
40+
3841
### varuint32
3942
A [LEB128](https://en.wikipedia.org/wiki/LEB128) variable-length integer, limited to uint32 values. `varuint32` values may contain leading zeros.
4043

@@ -75,8 +78,8 @@ The module starts with a preamble of two fields:
7578

7679
| Field | Type | Description |
7780
| ----- | ----- | ----- |
78-
| magic number | `uint32` | Magic number `0x6d736100` == `'\0asm'`. |
79-
| version | `uint32` | Version number `11` == `0x0b`. The version for MVP will be reset to `1`. |
81+
| magic number | `uint32` | Magic number `0x6d736100` (i.e., '\0asm') |
82+
| version | `uint32` | Version number, currently 10. The version for MVP will be reset to 1. |
8083

8184
This preamble is followed by a sequence of sections. Each section is identified by an
8285
immediate string. Sections whose identity is unknown to the WebAssembly
@@ -117,15 +120,19 @@ The type section declares all function signatures that will be used in the modul
117120

118121
| Field | Type | Description |
119122
| ----- | ----- | ----- |
120-
| count | `varuint32` | count of signature entries to follow |
123+
| count | `varuint32` | count of type entries to follow |
121124
| entries | `type_entry*` | repeated type entries as described below |
122125

123126
#### Type entry
124127
| Field | Type | Description |
125128
| ----- | ----- | ----- |
129+
| form | `uint8` | `0x40`, indicating a function type |
126130
| param_count | `varuint32` | the number of parameters to the function |
127-
| return_type | `value_type?` | the return type of the function, with `0` indicating no return type |
128131
| param_types | `value_type*` | the parameter types of the function |
132+
| return_count | `varuint1` | the number of results from the function |
133+
| return_type | `value_type?` | the result type of the function (if return_count is 1) |
134+
135+
(Note: In the future, this section may contain other forms of type entries as well, which can be distinguished by the `form` field.)
129136

130137
### Import section
131138

@@ -216,7 +223,7 @@ The start section declares the [start function](Modules.md#module-start-function
216223

217224
ID: `code`
218225

219-
The code section assigns a body to every function in the module.
226+
The code section contains a body for every function in the module.
220227
The count of function declared in the [function section](#function-section)
221228
and function bodies defined in this section must be the same and the `i`th
222229
declaration corresponds to the `i`th function body.
@@ -230,7 +237,7 @@ declaration corresponds to the `i`th function body.
230237

231238
ID: `data`
232239

233-
The data section declares the initialized data that should be loaded
240+
The data section declares the initialized data that is loaded
234241
into the linear memory.
235242

236243
| Field | Type | Description |

FAQ.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,3 +389,12 @@ those that motivated the development of the
389389

390390
Even Knuth found it worthwhile to give us his opinion on this issue at point,
391391
[a flame about 64-bit pointers](http://www-cs-faculty.stanford.edu/~uno/news08.html).
392+
393+
## Will I be able to access proprietary platform APIs (e.g. Android / iOS)?
394+
395+
Yes but it will depend on the _WebAssembly embedder_. Inside a browser you'll
396+
get access to the same HTML5 and other browser-specific APIs which are also
397+
accessible through regular JavaScript. However, if a wasm VM is provided as an
398+
[“app execution platform”](NonWeb.md) by a specific vendor, it might provide
399+
access to [proprietary platform-specific APIs](Portability.md#api) of e.g.
400+
Android / iOS.

Web.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,35 @@ WebAssembly's [modules](Modules.md) allow for natural [integration with
2828
the ES6 module system](Modules.md#integration-with-es6-modules) and allow
2929
synchronous calling to and from JavaScript.
3030

31+
### Function Names
32+
33+
A WebAssembly module imports and exports functions. WebAssembly names functions
34+
using arbitrary-length byte sequences. Any 8-bit values are permitted in a
35+
WebAssembly name, including the null byte and byte sequences that don't
36+
correspond to any Unicode code point regardless of encoding. The most natural
37+
Web representation of a mapping of function names to functions is a JS object
38+
in which each function is a property. Property names in JS are UTF-16 encoded
39+
strings. A WebAssembly module may fail validation on the Web if it imports or
40+
exports functions whose names do not transcode cleanly to UTF-16 according to
41+
the following conversion algorithm, assuming that the WebAssembly name is in a
42+
`Uint8Array` called `array`:
43+
44+
```
45+
function convertToJSString(array)
46+
{
47+
var string = "";
48+
for (var i = 0; i < array.length; ++i)
49+
string += String.fromCharCode(array[i]);
50+
return decodeURIComponent(escape(string));
51+
}
52+
```
53+
54+
This performs the UTF8 decoding (`decodeURIComponent(unescape(string))`) using
55+
a [common JS idiom](http://monsur.hossa.in/2012/07/20/utf-8-in-javascript.html).
56+
Transcoding failure is detected by `decodeURIComponent`, which may throw
57+
`URIError`. If it does, the WebAssembly module will not validate. This validation
58+
rule is only mandatory for Web embedding.
59+
3160
## Aliasing linear memory from JS
3261

3362
If [allowed by the module](Modules.md#linear-memory-section), JavaScript can

0 commit comments

Comments
 (0)