Skip to content

Commit c6acc6e

Browse files
Guy Bedfordguybedford
authored andcommitted
Support direct global bindings
1 parent 91545f3 commit c6acc6e

File tree

5 files changed

+273
-53
lines changed

5 files changed

+273
-53
lines changed

document/core/appendix/embedding.rst

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,37 @@ Modules
137137
\F{module\_validate}(m) &=& \ERROR && (\otherwise) \\
138138
\end{array}
139139
140+
.. index:: matching, external type
141+
.. _embed-extern-subtype:
142+
143+
:math:`\F{module\_extern\_subtype}(\externtype_1, \externtype_2) : \bool`
144+
.......................................................................
145+
146+
1. If :math:`\externtype_1` and :math:`\externtype_2` are both of the form :math:`\ETFUNC~\functype_1` and :math:`\ETFUNC~\functype_2` respectively:
147+
148+
a. Return true if and only if :math:`\vdashexterntypematch \ETFUNC~\functype_1 \matchesexterntype \ETFUNC~\functype_2`.
149+
150+
2. If :math:`\externtype_1` and :math:`\externtype_2` are both of the form :math:`\ETTABLE~\tabletype_1` and :math:`\ETTABLE~\tabletype_2` respectively:
151+
152+
a. Return true if and only if :math:`\vdashexterntypematch \ETTABLE~\tabletype_1 \matchesexterntype \ETTABLE~\tabletype_2`.
153+
154+
3. If :math:`\externtype_1` and :math:`\externtype_2` are both of the form :math:`\ETMEM~\memtype_1` and :math:`\ETMEM~\memtype_2` respectively:
155+
156+
a. Return true if and only if :math:`\vdashexterntypematch \ETMEM~\memtype_1 \matchesexterntype \ETMEM~\memtype_2`.
157+
158+
4. If :math:`\externtype_1` and :math:`\externtype_2` are both of the form :math:`\ETGLOBAL~\globaltype_1` and :math:`\ETGLOBAL~\globaltype_2` respectively:
159+
160+
a. Return true if and only if :math:`\vdashexterntypematch \ETGLOBAL~\globaltype_1 \matchesexterntype \ETGLOBAL~\globaltype_2`.
161+
162+
5. Return false.
163+
164+
.. math::
165+
\begin{array}{lclll}
166+
\F{module\_extern\_subtype}(\externtype_1, \externtype_2) &=& \TRUE && (\iff \vdashexterntypematch \externtype_1 \matchesexterntype \externtype_2) \\
167+
\end{array}
168+
169+
.. note::
170+
This function encapsulates the external type matching relation defined in the core specification. It allows for checking compatibility of external types when linking modules or validating imports against exports. The current implementation uses the exact matching rules from the core specification, but this function provides a single point for potential future extensions to the type system.
140171

141172
.. index:: instantiation, module instance
142173
.. _embed-module-instantiate:
@@ -218,6 +249,66 @@ Modules
218249
\end{array}
219250
220251
252+
.. index:: direct export
253+
.. _embed-direct-exports:
254+
255+
:math:`\F{module\_direct\_exports}(\module) : (\name, \externtype)^\ast`
256+
.......................................................................
257+
258+
1. Pre-condition: :math:`\module` is :ref:`valid <valid-module>` with external import types :math:`\externtype^\ast` and external export types :math:`{\externtype'}^\ast`.
259+
260+
2. Let :math:`\export^\ast` be the :ref:`exports <syntax-export>` :math:`\module.\MEXPORTS`.
261+
262+
3. Let :math:`\X{result}` be the empty sequence.
263+
264+
4. For each :math:`\export_i` in :math:`\export^\ast` and corresponding :math:`\externtype'_i` in :math:`{\externtype'}^\ast`, do:
265+
266+
a. If :math:`\isdirectexport(\module, \export_i.\EDESC)`, then append the pair :math:`(\export_i.\ENAME, \externtype'_i)` to :math:`\X{result}`.
267+
268+
5. Return :math:`\X{result}`.
269+
270+
.. math::
271+
~ \\
272+
\begin{array}{lclll}
273+
\F{module\_direct\_exports}(m) &=& (\X{ex}.\ENAME, \externtype')^\ast \\
274+
&& \qquad (\iff \X{ex} \in m.\MEXPORTS \wedge \externtype' \in {\externtype'}^\ast \wedge {} \vdashmodule m : \externtype^\ast \to {\externtype'}^\ast \\
275+
&& \qquad\quad \wedge \isdirectexport(m, \X{ex}.\EDESC)) \\
276+
\end{array}
277+
278+
279+
.. index:: indirect export, re-export
280+
.. _embed-indirect-exports:
281+
282+
:math:`\F{module\_indirect\_exports}(\module) : (\name, \name, \name)^\ast`
283+
................................................................
284+
285+
1. Pre-condition: :math:`\module` is :ref:`valid <valid-module>` with external import types :math:`\externtype^\ast` and external export types :math:`{\externtype'}^\ast`.
286+
287+
2. Let :math:`\import^\ast` be the :ref:`imports <syntax-import>` :math:`\module.\MIMPORTS`.
288+
289+
3. Let :math:`\export^\ast` be the :ref:`exports <syntax-export>` :math:`\module.\MEXPORTS`.
290+
291+
4. Let :math:`\X{result}` be the empty sequence.
292+
293+
5. For each :math:`\export_i` in :math:`\export^\ast`, do:
294+
295+
a. If :math:`\isindirectexport(\module, \export_i.\EDESC)`, then:
296+
297+
i. Let :math:`\import_j` be the import corresponding to the index in :math:`\export_i.\EDESC`.
298+
299+
ii. Append the triple :math:`(\export_i.\ENAME, \import_j.\IMODULE, \import_j.\INAME)` to :math:`\X{result}`.
300+
301+
6. Return :math:`\X{result}`.
302+
303+
.. math::
304+
~ \\
305+
\begin{array}{lclll}
306+
\F{module\_indirect\_exports}(m) &=& (\X{ex}.\ENAME, \X{im}.\IMODULE, \X{im}.\INAME)^\ast \\
307+
&& \qquad (\iff \X{ex} \in m.\MEXPORTS \wedge \X{im} \in m.\MIMPORTS \wedge {} \vdashmodule m : \externtype^\ast \to {\externtype'}^\ast \\
308+
&& \qquad\quad \wedge \isindirectexport(m, \X{ex}.\EDESC) \wedge \X{im} = \importforexport(m, \X{ex}.\EDESC)) \\
309+
\end{array}
310+
311+
221312
.. index:: module, module instance
222313
.. _embed-instance:
223314

document/core/syntax/modules.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ The |MSTART| component of a module declares the :ref:`function index <syntax-fun
327327
The module and its exports are not accessible externally before this initialization has completed.
328328

329329

330-
.. index:: ! export, name, index, function index, table index, memory index, global index, function, table, memory, global, instantiation
330+
.. index:: ! export, direct export, indirect export, re-export, name, index, function index, table index, memory index, global index, function, table, memory, global, instantiation
331331
pair: abstract syntax; export
332332
single: function; export
333333
single: table; export
@@ -356,6 +356,32 @@ Each export is labeled by a unique :ref:`name <syntax-name>`.
356356
Exportable definitions are :ref:`functions <syntax-func>`, :ref:`tables <syntax-table>`, :ref:`memories <syntax-mem>`, and :ref:`globals <syntax-global>`,
357357
which are referenced through a respective descriptor.
358358

359+
A *direct export* is one where the export references a function, table, memory, or global instance that is defined within the module itself rather than being imported.
360+
361+
An *indirect export* (or *re-export*) is one where the export references a function, table, memory, or global that the module imports.
362+
363+
The import corresponding to an export description is given by:
364+
365+
.. math::
366+
\begin{array}{lllll}
367+
\production{import for export} & \importforexport(m, \exportdesc) &=&
368+
\begin{cases}
369+
m.\MIMPORTS[\X{idx}] & (\iff \exportdesc = \EDFUNC~\X{idx} \wedge \X{idx} < |\etfuncs(m.\MIMPORTS)|) \\
370+
m.\MIMPORTS[\X{fidx} + \X{idx}] & (\iff \exportdesc = \EDTABLE~\X{idx} \wedge \X{idx} < |\ettables(m.\MIMPORTS)| \\
371+
& \quad \wedge \X{fidx} = |\etfuncs(m.\MIMPORTS)|) \\
372+
m.\MIMPORTS[\X{fidx} + \X{tidx} + \X{idx}] & (\iff \exportdesc = \EDMEM~\X{idx} \wedge \X{idx} < |\etmems(m.\MIMPORTS)| \\
373+
& \quad \wedge \X{fidx} = |\etfuncs(m.\MIMPORTS)| \wedge \X{tidx} = |\ettables(m.\MIMPORTS)|) \\
374+
m.\MIMPORTS[\X{fidx} + \X{tidx} + \X{midx} + \X{idx}] & (\iff \exportdesc = \EDGLOBAL~\X{idx} \wedge \X{idx} < |\etglobals(m.\MIMPORTS)| \\
375+
& \quad \wedge \X{fidx} = |\etfuncs(m.\MIMPORTS)| \wedge \X{tidx} = |\ettables(m.\MIMPORTS)| \\
376+
& \quad \wedge \X{midx} = |\etmems(m.\MIMPORTS)|) \\
377+
\epsilon & (\otherwise) \\
378+
\end{cases} \\
379+
\end{array}
380+
381+
For embedder convenience, we also define:
382+
383+
* :math:``\isdirectexport(m, \exportdesc) = (\importforexport(m, \exportdesc) = \epsilon)`
384+
* :math:``\isindirectexport(m, \exportdesc) = (\importforexport(m, \exportdesc) \neq \epsilon)`
359385

360386
Conventions
361387
...........

0 commit comments

Comments
 (0)