Skip to content

Commit

Permalink
[spec] Normative: Add i64<->BigInt conversion in JS API
Browse files Browse the repository at this point in the history
With this patch, BigInts have support for bidirectionally converting
to 64-bit integer WebAssembly values, which appears as
- Parameters and return values to exported WebAssembly functions
- Parameters and return values to host functions
- Imported and exported globals

BigInts can be read or written from WebAssembly memory using the
BigInt proposal's BigInt64Array or BigUint64Array; no additional
support is needed.

This patch includes basic conformance tests in jsapi.js, which tests
that the conversion is done properly in the six cases listed above.
I don't know of any Wasm implementation that attempts to implement
this specification, so I converted most of the test to be against
i32 and saw that converted version pass on V8 (this process helped
work out a few silly test bugs).

Closes WebAssembly/design#1172
  • Loading branch information
littledan authored and xtuc committed Oct 17, 2018
1 parent cf67a47 commit 2672899
Show file tree
Hide file tree
Showing 2 changed files with 979 additions and 5 deletions.
47 changes: 42 additions & 5 deletions document/js-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ Prepare For TR: true
"title": "WebAssembly Core Specification",
"publisher": "W3C WebAssembly Community Group",
"status": "Draft"
},
"BIGINT": {
"href": "https://tc39.github.io/proposal-bigint/",
"title": "BigInt Specification",
"publisher": "TC39",
"status": "Stage 3 proposal"
}
}
</pre>
Expand Down Expand Up @@ -160,11 +166,18 @@ urlPrefix: https://webassembly.github.io/spec/core/; spec: WebAssembly; type: df
text: var
text: const
text: address; url: exec/runtime.html#addresses
<<<<<<< HEAD
text: signed_32; url: exec/numerics.html#aux-signed
text: grow_memory; url: exec/instructions.html#exec-grow-memory
text: current frame; url: exec/conventions.html#exec-notation-textual
text: π—†π—ˆπ–½π—Žπ—…π–Ύ; url: exec/runtime.html#syntax-frame
text: π—†π–Ύπ—†π–Ίπ–½π–½π—‹π—Œ; url: exec/runtime.html#syntax-moduleinst
=======
text: signed_64; url: exec/numerics.html#aux-signed
urlPrefix: https://tc39.github.io/proposal-bigint/; spec: BIGINT; type: dfn
text: ToBigInt64; url: #sec-to-big-int64
text: BigInt; url: #sec-ecmascript-language-types-bigint-type
>>>>>>> [spec] Normative: Add i64<->BigInt conversion in JS API
</pre>

<pre class='link-defaults'>
Expand Down Expand Up @@ -334,6 +347,7 @@ A {{Module}} object represents a single WebAssembly module. Each {{Module}} obje
1. Let |index| be the number of external functions in |imports|. This value |index| is known as the <dfn>index of the host function</dfn> |funcaddr|.
1. Let |externfunc| be the [=external value=] [=external value|π–Ώπ—Žπ—‡π–Ό=] |funcaddr|.
1. [=Append=] |externfunc| to |imports|.
<<<<<<< HEAD
1. If |externtype| is of the form [=π—€π—…π—ˆπ–»π–Ίπ—…=] <var ignore>mut</var> |valtype|,
1. If [=Type=](|v|) is [=Number=],
1. If |valtype| is [=π—‚πŸ¨πŸ¦=], throw a {{LinkError}} exception.
Expand All @@ -345,6 +359,14 @@ A {{Module}} object represents a single WebAssembly module. Each {{Module}} obje
1. Let |globaladdr| be |v|.\[[Global]]
1. Otherwise,
1. Throw a {{LinkError}} exception.
=======
1. If |externtype| is of the form [=π—€π—…π—ˆπ–»π–Ίπ—…=] |globaltype|,
1. Let |value| be [=ToWebAssemblyValue=](|v|, |globaltype|.<em>[=global type|valtype=]</em>)
1. Assert: |globaltype|.<em>[=global type|mut=]</em> is [=global type|π–Όπ—ˆπ—‡π—Œπ—=], as verified by WebAssembly validation.
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
1. Let (|store|, |globaladdr|) be [=alloc_global=](|store|, |globaltype|, |value|).
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
>>>>>>> [spec] Normative: Add i64<->BigInt conversion in JS API
1. Let |externglobal| be [=external value|π—€π—…π—ˆπ–»π–Ίπ—…=] |globaladdr|.
1. [=Append=] |externglobal| to |imports|.
1. If |externtype| is of the form [=𝗆𝖾𝗆=] |memtype|,
Expand Down Expand Up @@ -376,7 +398,12 @@ A {{Module}} object represents a single WebAssembly module. Each {{Module}} obje
1. Let [=external value|π–Ώπ—Žπ—‡π–Ό=] |funcaddr| be |externval|.
1. Let |func| be the result of creating [=a new Exported Function=] from |funcaddr|.
1. Let |value| be |func|.
<<<<<<< HEAD
1. If |externtype| is of the form [=π—€π—…π—ˆπ–»π–Ίπ—…=] <var ignore>globaltype</var>,
=======
1. If |externtype| is of the form [=π—€π—…π—ˆπ–»π–Ίπ—…=] |globaltype|,
1. Assert: |globaltype|.<em>[=global type|mut=]</em> is [=global type|π–Όπ—ˆπ—‡π—Œπ—=], as verified by WebAssembly validation.
>>>>>>> [spec] Normative: Add i64<->BigInt conversion in JS API
1. Assert: |externval| is of the form [=external value|π—€π—…π—ˆπ–»π–Ίπ—…=] |globaladdr|.
1. Let [=external value|π—€π—…π—ˆπ–»π–Ίπ—…=] |globaladdr| be |externval|.
1. Let |global| be [=create a global object|a new Global object=] created from |globaladdr|.
Expand Down Expand Up @@ -875,10 +902,6 @@ This slot holds a [=function address=] relative to the [=surrounding agent=]'s [
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
1. Let |functype| be [=type_func=](|store|, |funcaddr|).
1. Let [|parameters|] β†’ [|results|] be |functype|.
1. If |parameters| or |results| contains an [=π—‚πŸ¨πŸ¦=], throw a {{TypeError}}.

Note: the above error is thrown each time the \[[Call]] method is invoked.

5. Let |args| be an empty list of WebAssembly values.
1. Let |i| be 0.
1. For each type |t| of |parameters|,
Expand All @@ -901,7 +924,6 @@ Note: Exported Functions do not have a \[[Construct]] method and thus it is not
To <dfn>create a host function</dfn> from the JavaScript object |func|, perform the following steps:

1. Let |hostfunc| be a [=host function=] which performs the following steps when called:
1. If the signature contains an [=π—‚πŸ¨πŸ¦=] (as argument or result), the host function throws a {{TypeError}} when called.
1. Let |arguments| be a [=list=] of the arguments of the invocation of this function.
1. Let |jsArguments| be an empty [=list=].
1. For each |arg| in |arguments|,
Expand All @@ -919,10 +941,19 @@ Note: Exported Functions do not have a \[[Construct]] method and thus it is not
<div algorithm>
The algorithm <dfn>ToJSValue</dfn>(|w|) coerces a [=WebAssembly value=] to a JavaScript value performs the following steps:

<<<<<<< HEAD
1. Assert: |w| is not of the form [=π—‚πŸ¨πŸ¦.π–Όπ—ˆπ—‡π—Œπ—=] <var ignore>i64</var>.
1. If |w| is of the form [=𝗂πŸ₯𝟀.π–Όπ—ˆπ—‡π—Œπ—=] |i32|, return [=the Number value=] for [=signed_32=](|i32|).
1. If |w| is of the form [=𝖿πŸ₯𝟀.π–Όπ—ˆπ—‡π—Œπ—=] |f32|, return [=the Number value=] for |f32|.
1. If |w| is of the form [=π–ΏπŸ¨πŸ¦.π–Όπ—ˆπ—‡π—Œπ—=] |f64|, return [=the Number value=] for |f64|.
=======
1. If |w| is of the form [=π—‚πŸ¨πŸ¦.π–Όπ—ˆπ—‡π—Œπ—=] |i64|,
1. Let |v| be [=signed_64=](|i64|).
1. Return a [=BigInt=] representing the mathematical value |v|.
1. If |w| is of the form [=𝗂πŸ₯𝟀.π–Όπ—ˆπ—‡π—Œπ—=] |i32|, return [=the Number value=] of |i32|.
1. If |w| is of the form [=𝖿πŸ₯𝟀.π–Όπ—ˆπ—‡π—Œπ—=] |f32|, return [=the Number value=] of |f32|.
1. If |w| is of the form [=π–ΏπŸ¨πŸ¦.π–Όπ—ˆπ—‡π—Œπ—=] |f64|, return [=the Number value=] of |f64|.
>>>>>>> [spec] Normative: Add i64<->BigInt conversion in JS API

<!-- If the WebAssembly value is optional, then given `None`, return JavaScript value `undefined`. -->

Expand All @@ -933,7 +964,13 @@ Note: Number values which are equal to NaN may have various observable NaN paylo
The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|) coerce a JavaScript value to a [=WebAssembly value=] performs the following steps:


<<<<<<< HEAD
1. Assert: |type| is not [=π—‚πŸ¨πŸ¦=].
=======
1. If |type| is [=π—‚πŸ¨πŸ¦=],
1. Let |i64| be [=ToBigInt64=](|v|).
1. Return [=π—‚πŸ¨πŸ¦.π–Όπ—ˆπ—‡π—Œπ—=] |i64|.
>>>>>>> [spec] Normative: Add i64<->BigInt conversion in JS API
1. If |type| is [=𝗂πŸ₯𝟀=],
1. Let |i32| be ? [=ToInt32=](|v|).
1. Return [=𝗂πŸ₯𝟀.π–Όπ—ˆπ—‡π—Œπ—=] |i32|.
Expand Down
Loading

0 comments on commit 2672899

Please sign in to comment.