Berry add `introspect.contains` and `bytes.addfloat` (#20635)

This commit is contained in:
s-hadinger 2024-02-01 09:52:24 +01:00 committed by GitHub
parent c7a1b3f7ca
commit 6c16a5122a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 44 additions and 0 deletions

View File

@ -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

View File

@ -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)

View File

@ -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)