Coverage for tests/bindgen/generated/list_types/intrinsics.py: 85%

62 statements  

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

1import ctypes 

2from typing import Any, List, Tuple 

3import wasmtime 

4 

5 

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)) 

19 

20 

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] 

30 

31 

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') 

42 

43 

44def _list_canon_lower(list: Any, ty: Any, size: int, align: int, realloc: wasmtime.Func, mem: wasmtime.Memory, store: wasmtime.Storelike) -> Tuple[int, int]: 

45 total_size = size * len(list) 

46 ptr = realloc(store, 0, 0, align, total_size) 

47 assert(isinstance(ptr, int)) 

48 ptr = ptr & 0xffffffff 

49 if ptr + total_size > mem.data_len(store): 

50 raise IndexError('list realloc return of bounds') 

51 raw_base = mem.data_ptr(store) 

52 base = ctypes.POINTER(ty)( 

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

54 ) 

55 for i, val in enumerate(list): 

56 base[i] = val 

57 return (ptr, len(list)) 

58 

59 

60def _list_canon_lift(ptr: int, len: int, size: int, ty: Any, mem: wasmtime.Memory ,store: wasmtime.Storelike) -> Any: 

61 ptr = ptr & 0xffffffff 

62 len = len & 0xffffffff 

63 if ptr + len * size > mem.data_len(store): 

64 raise IndexError('list out of bounds') 

65 raw_base = mem.data_ptr(store) 

66 base = ctypes.POINTER(ty)( 

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

68 ) 

69 if ty == ctypes.c_uint8: 

70 return ctypes.string_at(base, len) 

71 return base[:len] 

72 

73 

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

75 ptr = (base & 0xffffffff) + offset 

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

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

78 raw_base = mem.data_ptr(store) 

79 c_ptr = ctypes.POINTER(ty)( 

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

81 ) 

82 c_ptr[0] = val 

83 

84 

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

86 if i < min or i > max: 

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

88 return i 

89