Skip to content

Commit

Permalink
Fix table type (#564)
Browse files Browse the repository at this point in the history
* fix table ref type

* add table unit test
  • Loading branch information
bbyalcinkaya authored Jan 21, 2024
1 parent b6cf5bf commit 37b5b28
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions pykwasm/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ disallow_untyped_defs = true
exclude = [
'src/pykwasm/wasm2kast\.py',
'src/wasm/*',
'src/tests/unit/test_wasm2kast\.py',
]

[tool.poetry.scripts]
Expand Down
9 changes: 4 additions & 5 deletions pykwasm/src/pykwasm/wasm2kast.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ def func(f: Function):

def table(t: Table):
ls = limits(t.type.limits)
if isinstance(t.type.elem_type, addresses.FunctionAddress):
typ = a.funcref
else:
typ = a.externref
typ = ref_type(t.type.elem_type)
return a.table(ls, typ)


Expand All @@ -123,7 +120,9 @@ def glob(g: Global):
def ref_type(t: RefType):
if t is addresses.FunctionAddress:
return a.funcref
return a.externref
if t is addresses.ExternAddress:
return a.externref
raise ValueError(f'Invalid RefType: {t}')


def elem_mode(m: ElemMode) -> KInner:
Expand Down
9 changes: 8 additions & 1 deletion pykwasm/src/tests/integration/binary/tables.wat
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
(module
(table 1 funcref))
(table (import "spectest" "table") 0 funcref)
(table 0 funcref)
(table 1 funcref)
(table 1 100 funcref)
(table 0 externref)
(table 1 externref)
(table 1 100 externref)
)
21 changes: 21 additions & 0 deletions pykwasm/src/tests/unit/test_wasm2kast.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import pytest
from pyk.kast.inner import KApply, KInner
from wasm.datatypes import ExternAddress, FunctionAddress, Limits, Table, TableType

from pykwasm import wasm2kast
from pykwasm.kwasm_ast import KInt, externref, funcref

TABLE_TEST_DATA = (
(Table(TableType(Limits(0, None), FunctionAddress)), KApply('limitsMin', [KInt(0)]), funcref),
(Table(TableType(Limits(0, 100), ExternAddress)), KApply('limitsMinMax', [KInt(0), KInt(100)]), externref),
)


@pytest.mark.parametrize(('input', 'limits', 'typ'), TABLE_TEST_DATA)
def test_table(input: Table, limits: KInner, typ: KInner) -> None:
# When
t = wasm2kast.table(input)

# Then
assert limits == t.args[0]
assert typ == t.args[1]

0 comments on commit 37b5b28

Please sign in to comment.