Coverage for wasmtime/bindgen/generated/intrinsics.py: 84%

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 _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 _encode_utf8(val: str, realloc: wasmtime.Func, mem: wasmtime.Memory, store: wasmtime.Storelike) -> Tuple[int, int]: 

24 bytes = val.encode('utf8') 

25 ptr = realloc(store, 0, 0, 1, len(bytes)) 

26 assert(isinstance(ptr, int)) 

27 ptr = ptr & 0xffffffff 

28 if ptr + len(bytes) > mem.data_len(store): 

29 raise IndexError('string out of bounds') 

30 base = mem.data_ptr(store) 

31 base = ctypes.POINTER(ctypes.c_ubyte)( 

32 ctypes.c_ubyte.from_address(ctypes.addressof(base.contents) + ptr) 

33 ) 

34 ctypes.memmove(base, bytes, len(bytes)) 

35 return (ptr, len(bytes)) 

36 

37 

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

39 total_size = size * len(list) 

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

41 assert(isinstance(ptr, int)) 

42 ptr = ptr & 0xffffffff 

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

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

45 raw_base = mem.data_ptr(store) 

46 base = ctypes.POINTER(ty)( 

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

48 ) 

49 for i, val in enumerate(list): 

50 base[i] = val 

51 return (ptr, len(list)) 

52 

53 

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

55 ptr = ptr & 0xffffffff 

56 len = len & 0xffffffff 

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

58 raise IndexError('list out of bounds') 

59 raw_base = mem.data_ptr(store) 

60 base = ctypes.POINTER(ty)( 

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

62 ) 

63 if ty == ctypes.c_uint8: 

64 return ctypes.string_at(base, len) 

65 return base[:len] 

66 

67 

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

69 ptr = (base & 0xffffffff) + offset 

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

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

72 raw_base = mem.data_ptr(store) 

73 c_ptr = ctypes.POINTER(ty)( 

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

75 ) 

76 return c_ptr[0] 

77 

78 

79def _decode_utf8(mem: wasmtime.Memory, store: wasmtime.Storelike, ptr: int, len: int) -> str: 

80 ptr = ptr & 0xffffffff 

81 len = len & 0xffffffff 

82 if ptr + len > mem.data_len(store): 

83 raise IndexError('string out of bounds') 

84 base = mem.data_ptr(store) 

85 base = ctypes.POINTER(ctypes.c_ubyte)( 

86 ctypes.c_ubyte.from_address(ctypes.addressof(base.contents) + ptr) 

87 ) 

88 return ctypes.string_at(base, len).decode('utf-8') 

89