Updated main, buffer, added test

This commit is contained in:
Johan B.W. de Vries 2022-12-24 20:00:24 +01:00
parent 750f8806b6
commit 2ab574706f
4 changed files with 24 additions and 3 deletions

View File

@ -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)

View File

@ -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()

View File

@ -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)

View File

@ -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