mirror of
https://github.com/micropython/micropython.git
synced 2026-01-07 20:50:14 +01:00
ffi: Implement ffivar.get()/set() methods.
Done by introducing another factored out helper API in binary.c. This API can be reused also by array and struct modules.
This commit is contained in:
96
py/binary.c
Normal file
96
py/binary.c
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <assert.h>
|
||||
|
||||
#include "misc.h"
|
||||
#include "mpconfig.h"
|
||||
#include "qstr.h"
|
||||
#include "obj.h"
|
||||
#include "objint.h"
|
||||
#include "binary.h"
|
||||
|
||||
// Helpers to work with binary-encoded data
|
||||
|
||||
mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
|
||||
int val = 0;
|
||||
switch (typecode) {
|
||||
case 'b':
|
||||
val = ((int8_t*)p)[index];
|
||||
break;
|
||||
case BYTEARRAY_TYPECODE:
|
||||
case 'B':
|
||||
val = ((uint8_t*)p)[index];
|
||||
break;
|
||||
case 'h':
|
||||
val = ((int16_t*)p)[index];
|
||||
break;
|
||||
case 'H':
|
||||
val = ((uint16_t*)p)[index];
|
||||
break;
|
||||
case 'i':
|
||||
case 'l':
|
||||
return mp_obj_new_int(((int32_t*)p)[index]);
|
||||
case 'I':
|
||||
case 'L':
|
||||
return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
|
||||
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
||||
case 'q':
|
||||
case 'Q':
|
||||
// TODO: Explode API more to cover signedness
|
||||
return mp_obj_new_int_from_ll(((long long*)p)[index]);
|
||||
#endif
|
||||
#if MICROPY_ENABLE_FLOAT
|
||||
case 'f':
|
||||
return mp_obj_new_float(((float*)p)[index]);
|
||||
case 'd':
|
||||
return mp_obj_new_float(((double*)p)[index]);
|
||||
#endif
|
||||
}
|
||||
return MP_OBJ_NEW_SMALL_INT(val);
|
||||
}
|
||||
|
||||
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
|
||||
machine_int_t val = 0;
|
||||
if (MP_OBJ_IS_INT(val_in)) {
|
||||
val = mp_obj_int_get(val_in);
|
||||
}
|
||||
|
||||
switch (typecode) {
|
||||
case 'b':
|
||||
((int8_t*)p)[index] = val;
|
||||
break;
|
||||
case BYTEARRAY_TYPECODE:
|
||||
case 'B':
|
||||
val = ((uint8_t*)p)[index] = val;
|
||||
break;
|
||||
case 'h':
|
||||
val = ((int16_t*)p)[index] = val;
|
||||
break;
|
||||
case 'H':
|
||||
val = ((uint16_t*)p)[index] = val;
|
||||
break;
|
||||
case 'i':
|
||||
case 'l':
|
||||
((int32_t*)p)[index] = val;
|
||||
break;
|
||||
case 'I':
|
||||
case 'L':
|
||||
((uint32_t*)p)[index] = val;
|
||||
break;
|
||||
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
||||
case 'q':
|
||||
case 'Q':
|
||||
assert(0);
|
||||
((long long*)p)[index] = val;
|
||||
break;
|
||||
#endif
|
||||
#if MICROPY_ENABLE_FLOAT
|
||||
case 'f':
|
||||
((float*)p)[index] = mp_obj_float_get(val_in);
|
||||
break;
|
||||
case 'd':
|
||||
((double*)p)[index] = mp_obj_float_get(val_in);
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
7
py/binary.h
Normal file
7
py/binary.h
Normal file
@@ -0,0 +1,7 @@
|
||||
// Use special typecode to differentiate repr() of bytearray vs array.array('B')
|
||||
// (underlyingly they're same).
|
||||
#define BYTEARRAY_TYPECODE 0
|
||||
|
||||
int mp_binary_get_size(char typecode);
|
||||
mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
|
||||
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);
|
||||
@@ -8,3 +8,7 @@ typedef struct _mp_obj_int_t {
|
||||
void int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
|
||||
mp_obj_t int_unary_op(int op, mp_obj_t o_in);
|
||||
mp_obj_t int_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in);
|
||||
|
||||
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
||||
mp_obj_t mp_obj_new_int_from_ll(long long val);
|
||||
#endif
|
||||
|
||||
@@ -13,8 +13,6 @@
|
||||
|
||||
#if MICROPY_LONGINT_IMPL == MICROPY_LONGINT_IMPL_LONGLONG
|
||||
|
||||
STATIC mp_obj_t mp_obj_new_int_from_ll(long long val);
|
||||
|
||||
// Python3 no longer has "l" suffix for long ints. We allow to use it
|
||||
// for debugging purpose though.
|
||||
#ifdef DEBUG
|
||||
|
||||
Reference in New Issue
Block a user