From 036430ec6591e4e61f6e0537c7dc1589cff589d8 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sun, 19 Sep 2021 23:27:51 +0200 Subject: [PATCH] Berry improve `introspect.toptr()` (#13178) * Berry improve `introspect.toptr()` * Fix unwanted change --- lib/libesp32/Berry/default/be_lvgl_ctypes.c | 15 ++++++++++++--- lib/libesp32/Berry/src/be_introspectlib.c | 11 ++++++++--- lib/libesp32/Berry/src/be_strlib.c | 3 +++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/libesp32/Berry/default/be_lvgl_ctypes.c b/lib/libesp32/Berry/default/be_lvgl_ctypes.c index 1a4c3b046..119f5982c 100644 --- a/lib/libesp32/Berry/default/be_lvgl_ctypes.c +++ b/lib/libesp32/Berry/default/be_lvgl_ctypes.c @@ -6,7 +6,7 @@ #ifdef USE_LVGL #include "lvgl.h" - +extern __attribute__((noreturn)) void be_raisef(bvm *vm, const char *except, const char *msg, ...); // binary search within an array of sorted strings // the first 4 bytes are a pointer to a string @@ -259,6 +259,14 @@ int be_ctypes_setmember(bvm *vm) { be_pop(vm, 1); } + // If the value is a pointer, replace with an int of same value (works only on 32 bits CPU) + if (be_iscomptr(vm, 3)) { + void * v = be_tocomptr(vm, 3); + be_pushint(vm, (int32_t) v); + be_moveto(vm, -1, 3); + be_pop(vm, 1); + } + be_getmember(vm, 1, ".def"); const be_ctypes_structure_t *definitions; definitions = (const be_ctypes_structure_t *) be_tocomptr(vm, -1); @@ -304,9 +312,10 @@ int be_ctypes_setmember(bvm *vm) { be_pop(vm, 5); be_return_nil(vm); } + } else { + be_raisef(vm, "attribute_error", "class '%s' cannot assign to attribute '%s'", + be_classname(vm, 1), be_tostring(vm, 2)); } - - be_return_nil(vm); } BE_EXPORT_VARIABLE extern const bclass be_class_bytes; diff --git a/lib/libesp32/Berry/src/be_introspectlib.c b/lib/libesp32/Berry/src/be_introspectlib.c index dcabd309b..171c5a03f 100644 --- a/lib/libesp32/Berry/src/be_introspectlib.c +++ b/lib/libesp32/Berry/src/be_introspectlib.c @@ -84,7 +84,7 @@ static int m_toptr(bvm *vm) if (top >= 1) { bvalue *v = be_indexof(vm, 1); if (var_basetype(v) >= BE_GCOBJECT) { - be_pushint(vm, (int) var_toobj(v)); + be_pushcomptr(vm, var_toobj(v)); be_return(vm); } else { be_raise(vm, "value_error", "unsupported for this type"); @@ -97,7 +97,12 @@ static int m_fromptr(bvm *vm) { int top = be_top(vm); if (top >= 1) { - int v = be_toint(vm, 1); + void* v; + if (be_iscomptr(vm, 1)) { + v = be_tocomptr(vm, 1); + } else { + v = (void*) be_toint(vm, 1); + } if (v) { bgcobject * ptr = (bgcobject*) v; if (var_basetype(ptr) >= BE_GCOBJECT) { @@ -106,8 +111,8 @@ static int m_fromptr(bvm *vm) } else { be_raise(vm, "value_error", "unsupported for this type"); } + be_return(vm); } - be_return(vm); } be_return_nil(vm); } diff --git a/lib/libesp32/Berry/src/be_strlib.c b/lib/libesp32/Berry/src/be_strlib.c index 257bd8c1a..16cdaee9a 100644 --- a/lib/libesp32/Berry/src/be_strlib.c +++ b/lib/libesp32/Berry/src/be_strlib.c @@ -97,6 +97,9 @@ static bstring* sim2str(bvm *vm, bvalue *v) case BE_MODULE: module2str(sbuf, v); break; + case BE_COMPTR: + sprintf(sbuf, "", var_toobj(v)); + break; default: strcpy(sbuf, "(unknow value)"); break;