To write a native function, declare a python function with the expected arguments and the body has the "pass" statement.
Inside this function's doc string, put the magic sequence "__NATIVE__" and follow that with C code.
Notice that two underscore are prepended and two are appended. Four underscores total.
The __NATIVE__ lets the build system know that it should process this function in a special way
and it will take care of everything for you.
Inside the C code, use special macros to access the arguments:
NATIVE_GET_NUM_ARGS() // "returns " a C int
NATIVE_GET_LOCAL(n) // where n is a C int. Returns the Python object at argument index n.
Remember arguments are on a stack so the numbering may be reverse of what you think.
Then do whatever stuff in C you need to do.
The most important thing is that if you do more than one allocation from the VM's heap,
you'll need to protect those items because they aren't connected to the VM's roots
and the later allocations may reclaim the earlier allocations.
Use heap_gcPushTempRoot() and heap_gcPopTempRoot() for this protection.
Follow example in src/lib/sys.py sys.heap().
Finally, use NATIVE_SET_TOS() to put a python object on the top of the stack.
This wil serve as the native function's return value for Python.
Then return PM_RET_OK if all is well or another value of type PmReturn_t to signal an exception.
regards,
!!Dean
> --
> --
> You are subscribed to the "python-on-a-chip" (or p14p for short) Google Group.
> Site:
http://groups.google.com/group/python-on-a-chip
>
> ---
> You received this message because you are subscribed to the Google Groups "python-on-a-chip" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
python-on-a-ch...@googlegroups.com.
> For more options, visit
https://groups.google.com/groups/opt_out.