113 lines
2.7 KiB
Python
113 lines
2.7 KiB
Python
import sys
|
|
|
|
import pytest
|
|
|
|
import wasm3
|
|
|
|
from phasm.compiler import phasm_compile
|
|
from phasm.parser import phasm_parse
|
|
|
|
from .helpers import DASHES, wat2wasm, _dump_memory, _write_numbered_lines
|
|
|
|
def setup_interpreter(code_phasm):
|
|
phasm_module = phasm_parse(code_phasm)
|
|
wasm_module = phasm_compile(phasm_module)
|
|
code_wat = wasm_module.to_wat()
|
|
|
|
sys.stderr.write(f'{DASHES} Assembly {DASHES}\n')
|
|
_write_numbered_lines(code_wat)
|
|
|
|
code_wasm = wat2wasm(code_wat)
|
|
|
|
env = wasm3.Environment()
|
|
mod = env.parse_module(code_wasm)
|
|
|
|
rtime = env.new_runtime(1024 * 1024)
|
|
rtime.load(mod)
|
|
|
|
return rtime
|
|
|
|
@pytest.mark.integration_test
|
|
def test___init__():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> u8:
|
|
return 13
|
|
"""
|
|
|
|
rtime = setup_interpreter(code_py)
|
|
|
|
for idx in range(128):
|
|
rtime.get_memory(0)[idx] = idx
|
|
|
|
sys.stderr.write(f'{DASHES} Memory (pre run) {DASHES}\n')
|
|
_dump_memory(rtime.get_memory(0))
|
|
|
|
rtime.find_function('stdlib.alloc.__init__')()
|
|
|
|
sys.stderr.write(f'{DASHES} Memory (pre run) {DASHES}\n')
|
|
_dump_memory(rtime.get_memory(0))
|
|
|
|
memory = rtime.get_memory(0).tobytes()
|
|
|
|
assert (
|
|
b'\xC0\xA1\x00\x00'
|
|
b'\x00\x00\x00\x00'
|
|
b'\x00\x00\x00\x00'
|
|
b'\x10\x00\x00\x00'
|
|
b'\x10\x11\x12\x13' # Untouched because unused
|
|
) == memory[0:20]
|
|
|
|
@pytest.mark.integration_test
|
|
def test___alloc___no_init():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> u8:
|
|
return 13
|
|
"""
|
|
|
|
rtime = setup_interpreter(code_py)
|
|
|
|
for idx in range(128):
|
|
rtime.get_memory(0)[idx] = idx
|
|
|
|
sys.stderr.write(f'{DASHES} Memory (pre run) {DASHES}\n')
|
|
_dump_memory(rtime.get_memory(0))
|
|
|
|
with pytest.raises(RuntimeError, match='unreachable executed'):
|
|
rtime.find_function('stdlib.alloc.__alloc__')(32)
|
|
|
|
@pytest.mark.integration_test
|
|
def test___alloc___ok():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> u8:
|
|
return 13
|
|
"""
|
|
|
|
rtime = setup_interpreter(code_py)
|
|
|
|
for idx in range(128):
|
|
rtime.get_memory(0)[idx] = idx
|
|
|
|
sys.stderr.write(f'{DASHES} Memory (pre run) {DASHES}\n')
|
|
_dump_memory(rtime.get_memory(0))
|
|
|
|
rtime.find_function('stdlib.alloc.__init__')()
|
|
offset0 = rtime.find_function('stdlib.alloc.__alloc__')(32)
|
|
offset1 = rtime.find_function('stdlib.alloc.__alloc__')(32)
|
|
offset2 = rtime.find_function('stdlib.alloc.__alloc__')(32)
|
|
|
|
sys.stderr.write(f'{DASHES} Memory (pre run) {DASHES}\n')
|
|
_dump_memory(rtime.get_memory(0))
|
|
|
|
memory = rtime.get_memory(0).tobytes()
|
|
|
|
assert b'\xC0\xA1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' == memory[0:12]
|
|
|
|
assert 0x14 == offset0
|
|
assert 0x38 == offset1
|
|
assert 0x5C == offset2
|
|
|
|
assert False
|