Skip to content

Commit 2bc260a

Browse files
Rework of changes to bounds checking of location in VectorShape, moving most of the code into shared-module.
1 parent c6f2dae commit 2bc260a

File tree

4 files changed

+44
-26
lines changed

4 files changed

+44
-26
lines changed

shared-bindings/vectorio/VectorShape.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_x(mp_obj_t wrapper_shape, mp_obj_t
9999
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
100100

101101
mp_int_t x = mp_obj_get_int(x_obj);
102-
bool dirty = common_hal_vectorio_vector_shape_set_x(self, x);
103-
if (dirty) {
104-
common_hal_vectorio_vector_shape_set_dirty(self);
105-
}
102+
common_hal_vectorio_vector_shape_set_x(self, x);
106103
return mp_const_none;
107104
}
108105
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_x_obj, vectorio_vector_shape_obj_set_x);
@@ -133,10 +130,7 @@ STATIC mp_obj_t vectorio_vector_shape_obj_set_y(mp_obj_t wrapper_shape, mp_obj_t
133130
vectorio_vector_shape_t *self = MP_OBJ_TO_PTR(draw_protocol->draw_get_protocol_self(wrapper_shape));
134131

135132
mp_int_t y = mp_obj_get_int(y_obj);
136-
bool dirty = common_hal_vectorio_vector_shape_set_y(self, y);
137-
if (dirty) {
138-
common_hal_vectorio_vector_shape_set_dirty(self);
139-
}
133+
common_hal_vectorio_vector_shape_set_y(self, y);
140134
return mp_const_none;
141135
}
142136
MP_DEFINE_CONST_FUN_OBJ_2(vectorio_vector_shape_set_y_obj, vectorio_vector_shape_obj_set_y);

shared-bindings/vectorio/VectorShape.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
2121
void common_hal_vectorio_vector_shape_set_dirty(void *self);
2222

2323
mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self);
24-
bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x);
24+
void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x);
2525

2626
mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self);
2727
void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self, mp_obj_t xy);
2828

2929
mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self);
30-
bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y);
30+
void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y);
3131

3232
mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self);
3333
void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader);

shared-module/vectorio/VectorShape.c

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,10 @@ void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
166166
vectorio_ishape_t ishape,
167167
mp_obj_t pixel_shader, int32_t x, int32_t y) {
168168
VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y);
169-
common_hal_vectorio_vector_shape_set_x(self, x);
170-
common_hal_vectorio_vector_shape_set_y(self, y);
169+
vectorio_vector_shape_validate_x_bounds(x);
170+
self->x = x;
171+
vectorio_vector_shape_validate_y_bounds(y);
172+
self->y = y;
171173
self->pixel_shader = pixel_shader;
172174
self->ishape = ishape;
173175
self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area.
@@ -184,16 +186,14 @@ mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) {
184186
}
185187

186188

187-
bool common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
189+
void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
188190
VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x);
189191
if (self->x == x) {
190-
return false; // it's not dirty
191-
}
192-
if (x < SHRT_MIN || x > SHRT_MAX) {
193-
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
192+
return;
194193
}
194+
vectorio_vector_shape_validate_x_bounds(x);
195195
self->x = x;
196-
return true; // it's dirty
196+
common_hal_vectorio_vector_shape_set_dirty(self);
197197
}
198198

199199

@@ -203,16 +203,14 @@ mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) {
203203
}
204204

205205

206-
bool common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
206+
void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
207207
VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y);
208208
if (self->y == y) {
209-
return false; // it's not dirty
210-
}
211-
if (y < SHRT_MIN || y > SHRT_MAX) {
212-
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
209+
return;
213210
}
211+
vectorio_vector_shape_validate_y_bounds(y);
214212
self->y = y;
215-
return true; // it's dirty
213+
common_hal_vectorio_vector_shape_set_dirty(self);
216214
}
217215

218216
mp_obj_tuple_t *common_hal_vectorio_vector_shape_get_location(vectorio_vector_shape_t *self) {
@@ -239,14 +237,37 @@ void common_hal_vectorio_vector_shape_set_location(vectorio_vector_shape_t *self
239237
|| !mp_obj_get_int_maybe(tuple_items[ 1 ], &y)) {
240238
mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
241239
}
242-
bool dirty = common_hal_vectorio_vector_shape_set_x(self, x);
243-
dirty |= common_hal_vectorio_vector_shape_set_y(self, y);
240+
bool dirty = false;
241+
if (self->x != x) {
242+
vectorio_vector_shape_validate_x_bounds(x);
243+
self->x = x;
244+
dirty = true;
245+
}
246+
if (self->y != y) {
247+
vectorio_vector_shape_validate_y_bounds(y);
248+
self->y = y;
249+
dirty = true;
250+
}
244251
if (dirty) {
245252
common_hal_vectorio_vector_shape_set_dirty(self);
246253
}
247254
}
248255

249256

257+
void vectorio_vector_shape_validate_x_bounds(mp_int_t x) {
258+
if (x < SHRT_MIN || x > SHRT_MAX) {
259+
mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_x, SHRT_MIN, SHRT_MAX);
260+
}
261+
}
262+
263+
264+
void vectorio_vector_shape_validate_y_bounds(mp_int_t y) {
265+
if (y < SHRT_MIN || y > SHRT_MAX) {
266+
mp_raise_ValueError_varg(translate("%q must be between %d and %d"), MP_QSTR_y, SHRT_MIN, SHRT_MAX);
267+
}
268+
}
269+
270+
250271
mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self) {
251272
VECTORIO_SHAPE_DEBUG("%p get_pixel_shader\n", self);
252273
return self->pixel_shader;

shared-module/vectorio/VectorShape.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,7 @@ bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displ
5151
bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area);
5252
void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self);
5353

54+
void vectorio_vector_shape_validate_x_bounds(mp_int_t x);
55+
void vectorio_vector_shape_validate_y_bounds(mp_int_t y);
56+
5457
#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H

0 commit comments

Comments
 (0)