Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

Commit

Permalink
Merge upstream reftypes proposal (WebAssembly#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
rossberg committed Feb 15, 2021
2 parents 008e069 + dd5a6a6 commit e2aaaae
Show file tree
Hide file tree
Showing 23 changed files with 689 additions and 71 deletions.
2 changes: 1 addition & 1 deletion document/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ You will also need `npm` and `yarn` for all the LaTeX goodness. `npm` might alre

```
npm install -g yarn
cd document/core
cd document
make -C core bikeshed
```

Expand Down
2 changes: 1 addition & 1 deletion document/core/appendix/gen-index-instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def Instruction(name, opcode, type=None, validation=None, execution=None, operat
Instruction(r'\I32.\TRUNC\K{\_sat\_}\F64\K{\_u}', r'\hex{FC}~\hex{03}', r'[\F64] \to [\I32]', r'valid-cvtop', r'exec-cvtop', r'op-trunc_sat_u'),
Instruction(r'\I64.\TRUNC\K{\_sat\_}\F32\K{\_s}', r'\hex{FC}~\hex{04}', r'[\F32] \to [\I64]', r'valid-cvtop', r'exec-cvtop', r'op-trunc_sat_s'),
Instruction(r'\I64.\TRUNC\K{\_sat\_}\F32\K{\_u}', r'\hex{FC}~\hex{05}', r'[\F32] \to [\I64]', r'valid-cvtop', r'exec-cvtop', r'op-trunc_sat_u'),
Instruction(r'\I64.\TRUNC\K{\_sat}\_\F64\K{\_s}', r'\hex{FC}~\hex{06}', r'[\F64] \to [\I64]', r'valid-cvtop', r'exec-cvtop', r'op-trunc_sat_s'),
Instruction(r'\I64.\TRUNC\K{\_sat\_}\F64\K{\_s}', r'\hex{FC}~\hex{06}', r'[\F64] \to [\I64]', r'valid-cvtop', r'exec-cvtop', r'op-trunc_sat_s'),
Instruction(r'\I64.\TRUNC\K{\_sat\_}\F64\K{\_u}', r'\hex{FC}~\hex{07}', r'[\F64] \to [\I64]', r'valid-cvtop', r'exec-cvtop', r'op-trunc_sat_u'),
Instruction(r'\MEMORYINIT', r'\hex{FC}~\hex{08}', r'[\I32~\I32~\I32] \to []', r'valid-memory.init', r'exec-memory.init'),
Instruction(r'\DATADROP', r'\hex{FC}~\hex{09}', r'[] \to []', r'valid-data.drop', r'exec-data.drop'),
Expand Down
2 changes: 1 addition & 1 deletion document/core/appendix/index-instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ Instruction Binary Opcode Type
:math:`\I32.\TRUNC\K{\_sat\_}\F64\K{\_u}` :math:`\hex{FC}~\hex{03}` :math:`[\F64] \to [\I32]` :ref:`validation <valid-cvtop>` :ref:`execution <exec-cvtop>`, :ref:`operator <op-trunc_sat_u>`
:math:`\I64.\TRUNC\K{\_sat\_}\F32\K{\_s}` :math:`\hex{FC}~\hex{04}` :math:`[\F32] \to [\I64]` :ref:`validation <valid-cvtop>` :ref:`execution <exec-cvtop>`, :ref:`operator <op-trunc_sat_s>`
:math:`\I64.\TRUNC\K{\_sat\_}\F32\K{\_u}` :math:`\hex{FC}~\hex{05}` :math:`[\F32] \to [\I64]` :ref:`validation <valid-cvtop>` :ref:`execution <exec-cvtop>`, :ref:`operator <op-trunc_sat_u>`
:math:`\I64.\TRUNC\K{\_sat}\_\F64\K{\_s}` :math:`\hex{FC}~\hex{06}` :math:`[\F64] \to [\I64]` :ref:`validation <valid-cvtop>` :ref:`execution <exec-cvtop>`, :ref:`operator <op-trunc_sat_s>`
:math:`\I64.\TRUNC\K{\_sat\_}\F64\K{\_s}` :math:`\hex{FC}~\hex{06}` :math:`[\F64] \to [\I64]` :ref:`validation <valid-cvtop>` :ref:`execution <exec-cvtop>`, :ref:`operator <op-trunc_sat_s>`
:math:`\I64.\TRUNC\K{\_sat\_}\F64\K{\_u}` :math:`\hex{FC}~\hex{07}` :math:`[\F64] \to [\I64]` :ref:`validation <valid-cvtop>` :ref:`execution <exec-cvtop>`, :ref:`operator <op-trunc_sat_u>`
:math:`\MEMORYINIT` :math:`\hex{FC}~\hex{08}` :math:`[\I32~\I32~\I32] \to []` :ref:`validation <valid-memory.init>` :ref:`execution <exec-memory.init>`
:math:`\DATADROP` :math:`\hex{FC}~\hex{09}` :math:`[] \to []` :ref:`validation <valid-data.drop>` :ref:`execution <exec-data.drop>`
Expand Down
2 changes: 1 addition & 1 deletion document/core/binary/instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Table Instructions
\hex{26}~~x{:}\Btableidx &\Rightarrow& \TABLESET~x \\ &&|&
\hex{FC}~~12{:}\Bu32~~y{:}\Belemidx~~x{:}\Btableidx &\Rightarrow& \TABLEINIT~x~y \\ &&|&
\hex{FC}~~13{:}\Bu32~~x{:}\Belemidx &\Rightarrow& \ELEMDROP~x \\ &&|&
\hex{FC}~~14{:}\Bu32~~x{:}\Btableidx~~y{:}\Btableidx &\Rightarrow& \TABLECOPY~x~y \\
\hex{FC}~~14{:}\Bu32~~x{:}\Btableidx~~y{:}\Btableidx &\Rightarrow& \TABLECOPY~x~y \\ &&|&
\hex{FC}~~15{:}\Bu32~~x{:}\Btableidx &\Rightarrow& \TABLEGROW~x \\ &&|&
\hex{FC}~~16{:}\Bu32~~x{:}\Btableidx &\Rightarrow& \TABLESIZE~x \\ &&|&
\hex{FC}~~17{:}\Bu32~~x{:}\Btableidx &\Rightarrow& \TABLEFILL~x \\
Expand Down
4 changes: 2 additions & 2 deletions document/core/exec/instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ Memory Instructions

a. Let :math:`n` be the integer for which :math:`\bytes_{\iN}(n) = b^\ast`.

b. Let :math:`c` be the result of computing :math:`\extend\F{\_}\sx_{N,|t|}(n)`.
b. Let :math:`c` be the result of computing :math:`\extend^{\sx}_{N,|t|}(n)`.

13. Else:

Expand All @@ -1009,7 +1009,7 @@ Memory Instructions
\\[1ex]
\begin{array}{lcl@{\qquad}l}
S; F; (\I32.\CONST~i)~(t.\LOAD{N}\K{\_}\sx~\memarg) &\stepto&
S; F; (t.\CONST~\extend\F{\_}\sx_{N,|t|}(n))
S; F; (t.\CONST~\extend^{\sx}_{N,|t|}(n))
\end{array}
\\ \qquad
\begin{array}[t]{@{}r@{~}l@{}}
Expand Down
6 changes: 3 additions & 3 deletions document/core/syntax/instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ Instructions in this group are concerned with tables :ref:`table <syntax-table>`
\TABLESIZE~\tableidx \\&&|&
\TABLEGROW~\tableidx \\&&|&
\TABLEFILL~\tableidx \\&&|&
\TABLECOPY \\&&|&
\TABLEINIT~\elemidx \\&&|&
\TABLECOPY~\tableidx~\tableidx \\&&|&
\TABLEINIT~\tableidx~\elemidx \\&&|&
\ELEMDROP~\elemidx \\
\end{array}
Expand All @@ -281,7 +281,7 @@ It also takes an initialization value for the newly allocated entries.

The |TABLEFILL| instruction sets all entries in a range to a given value.

The |TABLECOPY| instruction copies elements from a source table region to a possibly overlapping destination region.
The |TABLECOPY| instruction copies elements from a source table region to a possibly overlapping destination region; the first index denotes the destination.
The |TABLEINIT| instruction copies elements from a :ref:`passive element segment <syntax-elem>` into a table.
The |ELEMDROP| instruction prevents further use of a passive element segment. This instruction is intended to be used as an optimization hint. After an element segment is dropped its elements can no longer be retrieved, so the memory used by this segment may be freed.

Expand Down
2 changes: 1 addition & 1 deletion document/core/text/instructions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ Table Instructions
\text{table.set}~~x{:}\Ttableidx_I &\Rightarrow& \TABLESET~x \\ &&|&
\text{table.size}~~x{:}\Ttableidx_I &\Rightarrow& \TABLESIZE~x \\ &&|&
\text{table.grow}~~x{:}\Ttableidx_I &\Rightarrow& \TABLEGROW~x \\ &&|&
\text{table.fill}~~x{:}\Ttableidx_I &\Rightarrow& \TABLEFILL~x \\
\text{table.fill}~~x{:}\Ttableidx_I &\Rightarrow& \TABLEFILL~x \\ &&|&
\text{table.copy}~~x{:}\Ttableidx_I~~y{:}\Ttableidx_I &\Rightarrow& \TABLECOPY~x~y \\ &&|&
\text{table.init}~~x{:}\Ttableidx_I~~y{:}\Telemidx_I &\Rightarrow& \TABLEINIT~x~y \\ &&|&
\text{elem.drop}~~x{:}\Telemidx_I &\Rightarrow& \ELEMDROP~x \\
Expand Down
40 changes: 18 additions & 22 deletions document/js-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ urlPrefix: https://webassembly.github.io/reference-types/core/; spec: WebAssembl
text: f32
text: f64
url: syntax/types.html#syntax-reftype
text: reftype
text: funcref
text: externref
text: function element; url: exec/runtime.html#syntax-funcelem
Expand Down Expand Up @@ -740,14 +741,9 @@ interface Table {
};
</pre>

<div>
A {{Table}} object represents a single [=table instance=]
which can be simultaneously referenced by multiple {{Instance}} objects.
Each {{Table}} object has the following internal slots:

* \[[Table]] : a [=table address=]
* \[[Values]] : a [=list=] whose elements depend on the element type of \[[Table]]'s [=table type=].
</div>
A {{Table}} object represents a single [=table instance=] which can be simultaneously referenced by
multiple {{Instance}} objects.
Each {{Table}} object has a \[[Table]] internal slot, which is a [=table address=].

<div algorithm>
To <dfn>initialize a table object</dfn> |table| from a [=table address=] |tableaddr|, perform the following steps:
Expand All @@ -770,7 +766,9 @@ Each {{Table}} object has the following internal slots:
<div algorithm>
The <dfn constructor for="Table">Table(|descriptor|, |value|)</dfn> constructor, when invoked, performs the following steps:
1. Let |elementType| be [=ToValueType=](|descriptor|["element"]).
1. let |initial| be |descriptor|["initial"].
1. If |elementType| is not a [=reftype=],
1. [=Throw=] a {{TypeError}} exception.
1. Let |initial| be |descriptor|["initial"].
1. If |descriptor|["maximum"] [=map/exists=], let |maximum| be |descriptor|["maximum"]; otherwise, let |maximum| be empty.
1. If |maximum| is not empty and |maximum| &lt; |initial|, throw a {{RangeError}} exception.
1. If |value| is missing,
Expand All @@ -782,8 +780,6 @@ Each {{Table}} object has the following internal slots:
1. Let (|store|, |tableaddr|) be [=table_alloc=](|store|, |type|, |ref|). <!-- TODO(littledan): Report allocation failure https://github.com/WebAssembly/spec/issues/584 -->
1. Set the [=surrounding agent=]'s [=associated store=] to |store|.
1. [=initialize a table object|Initialize=] **this** from |tableaddr|.
1. [=list/Empty=] **this**.\[[Values]].
1. [=list/Append=] |value| to **this**.\[[Values]] |initial| times.
</div>

<div algorithm=dom-Table-grow>
Expand All @@ -802,7 +798,6 @@ Each {{Table}} object has the following internal slots:
Note: The above exception can happen due to either insufficient memory or an invalid size parameter.

1. Set the [=surrounding agent=]'s [=associated store=] to |result|.
1. [=list/Append=] |value| to **this**.\[[Values]] |delta| times.
1. Return |initialSize|.
</div>

Expand All @@ -819,14 +814,12 @@ Each {{Table}} object has the following internal slots:
1. Let |store| be the [=surrounding agent=]'s [=associated store=].
1. Let |result| be [=table_read=](|store|, |tableaddr|, |index|).
1. If |result| is [=error=], throw a {{RangeError}} exception.
1. Let |function| be the result of creating [=a new Exported Function=] from |result|.
1. Return |function|.
1. Return [=ToJSValue=](|result|).
</div>

<div algorithm>
The <dfn method for="Table">set(|index|, |value|)</dfn> method, when invoked, performs the following steps:
1. Let |tableaddr| be **this**.\[[Table]].
1. Let |values| be **this**.\[[Values]].
1. Let (<var ignore>limits</var>, |elementType|) be [=table_type=](|tableaddr|).
1. If |value| is missing,
1. Let |ref| be [=DefaultValue=](|elementType|).
Expand Down Expand Up @@ -897,7 +890,7 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
1. If |s| equals "i64", return [=i64=].
1. If |s| equals "f32", return [=f32=].
1. If |s| equals "f64", return [=f64=].
1. If |s| equals "funcref", return [=funcref=].
1. If |s| equals "anyfunc", return [=funcref=].
1. If |s| equals "externref", return [=externref=].
1. Assert: This step is not reached.
</div>
Expand All @@ -908,6 +901,7 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
1. If |valuetype| equals [=i64=], return [=i64.const=] 0.
1. If |valuetype| equals [=f32=], return [=f32.const=] 0.
1. If |valuetype| equals [=f64=], return [=f64.const=] 0.
1. If |valuetype| equals [=funcref=], return [=ref.null=] [=funcref=].
1. If |valuetype| equals [=externref=], return [=ToWebAssemblyValue=](undefined, |valuetype|).
1. Assert: This step is not reached.
</div>
Expand All @@ -917,7 +911,7 @@ which can be simultaneously referenced by multiple {{Instance}} objects. Each
1. Let |mutable| be |descriptor|["mutable"].
1. Let |valuetype| be [=ToValueType=](|descriptor|["value"]).
1. If |v| is missing,
1. let |value| be [=DefaultValue=](|valuetype|).
1. Let |value| be [=DefaultValue=](|valuetype|).
1. Otherwise,
1. Let |value| be [=ToWebAssemblyValue=](|v|, |valuetype|).
1. If |mutable| is true, let |globaltype| be [=var=] |valuetype|; otherwise, let |globaltype| be [=const=] |valuetype|.
Expand Down Expand Up @@ -1088,10 +1082,13 @@ Note: Number values which are equal to NaN may have various observable NaN paylo
</div>

<div algorithm>
For <dfn>retrieving an extern value</dfn> from a [=extern address=] |externaddr|, perform the following steps:
1. Let |map| be the [=surrounding agent=]'s associated [=extern value cache=].
1. Assert: |map|[|externaddr|] [=map/exists=].
1. Return |map|[|externaddr|].

For <dfn>retrieving an extern value</dfn> from an [=extern address=] |externaddr|, perform the following steps:

1. Let |map| be the [=surrounding agent=]'s associated [=extern value cache=].
1. Assert: |map|[|externaddr|] [=map/exists=].
1. Return |map|[|externaddr|].

</div>

<div algorithm>
Expand Down Expand Up @@ -1129,7 +1126,6 @@ The algorithm <dfn>ToWebAssemblyValue</dfn>(|v|, |type|) coerces a JavaScript va

</div>


<h3 id="error-objects">Error Objects</h3>

WebAssembly defines the following Error classes: <dfn exception>CompileError</dfn>, <dfn exception>LinkError</dfn>, and <dfn exception>RuntimeError</dfn>.
Expand Down
22 changes: 16 additions & 6 deletions document/web-api/index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,27 @@ application/wasm
<dt>Encoding Considerations:</dt>
<dd>binary</dd>
<dt>Security Considerations:</dt>
<dd>See see WebAssembly Core Security Considerations<br/>
https://www.w3.org/TR/wasm-core/#security-considerations%E2%91%A0</dd>
<dd>
<p>WebAssembly is a standard, a safe, portable, low-level code format. The
security considerations associated with executing WebAssembly code are
described in https://www.w3.org/TR/wasm-core/#security-considerations.</p>
<p>The WebAssembly format includes no integrity or privacy protection. If
such protection is needed it must be provided externally, e.g., through
the use of HTTPS.</p>
</dd>
<dt>Interoperability Considerations:</dt>
<dd>See see WebAssembly Core Conformance<br/>
<dd>See WebAssembly Core Conformance<br/>
https://www.w3.org/TR/wasm-core/#conformance</dd>
<dt>Published specification:</dt>
https://www.w3.org/TR/wasm-core-1/
<dd>https://www.w3.org/TR/wasm-core-1/
https://www.w3.org/TR/wasm-js-api-1/
https://www.w3.org/TR/wasm-web-api-1/
https://www.w3.org/TR/wasm-web-api-1/</dd>
<dt>Application Usage:</dt>
<dd>The application/wasm media type is already in use as the type used to describe WebAssembly files when sent over HTTP to be executed by browsers, which is a common scenario. Additionally, several WebAssembly runtimes that take advantage of the safety and portability while targeting efficient execution and compact representation.</dd>
<dd>The application/wasm media type is intended for use as the type used to
describe WebAssembly files when sent over HTTP to be executed by browsers,
which is a common scenario. Additionally, the type is used by several
WebAssembly runtimes that take advantage of the safety and portability
while targeting efficient execution and compact representation.</dd>
<dt>Fragment Identifier Considerations:</dt>
<dd>None</dd>
<dt>Restrictions on usage:</dt>
Expand Down
30 changes: 30 additions & 0 deletions interpreter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,36 @@ The `input` and `output` meta commands determine the requested file format from
The interpreter supports a "dry" mode (flag `-d`), in which modules are only validated. In this mode, all actions and assertions are ignored.
It also supports an "unchecked" mode (flag `-u`), in which module definitions are not validated before use.


### Spectest host module

When running scripts, the interpreter predefines a simple host module named `"spectest"` that has the following module type:
```
(module
(global (export "global_i32") i32)
(global (export "global_i64") i64)
(global (export "global_f32") f32)
(global (export "global_f64") f64)
(table (export "table") 10 20 funcref)
(memory (export "memory") 1 2)
(func (export "print"))
(func (export "print_i32") (param i32))
(func (export "print_i64") (param i64))
(func (export "print_f32") (param f32))
(func (export "print_f64") (param f64))
(func (export "print_i32_f32") (param i32 f32))
(func (export "print_f64_f64") (param f64 f64))
)
```
The `print` functions are assumes to print their respective argument values to stdout (followed by a newline) and can be used to produce observable output.

Note: This module predates the `register` command and should no longer be needed for new tests.
We might remove it in the future, so consider it deprecated.


### Binary Scripts

The grammar of binary scripts is a subset of the grammar for general scripts:
Expand Down
2 changes: 1 addition & 1 deletion interpreter/exec/ixx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ struct

let popcnt x =
let rec loop acc i n =
if n = Rep.zero then
if i = 0 then
acc
else
let acc' = if and_ n Rep.one = Rep.one then acc + 1 else acc in
Expand Down
6 changes: 4 additions & 2 deletions interpreter/host/spectest.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ let lookup name t =
match Utf8.encode name, t with
| "print", _ -> ExternFunc (func print (FuncType ([], [])))
| "print_i32", _ -> ExternFunc (func print (FuncType ([NumType I32Type], [])))
| "print_i64", _ -> ExternFunc (func print (FuncType ([NumType I64Type], [])))
| "print_f32", _ -> ExternFunc (func print (FuncType ([NumType F32Type], [])))
| "print_f64", _ -> ExternFunc (func print (FuncType ([NumType F64Type], [])))
| "print_i32_f32", _ ->
ExternFunc (func print (FuncType ([NumType I32Type; NumType F32Type], [])))
| "print_f64_f64", _ ->
ExternFunc (func print (FuncType ([NumType F64Type; NumType F64Type], [])))
| "print_f32", _ -> ExternFunc (func print (FuncType ([NumType F32Type], [])))
| "print_f64", _ -> ExternFunc (func print (FuncType ([NumType F64Type], [])))
| "global_i32", _ -> ExternGlobal (global (GlobalType (NumType I32Type, Immutable)))
| "global_i64", _ -> ExternGlobal (global (GlobalType (NumType I64Type, Immutable)))
| "global_f32", _ -> ExternGlobal (global (GlobalType (NumType F32Type, Immutable)))
| "global_f64", _ -> ExternGlobal (global (GlobalType (NumType F64Type, Immutable)))
| "table", _ -> ExternTable table
Expand Down
2 changes: 1 addition & 1 deletion test/core/binary-leb128.wast
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,7 @@
"\41\00" ;; i32.const 0
"\41\03" ;; i32.const 3
"\36" ;; i32.store
"\03" ;; alignment 2
"\02" ;; alignment 2
"\82\80\80\80\10" ;; offset 2 with unused bits set
"\0b" ;; end
)
Expand Down
Loading

0 comments on commit e2aaaae

Please sign in to comment.