Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix table type #564

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]