Advertisement
kosx

Index.bs WEB-API

Dec 27th, 2020
842
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JSON 14.20 KB | None | 0 0
  1. <pre class='metadata'>
  2. Title: WebAssembly Web API
  3. Shortname: wasm-web-api
  4. Group: wasm
  5. Status: ED
  6. Level: 1
  7. TR: https://www.w3.org/TR/wasm-web-api-1/
  8. ED: https://webassembly.github.io/spec/web-api/
  9. Editor: Ms2ger (Igalia)
  10. Repository: WebAssembly/spec
  11. Abstract: This document describes the integration of WebAssembly with the broader web platform.
  12. Markup Shorthands: css no, markdown yes
  13. Prepare For TR: true
  14. </pre>
  15.  
  16. <pre class='biblio'>
  17. {
  18.   "ECMA-262": {
  19.     "href": "https://tc39.github.io/ecma262",
  20.     "title": "ECMAScript® Language Specification",
  21.     "publisher": "ECMA TC39",
  22.     "status": "Current Editor's Draft"
  23.   },
  24.   "WEBASSEMBLY": {
  25.     "href": "https://webassembly.github.io/spec/core/",
  26.     "title": "WebAssembly Core Specification",
  27.     "publisher": "W3C WebAssembly Community Group",
  28.     "status": "Draft"
  29.   },
  30.   "WASMJS": {
  31.     "href": "https://webassembly.github.io/spec/js-api/",
  32.     "title": "WebAssembly JS Integration Specification",
  33.     "publisher": "W3C WebAssembly Community Group",
  34.     "status": "Draft"
  35.   },
  36.   "SECURECONTEXTS": {
  37.     "href": "https://w3c.github.io/webappsec-secure-contexts/",
  38.     "title": "Secure Contexts",
  39.     "publisher": "WebAppSec WG",
  40.     "status": "Candidate Recommendation"
  41.   }
  42. }
  43. </pre>
  44.  
  45. <pre class="anchors">
  46. urlPrefix: https://tc39.github.io/ecma262/; spec: ECMA-262
  47.     type: exception; for: ECMAScript
  48.         text: TypeError; url: sec-native-error-types-used-in-this-standard-typeerror
  49.     type: interface
  50.         text: ArrayBuffer; url: sec-arraybuffer-objects
  51.     type: dfn
  52.         text: agent cluster; url: sec-agent-clusters
  53.         text: current Realm; url: current-realm
  54. urlPrefix: https://webassembly.github.io/spec/core/; spec: WebAssembly; type: dfn
  55.     text: function index; url: syntax/modules.html#syntax-funcidx
  56.     text: name section; url: appendix/custom.html?highlight=name%20section#binary-namesec
  57. urlPrefix: https://webassembly.github.io/spec/js-api/; spec: WASMJS
  58.     type: namespace
  59.         text: WebAssembly; url: #namespacedef-webassembly
  60.     type: exception
  61.         text: CompileError; url: #exceptiondef-compileerror
  62.     type: interface
  63.         text: Module; url: #module
  64.         text: WebAssemblyInstantiatedSource; url: #dictdef-webassemblyinstantiatedsource
  65.     type: dfn
  66.         text: compile a WebAssembly module; url: #compile-a-webassembly-module
  67.         text: instantiate a WebAssembly module; url: #instantiate-a-webassembly-module
  68.         text: instantiate; url: #dom-webassembly-instantiate
  69.         text: asynchronously compile a webassembly module; url: #asynchronously-compile-a-webassembly-module
  70.         text: instantiate a promise of a module; url: #instantiate-a-promise-of-a-module
  71.         text: Exported Function; url: #exported-function
  72. url:https://html.spec.whatwg.org/#cors-same-origin;text:CORS-same-origin;type:dfn;spec:HTML
  73. url:https://fetch.spec.whatwg.org/#concept-body-consume-body;text:consume body;type:dfn;spec:FETCH
  74. url:https://w3c.github.io/webappsec-secure-contexts/#environment-settings-object-contextually-secure; text:contextually secure; type: dfn; spec: SECURECONTEXTS
  75. </pre>
  76.  
  77. <pre class='link-defaults'>
  78. spec:ecma-262; type:exception; for:ECMAScript; text:TypeError
  79. spec:webidl; type:dfn; text:resolve
  80. </pre>
  81.  
  82. This document builds off of the WebAssembly specification [[WEBASSEMBLY]] and the WebAssembly JavaScript embedding [[WASMJS]].
  83.  
  84. <h2 id="streaming-modules">Streaming Module Compilation and Instantiation</h2>
  85.  
  86. <pre class="idl">
  87. partial namespace WebAssembly {
  88.   Promise&lt;Module> compileStreaming(Promise&lt;Response> source);
  89.   Promise&lt;WebAssemblyInstantiatedSource> instantiateStreaming(
  90.       Promise&lt;Response> source, optional object importObject);
  91. };
  92. </pre>
  93.  
  94. <div algorithm>
  95. The <dfn method for="WebAssembly">compileStreaming(|source|)</dfn> method, when invoked, returns the result of [=compile a potential WebAssembly response|compiling a potential WebAssembly response=] with |source|.
  96. </div>
  97.  
  98. <div algorithm>
  99. The <dfn method for="WebAssembly">instantiateStreaming(|source|, |importObject|)</dfn> method, when invoked, performs the following steps:
  100.  
  101.     1. Let |promiseOfModule| be the result of [=compile a potential WebAssembly response|compiling a potential WebAssembly response=] with |source|.
  102.     1. Return the result of [=instantiate a promise of a module|instantiating the promise of a module=] |promiseOfModule| with imports |importObject|.
  103. </div>
  104.  
  105. <div algorithm>
  106. To <dfn>compile a potential WebAssembly response</dfn> with a promise of a {{Response}} |source|, perform the following steps:
  107.  
  108. Note: This algorithm accepts a {{Response}} object, or a
  109.     promise for one, and compiles and instantiates the resulting bytes of the response. This compilation
  110.     can be performed in the background and in a streaming manner. If the {{Response}} is not
  111.     [=CORS-same-origin=], does not represent an [=ok status=], or does not match the
  112.     `` `application/wasm` `` MIME type, the returned promise will be rejected with a {{TypeError}}; if
  113.     compilation or instantiation fails, the returned promise will be rejected with a
  114.     {{CompileError}} or other relevant error type, depending on the cause of failure.
  115.  
  116.  
  117.     1. Let |returnValue| be [=a new promise=]
  118.     1. [=Upon fulfillment=] of |source| with value |unwrappedSource|:
  119.         1. Let |response| be |unwrappedSource|'s [=Response/response=].
  120.        1. Let |mimeType| be the result of [=extracting a MIME type=] from |response|'s [=response/header list=].
  121.         1. If |mimeType| is not `` `application/wasm` ``, reject |returnValue| with a {{TypeError}} and abort these substeps.
  122.  
  123.             Note: extra parameters are not allowed, including the empty `` `application/wasm;` ``.
  124.  
  125.         1. If |response| is not [=CORS-same-origin=], [=reject=] |returnValue| with a {{TypeError}} and abort these substeps.
  126.         1. If |response|'s [=response/status=] is not an [=ok status=], [=reject=] |returnValue| with a {{TypeError}} and abort these substeps.
  127.        1. [=Consume body|consume=] |response|'s body as an {{ArrayBuffer}}, and let |bodyPromise| be the result.
  128.  
  129.             Note: Although it is specified here that the response is consumed entirely before compilation proceeds, that is purely for ease of specification; implementations are likely to instead perform processing in a streaming fashion. The difference is unobservable, and thus the simpler model is specified. <!-- Using consume is a bit silly as it creates an ArrayBuffer but then we just want the underlying bytes. This is because of how streams is specced in terms of promises and JS objects whereas we want to operate more directly on the underlying concept. We can revisit this if things change in the Streams/Fetch specs. -->
  130.  
  131.         1. [=Upon fulfillment=] of |bodyPromise| with value |bodyArrayBuffer|:
  132.             1. Let |stableBytes| be a [=get a copy of the buffer source|copy of the bytes held by the buffer=] |bodyArrayBuffer|.
  133.             1. [=Asynchronously compile a WebAssembly module|Asynchronously compile the WebAssembly module=] |stableBytes| using the [=networking task source=] and [=resolve=] |returnValue| with the result.
  134.         1. [=Upon rejection=] of |bodyPromise| with reason |reason|:
  135.             1. [=Reject=] |returnValue| with |reason|.
  136.      1. [=Upon rejection=] of |source| with reason |reason|:
  137.          1. [=Reject=] |returnValue| with |reason|.
  138.      1. Return |returnValue|.
  139. </div>
  140.  
  141. <h2 id="serialization">Serialization</h2>
  142.  
  143. Web user agents must augment the {{Module}} interface with the <code>[<a extended-attribute>Serializable</a>]</code> extended attribute.
  144.  
  145. The [=serialization steps=], given |value|, |serialized|, and |forStorage|, are:
  146.  
  147.     1. If |forStorage| is true, throw a "<a exception>DataCloneError</a>" {{DOMException}}
  148.  
  149.     1. Set |serialized|.\[[Bytes]] to the [=sub-serialization=] of |value|.\[[Bytes]].
  150.  
  151.     1. Set |serialized|.\[[AgentCluster]] to the [=current Realm=]'s corresponding [=agent cluster=].
  152.  
  153. The [=deserialization steps=], given |serialized| and |value|, are:
  154.  
  155.    1. Let |bytes| be the [=sub-deserialization=] of |serialized|.\[[Bytes]].
  156.  
  157.    1. Set |value|.\[[Bytes]] to |bytes|.
  158.  
  159.    1. If |targetRealm|'s corresponding [=agent cluster=] is not |serialized|.\[[AgentCluster]], then throw a "<a exception>DataCloneError</a>" {{DOMException}}.
  160.  
  161.     1. [=Compile a WebAssembly module=] from |bytes| and set |value|.\[[Module]] to the result.
  162.  
  163. Engines should attempt to share/reuse internal compiled code when performing
  164. a structured serialization, although in corner cases like CPU upgrade or browser
  165. update, this might not be possible and full recompilation may be necessary.
  166.  
  167. Note: The semantics of a structured serialization is as-if the binary source, from which the
  168. {{Module}} was compiled, is serialized, then deserialized, and recompiled into the target realm.
  169. Given the above engine optimizations, structured serialization provides developers
  170. explicit control over both compiled-code caching and cross-window/worker code
  171. sharing.
  172.  
  173. <h2 id="conventions">Developer-Facing Display Conventions</h2>
  174.  
  175. <em>This section is non-normative.</em>
  176.  
  177. Browsers, JavaScript engines, and offline tools have common ways of referring to
  178. JavaScript artifacts and language constructs. For example, locations in
  179. JavaScript source code are printed in stack traces or error messages, and are
  180. represented naturally as decimal-format lines and columns in text files. Names
  181. of functions and variables are taken directly from the sources. Therefore (for
  182. example) even though the exact format of implementation-dependent stack trace
  183. strings does not always match, the locations are easily understandable and the
  184. same across browsers.
  185.  
  186. To achieve the same goal of a common representations for WebAssembly constructs, the
  187. following conventions are adopted.
  188.  
  189. A WebAssembly location is a reference to a particular instruction in the binary, and may be
  190. displayed by a browser or engine in similar contexts as JavaScript source locations.
  191. It has the following format:
  192.  
  193. `${url}:wasm-function[${funcIndex}]:${pcOffset}`
  194.  
  195. Where
  196. * `${url}` is the URL associated with the module, if applicable (see notes).
  197. * `${funcIndex}` is the [=function index=] relative to the module.
  198. * `${pcOffset}` is the offset in the module binary of the first byte of the instruction, printed in hexadecimal with lower-case digits, with a leading `0x` prefix.
  199.  
  200. Notes:
  201. * The URL field may be interpreted differently depending on the
  202.     context. When the response-based
  203.     instantiation <a href="#streaming-modules">API</a> is used in a
  204.     browser, the associated URL should be used; or when the
  205.     {{ArrayBuffer}}-based instantiation
  206.     [=instantiate|API=] is used, the browser should represent
  207.     the location of the API call. This kind of instantiation is analogous to
  208.     executing JavaScript using `eval`; therefore if the browser has an existing
  209.     method to represent the location of the `eval` call it can use a similar
  210.     one for `WebAssembly.instantiate`. For example if the browser uses
  211.     `foo.js line 10 > eval` or `eval at bar (foo.js:10:3)` for `eval`, it could
  212.     use `foo.js line 10 > WebAssembly.instantiate` or
  213.     `WebAssembly.instantiate at bar (foo.js:10:3)`, respectively.
  214.     Offline tools may use a filename instead.
  215. * Using hexadecimal for module offsets matches common conventions in native tools
  216.     such as `objdump` (where addresses are printed in hex) and makes them visually
  217.     distinct from JavaScript line numbers. Other numbers are represented in decimal.
  218.  
  219. While the "name" property of an [=Exported Function=] instance
  220. is specified by the [[WASMJS|JS API]], synthesized function names are also
  221. displayed in other contexts like call stacks in debuggers and string representations
  222. of stack traces.
  223. If a WebAssembly module contains a [=name section=],
  224. these names should be used to synthesize a function name as follows:
  225. * If a function name subsection is present, the displayed name should be `${module_name}.${function_name}` or `${function_name}`, depending on whether the module name is present.
  226. * Otherwise, the output can be context-dependent:
  227.     * If the function name is shown alongside its location in a stack trace, then just the module name (if present) or an empty string can be used (because the function index is already in the location).
  228.     * Otherwise, `${module_name}.wasm-function[${funcIndex}]` or `wasm-function[${funcIndex}]` should be used to convey the function index.
  229.  
  230. Note that this document does not specify the full format of strings such as
  231. stack frame representations; this allows engines to continue using their
  232. existing formats for JavaScript (which existing code may already be depending
  233. on) while still printing WebAssembly frames in a format consistent with
  234. JavaScript.
  235.  
  236. <h2 id="mediaType">Media-type Registration</h2>
  237.  
  238. This section will be submitted to the Internet Engineering Steering Group (IESG) for
  239. review, approval, and registration with IANA.
  240.  
  241. application/wasm
  242.  
  243. <dl>
  244. <dt>Type name:</dt>
  245.  <dd>application</dd>
  246. <dt>Subtype name:</dt>
  247.  <dd>wasm</dd>
  248. <dt>Required parameters:</dt>
  249.  <dd>None</dd>
  250. <dt>Optional parameters:</dt>
  251.  <dd>None</dd>
  252. <dt>Encoding considerations:</dt>
  253.  <dd>None (this is a binary format)</dd>
  254. <dt>Security considerations:</dt>
  255.  <dd>See see WebAssembly Core Security Considerations<br/>
  256.  https://www.w3.org/TR/wasm-core/#security-considerations%E2%91%A0</dd>
  257. <dt>Interoperability considerations:</dt>
  258.  <dd>See see WebAssembly Core Conformance<br/>
  259.  https://www.w3.org/TR/wasm-core/#conformance</dd>
  260. <dt>Published specification:</dt>
  261.  https://www.w3.org/TR/wasm-core-1/
  262.  https://www.w3.org/TR/wasm-js-api-1/
  263.  https://www.w3.org/TR/wasm-web-api-1/
  264. <dt>Applications that use this media type:</dt>
  265.  <dd>Web browsers and runtime systems</dd>
  266. <dt>Additional information:</dt>
  267. <dd><dl>
  268.  <dt>Magic number(s):</dt>
  269.  <dd>0x00 0x61 0x73 0x6D</dd>
  270.  <dt>File extension(s):</dt>
  271.  <dd>.wasm</dd>
  272.  <dt>Macintosh file type code(s):</dt>
  273.  <dd>None</dd>
  274. </dl></dd>
  275. <dt>Person & email address to contact for further information:</dt>
  276.  <dd>Eric Prud'hommeaux &lt;eric@w3.org></dd>
  277. <dt>Intended usage:</dt>
  278. <dd>Common</dd>
  279. <dt>Restrictions on usage:</dt>
  280. <dd>None</dd>
  281. <dt>Author(s):</dt>
  282. <dd>W3C's Web Assembly Working Group</dd>
  283. <dt>Change controller:</dt>
  284.  <dd>W3C</dd>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement