Berry `debug.caller` (#20470)

This commit is contained in:
s-hadinger 2024-01-12 08:29:14 +01:00 committed by GitHub
parent 2165ea549a
commit b0f4542707
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 2 deletions

View File

@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
- Berry provide lightweight options for `tasmota.wifi/eth/memory/rtc` (#20448)
- Berry `tasmota.webcolor` (#20454)
- Support for pipsolar inverter (#20408)
- Berry `debug.caller`
### Breaking Changed

View File

@ -1768,6 +1768,7 @@ void be_load_byteslib(bvm *vm)
{ "clear", m_clear },
{ "reverse", m_reverse },
{ "copy", m_copy },
{ "append", m_connect },
{ "+", m_merge },
{ "..", m_connect },
{ "==", m_equal },
@ -1815,6 +1816,7 @@ class be_class_bytes (scope: global, name: bytes) {
clear, func(m_clear)
reverse, func(m_reverse)
copy, func(m_copy)
append, func(m_connect)
+, func(m_merge)
.., func(m_connect)
==, func(m_equal)

View File

@ -13,6 +13,7 @@
#include "be_debug.h"
#include "be_map.h"
#include "be_vm.h"
#include "be_exec.h"
#include <string.h>
#if BE_USE_DEBUG_MODULE
@ -103,6 +104,26 @@ static int m_traceback(bvm *vm)
be_return_nil(vm);
}
static int m_caller(bvm *vm)
{
int depth = 1;
if (be_top(vm) >= 1 && be_isint(vm, 1)) {
depth = be_toint(vm, 1);
if (depth < 0) {
depth = -depth; /* take absolute value */
}
}
bcallframe *cf = (bcallframe*)be_stack_top(&vm->callstack) - depth;
bcallframe *base = be_stack_base(&vm->callstack);
if (cf >= base) {
bvalue *reg = be_incrtop(vm);
var_setval(reg, cf->func);
be_return(vm);
} else {
be_return_nil(vm);
}
}
#if BE_USE_DEBUG_HOOK
static int m_sethook(bvm *vm)
{
@ -236,6 +257,7 @@ be_native_module_attr_table(debug) {
be_native_module_function("varname", m_varname),
be_native_module_function("upvname", m_upvname)
#endif
be_native_module_function("caller", m_caller),
be_native_module_function("gcdebug", m_gcdebug)
};
@ -252,6 +274,7 @@ module debug (scope: global, depend: BE_USE_DEBUG_MODULE) {
top, func(m_top)
varname, func(m_varname), BE_DEBUG_VAR_INFO
upvname, func(m_upvname), BE_DEBUG_VAR_INFO
caller, func(m_caller)
// individual counters
allocs, func(m_allocs)
frees, func(m_frees)

View File

@ -110,13 +110,20 @@ assert(str(b) == "bytes('AA')")
b = b1 + '01'
assert(str(b) == "bytes('AA3031')")
#- .. -#
#- .. and append as synonym-#
b1 = bytes("1122")
b2 = bytes("334455")
b = b1..b2
assert(str(b1) == "bytes('1122334455')")
assert(str(b2) == "bytes('334455')")
assert(str(b) == "bytes('1122334455')")
#
b1 = bytes("1122")
b2 = bytes("334455")
b = b1.append(b2)
assert(str(b1) == "bytes('1122334455')")
assert(str(b2) == "bytes('334455')")
assert(str(b) == "bytes('1122334455')")
#- .. with string -#
b1 = bytes("AA")
@ -124,6 +131,12 @@ b1 .. ''
assert(str(b1) == "bytes('AA')")
b1 .. '01'
assert(str(b1) == "bytes('AA3031')")
#
b1 = bytes("AA")
b1.append('')
assert(str(b1) == "bytes('AA')")
b1.append('01')
assert(str(b1) == "bytes('AA3031')")
#- item -#
b = bytes("334455")

View File

@ -1,4 +1,29 @@
import debug
class A end
debug.attrdump(A) #- should not crash -#
debug.attrdump(A) #- should not crash -#
# debug.caller()
def caller_name_chain()
import debug
import introspect
var i = 1
var ret = []
var caller = debug.caller(i)
while caller
ret.push(introspect.name(caller))
i += 1
caller = debug.caller(i)
end
return ret
end
var chain = caller_name_chain()
assert(chain[0] == 'caller_name_chain')
def guess_my_name__()
return caller_name_chain()
end
chain = guess_my_name__()
print(chain)
assert(chain[0] == 'caller_name_chain')
assert(chain[1] == 'guess_my_name__')