From 2ab574706feaab30d9f59366a98760ad5c73a055 Mon Sep 17 00:00:00 2001 From: "Johan B.W. de Vries" Date: Sat, 24 Dec 2022 20:00:24 +0100 Subject: [PATCH] Updated main, buffer, added test --- examples/buffer.py | 2 +- phasm/__main__.py | 2 ++ tests/integration/helpers.py | 4 ++-- .../integration/test_examples/test_buffer.py | 19 +++++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 tests/integration/test_examples/test_buffer.py diff --git a/examples/buffer.py b/examples/buffer.py index 6f251ff..8fe6570 100644 --- a/examples/buffer.py +++ b/examples/buffer.py @@ -3,5 +3,5 @@ def index(inp: bytes, idx: u32) -> u8: return inp[idx] @exported -def length(inp: bytes) -> i32: +def length(inp: bytes) -> u32: return len(inp) diff --git a/phasm/__main__.py b/phasm/__main__.py index bc94cdb..97615e0 100644 --- a/phasm/__main__.py +++ b/phasm/__main__.py @@ -5,6 +5,7 @@ Functions for using this module from CLI import sys from .parser import phasm_parse +from .type3.entry import phasm_type3 from .compiler import phasm_compile def main(source: str, sink: str) -> int: @@ -16,6 +17,7 @@ def main(source: str, sink: str) -> int: code_py = fil.read() our_module = phasm_parse(code_py) + phasm_type3(our_module, verbose=False) wasm_module = phasm_compile(our_module) code_wat = wasm_module.to_wat() diff --git a/tests/integration/helpers.py b/tests/integration/helpers.py index ca4c8a7..3cd682c 100644 --- a/tests/integration/helpers.py +++ b/tests/integration/helpers.py @@ -24,7 +24,7 @@ class Suite: def __init__(self, code_py): self.code_py = code_py - def run_code(self, *args, runtime='pywasm3', imports=None): + def run_code(self, *args, runtime='pywasm3', func_name='testEntry', imports=None): """ Compiles the given python code into wasm and then runs it @@ -74,7 +74,7 @@ class Suite: runner.interpreter_dump_memory(sys.stderr) result = SuiteResult() - result.returned_value = runner.call('testEntry', *wasm_args) + result.returned_value = runner.call(func_name, *wasm_args) write_header(sys.stderr, 'Memory (post run)') runner.interpreter_dump_memory(sys.stderr) diff --git a/tests/integration/test_examples/test_buffer.py b/tests/integration/test_examples/test_buffer.py new file mode 100644 index 0000000..b987c69 --- /dev/null +++ b/tests/integration/test_examples/test_buffer.py @@ -0,0 +1,19 @@ +import pytest + +from ..helpers import Suite + +@pytest.mark.slow_integration_test +def test_index(): + with open('examples/buffer.py', 'r', encoding='ASCII') as fil: + code_py = "\n" + fil.read() + + result = Suite(code_py).run_code(b'Hello, world!', 5, func_name='index', runtime='wasmtime') + assert 44 == result.returned_value + +@pytest.mark.slow_integration_test +def test_length(): + with open('examples/buffer.py', 'r', encoding='ASCII') as fil: + code_py = "\n" + fil.read() + + result = Suite(code_py).run_code(b'Hello, world!', func_name='length') + assert 13 == result.returned_value