Coverage for tests/codegen/generated/records/intrinsics.py: 86%

21 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-02-20 16:25 +0000

1import ctypes 

2from typing import Any 

3import wasmtime 

4 

5 

6def _clamp(i: int, min: int, max: int) -> int: 

7 if i < min or i > max: 

8 raise OverflowError(f'must be between {min} and {max}') 

9 return i 

10 

11 

12def _store(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int, val: Any) -> None: 

13 ptr = (base & 0xffffffff) + offset 

14 if ptr + ctypes.sizeof(ty) > mem.data_len(store): 

15 raise IndexError('out-of-bounds store') 

16 raw_base = mem.data_ptr(store) 

17 c_ptr = ctypes.POINTER(ty)( 

18 ty.from_address(ctypes.addressof(raw_base.contents) + ptr) 

19 ) 

20 c_ptr[0] = val 

21 

22 

23def _load(ty: Any, mem: wasmtime.Memory, store: wasmtime.Storelike, base: int, offset: int) -> Any: 

24 ptr = (base & 0xffffffff) + offset 

25 if ptr + ctypes.sizeof(ty) > mem.data_len(store): 

26 raise IndexError('out-of-bounds store') 

27 raw_base = mem.data_ptr(store) 

28 c_ptr = ctypes.POINTER(ty)( 

29 ty.from_address(ctypes.addressof(raw_base.contents) + ptr) 

30 ) 

31 return c_ptr[0] 

32