Coverage for tests/codegen/generated/external_types/intrinsics.py: 89%
36 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 _decode_utf8(mem: wasmtime.Memory, store: wasmtime.Storelike, ptr: int, len: int) -> str:
7 ptr = ptr & 0xffffffff
8 len = len & 0xffffffff
9 if ptr + len > mem.data_len(store):
10 raise IndexError('string out of bounds')
11 base = mem.data_ptr(store)
12 base = ctypes.POINTER(ctypes.c_ubyte)(
13 ctypes.c_ubyte.from_address(ctypes.addressof(base.contents) + ptr)
14 )
15 return ctypes.string_at(base, len).decode('utf-8')
18def _store(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int, val: Any) -> None:
19 ptr = (base & 0xffffffff) + offset
20 if ptr + ctypes.sizeof(ty) > mem.data_len(store):
21 raise IndexError('out-of-bounds store')
22 raw_base = mem.data_ptr(store)
23 c_ptr = ctypes.POINTER(ty)(
24 ty.from_address(ctypes.addressof(raw_base.contents) + ptr)
25 )
26 c_ptr[0] = val
29def _encode_utf8(val: str, realloc: wasmtime.Func, mem: wasmtime.Memory, store: wasmtime.Storelike) -> Tuple[int, int]:
30 bytes = val.encode('utf8')
31 ptr = realloc(store, 0, 0, 1, len(bytes))
32 assert(isinstance(ptr, int))
33 ptr = ptr & 0xffffffff
34 if ptr + len(bytes) > mem.data_len(store):
35 raise IndexError('string out of bounds')
36 base = mem.data_ptr(store)
37 base = ctypes.POINTER(ctypes.c_ubyte)(
38 ctypes.c_ubyte.from_address(ctypes.addressof(base.contents) + ptr)
39 )
40 ctypes.memmove(base, bytes, len(bytes))
41 return (ptr, len(bytes))
44def _load(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int) -> Any:
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 return c_ptr[0]