From e910f3071c6c03df41cb51eb7804ece1b7d4586c Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Sun, 6 Nov 2022 10:52:17 +0100 Subject: [PATCH] Berry add ``dyn`` class --- CHANGELOG.md | 1 + lib/libesp32/berry/default/be_modtab.c | 2 + lib/libesp32/berry_tasmota/src/be_dyn_class.c | 4 + .../berry_tasmota/src/embedded/dyn.be | 27 +++ .../src/solidify/solidified_dyn.h | 157 ++++++++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 lib/libesp32/berry_tasmota/src/be_dyn_class.c create mode 100644 lib/libesp32/berry_tasmota/src/embedded/dyn.be create mode 100644 lib/libesp32/berry_tasmota/src/solidify/solidified_dyn.h diff --git a/CHANGELOG.md b/CHANGELOG.md index d5c140e23..52a1b30b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. - ESP32 Support for DMX ArtNet Led matrix animations (#16984) - Command ``SetOption47 1..255`` to delay power on relay state in seconds reducing power surge. ``SO47 1`` delays until network connected. ``SO47 2`` delays until mqtt connected - ESP32 DMX ArtNet optimization to avoid any object allocation and avoid garbage collector pauses +- Berry add ``dyn`` class ### Breaking Changed diff --git a/lib/libesp32/berry/default/be_modtab.c b/lib/libesp32/berry/default/be_modtab.c index 1cc28efee..5b018304e 100644 --- a/lib/libesp32/berry/default/be_modtab.c +++ b/lib/libesp32/berry/default/be_modtab.c @@ -178,6 +178,7 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = { NULL /* do not remove */ }; +be_extern_native_class(dyn); be_extern_native_class(tasmota); be_extern_native_class(Trigger); be_extern_native_class(Driver); @@ -228,6 +229,7 @@ be_extern_native_class(int64); BERRY_LOCAL bclass_array be_class_table = { #ifdef TASMOTA /* first list are direct classes */ + &be_native_class(dyn), &be_native_class(tasmota), &be_native_class(Trigger), &be_native_class(Driver), diff --git a/lib/libesp32/berry_tasmota/src/be_dyn_class.c b/lib/libesp32/berry_tasmota/src/be_dyn_class.c new file mode 100644 index 000000000..b41ac1b29 --- /dev/null +++ b/lib/libesp32/berry_tasmota/src/be_dyn_class.c @@ -0,0 +1,4 @@ +/******************************************************************** + * Tasmota dyn class + *******************************************************************/ +#include "solidify/solidified_dyn.h" diff --git a/lib/libesp32/berry_tasmota/src/embedded/dyn.be b/lib/libesp32/berry_tasmota/src/embedded/dyn.be new file mode 100644 index 000000000..5266304ba --- /dev/null +++ b/lib/libesp32/berry_tasmota/src/embedded/dyn.be @@ -0,0 +1,27 @@ +################################################################################# +# dyn class +# +# Allows to use a map with members +# see https://github.com/berry-lang/berry/wiki/Chapter-8 +################################################################################# +#@ solidify:dyn +class dyn + var _attr + def init() + self._attr = {} + end + def setmember(name, value) + self._attr[name] = value + end + def member(name) + if self._attr.contains(name) + return self._attr[name] + else + import undefined + return undefined + end + end + def tostring() + return self._attr.tostring() + end +end diff --git a/lib/libesp32/berry_tasmota/src/solidify/solidified_dyn.h b/lib/libesp32/berry_tasmota/src/solidify/solidified_dyn.h new file mode 100644 index 000000000..ccb1c98e5 --- /dev/null +++ b/lib/libesp32/berry_tasmota/src/solidify/solidified_dyn.h @@ -0,0 +1,157 @@ +/* Solidification of dyn.h */ +/********************************************************************\ +* Generated code, don't edit * +\********************************************************************/ +#include "be_constobj.h" + +/******************************************************************** +** Solidified function: tostring +********************************************************************/ +be_local_closure(dyn_tostring, /* name */ + be_nested_proto( + 3, /* nstack */ + 1, /* argc */ + 2, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 2]) { /* constants */ + /* K0 */ be_nested_str(_attr), + /* K1 */ be_nested_str(tostring), + }), + &be_const_str_tostring, + &be_const_str_solidified, + ( &(const binstruction[ 4]) { /* code */ + 0x88040100, // 0000 GETMBR R1 R0 K0 + 0x8C040301, // 0001 GETMET R1 R1 K1 + 0x7C040200, // 0002 CALL R1 1 + 0x80040200, // 0003 RET 1 R1 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: member +********************************************************************/ +be_local_closure(dyn_member, /* name */ + be_nested_proto( + 5, /* nstack */ + 2, /* argc */ + 2, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 3]) { /* constants */ + /* K0 */ be_nested_str(_attr), + /* K1 */ be_nested_str(contains), + /* K2 */ be_nested_str(undefined), + }), + &be_const_str_member, + &be_const_str_solidified, + ( &(const binstruction[12]) { /* code */ + 0x88080100, // 0000 GETMBR R2 R0 K0 + 0x8C080501, // 0001 GETMET R2 R2 K1 + 0x5C100200, // 0002 MOVE R4 R1 + 0x7C080400, // 0003 CALL R2 2 + 0x780A0003, // 0004 JMPF R2 #0009 + 0x88080100, // 0005 GETMBR R2 R0 K0 + 0x94080401, // 0006 GETIDX R2 R2 R1 + 0x80040400, // 0007 RET 1 R2 + 0x70020001, // 0008 JMP #000B + 0xA40A0400, // 0009 IMPORT R2 K2 + 0x80040400, // 000A RET 1 R2 + 0x80000000, // 000B RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: setmember +********************************************************************/ +be_local_closure(dyn_setmember, /* name */ + be_nested_proto( + 4, /* nstack */ + 3, /* argc */ + 2, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 1]) { /* constants */ + /* K0 */ be_nested_str(_attr), + }), + &be_const_str_setmember, + &be_const_str_solidified, + ( &(const binstruction[ 3]) { /* code */ + 0x880C0100, // 0000 GETMBR R3 R0 K0 + 0x980C0202, // 0001 SETIDX R3 R1 R2 + 0x80000000, // 0002 RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: init +********************************************************************/ +be_local_closure(dyn_init, /* name */ + be_nested_proto( + 2, /* nstack */ + 1, /* argc */ + 2, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 1]) { /* constants */ + /* K0 */ be_nested_str(_attr), + }), + &be_const_str_init, + &be_const_str_solidified, + ( &(const binstruction[ 4]) { /* code */ + 0x60040013, // 0000 GETGBL R1 G19 + 0x7C040000, // 0001 CALL R1 0 + 0x90020001, // 0002 SETMBR R0 K0 R1 + 0x80000000, // 0003 RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified class: dyn +********************************************************************/ +be_local_class(dyn, + 1, + NULL, + be_nested_map(5, + ( (struct bmapnode*) &(const bmapnode[]) { + { be_const_key(tostring, 2), be_const_closure(dyn_tostring_closure) }, + { be_const_key(member, 3), be_const_closure(dyn_member_closure) }, + { be_const_key(init, 4), be_const_closure(dyn_init_closure) }, + { be_const_key(setmember, -1), be_const_closure(dyn_setmember_closure) }, + { be_const_key(_attr, -1), be_const_var(0) }, + })), + (bstring*) &be_const_str_dyn +); +/*******************************************************************/ + +void be_load_dyn_class(bvm *vm) { + be_pushntvclass(vm, &be_class_dyn); + be_setglobal(vm, "dyn"); + be_pop(vm, 1); +} +/********************************************************************/ +/* End of solidification */