Browse Source
This allows you to pass a number (being an address) to a viper function that expects a pointer, and also allows casting of integers to pointers within viper functions. This was actually the original behaviour, but it regressed due to native type identifiers being promoted to 4 bits in width.pull/1820/merge
Damien George
9 years ago
3 changed files with 42 additions and 3 deletions
@ -0,0 +1,29 @@ |
|||
# test passing addresses to viper |
|||
|
|||
@micropython.viper |
|||
def get_addr(x:ptr) -> ptr: |
|||
return x |
|||
|
|||
@micropython.viper |
|||
def memset(dest:ptr8, c:int, n:int): |
|||
for i in range(n): |
|||
dest[i] = c |
|||
|
|||
# create array and get its address |
|||
ar = bytearray('0000') |
|||
addr = get_addr(ar) |
|||
print(type(ar)) |
|||
print(type(addr)) |
|||
print(ar) |
|||
|
|||
# pass array as an object |
|||
memset(ar, ord('1'), len(ar)) |
|||
print(ar) |
|||
|
|||
# pass direct pointer to array buffer |
|||
memset(addr, ord('2'), len(ar)) |
|||
print(ar) |
|||
|
|||
# pass direct pointer to array buffer, with offset |
|||
memset(addr + 2, ord('3'), len(ar) - 2) |
|||
print(ar) |
@ -0,0 +1,6 @@ |
|||
<class 'bytearray'> |
|||
<class 'int'> |
|||
bytearray(b'0000') |
|||
bytearray(b'1111') |
|||
bytearray(b'2222') |
|||
bytearray(b'2233') |
Loading…
Reference in new issue