fix 3169: Polygon.points property

The getter for vectorio.Polygon#points was not updated with the data type change of the stored points list.

This moves the implementation to shared_module and updates the data type to reflect the actual state.
This commit is contained in:
Kenny 2020-07-19 12:27:35 -07:00
parent 9cdf5e148a
commit efeae0d84f
2 changed files with 19 additions and 17 deletions

View File

@ -49,20 +49,7 @@ static mp_obj_t vectorio_polygon_make_new(const mp_obj_type_t *type, size_t n_ar
//| //|
STATIC mp_obj_t vectorio_polygon_obj_get_points(mp_obj_t self_in) { STATIC mp_obj_t vectorio_polygon_obj_get_points(mp_obj_t self_in) {
vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in); vectorio_polygon_t *self = MP_OBJ_TO_PTR(self_in);
mp_obj_t list = mp_obj_new_list(0, NULL); return common_hal_vectorio_polygon_get_points(self);
size_t len = 0;
mp_obj_t *items;
mp_obj_list_get(common_hal_vectorio_polygon_get_points(self), &len, &items);
for (size_t i = 0; i < len; i += 2) {
mp_obj_t tuple[] = { items[i], items[i+1] };
mp_obj_list_append(
list,
mp_obj_new_tuple(2, tuple)
);
}
return list;
} }
MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_points_obj, vectorio_polygon_obj_get_points); MP_DEFINE_CONST_FUN_OBJ_1(vectorio_polygon_get_points_obj, vectorio_polygon_obj_get_points);

View File

@ -20,7 +20,7 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
size_t len = 0; size_t len = 0;
mp_obj_t *items; mp_obj_t *items;
mp_obj_list_get(points_tuple_list, &len, &items); mp_obj_list_get(points_tuple_list, &len, &items);
VECTORIO_POLYGON_DEBUG("polygon_points_list len: %d\n", len); VECTORIO_POLYGON_DEBUG(" self.len: %d, len: %d, ", self->len, len);
if ( len < 3 ) { if ( len < 3 ) {
mp_raise_TypeError_varg(translate("Polygon needs at least 3 points")); mp_raise_TypeError_varg(translate("Polygon needs at least 3 points"));
@ -28,9 +28,11 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
if ( self->len < 2*len ) { if ( self->len < 2*len ) {
if ( self->points_list != NULL ) { if ( self->points_list != NULL ) {
VECTORIO_POLYGON_DEBUG("free(%d), ", sizeof(self->points_list));
gc_free( self->points_list ); gc_free( self->points_list );
} }
self->points_list = gc_alloc( 2 * len * sizeof(int), false, false ); self->points_list = gc_alloc( 2 * len * sizeof(int), false, false );
VECTORIO_POLYGON_DEBUG("alloc(%p, %d)", self->points_list, 2 * len * sizeof(int));
} }
self->len = 2*len; self->len = 2*len;
@ -56,22 +58,35 @@ static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple
void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) { void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) {
VECTORIO_POLYGON_DEBUG("%p polygon_construct\n", self); VECTORIO_POLYGON_DEBUG("%p polygon_construct: ", self);
self->points_list = NULL; self->points_list = NULL;
self->len = 0; self->len = 0;
self->on_dirty.obj = NULL; self->on_dirty.obj = NULL;
_clobber_points_list( self, points_list ); _clobber_points_list( self, points_list );
VECTORIO_POLYGON_DEBUG("\n");
} }
mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) { mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) {
return self->points_list; VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_get_points {len: %d, points_list: %p}\n", self, self->len, self->points_list);
mp_obj_t list = mp_obj_new_list(0, NULL);
for (size_t i = 0; i < self->len; i += 2) {
mp_obj_t tuple[] = { mp_obj_new_int(self->points_list[i]), mp_obj_new_int(self->points_list[i+1]) };
mp_obj_list_append(
list,
mp_obj_new_tuple(2, tuple)
);
}
return list;
} }
void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list) { void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list) {
VECTORIO_POLYGON_DEBUG("%p common_hal_vectorio_polygon_set_points: ", self);
_clobber_points_list( self, points_list ); _clobber_points_list( self, points_list );
if (self->on_dirty.obj != NULL) { if (self->on_dirty.obj != NULL) {
self->on_dirty.event(self->on_dirty.obj); self->on_dirty.event(self->on_dirty.obj);
} }
VECTORIO_POLYGON_DEBUG("\n");
} }
void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification) { void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification) {