Skip to content
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
48 changes: 45 additions & 3 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ def test_smoke(self):
config.wasm_multi_memory = True
config.wasm_memory64 = True
config.wasm_exceptions = True
config.cranelift_debug_verifier = True
config.wasm_component_model = True
config.wasm_component_model_map = True
config.wasm_function_references = True
config.wasm_wide_arithmetic = True
config.wasm_custom_page_sizes = True
config.wasm_gc = True
config.wasm_stack_switching = True
config.wasm_relaxed_simd = True
config.wasm_relaxed_simd_deterministic = True

config.strategy = "cranelift"
config.strategy = "auto"
config.cache = True
Expand All @@ -32,11 +41,44 @@ def test_smoke(self):
with self.assertRaises(WasmtimeError):
config.cranelift_opt_level = "nonexistent-level"
config.profiler = "none"
config.profiler = "jitdump"
config.profiler = "vtune"
config.profiler = "perfmap"
with self.assertRaises(WasmtimeError):
config.profiler = "nonexistent-profiler"

config.cranelift_debug_verifier = True
config.cranelift_nan_canonicalization = True
config.cranelift_flag_enable("preserve_frame_pointers")
config.cranelift_flag_set("opt_level", "speed")

config.consume_fuel = True
config.wasm_relaxed_simd = True
config.wasm_relaxed_simd_deterministic = True
config.max_wasm_stack = 1024 * 1024
config.gc_support = True
config.native_unwind_info = True
config.macos_use_mach_ports = True
config.signals_based_traps = True

config.memory_may_move = True
config.memory_reservation = 1 << 20
config.memory_guard_size = 1 << 16
config.memory_reservation_for_growth = 1 << 20
config.memory_init_cow = True

def test_target(self):
config = Config()
# Setting the host target should always succeed
import platform
host_triple = None
if platform.machine() == "x86_64" and platform.system() == "Linux":
host_triple = "x86_64-unknown-linux-gnu"
elif platform.machine() == "aarch64" and platform.system() == "Linux":
host_triple = "aarch64-unknown-linux-gnu"
if host_triple is not None:
config.target = host_triple
# An invalid target should raise
with self.assertRaises(WasmtimeError):
config.target = "not-a-real-target"

with closing(config) as config:
pass
Expand Down
60 changes: 60 additions & 0 deletions tests/test_linker.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,63 @@ def call():
assert(called['hits'] == 1)
linker.instantiate(Store(engine), module)
assert(called['hits'] == 2)

def test_define_unknown_imports_as_traps(self):
engine = Engine()
linker = Linker(engine)
module = Module(engine, """
(module
(import "env" "missing" (func))
)
""")
linker.define_unknown_imports_as_traps(module)
store = Store(engine)
instance = linker.instantiate(store, module)
self.assertIsNotNone(instance)

with self.assertRaises(TypeError):
linker.define_unknown_imports_as_traps("not a module") # type: ignore

def test_define_unknown_imports_as_default_values(self):
engine = Engine()
linker = Linker(engine)
module = Module(engine, """
(module
(import "env" "missing" (func (result i32)))
)
""")
store = Store(engine)
linker.define_unknown_imports_as_default_values(store, module)
instance = linker.instantiate(store, module)
self.assertIsNotNone(instance)

with self.assertRaises(TypeError):
linker.define_unknown_imports_as_default_values(store, "not a module") # type: ignore

def test_instantiate_pre(self):
engine = Engine()
linker = Linker(engine)
module = Module(engine, """
(module
(func (export "f") (result i32)
i32.const 42
)
)
""")
pre = linker.instantiate_pre(module)
self.assertIsNotNone(pre)

store1 = Store(engine)
instance1 = pre.instantiate(store1)
f1 = instance1.exports(store1)["f"]
assert isinstance(f1, Func)
self.assertEqual(f1(store1), 42)

store2 = Store(engine)
instance2 = pre.instantiate(store2)
f2 = instance2.exports(store2)["f"]
assert isinstance(f2, Func)
self.assertEqual(f2(store2), 42)

with self.assertRaises(TypeError):
linker.instantiate_pre("not a module") # type: ignore
20 changes: 20 additions & 0 deletions tests/test_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,23 @@ def test_slices(self):
self.assertEqual(memory.write(store, ba, -ba_size), ba_size)
out = memory.read(store, -ba_size)
self.assertEqual(ba, out)

def test_page_size_default(self):
store = Store()
ty = MemoryType(Limits(1, None))
self.assertEqual(ty.page_size, 65536)
self.assertEqual(ty.page_size_log2, 16)
memory = Memory(store, ty)
self.assertEqual(memory.page_size(store), 65536)
self.assertEqual(memory.page_size_log2(store), 16)

def test_page_size_custom(self):
config = Config()
config.wasm_custom_page_sizes = True
store = Store(Engine(config))
ty = MemoryType(Limits(1, None), page_size_log2=0)
self.assertEqual(ty.page_size, 1)
self.assertEqual(ty.page_size_log2, 0)
memory = Memory(store, ty)
self.assertEqual(memory.page_size(store), 1)
self.assertEqual(memory.page_size_log2(store), 0)
79 changes: 79 additions & 0 deletions tests/test_tag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import unittest
from wasmtime import *


class TestTag(unittest.TestCase):
def test_new(self):
store = Store()
functype = FuncType([ValType.i32()], [])
tagtype = TagType(functype)
tag = Tag(store, tagtype)
self.assertIsNotNone(tag)

def test_type(self):
functype = FuncType([ValType.i32()], [])
tagtype = TagType(functype)

self.assertEqual(tagtype.functype.params, [ValType.i32()])
self.assertEqual(tagtype.functype.results, [])

config = Config()
config.wasm_exceptions = True
engine = Engine(config)

module = Module(engine, """
(module
(tag (export "mytag") (param i32))
)
""")
ty = module.exports[0].type
assert isinstance(ty, TagType)
self.assertEqual(ty.functype.params, [ValType.i32()])
self.assertEqual(ty.functype.results, [])

store = Store(engine)
tag = Tag(store, tagtype)
retrieved = tag.type(store)
self.assertIsInstance(retrieved, TagType)

def test_eq(self):
store = Store()
functype = FuncType([ValType.i32()], [])
tagtype = TagType(functype)
tag1 = Tag(store, tagtype)
tag2 = Tag(store, tagtype)
self.assertFalse(tag1.eq(store, tag2))
self.assertTrue(tag1.eq(store, tag1))

def test_wrong_type(self):
store = Store()
with self.assertRaises(TypeError):
Tag(store, "not a tagtype") # type: ignore

def test_tag_import(self):
config = Config()
config.wasm_exceptions = True
engine = Engine(config)

module = Module(engine, """
(module (import "" "" (tag)))
""")
store = Store(engine)
with self.assertRaises(WasmtimeError):
Instance(store, module, [])
Instance(store, module, [Tag(store, TagType(FuncType([], [])))])

def test_tag_export(self):
config = Config()
config.wasm_exceptions = True
engine = Engine(config)

module = Module(engine, """
(module (tag (export "")))
""")
store = Store(engine)
i = Instance(store, module, [])
tag = i.exports(store)['']
assert isinstance(tag, Tag)
self.assertEqual(tag.type(store).functype.params, [])
self.assertEqual(tag.type(store).functype.results, [])
7 changes: 6 additions & 1 deletion wasmtime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ._engine import Engine
from ._store import Store, Storelike, StoreContext
from ._types import FuncType, GlobalType, MemoryType, TableType
from ._types import ValType, Limits, ImportType, ExportType
from ._types import ValType, Limits, ImportType, ExportType, TagType
from ._wat2wasm import wat2wasm
from ._module import Module
from ._value import Val
Expand All @@ -31,6 +31,8 @@
from ._instance import Instance
from ._wasi import WasiConfig, FilePerms, DirPerms
from ._linker import Linker
from ._tag import Tag
from ._instance_pre import InstancePre
from ._sharedmemory import SharedMemory

__all__ = [
Expand Down Expand Up @@ -65,4 +67,7 @@
'Linker',
'WasmtimeError',
'StoreContext',
'TagType',
'Tag',
'InstancePre',
]
Loading
Loading