Coverage for tests/codegen/generated/lists/intrinsics.py: 90%

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

16 

17 

18def _encode_utf8(val: str, realloc: wasmtime.Func, mem: wasmtime.Memory, store: wasmtime.Storelike) -> Tuple[int, int]: 

19 bytes = val.encode('utf8') 

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

21 assert(isinstance(ptr, int)) 

22 ptr = ptr & 0xffffffff 

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

24 raise IndexError('string out of bounds') 

25 base = mem.data_ptr(store) 

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

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

28 ) 

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

30 return (ptr, len(bytes)) 

31 

32 

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

34 ptr = (base & 0xffffffff) + offset 

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

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

37 raw_base = mem.data_ptr(store) 

38 c_ptr = ctypes.POINTER(ty)( 

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

40 ) 

41 c_ptr[0] = val 

42 

43 

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

45 ptr = ptr & 0xffffffff 

46 len = len & 0xffffffff 

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

48 raise IndexError('list out of bounds') 

49 raw_base = mem.data_ptr(store) 

50 base = ctypes.POINTER(ty)( 

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

52 ) 

53 if ty == ctypes.c_uint8: 

54 return ctypes.string_at(base, len) 

55 return base[:len] 

56 

57 

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

59 total_size = size * len(list) 

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

61 assert(isinstance(ptr, int)) 

62 ptr = ptr & 0xffffffff 

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

64 raise IndexError('list realloc return 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 for i, val in enumerate(list): 

70 base[i] = val 

71 return (ptr, len(list)) 

72 

73 

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

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 return c_ptr[0] 

83