Skip to content

Commit 153ca9a

Browse files
authored
Add exceptional return to func_invoke in embedding doc (#268)
Embedding: * Adds an auxiliary syntactic class to represent exception throwing/propagation * Allows `func_invoke` to propagate exceptions * Adds `tag_type` and `exn_alloc` to the embedding interface core/exec: * Refactors exception allocation into `alloc-exception` in modules.rst to expose it to the embedder
1 parent 3dbe24e commit 153ca9a

File tree

4 files changed

+110
-16
lines changed

4 files changed

+110
-16
lines changed

Diff for: document/core/appendix/embedding.rst

+73-6
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,19 @@ For numeric parameters, notation like :math:`n:\u32` is used to specify a symbol
2626

2727
.. _embed-error:
2828

29-
Errors
30-
~~~~~~
29+
Exceptions and Errors
30+
~~~~~~~~~~~~~~~~~~~~~
3131

32-
Failure of an interface operation is indicated by an auxiliary syntactic class:
32+
Invoking an exported function may throw or propagate exceptions, expressed by an auxiliary syntactic class:
33+
34+
.. math::
35+
\begin{array}{llll}
36+
\production{exception} & \exception &::=& \ETHROW ~ \exnaddr \\
37+
\end{array}
38+
39+
The exception address :math:`exnaddr` identifies the exception thrown.
40+
41+
Failure of an interface operation is also indicated by an auxiliary syntactic class:
3342

3443
.. math::
3544
\begin{array}{llll}
@@ -43,6 +52,8 @@ In addition to the error conditions specified explicitly in this section, implem
4352
Implementations can refine it to carry suitable classifications and diagnostic messages.
4453

4554

55+
56+
4657
Pre- and Post-Conditions
4758
~~~~~~~~~~~~~~~~~~~~~~~~
4859

@@ -293,21 +304,24 @@ Functions
293304
.. index:: invocation, value, result
294305
.. _embed-func-invoke:
295306

296-
:math:`\F{func\_invoke}(\store, \funcaddr, \val^\ast) : (\store, \val^\ast ~|~ \error)`
297-
........................................................................................
307+
:math:`\F{func\_invoke}(\store, \funcaddr, \val^\ast) : (\store, \val^\ast ~|~ \exception ~|~ \error)`
308+
......................................................................................................
298309

299310
1. Try :ref:`invoking <exec-invocation>` the function :math:`\funcaddr` in :math:`\store` with :ref:`values <syntax-val>` :math:`\val^\ast` as arguments:
300311

301312
a. If it succeeds with :ref:`values <syntax-val>` :math:`{\val'}^\ast` as results, then let :math:`\X{result}` be :math:`{\val'}^\ast`.
302313

303-
b. Else it has trapped, hence let :math:`\X{result}` be :math:`\ERROR`.
314+
b. Else if the outcome is an exception with a thrown :ref:`exception <exec-throw_ref>` :math:`\REFEXNADDR~\exnaddr` as the result, then let :math:`\X{result}` be :math:`\ETHROW~\exnaddr`
315+
316+
c. Else it has trapped, hence let :math:`\X{result}` be :math:`\ERROR`.
304317

305318
2. Return the new store paired with :math:`\X{result}`.
306319

307320
.. math::
308321
~ \\
309322
\begin{array}{lclll}
310323
\F{func\_invoke}(S, a, v^\ast) &=& (S', {v'}^\ast) && (\iff \invoke(S, a, v^\ast) \stepto^\ast S'; F; {v'}^\ast) \\
324+
\F{func\_invoke}(S, a, v^\ast) &=& (S', \ETHROW~a') && (\iff \invoke(S, a, v^\ast) \stepto^\ast S'; F; \XT[(\REFEXNADDR~a')~\THROWREF] \\
311325
\F{func\_invoke}(S, a, v^\ast) &=& (S', \ERROR) && (\iff \invoke(S, a, v^\ast) \stepto^\ast S'; F; \TRAP) \\
312326
\end{array}
313327
@@ -562,6 +576,59 @@ Tags
562576
\end{array}
563577
564578
579+
.. _embed-tag-type:
580+
581+
:math:`\F{tag\_type}(\store, \tagaddr) : \tagtype`
582+
..................................................
583+
584+
1. Return :math:`S.\STAGS[a].\TAGITYPE`.
585+
586+
2. Post-condition: the returned :ref:`tag type <syntax-tagtype>` is :ref:`valid <valid-tagtype>`.
587+
588+
.. math::
589+
\begin{array}{lclll}
590+
\F{tag\_type}(S, a) &=& S.\STAGS[a].\TAGITYPE \\
591+
\end{array}
592+
593+
594+
.. index:: exception, exception address, store, exception instance, exception type
595+
.. _embed-exception:
596+
597+
Exceptions
598+
~~~~~~~~~~
599+
600+
.. _embed-exn-alloc:
601+
602+
:math:`\F{exn\_alloc}(\store, \tagaddr, \val^\ast) : (\store, \exnaddr)`
603+
........................................................................
604+
605+
1. Pre-condition: :math:`\tagaddr` is an allocated :ref:`tag address <syntax-tagaddr>`.
606+
607+
2. Let :math:`\exnaddr` be the result of :ref:`allocating an exception <alloc-exception>` in :math:`\store` with :ref:`tag address <syntax-tagaddr>` :math:`\tagaddr` and initialization values :math:`\val^\ast`.
608+
609+
3. Return the new store paired with :math:`\exnaddr`.
610+
611+
.. math::
612+
\begin{array}{lclll}
613+
\F{exn\_alloc}(S, \tagaddr, \val^\ast) &=& (S', a) && (\iff \allocexn(S, \tagaddr, \val^\ast) = S', a) \\
614+
\end{array}
615+
616+
617+
.. _embed-exn-read:
618+
619+
:math:`\F{exn\_read}(\store, \exnaddr) : (\tagaddr, \val^\ast)`
620+
...............................................................
621+
622+
1. Let :math:`\X{ei}` be the :ref:`exception instance <syntax-exninst>` :math:`\store.\SEXNS[\exnaddr]`.
623+
624+
2. Return the :ref:`tag address <syntax-tagaddr>` :math:`\X{ei}.\EITAG~\tagaddr` paired with :ref:`values <syntax-val>` :math:`\X{ei}.\EIFIELDS~\val^\ast`.
625+
626+
.. math::
627+
\begin{array}{lcll}
628+
\F{exn\_read}(S, a) &=& (a', v^\ast) \\
629+
\end{array}
630+
631+
565632
.. index:: global, global address, store, global instance, global type, value
566633
.. _embed-global:
567634

Diff for: document/core/exec/instructions.rst

+7-9
Original file line numberDiff line numberDiff line change
@@ -2719,27 +2719,25 @@ Control Instructions
27192719

27202720
2. Assert: due to :ref:`validation <valid-throw>`, :math:`F.\AMODULE.\MITAGS[x]` exists.
27212721

2722-
3. Let :math:`a` be the :ref:`tag address <syntax-tagaddr>` :math:`F.\AMODULE.\MITAGS[x]`.
2722+
3. Let :math:`ta` be the :ref:`tag address <syntax-tagaddr>` :math:`F.\AMODULE.\MITAGS[x]`.
27232723

2724-
4. Assert: due to :ref:`validation <valid-throw>`, :math:`S.\STAGS[a]` exists.
2724+
4. Assert: due to :ref:`validation <valid-throw>`, :math:`S.\STAGS[ta]` exists.
27252725

2726-
5. Let :math:`\X{ti}` be the :ref:`tag instance <syntax-taginst>` :math:`S.\STAGS[a]`.
2726+
5. Let :math:`\X{ti}` be the :ref:`tag instance <syntax-taginst>` :math:`S.\STAGS[ta]`.
27272727

27282728
6. Let :math:`[t^n] \toF [{t'}^\ast]` be the :ref:`tag type <syntax-tagtype>` :math:`\X{ti}.\TAGITYPE`.
27292729

27302730
7. Assert: due to :ref:`validation <valid-throw>`, there are at least :math:`n` values on the top of the stack.
27312731

27322732
8. Pop the :math:`n` values :math:`\val^n` from the stack.
27332733

2734-
9. Let :math:`\X{exn}` be the :ref:`exception instance <syntax-exninst>` :math:`\{ \EITAG~a, \EIFIELDS~\val^n \}`.
2734+
9. Let :math:`\X{ea}` be the :ref:`exception address <syntax-exnaddr>` resulting from :ref:`allocating <alloc-exception>` an exception instance with tag address :math:`ta` and initializer values :math:`\val^n`.
27352735

2736-
10. Let :math:`\X{ea}` be the length of :math:`S.\SEXNS`.
2736+
10. Let :math:`\X{exn}` be :math:`S.\SEXNS[ea]`
27372737

2738-
11. Append :math:`\X{exn}` to :math:`S.\SEXNS`.
2738+
11. Push the value :math:`\REFEXNADDR~\X{ea}` to the stack.
27392739

2740-
12. Push the value :math:`\REFEXNADDR~\X{ea}` to the stack.
2741-
2742-
13. Execute the instruction |THROWREF|.
2740+
12. Execute the instruction |THROWREF|.
27432741

27442742
.. math::
27452743
~\\[-1ex]

Diff for: document/core/exec/modules.rst

+27-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ The following auxiliary typing rules specify this typing relation relative to a
190190
Allocation
191191
~~~~~~~~~~
192192

193-
New instances of :ref:`functions <syntax-funcinst>`, :ref:`tables <syntax-tableinst>`, :ref:`memories <syntax-meminst>`, :ref:`tags <syntax-taginst>`, and :ref:`globals <syntax-globalinst>` are *allocated* in a :ref:`store <syntax-store>` :math:`S`, as defined by the following auxiliary functions.
193+
New instances of :ref:`functions <syntax-funcinst>`, :ref:`tables <syntax-tableinst>`, :ref:`memories <syntax-meminst>`, :ref:`tags <syntax-taginst>`, :ref:`exceptions <syntax-exninst>`, and :ref:`globals <syntax-globalinst>` are *allocated* in a :ref:`store <syntax-store>` :math:`S`, as defined by the following auxiliary functions.
194194

195195

196196
.. index:: function, function instance, function address, module instance, function type
@@ -338,6 +338,32 @@ New instances of :ref:`functions <syntax-funcinst>`, :ref:`tables <syntax-tablei
338338
\end{array}
339339
340340
341+
.. index:: exception, exception instance, exception address, tag address
342+
.. _alloc-exception:
343+
344+
:ref:`Exceptions <syntax-exninst>`
345+
..................................
346+
347+
1. Let :math:`ta` be the :ref:`tag address <syntax-tagaddr>` associated with the exception to allocate and :math:`\EIFIELDS~\val^\ast` be the values to initialize the exception with.
348+
349+
2. Let :math:`a` be the first free :ref:`exception address <syntax-exnaddr>` in :math:`S`.
350+
351+
3. Let :math:`\exninst` be the :ref:`exception instance <syntax-exninst>` :math:`\{ \EITAG~ta, \EIFIELDS~\val^\ast \}`.
352+
353+
4. Append :math:`\exninst` to the |SEXNS| of :math:`S`.
354+
355+
5. Return :math:`a`.
356+
357+
.. math::
358+
\begin{array}{rlll}
359+
\allocexn(S, \tagaddr, \val^\ast) &=& S', \exnaddr \\[1ex]
360+
\mbox{where:} \hfill \\
361+
\exnaddr &=& |S.\SEXNS| \\
362+
\exninst &=& \{ \EITAG~\tagaddr, \EIFIELDS~\val^\ast \} \\
363+
S' &=& S \compose \{\SEXNS~\exninst\} \\
364+
\end{array}
365+
366+
341367
.. index:: global, global instance, global address, global type, value type, mutability, value
342368
.. _alloc-global:
343369

Diff for: document/core/util/macros.def

+3
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,7 @@
981981
.. |alloctable| mathdef:: \xref{exec/modules}{alloc-table}{\F{alloctable}}
982982
.. |allocmem| mathdef:: \xref{exec/modules}{alloc-mem}{\F{allocmem}}
983983
.. |alloctag| mathdef:: \xref{exec/modules}{alloc-tag}{\F{alloctag}}
984+
.. |allocexn| mathdef:: \xref{exec/modules}{alloc-exception}{\F{allocexn}}
984985
.. |allocglobal| mathdef:: \xref{exec/modules}{alloc-global}{\F{allocglobal}}
985986
.. |allocelem| mathdef:: \xref{exec/modules}{alloc-elem}{\F{allocelem}}
986987
.. |allocdata| mathdef:: \xref{exec/modules}{alloc-data}{\F{allocdata}}
@@ -1335,3 +1336,5 @@
13351336

13361337
.. |error| mathdef:: \xref{appendix/embedding}{embed-error}{\X{error}}
13371338
.. |ERROR| mathdef:: \xref{appendix/embedding}{embed-error}{\K{error}}
1339+
.. |exception| mathdef:: \xref{appendix/embedding}{embed-error}{\X{exception}}
1340+
.. |ETHROW| mathdef:: \xref{appendix/embedding}{embed-error}{\K{THROW}}

0 commit comments

Comments
 (0)