From 6c16a5122a94b4776094ed3755ed643230872604 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Thu, 1 Feb 2024 09:52:24 +0100 Subject: [PATCH] Berry add `introspect.contains` and `bytes.addfloat` (#20635) --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_byteslib.c | 28 +++++++++++++++++++++++ lib/libesp32/berry/src/be_introspectlib.c | 15 ++++++++++++ 3 files changed, 44 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 32e00dc50..04a9da372 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - HASPmota support for `min` and `max` attribute in `slider` (#20582) - ESP32-C3 support for GPIO11 (#18350) - ESP32 support for Shelly Plus Add-On using DS18x20 or DHT11/AM2301/DHT21/DHT22/AM2302/AM2321/SI7021 on GPIO0/1 (#20580) +- Berry add `introspect.contains` and `bytes.addfloat` ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_byteslib.c b/lib/libesp32/berry/src/be_byteslib.c index 221e3e2a4..1789dca94 100644 --- a/lib/libesp32/berry/src/be_byteslib.c +++ b/lib/libesp32/berry/src/be_byteslib.c @@ -1002,6 +1002,32 @@ static int m_setfloat(bvm *vm) be_return_nil(vm); } +/* + * Add a 32 bits float + * `addfloat(value:real or int [, big_endian:bool]) -> instance` + * + */ +static int m_addfloat(bvm *vm) +{ + int argc = be_top(vm); + buf_impl attr = bytes_check_data(vm, 4); /* we reserve 4 bytes anyways */ + check_ptr(vm, &attr); + if (attr.fixed) { be_raise(vm, BYTES_RESIZE_ERROR, BYTES_RESIZE_MESSAGE); } + if (argc >=2 && (be_isint(vm, 2) || be_isreal(vm, 2))) { + float val_f = (float) be_toreal(vm, 2); + int32_t* val_i = (int32_t*) &val_f; + bbool be = bfalse; + if (argc >= 3) { + be = be_tobool(vm, 3); + } + if (be) { buf_add4_be(&attr, *val_i); } else { buf_add4_le(&attr, *val_i); } + be_pop(vm, argc - 1); + m_write_attributes(vm, 1, &attr); /* update attributes */ + be_return(vm); + } + be_return_nil(vm); +} + /* * Fills a buffer with another buffer. * @@ -1761,6 +1787,7 @@ void be_load_byteslib(bvm *vm) { "setbytes", m_setbytes }, { "getfloat", m_getfloat }, { "setfloat", m_setfloat }, + { "addfloat", m_addfloat }, { "item", m_item }, { "setitem", m_setitem }, { "size", m_size }, @@ -1806,6 +1833,7 @@ class be_class_bytes (scope: global, name: bytes) { geti, func(m_geti) getfloat, func(m_getfloat) setfloat, func(m_setfloat) + addfloat, func(m_addfloat) set, func(m_set) seti, func(m_set) setbytes, func(m_setbytes) diff --git a/lib/libesp32/berry/src/be_introspectlib.c b/lib/libesp32/berry/src/be_introspectlib.c index ba3d60aa2..ba54aad25 100644 --- a/lib/libesp32/berry/src/be_introspectlib.c +++ b/lib/libesp32/berry/src/be_introspectlib.c @@ -93,6 +93,19 @@ static int m_findmember(bvm *vm) be_return_nil(vm); } +static int m_contains(bvm *vm) +{ + bbool contains = bfalse; + int top = be_top(vm); + if (top >= 2 && be_isstring(vm, 2) && (be_isinstance(vm, 1) || be_ismodule(vm, 1) || be_isclass(vm, 1))) { + if (be_getmember(vm, 1, be_tostring(vm, 2))) { + contains = btrue; + } + } + be_pushbool(vm, contains); + be_return(vm); +} + static int m_setmember(bvm *vm) { int top = be_top(vm); @@ -225,6 +238,7 @@ be_native_module_attr_table(introspect) { be_native_module_function("get", m_findmember), be_native_module_function("set", m_setmember), + be_native_module_function("contains", m_contains), be_native_module_function("module", m_getmodule), be_native_module_function("setmodule", m_setmodule), @@ -245,6 +259,7 @@ module introspect (scope: global, depend: BE_USE_INTROSPECT_MODULE) { get, func(m_findmember) set, func(m_setmember) + contains, func(m_contains) module, func(m_getmodule) setmodule, func(m_setmodule)