summaryrefslogtreecommitdiffstatshomepage
path: root/py/binary.c
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-20 05:19:10 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2014-04-20 13:08:33 +0300
commit0f836ef893f54726580589951ae46638eb7afff8 (patch)
treed075e2b8ddacdb5520d3e31869fb07395ee1c29a /py/binary.c
parent4e4fa94c4c5f1558537426c92438aa629b75d58b (diff)
downloadmicropython-0f836ef893f54726580589951ae46638eb7afff8.tar.gz
micropython-0f836ef893f54726580589951ae46638eb7afff8.zip
modstruct: Add 'O' typecode for passing mp_obj_t.
Useful as callback data, etc.
Diffstat (limited to 'py/binary.c')
-rw-r--r--py/binary.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/py/binary.c b/py/binary.c
index d8277669d0..e57766b41c 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -49,6 +49,8 @@ int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
case 'q': case 'Q':
// TODO: This is for x86
align = sizeof(int); size = sizeof(long long); break;
+ case 'P': case 'O':
+ align = size = sizeof(void*); break;
}
}
}
@@ -131,7 +133,9 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
*ptr += size;
- if (is_signed(val_type)) {
+ if (val_type == 'O') {
+ return (mp_obj_t)val;
+ } else if (is_signed(val_type)) {
return mp_obj_new_int(val);
} else {
return mp_obj_new_int_from_uint(val);
@@ -156,8 +160,16 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
#if MP_ENDIANNESS_BIG
#error Not implemented
#endif
- machine_int_t val = mp_obj_get_int(val_in);
+ machine_int_t val;
byte *in = (byte*)&val;
+ switch (val_type) {
+ case 'O':
+ in = (byte*)&val_in;
+ break;
+ default:
+ val = mp_obj_get_int(val_in);
+ }
+
int in_delta, out_delta;
uint val_sz = MIN(size, sizeof(val));
if (struct_type == '>') {