From efabaf3b2ba075976f122db780ea741207da55a1 Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:52:40 +0100 Subject: [PATCH] Berry add function check (#14787) --- lib/libesp32/berry/generate/be_const_strtab.h | 1 + .../berry/generate/be_const_strtab_def.h | 5 +-- .../berry_tasmota/src/be_tasmotalib.c | 32 ++++++++++++------- .../berry_tasmota/src/embedded/Tasmota.be | 3 ++ 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/lib/libesp32/berry/generate/be_const_strtab.h b/lib/libesp32/berry/generate/be_const_strtab.h index 7843fe0b8..67cb347f1 100644 --- a/lib/libesp32/berry/generate/be_const_strtab.h +++ b/lib/libesp32/berry/generate/be_const_strtab.h @@ -12,6 +12,7 @@ extern const bcstring be_const_str_AudioOutputI2S; extern const bcstring be_const_str_Auto_X2Dconfiguration; extern const bcstring be_const_str_BRY_X3A_X20ERROR_X2C_X20bad_X20json_X3A_X20; extern const bcstring be_const_str_BRY_X3A_X20Exception_X3E_X20_X27_X25s_X27_X20_X2D_X20_X25s; +extern const bcstring be_const_str_BRY_X3A_X20argument_X20must_X20be_X20a_X20function; extern const bcstring be_const_str_BRY_X3A_X20could_X20not_X20save_X20compiled_X20file_X20_X25s_X20_X28_X25s_X29; extern const bcstring be_const_str_BRY_X3A_X20failed_X20to_X20load_X20_persist_X2Ejson; extern const bcstring be_const_str_BRY_X3A_X20method_X20not_X20allowed_X2C_X20use_X20a_X20closure_X20like_X20_X27_X2F_X20args_X20_X2D_X3E_X20obj_X2Efunc_X28args_X29_X27; diff --git a/lib/libesp32/berry/generate/be_const_strtab_def.h b/lib/libesp32/berry/generate/be_const_strtab_def.h index 5932ff113..17bdadd36 100644 --- a/lib/libesp32/berry/generate/be_const_strtab_def.h +++ b/lib/libesp32/berry/generate/be_const_strtab_def.h @@ -78,6 +78,7 @@ be_define_const_str(AudioOutputI2S, "AudioOutputI2S", 638031784u, 0, 14, &be_con be_define_const_str(Auto_X2Dconfiguration, "Auto-configuration", 1665006109u, 0, 18, &be_const_str_lv_wifi_arcs_icon); be_define_const_str(BRY_X3A_X20ERROR_X2C_X20bad_X20json_X3A_X20, "BRY: ERROR, bad json: ", 2715135809u, 0, 22, NULL); be_define_const_str(BRY_X3A_X20Exception_X3E_X20_X27_X25s_X27_X20_X2D_X20_X25s, "BRY: Exception> '%s' - %s", 2246990964u, 0, 25, &be_const_str_copy); +be_define_const_str(BRY_X3A_X20argument_X20must_X20be_X20a_X20function, "BRY: argument must be a function", 3917068408u, 0, 32, &be_const_str_every_50ms); be_define_const_str(BRY_X3A_X20could_X20not_X20save_X20compiled_X20file_X20_X25s_X20_X28_X25s_X29, "BRY: could not save compiled file %s (%s)", 736659787u, 0, 41, NULL); be_define_const_str(BRY_X3A_X20failed_X20to_X20load_X20_persist_X2Ejson, "BRY: failed to load _persist.json", 2991913445u, 0, 33, &be_const_str_light_state); be_define_const_str(BRY_X3A_X20method_X20not_X20allowed_X2C_X20use_X20a_X20closure_X20like_X20_X27_X2F_X20args_X20_X2D_X3E_X20obj_X2Efunc_X28args_X29_X27, "BRY: method not allowed, use a closure like '/ args -> obj.func(args)'", 177121572u, 0, 70, &be_const_str_set_x); @@ -831,7 +832,7 @@ static const bstring* const m_string_table[] = { (const bstring *)&be_const_str_set_time, NULL, (const bstring *)&be_const_str_upper, - (const bstring *)&be_const_str_every_50ms, + (const bstring *)&be_const_str_BRY_X3A_X20argument_X20must_X20be_X20a_X20function, NULL, (const bstring *)&be_const_str_hex, NULL, @@ -1227,6 +1228,6 @@ static const bstring* const m_string_table[] = { static const struct bconststrtab m_const_string_table = { .size = 400, - .count = 823, + .count = 824, .table = m_string_table }; diff --git a/lib/libesp32/berry_tasmota/src/be_tasmotalib.c b/lib/libesp32/berry_tasmota/src/be_tasmotalib.c index 9d1ac2fd6..413273790 100644 --- a/lib/libesp32/berry_tasmota/src/be_tasmotalib.c +++ b/lib/libesp32/berry_tasmota/src/be_tasmotalib.c @@ -2103,24 +2103,32 @@ be_local_closure(Tasmota_check_not_method, /* name */ 0, /* has sup protos */ NULL, /* no sub protos */ 1, /* has constants */ - ( &(const bvalue[ 4]) { /* constants */ + ( &(const bvalue[ 6]) { /* constants */ /* K0 */ be_nested_str(introspect), - /* K1 */ be_nested_str(ismethod), + /* K1 */ be_nested_str(function), /* K2 */ be_nested_str(type_error), - /* K3 */ be_nested_str(BRY_X3A_X20method_X20not_X20allowed_X2C_X20use_X20a_X20closure_X20like_X20_X27_X2F_X20args_X20_X2D_X3E_X20obj_X2Efunc_X28args_X29_X27), + /* K3 */ be_nested_str(BRY_X3A_X20argument_X20must_X20be_X20a_X20function), + /* K4 */ be_nested_str(ismethod), + /* K5 */ be_nested_str(BRY_X3A_X20method_X20not_X20allowed_X2C_X20use_X20a_X20closure_X20like_X20_X27_X2F_X20args_X20_X2D_X3E_X20obj_X2Efunc_X28args_X29_X27), }), &be_const_str_check_not_method, &be_const_str_solidified, - ( &(const binstruction[ 9]) { /* code */ + ( &(const binstruction[15]) { /* code */ 0xA40A0000, // 0000 IMPORT R2 K0 - 0x8C0C0501, // 0001 GETMET R3 R2 K1 - 0x5C140200, // 0002 MOVE R5 R1 - 0x7C0C0400, // 0003 CALL R3 2 - 0x50100200, // 0004 LDBOOL R4 1 0 - 0x1C0C0604, // 0005 EQ R3 R3 R4 - 0x780E0000, // 0006 JMPF R3 #0008 - 0xB0060503, // 0007 RAISE 1 K2 K3 - 0x80000000, // 0008 RET 0 + 0x600C0004, // 0001 GETGBL R3 G4 + 0x5C100200, // 0002 MOVE R4 R1 + 0x7C0C0200, // 0003 CALL R3 1 + 0x200C0701, // 0004 NE R3 R3 K1 + 0x780E0000, // 0005 JMPF R3 #0007 + 0xB0060503, // 0006 RAISE 1 K2 K3 + 0x8C0C0504, // 0007 GETMET R3 R2 K4 + 0x5C140200, // 0008 MOVE R5 R1 + 0x7C0C0400, // 0009 CALL R3 2 + 0x50100200, // 000A LDBOOL R4 1 0 + 0x1C0C0604, // 000B EQ R3 R3 R4 + 0x780E0000, // 000C JMPF R3 #000E + 0xB0060505, // 000D RAISE 1 K2 K5 + 0x80000000, // 000E RET 0 }) ) ); diff --git a/lib/libesp32/berry_tasmota/src/embedded/Tasmota.be b/lib/libesp32/berry_tasmota/src/embedded/Tasmota.be index 1eb1f1d34..63a62d6a7 100644 --- a/lib/libesp32/berry_tasmota/src/embedded/Tasmota.be +++ b/lib/libesp32/berry_tasmota/src/embedded/Tasmota.be @@ -50,6 +50,9 @@ class Tasmota # check that the parameter is not a method, it would require a closure instead def check_not_method(f) import introspect + if type(f) != 'function' + raise "type_error", "BRY: argument must be a function" + end if introspect.ismethod(f) == true raise "type_error", "BRY: method not allowed, use a closure like '/ args -> obj.func(args)'" end