Skip to content

Commit 04c63fb

Browse files
pizlonatorjfbastien
authored andcommitted
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.
1 parent 0ac338d commit 04c63fb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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)