Coverage for tests/codegen/test_lists.py: 100%
31 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-02-20 16:25 +0000
1from . import bindgen, REALLOC
2from wasmtime import Store
3from typing import List
5module = """
6 (component
7 (import "host" (instance $i
8 (export "strings" (func (param "a" string) (result string)))
9 (export "bytes" (func (param "a" (list u8)) (result (list u8))))
10 (export "ints" (func (param "a" (list u32)) (result (list u32))))
11 (export "string-list" (func (param "a" (list string)) (result (list string))))
12 ))
14 (core module $libc
15 (memory (export "mem") 1)
16 {}
17 )
18 (core instance $libc (instantiate $libc))
20 (core func $strings (canon lower (func $i "strings")
21 (memory $libc "mem") (realloc (func $libc "realloc"))))
22 (core func $bytes (canon lower (func $i "bytes")
23 (memory $libc "mem") (realloc (func $libc "realloc"))))
24 (core func $ints (canon lower (func $i "ints")
25 (memory $libc "mem") (realloc (func $libc "realloc"))))
26 (core func $string-list (canon lower (func $i "string-list")
27 (memory $libc "mem") (realloc (func $libc "realloc"))))
29 (core module $m
30 (import "libc" "mem" (memory 1))
31 (import "" "strings" (func $strings (param i32 i32 i32)))
32 (import "" "bytes" (func $bytes (param i32 i32 i32)))
33 (import "" "ints" (func $ints (param i32 i32 i32)))
34 (import "" "string-list" (func $string-list (param i32 i32 i32)))
36 (func (export "strings") (param i32 i32) (result i32)
37 (call $strings (local.get 0) (local.get 1) (i32.const 8))
38 i32.const 8)
39 (func (export "bytes") (param i32 i32) (result i32)
40 (call $bytes (local.get 0) (local.get 1) (i32.const 8))
41 i32.const 8)
42 (func (export "ints") (param i32 i32) (result i32)
43 (call $ints (local.get 0) (local.get 1) (i32.const 8))
44 i32.const 8)
45 (func (export "string-list") (param i32 i32) (result i32)
46 (call $string-list (local.get 0) (local.get 1) (i32.const 8))
47 i32.const 8)
48 )
50 (core instance $i (instantiate $m
51 (with "libc" (instance $libc))
52 (with "" (instance
53 (export "strings" (func $strings))
54 (export "bytes" (func $bytes))
55 (export "ints" (func $ints))
56 (export "string-list" (func $string-list))
57 ))
58 ))
60 (func (export "strings") (param "a" string) (result string)
61 (canon lift (core func $i "strings")
62 (memory $libc "mem") (realloc (func $libc "realloc"))))
63 (func (export "bytes") (param "a" (list u8)) (result (list u8))
64 (canon lift (core func $i "bytes")
65 (memory $libc "mem") (realloc (func $libc "realloc"))))
66 (func (export "ints") (param "a" (list u32)) (result (list u32))
67 (canon lift (core func $i "ints")
68 (memory $libc "mem") (realloc (func $libc "realloc"))))
69 (func (export "string-list") (param "a" (list string)) (result (list string))
70 (canon lift (core func $i "string-list")
71 (memory $libc "mem") (realloc (func $libc "realloc"))))
72 )
73""".format(REALLOC)
74bindgen('lists', module)
76from .generated.lists import Root, RootImports, imports
79class Host(imports.HostHost):
80 def strings(self, a: str) -> str:
81 return a
83 def bytes(self, a: bytes) -> bytes:
84 return a
86 def ints(self, a: List[int]) -> List[int]:
87 return a
89 def string_list(self, a: List[str]) -> List[str]:
90 return a
93def test_bindings():
94 store = Store()
95 wasm = Root(store, RootImports(host=Host()))
97 assert wasm.strings(store, '') == ''
98 assert wasm.strings(store, 'a') == 'a'
99 assert wasm.strings(store, 'hello world') == 'hello world'
100 assert wasm.strings(store, 'hello ⚑ world') == 'hello ⚑ world'
102 assert wasm.bytes(store, b'') == b''
103 assert wasm.bytes(store, b'a') == b'a'
104 assert wasm.bytes(store, b'\x01\x02') == b'\x01\x02'
106 assert wasm.ints(store, []) == []
107 assert wasm.ints(store, [1]) == [1]
108 assert wasm.ints(store, [1, 2, 100, 10000]) == [1, 2, 100, 10000]
110 assert wasm.string_list(store, []) == []
111 assert wasm.string_list(store, ['']) == ['']
112 assert wasm.string_list(store, ['a', 'b', '', 'd', 'hello']) == ['a', 'b', '', 'd', 'hello']