From b03c138555c418d016e21debef8ac78725ca3fd3 Mon Sep 17 00:00:00 2001 From: Burak Bilge Yalcinkaya Date: Fri, 19 Jan 2024 01:53:21 +0300 Subject: [PATCH 1/2] fix table ref type --- pykwasm/src/pykwasm/wasm2kast.py | 9 ++++----- pykwasm/src/tests/integration/binary/tables.wat | 9 ++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/pykwasm/src/pykwasm/wasm2kast.py b/pykwasm/src/pykwasm/wasm2kast.py index 4bd63b843..2f45d6ef3 100644 --- a/pykwasm/src/pykwasm/wasm2kast.py +++ b/pykwasm/src/pykwasm/wasm2kast.py @@ -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) @@ -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: diff --git a/pykwasm/src/tests/integration/binary/tables.wat b/pykwasm/src/tests/integration/binary/tables.wat index 5dfd2780f..a5662a90d 100644 --- a/pykwasm/src/tests/integration/binary/tables.wat +++ b/pykwasm/src/tests/integration/binary/tables.wat @@ -1,2 +1,9 @@ (module - (table 1 funcref)) \ No newline at end of file + (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) +) From 5b651304c5a961945b71bdbca40a23505b9ac844 Mon Sep 17 00:00:00 2001 From: Burak Bilge Yalcinkaya Date: Fri, 19 Jan 2024 03:58:02 +0300 Subject: [PATCH 2/2] add table unit test --- pykwasm/pyproject.toml | 1 + pykwasm/src/tests/unit/test_wasm2kast.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 pykwasm/src/tests/unit/test_wasm2kast.py diff --git a/pykwasm/pyproject.toml b/pykwasm/pyproject.toml index 58fb5d506..a62f96f55 100644 --- a/pykwasm/pyproject.toml +++ b/pykwasm/pyproject.toml @@ -55,6 +55,7 @@ disallow_untyped_defs = true exclude = [ 'src/pykwasm/wasm2kast\.py', 'src/wasm/*', + 'src/tests/unit/test_wasm2kast\.py', ] [tool.poetry.scripts] diff --git a/pykwasm/src/tests/unit/test_wasm2kast.py b/pykwasm/src/tests/unit/test_wasm2kast.py new file mode 100644 index 000000000..4c5d188e1 --- /dev/null +++ b/pykwasm/src/tests/unit/test_wasm2kast.py @@ -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]