Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit b1c9394

Browse files
authored
Add table initialiser (#65)
1 parent 6194785 commit b1c9394

File tree

16 files changed

+206
-97
lines changed

16 files changed

+206
-97
lines changed

document/core/binary/modules.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,17 @@ It decodes into a vector of :ref:`tables <syntax-table>` that represent the |MTA
210210
\production{table section} & \Btablesec &::=&
211211
\X{tab}^\ast{:}\Bsection_4(\Bvec(\Btable)) &\Rightarrow& \X{tab}^\ast \\
212212
\production{table} & \Btable &::=&
213-
\X{tt}{:}\Btabletype &\Rightarrow& \{ \TTYPE~\X{tt} \} \\
213+
\X{tt}{:}\Btabletype
214+
&\Rightarrow& \{ \TTYPE~\X{tt}, \TINIT~(\REFNULL~\X{ht}) \}
215+
\qquad \iff \X{tt} = \limits~(\REF~\NULL^?~\X{ht}) \\
216+
\hex{40}~~\X{tt}{:}\Btabletype~~e{:}\Bexpr
217+
&\Rightarrow& \{ \TTYPE~\X{tt}, \TINIT~e \} \\
214218
\end{array}
215219
220+
.. note::
221+
The encoding of a table type cannot start with byte :math:`\hex{40}`,
222+
hence decoding is unambiguous.
223+
216224

217225
.. index:: ! memory section, memory, memory type
218226
pair: binary format; memory

document/core/exec/modules.rst

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -439,30 +439,28 @@ The allocation function for :ref:`modules <syntax-module>` requires a suitable l
439439
a list of initialization :ref:`values <syntax-val>` for the module's :ref:`globals <syntax-global>`,
440440
and list of :ref:`reference <syntax-ref>` vectors for the module's :ref:`element segments <syntax-elem>`.
441441

442-
1. Let :math:`\module` be the :ref:`module <syntax-module>` to allocate and :math:`\externval_{\F{im}}^\ast` the vector of :ref:`external values <syntax-externval>` providing the module's imports, :math:`\val^\ast` the initialization :ref:`values <syntax-val>` of the module's :ref:`globals <syntax-global>`, and :math:`(\reff^\ast)^\ast` the :ref:`reference <syntax-ref>` vectors of the module's :ref:`element segments <syntax-elem>`.
442+
1. Let :math:`\module` be the :ref:`module <syntax-module>` to allocate and :math:`\externval_{\F{im}}^\ast` the vector of :ref:`external values <syntax-externval>` providing the module's imports, :math:`\val_{\F{g}}^\ast` the initialization :ref:`values <syntax-val>` of the module's :ref:`globals <syntax-global>`, :math:`\reff_{\F{t}}^\ast` the initializer :ref:`reference <syntax-ref>` of the module's :ref:`tables <syntax-table>`, and :math:`(\reff_{\F{e}}^\ast)^\ast` the :ref:`reference <syntax-ref>` vectors of the module's :ref:`element segments <syntax-elem>`.
443443

444444
2. For each :ref:`function <syntax-func>` :math:`\func_i` in :math:`\module.\MFUNCS`, do:
445445

446446
a. Let :math:`\funcaddr_i` be the :ref:`function address <syntax-funcaddr>` resulting from :ref:`allocating <alloc-func>` :math:`\func_i` for the :ref:`\module instance <syntax-moduleinst>` :math:`\moduleinst` defined below.
447447

448448
3. For each :ref:`table <syntax-table>` :math:`\table_i` in :math:`\module.\MTABLES`, do:
449449

450-
a. Let :math:`\limits_i~t_i` be the :ref:`table type <syntax-tabletype>` :math:`\table_i.\TTYPE`.
451-
452-
b. Let :math:`\tableaddr_i` be the :ref:`table address <syntax-tableaddr>` resulting from :ref:`allocating <alloc-table>` :math:`\table_i.\TTYPE`
453-
with initialization value :math:`\REFNULL~t_i`.
450+
a. Let :math:`\tableaddr_i` be the :ref:`table address <syntax-tableaddr>` resulting from :ref:`allocating <alloc-table>` :math:`\table_i.\TTYPE`
451+
with initialization value :math:`\ref_{\F{t}}^\ast[i]`.
454452

455453
4. For each :ref:`memory <syntax-mem>` :math:`\mem_i` in :math:`\module.\MMEMS`, do:
456454

457455
a. Let :math:`\memaddr_i` be the :ref:`memory address <syntax-memaddr>` resulting from :ref:`allocating <alloc-mem>` :math:`\mem_i.\MTYPE`.
458456

459457
5. For each :ref:`global <syntax-global>` :math:`\global_i` in :math:`\module.\MGLOBALS`, do:
460458

461-
a. Let :math:`\globaladdr_i` be the :ref:`global address <syntax-globaladdr>` resulting from :ref:`allocating <alloc-global>` :math:`\global_i.\GTYPE` with initializer value :math:`\val^\ast[i]`.
459+
a. Let :math:`\globaladdr_i` be the :ref:`global address <syntax-globaladdr>` resulting from :ref:`allocating <alloc-global>` :math:`\global_i.\GTYPE` with initializer value :math:`\val_{\F{g}}^\ast[i]`.
462460

463461
6. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS`, do:
464462

465-
a. Let :math:`\elemaddr_i` be the :ref:`element address <syntax-elemaddr>` resulting from :ref:`allocating <alloc-elem>` an :ref:`element instance <syntax-eleminst>` of :ref:`reference type <syntax-reftype>` :math:`\elem_i.\ETYPE` with contents :math:`(\reff^\ast)^\ast[i]`.
463+
a. Let :math:`\elemaddr_i` be the :ref:`element address <syntax-elemaddr>` resulting from :ref:`allocating <alloc-elem>` an :ref:`element instance <syntax-eleminst>` of :ref:`reference type <syntax-reftype>` :math:`\elem_i.\ETYPE` with contents :math:`(\reff_{\F{e}}^\ast)^\ast[i]`.
466464

467465
7. For each :ref:`data segment <syntax-data>` :math:`\data_i` in :math:`\module.\MDATAS`, do:
468466

@@ -510,7 +508,7 @@ and list of :ref:`reference <syntax-ref>` vectors for the module's :ref:`element
510508
.. math::
511509
~\\
512510
\begin{array}{rlll}
513-
\allocmodule(S, \module, \externval_{\F{im}}^\ast, \val^\ast, (\reff^\ast)^\ast) &=& S', \moduleinst
511+
\allocmodule(S, \module, \externval_{\F{im}}^\ast, \val_{\F{g}}^\ast, \reff_{\F{t}}^\ast, (\reff_{\F{e}}^\ast)^\ast) &=& S', \moduleinst
514512
\end{array}
515513
516514
where:
@@ -537,14 +535,14 @@ where:
537535
S_1, \funcaddr^\ast &=&
538536
\allocfunc^\ast(S, \module.\MFUNCS, \moduleinst) \\
539537
S_2, \tableaddr^\ast &=&
540-
\alloctable^\ast(S_1, (\table.\TTYPE)^\ast, (\REFNULL~t)^\ast)
538+
\alloctable^\ast(S_1, (\table.\TTYPE)^\ast, \ref_{\F{t}}^\ast)
541539
\quad (\where (\table.\TTYPE)^\ast = (\limits~t)^\ast) \\
542540
S_3, \memaddr^\ast &=&
543541
\allocmem^\ast(S_2, (\mem.\MTYPE)^\ast) \\
544542
S_4, \globaladdr^\ast &=&
545-
\allocglobal^\ast(S_3, (\global.\GTYPE)^\ast, \val^\ast) \\
543+
\allocglobal^\ast(S_3, (\global.\GTYPE)^\ast, \val_{\F{g}}^\ast) \\
546544
S_5, \elemaddr^\ast &=&
547-
\allocelem^\ast(S_4, (\elem.\ETYPE)^\ast, (\reff^\ast)^\ast) \\
545+
\allocelem^\ast(S_4, (\elem.\ETYPE)^\ast, (\reff_{\F{e}}^\ast)^\ast) \\
548546
S', \dataaddr^\ast &=&
549547
\allocdata^\ast(S_5, (\data.\DINIT)^\ast) \\
550548
\exportinst^\ast &=&
@@ -627,35 +625,49 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep
627625

628626
7. Push the frame :math:`F_{\F{init}}` to the stack.
629627

630-
8. Let :math:`\val^\ast` be the vector of :ref:`global <syntax-global>` initialization :ref:`values <syntax-val>` determined by :math:`\module` and :math:`\externval^n`. These may be calculated as follows.
628+
8. Let :math:`\val_{\F{g}}^\ast` be the vector of :ref:`global <syntax-global>` initialization :ref:`values <syntax-val>` determined by :math:`\module` and :math:`\externval^n`. These may be calculated as follows.
631629

632630
a. For each :ref:`global <syntax-global>` :math:`\global_i` in :math:`\module.\MGLOBALS`, do:
633631

634-
i. Let :math:`\val_i` be the result of :ref:`evaluating <exec-expr>` the initializer expression :math:`\global_i.\GINIT`.
632+
i. Let :math:`\val_{\F{g}i}` be the result of :ref:`evaluating <exec-expr>` the initializer expression :math:`\global_i.\GINIT`.
633+
634+
b. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F_{\F{init}}` is now on the top of the stack.
635+
636+
c. Let :math:`\val_{\F{g}}^\ast` be the concatenation of :math:`\val_{\F{g}i}` in index order.
637+
638+
9. Let :math:`\reff_{\F{t}}^\ast` be the vector of :ref:`table <syntax-table>` initialization :ref:`references <syntax-ref>` determined by :math:`\module` and :math:`\externval^n`. These may be calculated as follows.
639+
640+
a. For each :ref:`table <syntax-table>` :math:`\table_i` in :math:`\module.\MTABLES`, do:
641+
642+
i. Let :math:`\val_{\F{t}i}` be the result of :ref:`evaluating <exec-expr>` the initializer expression :math:`\table_i.\TINIT`.
643+
644+
ii. Assert: due to :ref:`validation <valid-table>`, :math:`\val_{\F{t}i}` is a :ref:`reference <syntax-ref>`.
645+
646+
iii. Let :math:`\reff_{\F{t}i}` be the reference :math:`\val_{\F{t}i}`.
635647

636648
b. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F_{\F{init}}` is now on the top of the stack.
637649

638-
c. Let :math:`\val^\ast` be the concatenation of :math:`\val_i` in index order.
650+
c. Let :math:`\reff_{\F{t}}^\ast` be the concatenation of :math:`\reff_{ti}` in index order.
639651

640-
9. Let :math:`(\reff^\ast)^\ast` be the list of :ref:`reference <syntax-ref>` vectors determined by the :ref:`element segments <syntax-elem>` in :math:`\module`. These may be calculated as follows.
652+
10. Let :math:`(\reff_{\F{e}}^\ast)^\ast` be the list of :ref:`reference <syntax-ref>` vectors determined by the :ref:`element segments <syntax-elem>` in :math:`\module`. These may be calculated as follows.
641653

642654
a. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS`, and for each element :ref:`expression <syntax-expr>` :math:`\expr_{ij}` in :math:`\elem_i.\EINIT`, do:
643655

644656
i. Let :math:`\reff_{ij}` be the result of :ref:`evaluating <exec-expr>` the initializer expression :math:`\expr_{ij}`.
645657

646658
b. Let :math:`\reff^\ast_i` be the concatenation of function elements :math:`\reff_{ij}` in order of index :math:`j`.
647659

648-
c. Let :math:`(\reff^\ast)^\ast` be the concatenation of function element vectors :math:`\reff^\ast_i` in order of index :math:`i`.
660+
c. Let :math:`(\reff_{\F{e}}^\ast)^\ast` be the concatenation of function element vectors :math:`\reff^\ast_i` in order of index :math:`i`.
649661

650-
10. Pop the frame :math:`F_{\F{init}}` from the stack.
662+
11. Pop the frame :math:`F_{\F{init}}` from the stack.
651663

652-
11. Let :math:`\moduleinst` be a new module instance :ref:`allocated <alloc-module>` from :math:`\module` in store :math:`S` with imports :math:`\externval^n`, global initializer values :math:`\val^\ast`, and element segment contents :math:`(\reff^\ast)^\ast`, and let :math:`S'` be the extended store produced by module allocation.
664+
12. Let :math:`\moduleinst` be a new module instance :ref:`allocated <alloc-module>` from :math:`\module` in store :math:`S` with imports :math:`\externval^n`, global initializer values :math:`\val_{\F{g}}^\ast`, table initializer values :math:`\reff_{\F{t}}^\ast`, and element segment contents :math:`(\reff_{\F{e}}^\ast)^\ast`, and let :math:`S'` be the extended store produced by module allocation.
653665

654-
12. Let :math:`F` be the auxiliary :ref:`frame <syntax-frame>` :math:`\{ \AMODULE~\moduleinst, \ALOCALS~\epsilon \}`.
666+
13. Let :math:`F` be the auxiliary :ref:`frame <syntax-frame>` :math:`\{ \AMODULE~\moduleinst, \ALOCALS~\epsilon \}`.
655667

656-
13. Push the frame :math:`F` to the stack.
668+
14. Push the frame :math:`F` to the stack.
657669

658-
14. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS` whose :ref:`mode <syntax-elemmode>` is of the form :math:`\EACTIVE~\{ \ETABLE~\tableidx_i, \EOFFSET~\X{einstr}^\ast_i~\END \}`, do:
670+
15. For each :ref:`element segment <syntax-elem>` :math:`\elem_i` in :math:`\module.\MELEMS` whose :ref:`mode <syntax-elemmode>` is of the form :math:`\EACTIVE~\{ \ETABLE~\tableidx_i, \EOFFSET~\X{einstr}^\ast_i~\END \}`, do:
659671

660672
a. Let :math:`n` be the length of the vector :math:`\elem_i.\EINIT`.
661673

@@ -669,7 +681,7 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep
669681

670682
f. :ref:`Execute <exec-elem.drop>` the instruction :math:`\ELEMDROP~i`.
671683

672-
15. For each :ref:`data segment <syntax-data>` :math:`\data_i` in :math:`\module.\MDATAS` whose :ref:`mode <syntax-datamode>` is of the form :math:`\DACTIVE~\{ \DMEM~\memidx_i, \DOFFSET~\X{dinstr}^\ast_i~\END \}`, do:
684+
16. For each :ref:`data segment <syntax-data>` :math:`\data_i` in :math:`\module.\MDATAS` whose :ref:`mode <syntax-datamode>` is of the form :math:`\DACTIVE~\{ \DMEM~\memidx_i, \DOFFSET~\X{dinstr}^\ast_i~\END \}`, do:
673685

674686
a. Assert: :math:`\memidx_i` is :math:`0`.
675687

@@ -685,15 +697,15 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep
685697

686698
g. :ref:`Execute <exec-data.drop>` the instruction :math:`\DATADROP~i`.
687699

688-
16. If the :ref:`start function <syntax-start>` :math:`\module.\MSTART` is not empty, then:
700+
17. If the :ref:`start function <syntax-start>` :math:`\module.\MSTART` is not empty, then:
689701

690702
a. Let :math:`\start` be the :ref:`start function <syntax-start>` :math:`\module.\MSTART`.
691703

692704
b. :ref:`Execute <exec-call>` the instruction :math:`\CALL~\start.\SFUNC`.
693705

694-
17. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F` is now on the top of the stack.
706+
18. Assert: due to :ref:`validation <valid-module>`, the frame :math:`F` is now on the top of the stack.
695707

696-
18. Pop the frame :math:`F` from the stack.
708+
19. Pop the frame :math:`F` from the stack.
697709

698710

699711
.. math::
@@ -714,11 +726,13 @@ It is up to the :ref:`embedder <embedder>` to define how such conditions are rep
714726
&\wedge& \module.\MDATAS = \data^m \\
715727
&\wedge& \module.\MSTART = \start^? \\
716728
&\wedge& (\expr_{\F{g}} = \global.\GINIT)^\ast \\
729+
&\wedge& (\expr_{\F{t}} = \table.\GINIT)^\ast \\
717730
&\wedge& (\expr_{\F{e}}^\ast = \elem.\EINIT)^n \\[1ex]
718731
&\wedge& S', \moduleinst = \allocmodule(S, \module, \externval^k, \val^\ast, (\reff^\ast)^n) \\
719732
&\wedge& F = \{ \AMODULE~\moduleinst, \ALOCALS~\epsilon \} \\[1ex]
720-
&\wedge& (S'; F; \expr_{\F{g}} \stepto^\ast S'; F; \val~\END)^\ast \\
721-
&\wedge& ((S'; F; \expr_{\F{e}} \stepto^\ast S'; F; \reff~\END)^\ast)^n \\
733+
&\wedge& (S'; F; \expr_{\F{g}} \stepto^\ast S'; F; \val_{\F{g}}~\END)^\ast \\
734+
&\wedge& (S'; F; \expr_{\F{t}} \stepto^\ast S'; F; \reff_{\F{t}}~\END)^\ast \\
735+
&\wedge& ((S'; F; \expr_{\F{e}} \stepto^\ast S'; F; \reff_{\F{e}}~\END)^\ast)^n \\
722736
&\wedge& (\tableaddr = \moduleinst.\MITABLES[\elem.\ETABLE])^\ast \\
723737
&\wedge& (\memaddr = \moduleinst.\MIMEMS[\data.\DMEM])^\ast \\
724738
&\wedge& (\funcaddr = \moduleinst.\MIFUNCS[\start.\SFUNC])^?)

document/core/syntax/modules.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,14 @@ The |MTABLES| component of a module defines a vector of *tables* described by th
172172
.. math::
173173
\begin{array}{llll}
174174
\production{table} & \table &::=&
175-
\{ \TTYPE~\tabletype \} \\
175+
\{ \TTYPE~\tabletype, \TINIT~\expr \} \\
176176
\end{array}
177177
178-
A table is a vector of opaque values of a particular :ref:`reference type <syntax-reftype>`.
179-
The |LMIN| size in the :ref:`limits <syntax-limits>` of the table type specifies the initial size of that table, while its |LMAX|, if present, restricts the size to which it can grow later.
178+
A table is an array of opaque values of a particular :ref:`reference type <syntax-reftype>`.
179+
Moreover, each table slot is initialized with the |TINIT| value given by a :ref:`constant <valid-constant>` initializer :ref:`expression <syntax-expr>`.
180+
Tables can further be initialized through :ref:`element segments <syntax-elem>`.
180181

181-
Tables can be initialized through :ref:`element segments <syntax-elem>`.
182+
The |LMIN| size in the :ref:`limits <syntax-limits>` of the table type specifies the initial size of that table, while its |LMAX|, if present, restricts the size to which it can grow later.
182183

183184
Tables are referenced through :ref:`table indices <syntax-tableidx>`,
184185
starting with the smallest index not referencing a table :ref:`import <syntax-import>`.

document/core/util/macros.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@
289289
.. |LTYPE| mathdef:: \xref{syntax/modules}{syntax-local}{\K{type}}
290290

291291
.. |TTYPE| mathdef:: \xref{syntax/modules}{syntax-table}{\K{type}}
292+
.. |TINIT| mathdef:: \xref{syntax/modules}{syntax-table}{\K{init}}
292293

293294
.. |MTYPE| mathdef:: \xref{syntax/modules}{syntax-mem}{\K{type}}
294295

@@ -937,7 +938,6 @@
937938
.. |vdashinstrtype| mathdef:: \xref{valid/instructions}{valid-instrtype}{\vdash}
938939

939940
.. |vdashvaltypedefaultable| mathdef:: \xref{valid/types}{valid-defaultable}{\vdash}
940-
.. |vdashtabletypedefaultable| mathdef:: \xref{valid/types}{valid-defaultable}{\vdash}
941941

942942
.. |vdashnumtypematch| mathdef:: \xref{valid/matching}{match-numtype}{\vdash}
943943
.. |vdashvectypematch| mathdef:: \xref{valid/matching}{match-vectype}{\vdash}

document/core/valid/modules.rst

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Locals
9999
For cases where both rules are applicable, the former yields the more permissable type.
100100

101101

102-
.. index:: table, table type, defaultable
102+
.. index:: table, table type, reference type, expression, constant, defaultable
103103
pair: validation; table
104104
single: abstract syntax; table
105105
.. _valid-table:
@@ -109,20 +109,30 @@ Tables
109109

110110
Tables :math:`\table` are classified by :ref:`table types <syntax-tabletype>`.
111111

112-
:math:`\{ \TTYPE~\tabletype \}`
113-
...............................
112+
:math:`\{ \TTYPE~\tabletype, \TINIT~\expr \}`
113+
.............................................
114114

115115
* The :ref:`table type <syntax-tabletype>` :math:`\tabletype` must be :ref:`valid <valid-tabletype>`.
116116

117+
* Let :math:`t` be the element :ref:`reference type <syntax-reftype>` of :math:`\tabletype`.
118+
119+
* The expression :math:`\expr` must be :ref:`valid <valid-expr>` with :ref:`result type <syntax-resulttype>` :math:`[t]`.
120+
121+
* The expression :math:`\expr` must be :ref:`constant <valid-constant>`.
122+
117123
* Then the table definition is valid with type :math:`\tabletype`.
118124

119125
.. math::
120126
\frac{
121127
C \vdashtabletype \tabletype \ok
122128
\qquad
123-
C \vdashtabletypedefaultable \tabletype \defaultable
129+
\tabletype = \limits~t
130+
\qquad
131+
C \vdashexpr \expr : [t]
132+
\qquad
133+
C \vdashexprconst \expr \const
124134
}{
125-
C \vdashtable \{ \TTYPE~\tabletype \} : \tabletype
135+
C \vdashtable \{ \TTYPE~\tabletype, \TINIT~\expr \} : \tabletype
126136
}
127137
128138
@@ -151,7 +161,7 @@ Memories :math:`\mem` are classified by :ref:`memory types <syntax-memtype>`.
151161
}
152162
153163
154-
.. index:: global, global type, expression
164+
.. index:: global, global type, expression, constant
155165
pair: validation; global
156166
single: abstract syntax; global
157167
.. _valid-global:
@@ -185,7 +195,7 @@ Globals :math:`\global` are classified by :ref:`global types <syntax-globaltype>
185195
}
186196
187197
188-
.. index:: element, table, table index, expression, function index
198+
.. index:: element, table, table index, expression, constant, function index
189199
pair: validation; element
190200
single: abstract syntax; element
191201
single: table; element
@@ -286,7 +296,7 @@ Element segments :math:`\elem` are classified by the :ref:`reference type <synta
286296
287297
288298
289-
.. index:: data, memory, memory index, expression, byte
299+
.. index:: data, memory, memory index, expression, constant, byte
290300
pair: validation; data
291301
single: abstract syntax; data
292302
single: memory; data

document/core/valid/types.rst

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -479,19 +479,3 @@ Value Types
479479
}{
480480
C \vdashvaltypedefaultable (\REF~\NULL~\heaptype) \defaultable
481481
}
482-
483-
484-
Table Types
485-
...........
486-
487-
* A defaultable :ref:`table type <syntax-tabletype>` must be:
488-
489-
- of the form :math:`\limits~t`, where `t` is a :ref:`defaultable <valid-defaultable>` :ref:`reference type <syntax-numtype>`.
490-
491-
492-
.. math::
493-
\frac{
494-
C \vdashvaltypedefaultable \reftype \defaultable
495-
}{
496-
C \vdashtabletypedefaultable \limits~\reftype \defaultable
497-
}

interpreter/binary/decode.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,8 +908,20 @@ let func_section s =
908908
(* Table section *)
909909

910910
let table s =
911-
let ttype = table_type s in
912-
{ttype}
911+
either [
912+
(fun s ->
913+
expect 0x40 s "";
914+
zero s;
915+
let ttype = table_type s in
916+
let tinit = const s in
917+
{ttype; tinit}
918+
);
919+
(fun s ->
920+
let at = region s (pos s) (pos s) in
921+
let TableT (_, (_, ht)) as ttype = table_type s in
922+
{ttype; tinit = [RefNull ht @@ at] @@ at}
923+
);
924+
] s
913925

914926
let table_section s =
915927
section `TableSection (vec (at table)) [] s

interpreter/binary/encode.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,8 +798,11 @@ struct
798798
(* Table section *)
799799

800800
let table tab =
801-
let {ttype} = tab.it in
802-
table_type ttype
801+
let {ttype; tinit} = tab.it in
802+
match ttype, tinit.it with
803+
| TableT (_, (_, ht1)), [{it = RefNull ht2; _}] when ht1 = ht2 ->
804+
table_type ttype
805+
| _ -> op 0x40; op 0x00; table_type ttype; const tinit
803806

804807
let table_section tabs =
805808
section 4 (vec table) tabs (tabs <> [])

interpreter/exec/eval.ml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -758,9 +758,14 @@ let create_func (inst : module_inst) (f : func) : func_inst =
758758
Func.alloc (type_ inst f.it.ftype) (Lib.Promise.make ()) f
759759

760760
let create_table (inst : module_inst) (tab : table) : table_inst =
761-
let {ttype} = tab.it in
762-
let TableT (_lim, (_nul, t)) as tt = Types.Sem.sem_table_type inst.types ttype in
763-
Table.alloc tt (NullRef t)
761+
let {ttype; tinit} = tab.it in
762+
let tt = Types.Sem.sem_table_type inst.types ttype in
763+
let r =
764+
match eval_const inst tinit with
765+
| Ref r -> r
766+
| _ -> Crash.error tinit.at "non-reference table initializer"
767+
in
768+
Table.alloc tt r
764769

765770
let create_memory (inst : module_inst) (mem : memory) : memory_inst =
766771
let {mtype} = mem.it in

interpreter/syntax/ast.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ type table = table' Source.phrase
240240
and table' =
241241
{
242242
ttype : table_type;
243+
tinit : const;
243244
}
244245

245246
type memory = memory' Source.phrase

0 commit comments

Comments
 (0)