1# test passing addresses to viper 2 3 4@micropython.viper 5def get_addr(x: ptr) -> ptr: 6 return x 7 8 9@micropython.viper 10def memset(dest: ptr8, c: int, n: int): 11 for i in range(n): 12 dest[i] = c 13 14 15@micropython.viper 16def memsum(src: ptr8, n: int) -> int: 17 s = 0 18 for i in range(n): 19 s += src[i] 20 return s 21 22 23# create array and get its address 24ar = bytearray("0000") 25addr = get_addr(ar) 26print(type(ar)) 27print(type(addr)) 28print(ar) 29 30# pass array as an object 31memset(ar, ord("1"), len(ar)) 32print(ar) 33 34# pass direct pointer to array buffer 35memset(addr, ord("2"), len(ar)) 36print(ar) 37 38# pass direct pointer to array buffer, with offset 39memset(addr + 2, ord("3"), len(ar) - 2) 40print(ar) 41 42# pass a read-only bytes object in 43print(memsum(b"\x01\x02\x03\x04", 4)) 44