summaryrefslogtreecommitdiff
path: root/tests/micropython/viper_addr.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-02-09 13:29:20 +0000
committerDamien George <damien.p.george@gmail.com>2016-02-09 13:29:20 +0000
commit3e02b1d19a4b1fb36ee36b0d43144bad6b797f2f (patch)
tree5545397c4105c818838c6b18c664b4f82f77cb56 /tests/micropython/viper_addr.py
parent9e78ab4b866c8bc4cf8b1bc21b848aadc64097cb (diff)
py/viper: Allow casting of Python integers to viper pointers.
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.
Diffstat (limited to 'tests/micropython/viper_addr.py')
-rw-r--r--tests/micropython/viper_addr.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/micropython/viper_addr.py b/tests/micropython/viper_addr.py
new file mode 100644
index 000000000..cd953ce07
--- /dev/null
+++ b/tests/micropython/viper_addr.py
@@ -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)