Coverage for tests/bindgen/generated/export_resources/intrinsics.py: 70%
40 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
1import ctypes
2from typing import Any, Tuple
3import wasmtime
6def _encode_utf8(val: str, realloc: wasmtime.Func, mem: wasmtime.Memory, store: wasmtime.Storelike) -> Tuple[int, int]:
7 bytes = val.encode('utf8')
8 ptr = realloc(store, 0, 0, 1, len(bytes))
9 assert(isinstance(ptr, int))
10 ptr = ptr & 0xffffffff
11 if ptr + len(bytes) > mem.data_len(store):
12 raise IndexError('string out of bounds')
13 base = mem.data_ptr(store)
14 base = ctypes.POINTER(ctypes.c_ubyte)(
15 ctypes.c_ubyte.from_address(ctypes.addressof(base.contents) + ptr)
16 )
17 ctypes.memmove(base, bytes, len(bytes))
18 return (ptr, len(bytes))
21def _load(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int) -> Any:
22 ptr = (base & 0xffffffff) + offset
23 if ptr + ctypes.sizeof(ty) > mem.data_len(store):
24 raise IndexError('out-of-bounds store')
25 raw_base = mem.data_ptr(store)
26 c_ptr = ctypes.POINTER(ty)(
27 ty.from_address(ctypes.addressof(raw_base.contents) + ptr)
28 )
29 return c_ptr[0]
32def _decode_utf8(mem: wasmtime.Memory, store: wasmtime.Storelike, ptr: int, len: int) -> str:
33 ptr = ptr & 0xffffffff
34 len = len & 0xffffffff
35 if ptr + len > mem.data_len(store):
36 raise IndexError('string out of bounds')
37 base = mem.data_ptr(store)
38 base = ctypes.POINTER(ctypes.c_ubyte)(
39 ctypes.c_ubyte.from_address(ctypes.addressof(base.contents) + ptr)
40 )
41 return ctypes.string_at(base, len).decode('utf-8')
44def _store(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int, val: Any) -> None:
45 ptr = (base & 0xffffffff) + offset
46 if ptr + ctypes.sizeof(ty) > mem.data_len(store):
47 raise IndexError('out-of-bounds store')
48 raw_base = mem.data_ptr(store)
49 c_ptr = ctypes.POINTER(ty)(
50 ty.from_address(ctypes.addressof(raw_base.contents) + ptr)
51 )
52 c_ptr[0] = val
55def _clamp(i: int, min: int, max: int) -> int:
56 if i < min or i > max:
57 raise OverflowError(f'must be between {min} and {max}')
58 return i